Merge lp:~jamesodhunt/upstart/kmsg-noctty into lp:upstart

Proposed by James Hunt on 2014-01-22
Status: Merged
Merged at revision: 1592
Proposed branch: lp:~jamesodhunt/upstart/kmsg-noctty
Merge into: lp:upstart
Diff against target: 76 lines (+37/-14)
2 files modified
ChangeLog (+6/-0)
init/main.c (+31/-14)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/kmsg-noctty
Reviewer Review Type Date Requested Status
St├ęphane Graber 2014-01-22 Approve on 2014-01-22
Review via email: mp+202673@code.launchpad.net
To post a comment you must log in.
St├ęphane Graber (stgraber) wrote :

Looks good to me.

review: Approve
James Hunt (jamesodhunt) wrote :

Thanks for reviewing - merged.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2014-01-17 15:39:55 +0000
3+++ ChangeLog 2014-01-22 13:50:27 +0000
4@@ -1,3 +1,9 @@
5+2014-01-22 James Hunt <james.hunt@ubuntu.com>
6+
7+ * init/main.c: logger_kmsg(): Use open(2) rather than fopen(3) to avoid
8+ stealing the console in an container: fopen(3) may not specify
9+ O_NOCTTY and Upstart should not own the console (LP: #1263738).
10+
11 2014-01-17 James Hunt <james.hunt@ubuntu.com>
12
13 * init/conf.c:
14
15=== modified file 'init/main.c'
16--- init/main.c 2013-11-05 16:15:19 +0000
17+++ init/main.c 2014-01-22 13:50:27 +0000
18@@ -740,8 +740,12 @@
19 logger_kmsg (NihLogLevel priority,
20 const char *message)
21 {
22- int tag;
23- FILE *kmsg;
24+ int tag;
25+ int fd;
26+ ssize_t ret;
27+ size_t remaining = -1;
28+ nih_local char *buffer = NULL;
29+ char *p;
30
31 nih_assert (message != NULL);
32
33@@ -768,18 +772,31 @@
34 tag = 'd';
35 }
36
37- kmsg = fopen ("/dev/kmsg", "w");
38- if (! kmsg)
39- return -1;
40-
41- if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
42- int saved_errno = errno;
43- fclose (kmsg);
44- errno = saved_errno;
45- return -1;
46- }
47-
48- if (fclose (kmsg) < 0)
49+ fd = open ("/dev/kmsg", O_WRONLY | O_NOCTTY);
50+ if (fd < 0)
51+ return -1;
52+
53+ buffer = nih_sprintf (NULL, "<%c>%s: %s\n", tag, program_name, message);
54+ if (! buffer)
55+ goto out;
56+
57+ p = buffer;
58+
59+ remaining = strlen (p);
60+
61+ do {
62+ ret = write (fd, p, remaining);
63+ if (ret > 0) {
64+ p += ret;
65+ remaining -= ret;
66+ } else if (! ret || (ret < 0 && errno != EINTR)) {
67+ close (fd);
68+ return -1;
69+ }
70+ } while (remaining);
71+
72+out:
73+ if (close (fd) < 0)
74 return -1;
75
76 return 0;

Subscribers

People subscribed via source and target branches