Signal mechanism in Linux kernel-from user layer to kernel layer

Source: Internet
Author: User
Tags posix

Signal mechanism in Linux kernel-from user layer to kernel layer

Kernel version:2.6.14

CPU architecture:ARM920T

Author:Ce123. (http://blog.csdn.net/ce123)

1. Introduction

If a process needs to process a signal, it must register the signal in the process. The registration signal is mainly used to determine the ing relationship between the signal value and the action of the process against the signal value, that is, the process to be processed and the operation to be executed when the signal is transmitted to the process. There are two main functions for signal registration: Signal () and sigaction ().

2. Signal ()

The signal () function prototype is as follows:

void (*signal(int signum, void (*handler)(int)))(int); 

Add the following header file to the process that uses this call:

#include <signal.h> 

The preceding Declaration format is complex. If you do not know how to use it, you can use the format defined in the following type (POSIX definition ):

typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); 

However, this format has different types of definitions in different systems. Therefore, we recommend that you refer to the manual. In the call, the SIGNUM parameter indicates the signal to set the processing method. The second handler parameter is a processing function, or

  • Sig_ign: Ignore the signal specified by the SIGNUM parameter.
  • Sig_dfl: the signal processing method specified by the recovery parameter SIGNUM is the default value.

The integer parameter passed to the signal processing routine is the signal value, so that a signal processing routine can process multiple signals. When the system calls signal (), the return value is the processing routine of the previous signal SIGNUM or the error code sig_err is returned.

Signal () sets the user State processing function for a specified signal by calling sys_signal. Sys_signal () is defined as follows:

/* * For backwards compatibility.  Functionality superseded by sigaction. */asmlinkage unsigned longsys_signal(int sig, __sighandler_t handler){struct k_sigaction new_sa, old_sa;int ret;new_sa.sa.sa_handler = handler;new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;ret = do_sigaction(sig, &new_sa, &old_sa);return ret ? ret : (unsigned long)old_sa.sa.sa_handler;}

_ Sighandler_t is defined as follows:

typedef void __signalfn_t(int);typedef __signalfn_t __user *__sighandler_t;

The signal is specified by the first parameter of sys_signal (), and the address of the signal processing function is specified by the second parameter. Sys_signal () sets a k_sigaction structure based on the two parameters and calls do_sigaction (). The definition of this function will be explained in detail later.

2. sigaction ()

The sigaction () function prototype is as follows:

sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);

The system call corresponding to sigaction () is do_sigaction (). The do_sigaction () function is described as follows:

2.1do _ sigaction ()

Intdo_sigaction (INT Sig, const struct k_sigaction * Act, struct k_sigaction * oact) {struct k_sigaction * k; If (! Valid_signal (SIG) | sig <1 | (Act & sig_kernel_only (SIG) Return-einval; k = & currentt-> sighand-> action [sig-1]; spin_lock_irq (& currentt-> sighand-> siglock); If (signal_pending (current) {/** if there might be a fatal signal pending on multiple * threads, make sure we take it before changing the action. */spin_unlock_irq (& currentt-> sighand-> siglock); Return-erestartnointr;} If (oact) // convert the original k_sigacti On is saved to the oact structure. Here we copy the entire data structure * oact = * k; If (ACT) {/** POSIX 3.3.1.3: * "setting a signal action to sig_ign for a signal that is * Pending shall cause the pending signal to be discarded, * whether or not it is blocked. "**" setting a signal action to sig_dfl for a signal that is * pending and whose default action is to ignore the signal * (for example, sigchld ), shall cause the pending signal * Be discarded, whether or not it is blocked "*/If (Act-> SA. sa_handler = sig_ign | (Act-> SA. sa_handler = sig_dfl & sig_kernel_ignore (SIG) {/** this is a fairly rare case, so we only take the * tasklist_lock once we're re sure we'll need it. * Now we must do this little unlock and relock * Dance to maintain the lock hierarchy. */struct task_struct * t = current; spin_unlock_irq (& T-> sighand-> siglock ); Read_lock (& tasklist_lock); spin_lock_irq (& T-> sighand-> siglock); * k = * Act; // copy the new k_sigaction structure to the sighand-> sigdelsetmask (& K-> SA. sa_mask, sigmask (sigkill) | sigmask (sigstop); rm_from_queue (sigmask (SIG), & T-> signal-> shared_pending); do {rm_from_queue (sigmask (SIG ), & T-> pending); recalc_sigpending_tsk (t); t = next_thread (t);} while (T! = Current); spin_unlock_irq (& Current-> sighand-> siglock); read_unlock (& tasklist_lock); Return 0;} * k = * Act; // copy the new k_sigaction structure to the sighand-> sigdelsetmask (& K-> SA. sa_mask, sigmask (sigkill) | sigmask (sigstop);} spin_unlock_irq (& currentt-> sighand-> siglock); Return 0 ;}
Related Article

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.