diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-24 13:25:20 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-24 13:25:20 +0000 |
commit | a9acbe6caddc59e7e25f4e469322e5c210e17775 (patch) | |
tree | 4e11174d18e586c5f7510adf31323f9dd98d7446 | |
parent | 00d8417631b1ae378d708e885dce74bafb02be96 (diff) | |
download | busybox-w32-a9acbe6caddc59e7e25f4e469322e5c210e17775.tar.gz busybox-w32-a9acbe6caddc59e7e25f4e469322e5c210e17775.tar.bz2 busybox-w32-a9acbe6caddc59e7e25f4e469322e5c210e17775.zip |
timeout: new applet. 370 bytes. by Roberto Foglietta.
-rw-r--r-- | findutils/grep.c | 4 | ||||
-rw-r--r-- | include/applets.h | 1 | ||||
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | include/usage.h | 6 | ||||
-rw-r--r-- | libbb/pw_encrypt_sha.c | 2 | ||||
-rw-r--r-- | miscutils/Config.in | 7 | ||||
-rw-r--r-- | miscutils/Kbuild | 1 | ||||
-rw-r--r-- | miscutils/timeout.c | 228 |
8 files changed, 247 insertions, 4 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 73e74f4b3..6a6ddb679 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -374,11 +374,11 @@ static int grep_file(FILE *file) | |||
374 | break; | 374 | break; |
375 | #else | 375 | #else |
376 | if (re_search(&gl->compiled_regex, line, line_len, | 376 | if (re_search(&gl->compiled_regex, line, line_len, |
377 | gl->matched_range.rm_eo, line_len - gl->matched_range.rm_eo, | 377 | gl->matched_range.rm_eo, line_len - gl->matched_range.rm_eo, |
378 | &gl->matched_range) < 0) | 378 | &gl->matched_range) < 0) |
379 | break; | 379 | break; |
380 | #endif | 380 | #endif |
381 | } | 381 | } |
382 | } else { | 382 | } else { |
383 | print_line(line, line_len, linenum, ':'); | 383 | print_line(line, line_len, linenum, ':'); |
384 | } | 384 | } |
diff --git a/include/applets.h b/include/applets.h index 286f71d1e..fb904bb9f 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -372,6 +372,7 @@ USE_TFTP(APPLET(tftp, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | |||
372 | USE_TFTPD(APPLET(tftpd, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 372 | USE_TFTPD(APPLET(tftpd, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
373 | #endif | 373 | #endif |
374 | USE_TIME(APPLET(time, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 374 | USE_TIME(APPLET(time, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
375 | USE_TIMEOUT(APPLET(timeout, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | ||
375 | USE_TOP(APPLET(top, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 376 | USE_TOP(APPLET(top, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
376 | USE_TOUCH(APPLET_NOFORK(touch, touch, _BB_DIR_BIN, _BB_SUID_NEVER, touch)) | 377 | USE_TOUCH(APPLET_NOFORK(touch, touch, _BB_DIR_BIN, _BB_SUID_NEVER, touch)) |
377 | USE_TR(APPLET(tr, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 378 | USE_TR(APPLET(tr, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
diff --git a/include/libbb.h b/include/libbb.h index 85a915ec1..77c4c6088 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -923,7 +923,7 @@ void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC; | |||
923 | * yet doesn't represent any valid Unicode characher. | 923 | * yet doesn't represent any valid Unicode characher. |
924 | * Also, -1 is reserved for error indication and we don't use it. */ | 924 | * Also, -1 is reserved for error indication and we don't use it. */ |
925 | enum { | 925 | enum { |
926 | KEYCODE_UP = -2, | 926 | KEYCODE_UP = -2, |
927 | KEYCODE_DOWN = -3, | 927 | KEYCODE_DOWN = -3, |
928 | KEYCODE_RIGHT = -4, | 928 | KEYCODE_RIGHT = -4, |
929 | KEYCODE_LEFT = -5, | 929 | KEYCODE_LEFT = -5, |
diff --git a/include/usage.h b/include/usage.h index 49c94000b..3c657a15e 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -4321,6 +4321,12 @@ | |||
4321 | "\nOptions:" \ | 4321 | "\nOptions:" \ |
4322 | "\n -v Verbose" \ | 4322 | "\n -v Verbose" \ |
4323 | 4323 | ||
4324 | #define timeout_trivial_usage \ | ||
4325 | "[-t SECS] [-s SIG] PROG [ARGS]" | ||
4326 | #define timeout_full_usage "\n\n" \ | ||
4327 | "Runs PROG. Sends SIG to it if it is not gone in SECS seconds.\n" \ | ||
4328 | "Defaults: SECS: 10, SIG: TERM.\n" \ | ||
4329 | |||
4324 | #define top_trivial_usage \ | 4330 | #define top_trivial_usage \ |
4325 | "[-b] [-nCOUNT] [-dSECONDS]" | 4331 | "[-b] [-nCOUNT] [-dSECONDS]" |
4326 | #define top_full_usage "\n\n" \ | 4332 | #define top_full_usage "\n\n" \ |
diff --git a/libbb/pw_encrypt_sha.c b/libbb/pw_encrypt_sha.c index 3173506f5..e71f96fdc 100644 --- a/libbb/pw_encrypt_sha.c +++ b/libbb/pw_encrypt_sha.c | |||
@@ -210,7 +210,7 @@ do { \ | |||
210 | b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4); | 210 | b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4); |
211 | if (k == 29) | 211 | if (k == 29) |
212 | break; | 212 | break; |
213 | i = k + 1; | 213 | i = k + 1; |
214 | } | 214 | } |
215 | b64_from_24bit(0, alt_result[31], alt_result[30], 3); | 215 | b64_from_24bit(0, alt_result[31], alt_result[30], 3); |
216 | /* was: | 216 | /* was: |
diff --git a/miscutils/Config.in b/miscutils/Config.in index fc7333bb6..68732e682 100644 --- a/miscutils/Config.in +++ b/miscutils/Config.in | |||
@@ -529,6 +529,13 @@ config TIME | |||
529 | When the command finishes, time writes a message to standard output | 529 | When the command finishes, time writes a message to standard output |
530 | giving timing statistics about this program run. | 530 | giving timing statistics about this program run. |
531 | 531 | ||
532 | config TIMEOUT | ||
533 | bool "timeout" | ||
534 | default n | ||
535 | help | ||
536 | Runs a program and watches it. If it does not terminate in | ||
537 | specified number of seconds, it is sent a signal. | ||
538 | |||
532 | config TTYSIZE | 539 | config TTYSIZE |
533 | bool "ttysize" | 540 | bool "ttysize" |
534 | default n | 541 | default n |
diff --git a/miscutils/Kbuild b/miscutils/Kbuild index 13791ef18..e6e434755 100644 --- a/miscutils/Kbuild +++ b/miscutils/Kbuild | |||
@@ -34,5 +34,6 @@ lib-$(CONFIG_SETSID) += setsid.o | |||
34 | lib-$(CONFIG_STRINGS) += strings.o | 34 | lib-$(CONFIG_STRINGS) += strings.o |
35 | lib-$(CONFIG_TASKSET) += taskset.o | 35 | lib-$(CONFIG_TASKSET) += taskset.o |
36 | lib-$(CONFIG_TIME) += time.o | 36 | lib-$(CONFIG_TIME) += time.o |
37 | lib-$(CONFIG_TIMEOUT) += timeout.o | ||
37 | lib-$(CONFIG_TTYSIZE) += ttysize.o | 38 | lib-$(CONFIG_TTYSIZE) += ttysize.o |
38 | lib-$(CONFIG_WATCHDOG) += watchdog.o | 39 | lib-$(CONFIG_WATCHDOG) += watchdog.o |
diff --git a/miscutils/timeout.c b/miscutils/timeout.c new file mode 100644 index 000000000..6a0a9a6e6 --- /dev/null +++ b/miscutils/timeout.c | |||
@@ -0,0 +1,228 @@ | |||
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
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 | |||
33 | int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
34 | int 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 | |||
147 | int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
148 | int 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 | } | ||