LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-18-2007, 07:23 AM   #1
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Red face 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...

Last edited by vdx; 09-21-2007 at 05:36 AM.
 
Old 09-18-2007, 02:34 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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.
 
Old 09-18-2007, 05:12 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 69
Also, the second argument to signal should be sighandler, not signum.
 
Old 09-21-2007, 05:39 AM   #4
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
Red face

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 ??
 
Old 09-21-2007, 07:15 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,995

Rep: Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737
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).
 
Old 09-22-2007, 03:14 AM   #6
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
thnx tronayne

ur code is very much helpfull to me..

thnx once again...
 
Old 09-22-2007, 03:39 AM   #7
jdiggitydogg
Member
 
Registered: Sep 2007
Posts: 42

Rep: Reputation: 15
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.
 
Old 09-24-2007, 12:43 AM   #8
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
thnx jdiggitydogg,,,,,,,,it seems also a usefull trick
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SIGSEGV handler (segmentation fauld handler) myp Programming 8 03-08-2011 02:17 PM
bactrace- signal handler- gcc optimization virtualCoder Programming 6 09-18-2007 12:23 AM
howto know whether a function is called in signal handler snowing Programming 1 11-07-2005 09:55 PM
<0>Kernel panic: Aiee, killing interrupt handler! In interrupt handler - not syncing mrb Linux - Newbie 2 01-09-2005 09:47 AM
Konqueror / KDE Crash Handler: Signal 11 Bob P Linux - Software 4 10-13-2004 04:21 PM


All times are GMT -5. The time now is 06:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration