From owner-p4-projects@FreeBSD.ORG Thu Dec 6 22:57:39 2012 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id EF701FC4; Thu, 6 Dec 2012 22:57:38 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A759EFC2 for ; Thu, 6 Dec 2012 22:57:38 +0000 (UTC) (envelope-from brooks@freebsd.org) Received: from skunkworks.freebsd.org (skunkworks.freebsd.org [IPv6:2001:4f8:fff6::2d]) by mx1.freebsd.org (Postfix) with ESMTP id 8531C8FC08 for ; Thu, 6 Dec 2012 22:57:38 +0000 (UTC) Received: from skunkworks.freebsd.org (localhost [127.0.0.1]) by skunkworks.freebsd.org (8.14.5/8.14.5) with ESMTP id qB6MvcF6064496 for ; Thu, 6 Dec 2012 22:57:38 GMT (envelope-from brooks@freebsd.org) Received: (from perforce@localhost) by skunkworks.freebsd.org (8.14.5/8.14.5/Submit) id qB6MvctJ064493 for perforce@freebsd.org; Thu, 6 Dec 2012 22:57:38 GMT (envelope-from brooks@freebsd.org) Date: Thu, 6 Dec 2012 22:57:38 GMT Message-Id: <201212062257.qB6MvctJ064493@skunkworks.freebsd.org> X-Authentication-Warning: skunkworks.freebsd.org: perforce set sender to brooks@freebsd.org using -f From: Brooks Davis Subject: PERFORCE change 219893 for review To: Perforce Change Reviews Precedence: bulk X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.14 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Dec 2012 22:57:39 -0000 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 #include +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)