Sponsored Content
Top Forums Programming catching all signals in a single handler Post 58959 by xtrix on Monday 6th of December 2004 11:31:34 AM
Old 12-06-2004
catching all signals in a single handler

Hi guys - got some questions... hope something helpfull will come out!!! I just want to catch every signal that can be caught and print some info by using only one handler.

I use the sigaction() function and only one instance of a sigaction struct as an argument to the sigaction() function.

1) If sigset_t sigaction.sa_mask is empty, things seem to work fine when i send signals to my process from the command line. However the shell sends an additional SIGCONT to my process for every signal that i send. Why is this happening?

2) If sigset_t sigaction.sa_mask is filled (meaning - from my point of view Smilie - that all signals should block while sig_handler's body is being executed) i get weird results. For example the scheduler (pid=0) is repeatedly sending a SIGBUS signal to my process. Why is this happening?

Thanx in advance, please have a look at the code...

P.S. This is a simplified version of the source code. In fact the program is multi-threaded. Anyway considering just one thread of execution, would this be right?

//----------------------------------------------------------------------------------
void sig_handler(int sig, siginfo_t * info, void * context)
{
FILE *f= fopen("signal_trap", "a");

if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS || sig == SIGCHLD || sig == SIGTRAP || sig == SIGPOLL)
fprintf(f, "Signal = %d, subcode = %d, pid = %d, uid = %d, address = %p, errno = %d\n", sig, info->si_code, info->si_pid, info->si_uid, info->si_addr, info->si_errno);
else
fprintf(f, "Signal = %d, pid = %d, uid = %d, address = %p, errno = %d\n", sig, info->si_pid, info->si_uid, info->si_addr, info->si_errno);

fprintf(f, "\n");
fclose(f);
}
//----------------------------------------------------------------------------------
int main()
{
struct sigaction act;
sigemptyset(&act.sa_mask); // or sigfillset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = sig_handler;

sigaction(SIGABRT, &act, 0);
sigaction(SIGALRM, &act, 0);
sigaction(SIGFPE, &act, 0);
sigaction(SIGHUP, &act, 0);
sigaction(SIGILL, &act, 0);
sigaction(SIGINT, &act, 0);
sigaction(SIGPIPE, &act, 0);

// blah blah blah - all available signals use struct sigaction act

return 0;
}
//----------------------------------------------------------------------------------
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

catching interrupts

hey i have been facing a problem,can you tell me if we can catch ctrl d in unix i have tried and sucessfully catched and disabled ctrl-c and ctrl -z but am not sure if we can do the same for CTRL-D, so got any clue mail on he forum or ...i mean c programming in Unix thats what i am working on (1 Reply)
Discussion started by: toughguy2handle
1 Replies

2. Shell Programming and Scripting

catching some errors

I need to find a way to keep a running tally of how many times events or actions occur. Say if a user is prompted to make inputs of 1 or 2, I want it to keep track of how many times 1 was entered, and how many times 2 was entered. Thanks for your help (5 Replies)
Discussion started by: bebop1111116
5 Replies

3. UNIX for Dummies Questions & Answers

Catching print jobs.

Hi, I am wondering how to catch print jobs to process them before been served to the printer. I was told that the challenge is to catch raw text that an old legacy application sends to the printer (invoices, quotes, etc) and save them as text files to allow a new application to process them... (5 Replies)
Discussion started by: miguel77mex
5 Replies

4. Programming

Signal catching

Hi! I want to catch all signals that my program receives print their name and then execute the default handler. Can you help me on that? I've tried the following code: #include <stdio.h> #include <unistd.h> #include <signal.h> void (*hnd)(int i); char signals = { "SIGHUP",... (7 Replies)
Discussion started by: dark_knight
7 Replies

5. Shell Programming and Scripting

XML Handler in perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Meanwhile what is your opinion about XML Writer library? Thanks in Advance. (2 Replies)
Discussion started by: Zaxon
2 Replies

6. Shell Programming and Scripting

Catching errors

Hi, I'm writing a scheduling script which will co-ordinate the launching of scripts. This script is scheduling based on an input file, and launches the appropriate scripts at the right times. The only issue I'm having is: - if a script dies, or even has a syntax error, I want to catch... (1 Reply)
Discussion started by: GoldenEye4ever
1 Replies

7. UNIX for Dummies Questions & Answers

Doubt with irq handler.......

Hello, I have develop a driver for my hardware and now, I need to handle a IRQ but I does not work. As I can understand, to handle a irq, it is necessary to make a request_irq(). If the return value is zero, ok, no problem to handle irq. Here is a easy example of my driver: #include... (8 Replies)
Discussion started by: webquinty
8 Replies

8. Programming

SIGCHLD interrupts its own handler

Hi. I have a program whose job it is to manage 15 child processes. Sometimes these children die (sometimes deliberately other times with a SEGV). This causes a SIGCHLD to be sent to my program which uses waitpid() in the signal handler to gather information and, in most cases, restart the child.... (3 Replies)
Discussion started by: jrichemont
3 Replies

9. Shell Programming and Scripting

Catching error in sftp

Hi All Experts, I have a script which logs to the server via sftp connection with below code :- user_name@sftp_server.com and the connection is going smooth. My requirement is to place file in sftp_server in some path. and if path doesn't exist or the file is not put successfully I... (3 Replies)
Discussion started by: punitsoneji
3 Replies
sigsuspend(2)							   System Calls 						     sigsuspend(2)

NAME
sigsuspend - install a signal mask and suspend caller until signal SYNOPSIS
#include <signal.h> int sigsuspend(const sigset_t *set); DESCRIPTION
The sigsuspend() function replaces the caller's signal mask with the set of signals pointed to by the set argument and suspends the caller until delivery of a signal whose action is either to execute a signal catching function or to terminate the process. If the set argument points to an invalid address, the behavior is undefined and errno may be set to EFAULT. If the action is to terminate the process, sigsuspend() does not return. If the action is to execute a signal catching function, sigsus- pend() returns after the signal catching function returns. On return, the signal mask is restored to the set that existed before the call to sigsuspend(). It is not possible to block signals that cannot be ignored (see signal.h(3HEAD)). This restriction is silently imposed by the system. RETURN VALUES
Since sigsuspend() suspends the caller's execution indefinitely, there is no successful completion return value. On failure, it returns -1 and sets errno to indicate the error. ERRORS
The sigsuspend() function will fail if: EINTR A signal was caught by the caller and control was returned from the signal catching function. The sigsuspend() function may fail if: EFAULT The set argument points to an illegal address. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sigaction(2), sigprocmask(2), sigwait(2), signal(3C), signal.h(3HEAD), sigsetops(3C), attributes(5) NOTES
If the caller specifies more than one unblocked signal in the mask to sigsuspend(), more than one signal might be processed before the call to sigsuspend() returns. While the caller is executing the signal handler that interrupted its call to sigsuspend(), its signal mask is the one passed to sigsus- pend(), modified as usual by the signal mask specification in the signal's sigaction(2) parameters. The caller's signal mask is not restored to its previous value until the caller returns from all the signal handlers that interrupted sigsuspend(). SunOS 5.11 24 Jun 2001 sigsuspend(2)
All times are GMT -4. The time now is 04:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy