daemonize the server

This commit is contained in:
2020-08-05 17:52:39 -04:00
parent f72d65e6cb
commit f36f64b453
4 changed files with 86 additions and 4 deletions

View File

@@ -1,7 +1,9 @@
server_dependencies = [
dependency('glib-2.0'),
dependency('gio-unix-2.0'),
dependency('gtk+-3.0', version: '>=3.0.0'),
meson.get_compiler('c').find_library('m', required: true)
meson.get_compiler('c').find_library('m', required: true),
meson.get_compiler('vala').find_library('posix')
]
# if not always authenticated then polkit will be used for authentication

View File

@@ -21,6 +21,19 @@ namespace TUFManager {
* and handling root related tasks
*/
namespace Server {
private static bool foreground = false;
private const OptionEntry[] options = {
{ "foreground", 'f', 0, OptionArg.NONE, ref foreground, N_ ("Run the daemon in the foreground"), null },
{ null }
};
private static void on_exit (int signum) {
if (loop != null) {
loop.quit ();
}
}
/**
* The entry point to the server launches a system dbus daemon
*
@@ -28,16 +41,60 @@ namespace TUFManager {
* @return Returns 0 on success
*/
public static int main (string[] args) {
Intl.setlocale (LocaleCategory.ALL, "");
Intl.bindtextdomain (GETTEXT_PACKAGE, "/usr/share/locale");
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (GETTEXT_PACKAGE);
try {
var opt_context = new OptionContext ("");
opt_context.set_translation_domain (GETTEXT_PACKAGE);
opt_context.set_help_enabled (true);
opt_context.add_main_entries (options, null);
opt_context.parse (ref args);
}
catch (OptionError e) {
print (_ ("Error: %s\n"), e.message);
print (_ ("Run '%s --help' to see a full list of available command line options.\n"), args[0]);
return 1;
}
if (!foreground) {
var pid = Posix.fork ();
if (pid < 0) {
Posix.exit (Posix.EXIT_FAILURE);
}
else if (pid > 0) {
Posix.exit (Posix.EXIT_SUCCESS);
}
Posix.umask (0);
var sid = Posix.setsid ();
if (sid < 0) {
Posix.exit (Posix.EXIT_FAILURE);
}
if (Posix.chdir ("/") < 0) {
Posix.exit (Posix.EXIT_FAILURE);
}
}
Process.signal(ProcessSignal.INT, on_exit);
Process.signal(ProcessSignal.TERM, on_exit);
Bus.own_name (BusType.SYSTEM,
"org.tuf.manager.server",
BusNameOwnerFlags.NONE,
on_bus_acquired,
() => {},
() => {
stderr.printf ("Could not acquire name\n");
stderr.printf (_ ("Could not acquire bus name\n"));
});
loop = new MainLoop ();
loop.run ();
return 0;
}
}

View File

@@ -28,7 +28,7 @@ namespace TUFManager {
/**
* The loop of the dbus daemon running in the background
*/
MainLoop loop;
MainLoop? loop = null;
/**
* Register the bus after the name has been aquired
@@ -47,7 +47,9 @@ namespace TUFManager {
}
catch (IOError e) {
stderr.printf ("Could not register service\n");
loop.quit ();
if (loop != null) {
loop.quit ();
}
}
}