X-Git-Url: https://git.jankratochvil.net/?a=blobdiff_plain;f=src%2Forphanripper.c;h=ffccabfcff621f075a47ba37ccb6b14e54b0b1c7;hb=186b152206b499eb5dd12c8edf5e8fbeffc727d7;hp=9d5d8df69db474fb6d25a0eb9ffe8c9d50185f65;hpb=edcc77643ce7cc703aa93529b5648da5534bb907;p=nethome.git diff --git a/src/orphanripper.c b/src/orphanripper.c index 9d5d8df..ffccabf 100644 --- a/src/orphanripper.c +++ b/src/orphanripper.c @@ -41,37 +41,26 @@ #include #include #include +#include #define LENGTH(x) (sizeof (x) / sizeof (*(x))) static const char *progname; -static volatile int signal_child_hit = 0; +static volatile pid_t child; -/* We use it to race-safely emulate ppoll(2) by poll(2). */ -static int pipefd[2]; - -static void signal_child (int signo) +static void signal_chld (int signo) { - int i; - - signal_child_hit = 1; - - assert (pipefd[1] != -1); - i = close (pipefd[1]); - assert (i == 0); - pipefd[1] = -1; } -static volatile int signal_alarm_hit = 0; +static volatile int signal_alrm_hit = 0; -static void signal_alarm (int signo) +static void signal_alrm (int signo) { - signal_alarm_hit = 1; + signal_alrm_hit = 1; } static char childptyname[LINE_MAX]; -static pid_t child; static void print_child_error (const char *reason, char **argv) { @@ -86,26 +75,95 @@ static void print_child_error (const char *reason, char **argv) fputc ('\n', stderr); } +static int read_out (int amaster) +{ + char buf[LINE_MAX]; + ssize_t buf_got; + + buf_got = read (amaster, buf, sizeof buf); + if (buf_got == 0) + return 0; + /* Weird but at least after POLLHUP we get EIO instead of just EOF. */ + if (buf_got == -1 && errno == EIO) + return 0; + if (buf_got == -1 && errno == EAGAIN) + return 0; + 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); + } + return 1; +} + +/* kill (child, 0) ==0 sometimes even when CHILD's state is already Z. */ + +static int child_exited (void) +{ + char buf[200]; + int fd, i, retval; + ssize_t got; + char *state; + + snprintf (buf, sizeof (buf), "/proc/%ld/stat", (long) child); + fd = open (buf, O_RDONLY); + if (fd == -1) + { + perror ("open (/proc/CHILD/stat)"); + exit (EXIT_FAILURE); + } + got = read (fd, buf, sizeof(buf)); + if (got <= 0) + { + perror ("read (/proc/CHILD/stat)"); + exit (EXIT_FAILURE); + } + if (close (fd) != 0) + { + perror ("close (/proc/CHILD/stat)"); + exit (EXIT_FAILURE); + } + i = sscanf (buf, "%*d%*s%ms", &state); + if (i != 1) + { + perror ("sscanf (/proc/CHILD/stat)"); + exit (EXIT_FAILURE); + } + retval = strcmp (state, "Z") == 0; + free (state); + return retval; +} + static int spawn (char **argv, int timeout) { pid_t child_got; int status, amaster, i, rc; struct sigaction act; + sigset_t set; struct termios termios; unsigned alarm_orig; - i = pipe (pipefd); - assert (i == 0); - - /* We do not use signal(2) to be sure we have SA_RESTART set. */ + /* We do not use signal(2) to be sure we do not have SA_RESTART. */ memset (&act, 0, sizeof (act)); - act.sa_handler = signal_child; + act.sa_handler = signal_chld; i = sigemptyset (&act.sa_mask); assert (i == 0); - act.sa_flags = SA_RESTART; + act.sa_flags = 0; /* !SA_RESTART */ i = sigaction (SIGCHLD, &act, NULL); assert (i == 0); + i = sigemptyset (&set); + assert (i == 0); + i = sigaddset (&set, SIGCHLD); + assert (i == 0); + i = sigprocmask (SIG_SETMASK, &set, NULL); + assert (i == 0); + /* With TERMP passed as NULL we get "\n" -> "\r\n". */ termios.c_iflag = IGNBRK | IGNPAR; termios.c_oflag = 0; @@ -126,11 +184,6 @@ static int spawn (char **argv, int timeout) perror ("forkpty(3)"); exit (EXIT_FAILURE); case 0: - i = close (pipefd[0]); - assert (i == 0); - i = close (pipefd[1]); - assert (i == 0); - /* Do not replace STDIN as inferiors query its termios. */ #if 0 i = close (STDIN_FILENO); @@ -139,6 +192,11 @@ static int spawn (char **argv, int timeout) assert (i == STDIN_FILENO); #endif + i = sigemptyset (&set); + assert (i == 0); + i = sigprocmask (SIG_SETMASK, &set, NULL); + 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 @@ -168,62 +226,55 @@ static int spawn (char **argv, int timeout) exit (EXIT_FAILURE); } - /* We do not use signal(2) to be sure we have SA_RESTART set. */ - act.sa_handler = signal_alarm; - act.sa_flags &= ~SA_RESTART; + /* We do not use signal(2) to be sure we do not have SA_RESTART. */ + act.sa_handler = signal_alrm; i = sigaction (SIGALRM, &act, NULL); assert (i == 0); alarm_orig = alarm (timeout); assert (alarm_orig == 0); - while (!signal_alarm_hit) + i = sigemptyset (&set); + assert (i == 0); + + while (!signal_alrm_hit) { - struct pollfd pollfd[2]; - char buf[LINE_MAX]; - ssize_t buf_got; + struct pollfd pollfd; - pollfd[0].fd = amaster; - pollfd[0].events = POLLIN; - pollfd[1].fd = pipefd[0]; - pollfd[1].events = POLLIN; - i = poll (pollfd, LENGTH (pollfd), -1); + pollfd.fd = amaster; + pollfd.events = POLLIN; + i = ppoll (&pollfd, 1, NULL, &set); if (i == -1 && errno == EINTR) - { - /* Weird but SA_RESTART sometimes does not work. */ + { + if (child_exited ()) + break; + /* Non-CHILD child may have exited. */ continue; } - assert (i >= 1); + assert (i == 1); /* Data available? Process it first. */ - if (pollfd[0].revents & POLLIN) + if (pollfd.revents & POLLIN) { - buf_got = read (amaster, buf, sizeof buf); - if (buf_got <= 0) + if (!read_out (amaster)) { - perror ("read (amaster)"); - exit (EXIT_FAILURE); - } - if (write (STDOUT_FILENO, buf, buf_got) != buf_got) - { - perror ("write(2)"); + fprintf (stderr, "%s: Unexpected EOF\n", progname); exit (EXIT_FAILURE); } } - if (pollfd[0].revents & POLLHUP) + if (pollfd.revents & POLLHUP) break; - if ((pollfd[0].revents &= ~POLLIN) != 0) + if ((pollfd.revents &= ~POLLIN) != 0) { fprintf (stderr, "%s: ppoll(2): revents 0x%x\n", progname, - (unsigned) pollfd[0].revents); + (unsigned) pollfd.revents); exit (EXIT_FAILURE); } /* Child exited? */ - if (pollfd[1].revents & POLLHUP) + if (child_exited ()) break; - assert (pollfd[1].revents == 0); } - if (signal_alarm_hit) + if (signal_alrm_hit) { i = kill (child, SIGKILL); assert (i == 0); @@ -238,7 +289,7 @@ static int spawn (char **argv, int timeout) fprintf (stderr, "waitpid (%d) = %d: %m\n", (int) child, (int) child_got); exit (EXIT_FAILURE); } - if (signal_alarm_hit) + if (signal_alrm_hit) { char *buf; @@ -268,12 +319,23 @@ static int spawn (char **argv, int timeout) exit (EXIT_FAILURE); } - assert (signal_child_hit != 0); - - assert (pipefd[1] == -1); - i = close (pipefd[0]); + /* Not used in fact. */ + i = sigprocmask (SIG_SETMASK, &set, NULL); assert (i == 0); + /* Do not unset O_NONBLOCK as a stale child (the whole purpose of this + program) having open its output pty would block us in read_out. */ +#if 0 + i = fcntl (amaster, F_SETFL, O_RDONLY /* !O_NONBLOCK */); + if (i != 0) + { + perror ("fcntl (amaster, F_SETFL, O_RDONLY /* !O_NONBLOCK */)"); + exit (EXIT_FAILURE); + } +#endif + + while (read_out (amaster)); + /* 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. */