3
0

timeout.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "libbb.h"
  31. int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int timeout_main(int argc UNUSED_PARAM, char **argv)
  33. {
  34. int signo;
  35. int status;
  36. int parent = 0;
  37. int timeout = 10;
  38. pid_t pid;
  39. #if !BB_MMU
  40. char *sv1, *sv2;
  41. #endif
  42. const char *opt_s = "TERM";
  43. /* -p option is not documented, it is needed to support NOMMU. */
  44. /* -t SECONDS; -p PARENT_PID */
  45. opt_complementary = "t+" USE_FOR_NOMMU(":p+");
  46. /* '+': stop at first non-option */
  47. getopt32(argv, "+s:t:" USE_FOR_NOMMU("p:"), &opt_s, &timeout, &parent);
  48. /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
  49. signo = get_signum(opt_s);
  50. if (signo < 0)
  51. bb_error_msg_and_die("unknown signal '%s'", opt_s);
  52. /* We want to create a grandchild which will watch
  53. * and kill the grandparent. Other methods:
  54. * making parent watch child disrupts parent<->child link
  55. * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
  56. * it's better if service_prog is a child of tcpsvd!),
  57. * making child watch parent results in programs having
  58. * unexpected children. */
  59. if (parent) /* we were re-execed, already grandchild */
  60. goto grandchild;
  61. if (!argv[optind]) /* no PROG? */
  62. bb_show_usage();
  63. #if !BB_MMU
  64. sv1 = argv[optind];
  65. sv2 = argv[optind + 1];
  66. #endif
  67. pid = xvfork();
  68. if (pid == 0) {
  69. /* Child: spawn grandchild and exit */
  70. parent = getppid();
  71. #if !BB_MMU
  72. argv[optind] = xasprintf("-p%u", parent);
  73. argv[optind + 1] = NULL;
  74. #endif
  75. /* NB: exits with nonzero on error: */
  76. bb_daemonize_or_rexec(0, argv);
  77. /* Here we are grandchild. Sleep, then kill grandparent */
  78. grandchild:
  79. /* Just sleep(NUGE_NUM); kill(parent) may kill wrong process! */
  80. while (1) {
  81. sleep(1);
  82. if (--timeout <= 0)
  83. break;
  84. if (kill(parent, 0)) {
  85. /* process is gone */
  86. return EXIT_SUCCESS;
  87. }
  88. }
  89. kill(parent, signo);
  90. return EXIT_SUCCESS;
  91. }
  92. /* Parent */
  93. wait(&status); /* wait for child to die */
  94. /* Did intermediate [v]fork or exec fail? */
  95. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  96. return EXIT_FAILURE;
  97. /* Ok, exec a program as requested */
  98. argv += optind;
  99. #if !BB_MMU
  100. argv[0] = sv1;
  101. argv[1] = sv2;
  102. #endif
  103. BB_EXECVP_or_die(argv);
  104. }