summaryrefslogblamecommitdiffhomepage
path: root/src/nxt_worker_process.c
blob: 1a7d0c5392e06cc825d420e17b5f0b74e38c54cb (plain) (tree)
1
2
3
4
5
6
7
8
9






                            
                        
                     
                             
                       

 
                                                      
                                                                          
                
                                                                           
                
                                                                           


                
                                                          
                                    

                                     
                          
                          
                                

                                         
                                         
                                         
                                         

                         


  





                                                       
                                

                                         
                                         
                                         
                                         

                         


  

                                                          
                                


                                     


                                         
                                         
                                         
                                         















                                                             
                                         

                         


  
                                                       





                                                                  
                                                                 



                         
           
                                         
 





                                      
 
                               
 
                                                
 
                                                       





                                        
                                                                  

                               
                                                               

     
                                     
 

                                      

                        
                                               





                            
                                      

     
                           



           
                                                                          
 

                                                             


 
    
                                                                           
 
                                  



           
                                                                           
 

                                                    


                      
                           



           
                                                                           
 

                                                    


                          
                                  
 

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <nxt_runtime.h>
#include <nxt_port.h>
#include <nxt_main_process.h>
#include <nxt_router.h>


static void nxt_worker_process_quit(nxt_task_t *task);
static void nxt_worker_process_signal_handler(nxt_task_t *task, void *obj,
    void *data);
static void nxt_worker_process_sigterm_handler(nxt_task_t *task, void *obj,
    void *data);
static void nxt_worker_process_sigquit_handler(nxt_task_t *task, void *obj,
    void *data);


nxt_port_handler_t  nxt_worker_process_port_handlers[] = {
    nxt_worker_process_quit_handler,
    nxt_port_new_port_handler,
    nxt_port_change_log_file_handler,
    nxt_port_mmap_handler,
    nxt_port_data_handler,
    nxt_port_remove_pid_handler,
    NULL, /* NXT_PORT_MSG_READY        */
    NULL, /* NXT_PORT_MSG_START_WORKER */
    NULL, /* NXT_PORT_MSG_SOCKET       */
    NULL, /* NXT_PORT_MSG_MODULES      */
    NULL, /* NXT_PORT_MSG_CONF_STORE   */
    nxt_port_rpc_handler,
    nxt_port_rpc_handler,
};


nxt_port_handler_t  nxt_app_process_port_handlers[] = {
    nxt_worker_process_quit_handler,
    nxt_port_new_port_handler,
    nxt_port_change_log_file_handler,
    nxt_port_mmap_handler,
    nxt_port_app_data_handler,
    nxt_port_remove_pid_handler,
    NULL, /* NXT_PORT_MSG_READY        */
    NULL, /* NXT_PORT_MSG_START_WORKER */
    NULL, /* NXT_PORT_MSG_SOCKET       */
    NULL, /* NXT_PORT_MSG_MODULES      */
    NULL, /* NXT_PORT_MSG_CONF_STORE   */
    nxt_port_rpc_handler,
    nxt_port_rpc_handler,
};


nxt_port_handler_t  nxt_router_process_port_handlers[] = {
    nxt_worker_process_quit_handler,
    nxt_router_new_port_handler,
    nxt_port_change_log_file_handler,
    nxt_port_mmap_handler,
    nxt_router_conf_data_handler,
    nxt_router_remove_pid_handler,
    NULL, /* NXT_PORT_MSG_READY        */
    NULL, /* NXT_PORT_MSG_START_WORKER */
    NULL, /* NXT_PORT_MSG_SOCKET       */
    NULL, /* NXT_PORT_MSG_MODULES      */
    NULL, /* NXT_PORT_MSG_CONF_STORE   */
    nxt_port_rpc_handler,
    nxt_port_rpc_handler,
};


nxt_port_handler_t  nxt_discovery_process_port_handlers[] = {
    nxt_worker_process_quit_handler,
    nxt_port_new_port_handler,
    nxt_port_change_log_file_handler,
    nxt_port_mmap_handler,
    nxt_port_data_handler,
    nxt_port_remove_pid_handler,
    NULL, /* NXT_PORT_MSG_READY        */
    NULL, /* NXT_PORT_MSG_START_WORKER */
    NULL, /* NXT_PORT_MSG_SOCKET       */
    NULL, /* NXT_PORT_MSG_MODULES      */
    NULL, /* NXT_PORT_MSG_CONF_STORE   */
    nxt_port_rpc_handler,
    nxt_port_rpc_handler,
};


const nxt_sig_event_t  nxt_worker_process_signals[] = {
    nxt_event_signal(SIGHUP,  nxt_worker_process_signal_handler),
    nxt_event_signal(SIGINT,  nxt_worker_process_sigterm_handler),
    nxt_event_signal(SIGQUIT, nxt_worker_process_sigterm_handler),
    nxt_event_signal(SIGTERM, nxt_worker_process_sigquit_handler),
    nxt_event_signal(SIGCHLD, nxt_worker_process_signal_handler),
    nxt_event_signal(SIGUSR1, nxt_worker_process_signal_handler),
    nxt_event_signal(SIGUSR2, nxt_worker_process_signal_handler),
    nxt_event_signal_end,
};


static void
nxt_worker_process_quit(nxt_task_t *task)
{
    nxt_uint_t           n;
    nxt_queue_t          *listen;
    nxt_runtime_t        *rt;
    nxt_queue_link_t     *link, *next;
    nxt_listen_event_t   *lev;
    nxt_listen_socket_t  *ls;

    rt = task->thread->runtime;

    nxt_debug(task, "close listen connections");

    listen = &task->thread->engine->listen_connections;

    for (link = nxt_queue_first(listen);
         link != nxt_queue_tail(listen);
         link = next)
    {
        next = nxt_queue_next(link);
        lev = nxt_queue_link_data(link, nxt_listen_event_t, link);
        nxt_queue_remove(link);

        nxt_fd_event_close(task->thread->engine, &lev->socket);
    }

    if (rt->listen_sockets != NULL) {

        ls = rt->listen_sockets->elts;
        n = rt->listen_sockets->nelts;

        while (n != 0) {
            nxt_socket_close(task, ls->socket);
            ls->socket = -1;

            ls++;
            n--;
        }

        rt->listen_sockets->nelts = 0;
    }

    nxt_runtime_quit(task);
}


static void
nxt_worker_process_signal_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_trace(task, "signal signo:%d (%s) recevied, ignored",
              (int) (uintptr_t) obj, data);
}


void
nxt_worker_process_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
{
    nxt_worker_process_quit(task);
}


static void
nxt_worker_process_sigterm_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_debug(task, "sigterm handler signo:%d (%s)",
              (int) (uintptr_t) obj, data);

    /* A fast exit. */

    nxt_runtime_quit(task);
}


static void
nxt_worker_process_sigquit_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_debug(task, "sigquit handler signo:%d (%s)",
              (int) (uintptr_t) obj, data);

    /* A graceful exit. */

    nxt_worker_process_quit(task);
}