diff --git a/Base/etc/SystemServer.ini b/Base/etc/SystemServer.ini index e07c1804cd..db7d379185 100644 --- a/Base/etc/SystemServer.ini +++ b/Base/etc/SystemServer.ini @@ -70,3 +70,4 @@ User=anon [Terminal] User=anon +WorkingDirectory=/home/anon diff --git a/Base/usr/share/man/man5/SystemServer.md b/Base/usr/share/man/man5/SystemServer.md index 024ce1bdc1..9bc103d891 100644 --- a/Base/usr/share/man/man5/SystemServer.md +++ b/Base/usr/share/man/man5/SystemServer.md @@ -25,6 +25,7 @@ describing how to launch and manage this service. * `Socket` - a path to a socket to create on behalf of the service. For lazy services, SystemServer will actually watch the socket for new connection attempts. An open file descriptor to this socket will be passed as fd 3 to the service. * `SocketPermissions` - (octal) file system permissions for the socket file. The default permissions are 0600. * `User` - a name of the user to run the service as. This impacts what UID, GID (and extra GIDs) the service processes have. By default, services are run as root. +* `WorkingDirectory` - The working directory in which the service is spawned. By Default, services are spawned in the root (`"/"`) directory. ## Environment diff --git a/Servers/SystemServer/Service.cpp b/Servers/SystemServer/Service.cpp index 6999c14d99..7c7331a80f 100644 --- a/Servers/SystemServer/Service.cpp +++ b/Servers/SystemServer/Service.cpp @@ -188,6 +188,13 @@ void Service::spawn() } else if (m_pid == 0) { // We are the child. + if (!m_working_directory.is_null()) { + if (chdir(m_working_directory.characters()) < 0) { + perror("chdir"); + ASSERT_NOT_REACHED(); + } + } + struct sched_param p; p.sched_priority = m_priority; int rc = sched_setparam(0, &p); @@ -320,6 +327,8 @@ Service::Service(const Core::ConfigFile& config, const StringView& name) m_socket_permissions = strtol(socket_permissions_string.characters(), nullptr, 8) & 04777; setup_socket(); } + + m_working_directory = config.read_entry(name, "WorkingDirectory"); } void Service::save_to(JsonObject& json) @@ -352,4 +361,5 @@ void Service::save_to(JsonObject& json) json.set("pid", nullptr); json.set("restart_attempts", m_restart_attempts); + json.set("working_directory", m_working_directory); } diff --git a/Servers/SystemServer/Service.h b/Servers/SystemServer/Service.h index 587fc805f5..de408ec04d 100644 --- a/Servers/SystemServer/Service.h +++ b/Servers/SystemServer/Service.h @@ -81,6 +81,9 @@ private: // times where it has exited unsuccessfully and too quickly. int m_restart_attempts { 0 }; + // The working directory in which to spawn the service + String m_working_directory; + void resolve_user(); void setup_socket(); void setup_notifier();