------------------------------------------------------------
revno: 1490
committer: James Hunt <[email protected]>
branch nick: upstart
timestamp: Thu 2013-06-27 11:16:35 +0100
message:
  * lib/Makefile.am: Added dependency on libtest_util_common.
  * lib/tests/test_libupstart.c: 
    - Use test_util_common.
    - test_libupstart():
      - Start a private D-Bus server and Session Init to handle possibility
        of building on systems not using Upstart as PID 1.
    - main(): Disable tests if required environment not available (as done
      for test_initctl).
  * test/test_util_common.c:
    - wait_for_upstart(): Pass pid of session init to pass to
      set_upstart_session().
    - set_upstart_session(): Now accepts a session init pid to allow
      filtering of sessions and remove limitations of this function.
  * test/test_util_common.h:
    - REEXEC_UPSTART(): Updated for changed wait_for_upstart() parameter.
  * util/tests/test_initctl.c:
    - Moved in_chroot() and dbus_configured() to test_util_common.c.
modified:
  ChangeLog
  lib/Makefile.am
  lib/tests/test_libupstart.c
  test/test_util_common.c
  test/test_util_common.h
  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 'ChangeLog'
--- ChangeLog	2013-06-26 12:10:23 +0000
+++ ChangeLog	2013-06-27 10:16:35 +0000
@@ -1,3 +1,23 @@
+2013-06-27  James Hunt  <[email protected]>
+
+	* lib/Makefile.am: Added dependency on libtest_util_common.
+	* lib/tests/test_libupstart.c: 
+	  - Use test_util_common.
+	  - test_libupstart():
+	    - Start a private D-Bus server and Session Init to handle possibility
+	      of building on systems not using Upstart as PID 1.
+	  - main(): Disable tests if required environment not available (as done
+	    for test_initctl).
+	* test/test_util_common.c:
+	  - wait_for_upstart(): Pass pid of session init to pass to
+	    set_upstart_session().
+	  - set_upstart_session(): Now accepts a session init pid to allow
+	    filtering of sessions and remove limitations of this function.
+	* test/test_util_common.h:
+	  - REEXEC_UPSTART(): Updated for changed wait_for_upstart() parameter.
+	* util/tests/test_initctl.c:
+	  - Moved in_chroot() and dbus_configured() to test_util_common.c.
+
 2013-06-26  James Hunt  <[email protected]>
 
 	* extra/Makefile.am: Add man pages and conf file.

=== modified file 'lib/Makefile.am'
--- lib/Makefile.am	2013-05-30 08:09:45 +0000
+++ lib/Makefile.am	2013-06-27 10:16:35 +0000
@@ -16,7 +16,8 @@
 	-DLOCALEDIR="\"$(localedir)\"" \
 	-DSBINDIR="\"$(sbindir)\"" \
 	-I$(top_builddir) -I$(top_srcdir) -iquote$(builddir) -iquote$(srcdir) \
-	-I$(top_srcdir)/intl
+	-I$(top_srcdir)/intl \
+	-I$(top_srcdir)/test -iquote $(top_srcdir)/test
 
 lib_LTLIBRARIES = libupstart.la
 
@@ -81,6 +82,7 @@
 	$(NIH_LIBS) \
 	$(NIH_DBUS_LIBS) \
 	$(DBUS_LIBS) \
+	$(top_builddir)/test/libtest_util_common.a \
 	-lrt
 
 com_ubuntu_Upstart_OUTPUTS = \

=== modified file 'lib/tests/test_libupstart.c'
--- lib/tests/test_libupstart.c	2013-05-23 09:36:58 +0000
+++ lib/tests/test_libupstart.c	2013-06-27 10:16:35 +0000
@@ -35,6 +35,7 @@
 #include "dbus/upstart.h"
 
 #include "upstart.h"
+#include "test_util_common.h"
 
 /**
  * upstart_open:
@@ -94,15 +95,20 @@
 	nih_local NihDBusProxy  *upstart = NULL;
 	nih_local char          *version = NULL;
 	int                      ret;
+	pid_t                    upstart_pid;
+	pid_t                    dbus_pid;
 
 	TEST_GROUP ("libupstart");
 
 	TEST_FEATURE ("version");
 
+	/* Create a private Session Init instance to connect to */
+	TEST_DBUS (dbus_pid);
+	START_UPSTART (upstart_pid, TRUE);
+
 	upstart = upstart_open (NULL);
 	TEST_NE_P (upstart, NULL);
 
-
 	/* Basic test (that does not change the state of the system
 	 * running this test) to see if we can query version of running
 	 * Upstart instance.
@@ -112,13 +118,23 @@
 
 	nih_message ("Running instance version: '%s'", version);
 	assert0 (fnmatch ("init (upstart*)", version, 0x0));
+
+	STOP_UPSTART (upstart_pid);
+	TEST_DBUS_END (dbus_pid);
 }
 
 int
 main (int   argc,
       char *argv[])
 {
-	test_libupstart ();
+	if (in_chroot () && ! dbus_configured ()) {
+		fprintf(stderr, "\n\n"
+			"WARNING: not running %s tests as within "
+			"chroot environment without D-Bus"
+			"\n\n", __FILE__);
+	} else {
+		test_libupstart ();
+	}
 
 	return 0;
 }

=== modified file 'test/test_util_common.c'
--- test/test_util_common.c	2013-06-25 09:19:05 +0000
+++ test/test_util_common.c	2013-06-27 10:16:35 +0000
@@ -63,15 +63,15 @@
 /**
  * wait_for_upstart:
  *
- * @user: TRUE if waiting for a Session Init (which uses a private bus
- * rather than the session bus), else FALSE.
+ * @session_init_pid: pid of Session Init (which uses a private bus
+ * rather than the session bus), else 0.
  *
  * Wait for Upstart to appear on D-Bus denoting its completion of
  * initialisation. Wait time is somewhat arbitrary (but more
  * than adequate!).
  **/
 void
-wait_for_upstart (int user)
+wait_for_upstart (int session_init_pid)
 {
 	nih_local NihDBusProxy *upstart = NULL;
 	DBusConnection         *connection;
@@ -82,8 +82,8 @@
 	/* XXX: arbitrary value */
 	int                     attempts = 10;
 
-	if (user) {
-		TEST_TRUE (set_upstart_session ());
+	if (session_init_pid) {
+		TEST_TRUE (set_upstart_session (session_init_pid));
 		address = getenv ("UPSTART_SESSION");
 	} else {
 		address = getenv ("DBUS_SESSION_BUS_ADDRESS");
@@ -127,19 +127,16 @@
 /**
  * set_upstart_session:
  *
+ * @session_init_pid: pid of Session Init.
+ *
  * Attempt to "enter" an Upstart session by setting UPSTART_SESSION to
- * the value of the currently running session.
- *
- * It is only legitimate to call this function if you have previously
- * started a Session Init process.
- *
- * Limitations: Assumes that at most 1 session is running.
+ * the value of the session running under pid @session_init_pid.
  *
  * Returns: TRUE if it was possible to enter the currently running
  * Upstart session, else FALSE.
  **/
 int
-set_upstart_session (void)
+set_upstart_session (pid_t session_init_pid)
 {
 	char                     *value;
 	nih_local char           *cmd = NULL;
@@ -147,10 +144,13 @@
 	size_t                    lines = 0;
 	int                       got = FALSE;
 	int                       i;
+	pid_t                     pid;
 
 	/* XXX: arbitrary value */
 	int                       loops = 5;
 
+	nih_assert (session_init_pid);
+
 	/* list-sessions relies on this */
 	if (! getenv ("XDG_RUNTIME_DIR"))
 		return FALSE;
@@ -162,35 +162,48 @@
 	 * within a reasonable period of time.
 	 */
 	for (i = 0; i < loops; i++) {
-		sleep (1);
+        sleep (1);
 
 		RUN_COMMAND (NULL, cmd, &output, &lines);
-		if (lines != 1)
-			continue;
-
-		/* No pid in output */
-		if (! isdigit(output[0][0]))
-			continue;
-
-		/* look for separator between pid and value of
-		 * UPSTART_SESSION.
-		 */
-		value = strstr (output[0], " ");
-		if (! value)
-			continue;
-
-		/* jump over space */
-		value  += 1;
-		if (! value)
-			continue;
-
-		/* No socket address */
-		if (strstr (value, "unix:abstract") == value) {
-			got = TRUE;
-			break;
-		}
+
+        if (lines < 1)
+            continue;
+
+        /* Look for the specific session */
+        for (size_t line = 0; line < lines; lines++) {
+
+            /* No pid in output */
+            if (! isdigit(output[line][0]))
+                continue;
+
+            pid = (pid_t)atoi(output[line]);
+            nih_assert (pid > 0);
+
+            if (pid != session_init_pid)
+                continue;
+
+            /* look for separator between pid and value of
+             * UPSTART_SESSION.
+             */
+            value = strstr (output[0], " ");
+            if (! value)
+                continue;
+
+            /* jump over space */
+            value  += 1;
+            if (! value)
+                continue;
+
+            /* No socket address */
+            if (strstr (value, "unix:abstract") == value) {
+                got = TRUE;
+                goto out;
+            }
+        }
 	}
 
+out:
+
 	if (got != TRUE)
 		return FALSE;
 
@@ -398,7 +411,7 @@
 	}
 
 	sigprocmask (SIG_SETMASK, &orig_set, NULL);
-	wait_for_upstart (user);
+	wait_for_upstart (user ? *pid : 0);
 }
 
 /**
@@ -625,3 +638,50 @@
 
 	return session_file;
 }
+
+/**
+ * in_chroot:
+ *
+ * Determine if running inside a chroot environment.
+ *
+ * Failures are fatal.
+ *
+ * Returns TRUE if within a chroot, else FALSE.
+ **/
+int
+in_chroot (void)
+{
+	struct stat st;
+	int i;
+	char dir[] = "/";
+
+	i = stat(dir, &st);
+	    
+	if ( i != 0 ) { 
+		fprintf (stderr, "ERROR: cannot stat '%s'\n", dir);
+		exit (EXIT_FAILURE);
+	}
+
+	if ( st.st_ino == 2 )
+		return FALSE;
+
+	return TRUE;
+}
+
+/**
+ * dbus_configured
+ *
+ * Determine if D-Bus has been configured (with dbus-uuidgen).
+ *
+ * Returns TRUE if D-Bus appears to have been configured,
+ * else FALSE.
+ **/
+int
+dbus_configured (void)
+{
+	struct stat st;
+	char path[] = "/var/lib/dbus/machine-id";
+
+	return !stat (path, &st);
+}
+

=== modified file 'test/test_util_common.h'
--- test/test_util_common.h	2013-06-25 09:19:05 +0000
+++ test/test_util_common.h	2013-06-27 10:16:35 +0000
@@ -383,13 +383,14 @@
 /**
  * REEXEC_UPSTART:
  *
- * @pid: pid of upstart.
+ * @pid: pid of upstart,
+ * @user: TRUE if @pid refers to a Session Init, else FALSE.
  *
  * Force upstart to perform a re-exec.
  **/
-#define REEXEC_UPSTART(pid)                                          \
+#define REEXEC_UPSTART(pid, user)                                    \
 	KILL_UPSTART (pid, SIGTERM, FALSE);                          \
-	wait_for_upstart (FALSE)
+	wait_for_upstart (user ? pid : FALSE)
 
 /**
  * RUN_COMMAND:
@@ -686,10 +687,10 @@
 extern int test_user_mode;
 
 /* Prototypes */
-int set_upstart_session (void)
+int set_upstart_session (pid_t session_init_pid)
 	__attribute__ ((warn_unused_result));
 
-void wait_for_upstart (int user);
+void wait_for_upstart (int session_init_pid);
 
 pid_t timed_waitpid (pid_t pid, time_t timeout)
 	__attribute__ ((warn_unused_result));
@@ -722,4 +723,10 @@
 char *get_session_file (const char *xdg_runtime_dir, pid_t pid)
 	__attribute__ ((warn_unused_result));
 
+int in_chroot (void)
+	__attribute__ ((warn_unused_result));
+
+int dbus_configured (void)
+	__attribute__ ((warn_unused_result));
+
 #endif /* TEST_UTIL_COMMON_H */

=== modified file 'util/tests/test_initctl.c'
--- util/tests/test_initctl.c	2013-06-25 10:13:12 +0000
+++ util/tests/test_initctl.c	2013-06-27 10:16:35 +0000
@@ -10928,7 +10928,7 @@
 	TEST_EQ (lines, 3);
 	nih_free (output);
 
-	REEXEC_UPSTART (upstart_pid);
+	REEXEC_UPSTART (upstart_pid, FALSE);
 
 	/* Ensure we can still list jobs after a re-exec */
 	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
@@ -11069,7 +11069,7 @@
 	TEST_FILE_END (file);
 	fclose (file);
 
-	REEXEC_UPSTART (upstart_pid);
+	REEXEC_UPSTART (upstart_pid, FALSE);
 	
 	/* Create flag file to allow job to proceed */
 	{
@@ -16636,53 +16636,6 @@
         TEST_EQ (rmdir (logdir), 0);
 }
 
-
-/**
- * in_chroot:
- *
- * Determine if running inside a chroot environment.
- *
- * Failures are fatal.
- *
- * Returns TRUE if within a chroot, else FALSE.
- **/
-int
-in_chroot (void)
-{
-	struct stat st;
-	int i;
-	char dir[] = "/";
-
-	i = stat(dir, &st);
-	    
-	if ( i != 0 ) { 
-		fprintf (stderr, "ERROR: cannot stat '%s'\n", dir);
-		exit (EXIT_FAILURE);
-	}
-
-	if ( st.st_ino == 2 )
-		return FALSE;
-
-	return TRUE;
-}
-
-/**
- * dbus_configured
- *
- * Determine if D-Bus has been configured (with dbus-uuidgen).
- *
- * Returns TRUE if D-Bus appears to have been configured,
- * else FALSE.
- **/
-int
-dbus_configured (void)
-{
-	struct stat st;
-	char path[] = "/var/lib/dbus/machine-id";
-
-	return !stat (path, &st);
-}
-
 int
 main (int   argc,
       char *argv[])

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

Reply via email to