Description of struct sigaction and signal processing functions-1

Source: Internet
Author: User
Tags signal handler

From: http://hi.baidu.com/phps/blog/item/b064b7b71944cff630add1b4.html

 

A struct related to Linux CITIC code, struct sigaction, is mainly used when the sigaction signal is installed and the sigqueue signal is sent.
The structure is located in/usr/include/bits/sigaction. h.
You can find the description of this structure in it {the system I currently implement is as 4 kernel version 2.6.9-5.el}
/* Structure describing the action to be taken when a signal arrives .*/
Struct sigaction
{
/* Signal handler .*/
# Ifdef _ use_posix199309
Union
{
/* Used if sa_siginfo is not set .*/
_ Sighandler_t sa_handler;
/* Used if sa_siginfo is set .*/
Void (* sa_sigaction) (INT, siginfo_t *, void *);
}
_ Sigaction_handler;



# Define sa_handler _ sigaction_handler.sa_handler
# Define sa_sigaction _ sigaction_handler.sa_sigaction
# Else
_ Sighandler_t sa_handler;
# Endif

 

/* Additional set of signals to be blocked .*/
_ Sigset_t sa_mask;

/* Special flags .*/
Int sa_flags;

/* Restore handler .*/
Void (* sa_restorer
) (Void );
};

I checked some books on the Internet. This structure is probably like this.
Sa_restorer is out of date and POSIX does not support it and should not be used again.

1. The two elements _ sa_handler and * _ sa_sigaction in the combined data structure specify the signal Association function, that is, the user-specified signal processing function. Besides User-Defined processing functions
In addition, it can also be SIG_DFL
(Use the default processing method), or SIG_IGN
(Ignore signal ).
2. The processing function specified by _ sa_handler has only one parameter, namely, the signal value. Therefore, the signal cannot transmit any information except the signal value.

3. The signal processing function specified by _ sa_sigaction has three parameters for real-time signals (of course, non-real-time signals are also supported ), it specifies a three-parameter signal processing function. The first parameter is the signal value, the third parameter is not used (posix has no standard to use this parameter), and the second parameter is a pointer to the siginfo_t structure, the structure contains the data value carried by the signal. The structure pointed to by the parameter is as follows:
This is defined in/usr/include/bits/siginfo. h.

Typedef struct siginfo
{
Int si_signo;/* Signal number .*/
Int si_errno;/* If non-zero, an errno value associated
This signal, as defined in <errno. h> .*/
Int si_code;/* Signal code .*/

Union
{
Int _ pad [_ SI_PAD_SIZE];

/* Kill ().*/
Struct
{
_ Pid_t si_pid;/* Sending process ID .*/
_ Uid_t si_uid;/* Real user ID of sending process .*/
} _ Kill;

/* POSIX.1b timers .*/
Struct
{
Int si_tid;/* timer ID .*/
Int si_overrun;/* overrun count .*/
Sigval_t si_sigval;/* signal value .*/
} _ Timer;

/* Posix.1b signals .*/
Struct
{
_ Pid_t si_pid;/* Sending process ID .*/
_ Uid_t si_uid;/* Real user ID of sending process .*/
Sigval_t si_sigval;/* Signal value .*/
} _ Rt;

/* SIGCHLD .*/
Struct
{
_ Pid_t si_pid;/* Which child .*/
_ Uid_t si_uid;/* Real user ID of sending process .*/
Int si_status;/* Exit value or signal .*/
_ Clock_t si_utime;
_ Clock_t si_stime;
} _ Sigchld;

/* SIGILL, SIGFPE, SIGSEGV, SIGBUS .*/
Struct
{
Void * si_addr;/* Faulting insn/memory ref .*/
} _ Sigfault;

/* SIGPOLL .*/
Struct
{
Long int si_band;/* Band event for SIGPOLL .*/
Int si_fd;
} _ Sigpoll;
} _ Sifields;
} Siginfo_t;

Is it too complicated? I have a simplified version on the Internet.

Siginfo_t {
Int si_signo;/* signal value, which is meaningful to all signals */
Int si_errno;/* errno value, meaning all signals */
Int si_code;/* cause of signal generation, meaning to all signals */
Union {/* join data structure, different members adapt to different signals */
// Ensure that sufficient storage space is allocated.
Int _ pad [SI_PAD_SIZE];
// Meaningful SIGKILL Structure
Struct {
...
}...
......
......
// Meaningful structure for SIGILL, SIGFPE, SIGSEGV, and SIGBUS
Struct {
...
}...
......
}
}







4. sa_mask specifies which signals should be blocked during the execution of the signal processing program. By default, the current signal is blocked to prevent nested transmission of the signal, unless the sa_nodefer or sa_nomask flag is specified.

Note: The prerequisite for signal blocking specified by sa_mask is that the signal specified by sa_mask is blocked only when the processing function of the signal is executed by sigaction.

5. sa_flags contains many flag spaces, including the sa_nodefer and sa_nomask. Another important flag is sa_siginfo. When this flag is set, the parameters attached to the signal can be passed to the signal processing function. Therefore, you should specify a processing function for sa_sigaction in the sigaction structure instead of a signal processing function for sa_handler. Otherwise, setting this flag becomes meaningless. Even if a signal processing function is specified for sa_sigaction, if sa_siginfo is not set, the signal processing function cannot obtain the data transmitted from the signal, access to this information in the signal processing function will lead to a segment error (segmentation fault ).

Note: many documents have stated that if this flag is set, three-parameter signal processing functions must be defined. This is not the case. The verification method is simple: implement a single parameter signal processing function and set this flag in the program to view the running result of the program. In fact, you can regard this flag as a switch for whether to transmit a parameter. If this bit is set, a parameter is transmitted. Otherwise, no parameter is transmitted.

The structure is almost the same. In fact, I fainted several times when I saw these things. To put it simply, a few words are:

If we want to trigger some actions when a signal is generated, we can install the signal.

Signal is used to install unreliable signals. Linux is now implemented using sigaction.
Sigaction is used to install reliable signals. Of course, it can also install unreliable signals and provide more information.

When signal is installed, only two parameters are required. One is the signal value and the other is the function triggered when the signal is generated. This function accepts an integer.
When sigaction is installed, there are three parameters. The first parameter is the signal value, and the second parameter is the sigaction structure pointer. This structure shows the functions called when the signal occurs and other information. The third parameter is also a sigaction structure pointer oact, which does not understand the man page statement and seems to be inconsistent with the actual operation.

If the argument oact is not a null pointer, the action previusly associated with the signal is stored in the location pointed to by the argument oact. if the argument act is a null pointer, signal handling is unchanged; thus, the call can be used to enquire about the current handling of a given signal.

 

Let's talk about the second parameter of the sigaction function. Its main member is the trigger function specified by sa_handler, which only includes one parameter, that is, the signal value. This is no different from signal call, the trigger function specified by sa_sigaction has three parameters. The first parameter is the signal value, and the second parameter is the structure siginfo of the additional information of the package, the third parameter is null. If you want to pass additional information to the trigger function, you must set the sa_flag of the second parameter sigaction structure to sa_siginfo.

According to my understanding of man, this means that sa_sigaction is used when sa_flag sets sa_siginfo, and sa_handler is used when no sa_handler is set.

 

 

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.