1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:27:35 +00:00

SystemServer: Implement keepalive

When reaping a child, SystemServer will now match up child's pid with its own
record of the services, and respawn the service if keepalive is enabled for it.

For example, we want to restart the WindowServer if it crashes, but we wouldn't
want to restart the Terminal if it gets closed.
This commit is contained in:
Sergey Bugaev 2019-11-26 19:19:59 +03:00 committed by Andreas Kling
parent b93065359e
commit 396ad4d6b2
4 changed files with 64 additions and 4 deletions

View file

@ -16,6 +16,7 @@ struct UidAndGid {
};
static HashMap<String, UidAndGid>* s_user_map;
static HashMap<pid_t, Service*> s_service_map;
void Service::resolve_user()
{
@ -35,6 +36,14 @@ void Service::resolve_user()
m_gid = user.value().gid;
}
Service* Service::find_by_pid(pid_t pid)
{
auto it = s_service_map.find(pid);
if (it == s_service_map.end())
return nullptr;
return (*it).value;
}
void Service::spawn()
{
dbg() << "Spawning " << name();
@ -81,9 +90,26 @@ void Service::spawn()
rc = execv(argv[0], argv);
perror("exec");
ASSERT_NOT_REACHED();
} else {
// We are the parent.
s_service_map.set(m_pid, this);
}
}
void Service::did_exit(int exit_code)
{
ASSERT(m_pid > 0);
(void)exit_code;
dbg() << "Service " << name() << " has exited";
s_service_map.remove(m_pid);
m_pid = -1;
if (m_keep_alive)
spawn();
}
Service::Service(const CConfigFile& config, const StringView& name)
: CObject(nullptr)
{
@ -106,6 +132,8 @@ Service::Service(const CConfigFile& config, const StringView& name)
else
ASSERT_NOT_REACHED();
m_keep_alive = config.read_bool_entry(name, "KeepAlive");
m_user = config.read_entry(name, "User");
if (!m_user.is_null())
resolve_user();
@ -127,6 +155,7 @@ void Service::save_to(JsonObject& json)
json.set("stdio_file_path", m_stdio_file_path);
json.set("priority", m_priority);
json.set("keep_alive", m_keep_alive);
json.set("user", m_user);
json.set("uid", m_uid);
json.set("gid", m_gid);