kill.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * --------
  24. * 2003-03-11 changed to the new locking scheme: locking.h (andrei)
  25. *
  26. *
  27. * in this file, we implement the ability to send a kill signal to
  28. * a child after some time; its a quick ugly hack, for example kill
  29. * is sent without any knowledge whether the kid is still alive
  30. *
  31. * also, it was never compiled without FAST_LOCK -- nothing will
  32. * work if you turn it off
  33. *
  34. * there is also an ugly s/HACK
  35. *
  36. * and last but not least -- we don't know the child pid (we use popen)
  37. * so we cannot close anyway
  38. *
  39. *
  40. */
  41. #include <errno.h>
  42. #include <sys/types.h>
  43. #include <signal.h>
  44. #include "../../mem/shm_mem.h"
  45. #include "../../dprint.h"
  46. #include "../../timer.h"
  47. #include "../../locking.h"
  48. #include "kill.h"
  49. static gen_lock_t *kill_lock=NULL;
  50. static struct timer_list kill_list;
  51. #define lock() lock_get(kill_lock)
  52. #define unlock() lock_release(kill_lock)
  53. /* copy and paste from TM -- might consider putting in better
  54. in some utils part of core
  55. */
  56. static void timer_routine(unsigned int ticks , void * attr)
  57. {
  58. struct timer_link *tl, *tmp_tl, *end, *ret;
  59. int killr;
  60. /* check if it worth entering the lock */
  61. if (kill_list.first_tl.next_tl==&kill_list.last_tl
  62. || kill_list.first_tl.next_tl->time_out > ticks )
  63. return;
  64. lock();
  65. end = &kill_list.last_tl;
  66. tl = kill_list.first_tl.next_tl;
  67. while( tl!=end && tl->time_out <= ticks ) {
  68. tl=tl->next_tl;
  69. }
  70. /* nothing to delete found */
  71. if (tl->prev_tl==&kill_list.first_tl) {
  72. unlock();
  73. return;
  74. }
  75. /* the detached list begins with current beginning */
  76. ret = kill_list.first_tl.next_tl;
  77. /* and we mark the end of the split list */
  78. tl->prev_tl->next_tl = 0;
  79. /* the shortened list starts from where we suspended */
  80. kill_list.first_tl.next_tl = tl;
  81. tl->prev_tl = & kill_list.first_tl;
  82. unlock();
  83. /* process the list now */
  84. while (ret) {
  85. tmp_tl=ret->next_tl;
  86. ret->next_tl=ret->prev_tl=0;
  87. if (ret->time_out>0) {
  88. killr=kill(ret->pid, SIGTERM );
  89. LM_DBG("child process (%d) kill status: %d\n", ret->pid, killr );
  90. }
  91. shm_free(ret);
  92. ret=tmp_tl;
  93. }
  94. }
  95. int schedule_to_kill( int pid )
  96. {
  97. struct timer_link *tl;
  98. tl=shm_malloc( sizeof(struct timer_link) );
  99. if (tl==0) {
  100. LM_ERR("no shmem\n");
  101. return -1;
  102. }
  103. memset(tl, 0, sizeof(struct timer_link) );
  104. lock();
  105. tl->pid=pid;
  106. tl->time_out=get_ticks()+time_to_kill;
  107. tl->prev_tl=kill_list.last_tl.prev_tl;
  108. tl->next_tl=&kill_list.last_tl;
  109. kill_list.last_tl.prev_tl=tl;
  110. tl->prev_tl->next_tl=tl;
  111. unlock();
  112. return 1;
  113. }
  114. int initialize_kill(void)
  115. {
  116. /* if disabled ... */
  117. if (time_to_kill==0) return 1;
  118. if ((register_timer( timer_routine,
  119. 0 /* param */, 1 /* period */)<0)) {
  120. LM_ERR("no exec timer registered\n");
  121. return -1;
  122. }
  123. kill_list.first_tl.next_tl=&kill_list.last_tl;
  124. kill_list.last_tl.prev_tl=&kill_list.first_tl;
  125. kill_list.first_tl.prev_tl=
  126. kill_list.last_tl.next_tl = 0;
  127. kill_list.last_tl.time_out=-1;
  128. kill_lock=lock_alloc();
  129. if (kill_lock==0) {
  130. LM_ERR("no shm mem for mutex\n");
  131. return -1;
  132. }
  133. lock_init(kill_lock);
  134. LM_DBG("kill initialized\n");
  135. return 1;
  136. }
  137. void destroy_kill(void)
  138. {
  139. /* if disabled ... */
  140. if (time_to_kill==0)
  141. return;
  142. if (kill_lock) {
  143. lock_destroy(kill_lock);
  144. lock_dealloc(kill_lock);
  145. }
  146. return;
  147. }