1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 02:48:11 +00:00

SystemServer: Read service list from a config file

This replaces the hardcoded services list with a very simple config
file in /etc/SystemServer.ini :^)

Closes https://github.com/SerenityOS/serenity/issues/610
This commit is contained in:
Sergey Bugaev 2019-11-26 19:05:09 +03:00 committed by Andreas Kling
parent 2f9be662ef
commit b93065359e
5 changed files with 239 additions and 71 deletions

View file

@ -1,11 +1,11 @@
#include "Service.h"
#include <AK/Assertions.h>
#include <LibCore/CConfigFile.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CFile.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
@ -18,52 +18,6 @@ void sigchld_handler(int)
dbg() << "reaped pid " << pid;
}
void start_process(const String& program, const Vector<String>& arguments, int prio, const char* tty = nullptr)
{
pid_t pid = 0;
while (true) {
dbg() << "Forking for " << program << "...";
int pid = fork();
if (pid < 0) {
dbg() << "Fork " << program << " failed! " << strerror(errno);
continue;
} else if (pid > 0) {
// parent...
dbg() << "Process " << program << " hopefully started with priority " << prio << "...";
return;
}
break;
}
while (true) {
dbg() << "Executing for " << program << "... at prio " << prio;
struct sched_param p;
p.sched_priority = prio;
int ret = sched_setparam(pid, &p);
ASSERT(ret == 0);
if (tty != nullptr) {
close(0);
ASSERT(open(tty, O_RDWR) == 0);
dup2(0, 1);
dup2(0, 2);
}
char* progv[256];
progv[0] = const_cast<char*>(program.characters());
for (int i = 0; i < arguments.size() && i < 254; i++)
progv[i + 1] = const_cast<char*>(arguments[i].characters());
progv[arguments.size() + 1] = nullptr;
ret = execv(progv[0], progv);
if (ret < 0) {
dbg() << "Exec " << progv[0] << " failed! " << strerror(errno);
continue;
}
break;
}
}
static void check_for_test_mode()
{
auto f = CFile::construct("/proc/cmdline");
@ -88,35 +42,42 @@ static void check_for_test_mode()
}
}
static void mount_all_filesystems()
{
dbg() << "Spawning mount -a to mount all filesystems.";
pid_t pid = fork();
if (pid < 0) {
perror("fork");
ASSERT_NOT_REACHED();
} else if (pid == 0) {
execl("/bin/mount", "mount", "-a", nullptr);
perror("exec");
ASSERT_NOT_REACHED();
} else {
wait(nullptr);
}
}
int main(int, char**)
{
int lowest_prio = sched_get_priority_min(SCHED_OTHER);
int highest_prio = sched_get_priority_max(SCHED_OTHER);
// Mount the filesystems.
start_process("/bin/mount", { "-a" }, highest_prio);
wait(nullptr);
// NOTE: We don't start anything on tty0 since that's the "active" TTY while WindowServer is up.
start_process("/bin/TTYServer", { "tty1" }, highest_prio, "/dev/tty1");
// Drop privileges.
setgid(100);
setuid(100);
mount_all_filesystems();
signal(SIGCHLD, sigchld_handler);
start_process("/bin/ProtocolServer", {}, lowest_prio);
start_process("/bin/LookupServer", {}, lowest_prio);
start_process("/bin/WindowServer", {}, highest_prio);
start_process("/bin/AudioServer", {}, highest_prio);
start_process("/bin/Taskbar", {}, highest_prio);
start_process("/bin/Terminal", {}, highest_prio - 1);
CEventLoop event_loop;
// Read our config and instantiate services.
Vector<RefPtr<Service>> services;
auto config = CConfigFile::get_for_system("SystemServer");
for (auto name : config->groups())
services.append(Service::construct(*config, name));
for (auto& service : services)
service->spawn();
// This won't return if we're in test mode.
check_for_test_mode();
while (1) {
sleep(3600);
}
return event_loop.exec();
}