kill.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <errno.h>
00044 #include <sys/types.h>
00045 #include <signal.h>
00046
00047 #include "../../mem/shm_mem.h"
00048 #include "../../dprint.h"
00049 #include "../../timer.h"
00050 #include "../../locking.h"
00051
00052 #include "kill.h"
00053
00054
00055 static gen_lock_t *kill_lock=NULL;
00056
00057
00058 static struct timer_list kill_list;
00059
00060
00061
00062 #define lock() lock_get(kill_lock)
00063
00064 #define unlock() lock_release(kill_lock)
00065
00066
00067
00068
00069
00070
00071 static void timer_routine(unsigned int ticks , void * attr)
00072 {
00073 struct timer_link *tl, *tmp_tl, *end, *ret;
00074 int killr;
00075
00076
00077 if (kill_list.first_tl.next_tl==&kill_list.last_tl
00078 || kill_list.first_tl.next_tl->time_out > ticks )
00079 return;
00080
00081 lock();
00082 end = &kill_list.last_tl;
00083 tl = kill_list.first_tl.next_tl;
00084 while( tl!=end && tl->time_out <= ticks ) {
00085 tl=tl->next_tl;
00086 }
00087
00088
00089 if (tl->prev_tl==&kill_list.first_tl) {
00090 unlock();
00091 return;
00092 }
00093
00094 ret = kill_list.first_tl.next_tl;
00095
00096 tl->prev_tl->next_tl = 0;
00097
00098 kill_list.first_tl.next_tl = tl;
00099 tl->prev_tl = & kill_list.first_tl;
00100 unlock();
00101
00102
00103 while (ret) {
00104 tmp_tl=ret->next_tl;
00105 ret->next_tl=ret->prev_tl=0;
00106 if (ret->time_out>0) {
00107 killr=kill(ret->pid, SIGTERM );
00108 LM_DBG("child process (%d) kill status: %d\n", ret->pid, killr );
00109 }
00110 shm_free(ret);
00111 ret=tmp_tl;
00112 }
00113 }
00114
00115 int schedule_to_kill( int pid )
00116 {
00117 struct timer_link *tl;
00118 tl=shm_malloc( sizeof(struct timer_link) );
00119 if (tl==0) {
00120 LM_ERR("no shmem\n");
00121 return -1;
00122 }
00123 memset(tl, 0, sizeof(struct timer_link) );
00124 lock();
00125 tl->pid=pid;
00126 tl->time_out=get_ticks()+time_to_kill;
00127 tl->prev_tl=kill_list.last_tl.prev_tl;
00128 tl->next_tl=&kill_list.last_tl;
00129 kill_list.last_tl.prev_tl=tl;
00130 tl->prev_tl->next_tl=tl;
00131 unlock();
00132 return 1;
00133 }
00134
00135 int initialize_kill(void)
00136 {
00137
00138 if (time_to_kill==0) return 1;
00139 if ((register_timer( timer_routine,
00140 0 , 1 )<0)) {
00141 LM_ERR("no exec timer registered\n");
00142 return -1;
00143 }
00144 kill_list.first_tl.next_tl=&kill_list.last_tl;
00145 kill_list.last_tl.prev_tl=&kill_list.first_tl;
00146 kill_list.first_tl.prev_tl=
00147 kill_list.last_tl.next_tl = 0;
00148 kill_list.last_tl.time_out=-1;
00149 kill_lock=lock_alloc();
00150 if (kill_lock==0) {
00151 LM_ERR("no shm mem for mutex\n");
00152 return -1;
00153 }
00154 lock_init(kill_lock);
00155 LM_DBG("kill initialized\n");
00156 return 1;
00157 }
00158
00159 void destroy_kill(void)
00160 {
00161
00162 if (time_to_kill==0)
00163 return;
00164 if (kill_lock) {
00165 lock_destroy(kill_lock);
00166 lock_dealloc(kill_lock);
00167 }
00168 return;
00169 }