Attachment 'timeout.c'

Download

   1 #include <sys/types.h>
   2 #include <unistd.h>
   3 #include <stdlib.h>
   4 #include <signal.h>
   5 #include <stdio.h>
   6 #include <errno.h>
   7 #include <string.h>
   8 
   9 int main(int argc, char *argv[])
  10 {
  11     const pid_t parent = getpid();
  12     int timeout;
  13 
  14     if (argc < 3) {
  15         fprintf(stderr, "Usage: %s TIMEOUT COMMAND [ARG]...\n", argv[0]);
  16         return 2;
  17     }
  18     timeout = strtol(argv[1], NULL, 10);
  19     if (timeout <= 0) {
  20         fprintf(stderr, "%s: %s: Invalid argument\n", argv[0], argv[1]);
  21         return 2;
  22     }
  23 
  24     switch (fork()) {
  25         case -1: { // fork failed
  26             fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
  27             return 126;
  28         }
  29         case 0: { // this is the child, a parricidal watchdog
  30             int seconds;
  31             fclose(stdin);
  32             fclose(stdout);
  33             fclose(stderr);
  34             for (seconds = 0; seconds < timeout; seconds++) {
  35                 sleep(1);
  36                 if (kill(parent, 0) == -1) // has the parent already finished?
  37                     return 0; // okay, there's nothing left to do
  38             }
  39             kill(parent, SIGTERM);
  40             sleep(5);
  41             kill(parent, SIGKILL);
  42             return 0;
  43         }
  44         default: { // this is the parent
  45             execvp(argv[2], &argv[2]);
  46             // upon success, execvp will not return here
  47             if (errno == 2) {
  48                 fprintf(stderr, "%s: %s: Command not found\n", argv[0], argv[2]);
  49                 return 127; // not found
  50             } else {
  51                 fprintf(stderr, "%s: %s: %s\n", argv[0], argv[2], strerror(errno));
  52                 return 126; // not executable
  53             }
  54         }
  55     }
  56 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-10-29 21:58:46, 0.8 KB) [[attachment:asc.c]]
  • [get | view] (2008-10-29 15:52:36, 0.6 KB) [[attachment:bbox]]
  • [get | view] (2008-10-29 15:52:48, 0.8 KB) [[attachment:bib2bib]]
  • [get | view] (2008-10-29 21:58:53, 0.0 KB) [[attachment:bits.c]]
  • [get | view] (2008-10-29 21:58:59, 0.7 KB) [[attachment:claus.c]]
  • [get | view] (2008-10-29 15:53:02, 0.9 KB) [[attachment:cvstags]]
  • [get | view] (2008-10-29 15:53:20, 0.7 KB) [[attachment:german2iso]]
  • [get | view] (2008-10-29 15:53:26, 1.1 KB) [[attachment:germanuml]]
  • [get | view] (2008-10-29 15:53:34, 0.4 KB) [[attachment:gnuplot_diff]]
  • [get | view] (2008-10-29 15:53:42, 1.2 KB) [[attachment:httphead]]
  • [get | view] (2008-10-30 10:56:00, 2.4 KB) [[attachment:ldapphone.cc]]
  • [get | view] (2008-10-29 21:59:14, 0.0 KB) [[attachment:leak.cc]]
  • [get | view] (2008-10-29 22:00:45, 1.3 KB) [[attachment:lfc-lr.cc]]
  • [get | view] (2008-10-29 22:00:55, 1.9 KB) [[attachment:lfc-se-ls.cc]]
  • [get | view] (2008-10-29 22:01:03, 2.2 KB) [[attachment:lfc-se-user.cc]]
  • [get | view] (2008-10-29 22:01:09, 1.5 KB) [[attachment:lfc-sfn2lfn.cc]]
  • [get | view] (2008-10-29 21:59:35, 0.4 KB) [[attachment:liststat.cc]]
  • [get | view] (2008-10-29 21:59:41, 0.2 KB) [[attachment:listsum.cc]]
  • [get | view] (2008-10-29 21:59:47, 0.4 KB) [[attachment:listtab.cc]]
  • [get | view] (2008-10-29 15:53:55, 1.3 KB) [[attachment:lsf]]
  • [get | view] (2008-10-29 15:54:01, 0.3 KB) [[attachment:mangrep]]
  • [get | view] (2008-10-29 15:54:09, 0.4 KB) [[attachment:msec2time]]
  • [get | view] (2008-10-29 15:54:17, 0.3 KB) [[attachment:notouch]]
  • [get | view] (2008-10-29 15:54:27, 1.1 KB) [[attachment:pathgrep]]
  • [get | view] (2008-10-29 21:59:55, 0.7 KB) [[attachment:realtime.cc]]
  • [get | view] (2008-10-29 15:54:39, 0.5 KB) [[attachment:root2iso]]
  • [get | view] (2008-10-29 15:54:47, 0.4 KB) [[attachment:rpath]]
  • [get | view] (2008-10-29 15:54:58, 0.7 KB) [[attachment:sourcegrep]]
  • [get | view] (2008-10-29 15:55:06, 0.3 KB) [[attachment:system]]
  • [get | view] (2008-10-29 22:00:07, 1.6 KB) [[attachment:timeout.c]]
  • [get | view] (2008-11-18 12:03:33, 0.4 KB) [[attachment:unicode]]
  • [get | view] (2008-10-29 22:00:15, 0.4 KB) [[attachment:unixtime2time.c]]
  • [get | view] (2008-10-29 15:55:18, 0.1 KB) [[attachment:win]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.