Sigaction system Call of Linux signal function __oracle

Source: Internet
Author: User
Tags function prototype posix

Directory

1. Sigaction function detailed 1.1 structure body siginfo_t Detailed 1.2 sa_sigaction function pointer 3rd parameter void * Explanation

1. sigaction function Detailed

The Sigaction function is more robust and more powerful than the signal function, and its function prototype is:

#include <signal.h>
 int sigaction (int signum, const struct sigaction *act,
                struct sigaction);
Return value: Success: 0
       failure:-1 Error reason placed in errno


For parameters:
-Signum: Refers to the type of signal to be captured, supporting 64 kinds of signals on the Linux system (except Sigkill and Sigstop)
-act: This parameter specifies the new signal processing method.
-Oact: This parameter outputs the previous processing of the signal (if not NULL)
As can be seen from the parameters of the function: both act and oact are pointers to sigaction struct types, sigaction structures describe the details of signal processing, and their structure is defined as follows:

The structure describes the action taken when the signal arrives
struct sigaction
  {
#ifdef __use_posix199309
    Union
      {
    __sighandler_t sa _handler;
    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

    __sigset_t sa_mask;

    int sa_flags;

    void (*sa_restorer) (void);
  


For a detailed description of the members in the struct sigaction of the struct body:
-Sa_hander: Specifies the signal processing function, which is the same as the parameter handler in the signal () function, which is primarily used to support the form of the old installation function signal () of the signal.
-Sa_sigaction: For the new signal installation mechanism. When a handler function is called, not only can the signal be numbered, but the reason for the call and the context in which the problem is generated are available.
%%%% Note: Sa_hander and sa_sigaction are mutually exclusive, and if you perform Sa_hander, you cannot execute sa_sigaction; and vice versa%%%%
-Sa_mask: A signal mask used to set up a process, that is, to add a signal mask based on the original process's signal mask to specify which signals are not enough to send to this process. Sa_mask is the sigset_t type of the semaphore set, which specifies a set of signals.
-Sa_restorer: This member is obsolete, do not use.
-Sa_flags: Used to set the behavior of a program when it receives a signal. The optional values are the following table:

Options meaning
Sa_nocldstop If the sig parameter of the sigaction is SIGCHLD, the flag is set to indicate that the child process is paused without generating
Sa_nocldwait If the sigaction sig parameter is SIGCHLD, the flag is set to indicate that the child process ends without a zombie process
Sa_siginfo Using sa_sigaction as a signal processing function (rather than sa_handler), it provides more relevant information to the process
Sa_onstack Call a signal processing function on an optional signal stack with a sigaltstack function color or
Sa_restart Re-invoke system calls terminated by this signal
Sa_nodefer When a signal is received and enters its signal processing function, the signal is not masked. By default, we expect the process to no longer receive the same signal when processing a signal, or else it will cause some race conditions
Sa_resethand The default processing method of the reply signal after the execution of the signal processing function
Sa_interrupt Terminal system calls


Code 1:

/************************************************************************* * File name:sigaction.c * author:the ans Wer * Function:other * mail:2412799512@qq.com * Created time:2018 year April 29 Sunday 05:06 43 seconds ************* /#include <stdio.h> #include <signal.h> # Include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <string.h> void Sig_
    Handler (int signum,siginfo_t *info,void *data) {printf ("Receive signal [%d]\n", Signum);
Sleep (5);
        int main (int argc,char **argv) {if (argc <2) {fprintf (stderr, "usage:%s signum.\n", argv[0]);
    return-1;
    } struct Sigaction Act;
    int Signum;
    Signum = Atoi (argv[1]);
    Sigemptyset (&act.sa_mask);
    Act.sa_sigaction = Sig_handler; Act.sa_flags |
    Sa_siginfo;
      if (Sigaction (Signum,&act,null) < 0) {fprintf (stderr, "sigaction err.\n");  Return-2;
        while (1) {sleep (2);
    Puts ("Wait for the Sigal");
return 0;
 }

The program prints every 2 seconds: wait for the Sigal. When the Linux terminal presses the key combination: Ctrl + C, the user-defined function is invoked: ^creceive signal [2] then continues to print every 2 seconds for the Sigal. If you press the combination key at the terminal: Ctrl + \, the program exits and terminates.

2018-05-06 Supplements:

As explained above, the structure body member Sa_flags is used to describe and specify the sigaction of the Sigaction function is the old signal or the newest one. If the new signal function is sa_sigaction, then: Sa_flags | Sa_siginfo ("|") or-> bitwise operation); If you want to use the old Sa_handle, specify sa_flags = 0;
Code 2:

/************************************************************************* * File name:sigaction.c * author:the ans Wer * Function:other * mail:2412799512@qq.com * Created time:2018 year May 06 Sunday 14:48 03 seconds ************* /#include <stdio.h> #include <signal.h> # Include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <string.h> void Sig_
    Handler (int signum,siginfo_t *info,void *data) {printf ("Receive signal [%d]\n", Signum);
Sleep (5);
    } void Oldsig_handler (int signo) {printf ("Oldsig_handler receive signal [%d]\n", Signo);
Sleep (2);
        int main (int argc,char **argv) {if (argc <2) {fprintf (stderr, "usage:%s signum.\n", argv[0]);
    return-1;
    } struct Sigaction Act;
    int Signum;
    Signum = Atoi (argv[1]);
    Sigemptyset (&act.sa_mask);  Act.sa_handler = Oldsig_handler; Assign the Oldsig_handler function address to SA_hanlder function pointer act.sa_flags = 0;
    Specifies sa_flags = 0;
        if (Sigaction (Signum,&act,null) < 0) {fprintf (stderr, "sigaction err.\n");
    Return-2;
        while (1) {sleep (2);
    Puts ("Wait for the Sigal");
return 0;

 }

The program implements the same functionality as "code 1". See "Code 1" for a detailed description.
1.1 Structural Body siginfo_t detailed explanation

Previously said Sigaction is a sigal function of the enhanced version. So the Sigaction function will certainly have functions that sigal function does not have, although "code 1" with the sa_sigaction new function, but I only use the first parameter, the signal number (if only use this parameter, and direct use of Sigal no difference).

Man Sigaction can see the members of the Sgaction body sa_sigaction is a function pointer to a function that has a public 3 parameters, as follows:
void (sa_sigaction) (int, siginfo_t, void *);

The siginfo_t is a structural body, defined as follows:

typedef struct {int Si_signo;       /* signal */int si_errno;        /* error value, see <error.h>/int si_code;

     /* Signal Code *//*************** Union type ******************/Union {int _pad[__si_pad_size];  /* Kill ().  */struct {__pid_t si_pid/* Send the ID of the process. * * __uid_t Si_uid;  /* The true ID of the send 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/* Send the ID of the process. * * __uid_t Si_uid;  /* The true ID of the send process. * * sigval_t Si_sigval;

    /* Signal value */} _rt;  /* sigchld.  */struct {__pid_t si_pid/* Send the ID of the process. * * __uid_t Si_uid;  /* The true ID of the send process.  */int si_status;  /* exit value or signal.  * * __sigchld_clock_t si_utime;  /* User time consumption.  * * __sigchld_clock_t si_stime;  /* System time consumption. */} _SIGCHLD;  * * Sigill, SIGFPE, SIGSEGV, Sigbus.  * * struct {void *si_addr;  /* Fault break Trap/memory reference.  */short int si_addr_lsb;  /* Valid LSB for report address.

    */} _sigfault;  /* Sigpoll.   * * struct {long int si_band; /* The band event in the Sigpoll signal.   */int si_fd; /* File descriptor.

    */} _sigpoll;  /* Sigsys.   * * struct {void *_call_addr;   * * User call/int _syscall;  /* Triggers the system call number. * * unsigned int _arch;  /* Audit_arch_ system call.
     */} _sigsys;   } _sifields; %%%% Union range%%%%/******************************************/} siginfo_t __si_alignment;

A consortium (also known as "shared Body") is embedded in the siginfo_t structure, and multiple struct strcut types are nested within the consortium. The range of the Union of the Union is:
Union {Union member category}_sifields, in addition to this Union, the SIGINFO_T structure is mainly 3 members of the structure body:

int Si_signo;       /* Signal  *
/int si_errno;       /* error value, see <error.h>
/int si_code;        /* signal code; Si_code is a value (not a bit mask) that explains why this signal is sent.


Si_signo, Si_errno, and si_code are defined for all signals. (Si_errno are usually not used on Linux.) The rest of the structure may be a union, so you should read only fields that make sense for a given signal. 1.2 sa_sigaction function pointer 3rd argument void * Explanation

The Sa_sigaction function is a function pointer, which points to a function of 3 parameters, which has previously explained the meaning of the preceding 2 parameters in the formal parameter. The following is a detailed explanation of the 3rd parameter. Sa_sigaction point to the specific form of the function:
void
Handler (int sig, siginfo_t *info, void *ucontext)
{
...
}

This is a pointer to the UCONTEXT_T structure body, converted to a void * type. The structure that the field points to contains a signal context. The information saved on the user space stack. Specific can go to refer to: Sigreturn (2). Further information is in GetContext (3). You can see the ucontext_t structure definition. But the 3rd parameter is usually not used;

The 2nd parameter in Sa_sigaction is used in code 3: and the process ID and the real ID for which the signal is sent are printed.

/************************************************************************* * File name:sigaction.c * author:the ans Wer * Function:other * mail:2412799512@qq.com * Created time:2018 year April 29 Sunday 05:06 43 seconds ************* /#include <stdio.h> #include <signal.h> # Include <sys/types.h> #include <unistd.h> #include <sys/stat.h> #include <stdlib.h> #include
    <string.h> void Sig_handler (int signum,siginfo_t *info,void *data) {printf ("main pid: [%d]\n", Getpid ());

    printf ("Receive signal [%d]\n", Signum);
    printf ("Signal number [%d]\n", Info->si_signo);
    printf ("Signal code [%d]\n", Info->si_code);

    printf ("Sigal errno [%d]\n", Info->si_errno);
    printf ("Sending process ID [%d]\n", info->si_pid);
    printf ("Real user ID of sending process [%d]\n", info->si_uid);


    printf ("Si_user [%d]\n", si_user);
Sleep (5); } void Oldsig_handler(int signo)
    {printf ("Oldsig_handler receive signal [%d]\n", Signo);
Sleep (2);
        int main (int argc,char **argv) {if (argc <2) {fprintf (stderr, "usage:%s signum.\n", argv[0]);
    return-1;
    } struct Sigaction Act;
    int Signum;
    Signum = Atoi (argv[1]);
    Sigemptyset (&act.sa_mask);
    Act.sa_sigaction = Sig_handler; Act.sa_flags |
    Sa_siginfo;
        if (Sigaction (Signum,&act,null) < 0) {fprintf (stderr, "sigaction err.\n");
    Return-2;
        while (1) {sleep (2);
    Puts ("Wait for the Sigal");
return 0;

 }

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.