LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   signal handler routine (https://www.linuxquestions.org/questions/programming-9/signal-handler-routine-585526/)

vdx 09-18-2007 07:23 AM

signal handler routine
 
frnds,

I want to catch all the signals which I get.

for example,
Code:


void sighandler(int signum)
{
  printf("Caught signal = %d\n",signum);
}

int main(int argc, char **argv)
{
  ......
  ......

  signal(SIG_ALL,sighandler);

  .......
  .......
}

is there any constant like SIG_ALL, so wen my program catches any signal it can call the "sighandler" routine.

or may be I am wrong, then is there any special method to implement this ?

thnx in advance...

jim mcnamara 09-18-2007 02:34 PM

You cannot block all signals, SIGKILL for example.

You have to invoke signal once for each separate signal.

Next, printf() may cause problems in your signal handler. Most of the standard library functions have problems because they can be interrupted by yet another signal. Try using write() instead.

osor 09-18-2007 05:12 PM

Also, the second argument to signal should be sighandler, not signum.

vdx 09-21-2007 05:39 AM

thnx jim mcnamara

thnx for ur replay

but the problem is still there..

if i want to catch all the signals except(non catchable signals),
how can I achive this ??

is there any trick ??

tronayne 09-21-2007 07:15 AM

Here's a demonstration program that catches an interrupt signal; e.g., when a user tries to abort a program with CTRL-C or the delete key. By use setsig, sigsetjmp and siglongjmp the program cannot be killed by flooding it with interrupt signals (this is the sort of thing that you'd use to clean up files when an interrupt is received):
Code:

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <signal.h>

sigjmp_buf      env;

static  void    catch_int      (int);

void    main    (void)
{
        char    line [BUFSIZ];

        (void) sigset (SIGINT, catch_int);
        (void) sigsetjmp (env, 1);
        (void) fprintf (stdout, "READY\n");
        (void) gets (line);
        exit (EXIT_SUCCESS);
}

static  void    catch_int      (int signo)
{
        siglongjmp (env, signo);
        return;
}

This isn't a bad template to use for building up what you're talking about; it appears in Chapter 6 of Kochan and Wood Topics in C Programming Revised Edition (ISBN 0-471-53404-8).

vdx 09-22-2007 03:14 AM

thnx tronayne

ur code is very much helpfull to me..

thnx once again...

jdiggitydogg 09-22-2007 03:39 AM

a quick scan of the header files (/usr/include/asm/signal.h & /usr/include/bits/signum.h) does not reveal a single macro that specifies all catch-able signals.

however, i suggest you write a loop that sets them all in just a couple lines of code. here's some psuedo code:

for ( i = 0; i < 32; i++ )
{
signal( i, sighandler );
}

don't know what the system will do if you try to set a signal handler for a signal that cannot be caught.

vdx 09-24-2007 12:43 AM

thnx jdiggitydogg,,,,,,,,it seems also a usefull trick


All times are GMT -5. The time now is 10:37 AM.