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
sigaction(2)							   System Calls 						      sigaction(2)

NAME
sigaction - detailed signal management SYNOPSIS
#include <signal.h> int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact); DESCRIPTION
The sigaction() function allows the calling process to examine or specify the action to be taken on delivery of a specific signal. See sig- nal.h(3HEAD) for an explanation of general signal concepts. The sig argument specifies the signal and can be assigned any of the signals specified in signal.h(3HEAD) except SIGKILL and SIGSTOP. If the argument act is not NULL, it points to a structure specifying the new action to be taken when delivering sig. If the argument oact is not NULL, it points to a structure where the action previously associated with sig is to be stored on return from sigaction(). The sigaction structure includes the following members: void (*sa_handler)(); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; The storage occupied by sa_handler and sa_sigaction may overlap, and a standard-conforming application (see standards(5)) must not use both simultaneously. The sa_handler member identifies the action to be associated with the specified signal, if the SA_SIGINFO flag (see below) is cleared in the sa_flags field of the sigaction structure. It may take any of the values specified in signal.h(3HEAD) or that of a user specified sig- nal handler. If the SA_SIGINFO flag is set in the sa_flags field, the sa_sigaction field specifies a signal-catching function. The sa_mask member specifies a set of signals to be blocked while the signal handler is active. On entry to the signal handler, that set of signals is added to the set of signals already being blocked when the signal is delivered. In addition, the signal that caused the handler to be executed will also be blocked, unless the SA_NODEFER flag has been specified. SIGSTOP and SIGKILL cannot be blocked (the system silently enforces this restriction). The sa_flags member specifies a set of flags used to modify the delivery of the signal. It is formed by a logical OR of any of the follow- ing values: SA_ONSTACK If set and the signal is caught, and if the thread that is chosen to processes a delivered signal has an alternate signal stack declared with sigaltstack(2), then it will process the signal on that stack. Otherwise, the signal is delivered on the thread's normal stack. SA_RESETHAND If set and the signal is caught, the disposition of the signal is reset to SIG_DFL and the signal will not be blocked on entry to the signal handler (SIGILL, SIGTRAP, and SIGPWR cannot be automatically reset when delivered; the system silently enforces this restriction). SA_NODEFER If set and the signal is caught, the signal will not be automatically blocked by the kernel while it is being caught. SA_RESTART If set and the signal is caught, functions that are interrupted by the execution of this signal's handler are transparently restarted by the system, namely fcntl(2), ioctl(2), wait(3C), waitid(2), and the following functions on slow devices like terminals: getmsg() and getpmsg() (see getmsg(2)); putmsg() and putpmsg() (see putmsg(2)); pread(), read(), and readv() (see read(2)); pwrite(), write(), and writev() (see write(2)); recv(), recvfrom(), and recvmsg() (see recv(3SOCKET)); and send(), sendto(), and sendmsg() (see send(3SOCKET)). Otherwise, the function returns an EINTR error. SA_SIGINFO If cleared and the signal is caught, sig is passed as the only argument to the signal-catching function. If set and the signal is caught, two additional arguments are passed to the signal-catching function. If the second argument is not equal to NULL, it points to a siginfo_t structure containing the reason why the signal was generated (see sig- info.h(3HEAD)); the third argument points to a ucontext_t structure containing the receiving process's context when the signal was delivered (see ucontext.h(3HEAD)). SA_NOCLDWAIT If set and sig equals SIGCHLD, the system will not create zombie processes when children of the calling process exit. If the calling process subsequently issues a wait(3C), it blocks until all of the calling process's child processes terminate, and then returns -1 with errno set to ECHILD. SA_NOCLDSTOP If set and sig equals SIGCHLD, SIGCHLD will not be sent to the calling process when its child processes stop or continue. RETURN VALUES
Upon successful completion, 0 is returned. Otherwise, -1 is returned, errno is set to indicate the error, and no new signal handler is installed. ERRORS
The sigaction() function will fail if: EINVAL The value of the sig argument is not a valid signal number or is equal to SIGKILL or SIGSTOP. In addition, if in a multithreaded process, it is equal to SIGWAITING, SIGCANCEL, or SIGLWP. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ |Standard |See standards(5). | +-----------------------------+-----------------------------+ SEE ALSO
kill(1), Intro(2), exit(2), fcntl(2), getmsg(2), ioctl(2), kill(2), pause(2), putmsg(2), read(2), sigaltstack(2), sigprocmask(2), sigsend(2), sigsuspend(2), waitid(2), write(2), recv(3SOCKET), send(3SOCKET), siginfo.h(3HEAD), signal(3C), signal.h(3HEAD), sigsetops(3C), ucontext.h(3HEAD), wait(3C), attributes(5), standards(5) NOTES
The handler routine can be declared: void handler (int sig, siginfo_t *sip, ucontext_t *ucp); The sig argument is the signal number. The sip argument is a pointer (to space on the stack) to a siginfo_t structure, which provides addi- tional detail about the delivery of the signal. The ucp argument is a pointer (again to space on the stack) to a ucontext_t structure (defined in <sys/ucontext.h>) which contains the context from before the signal. It is not recommended that ucp be used by the handler to restore the context from before the signal delivery. SunOS 5.11 23 Mar 2005 sigaction(2)
All times are GMT -4. The time now is 03:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy