daemonize the server

This commit is contained in:
Chris Cromer 2020-08-05 17:52:39 -04:00
parent f72d65e6cb
commit f36f64b453
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
4 changed files with 86 additions and 4 deletions

21
contrib/openrc/tufmanager Normal file
View File

@ -0,0 +1,21 @@
#!@SBINDIR@/openrc-run
# Copyright 2020 Chris Cromer
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
command=@PKG_PREFIX@/libexec/tuf-manager/tuf-server
pidfile=/var/run/tufmanager/tufmanager.pid
name="TUF Manager daemon"
depend()
{
need dbus
}

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 ();
}
}
}