kill.c

Go to the documentation of this file.
00001 /*
00002  * $Id: kill.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2003-03-11  changed to the new locking scheme: locking.h (andrei)
00025  *
00026  *
00027  * in this file, we implement the ability to send a kill signal to
00028  * a child after some time; its a quick ugly hack, for example kill
00029  * is sent without any knowledge whether the kid is still alive
00030  *
00031  * also, it was never compiled without FAST_LOCK -- nothing will
00032  * work if you turn it off
00033  *
00034  * there is also an ugly s/HACK
00035  *
00036  * and last but not least -- we don't know the child pid (we use popen)
00037  * so we cannot close anyway
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 /* copy and paste from TM -- might consider putting in better
00069    in some utils part of core
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    /* check if it worth entering the lock */
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    /* nothing to delete found */
00089    if (tl->prev_tl==&kill_list.first_tl) {
00090       unlock();
00091       return;
00092    }
00093    /* the detached list begins with current beginning */
00094    ret = kill_list.first_tl.next_tl;
00095    /* and we mark the end of the split list */
00096    tl->prev_tl->next_tl = 0;
00097    /* the shortened list starts from where we suspended */
00098    kill_list.first_tl.next_tl = tl;
00099    tl->prev_tl = & kill_list.first_tl;
00100    unlock();
00101 
00102    /* process the list now */
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    /* if disabled ... */
00138    if (time_to_kill==0) return 1;
00139     if ((register_timer( timer_routine,
00140             0 /* param */, 1 /* period */)<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    /* if disabled ... */
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 }

Generated on Wed May 23 08:00:57 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6