1 /* intr 1.4 - run a command with interrupts enabled
7 #define _POSIX_SOURCE 1
19 static char DEV_LOG
[]= "/dev/log";
21 static char DEV_LOG
[]= "/dev/console";
24 static void say(const char *s
)
26 write(2, s
, strlen(s
));
29 static void fatal(const char *label
)
41 static void usage(void)
43 say("Usage: intr [-d] [-t seconds] command [arg ...]\n");
47 int main(int argc
, char **argv
)
55 while (i
< argc
&& argv
[i
][0] == '-') {
56 char *opt
= argv
[i
++]+1, *end
;
59 if (opt
[0] == '-' && opt
[1] == 0) break;
61 while (*opt
!= 0) switch (*opt
++) {
67 /* -t n: alarm in n seconds. */
69 if (i
== argc
) usage();
72 sec
= strtoul(opt
, &end
, 10);
73 if (end
== opt
|| *end
!= 0 || (n
= sec
) != sec
)
82 if ((argc
- i
) < 1) usage();
84 /* Try to open the controlling tty. */
85 if ((fd
= open("/dev/tty", O_RDWR
)) < 0) {
86 if (errno
!= ENXIO
) fatal("/dev/tty");
90 /* Bring to the foreground. If we already have a controlling
91 * tty then use it. Otherwise try to allocate the console as
92 * controlling tty and begin a process group.
95 if (setsid() < 0) fatal("setsid()");
97 fd
= open("/dev/console", O_RDWR
);
109 /* Set the usual signals back to the default. */
110 signal(SIGHUP
, SIG_DFL
);
111 signal(SIGINT
, SIG_DFL
);
112 signal(SIGQUIT
, SIG_DFL
);
113 signal(SIGTERM
, SIG_DFL
);
115 /* Send to the background. Redirect input to /dev/null, and
116 * output to the log device. Detach from the process group.
121 if (setsid() < 0) fatal("setsid()");
123 if ((fd
= open("/dev/null", O_RDWR
)) < 0) fatal("/dev/null");
128 if ((fd
= open(DEV_LOG
, O_WRONLY
)) < 0) fatal(DEV_LOG
);
135 /* Move to the root directory. */
139 /* Schedule the alarm. (It is inherited over execve.) */
140 if (n
!= 0) alarm(n
);
143 execvp(argv
[i
], argv
+ i
);