From edcc77643ce7cc703aa93529b5648da5534bb907 Mon Sep 17 00:00:00 2001 From: lace <> Date: Sun, 24 Feb 2008 04:35:59 +0000 Subject: [PATCH] New options `-t': timeout --- src/orphanripper.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 106 insertions(+), 13 deletions(-) diff --git a/src/orphanripper.c b/src/orphanripper.c index f60651a..9d5d8df 100644 --- a/src/orphanripper.c +++ b/src/orphanripper.c @@ -63,15 +63,36 @@ static void signal_child (int signo) pipefd[1] = -1; } +static volatile int signal_alarm_hit = 0; + +static void signal_alarm (int signo) +{ + signal_alarm_hit = 1; +} + static char childptyname[LINE_MAX]; static pid_t child; -static int spawn (char **argv) +static void print_child_error (const char *reason, char **argv) +{ + char **sp; + + fprintf (stderr, "%s: %d %s:", progname, (int) child, reason); + for (sp = argv; *sp != NULL; sp++) + { + fputc (' ', stderr); + fputs (*sp, stderr); + } + fputc ('\n', stderr); +} + +static int spawn (char **argv, int timeout) { pid_t child_got; - int status, amaster, i; + int status, amaster, i, rc; struct sigaction act; struct termios termios; + unsigned alarm_orig; i = pipe (pipefd); assert (i == 0); @@ -134,7 +155,7 @@ static int spawn (char **argv) perror ("getpgrp(2)"); exit (EXIT_FAILURE); } - execvp (argv[1], argv + 1); + execvp (argv[0], argv); perror ("execvp(2)"); exit (EXIT_FAILURE); default: @@ -146,7 +167,17 @@ static int spawn (char **argv) perror ("fcntl (amaster, F_SETFL, O_NONBLOCK)"); exit (EXIT_FAILURE); } - for (;;) + + /* We do not use signal(2) to be sure we have SA_RESTART set. */ + act.sa_handler = signal_alarm; + act.sa_flags &= ~SA_RESTART; + i = sigaction (SIGALRM, &act, NULL); + assert (i == 0); + + alarm_orig = alarm (timeout); + assert (alarm_orig == 0); + + while (!signal_alarm_hit) { struct pollfd pollfd[2]; char buf[LINE_MAX]; @@ -191,6 +222,15 @@ static int spawn (char **argv) break; assert (pollfd[1].revents == 0); } + + if (signal_alarm_hit) + { + i = kill (child, SIGKILL); + assert (i == 0); + } + else + alarm (0); + /* WNOHANG still could fail. */ child_got = waitpid (child, &status, 0); if (child != child_got) @@ -198,13 +238,38 @@ static int spawn (char **argv) fprintf (stderr, "waitpid (%d) = %d: %m\n", (int) child, (int) child_got); exit (EXIT_FAILURE); } - if (!WIFEXITED (status)) + if (signal_alarm_hit) + { + char *buf; + + if (asprintf (&buf, "Timed out after %d seconds", timeout) != -1) + { + print_child_error (buf, argv); + free (buf); + } + rc = 128 + SIGALRM; + } + else if (WIFEXITED (status)) + rc = WEXITSTATUS (status); + else if (WIFSIGNALED (status)) + { + print_child_error (strsignal (WTERMSIG (status)), argv); + rc = 128 + WTERMSIG (status); + } + else if (WIFSTOPPED (status)) + { + fprintf (stderr, "waitpid (%d): WIFSTOPPED - WSTOPSIG is %d\n", + (int) child, WSTOPSIG (status)); + exit (EXIT_FAILURE); + } + else { 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); @@ -221,7 +286,7 @@ static int spawn (char **argv) } #endif - return WEXITSTATUS (status); + return rc; } /* Detected commandline may look weird due to a race: @@ -296,7 +361,7 @@ static int dir_scan (const char *dirname, pathname_len = snprintf (pathname, sizeof pathname, "%s/%s", dirname, dirent->d_name); - if (pathname_len <= 0 || pathname_len >= sizeof pathname) + if (pathname_len <= 0 || pathname_len >= (int) sizeof pathname) { fprintf (stderr, "entry file name too long: `%s' / `%s'\n", dirname, dirent->d_name); @@ -372,7 +437,7 @@ static int fd_fs_scan (pid_t pid, int (*func) (pid_t pid, const char *link)) if (dirent->d_type == DT_DIR) return 0; buf_len = readlink (pathname, buf, sizeof buf - 1); - if (buf_len <= 0 || buf_len >= sizeof buf - 1) + if (buf_len <= 0 || buf_len >= (ssize_t) sizeof buf - 1) { if (errno != ENOENT && errno != EACCES) fprintf (stderr, "Error reading link \"%s\": %m\n", pathname); @@ -582,16 +647,44 @@ static void rip (void) int main (int argc, char **argv) { + int timeout = 0; int rc; - if (argc < 2 || strcmp (argv[1], "-h") == 0 - || strcmp (argv[1], "--help") == 0) + progname = *argv++; + argc--; + + if (argc < 1 || strcmp (*argv, "-h") == 0 + || strcmp (*argv, "--help") == 0) { - fputs ("Syntax: orphanripper \n", stdout); + puts ("Syntax: orphanripper [-t ] "); exit (EXIT_FAILURE); } - progname = argv[0]; - rc = spawn (argv); + if ((*argv)[0] == '-' && (*argv)[1] == 't') + { + char *timeout_s = NULL; + + if ((*argv)[2] == 0) + timeout_s = *++argv; + else if (isdigit ((*argv)[2])) + timeout_s = (*argv) + 2; + if (timeout_s != NULL) + { + long l; + char *endptr; + + argv++; + l = strtol (timeout_s, &endptr, 0); + timeout = l; + if ((endptr != NULL && *endptr != 0) || timeout < 0 || timeout != l) + { + fprintf (stderr, "%s: Invalid timeout value: %s\n", progname, + timeout_s); + exit (EXIT_FAILURE); + } + } + } + + rc = spawn (argv, timeout); rip (); return rc; } -- 1.8.3.1