blob: 6a0a9a6e634291f309624e190a04d4a374174d44 [file] [log] [blame]
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +00001/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * COPYING NOTES
3 *
4 * timeout.c -- a timeout handler for shell commands
5 *
6 * Copyright (C) 2005-6, Roberto A. Foglietta <me@roberto.foglietta.name>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20 */
21/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
22 * REVISION NOTES:
23 * released 17-11-2005 by Roberto A. Foglietta
24 * talarm 04-12-2005 by Roberto A. Foglietta
25 * modified 05-12-2005 by Roberto A. Foglietta
26 * sizerdct 06-12-2005 by Roberto A. Foglietta
27 * splitszf 12-05-2006 by Roberto A. Foglietta
28 * rewrite 14-11-2008 vda
29 */
30
31#include "libbb.h"
32
33int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34int timeout_main(int argc UNUSED_PARAM, char **argv)
35{
36 int signo;
37 int status;
38 int parent = 0;
39 int timeout = 10;
40 pid_t pid;
41#if !BB_MMU
42 char *sv1, *sv2;
43#endif
44 const char *opt_s = "TERM";
45
46 /* -p option is not documented, it is needed to support NOMMU. */
47
48 /* -t SECONDS; -p PARENT_PID */
49 opt_complementary = "t+" USE_FOR_NOMMU(":p+");
50 getopt32(argv, "s:t" USE_FOR_NOMMU(":p:"), &opt_s, &timeout, &parent);
51 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
52 signo = get_signum(opt_s);
53 if (signo < 0)
54 bb_error_msg_and_die("unknown signal '%s'", opt_s);
55
56 /* We want to create a grandchild which will watch
57 * and kill the grandparent. Other methods:
58 * making parent watch child disrupts parent<->child link
59 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
60 * it's better if service_prog is a child of tcpsvd!),
61 * making child watch parent results in programs having
62 * unexpected children. */
63
64 if (parent) /* we were re-execed, already grandchild */
65 goto grandchild;
66 if (!argv[optind]) /* no PROG? */
67 bb_show_usage();
68
69 pid = vfork();
70 if (pid < 0)
71 bb_perror_msg_and_die("vfork");
72#if !BB_MMU
73 sv1 = argv[optind];
74 sv2 = argv[optind + 1];
75#endif
76 if (pid == 0) {
77 /* Child: spawn grandchild and exit */
78 parent = getppid();
79#if !BB_MMU
80 argv[optind] = xasprintf("-p%u", parent);
81 argv[optind + 1] = NULL;
82#endif
83 /* NB: exits with nonzero on error: */
84 bb_daemonize_or_rexec(0, argv);
85 /* Here we are grandchild. Sleep, then kill grandparent */
86 grandchild:
87 /* Just sleep(NUGE_NUM); kill(parent) may kill wrong process! */
88 while (1) {
89 sleep(1);
90 if (--timeout <= 0)
91 break;
92 if (kill(parent, 0)) {
93 /* process is gone */
94 return EXIT_SUCCESS;
95 }
96 }
97 kill(parent, signo);
98 return EXIT_SUCCESS;
99 }
100
101 /* Parent */
102 wait(&status); /* wait for child to die */
103 /* Did intermediate [v]fork or exec fail? */
104 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
105 return EXIT_FAILURE;
106 /* Ok, exec a program as requested */
107 argv += optind;
108#if !BB_MMU
109 argv[0] = sv1;
110 argv[1] = sv2;
111#endif
112 BB_EXECVP(argv[0], argv);
113 bb_perror_msg_and_die("exec '%s'", argv[0]);
114}
115/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
116 * COPYING NOTES
117 *
118 * timeout.c -- a timeout handler for shell commands
119 *
120 * Copyright (C) 2005-6, Roberto A. Foglietta <me@roberto.foglietta.name>
121 *
122 * This program is free software; you can redistribute it and/or modify
123 * it under the terms of the GNU General Public License as published by
124 * the Free Software Foundation; version 2 of the License.
125 *
126 * This program is distributed in the hope that it will be useful,
127 * but WITHOUT ANY WARRANTY; without even the implied warranty of
128 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129 * GNU General Public License for more details.
130 *
131 * You should have received a copy of the GNU General Public License
132 * along with this program; if not, write to the Free Software
133 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
134 */
135/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
136 * REVISION NOTES:
137 * released 17-11-2005 by Roberto A. Foglietta
138 * talarm 04-12-2005 by Roberto A. Foglietta
139 * modified 05-12-2005 by Roberto A. Foglietta
140 * sizerdct 06-12-2005 by Roberto A. Foglietta
141 * splitszf 12-05-2006 by Roberto A. Foglietta
142 * rewrite 14-11-2008 vda
143 */
144
145#include "libbb.h"
146
147int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
148int timeout_main(int argc UNUSED_PARAM, char **argv)
149{
150 int signo;
151 int status;
152 int parent = 0;
153 int timeout = 10;
154 pid_t pid;
155#if !BB_MMU
156 char *sv1, *sv2;
157#endif
158 const char *opt_s = "TERM";
159
160 /* -p option is not documented, it is needed to support NOMMU. */
161
162 /* -t SECONDS; -p PARENT_PID */
163 opt_complementary = "t+" USE_FOR_NOMMU(":p+");
164 getopt32(argv, "s:t" USE_FOR_NOMMU(":p:"), &opt_s, &timeout, &parent);
165 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
166 signo = get_signum(opt_s);
167 if (signo < 0)
168 bb_error_msg_and_die("unknown signal '%s'", opt_s);
169
170 /* We want to create a grandchild which will watch
171 * and kill the grandparent. Other methods:
172 * making parent watch child disrupts parent<->child link
173 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
174 * it's better if service_prog is a child of tcpsvd!),
175 * making child watch parent results in programs having
176 * unexpected children. */
177
178 if (parent) /* we were re-execed, already grandchild */
179 goto grandchild;
180 if (!argv[optind]) /* no PROG? */
181 bb_show_usage();
182
183 pid = vfork();
184 if (pid < 0)
185 bb_perror_msg_and_die("vfork");
186#if !BB_MMU
187 sv1 = argv[optind];
188 sv2 = argv[optind + 1];
189#endif
190 if (pid == 0) {
191 /* Child: spawn grandchild and exit */
192 parent = getppid();
193#if !BB_MMU
194 argv[optind] = xasprintf("-p%u", parent);
195 argv[optind + 1] = NULL;
196#endif
197 /* NB: exits with nonzero on error: */
198 bb_daemonize_or_rexec(0, argv);
199 /* Here we are grandchild. Sleep, then kill grandparent */
200 grandchild:
201 /* Just sleep(NUGE_NUM); kill(parent) may kill wrong process! */
202 while (1) {
203 sleep(1);
204 if (--timeout <= 0)
205 break;
206 if (kill(parent, 0)) {
207 /* process is gone */
208 return EXIT_SUCCESS;
209 }
210 }
211 kill(parent, signo);
212 return EXIT_SUCCESS;
213 }
214
215 /* Parent */
216 wait(&status); /* wait for child to die */
217 /* Did intermediate [v]fork or exec fail? */
218 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
219 return EXIT_FAILURE;
220 /* Ok, exec a program as requested */
221 argv += optind;
222#if !BB_MMU
223 argv[0] = sv1;
224 argv[1] = sv2;
225#endif
226 BB_EXECVP(argv[0], argv);
227 bb_perror_msg_and_die("exec '%s'", argv[0]);
228}