Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Dec 2012 22:57:38 GMT
From:      Brooks Davis <brooks@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 219893 for review
Message-ID:  <201212062257.qB6MvctJ064493@skunkworks.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@219893?ac=10

Change 219893 by brooks@brooks_zenith on 2012/12/06 22:56:43

	Resolve a race in starting the shell with sending the first
	character by starting the shell during the general startup code
	instead of when the terminal is first used.  This should
	make the race impractical to trigger by accident if not outright
	impossible.

Affected files ...

.. //depot/projects/ctsrd/cheribsd/src/ctsrd/pictview/pictview.c#9 edit

Differences ...

==== //depot/projects/ctsrd/cheribsd/src/ctsrd/pictview/pictview.c#9 (text+ko) ====

@@ -54,6 +54,8 @@
 #include <de4tc.h>
 #include <imagebox.h>
 
+static void start_keyboard_shell(void);
+
 static pid_t browser_pid;
 static pid_t kbd_pid;
 
@@ -72,9 +74,10 @@
   else {
     if (pid == browser_pid)
       browser_pid = 0;
-    else if (pid == kbd_pid)
+    else if (pid == kbd_pid) {
       kbd_pid = 0;
-    else
+      kbdfd = -1;
+    } else
       warnx("unexpected pid from wait4(): %d", pid);
   }
 }
@@ -167,6 +170,8 @@
   read_png_file("/usr/share/images/keyboardS.png", keyboard_imgs[1], keyboard_width, keyboard_height);
   read_png_file("/usr/share/images/keyboardN.png", keyboard_imgs[2], keyboard_width, keyboard_height);
   read_png_file("/usr/share/images/keyboardC.png", keyboard_imgs[3], keyboard_width, keyboard_height);
+
+  start_keyboard_shell();
 }
 
 static void
@@ -184,6 +189,31 @@
   }
 }
 
+static void
+start_keyboard_shell(void)
+{
+  int pmaster, pslave;
+
+  if (openpty(&pmaster, &pslave, NULL, NULL, NULL) == -1)
+    err(1, "openpty");
+  kbd_pid = fork();
+  if (kbd_pid < 0)
+    err(1, "fork()");
+  else if (kbd_pid > 0) {
+    close(pslave);
+    kbdfd = pmaster;
+  } else {
+    close(pmaster);
+    if (login_tty(pslave) < 0) {
+      syslog(LOG_ALERT, "login_tty failed in child: %s", strerror(errno));
+      err(1, "tty_login");
+    }
+    execl("/bin/sh", "sh", NULL);
+    syslog(LOG_ALERT, "exec of /bin/sh failed: %s", strerror(errno));
+    err(1, "execl()");
+  }
+}
+
 void
 keyboard_on(void)
 {
@@ -198,8 +228,7 @@
   const int poll_timeout = touch_timeout;
   int wait_poll_timeout = poll_timeout;
 
-  static int pmaster;
-  int pslave, n;
+  int n;
   char *devpath, buf[1024];
   ssize_t rlen;
   struct pollfd pfd[1];
@@ -232,26 +261,8 @@
   keymap[1][1][3] = '\xff';
   keymap[2][1][3] = '\xff';
 
-  if (kbdfd < 0) {
-    if (openpty(&pmaster, &pslave, NULL, NULL, NULL) == -1)
-      err(1, "openpty");
-    kbd_pid = fork();
-    if (kbd_pid < 0)
-      err(1, "fork()");
-    else if (kbd_pid > 0) {
-      close(pslave);
-      kbdfd = pmaster;
-    } else {
-      close(pmaster);
-      if (login_tty(pslave) < 0) {
-        syslog(LOG_ALERT, "login_tty failed in child: %s", strerror(errno));
-	err(1, "tty_login");
-      }
-      execl("/bin/sh", "sh", NULL);
-      syslog(LOG_ALERT, "exec of /bin/sh failed: %s", strerror(errno));
-      err(1, "execl()");
-    }
-  }
+  if (kbdfd < 0)
+    start_keyboard_shell();
 
   fb_fade2off();
   fb_fade2text(127);
@@ -339,7 +350,7 @@
       wait_poll_timeout = poll_timeout;
 
       /* Check for output from the child and post it if needed */
-      pfd[0].fd = pmaster;
+      pfd[0].fd = kbdfd;
       pfd[0].events = POLLIN;
       n = poll(pfd, 1, 0);
       if (n == 0)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201212062257.qB6MvctJ064493>