Merge authors: Stéphane Graber (stgraber) Related merge proposals: https://code.launchpad.net/~stgraber/upstart/upstart-make-event-bridge-usable/+merge/144399 proposed by: Stéphane Graber (stgraber) review: Approve - James Hunt (jamesodhunt) ------------------------------------------------------------ revno: 1430 [merge] committer: James Hunt <[email protected]> branch nick: upstart timestamp: Thu 2013-01-24 08:54:18 +0000 message: * Merge of lp:~stgraber/upstart/upstart-make-event-bridge-usable. modified: extra/conf/upstart-event-bridge.conf extra/upstart-event-bridge.c
-- lp:upstart https://code.launchpad.net/~upstart-devel/upstart/trunk Your team Upstart Reviewers is subscribed to branch lp:upstart. To unsubscribe from this branch go to https://code.launchpad.net/~upstart-devel/upstart/trunk/+edit-subscription
=== modified file 'extra/conf/upstart-event-bridge.conf' --- extra/conf/upstart-event-bridge.conf 2013-01-10 18:44:12 +0000 +++ extra/conf/upstart-event-bridge.conf 2013-01-22 20:08:29 +0000 @@ -10,7 +10,6 @@ start on started dbus stop on stopped dbus -expect daemon respawn -exec upstart-event-bridge --daemon +exec upstart-event-bridge === modified file 'extra/upstart-event-bridge.c' --- extra/upstart-event-bridge.c 2012-12-19 17:02:33 +0000 +++ extra/upstart-event-bridge.c 2013-01-23 19:18:43 +0000 @@ -1,6 +1,6 @@ /* upstart * - * Copyright © 2012 Canonical Ltd. + * Copyright © 2012-2013 Canonical Ltd. * Author: Stéphane Graber <[email protected]> * * This program is free software; you can redistribute it and/or modify @@ -67,11 +67,11 @@ static NihDBusProxy *system_upstart = NULL; /** - * session_upstart: + * user_upstart: * - * Proxy to session Upstart daemon. + * Proxy to user Upstart daemon instance. **/ -static NihDBusProxy *session_upstart = NULL; +static NihDBusProxy *user_upstart = NULL; /** * options: @@ -92,13 +92,18 @@ { char ** args; DBusConnection * system_connection; - DBusConnection * session_connection; + DBusConnection * user_connection; int ret; + char * pidfile_path = NULL; + char * pidfile = NULL; + char * user_session_addr = NULL; + nih_local char ** user_session_path = NULL; + char * path_element = NULL; nih_main_init (argv[0]); - nih_option_set_synopsis (_("Bridge system upstart events into session upstart")); + nih_option_set_synopsis (_("Bridge system upstart events into the user session upstart")); nih_option_set_help ( _("By default, upstart-event-bridge does not detach from the " "console and remains in the foreground. Use the --daemon " @@ -108,6 +113,12 @@ if (! args) exit (1); + user_session_addr = getenv ("UPSTART_SESSION"); + if (! user_session_addr) { + nih_fatal (_("UPSTART_SESSION isn't set in environment")); + exit (1); + } + /* Initialise the connection to system Upstart */ system_connection = NIH_SHOULD (nih_dbus_bus (DBUS_BUS_SYSTEM, upstart_disconnected)); @@ -160,24 +171,24 @@ exit (1); } - /* Initialise the connection to session Upstart */ - session_connection = nih_dbus_bus (DBUS_BUS_SESSION, upstart_disconnected); + /* Initialise the connection to user session Upstart */ + user_connection = NIH_SHOULD (nih_dbus_connect (user_session_addr, upstart_disconnected)); - if (! session_connection) { + if (! user_connection) { NihError *err; err = nih_error_get (); - nih_fatal ("%s: %s", _("Could not connect to session Upstart"), + nih_fatal ("%s: %s", _("Could not connect to the user session Upstart"), err->message); nih_free (err); exit (1); } - session_upstart = NIH_SHOULD (nih_dbus_proxy_new (NULL, session_connection, - DBUS_SERVICE_UPSTART, DBUS_PATH_UPSTART, + user_upstart = NIH_SHOULD (nih_dbus_proxy_new (NULL, user_connection, + NULL, DBUS_PATH_UPSTART, NULL, NULL)); - if (! session_upstart) { + if (! user_upstart) { NihError *err; err = nih_error_get (); @@ -190,6 +201,32 @@ /* Become daemon */ if (daemonise) { + /* Deal with the pidfile location when becoming a daemon. + * We need to be able to run one bridge per upstart daemon. + * Store the PID file in XDG_RUNTIME_DIR or HOME and include the pid of + * the Upstart instance (last part of the DBus path) in the filename. + */ + + /* Extract PID from UPSTART_SESSION */ + user_session_path = nih_str_split (NULL, user_session_addr, "/", TRUE); + for (int i = 0; user_session_path[i] != NULL; i++) + path_element = user_session_path[i]; + + if (! path_element) { + nih_fatal (_("Invalid value for UPSTART_SESSION")); + exit (1); + } + + pidfile_path = getenv ("XDG_RUNTIME_DIR"); + if (!pidfile_path) + pidfile_path = getenv ("HOME"); + + if (pidfile_path) { + NIH_MUST (nih_strcat_sprintf (&pidfile, NULL, "%s/upstart-event-bridge.%s.pid", + pidfile_path, path_element)); + nih_main_set_pidfile (pidfile); + } + if (nih_main_daemonise () < 0) { NihError *err; @@ -213,6 +250,11 @@ ret = nih_main_loop (); + /* Destroy any PID file we may have created */ + if (daemonise) { + nih_main_unlink_pidfile(); + } + return ret; } @@ -253,7 +295,7 @@ NIH_MUST (nih_strcat_sprintf (&new_event_name, NULL, ":sys:%s", event_name)); /* Re-transmit the event */ - pending_call = upstart_emit_event (session_upstart, + pending_call = upstart_emit_event (user_upstart, new_event_name, event_env, FALSE, NULL, emit_event_error, NULL, NIH_DBUS_TIMEOUT_NEVER); @@ -277,7 +319,7 @@ DBusPendingCall * pending_call; /* Re-transmit the event */ - pending_call = upstart_emit_event (session_upstart, + pending_call = upstart_emit_event (user_upstart, ":sys:restarted", NULL, FALSE, NULL, emit_event_error, NULL, NIH_DBUS_TIMEOUT_NEVER);
-- upstart-devel mailing list [email protected] Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/upstart-devel
