------------------------------------------------------------
revno: 1406
committer: James Hunt <[email protected]>
branch nick: upstart
timestamp: Tue 2012-12-11 16:45:55 +0000
message:
  * dbus/com.ubuntu.Upstart.xml: Added "GetEnv" and "SetEnv" methods.
  * init/control.c:
    - control_set_env(): Implementation of "SetEnv" D-Bus method.
    - control_get_env(): Implementation of "GetEnv" D-Bus method.
  * init/job_class.c:
    - Added job_environ environment table.
    - job_class_environ_init(): Initialise job_environ.
    - job_class_environment: Use job_environ.
  * init/main.c: main(): Call job_class_environ_init().
modified:
  ChangeLog
  dbus/com.ubuntu.Upstart.xml
  init/control.c
  init/job_class.c
  init/job_class.h
  init/main.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	2012-12-11 13:59:01 +0000
+++ ChangeLog	2012-12-11 16:45:55 +0000
@@ -1,6 +1,15 @@
 2012-12-11  James Hunt  <[email protected]>
 
 	* init/Makefile.am: Add explicit -lrt for tests (LP: #1088863)
+	* dbus/com.ubuntu.Upstart.xml: Added "GetEnv" and "SetEnv" methods.
+	* init/control.c:
+	  - control_set_env(): Implementation of "SetEnv" D-Bus method.
+	  - control_get_env(): Implementation of "GetEnv" D-Bus method.
+	* init/job_class.c:
+	  - Added job_environ environment table.
+	  - job_class_environ_init(): Initialise job_environ.
+	  - job_class_environment: Use job_environ.
+	* init/main.c: main(): Call job_class_environ_init().
 
 2012-12-07  James Hunt  <[email protected]>
 

=== modified file 'dbus/com.ubuntu.Upstart.xml'
--- dbus/com.ubuntu.Upstart.xml	2012-12-07 18:26:43 +0000
+++ dbus/com.ubuntu.Upstart.xml	2012-12-11 16:45:55 +0000
@@ -42,6 +42,16 @@
       <annotation name="com.netsplit.Nih.Method.Async" value="true" />
     </method>
 
+    <method name="GetEnv">
+      <arg name="name" type="s" direction="in" />
+      <arg name="value" type="s" direction="out" />
+    </method>
+
+    <method name="SetEnv">
+      <arg name="var" type="s" direction="in" />
+      <arg name="replace" type="b" direction="in" />
+    </method>
+
     <!-- Signals for changes to the job list -->
     <signal name="JobAdded">
       <arg name="job" type="o" />

=== modified file 'init/control.c'
--- init/control.c	2012-12-07 18:26:43 +0000
+++ init/control.c	2012-12-11 16:45:55 +0000
@@ -1067,3 +1067,140 @@
 
 	return 0;
 }
+
+/**
+ * control_set_env:
+ *
+ * @data: not used,
+ * @message: D-Bus connection and message received,
+ * @var: name[/value] pair of environment variable to set,
+ * @replace: TRUE if @name should be overwritten if already set, else
+ *  FALSE.
+ *
+ * Implements the SetEnv method of the com.ubuntu.Upstart
+ * interface.
+ *
+ * Called to request Upstart store a particular name/value pair that
+ * will be exported to all jobs' environments.
+ *
+ * Returns: zero on success, negative value on raised error.
+ **/
+int
+control_set_env (void           *data,
+		 NihDBusMessage *message,
+		 const char     *var,
+		 int             replace)
+{
+	Session   *session;
+	char     **ret;
+	uid_t      uid;
+
+	nih_assert (message != NULL);
+	nih_assert (var);
+
+	uid = getuid ();
+
+	/* Get the relevant session */
+	session = session_from_dbus (NULL, message);
+
+	/* Chroot sessions must not be able to influence
+	 * the outside system.
+	 */
+	if (session && session->chroot) {
+		nih_warn (_("Ignoring set env request from chroot session"));
+		return 0;
+	}
+
+	/* Disallow users from changing Upstarts environment, unless they happen to
+	 * own this process (which they may do in the test scenario and
+	 * when running Upstart as a non-privileged user).
+	 */
+	if (session && session->user != uid) {
+		nih_dbus_error_raise_printf (
+			DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
+			_("You do not have permission to modify the init environment"));
+		return -1;
+	}
+
+	job_class_environment_init ();
+
+	ret = environ_add (&job_environ, NULL, NULL, replace, var);
+
+	if (! ret)
+		nih_return_no_memory_error (-1);
+
+	return 0;
+}
+
+/**
+ * control_get_env:
+ *
+ * @data: not used,
+ * @message: D-Bus connection and message received,
+ * @name: name of environment variable to retrieve,
+ * @value: value of @name.
+ *
+ * Implements the SetEnv method of the com.ubuntu.Upstart
+ * interface.
+ *
+ * Called to request Upstart store a particular name/value pair that
+ * will be exported to all jobs' environments.
+ *
+ * Returns: zero on success, negative value on raised error.
+ **/
+int
+control_get_env (void             *data,
+		 NihDBusMessage   *message,
+		 char             *name,
+		 char            **value)
+{
+	Session     *session;
+	const char  *tmp;
+	uid_t        uid;
+
+	nih_assert (message != NULL);
+	nih_assert (name);
+	nih_assert (value);
+
+	uid = getuid ();
+
+	/* Get the relevant session */
+	session = session_from_dbus (NULL, message);
+
+	/* Chroot sessions must not be able to influence
+	 * the outside system.
+	 */
+	if (session && session->chroot) {
+		nih_warn (_("Ignoring get env request from chroot session"));
+		return 0;
+	}
+
+	/* Disallow users from changing Upstarts environment, unless they happen to
+	 * own this process (which they may do in the test scenario and
+	 * when running Upstart as a non-privileged user).
+	 */
+	if (session && session->user != uid) {
+		nih_dbus_error_raise_printf (
+			DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
+			_("You do not have permission to modify the init environment"));
+		return -1;
+	}
+
+	job_class_environment_init ();
+
+	tmp = environ_get (job_environ, name);
+	if (! tmp)
+		goto error;
+
+	*value = nih_strdup (message, tmp);
+	if (! *value)
+		nih_return_no_memory_error (-1);
+
+	return 0;
+
+error:
+	nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
+			"%s: %s",
+			_("No such variable"), name);
+	return -1;
+}

=== modified file 'init/job_class.c'
--- init/job_class.c	2012-11-22 16:32:36 +0000
+++ init/job_class.c	2012-12-11 16:45:55 +0000
@@ -83,6 +83,13 @@
  **/
 NihHash *job_classes = NULL;
 
+/**
+ * job_environ:
+ *
+ * Array of environment variables that will be set in the jobs
+ * environment.
+ **/
+char **job_environ = NULL;
 
 /**
  * job_class_init:
@@ -96,6 +103,23 @@
 		job_classes = NIH_MUST (nih_hash_string_new (NULL, 0));
 }
 
+/**
+ * job_class_environ_init:
+ *
+ * Initialise the job_environ array.
+ **/
+void
+job_class_environment_init (void)
+{
+	char * const default_environ[] = { JOB_DEFAULT_ENVIRONMENT, NULL };
+
+	if (! job_environ) {
+		job_environ = NIH_MUST (nih_str_array_new (NULL));
+		NIH_MUST (environ_append (&job_environ, NULL, 0, TRUE, default_environ));
+	}
+
+}
+
 
 /**
  * job_class_new:
@@ -509,21 +533,22 @@
 		       JobClass   *class,
 		       size_t     *len)
 {
-	char * const   builtin[] = { JOB_DEFAULT_ENVIRONMENT, NULL };
-	char         **env;
+	char  **env;
 
 	nih_assert (class != NULL);
 
+	job_class_environment_init ();
+
 	env = nih_str_array_new (parent);
 	if (! env)
 		return NULL;
 	if (len)
 		*len = 0;
 
-	/* Copy the builtin set of environment variables, usually these just
+	/* Copy the set of environment variables, usually these just
 	 * pick up the values from init's own environment.
 	 */
-	if (! environ_append (&env, parent, len, TRUE, builtin))
+	if (! environ_append (&env, parent, len, TRUE, job_environ))
 		goto error;
 
 	/* Copy the set of environment variables from the job configuration,

=== modified file 'init/job_class.h'
--- init/job_class.h	2012-09-24 09:10:05 +0000
+++ init/job_class.h	2012-12-11 16:45:55 +0000
@@ -225,11 +225,14 @@
 
 NIH_BEGIN_EXTERN
 
-extern NihHash *job_classes;
+extern NihHash  *job_classes;
+extern char    **job_environ;
 
 
 void        job_class_init                 (void);
 
+void        job_class_environment_init     (void);
+
 JobClass  * job_class_new                  (const void *parent,
 					    const char *name,
 					    Session *session)

=== modified file 'init/main.c'
--- init/main.c	2012-11-07 15:17:58 +0000
+++ init/main.c	2012-12-11 16:45:55 +0000
@@ -603,6 +603,8 @@
 	if (disable_sessions)
 		nih_debug ("Sessions disabled");
 
+	job_class_environment_init ();
+
 	/* Run through the loop at least once to deal with signals that were
 	 * delivered to the previous process while the mask was set or to
 	 * process the startup event we emitted.
@@ -938,4 +940,3 @@
 
 	 return 0;
 }
-

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

Reply via email to