timeout.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. //config:config TIMEOUT
  31. //config: bool "timeout (6.5 kb)"
  32. //config: default y
  33. //config: help
  34. //config: Runs a program and watches it. If it does not terminate in
  35. //config: specified number of seconds, it is sent a signal.
  36. //applet:IF_TIMEOUT(APPLET(timeout, BB_DIR_USR_BIN, BB_SUID_DROP))
  37. //kbuild:lib-$(CONFIG_TIMEOUT) += timeout.o
  38. //usage:#define timeout_trivial_usage
  39. //usage: "[-s SIG] [-k KILL_SECS] SECS PROG ARGS"
  40. //usage:#define timeout_full_usage "\n\n"
  41. //usage: "Run PROG. Send SIG to it if it is not gone in SECS seconds.\n"
  42. //usage: "Default SIG: TERM."
  43. //usage: "If it still exists in KILL_SECS seconds, send KILL.\n"
  44. #include "libbb.h"
  45. static NOINLINE int timeout_wait(duration_t timeout, pid_t pid)
  46. {
  47. /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
  48. while (1) {
  49. #if ENABLE_FLOAT_DURATION
  50. if (timeout < 1)
  51. sleep_for_duration(timeout);
  52. else
  53. #endif
  54. sleep1();
  55. if (--timeout <= 0)
  56. break;
  57. if (kill(pid, 0)) {
  58. /* process is gone */
  59. return EXIT_SUCCESS;
  60. }
  61. }
  62. return EXIT_FAILURE;
  63. }
  64. int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  65. int timeout_main(int argc UNUSED_PARAM, char **argv)
  66. {
  67. int signo;
  68. int status;
  69. int parent = 0;
  70. duration_t timeout;
  71. duration_t kill_timeout;
  72. pid_t pid;
  73. #if !BB_MMU
  74. char *sv1, *sv2;
  75. #endif
  76. const char *opt_s = "TERM";
  77. char *opt_k = NULL;
  78. /* -p option is not documented, it is needed to support NOMMU. */
  79. /* -t SECONDS; -p PARENT_PID */
  80. /* '+': stop at first non-option */
  81. getopt32(argv, "+s:k:" USE_FOR_NOMMU("p:+"), &opt_s, &opt_k, &parent);
  82. /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
  83. signo = get_signum(opt_s);
  84. if (signo < 0)
  85. bb_error_msg_and_die("unknown signal '%s'", opt_s);
  86. kill_timeout = 0;
  87. if (opt_k)
  88. kill_timeout = parse_duration_str(opt_k);
  89. if (!argv[optind])
  90. bb_show_usage();
  91. timeout = parse_duration_str(argv[optind++]);
  92. if (!argv[optind]) /* no PROG? */
  93. bb_show_usage();
  94. /* We want to create a grandchild which will watch
  95. * and kill the grandparent. Other methods:
  96. * making parent watch child disrupts parent<->child link
  97. * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
  98. * it's better if service_prog is a child of tcpsvd!),
  99. * making child watch parent results in programs having
  100. * unexpected children. */
  101. if (parent) /* we were re-execed, already grandchild */
  102. goto grandchild;
  103. #if !BB_MMU
  104. sv1 = argv[optind];
  105. sv2 = argv[optind + 1];
  106. #endif
  107. pid = xvfork();
  108. if (pid == 0) {
  109. /* Child: spawn grandchild and exit */
  110. parent = getppid();
  111. #if !BB_MMU
  112. argv[optind] = xasprintf("-p%u", parent);
  113. argv[optind + 1] = NULL;
  114. #endif
  115. /* NB: exits with nonzero on error: */
  116. bb_daemonize_or_rexec(0, argv);
  117. /* Here we are grandchild. Sleep, then kill grandparent */
  118. grandchild:
  119. if (timeout_wait(timeout, parent) == EXIT_SUCCESS)
  120. return EXIT_SUCCESS;
  121. kill(parent, signo);
  122. if (kill_timeout > 0) {
  123. if (timeout_wait(kill_timeout, parent) == EXIT_SUCCESS)
  124. return EXIT_SUCCESS;
  125. kill(parent, SIGKILL);
  126. }
  127. return EXIT_SUCCESS;
  128. }
  129. /* Parent */
  130. wait(&status); /* wait for child to die */
  131. /* Did intermediate [v]fork or exec fail? */
  132. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  133. return EXIT_FAILURE;
  134. /* Ok, exec a program as requested */
  135. argv += optind;
  136. #if !BB_MMU
  137. argv[0] = sv1;
  138. argv[1] = sv2;
  139. #endif
  140. BB_EXECVP_or_die(argv);
  141. }