Hi, in my little programme i have to handle the signal SIGTERM, and following basic steps this is what i do:
Code:
int main(int argc, char* argv[]) {
if (argc<2) return -1;
struct sigaction ac;
serverChannel_t srv;
channel_t clnt;
(void)unlink(SOCKPATH);
snprintf(dirpath, FILENAME_MAX+1, argv[1]);
if (!(planners=read_planners(dirpath))) {
perror("Erorr during planners initialization\n");
exit(EXIT_FAILURE);
}
if ((srv=createServerChannel(SOCKPATH))<=0) {
perror("Erorr during socket initialization");
exit(EXIT_FAILURE);
}
memset(&ac, 0, sizeof(ac));
ac.sa_handler=sig_handler; //handler of the function
sigaction(SIGTERM, &ac, NULL);
.............................
.............................
}
/* the handler */
static void sig_handler(int n) {
int i=0;printf("Handler is executing!\n"); fflush(stdout);
plan_t * next;
while (many_tids>0)printf("<%d>;", many_tids);
if (planners) {
write_planners(dirpath, planners));
while (i++<plan_elems) {
next=planners->nxt;
dealloca_lista(planners->events);
free(planners);
planners=next;
}
}
(void)unlink(SOCKPATH);
exit(EXIT_SUCCESS);
}
The handler works fine, i mean when the signal is sent to the process the handlers is executed.
The problem is that i have a script bash which does several tests on this program, and one of these is:
Code:
if ! killall -w -TERM dserver; then
echo Error: SIGTERM not handled
exit 1
fi
The killall command return 0 if the process terminates, otherwise it will return a number different from 0, so that script line means, if killall fails print on the screen that message.
the '-w' option specifies that killall has to wait, cause the process might invoke some cleanup function (is my case), so if the process decides to ignore the SIGTERM, killall -w will never return.
Unfortunately what i get wen i run the script is the message "SIGTERM not handled"!
Any idea please?