Sigaction Structure Analysis of linux signal mechanism, signal function, and signal capturing

Source: Internet
Author: User
Tags signal handler
The second parameter of the signal installation function sigaction (int signum, const struct sigaction * Act, struct sigaction * oldact) is a pointer to the sigaction structure (the struct name is the same as the function name, do not confuse it ). In the sigaction structure instance, it specifies the processing of specific signals, the information transmitted by signals, and which functions should be shielded during the execution of signal processing functions. Of course, this pointer can also be null, and the process processes the signal by default. The following describes the sigaction structure and general usage.

For the kernel header file, the struct sigaction struct is defined in kernel/include/ASM/signal. H. This header file is also included in kernel/include/Linux/signal. h.
For the header file of the user space, struct sigaction is defined in/usr/include/bits/sigaction. h. The header file is/usr/include/signal. h contains, so if this structure is used in the application, as long as # include You can. Note that the definition in the kernel is different from that in the application. The sigaction structure in the kernel space only supports signal processing functions with the function type of _ sighandler_t, and cannot process additional information transmitted. The specific definition is as follows:

......
/* Type of a signal handler .*/
Typedef void (* _ sighandler_t) (INT );

......
# Ifdef _ KERNEL __
Struct old_sigaction {
_ Sighandler_t sa_handler;
Old_sigset_t sa_mask;
Unsigned long sa_flags;
Void (* sa_restorer) (void );
};

Struct sigaction {
_ Sighandler_t sa_handler;
Unsigned long sa_flags;
Void (* sa_restorer) (void );
Sigset_t sa_mask;/* mask last for extensibility */
};

Struct k_sigaction {
Struct sigaction SA;
};

# Else
/* Here we must cater to libcs that poke about in kernel headers .*/

Struct sigaction {
Union {
_ Sighandler_t _ sa_handler;
Void (* _ sa_sigaction) (int, struct siginfo *, void *);
} _ U;
Sigset_t sa_mask;
Unsigned long sa_flags;
Void (* sa_restorer) (void );
};

# Define sa_handler _ u. _ sa_handler
# Define sa_sigaction _ u. _ sa_sigaction

# Endif/* _ KERNEL __*/

The prototype of sa_handler is a function pointer with the int parameter and void type returned. The parameter is the signal value, so the signal cannot transmit any information except the signal value;

The prototype of sa_sigaction is a function pointer with three parameters: int, struct siginfo *, and void. The first parameter is the signal value. The second parameter is a pointer to the struct siginfo structure, which contains the data value carried by the signal. The third parameter is not used.

Sa_mask specifies which signals should be blocked during the execution of the signal processing program. The current signal is blocked by default.

Sa_flags contains many flag spaces. The more important one is SA_SIGINFO. When this flag is set, it indicates that parameters attached to the signal can be passed to the signal processing function. Even if sa_sigaction specifies the signal processing function, if SA_SIGINFO is not set, the signal processing function cannot obtain the data transmitted from the signal. Access to the information in the signal processing function will cause a segment error.

Sa_restorer is out of date. POSIX does not support it and should not be used any more.

Therefore, when your signal needs to receive additional information, you must assign a signal processing function pointer to sa_sigaction, and assign SA_SIGINFO to sa_flags, similar to the following code:
# Include
......
Void sig_handler_with_arg (int sig, siginfo_t * sig_info, void * unused ){......}

Int main (int argc, char ** argv)
{
Struct sigaction sig_act;
......
Sigemptyset (& sig_act.sa_mask );
Sig_act.sa_sigaction = sig_handler_with_arg;
Sig_act.sa_flags = SA_SIGINFO;

......
}
If your application only needs to receive signals instead of additional information, you need to set sa_handler instead of sa_sigaction. Your program may be similar to the following code:
# Include
......
Void sig_handler (int sig ){......}

Int main (int argc, char ** argv)
{
Struct sigaction sig_act;
......
Sigemptyset (& sig_act.sa_mask );
Sig_act.sa_handler = sig_handler;
Sig_act.sa_flags = 0;

......
}

For more information, see the sigaction man manual.

Supplement:

In short:

// Customize the exit function
Sigact. sa_handler = mysighandler;
Sigemptyset (& sigact. sa_mask );
Sigact. sa_flags = 0;
Sigaction (SIGINT, & sigact, NULL );
Sigaction (SIGTERM, & sigact, NULL );
Sigaction (SIGQUIT, & sigact, NULL );

Or use the signal function to capture signals:

Void (* signal (int signo, void (* handler) (int );

When signal arrives, the program runs a function, which is specified by you.

With various signal definitions:

Use the kill-l command on the terminal to display all signals.
$ Kill-l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN + 1 36) SIGRTMIN + 2 37) SIGRTMIN + 3 38) SIGRTMIN + 4
39) SIGRTMIN + 5 40) SIGRTMIN + 6 41) SIGRTMIN + 7 42) SIGRTMIN + 8
43) SIGRTMIN + 9 44) SIGRTMIN + 10 45) SIGRTMIN + 11 46) SIGRTMIN + 12
47) SIGRTMIN + 13 48) SIGRTMIN + 14 49) SIGRTMIN + 15 50) SIGRTMAX-14
(51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54)
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62)
63) SIGRTMAX-1 64) SIGRTMAX

Among them, the first 31 signals are unreliable signals (non-real-time, which may lead to loss of Signals), and the following signals are reliable signals (Real-Time real_time ).
Queuing, will not be lost ).

1) SIGHUP (suspended) when the user of the running process logs out, the process is notified and the process is terminated.

2) SIGINT (interrupted) when the user presses, the foreground process group is notified to terminate the process

3) when the SIGQUIT user presses or notifies the process to terminate the process

4) SIGILL (illegal command) executes illegal commands, such as errors in executable files, attempts to execute data segments, and Stack Overflow

5) SIGTRAP is generated by the breakpoint instruction or other trap instruction. It is used by the debugger.

6) SIGABRT (abnormal abort) signal generated by calling the abort Function

7) SIGBUS illegal address, including memory address alignment error. eg: access an integer with four characters long, but its address is not a multiple of 4.

8) a fatal arithmetic operation error occurs in SIGFPE, including a floating point operation error, overflow, and division of 0.

(9) SIGKILL: reliably terminates a process when the user sends a signal to the process through the kill-9 command.

10) used by SIGUSR1 users

11) SIGSEGV (out-of-range) terminate a process when a process attempts to access a memory space that is not owned by itself, leading to a memory error

12) used by SIGUSR2 users

13) The SIGPIPE is written to the pipeline without a read process, or the read process for Socket communication SOCT_STREAM has been terminated and then written.

14) when the SIGALRM (time-out) alarm function uses this signal, the clock timer times out and responds.

15) When SIGTERM uses a kill command without parameters to terminate the process

17) SIGCHLD (child process ended) notifies the parent process when the child process is terminated

18) SIGCONT (pause process continues) causes a stopped process to continue. This signal cannot be blocked.

19) SIGSTOP (STOP) control signal, pause the execution of the stopped process. This signal cannot be blocked, processed, or ignored.

20) SIGTSTP (pause/Stop) Interactive stop signal, Ctrl-Z sends this signal

21) sigttin when the background job needs to read data from the user terminal, the terminal driver generates the sigttin Signal

22) sigttou when the background job needs to write data to the user terminal, the terminal driver generates the sigttou Signal

23) When sigurg has "urgent" data or out-of-band data on the network reaches the socket.

24) The sigxcpu exceeds the CPU time limit. This limit can be read/changed by getrlimit/setrlimit.

25) sigxfsz when the process attempts to expand the file to exceed the file size resource limit.

26) sigvtalrm virtual clock signal. Similar to sigalrm, the CPU time occupied by the process is calculated.

27) sigprof (Synopsis time-out) setitimer (2) profiling interval Timer)

28) it is issued when the sigwinch window size is changed.

29) The sigio (asynchronous I/O) file descriptor is ready to start input/output operations.

30) sigpwr power supply failure/restart

31) invalid sigsys system call.

In the Signals listed above,
Signals that cannot be captured, blocked, or ignored by programs include: sigkill and sigstop.
Signals that cannot be restored to the default action include: sigill and sigtrap.
By default, the signals that cause process abortion include: SIGABRT, sigbus, sigfpe, sigill, sigiot, sigquit, SIGSEGV, sigtrap, sigxcpu, sigxfsz
By default, the process exits with the following signals: sigalrm, sighup, SIGINT, sigkill, sigpipe, sigpoll, sigprof, sigsys, sigterm, SIGUSR1, sigusr2, and sigvtalrm.
By default, the signal that causes the process to stop is: sigstop, sigtstp, sigttin, sigttou
Signals ignored by default include sigchld, sigpwr, sigurg, and sigwinch.

In addition, sigio exits in svr4 and is ignored in 4.3bsd; sigcont continues when the process is suspended; otherwise, it is ignored and cannot be blocked.

In Unix/Linux, the signal function is a complicated one. Its definition prototype is as follows:
Void (* signal (INT signo, void (* func) (INT)
In this function, the outermost function body
Void (* XXX) (INT) indicates a pointer pointing to a function XXX. The Function Represented by XXX requires an int-type parameter and returns void.
Signal (INT signo, void (* func) (INT) is the main body of the signal function.
It requires two int-type signo parameters and a function pointing to the function.
Void (* func) (INT ).
Because of its complexity, typedef is used in [plauger 1992] to simplify it.
Typedef void sigfuc (INT); // here it can be regarded as a return value.
That's how to simplify the signal function.
Sigfunc * signal (INT, sigfuc *);

The signal. h header file contains the following definitions:
# Define sig_err (void (*) ()-1
# Define SIG_DFL (void (*) () 0
# Define SIG_IGN (void (*) () 1

The interpretation of the signal function in the original chapter is not very clear, for the signal function, see the http://narmy.cn/linux/read.php/90.htm

From: http://blog.csdn.net/lanmanck/archive/2009/09/19/4568911.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.