Merge lp:~jamesodhunt/upstart/bug-1159895 into lp:upstart

Proposed by James Hunt
Status: Merged
Merged at revision: 1480
Proposed branch: lp:~jamesodhunt/upstart/bug-1159895
Merge into: lp:upstart
Diff against target: 501 lines (+167/-56)
5 files modified
ChangeLog (+21/-0)
init/job_class.c (+11/-4)
init/job_process.c (+0/-3)
util/man/initctl.8 (+7/-4)
util/tests/test_initctl.c (+128/-45)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/bug-1159895
Reviewer Review Type Date Requested Status
Stéphane Graber (community) Approve
James Hunt Needs Resubmitting
Review via email: mp+166836@code.launchpad.net

Description of the change

= Behaviour Change =

Previously running 'initctl list-env' in a Session Init environment would only list those minimal variables added by init (PATH and TERM) along with any variables explicitly set via 'initctl set-env'. However, it now lists that set plus all variables set in inits environment at startup time.

* init/job_class.c: job_class_environment_init(): Copy inits environment
  to the default job class environment for user mode where appropriate.
* init/job_process.c: job_process_run(): Don't copy inits environment
  into the job instances environment table as those values are now
  already in the table.
* util/tests/test_initctl.c: Updates for new behaviour (where
  'list-env' will now contain the entire environment of the init
  process, not just those variables explicitly set via set-env).
* util/man/initctl.8: Update on behaviour.

To post a comment you must log in.
Revision history for this message
Stéphane Graber (stgraber) wrote :

I think this looks good.

Do we have a test that runs upstart with no_inherit_env set and checks that the environment in that case is sane (exact match to what we'd expect)?
If we don't, we definitely should add that as your branch relaxes some of the existing tests in that regard.

lp:~jamesodhunt/upstart/bug-1159895 updated
1479. By James Hunt

* util/tests/test_initctl.c:
  - test_no_inherit_job_env(): New function to test --no-inherit-env.
  - test_job_env():
    - Move code that sets HOME+PATH if not set from
      test_default_job_env() so that test_no_inherit_job_env() can make
      use of it.
    - Session file cleanup tweaks to work with test_no_inherit_job_env().

Revision history for this message
James Hunt (jamesodhunt) wrote :

Good call - added some basic '--no-inherit-env' tests.

review: Needs Resubmitting
Revision history for this message
Stéphane Graber (stgraber) wrote :

Perfect, thanks.

Note that your changelog changes are currently failing to merge so you'll need to resolve that conflict on merge.

review: Approve
lp:~jamesodhunt/upstart/bug-1159895 updated
1480. By James Hunt

Sync with lp:upstart.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2013-06-05 16:18:42 +0000
3+++ ChangeLog 2013-06-20 09:37:27 +0000
4@@ -33,8 +33,29 @@
5 approach of detecting the format of the JSON is safer.
6 * init/state.h: Remove STATE_VERSION.
7
8+2013-06-03 James Hunt <james.hunt@ubuntu.com>
9+
10+ * util/tests/test_initctl.c:
11+ - test_no_inherit_job_env(): New function to test --no-inherit-env.
12+ - test_job_env():
13+ - Move code that sets HOME+PATH if not set from
14+ test_default_job_env() so that test_no_inherit_job_env() can make
15+ use of it.
16+ - Session file cleanup tweaks to work with test_no_inherit_job_env().
17+
18 2013-05-31 James Hunt <james.hunt@ubuntu.com>
19
20+ * init/job_class.c: job_class_environment_init(): Copy inits environment
21+ to the default job class environment for user mode where appropriate.
22+ (LP: #1159895).
23+ * init/job_process.c: job_process_run(): Don't copy inits environment
24+ into the job instances environment table as those values are now
25+ already in the table.
26+ * util/tests/test_initctl.c: Updates for new behaviour (where
27+ 'list-env' will now contain the entire environment of the init
28+ process, not just those variables explicitly set via set-env).
29+ * util/man/initctl.8: Update on behaviour.
30+
31 [ Eric S. Raymond <esr@thyrsus.com> ]
32 * init/man/init.5: Fix unliftable markup (LP: #1185108).
33
34
35=== modified file 'init/job_class.c'
36--- init/job_class.c 2013-05-29 10:36:36 +0000
37+++ init/job_class.c 2013-06-20 09:37:27 +0000
38@@ -62,6 +62,9 @@
39 #include <json.h>
40
41 extern json_object *json_classes;
42+extern int user_mode;
43+extern int no_inherit_env;
44+extern char **environ;
45
46 /* Prototypes for static functions */
47 static void job_class_add (JobClass *class);
48@@ -115,10 +118,14 @@
49 {
50 char * const default_environ[] = { JOB_DEFAULT_ENVIRONMENT, NULL };
51
52- if (! job_environ) {
53- job_environ = NIH_MUST (nih_str_array_new (NULL));
54- NIH_MUST (environ_append (&job_environ, NULL, 0, TRUE, default_environ));
55- }
56+ if (job_environ)
57+ return;
58+
59+ job_environ = NIH_MUST (nih_str_array_new (NULL));
60+ NIH_MUST (environ_append (&job_environ, NULL, 0, TRUE, default_environ));
61+
62+ if (user_mode && ! no_inherit_env)
63+ NIH_MUST(environ_append (&job_environ, NULL, 0, TRUE, environ));
64 }
65
66 /**
67
68=== modified file 'init/job_process.c'
69--- init/job_process.c 2013-05-24 17:21:47 +0000
70+++ init/job_process.c 2013-06-20 09:37:27 +0000
71@@ -283,9 +283,6 @@
72 envc = 0;
73 env = NIH_MUST (nih_str_array_new (NULL));
74
75- if (user_mode && ! no_inherit_env)
76- NIH_MUST(environ_append (&env, NULL, &envc, TRUE, environ));
77-
78 if (job->env)
79 NIH_MUST(environ_append (&env, NULL, &envc, TRUE, job->env));
80
81
82=== modified file 'util/man/initctl.8'
83--- util/man/initctl.8 2013-02-15 14:07:05 +0000
84+++ util/man/initctl.8 2013-06-20 09:37:27 +0000
85@@ -565,10 +565,13 @@
86 job-specific environment table; otherwise the global environment table
87 that is applied to all jobs when they first start is queried.
88
89-Note that the global job environment table only includes the minimal set
90-of standard system variables added by the
91-.BR init (8)
92-daemon along with any variables set using
93+Note that the global job environment table comprises those variables
94+already set in the
95+.BR init (8)
96+daemons environment at startup, the minimal set of standard system
97+variables added by the
98+.BR init (8)
99+daemon, and any variables set using
100 .BR set\-env "."
101 See
102 .BR init (5)
103
104=== modified file 'util/tests/test_initctl.c'
105--- util/tests/test_initctl.c 2013-02-27 11:46:04 +0000
106+++ util/tests/test_initctl.c 2013-06-20 09:37:27 +0000
107@@ -16436,21 +16436,6 @@
108 assert (upstart_pid);
109 assert (dbus_pid);
110
111- /*******************************************************************/
112- /* Ensure basic variables are set in the current environment */
113-
114- if (! getenv ("TERM")) {
115- fprintf (stderr, "WARNING: setting TERM to '%s' as not set\n",
116- TEST_INITCTL_DEFAULT_TERM);
117- assert0 (setenv ("TERM", TEST_INITCTL_DEFAULT_TERM, 1));
118- }
119-
120- if (! getenv ("PATH")) {
121- fprintf (stderr, "WARNING: setting PATH to '%s' as not set\n",
122- TEST_INITCTL_DEFAULT_PATH);
123- assert0 (setenv ("PATH", TEST_INITCTL_DEFAULT_PATH, 1));
124- }
125-
126 cmd = nih_sprintf (NULL, "%s reset-env 2>&1", get_initctl ());
127 TEST_NE_P (cmd, NULL);
128 RUN_COMMAND (NULL, cmd, &output, &line_count);
129@@ -16463,9 +16448,9 @@
130 TEST_NE_P (cmd, NULL);
131 RUN_COMMAND (NULL, cmd, &output, &line_count);
132
133- TEST_EQ (line_count, 2);
134- TEST_STR_MATCH (output[0], "PATH=*");
135- TEST_STR_MATCH (output[1], "TERM=*");
136+ TEST_GE (line_count, 2);
137+ TEST_STR_ARRAY_CONTAINS (output, "PATH=*");
138+ TEST_STR_ARRAY_CONTAINS (output, "TERM=*");
139 nih_free (output);
140
141 /*******************************************************************/
142@@ -16475,9 +16460,9 @@
143 TEST_NE_P (cmd, NULL);
144 RUN_COMMAND (NULL, cmd, &output, &line_count);
145
146- TEST_EQ (line_count, 2);
147- TEST_STR_MATCH (output[0], "PATH=*");
148- TEST_STR_MATCH (output[1], "TERM=*");
149+ TEST_GE (line_count, 2);
150+ TEST_STR_ARRAY_CONTAINS (output, "PATH=*");
151+ TEST_STR_ARRAY_CONTAINS (output, "TERM=*");
152 nih_free (output);
153
154 /*******************************************************************/
155@@ -16542,17 +16527,15 @@
156
157 fi = fopen (logfile, "r");
158 TEST_NE_P (fi, NULL);
159- TEST_FILE_MATCH (fi, "PATH=*");
160- TEST_FILE_MATCH (fi, "TERM=*");
161+ TEST_FILE_CONTAINS (fi, "PATH=*");
162+ TEST_FILE_CONTAINS (fi, "TERM=*");
163
164 /* asterisk required to match '\r\n' */
165- TEST_FILE_MATCH (fi, "UPSTART_JOB=foo*");
166- TEST_FILE_MATCH (fi, "UPSTART_INSTANCE=*");
167- TEST_FILE_MATCH (fi, "UPSTART_SESSION=*");
168- TEST_FILE_END (fi);
169+ TEST_FILE_CONTAINS (fi, "UPSTART_JOB=foo*");
170+ TEST_FILE_CONTAINS (fi, "UPSTART_INSTANCE=*");
171+ TEST_FILE_CONTAINS (fi, "UPSTART_SESSION=*");
172 fclose (fi);
173
174-
175 DELETE_FILE (confdir, "foo.conf");
176 TEST_EQ (unlink (logfile), 0);
177
178@@ -16729,6 +16712,7 @@
179 cmd = nih_sprintf (NULL, "%s reset-env 2>&1", get_initctl ());
180 TEST_NE_P (cmd, NULL);
181 RUN_COMMAND (NULL, cmd, &output, &line_count);
182+ TEST_EQ (line_count, 0);
183 nih_free (output);
184
185 /* Ensure nothing changed */
186@@ -16746,7 +16730,7 @@
187 name, value);
188 TEST_NE_P (cmd, NULL);
189 RUN_COMMAND (NULL, cmd, &output, &line_count);
190- nih_free (output);
191+ TEST_EQ (line_count, 0);
192
193 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
194 name);
195@@ -16767,6 +16751,7 @@
196 RUN_COMMAND (NULL, cmd, &output, &line_count);
197 TEST_EQ (line_count, 1);
198 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
199+ nih_free (output);
200
201 /*******************************************************************/
202 TEST_FEATURE ("set-env in 'name=' form");
203@@ -16777,6 +16762,7 @@
204 name);
205 TEST_NE_P (cmd, NULL);
206 RUN_COMMAND (NULL, cmd, &output, &line_count);
207+ TEST_EQ (line_count, 0);
208 nih_free (output);
209
210 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
211@@ -16802,6 +16788,7 @@
212 RUN_COMMAND (NULL, cmd, &output, &line_count);
213 TEST_EQ (line_count, 1);
214 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
215+ nih_free (output);
216
217 /*******************************************************************/
218 TEST_FEATURE ("set-env in 'name' form");
219@@ -16837,6 +16824,7 @@
220 RUN_COMMAND (NULL, cmd, &output, &line_count);
221 TEST_EQ (line_count, 1);
222 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
223+ nih_free (output);
224
225 /*******************************************************************/
226 TEST_FEATURE ("set-env for already set variable");
227@@ -16849,7 +16837,7 @@
228 name, value);
229 TEST_NE_P (cmd, NULL);
230 RUN_COMMAND (NULL, cmd, &output, &line_count);
231- nih_free (output);
232+ TEST_EQ (line_count, 0);
233
234 /* check it */
235 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
236@@ -16866,7 +16854,7 @@
237 name, value);
238 TEST_NE_P (cmd, NULL);
239 RUN_COMMAND (NULL, cmd, &output, &line_count);
240- nih_free (output);
241+ TEST_EQ (line_count, 0);
242
243 /* check it again */
244 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
245@@ -16889,6 +16877,7 @@
246 RUN_COMMAND (NULL, cmd, &output, &line_count);
247 TEST_EQ (line_count, 1);
248 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
249+ nih_free (output);
250
251 /*******************************************************************/
252 TEST_FEATURE ("set-env --retain");
253@@ -16901,7 +16890,7 @@
254 name, value);
255 TEST_NE_P (cmd, NULL);
256 RUN_COMMAND (NULL, cmd, &output, &line_count);
257- nih_free (output);
258+ TEST_EQ (line_count, 0);
259
260 /* check it */
261 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
262@@ -16917,7 +16906,7 @@
263 get_initctl (), name, "HELLO");
264 TEST_NE_P (cmd, NULL);
265 RUN_COMMAND (NULL, cmd, &output, &line_count);
266- nih_free (output);
267+ TEST_EQ (line_count, 0);
268
269 /* check that value did *NOT* change */
270 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
271@@ -16939,6 +16928,7 @@
272 RUN_COMMAND (NULL, cmd, &output, &line_count);
273 TEST_EQ (line_count, 1);
274 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
275+ nih_free (output);
276
277 /*******************************************************************/
278 TEST_FEATURE ("set-env with space within value and trailing tab");
279@@ -16950,7 +16940,7 @@
280 name, value);
281 TEST_NE_P (cmd, NULL);
282 RUN_COMMAND (NULL, cmd, &output, &line_count);
283- nih_free (output);
284+ TEST_EQ (line_count, 0);
285
286 cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
287 name);
288@@ -16971,6 +16961,7 @@
289 RUN_COMMAND (NULL, cmd, &output, &line_count);
290 TEST_EQ (line_count, 1);
291 TEST_EQ_STR (output[0], "initctl: No such variable: foo");
292+ nih_free (output);
293
294 /*******************************************************************/
295 TEST_FEATURE ("list-env output order");
296@@ -16981,19 +16972,19 @@
297 "zygote", "cell");
298 TEST_NE_P (cmd, NULL);
299 RUN_COMMAND (NULL, cmd, &output, &line_count);
300- nih_free (output);
301+ TEST_EQ (line_count, 0);
302
303 cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
304 "median", "middle");
305 TEST_NE_P (cmd, NULL);
306 RUN_COMMAND (NULL, cmd, &output, &line_count);
307- nih_free (output);
308+ TEST_EQ (line_count, 0);
309
310 cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
311 "aardvark", "mammal");
312 TEST_NE_P (cmd, NULL);
313 RUN_COMMAND (NULL, cmd, &output, &line_count);
314- nih_free (output);
315+ TEST_EQ (line_count, 0);
316
317 cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
318 TEST_NE_P (cmd, NULL);
319@@ -17037,13 +17028,13 @@
320 "aardvark", "mammal");
321 TEST_NE_P (cmd, NULL);
322 RUN_COMMAND (NULL, cmd, &output, &line_count);
323- nih_free (output);
324+ TEST_EQ (line_count, 0);
325
326 cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
327 "zygote", "cell");
328 TEST_NE_P (cmd, NULL);
329 RUN_COMMAND (NULL, cmd, &output, &line_count);
330- nih_free (output);
331+ TEST_EQ (line_count, 0);
332
333 cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
334 TEST_NE_P (cmd, NULL);
335@@ -17067,25 +17058,26 @@
336 "aardvark", "mammal");
337 TEST_NE_P (cmd, NULL);
338 RUN_COMMAND (NULL, cmd, &output, &line_count);
339- nih_free (output);
340+ TEST_EQ (line_count, 0);
341
342 cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
343 "FOO", "BAR");
344 TEST_NE_P (cmd, NULL);
345 RUN_COMMAND (NULL, cmd, &output, &line_count);
346- nih_free (output);
347+ TEST_EQ (line_count, 0);
348
349 cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
350 "_________", "_________");
351 TEST_NE_P (cmd, NULL);
352 RUN_COMMAND (NULL, cmd, &output, &line_count);
353- nih_free (output);
354+ TEST_EQ (line_count, 0);
355
356 CREATE_FILE (confdir, "modified-env.conf", "exec env");
357
358 cmd = nih_sprintf (NULL, "%s start modified-env 2>&1", get_initctl ());
359 TEST_NE_P (cmd, NULL);
360 RUN_COMMAND (NULL, cmd, &output, &line_count);
361+ TEST_EQ (line_count, 1);
362 nih_free (output);
363
364 logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
365@@ -17211,6 +17203,7 @@
366 TEST_NE_P (cmd, NULL);
367
368 RUN_COMMAND (NULL, cmd, &output, &line_count);
369+ TEST_EQ (line_count, 1);
370 nih_free (output);
371
372 logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
373@@ -17254,6 +17247,7 @@
374 cmd = nih_sprintf (NULL, "%s start foo 2>&1", get_initctl ());
375 TEST_NE_P (cmd, NULL);
376 RUN_COMMAND (NULL, cmd, &output, &line_count);
377+ TEST_EQ (line_count, 1);
378 nih_free (output);
379
380 logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
381@@ -17333,6 +17327,73 @@
382 /*******************************************************************/
383 }
384
385+void
386+test_no_inherit_job_env (const char *runtimedir, const char *confdir, const char *logdir)
387+{
388+ nih_local char *cmd = NULL;
389+ char **output;
390+ size_t lines;
391+ pid_t upstart_pid = 0;
392+ char *extra[] = { "--no-inherit-env", NULL };
393+ nih_local char *logfile = NULL;
394+ nih_local char *session_file = NULL;
395+ FILE *fi;
396+
397+ start_upstart_common (&upstart_pid, TRUE, confdir, logdir, extra);
398+
399+ /*******************************************************************/
400+ TEST_FEATURE ("ensure list-env in '--user --no-inherit-env' environment gives expected output");
401+
402+ cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
403+ TEST_NE_P (cmd, NULL);
404+ RUN_COMMAND (NULL, cmd, &output, &lines);
405+
406+ /* environment should comprise the default environment only */
407+ TEST_EQ (lines, 2);
408+ TEST_STR_MATCH (output[0], "PATH=*");
409+ TEST_STR_MATCH (output[1], "TERM=*");
410+ nih_free (output);
411+
412+ /*******************************************************************/
413+ TEST_FEATURE ("ensure '--user --no-inherit-env' provides expected job environment");
414+
415+ CREATE_FILE (confdir, "foo.conf", "exec env");
416+
417+ cmd = nih_sprintf (NULL, "%s start foo 2>&1", get_initctl ());
418+ TEST_NE_P (cmd, NULL);
419+ RUN_COMMAND (NULL, cmd, &output, &lines);
420+ nih_free (output);
421+
422+ logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
423+ logdir,
424+ "foo.log"));
425+
426+ WAIT_FOR_FILE (logfile);
427+
428+ fi = fopen (logfile, "r");
429+ TEST_NE_P (fi, NULL);
430+ TEST_FILE_CONTAINS (fi, "PATH=*");
431+ TEST_FILE_CONTAINS (fi, "TERM=*");
432+
433+ /* asterisk required to match '\r\n' */
434+ TEST_FILE_CONTAINS (fi, "UPSTART_JOB=foo*");
435+ TEST_FILE_CONTAINS (fi, "UPSTART_INSTANCE=*");
436+ TEST_FILE_CONTAINS (fi, "UPSTART_SESSION=*");
437+ fclose (fi);
438+
439+ DELETE_FILE (confdir, "foo.conf");
440+ TEST_EQ (unlink (logfile), 0);
441+
442+ /*******************************************************************/
443+
444+ session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
445+ runtimedir, (int)upstart_pid));
446+
447+ STOP_UPSTART (upstart_pid);
448+
449+ unlink (session_file);
450+}
451+
452 /*
453 * Test all the commands which affect the job environment table together
454 * as they are so closely related.
455@@ -17376,6 +17437,21 @@
456
457 TEST_EQ (setenv ("XDG_RUNTIME_DIR", runtimedir, 1), 0);
458
459+ /*******************************************************************/
460+ /* Ensure basic variables are set in the current environment */
461+
462+ if (! getenv ("TERM")) {
463+ fprintf (stderr, "WARNING: setting TERM to '%s' as not set\n",
464+ TEST_INITCTL_DEFAULT_TERM);
465+ assert0 (setenv ("TERM", TEST_INITCTL_DEFAULT_TERM, 1));
466+ }
467+
468+ if (! getenv ("PATH")) {
469+ fprintf (stderr, "WARNING: setting PATH to '%s' as not set\n",
470+ TEST_INITCTL_DEFAULT_PATH);
471+ assert0 (setenv ("PATH", TEST_INITCTL_DEFAULT_PATH, 1));
472+ }
473+
474 TEST_DBUS (dbus_pid);
475 start_upstart_common (&upstart_pid, TRUE, confdir, logdir, NULL);
476
477@@ -17410,14 +17486,21 @@
478 /*******************************************************************/
479
480 STOP_UPSTART (upstart_pid);
481+ session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
482+ runtimedir, (int)upstart_pid));
483+ unlink (session_file);
484+
485+ /*******************************************************************/
486+
487+ test_no_inherit_job_env (runtimedir, confdir, logdir);
488+
489+ /*******************************************************************/
490+
491 TEST_DBUS_END (dbus_pid);
492 assert0 (unsetenv ("UPSTART_CONFDIR"));
493 assert0 (unsetenv ("UPSTART_LOGDIR"));
494 assert0 (unsetenv ("UPSTART_SESSION"));
495
496- session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
497- runtimedir, (int)upstart_pid));
498- unlink (session_file);
499 session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions", runtimedir));
500 TEST_EQ (rmdir (session_file), 0);
501 session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart", runtimedir));

Subscribers

People subscribed via source and target branches