James Hunt has proposed merging lp:~jamesodhunt/upstart/kmsg-noctty into 
lp:upstart.

Requested reviews:
  Upstart Reviewers (upstart-reviewers)
Related bugs:
  Bug #1263738 in upstart : "login console 0 in user namespace container is not 
configured right"
  https://bugs.launchpad.net/upstart/+bug/1263738

For more details, see:
https://code.launchpad.net/~jamesodhunt/upstart/kmsg-noctty/+merge/202673
-- 
https://code.launchpad.net/~jamesodhunt/upstart/kmsg-noctty/+merge/202673
Your team Upstart Reviewers is requested to review the proposed merge of 
lp:~jamesodhunt/upstart/kmsg-noctty into lp:upstart.
=== modified file 'ChangeLog'
--- ChangeLog	2014-01-17 15:39:55 +0000
+++ ChangeLog	2014-01-22 13:50:27 +0000
@@ -1,3 +1,9 @@
+2014-01-22  James Hunt  <[email protected]>
+
+	* init/main.c: logger_kmsg(): Use open(2) rather than fopen(3) to avoid
+	  stealing the console in an container: fopen(3) may not specify
+	  O_NOCTTY and Upstart should not own the console (LP: #1263738).
+
 2014-01-17  James Hunt  <[email protected]>
 
 	* init/conf.c:

=== modified file 'init/main.c'
--- init/main.c	2013-11-05 16:15:19 +0000
+++ init/main.c	2014-01-22 13:50:27 +0000
@@ -740,8 +740,12 @@
 logger_kmsg (NihLogLevel priority,
 	     const char *message)
 {
-	int   tag;
-	FILE *kmsg;
+	int             tag;
+	int             fd;
+	ssize_t         ret;
+	size_t          remaining = -1;
+	nih_local char *buffer = NULL;
+	char           *p;
 
 	nih_assert (message != NULL);
 
@@ -768,18 +772,31 @@
 		tag = 'd';
 	}
 
-	kmsg = fopen ("/dev/kmsg", "w");
-	if (! kmsg)
-		return -1;
-
-	if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
-		int saved_errno = errno;
-		fclose (kmsg);
-		errno = saved_errno;
-		return -1;
-	}
-
-	if (fclose (kmsg) < 0)
+	fd = open ("/dev/kmsg", O_WRONLY | O_NOCTTY);
+	if (fd < 0)
+		return -1;
+
+	buffer = nih_sprintf (NULL, "<%c>%s: %s\n", tag, program_name, message);
+	if (! buffer)
+		goto out;
+
+	p = buffer;
+
+	remaining = strlen (p);
+
+	do {
+		ret = write (fd, p, remaining);
+		if (ret > 0) {
+			p += ret;
+			remaining -= ret;
+		} else if (! ret || (ret < 0 && errno != EINTR)) {
+			close (fd);
+			return -1;
+		}
+	} while (remaining);
+
+out:
+	if (close (fd) < 0)
 		return -1;
 
 	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