Operating System - Linux
1753831 Members
9041 Online
108806 Solutions
New Discussion юеВ

i cannot trap the SIGTERM when someone kills my process

 
SOLVED
Go to solution
Pedro Dinis
Advisor

i cannot trap the SIGTERM when someone kills my process


i am using

kill 9897
kill -15 7897

and its not catching the SIGTERM signal



this is the function daemonize :

static void daemonize( const char *lockfile )
{
pid_t pid, sid, parent;
int lfp = -1;
char * sstring="";

log_message(LOG_FILE,"iniciar al demonio");

/* already a daemon */
if ( getppid() == 1 ) return;

/* Create the lock file as the current user */
if ( lockfile && lockfile[0] ) {
lfp = open(lockfile,O_RDWR|O_CREAT,0640);
if ( lfp < 0 ) {
sscanf( sstring, "incapaz crear el archivo de la cerradura %s, code=%d (%s)", lockfile, errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
}
/* Drop user if there is one, and we were run as root */
if ( getuid() == 0 || geteuid() == 0 ) {
struct passwd *pw = getpwnam(RUN_AS_USER);
if ( pw ) {
log_message(LOG_FILE, "fijar a usuario a " RUN_AS_USER);

setuid( pw->pw_uid );
}
}
/* Trap signals that we expect to recieve */
signal(SIGCHLD,child_handler);
signal(SIGUSR1,child_handler);
signal(SIGALRM,child_handler);
signal(SIGHUP, child_handler);
signal(SIGTERM,child_handler);

/* Fork off the parent process */
pid = fork();
if (pid < 0) {
sscanf( sstring, "incapaz bifurcar demonio, c├│digo =%d (%s)", errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}

/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
/* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
for two seconds to elapse (SIGALRM). pause() should not return. */
alarm(2);
pause();
exit(EXIT_FAILURE);
}

/* At this point we are executing as the child process */
parent = getppid();

/* Cancel certain signals */
signal(SIGCHLD,SIG_DFL); /* A child process dies */
signal(SIGTSTP,SIG_IGN); /* Various TTY signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP, child_handler);
signal(SIGTERM,child_handler);
// signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
//signal(SIGTERM,SIG_DFL); /* Die on SIGTERM */

/* Change the file mode mask */
umask(0);

/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
sscanf( sstring, "incapaz crear una nueva sesi├│n, c├│digo %d (%s)", errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
/*Change the current working directory. This prevents the current directory from being locked; hence not being able to remove it.*/
if ((chdir("/")) < 0) {
sscanf( sstring, "incapaz cambiar el directorio a %s, code %d (%s)","/", errno, strerror(errno) );
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}

/* Redirect standard files to /dev/null */
freopen( "/dev/null", "r", stdin);
freopen( "/dev/null", "w", stdout);
freopen( "/dev/null", "w", stderr);

/* Tell the parent process that we are A-okay */
kill( parent, SIGUSR1 );

log_message(LOG_FILE,"el salir daemonize la funci├│n");

}


this is the signal handler :

static void child_handler(int signum)
{
switch(signum)
{
case SIGALRM: exit(EXIT_FAILURE); break;
case SIGUSR1: exit(EXIT_SUCCESS); break;
case SIGCHLD: exit(EXIT_FAILURE); break;
case SIGHUP:
log_message(LOG_FILE,"a Terminar el demonio::::::::SIGHUP");
exit(EXIT_SUCCESS);
break;
case SIGTERM:
log_message(LOG_FILE,"a Terminar el demonio::::::::SIGTERM");
exit(0);
break;
}
}



this is the main ()

main()
{
daemonize();
while(1) sleep(1); /* run */
}


SLB SLB SLB Glorioso
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: i cannot trap the SIGTERM when someone kills my process

Can you use "tusc -fp" on the demon to see what is happening?
Pedro Dinis
Advisor

Re: i cannot trap the SIGTERM when someone kills my process

hi

thanks for replying

it is working now, i had a problem with the log function, now it is logging when the daemon gets a SIGTERM.
SLB SLB SLB Glorioso