Nginx source code analysis-how to send signals

Source: Internet
Author: User

Author: APO
Link: http://blog.csdn.net/livelylittlefish/article/details/7308142

Content

0. Order

1. send signals

(1) Start nginx with the "-s" option

(2) ngx_signal_process () function processing

(3) ngx_ OS _signal_process () function processing

2. Summary

0. Order

This article mainly analyzes how nginx sends signals and processes them. If there is no special description in this article,. indicates the nginx-1.0.4 code directory, this article is/usr/src/nginx-1.0.4.

1. send signals

The nginx sending signal is completed by a new process ngx_process = ngx_process_signaller = 2. The following three steps are completed.

(1) Start nginx with the "-s" option

Only four types of signals are processed: "Stop", "quit", "Reopen", and "reload ". The sending method is as follows. See how the ngx_get_options () function processes the "-s" option.

#./Nginx-s reopen

Ngx_get_options () assigns the "Reopen" signal to the global variable ngx_signal, and assigns ngx_process to ngx_process_signaller = 2. This command starts a new nginx process, starts the main () function, and directly calls ngx_signal_process (), as shown below.

    if (ngx_signal) {        return ngx_signal_process(cycle, ngx_signal);    }

You can perform an experiment on your own to enable nginx, and then enable the nginx sending signal to view the changes in the nginx process.

 

(2) ngx_signal_process () function processing

Function:

  • Read the ngx_core_module Configuration Structure ngx_core_conf_t;
  • Find the Working Process file according to the configuration structure, such as "/usr/local/nginx/logs/nginx. PID" (This file stores the nginx process ID, that is, pid );
  • Open the file and read the PID;
  • Call ngx_ OS _signal_process () to send the signal;

 

(3) ngx_ OS _signal_process () function processing

  • Traverse the signals array and find the corresponding signo Based on the given signal name;
  • Call kill to send the signo signal to the PID;

 

The Code is as follows. For more information, see source code.

Ngx_int_tngx_ OS _signal_process (ngx_cycle_t * cycle, char * Name, ngx_int_t PID) {ngx_signal_t * SIG; For (Sig = signals; sig-> signo! = 0; sig ++) {If (ngx_strcmp (name, sig-> name) = 0) {If (kill (PID, sig-> signo )! =-1) {/* send a signal to the process through system call */return 0;} ngx_log_error (ngx_log_alert, cycle-> log, ngx_errno, "Kill (% P, % d) failed ", PID, sig-> signo) ;}} return 1 ;}

Kill-Send signal to a process

Description

The kill () system call can be used to send any signal to any process group or process.

If PID is positive, then signal SIG is sent to the process with the ID specified by PID.

If PID equals 0, then SIG is sent to every process in the Process Group of the calling process.

If PID equals-1, then SIG is sent to every process for which the calling process has permission to send signals, doesn t for process 1 (init), but see below.

If PID is less than-1, then SIG is sent to every process in the process group whose ID is-PID.

If SIG is 0, then no signal is sent, but error checking is still completed MED; this can be used to check for the existence of a process ID or process group ID.

For a process to have permission to send a signal it must either be privileged (under Linux: have the cap_kill capability ), or the real or valid tive user ID of the sending process must equal the real or saved set-user-ID
Of the target process. In the case of sigcont it suffices when the sending and processing ing processes belong to the same session.

2. Summary

This article mainly analyzes how nginx sends signals during running.

Reference

# Man kill

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.