summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_worker_process.c
blob: a30ac9e636e3e3f92f3d62c03714449e4eb0707a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

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

#include <nxt_main.h>
#include <nxt_cycle.h>
#include <nxt_port.h>
#include <nxt_master_process.h>


static void nxt_worker_process_quit(nxt_task_t *task);
static void nxt_worker_process_quit_handler(nxt_task_t *task,
    nxt_port_recv_msg_t *msg);
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);


static nxt_process_port_handler_t  nxt_worker_process_port_handlers[] = {
    nxt_worker_process_quit_handler,
    nxt_process_port_new_handler,
    nxt_process_port_change_log_file_handler,
    nxt_process_port_data_handler,
};


static const nxt_event_sig_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(SIGUSR1, nxt_worker_process_signal_handler),
    nxt_event_signal_end,
};


void
nxt_worker_process_start(void *data)
{
    nxt_int_t                  n;
    nxt_cycle_t                *cycle;
    nxt_thread_t               *thr;
    nxt_process_port_t         *proc;
    const nxt_event_set_ops_t  *event_set;

    cycle = data;

    nxt_thread_init_data(nxt_thread_cycle_data);
    nxt_thread_cycle_set(cycle);

    thr = nxt_thread();

    nxt_log_error(NXT_LOG_INFO, thr->log, "worker process");

    nxt_process_title("nginext: worker process");

    cycle->type = NXT_PROCESS_WORKER;

    nxt_random_init(&nxt_random_data);

    if (getuid() == 0) {
        /* Super-user. */

        n = nxt_user_cred_set(&cycle->user_cred);
        if (n != NXT_OK) {
            goto fail;
        }
    }

    /* Update inherited master process event engine and signals processing. */
    thr->engine->signals->sigev = nxt_worker_process_signals;

    event_set = nxt_service_get(cycle->services, "engine", cycle->engine);
    if (event_set == NULL) {
        goto fail;
    }

    if (nxt_event_engine_change(thr, &nxt_main_task, event_set, cycle->batch) != NXT_OK) {
        goto fail;
    }

#if 0
    if (nxt_cycle_listen_sockets_enable(thr, cycle) != NXT_OK) {
        goto fail;
    }
#endif

    proc = cycle->processes->elts;

    /* A master process port. */
    nxt_port_read_close(proc[0].port);
    nxt_port_write_enable(&nxt_main_task, proc[0].port);

    /* A worker process port. */
    nxt_process_port_create(thr, &proc[cycle->current_process],
                            nxt_worker_process_port_handlers);

#if (NXT_THREADS)
    {
        nxt_int_t  ret;

        ret = nxt_cycle_thread_pool_create(thr, cycle, cycle->auxiliary_threads,
                                           60000 * 1000000LL);

        if (nxt_slow_path(ret != NXT_OK)) {
            goto fail;
        }
    }

    nxt_app_start(cycle);
#endif

    return;

fail:

    exit(1);
    nxt_unreachable();
}


static void
nxt_worker_process_quit(nxt_task_t *task)
{
    nxt_uint_t               n;
    nxt_cycle_t              *cycle;
    nxt_queue_t              *listen;
    nxt_queue_link_t         *link, *next;
    nxt_listen_socket_t      *ls;
    nxt_event_conn_listen_t  *cls;

    cycle = nxt_thread_cycle();

    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);
        cls = nxt_queue_link_data(link, nxt_event_conn_listen_t, link);
        nxt_queue_remove(link);

        nxt_event_fd_close(task->thread->engine, &cls->socket);
    }

    if (cycle->listen_sockets != NULL) {

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

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

            ls++;
            n--;
        }

        cycle->listen_sockets->nelts = 0;
    }

    nxt_cycle_quit(task, cycle);
}


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);
}


static 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_cycle_quit(task, NULL);
}


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);
}