diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-24 22:34:47 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-24 22:34:47 +0000 |
| commit | 8a2f6bfc2fd378e3504b0938582a314c52c9f2af (patch) | |
| tree | 7ea0ff8386bf8a9bde139044845ee82bec3d6748 /miscutils | |
| parent | 4240408b700d303f5f9a78e1ef41325a9547cd2e (diff) | |
| download | busybox-w32-8a2f6bfc2fd378e3504b0938582a314c52c9f2af.tar.gz busybox-w32-8a2f6bfc2fd378e3504b0938582a314c52c9f2af.tar.bz2 busybox-w32-8a2f6bfc2fd378e3504b0938582a314c52c9f2af.zip | |
timeout: fix a few bugs. vda is stupid
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/timeout.c | 117 |
1 files changed, 2 insertions, 115 deletions
diff --git a/miscutils/timeout.c b/miscutils/timeout.c index 6a0a9a6e6..65a8ceadf 100644 --- a/miscutils/timeout.c +++ b/miscutils/timeout.c | |||
| @@ -47,7 +47,8 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
| 47 | 47 | ||
| 48 | /* -t SECONDS; -p PARENT_PID */ | 48 | /* -t SECONDS; -p PARENT_PID */ |
| 49 | opt_complementary = "t+" USE_FOR_NOMMU(":p+"); | 49 | opt_complementary = "t+" USE_FOR_NOMMU(":p+"); |
| 50 | getopt32(argv, "s:t" USE_FOR_NOMMU(":p:"), &opt_s, &timeout, &parent); | 50 | /* '+': stop at first non-option */ |
| 51 | getopt32(argv, "+s:t" USE_FOR_NOMMU(":p:"), &opt_s, &timeout, &parent); | ||
| 51 | /*argv += optind; - no, wait for bb_daemonize_or_rexec! */ | 52 | /*argv += optind; - no, wait for bb_daemonize_or_rexec! */ |
| 52 | signo = get_signum(opt_s); | 53 | signo = get_signum(opt_s); |
| 53 | if (signo < 0) | 54 | if (signo < 0) |
| @@ -66,127 +67,13 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
| 66 | if (!argv[optind]) /* no PROG? */ | 67 | if (!argv[optind]) /* no PROG? */ |
| 67 | bb_show_usage(); | 68 | bb_show_usage(); |
| 68 | 69 | ||
| 69 | pid = vfork(); | ||
| 70 | if (pid < 0) | ||
| 71 | bb_perror_msg_and_die("vfork"); | ||
| 72 | #if !BB_MMU | 70 | #if !BB_MMU |
| 73 | sv1 = argv[optind]; | 71 | sv1 = argv[optind]; |
| 74 | sv2 = argv[optind + 1]; | 72 | sv2 = argv[optind + 1]; |
| 75 | #endif | 73 | #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(); | 74 | pid = vfork(); |
| 184 | if (pid < 0) | 75 | if (pid < 0) |
| 185 | bb_perror_msg_and_die("vfork"); | 76 | 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) { | 77 | if (pid == 0) { |
| 191 | /* Child: spawn grandchild and exit */ | 78 | /* Child: spawn grandchild and exit */ |
| 192 | parent = getppid(); | 79 | parent = getppid(); |
