mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:27:43 +00:00
SystemServer+LibCore: Allow service to request multiple sockets
SystemServer only allowed a single socket to be created for a service before this. Now, SystemServer will allow any amount of sockets. The sockets can be defined like so: [SomeService] Socket=/tmp/portal/socket1,/tmp/portal/socket2,/tmp/portal/socket3 SocketPermissions=660,600 The last item in SocketPermissions is applied to the remainder of the sockets in the Socket= line, so multiple sockets can have the same permissions without having to repeat them. Defining multiple sockets is not allowed for socket-activated services at the moment, and wouldn't make much sense anyway. This patch also makes socket takeovers more robust by removing the assumption that the socket will always be passed in fd 3. Now, the SOCKET_TAKEOVER environment variable carries information about which endpoint corresponds to which socket, like so: SOCKET_TAKEOVER=/tmp/portal/socket1:3 /tmp/portal/socket2:4 and LocalServer/LocalService will parse this automatically and select the correct one. The old behavior of getting the default socket is preserved so long as the service only requests a single socket in SystemServer.ini.
This commit is contained in:
parent
4316e817db
commit
5b95850e28
6 changed files with 178 additions and 65 deletions
|
@ -50,28 +50,39 @@ LocalServer::~LocalServer()
|
|||
::close(m_fd);
|
||||
}
|
||||
|
||||
bool LocalServer::take_over_from_system_server()
|
||||
bool LocalServer::take_over_from_system_server(String const& socket_path)
|
||||
{
|
||||
if (m_listening)
|
||||
return false;
|
||||
|
||||
constexpr auto socket_takeover = "SOCKET_TAKEOVER";
|
||||
if (!LocalSocket::s_overtaken_sockets_parsed)
|
||||
LocalSocket::parse_sockets_from_system_server();
|
||||
|
||||
if (getenv(socket_takeover)) {
|
||||
int fd = -1;
|
||||
if (socket_path.is_null()) {
|
||||
// We want the first (and only) socket.
|
||||
if (LocalSocket::s_overtaken_sockets.size() == 1) {
|
||||
fd = LocalSocket::s_overtaken_sockets.begin()->value;
|
||||
}
|
||||
} else {
|
||||
auto it = LocalSocket::s_overtaken_sockets.find(socket_path);
|
||||
if (it != LocalSocket::s_overtaken_sockets.end()) {
|
||||
fd = it->value;
|
||||
}
|
||||
}
|
||||
|
||||
if (fd >= 0) {
|
||||
// Sanity check: it has to be a socket.
|
||||
struct stat stat;
|
||||
int rc = fstat(3, &stat);
|
||||
int rc = fstat(fd, &stat);
|
||||
if (rc == 0 && S_ISSOCK(stat.st_mode)) {
|
||||
// The SystemServer has passed us the socket as fd 3,
|
||||
// so use that instead of creating our own.
|
||||
m_fd = 3;
|
||||
// The SystemServer has passed us the socket, so use that instead of
|
||||
// creating our own.
|
||||
m_fd = fd;
|
||||
// It had to be !CLOEXEC for obvious reasons, but we
|
||||
// don't need it to be !CLOEXEC anymore, so set the
|
||||
// CLOEXEC flag now.
|
||||
fcntl(m_fd, F_SETFD, FD_CLOEXEC);
|
||||
// We wouldn't want our children to think we're passing
|
||||
// them a socket either, so unset the env variable.
|
||||
unsetenv(socket_takeover);
|
||||
|
||||
m_listening = true;
|
||||
setup_notifier();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue