1 | /* $Id: timeout.c,v 1.9 1999-01-22 23:16:24 ghudson Exp $ */ |
---|
2 | |
---|
3 | #include <mit-copyright.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <signal.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <sys/stat.h> |
---|
8 | #include <sys/time.h> |
---|
9 | #ifdef _AIX |
---|
10 | #define _POSIX_SOURCE |
---|
11 | #undef _BSD |
---|
12 | #endif |
---|
13 | #include <sys/wait.h> |
---|
14 | #ifdef _AIX |
---|
15 | #undef _POSIX_SOURCE |
---|
16 | #endif |
---|
17 | |
---|
18 | #ifndef FALSE |
---|
19 | #define FALSE 0 |
---|
20 | #define TRUE (!FALSE) |
---|
21 | #endif |
---|
22 | |
---|
23 | #ifndef __STDC__ |
---|
24 | #define volatile |
---|
25 | #endif |
---|
26 | |
---|
27 | int app_pid; |
---|
28 | volatile int app_running; |
---|
29 | int app_exit_status; |
---|
30 | main(argc, argv) |
---|
31 | int argc; |
---|
32 | char **argv; |
---|
33 | { |
---|
34 | int maxidle, ttl; |
---|
35 | char *name, msg[512]; |
---|
36 | struct stat stbuf; |
---|
37 | struct timeval now, start; |
---|
38 | void child(), wakeup(); |
---|
39 | #ifdef POSIX |
---|
40 | struct sigaction sigact; |
---|
41 | sigemptyset(&sigact.sa_mask); |
---|
42 | sigact.sa_flags = 0; |
---|
43 | #endif |
---|
44 | |
---|
45 | name = *argv++; |
---|
46 | if (argc < 3) { |
---|
47 | fprintf(stderr, "usage: %s max-idle-time application command line...\n", |
---|
48 | name); |
---|
49 | exit(1); |
---|
50 | } |
---|
51 | maxidle = atoi(*argv++); |
---|
52 | argc -= 2; |
---|
53 | |
---|
54 | app_running = TRUE; |
---|
55 | #ifdef POSIX |
---|
56 | sigact.sa_handler = child; |
---|
57 | sigaction(SIGCHLD, &sigact, NULL); |
---|
58 | sigact.sa_handler = wakeup; |
---|
59 | sigaction(SIGALRM, &sigact, NULL); |
---|
60 | #else |
---|
61 | signal(SIGCHLD, child); |
---|
62 | signal(SIGALRM, wakeup); |
---|
63 | #endif |
---|
64 | /* launch application */ |
---|
65 | switch (app_pid = fork()) { |
---|
66 | case 0: |
---|
67 | execvp(argv[0], argv); |
---|
68 | sprintf(msg, "%s: failed to exec application %s\n", name, argv[0]); |
---|
69 | perror(msg); |
---|
70 | exit(1); |
---|
71 | case -1: |
---|
72 | sprintf(msg, "%s: failed to fork to create application\n", name); |
---|
73 | perror(msg); |
---|
74 | exit(1); |
---|
75 | default: |
---|
76 | break; |
---|
77 | } |
---|
78 | |
---|
79 | gettimeofday(&start, NULL); |
---|
80 | ttl = maxidle; |
---|
81 | |
---|
82 | /* wait for application to die or idle-time to be reached */ |
---|
83 | while (app_running) { |
---|
84 | alarm(ttl); |
---|
85 | sigpause(0); |
---|
86 | if (!app_running) |
---|
87 | break; |
---|
88 | fstat(1, &stbuf); |
---|
89 | gettimeofday(&now, NULL); |
---|
90 | ttl = start.tv_sec + maxidle - now.tv_sec; |
---|
91 | /* only check idle time if we've been running at least that long */ |
---|
92 | if (ttl <= 0) |
---|
93 | ttl = stbuf.st_atime + maxidle - now.tv_sec; |
---|
94 | if (ttl <= 0) { |
---|
95 | fprintf(stderr, "\nMAX IDLE TIME REACHED.\n"); |
---|
96 | kill(app_pid, SIGINT); |
---|
97 | exit(0); |
---|
98 | } |
---|
99 | } |
---|
100 | exit(app_exit_status); |
---|
101 | } |
---|
102 | |
---|
103 | void child() |
---|
104 | { |
---|
105 | #ifdef POSIX |
---|
106 | int status; |
---|
107 | #else |
---|
108 | union wait status; |
---|
109 | #endif |
---|
110 | int pid; |
---|
111 | |
---|
112 | #ifdef POSIX |
---|
113 | pid = waitpid(-1, &status, WNOHANG); |
---|
114 | #else |
---|
115 | pid = wait3(&status, WNOHANG, 0); |
---|
116 | #endif |
---|
117 | if (pid != app_pid || WIFSTOPPED(status)) |
---|
118 | return; |
---|
119 | app_running = FALSE; |
---|
120 | if (WIFEXITED(status)) |
---|
121 | #ifdef POSIX |
---|
122 | app_exit_status = WEXITSTATUS(status); |
---|
123 | else |
---|
124 | app_exit_status = WTERMSIG(status); |
---|
125 | #else |
---|
126 | app_exit_status = status.w_retcode; |
---|
127 | else |
---|
128 | app_exit_status = -status.w_termsig; |
---|
129 | #endif |
---|
130 | } |
---|
131 | |
---|
132 | void wakeup() |
---|
133 | { |
---|
134 | } |
---|