Merge authors:
  Stéphane Graber (stgraber)
Related merge proposals:
  
https://code.launchpad.net/~stgraber/upstart/upstart-session-socket/+merge/143344
  proposed by: Stéphane Graber (stgraber)
  review: Approve - James Hunt (jamesodhunt)
------------------------------------------------------------
revno: 1427 [merge]
committer: James Hunt <[email protected]>
branch nick: upstart
timestamp: Wed 2013-01-23 11:48:06 +0000
message:
  * Merge of lp:~stgraber/upstart/upstart-session-socket.
modified:
  init/control.c
  init/job_process.c
  init/main.c
  init/man/init.5
  init/state.c
  util/initctl.c
  util/man/initctl.8
  util/tests/test_initctl.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 'init/control.c'
--- init/control.c	2012-12-17 11:45:28 +0000
+++ init/control.c	2013-01-22 15:51:01 +0000
@@ -76,11 +76,18 @@
 int use_session_bus = FALSE;
 
 /**
+ * user_mode:
+ *
+ * If TRUE, upstart runs in user session mode.
+ **/
+int user_mode = FALSE;
+
+/**
  * control_server_address:
  *
  * Address on which the control server may be reached.
  **/
-const char *control_server_address = DBUS_ADDRESS_UPSTART;
+char *control_server_address = NULL;
 
 /**
  * control_server:
@@ -116,6 +123,14 @@
 {
 	if (! control_conns)
 		control_conns = NIH_MUST (nih_list_new (NULL));
+
+	if (! control_server_address) {
+		if (user_mode)
+			NIH_MUST (nih_strcat_sprintf (&control_server_address, NULL,
+					    "%s-session/%d/%d", DBUS_ADDRESS_UPSTART, getuid (), getpid ()));
+		else
+			control_server_address = nih_strdup (NULL, DBUS_ADDRESS_UPSTART);
+	}
 }
 
 

=== modified file 'init/job_process.c'
--- init/job_process.c	2012-12-18 09:02:24 +0000
+++ init/job_process.c	2013-01-21 22:44:27 +0000
@@ -65,6 +65,7 @@
 #include "job_class.h"
 #include "job.h"
 #include "errors.h"
+#include "control.h"
 
 
 /**
@@ -126,6 +127,8 @@
 static void job_process_trace_fork      (Job *job, ProcessType process);
 static void job_process_trace_exec      (Job *job, ProcessType process);
 
+extern int          user_mode;
+extern char         *control_server_address;
 
 /**
  * job_process_run:
@@ -275,6 +278,9 @@
 			       "UPSTART_JOB=%s", job->class->name));
 	NIH_MUST (environ_set (&env, NULL, &envc, TRUE,
 			       "UPSTART_INSTANCE=%s", job->name));
+	if (user_mode)
+		NIH_MUST (environ_set (&env, NULL, &envc, TRUE,
+			       "UPSTART_SESSION=%s", control_server_address));
 
 	/* If we're about to spawn the main job and we expect it to become
 	 * a daemon or fork before we can move out of spawned, we need to

=== modified file 'init/main.c'
--- init/main.c	2012-12-18 14:09:55 +0000
+++ init/main.c	2013-01-22 15:53:05 +0000
@@ -87,7 +87,6 @@
 
 static void handle_confdir      (void);
 static void handle_logdir       (void);
-static void handle_usermode     (void);
 static int  console_type_setter (NihOption *option, const char *arg);
 
 
@@ -121,13 +120,7 @@
  **/
 static int disable_startup_event = FALSE;
 
-/**
- * user_mode:
- *
- * If TRUE, upstart runs in user session mode.
- **/
-static int user_mode = FALSE;
-
+extern int          user_mode;
 extern int          disable_sessions;
 extern int          disable_job_logging;
 extern int          use_session_bus;
@@ -207,7 +200,6 @@
 
 	handle_confdir ();
 	handle_logdir ();
-	handle_usermode ();
 
 	if (disable_job_logging)
 		nih_debug ("Job logging disabled");
@@ -215,7 +207,7 @@
 	control_handle_bus_type ();
 
 #ifndef DEBUG
-	if (use_session_bus == FALSE) {
+	if (use_session_bus == FALSE && user_mode == FALSE) {
 
 		int needs_devtmpfs = 0;
 
@@ -391,7 +383,7 @@
 		nih_signal_reset ();
 
 #ifndef DEBUG
-	if (use_session_bus == FALSE) {
+	if (use_session_bus == FALSE && user_mode == FALSE) {
 		/* Catch fatal errors immediately rather than waiting for a new
 		 * iteration through the main loop.
 		 */
@@ -408,7 +400,7 @@
 	nih_signal_set_handler (SIGALRM, nih_signal_handler);
 
 #ifndef DEBUG
-	if (use_session_bus == FALSE) {
+	if (use_session_bus == FALSE && user_mode == FALSE) {
 		/* Ask the kernel to send us SIGINT when control-alt-delete is
 		 * pressed; generate an event with the same name.
 		 */
@@ -581,7 +573,7 @@
 	}
 
 #ifndef DEBUG
-	if (use_session_bus == FALSE) {
+	if (use_session_bus == FALSE && user_mode == FALSE) {
 		/* Now that the startup is complete, send all further logging output
 		 * to kmsg instead of to the console.
 		 */
@@ -970,18 +962,6 @@
 			log_dir);
 }
 
-/**
- * handle_usermode:
- *
- * Setup user session mode.
- **/
-static void
-handle_usermode (void)
-{
-	if (user_mode)
-		use_session_bus = TRUE;
-}
-
 /**  
  * NihOption setter function to handle selection of default console
  * type.

=== modified file 'init/man/init.5'
--- init/man/init.5	2012-12-18 17:40:49 +0000
+++ init/man/init.5	2013-01-16 19:20:54 +0000
@@ -444,6 +444,10 @@
 .BR initctl (8)
 utility to default to acting on the job the commands are called from.
 
+When running in a user session, the additional UPSTART_SESSION environment
+variable is also set and contains the DBus path to the private bus used by
+the upstart instance daemon.
+
 .TP
 .B env \fIKEY\fR[=\fIVALUE\fR]
 Defines a default environment variable, the value of which may be overridden

=== modified file 'init/state.c'
--- init/state.c	2012-12-07 18:26:43 +0000
+++ init/state.c	2013-01-21 22:44:27 +0000
@@ -48,7 +48,6 @@
 json_object *json_events = NULL;
 json_object *json_classes = NULL;
 
-extern int   use_session_bus;
 extern char *log_dir;
 
 /**

=== modified file 'util/initctl.c'
--- util/initctl.c	2012-03-16 21:02:13 +0000
+++ util/initctl.c	2013-01-22 16:50:19 +0000
@@ -145,6 +145,14 @@
 int dbus_bus_type = -1;
 
 /**
+ * user_mode:
+ *
+ * If TRUE, talk to Upstart over the private socket defined in UPSTART_SESSION
+ * if UPSTART_SESSION isn't defined, then fallback to the session bus.
+ **/
+int user_mode = FALSE;
+
+/**
  * dest_name:
  *
  * Name on the D-Bus system bus that the message should be sent to when
@@ -291,11 +299,30 @@
 	DBusError       dbus_error;
 	DBusConnection *connection;
 	NihDBusProxy *  upstart;
-
-	if (use_dbus < 0)
-		use_dbus = getuid () ? TRUE : FALSE;
-	if (use_dbus >= 0 && dbus_bus_type < 0)
-		dbus_bus_type = DBUS_BUS_SYSTEM;
+	char * user_addr;
+
+	user_addr = getenv ("UPSTART_SESSION");
+
+	if (user_addr && dbus_bus_type < 0) {
+		user_mode = TRUE;
+	}
+
+	if (! user_mode) {
+		if (use_dbus < 0)
+			use_dbus = getuid () ? TRUE : FALSE;
+		if (use_dbus >= 0 && dbus_bus_type < 0)
+			dbus_bus_type = DBUS_BUS_SYSTEM;
+	}
+	else {
+		if (! user_addr) {
+			nih_error ("UPSTART_SESSION isn't set in the environment. "
+				       "Unable to locate the Upstart instance.");
+			return NULL;
+		}
+		dest_address = user_addr;
+		use_dbus = FALSE;
+	}
+
 
 	dbus_error_init (&dbus_error);
 	if (use_dbus) {
@@ -2356,6 +2383,8 @@
 	  NULL, NULL, NULL, dbus_bus_type_setter },
 	{ 0, "dest", N_("destination well-known name on D-Bus bus"),
 	  NULL, "NAME", &dest_name, NULL },
+	{ 0, "user", N_("run in user mode (as used for user sessions)"),
+		NULL, NULL, &user_mode, NULL },
 
 	NIH_OPTION_LAST
 };

=== modified file 'util/man/initctl.8'
--- util/man/initctl.8	2012-08-31 21:01:48 +0000
+++ util/man/initctl.8	2013-01-22 17:14:40 +0000
@@ -39,6 +39,13 @@
 .\"
 .SH OPTIONS
 .TP
+.B \-\-user
+User mode. In this mode, initctl will talk to the
+.BR init (8)
+daemon using the D\-Bus private socket defined in the UPSTART_SESSION
+environment variable.
+.\"
+.TP
 .B \-\-session
 Connect to
 .BR init (8)

=== modified file 'util/tests/test_initctl.c'
--- util/tests/test_initctl.c	2012-10-26 16:28:12 +0000
+++ util/tests/test_initctl.c	2013-01-22 18:17:04 +0000
@@ -29,7 +29,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include <regex.h>
-#include <sys/types.h>        
+#include <sys/types.h>
 #include <sys/stat.h>
 
 #include <nih-dbus/dbus_error.h>
@@ -379,6 +379,7 @@
 }
 
 extern int use_dbus;
+extern int user_mode;
 extern int dbus_bus_type;
 extern char *dest_name;
 extern const char *dest_address;
@@ -508,6 +509,91 @@
 		dbus_shutdown ();
 	}
 
+	/* Check that we can create a proxy to Upstart's private internal
+	 * server in user mode, and that this is the default behaviour if we don't
+	 * fiddle with the other options.  The returned proxy should
+	 * hold the only reference to the connection.
+	 */
+	TEST_FEATURE ("with user-mode");
+	TEST_ALLOC_FAIL {
+		use_dbus = -1;
+		dbus_bus_type = -1;
+		dest_name = NULL;
+		dest_address = DBUS_ADDRESS_UPSTART;
+		user_mode = TRUE;
+
+		assert0 (setenv ("UPSTART_SESSION",
+				 "unix:abstract=/com/ubuntu/upstart/test-session",
+				 TRUE));
+
+		TEST_ALLOC_SAFE {
+			server = nih_dbus_server (getenv ("UPSTART_SESSION"),
+						  my_connect_handler,
+						  NULL);
+			assert (server != NULL);
+		}
+
+		my_connect_handler_called = FALSE;
+		last_connection = NULL;
+
+		TEST_DIVERT_STDERR (output) {
+			proxy = upstart_open (NULL);
+		}
+		rewind (output);
+
+		if (test_alloc_failed
+		    && (proxy == NULL)) {
+			TEST_FILE_EQ (output, "test: Cannot allocate memory\n");
+			TEST_FILE_END (output);
+			TEST_FILE_RESET (output);
+
+			if (last_connection) {
+				dbus_connection_close (last_connection);
+				dbus_connection_unref (last_connection);
+			}
+
+			dbus_server_disconnect (server);
+			dbus_server_unref (server);
+
+			dbus_shutdown ();
+			continue;
+		}
+
+		nih_main_loop ();
+
+		TEST_TRUE (my_connect_handler_called);
+		TEST_NE_P (last_connection, NULL);
+
+		TEST_NE_P (proxy, NULL);
+		TEST_ALLOC_SIZE (proxy, sizeof (NihDBusProxy));
+
+		TEST_NE_P (proxy->connection, NULL);
+		TEST_EQ_P (proxy->name, NULL);
+		TEST_EQ_P (proxy->owner, NULL);
+		TEST_EQ_STR (proxy->path, DBUS_PATH_UPSTART);
+		TEST_ALLOC_PARENT (proxy->path, proxy);
+		TEST_FALSE (proxy->auto_start);
+
+		TEST_EQ_P (proxy->lost_handler, NULL);
+		TEST_EQ_P (proxy->data, NULL);
+
+		nih_free (proxy);
+
+		TEST_FILE_END (output);
+		TEST_FILE_RESET (output);
+
+		dbus_connection_close (last_connection);
+		dbus_connection_unref (last_connection);
+
+		dbus_server_disconnect (server);
+		dbus_server_unref (server);
+
+		dbus_shutdown ();
+
+		unsetenv ("UPSTART_SESSION");
+		user_mode = FALSE;
+	}
+
 
 	/* Check that we can create a connection to Upstart via the system
 	 * bus.  The returned proxy should use the default name on that
@@ -719,6 +805,36 @@
 	}
 
 
+	/* Check that when we attempt to connect to Upstart in user mode but
+	 * without UPSTART_SESSION set in the environment, an appropriate
+	 * error is output.
+	 */
+	TEST_FEATURE ("with user-mode and no target");
+	TEST_ALLOC_FAIL {
+		use_dbus = -1;
+		dbus_bus_type = -1;
+		dest_name = NULL;
+		dest_address = DBUS_ADDRESS_UPSTART;
+		user_mode = TRUE;
+
+		unsetenv ("UPSTART_SESSION");
+
+		TEST_DIVERT_STDERR (output) {
+			proxy = upstart_open (NULL);
+		}
+		rewind (output);
+
+		TEST_EQ_P (proxy, NULL);
+
+		TEST_FILE_EQ (output, ("test: UPSTART_SESSION isn't set in the environment. "
+				       "Unable to locate the Upstart instance.\n"));
+		TEST_FILE_END (output);
+		TEST_FILE_RESET (output);
+
+		dbus_shutdown ();
+		user_mode = FALSE;
+	}
+
 	fclose (output);
 }
 

-- 
upstart-devel mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/upstart-devel

Reply via email to