diff --git a/contrib/openrc/tufmanager b/contrib/openrc/tufmanager new file mode 100644 index 0000000..d570daa --- /dev/null +++ b/contrib/openrc/tufmanager @@ -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 +} diff --git a/src/meson.build b/src/meson.build index 7ea81e2..c2edcc2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -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 diff --git a/src/server-main.vala b/src/server-main.vala index 797d304..a60eaa2 100644 --- a/src/server-main.vala +++ b/src/server-main.vala @@ -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; } } diff --git a/src/server.vala b/src/server.vala index c5951c3..a909853 100644 --- a/src/server.vala +++ b/src/server.vala @@ -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 (); + } } }