Handle a signal

This is a discussion on Handle a signal within the C Programming forums, part of the General Programming Boards category; Hi, in my little programme i have to handle the signal SIGTERM, and following basic steps this is what i ...

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Handle a signal

    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?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Read this - http://linuxreviews.org/man/signal/
    Scroll down to "Async-signal-safe functions"
    Rewrite your signal handler.

    > Unfortunately what i get wen i run the script is the message "SIGTERM not handled"!
    You're meant to "get out of Dodge" with all due dispatch, not dawdle around doing all the things you would normally do at a normal program exit.

    If you take too long, it (the shell) might give up waiting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by flexo87 View Post
    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?

    Well your script is logically inconistant, you tell kill to wait 'if it is not working' however then
    you say to print an error mesage if it did not work.

    Logically that script cannot produce that error message.

    So it didn't, you are deluding yourself, it never happened. (stay off the drugs)
    Last edited by esbo; 01-29-2009 at 05:36 PM.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    What happens if you use a logical script?
    Such as:-
    Code:
    if  ! killall -TERM dserver; then
        echo Error: SIGTERM not handled
        exit 1
    fi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21