timeout_watchdog.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file contrib/timeout_watchdog.c
  19. * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period
  20. * @author Matthias Wachs
  21. */
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. static pid_t child;
  29. static void
  30. sigchld_handler (int val)
  31. {
  32. int status = 0;
  33. int ret = 0;
  34. waitpid (child, &status, 0);
  35. if (WIFEXITED (status) != 0)
  36. {
  37. ret = WEXITSTATUS (status);
  38. printf ("Test process exited with result %u\n", ret);
  39. }
  40. if (WIFSIGNALED (status) != 0)
  41. {
  42. ret = WTERMSIG (status);
  43. printf ("Test process was signaled %u\n", ret);
  44. }
  45. exit (ret);
  46. }
  47. static void
  48. sigint_handler (int val)
  49. {
  50. kill (0, val);
  51. exit (val);
  52. }
  53. int
  54. main (int argc, char *argv[])
  55. {
  56. int timeout = 0;
  57. pid_t gpid = 0;
  58. if (argc < 3)
  59. {
  60. printf
  61. ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
  62. exit (1);
  63. }
  64. timeout = atoi (argv[1]);
  65. if (timeout == 0)
  66. timeout = 600;
  67. /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
  68. gpid = getpgrp ();
  69. signal (SIGCHLD, sigchld_handler);
  70. signal (SIGABRT, sigint_handler);
  71. signal (SIGFPE, sigint_handler);
  72. signal (SIGILL, sigint_handler);
  73. signal (SIGINT, sigint_handler);
  74. signal (SIGSEGV, sigint_handler);
  75. signal (SIGTERM, sigint_handler);
  76. child = fork ();
  77. if (child == 0)
  78. {
  79. /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
  80. //setpgrp (0, pid_t gpid);
  81. if (-1 != gpid)
  82. setpgid (0, gpid);
  83. execvp (argv[2], &argv[2]);
  84. exit (1);
  85. }
  86. if (child > 0)
  87. {
  88. sleep (timeout);
  89. printf ("Child processes were killed after timeout of %u seconds\n",
  90. timeout);
  91. kill (0, SIGTERM);
  92. exit (1);
  93. }
  94. exit (1);
  95. }
  96. /* end of timeout_watchdog.c */