X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=src%2Forphanripper.c;h=cf3039fddaf7a91e4642a8ec9df61ba36ac260e4;hp=cdf334912a18b1a1480046f244932dfe4d125f79;hb=f4cf3400508a136e366e2ce3dde132f5b1be7ac2;hpb=12128741f45224ae02b362e4acfd5a4683434f0c diff --git a/src/orphanripper.c b/src/orphanripper.c index cdf3349..cf3039f 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,46 @@ 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); +#ifdef FLUSHO + /* Workaround a readline deadlock bug in _get_tty_settings(). */ + termios.c_lflag &= ~FLUSHO; +#endif + 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 replace STDIN as inferiors query its termios. */ +#if 0 + i = close (STDIN_FILENO); + assert (i == 0); + i = open ("/dev/null", O_RDONLY); + assert (i == STDIN_FILENO); +#endif + /* 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 +141,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 +196,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. */