blob: 83ae56e69edb333d3d951a01d83de0cc56e017a8 [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+");
Denis Vlasenko8a2f6bf2008-11-24 22:34:47 +000050 /* '+': stop at first non-option */
Denis Vlasenko3fd10462009-03-22 14:49:00 +000051 getopt32(argv, "+s:t:" USE_FOR_NOMMU("p:"), &opt_s, &timeout, &parent);
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000052 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
53 signo = get_signum(opt_s);
54 if (signo < 0)
55 bb_error_msg_and_die("unknown signal '%s'", opt_s);
56
57 /* We want to create a grandchild which will watch
58 * and kill the grandparent. Other methods:
59 * making parent watch child disrupts parent<->child link
60 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
61 * it's better if service_prog is a child of tcpsvd!),
62 * making child watch parent results in programs having
63 * unexpected children. */
64
65 if (parent) /* we were re-execed, already grandchild */
66 goto grandchild;
67 if (!argv[optind]) /* no PROG? */
68 bb_show_usage();
69
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000070#if !BB_MMU
71 sv1 = argv[optind];
72 sv2 = argv[optind + 1];
73#endif
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000074 pid = vfork();
75 if (pid < 0)
76 bb_perror_msg_and_die("vfork");
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000077 if (pid == 0) {
78 /* Child: spawn grandchild and exit */
79 parent = getppid();
80#if !BB_MMU
81 argv[optind] = xasprintf("-p%u", parent);
82 argv[optind + 1] = NULL;
83#endif
84 /* NB: exits with nonzero on error: */
85 bb_daemonize_or_rexec(0, argv);
86 /* Here we are grandchild. Sleep, then kill grandparent */
87 grandchild:
88 /* Just sleep(NUGE_NUM); kill(parent) may kill wrong process! */
89 while (1) {
90 sleep(1);
91 if (--timeout <= 0)
92 break;
93 if (kill(parent, 0)) {
94 /* process is gone */
95 return EXIT_SUCCESS;
96 }
97 }
98 kill(parent, signo);
99 return EXIT_SUCCESS;
100 }
101
102 /* Parent */
103 wait(&status); /* wait for child to die */
104 /* Did intermediate [v]fork or exec fail? */
105 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
106 return EXIT_FAILURE;
107 /* Ok, exec a program as requested */
108 argv += optind;
109#if !BB_MMU
110 argv[0] = sv1;
111 argv[1] = sv2;
112#endif
113 BB_EXECVP(argv[0], argv);
114 bb_perror_msg_and_die("exec '%s'", argv[0]);
115}