From 57cf3ccb23ec78e49ada0646b06793c8da5561aa Mon Sep 17 00:00:00 2001 From: lace <> Date: Tue, 10 Jul 2007 16:45:00 +0000 Subject: [PATCH] Fixed backwards compatibility with RHEL-4 - missing ppoll(2). Fixed output termios formatting. Fixed some race-missed last output data. --- src/orphanripper.c | 100 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/src/orphanripper.c b/src/orphanripper.c index cdf3349..0615aa4 100644 --- a/src/orphanripper.c +++ b/src/orphanripper.c @@ -24,6 +24,7 @@ * 2007-07-10 Jan Kratochvil */ +/* For getpgid(2). */ #define _GNU_SOURCE 1 #include @@ -41,13 +42,25 @@ #include #include +#define LENGTH(x) (sizeof (x) / sizeof (*(x))) + static const char *progname; static volatile int signal_child_hit = 0; +/* We use it to race-safely emulate ppoll(2) by poll(2). */ +static int pipefd[2]; + static void signal_child (int signo) { + int i; + signal_child_hit = 1; + + assert (pipefd[1] != -1); + i = close (pipefd[1]); + assert (i == 0); + pipefd[1] = -1; } static char childptyname[LINE_MAX]; @@ -58,34 +71,33 @@ static int spawn (char **argv) pid_t child_got; int status, amaster, i; struct sigaction act; - sigset_t sigset; + struct termios termios; - /* Never miss SIGCHLD. */ - i = sigemptyset (&sigset); - assert (i == 0); - i = sigaddset (&sigset, SIGCHLD); - assert (i == 0); - i = sigprocmask (SIG_BLOCK, &sigset, NULL); - assert (i == 0); - i = sigemptyset (&sigset); + i = pipe (pipefd); assert (i == 0); - /* We do not use signal(2) to be sure we have SA_RESTART unset. */ + /* We do not use signal(2) to be sure we have SA_RESTART set. */ memset (&act, 0, sizeof (act)); act.sa_handler = signal_child; i = sigemptyset (&act.sa_mask); assert (i == 0); - act.sa_flags = 0; + act.sa_flags = SA_RESTART; i = sigaction (SIGCHLD, &act, NULL); assert (i == 0); - child = forkpty (&amaster, childptyname, NULL, NULL); + /* With TERMP passed as NULL we get "\n" -> "\r\n". */ + cfmakeraw (&termios); + child = forkpty (&amaster, childptyname, &termios, NULL); switch (child) { case -1: perror ("forkpty(3)"); exit (EXIT_FAILURE); case 0: + i = close (pipefd[0]); + assert (i == 0); + i = close (pipefd[1]); + assert (i == 0); /* Do not setpgrp(2) in the parent process as the process-group is shared for the whole sh(1) pipeline we could be a part of. The process-group is set according to PID of the first @@ -116,42 +128,48 @@ static int spawn (char **argv) } for (;;) { - struct pollfd pollfd; + struct pollfd pollfd[2]; char buf[LINE_MAX]; ssize_t buf_got; - pollfd.fd = amaster; - pollfd.events = POLLIN; - i = ppoll (&pollfd, 1, NULL, &sigset); - assert (i == -1 || i == 1); - if (i == -1) - { - assert (errno == EINTR && signal_child_hit != 0); - break; - } - /* i == 1 */ - assert (pollfd.revents != 0); - if (pollfd.revents & POLLHUP) - break; - if (pollfd.revents != POLLIN) + pollfd[0].fd = amaster; + pollfd[0].events = POLLIN; + pollfd[1].fd = pipefd[0]; + pollfd[1].events = POLLIN; + i = poll (pollfd, LENGTH (pollfd), -1); + if (i == -1 && errno == EINTR) { - fprintf (stderr, "%s: ppoll(2): revents 0x%x\n", progname, - (unsigned) pollfd.revents); - exit (EXIT_FAILURE); + /* Weird but SA_RESTART sometimes does not work. */ + continue; } - buf_got = read (amaster, buf, sizeof buf); - if (buf_got == 0) - break; - if (buf_got == -1) + assert (i >= 1); + /* Data available? Process it first. */ + if (pollfd[0].revents & POLLIN) { - perror ("read (amaster)"); - exit (EXIT_FAILURE); + buf_got = read (amaster, buf, sizeof buf); + if (buf_got <= 0) + { + perror ("read (amaster)"); + exit (EXIT_FAILURE); + } + if (write (STDOUT_FILENO, buf, buf_got) != buf_got) + { + perror ("write(2)"); + exit (EXIT_FAILURE); + } } - if (write (STDOUT_FILENO, buf, buf_got) != buf_got) + if (pollfd[0].revents & POLLHUP) + break; + if ((pollfd[0].revents &= ~POLLIN) != 0) { - perror ("write(2)"); + fprintf (stderr, "%s: ppoll(2): revents 0x%x\n", progname, + (unsigned) pollfd[0].revents); exit (EXIT_FAILURE); } + /* Child exited? */ + if (pollfd[1].revents & POLLHUP) + break; + assert (pollfd[1].revents == 0); } /* WNOHANG still could fail. */ child_got = waitpid (child, &status, 0); @@ -165,6 +183,12 @@ static int spawn (char **argv) fprintf (stderr, "waitpid (%d): !WIFEXITED (%d)\n", (int) child, status); exit (EXIT_FAILURE); } + + assert (signal_child_hit != 0); + assert (pipefd[1] == -1); + i = close (pipefd[0]); + assert (i == 0); + /* Do not close the master FD as the child would have `/dev/pts/23 (deleted)' entries which are not expected (and expecting ` (deleted)' would be a race. */ -- 1.8.3.1