I have the following sigaction handler code
void signal_term_handler(int sig)
{
int rc = async_lockf(pid_file, F_UNLCK);
if(rc) {
char piderr[] = "PID file unlock failed!\n";
write(STDOUT_FILENO, piderr, (sizeof(piderr))-1);
}
close(pid_file);
char exitmsg[] = "EXIT Daemon:TERM signal Received!\n";
write(STDOUT_FILENO, exitmsg, (sizeof(exitmsg))-1);
_exit(EXIT_SUCCESS); //async-signal-save exit
}
All the function calls in the above function are an async-signal-save. Even the async_lockf()
is an async-signal-save:
/*
* The code of async_lockf is copied from eglibc-2.11.3/io/lockf.c
* The lockf.c is under the terms of the GNU Lesser General Public
* Copyright (C) 1994,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
* This file is part of the GNU C Library.
*/
int async_lockf(int fd, int cmd)
{
struct flock fl = {0};
/* async_lockf is always relative to the current file position. */
fl.l_whence = SEEK_CUR;
fl.l_start = 0;
fl.l_len = 0;
switch (cmd)
{
case F_TEST:
/* Test the async_lock: return 0 if FD is unlocked or locked by this process;
return -1, set errno to EACCES, if another process holds the lock. */
fl.l_type = F_RDLCK;
if (fcntl (fd, F_GETLK, &fl) < 0)
return -1;
if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
return 0;
errno = EACCES;
return -1;
case F_ULOCK:
fl.l_type = F_UNLCK;
cmd = F_SETLK;
break;
case F_LOCK:
fl.l_type = F_WRLCK;
cmd = F_SETLK;
break;
case F_TLOCK:
fl.l_type = F_WRLCK;
cmd = F_SETLK;
break;
default:
errno = EINVAL;
return -1;
}
/* async_lockf() is a cancellation point but so is fcntl() if F_SETLKW is
used. Therefore we don't have to care about cancellation here,
the fcntl() function will take care of it. */
return fcntl (fd, cmd, &fl);
}
the sigaction handler should close the application if I execute kill -15
command but some times I get the processus running and does not exit. this happens rarely. For example If I launch the application and then I stopped with kill -15
1000 times, this behaviour will happens only ~5 times
Any explaination for this strange behaviour? Why my application does not exist? Especially that I m using async-signal-save function (_exit()
) to close the processus
To see what's happening, try attaching
strace
orgdb
to the process and to see where it's stuck. My best guess is that you have code that's masking signals (sigprocmask
) while performing a blocking operation, thus preventing the signal handler from running.