0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <nginx.h>
|
|
10
|
|
11
|
|
12 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
|
2
|
13 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
|
|
14 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
|
0
|
15 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
|
|
16 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
|
|
17 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
278
|
18 static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
22
|
19 static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
126
|
20 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
21 void *conf);
|
0
|
22
|
|
23
|
10
|
24 static ngx_conf_enum_t ngx_debug_points[] = {
|
|
25 { ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
|
|
26 { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
|
|
27 { ngx_null_string, 0 }
|
126
|
28 };
|
10
|
29
|
|
30
|
0
|
31 static ngx_command_t ngx_core_commands[] = {
|
|
32
|
|
33 { ngx_string("daemon"),
|
112
|
34 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
|
0
|
35 ngx_conf_set_flag_slot,
|
|
36 0,
|
|
37 offsetof(ngx_core_conf_t, daemon),
|
|
38 NULL },
|
|
39
|
|
40 { ngx_string("master_process"),
|
112
|
41 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
|
0
|
42 ngx_conf_set_flag_slot,
|
|
43 0,
|
|
44 offsetof(ngx_core_conf_t, master),
|
|
45 NULL },
|
|
46
|
112
|
47 { ngx_string("timer_resolution"),
|
|
48 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
49 ngx_conf_set_msec_slot,
|
|
50 0,
|
|
51 offsetof(ngx_core_conf_t, timer_resolution),
|
|
52 NULL },
|
|
53
|
92
|
54 { ngx_string("pid"),
|
|
55 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
56 ngx_conf_set_str_slot,
|
|
57 0,
|
|
58 offsetof(ngx_core_conf_t, pid),
|
|
59 NULL },
|
|
60
|
160
|
61 { ngx_string("lock_file"),
|
|
62 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
63 ngx_conf_set_str_slot,
|
|
64 0,
|
|
65 offsetof(ngx_core_conf_t, lock_file),
|
|
66 NULL },
|
|
67
|
0
|
68 { ngx_string("worker_processes"),
|
|
69 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
70 ngx_conf_set_num_slot,
|
|
71 0,
|
|
72 offsetof(ngx_core_conf_t, worker_processes),
|
|
73 NULL },
|
|
74
|
10
|
75 { ngx_string("debug_points"),
|
|
76 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
77 ngx_conf_set_enum_slot,
|
|
78 0,
|
|
79 offsetof(ngx_core_conf_t, debug_points),
|
|
80 &ngx_debug_points },
|
|
81
|
92
|
82 { ngx_string("user"),
|
|
83 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
|
|
84 ngx_set_user,
|
|
85 0,
|
|
86 0,
|
|
87 NULL },
|
|
88
|
|
89 { ngx_string("worker_priority"),
|
|
90 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
91 ngx_set_priority,
|
|
92 0,
|
|
93 0,
|
|
94 NULL },
|
|
95
|
126
|
96 { ngx_string("worker_cpu_affinity"),
|
|
97 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
|
|
98 ngx_set_cpu_affinity,
|
|
99 0,
|
|
100 0,
|
|
101 NULL },
|
|
102
|
92
|
103 { ngx_string("worker_rlimit_nofile"),
|
|
104 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
105 ngx_conf_set_num_slot,
|
|
106 0,
|
|
107 offsetof(ngx_core_conf_t, rlimit_nofile),
|
|
108 NULL },
|
|
109
|
138
|
110 { ngx_string("worker_rlimit_core"),
|
|
111 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
270
|
112 ngx_conf_set_size_slot,
|
138
|
113 0,
|
|
114 offsetof(ngx_core_conf_t, rlimit_core),
|
|
115 NULL },
|
|
116
|
92
|
117 { ngx_string("worker_rlimit_sigpending"),
|
|
118 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
119 ngx_conf_set_num_slot,
|
|
120 0,
|
|
121 offsetof(ngx_core_conf_t, rlimit_sigpending),
|
|
122 NULL },
|
|
123
|
|
124 { ngx_string("working_directory"),
|
|
125 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
126 ngx_conf_set_str_slot,
|
|
127 0,
|
|
128 offsetof(ngx_core_conf_t, working_directory),
|
|
129 NULL },
|
|
130
|
278
|
131 { ngx_string("env"),
|
|
132 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
133 ngx_set_env,
|
|
134 0,
|
|
135 0,
|
|
136 NULL },
|
|
137
|
0
|
138 #if (NGX_THREADS)
|
|
139
|
|
140 { ngx_string("worker_threads"),
|
|
141 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
142 ngx_conf_set_num_slot,
|
|
143 0,
|
|
144 offsetof(ngx_core_conf_t, worker_threads),
|
|
145 NULL },
|
|
146
|
|
147 { ngx_string("thread_stack_size"),
|
|
148 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
149 ngx_conf_set_size_slot,
|
|
150 0,
|
|
151 offsetof(ngx_core_conf_t, thread_stack_size),
|
|
152 NULL },
|
|
153
|
|
154 #endif
|
|
155
|
|
156 ngx_null_command
|
|
157 };
|
|
158
|
|
159
|
|
160 static ngx_core_module_t ngx_core_module_ctx = {
|
|
161 ngx_string("core"),
|
|
162 ngx_core_module_create_conf,
|
|
163 ngx_core_module_init_conf
|
|
164 };
|
|
165
|
|
166
|
|
167 ngx_module_t ngx_core_module = {
|
58
|
168 NGX_MODULE_V1,
|
0
|
169 &ngx_core_module_ctx, /* module context */
|
|
170 ngx_core_commands, /* module directives */
|
|
171 NGX_CORE_MODULE, /* module type */
|
90
|
172 NULL, /* init master */
|
0
|
173 NULL, /* init module */
|
90
|
174 NULL, /* init process */
|
|
175 NULL, /* init thread */
|
|
176 NULL, /* exit thread */
|
|
177 NULL, /* exit process */
|
|
178 NULL, /* exit master */
|
|
179 NGX_MODULE_V1_PADDING
|
0
|
180 };
|
|
181
|
|
182
|
480
|
183 ngx_uint_t ngx_max_module;
|
0
|
184
|
480
|
185 static ngx_uint_t ngx_show_version;
|
|
186 static ngx_uint_t ngx_show_configure;
|
|
187 #if (NGX_WIN32)
|
|
188 static char *ngx_signal;
|
|
189 #endif
|
|
190
|
188
|
191
|
278
|
192 static char **ngx_os_environ;
|
150
|
193
|
0
|
194
|
52
|
195 int ngx_cdecl
|
48
|
196 main(int argc, char *const *argv)
|
0
|
197 {
|
2
|
198 ngx_int_t i;
|
|
199 ngx_log_t *log;
|
|
200 ngx_cycle_t *cycle, init_cycle;
|
|
201 ngx_core_conf_t *ccf;
|
0
|
202
|
4
|
203 #if (NGX_FREEBSD)
|
0
|
204 ngx_debug_init();
|
|
205 #endif
|
|
206
|
|
207 /* TODO */ ngx_max_sockets = -1;
|
|
208
|
|
209 ngx_time_init();
|
|
210
|
4
|
211 #if (NGX_PCRE)
|
0
|
212 ngx_regex_init();
|
|
213 #endif
|
|
214
|
|
215 ngx_pid = ngx_getpid();
|
|
216
|
50
|
217 log = ngx_log_init();
|
|
218 if (log == NULL) {
|
0
|
219 return 1;
|
|
220 }
|
|
221
|
90
|
222 /* STUB */
|
0
|
223 #if (NGX_OPENSSL)
|
|
224 ngx_ssl_init(log);
|
|
225 #endif
|
|
226
|
|
227 /* init_cycle->log is required for signal handlers and ngx_getopt() */
|
|
228
|
|
229 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
|
|
230 init_cycle.log = log;
|
|
231 ngx_cycle = &init_cycle;
|
|
232
|
50
|
233 init_cycle.pool = ngx_create_pool(1024, log);
|
|
234 if (init_cycle.pool == NULL) {
|
0
|
235 return 1;
|
|
236 }
|
|
237
|
160
|
238 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
|
2
|
239 return 1;
|
|
240 }
|
|
241
|
160
|
242 if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) {
|
0
|
243 return 1;
|
|
244 }
|
|
245
|
188
|
246 if (ngx_show_version) {
|
418
|
247
|
480
|
248 ngx_log_stderr("nginx version: " NGINX_VER);
|
188
|
249
|
270
|
250 if (ngx_show_configure) {
|
188
|
251 #ifdef NGX_COMPILER
|
480
|
252 ngx_log_stderr("built by " NGX_COMPILER);
|
188
|
253 #endif
|
270
|
254
|
480
|
255 ngx_log_stderr("configure arguments: " NGX_CONFIGURE);
|
270
|
256 }
|
|
257
|
190
|
258 if (!ngx_test_config) {
|
|
259 return 0;
|
|
260 }
|
188
|
261 }
|
|
262
|
0
|
263 if (ngx_test_config) {
|
|
264 log->log_level = NGX_LOG_INFO;
|
|
265 }
|
|
266
|
160
|
267 if (ngx_os_init(log) != NGX_OK) {
|
0
|
268 return 1;
|
|
269 }
|
|
270
|
356
|
271 /*
|
|
272 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
|
|
273 */
|
270
|
274
|
356
|
275 if (ngx_crc32_table_init() != NGX_OK) {
|
270
|
276 return 1;
|
|
277 }
|
|
278
|
160
|
279 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
|
0
|
280 return 1;
|
|
281 }
|
|
282
|
|
283 ngx_max_module = 0;
|
|
284 for (i = 0; ngx_modules[i]; i++) {
|
|
285 ngx_modules[i]->index = ngx_max_module++;
|
|
286 }
|
|
287
|
|
288 cycle = ngx_init_cycle(&init_cycle);
|
|
289 if (cycle == NULL) {
|
|
290 if (ngx_test_config) {
|
480
|
291 ngx_log_stderr("the configuration file %s test failed",
|
|
292 init_cycle.conf_file.data);
|
0
|
293 }
|
|
294
|
|
295 return 1;
|
|
296 }
|
|
297
|
|
298 if (ngx_test_config) {
|
480
|
299 ngx_log_stderr("the configuration file %s was tested successfully",
|
|
300 cycle->conf_file.data);
|
0
|
301 return 0;
|
|
302 }
|
|
303
|
|
304 ngx_os_status(cycle->log);
|
|
305
|
|
306 ngx_cycle = cycle;
|
|
307
|
|
308 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
309
|
480
|
310 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
|
|
311 ngx_process = NGX_PROCESS_MASTER;
|
|
312 }
|
0
|
313
|
10
|
314 #if (NGX_WIN32)
|
0
|
315
|
480
|
316 if (ngx_signal) {
|
|
317 return ngx_signal_process(cycle, ngx_signal);
|
0
|
318 }
|
|
319
|
|
320 #else
|
|
321
|
160
|
322 if (ngx_init_signals(cycle->log) != NGX_OK) {
|
88
|
323 return 1;
|
|
324 }
|
|
325
|
0
|
326 if (!ngx_inherited && ccf->daemon) {
|
160
|
327 if (ngx_daemon(cycle->log) != NGX_OK) {
|
0
|
328 return 1;
|
|
329 }
|
|
330
|
|
331 ngx_daemonized = 1;
|
|
332 }
|
|
333
|
480
|
334 #endif
|
|
335
|
160
|
336 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
|
0
|
337 return 1;
|
|
338 }
|
|
339
|
480
|
340 if (ngx_process == NGX_PROCESS_SINGLE) {
|
|
341 ngx_single_process_cycle(cycle);
|
0
|
342
|
|
343 } else {
|
480
|
344 ngx_master_process_cycle(cycle);
|
0
|
345 }
|
|
346
|
|
347 return 0;
|
|
348 }
|
|
349
|
|
350
|
48
|
351 static ngx_int_t
|
|
352 ngx_add_inherited_sockets(ngx_cycle_t *cycle)
|
0
|
353 {
|
50
|
354 u_char *p, *v, *inherited;
|
|
355 ngx_int_t s;
|
|
356 ngx_listening_t *ls;
|
0
|
357
|
|
358 inherited = (u_char *) getenv(NGINX_VAR);
|
|
359
|
|
360 if (inherited == NULL) {
|
|
361 return NGX_OK;
|
|
362 }
|
|
363
|
10
|
364 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
0
|
365 "using inherited sockets from \"%s\"", inherited);
|
|
366
|
10
|
367 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
|
242
|
368 sizeof(ngx_listening_t))
|
454
|
369 != NGX_OK)
|
10
|
370 {
|
|
371 return NGX_ERROR;
|
|
372 }
|
0
|
373
|
|
374 for (p = inherited, v = p; *p; p++) {
|
|
375 if (*p == ':' || *p == ';') {
|
|
376 s = ngx_atoi(v, p - v);
|
|
377 if (s == NGX_ERROR) {
|
|
378 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
86
|
379 "invalid socket number \"%s\" in " NGINX_VAR
|
|
380 " environment variable, ignoring the rest"
|
|
381 " of the variable", v);
|
0
|
382 break;
|
|
383 }
|
|
384
|
|
385 v = p + 1;
|
|
386
|
50
|
387 ls = ngx_array_push(&cycle->listening);
|
|
388 if (ls == NULL) {
|
0
|
389 return NGX_ERROR;
|
|
390 }
|
|
391
|
72
|
392 ngx_memzero(ls, sizeof(ngx_listening_t));
|
|
393
|
50
|
394 ls->fd = (ngx_socket_t) s;
|
0
|
395 }
|
|
396 }
|
|
397
|
|
398 ngx_inherited = 1;
|
|
399
|
|
400 return ngx_set_inherited_sockets(cycle);
|
|
401 }
|
|
402
|
|
403
|
278
|
404 char **
|
|
405 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
|
|
406 {
|
|
407 char **p, **env;
|
|
408 ngx_str_t *var;
|
|
409 ngx_uint_t i, n;
|
|
410 ngx_core_conf_t *ccf;
|
|
411
|
|
412 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
413
|
280
|
414 if (last == NULL && ccf->environment) {
|
|
415 return ccf->environment;
|
278
|
416 }
|
|
417
|
|
418 var = ccf->env.elts;
|
|
419
|
|
420 for (i = 0; i < ccf->env.nelts; i++) {
|
|
421 if (ngx_strcmp(var[i].data, "TZ") == 0
|
|
422 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
|
|
423 {
|
|
424 goto tz_found;
|
|
425 }
|
|
426 }
|
|
427
|
|
428 var = ngx_array_push(&ccf->env);
|
322
|
429 if (var == NULL) {
|
|
430 return NULL;
|
|
431 }
|
278
|
432
|
|
433 var->len = 2;
|
|
434 var->data = (u_char *) "TZ";
|
|
435
|
|
436 var = ccf->env.elts;
|
|
437
|
|
438 tz_found:
|
|
439
|
280
|
440 n = 0;
|
|
441
|
278
|
442 for (i = 0; i < ccf->env.nelts; i++) {
|
|
443
|
|
444 if (var[i].data[var[i].len] == '=') {
|
|
445 n++;
|
|
446 continue;
|
|
447 }
|
|
448
|
|
449 for (p = ngx_os_environ; *p; p++) {
|
|
450
|
|
451 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
|
|
452 && (*p)[var[i].len] == '=')
|
|
453 {
|
|
454 n++;
|
|
455 break;
|
|
456 }
|
|
457 }
|
|
458 }
|
|
459
|
|
460 if (last) {
|
280
|
461 env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle->log);
|
278
|
462 *last = n;
|
|
463
|
|
464 } else {
|
|
465 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
|
|
466 }
|
|
467
|
|
468 if (env == NULL) {
|
|
469 return NULL;
|
|
470 }
|
|
471
|
|
472 n = 0;
|
|
473
|
|
474 for (i = 0; i < ccf->env.nelts; i++) {
|
|
475
|
|
476 if (var[i].data[var[i].len] == '=') {
|
|
477 env[n++] = (char *) var[i].data;
|
|
478 continue;
|
|
479 }
|
|
480
|
|
481 for (p = ngx_os_environ; *p; p++) {
|
|
482
|
|
483 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
|
|
484 && (*p)[var[i].len] == '=')
|
|
485 {
|
|
486 env[n++] = *p;
|
|
487 break;
|
|
488 }
|
|
489 }
|
|
490 }
|
|
491
|
|
492 env[n] = NULL;
|
|
493
|
|
494 if (last == NULL) {
|
|
495 ccf->environment = env;
|
|
496 environ = env;
|
|
497 }
|
|
498
|
|
499 return env;
|
|
500 }
|
|
501
|
|
502
|
92
|
503 ngx_pid_t
|
|
504 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
|
0
|
505 {
|
278
|
506 char **env, *var;
|
|
507 u_char *p;
|
|
508 ngx_uint_t i, n;
|
|
509 ngx_pid_t pid;
|
|
510 ngx_exec_ctx_t ctx;
|
|
511 ngx_core_conf_t *ccf;
|
|
512 ngx_listening_t *ls;
|
0
|
513
|
|
514 ctx.path = argv[0];
|
|
515 ctx.name = "new binary process";
|
|
516 ctx.argv = argv;
|
|
517
|
278
|
518 n = 2;
|
|
519 env = ngx_set_environment(cycle, &n);
|
|
520 if (env == NULL) {
|
|
521 return NGX_INVALID_PID;
|
|
522 }
|
|
523
|
0
|
524 var = ngx_alloc(sizeof(NGINX_VAR)
|
50
|
525 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
|
0
|
526 cycle->log);
|
|
527
|
10
|
528 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
|
0
|
529
|
|
530 ls = cycle->listening.elts;
|
|
531 for (i = 0; i < cycle->listening.nelts; i++) {
|
10
|
532 p = ngx_sprintf(p, "%ud;", ls[i].fd);
|
0
|
533 }
|
|
534
|
10
|
535 *p = '\0';
|
|
536
|
278
|
537 env[n++] = var;
|
2
|
538
|
|
539 #if (NGX_SETPROCTITLE_USES_ENV)
|
|
540
|
10
|
541 /* allocate the spare 300 bytes for the new binary process title */
|
2
|
542
|
278
|
543 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
544 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
545 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
546 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
547 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
|
2
|
548
|
|
549 #endif
|
|
550
|
278
|
551 env[n] = NULL;
|
|
552
|
|
553 #if (NGX_DEBUG)
|
|
554 {
|
|
555 char **e;
|
|
556 for (e = env; *e; e++) {
|
|
557 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
|
|
558 }
|
|
559 }
|
|
560 #endif
|
|
561
|
|
562 ctx.envp = (char *const *) env;
|
0
|
563
|
92
|
564 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
565
|
278
|
566 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
|
92
|
567 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
568 ngx_rename_file_n " %s to %s failed "
|
|
569 "before executing new binary process \"%s\"",
|
|
570 ccf->pid.data, ccf->oldpid.data, argv[0]);
|
|
571
|
278
|
572 ngx_free(env);
|
92
|
573 ngx_free(var);
|
|
574
|
|
575 return NGX_INVALID_PID;
|
|
576 }
|
|
577
|
0
|
578 pid = ngx_execute(cycle, &ctx);
|
|
579
|
92
|
580 if (pid == NGX_INVALID_PID) {
|
278
|
581 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
|
92
|
582 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
278
|
583 ngx_rename_file_n " %s back to %s failed after "
|
|
584 "the try to execute the new binary process \"%s\"",
|
92
|
585 ccf->oldpid.data, ccf->pid.data, argv[0]);
|
|
586 }
|
|
587 }
|
|
588
|
278
|
589 ngx_free(env);
|
0
|
590 ngx_free(var);
|
|
591
|
|
592 return pid;
|
|
593 }
|
|
594
|
|
595
|
92
|
596 static ngx_int_t
|
|
597 ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
|
0
|
598 {
|
|
599 ngx_int_t i;
|
|
600
|
2
|
601 for (i = 1; i < argc; i++) {
|
|
602 if (argv[i][0] != '-') {
|
0
|
603 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
2
|
604 "invalid option: \"%s\"", argv[i]);
|
0
|
605 return NGX_ERROR;
|
|
606 }
|
|
607
|
2
|
608 switch (argv[i][1]) {
|
0
|
609
|
188
|
610 case 'v':
|
|
611 ngx_show_version = 1;
|
|
612 break;
|
|
613
|
270
|
614 case 'V':
|
|
615 ngx_show_version = 1;
|
|
616 ngx_show_configure = 1;
|
|
617 break;
|
|
618
|
0
|
619 case 't':
|
|
620 ngx_test_config = 1;
|
|
621 break;
|
|
622
|
|
623 case 'c':
|
2
|
624 if (argv[i + 1] == NULL) {
|
0
|
625 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
384
|
626 "the option \"-c\" requires file name");
|
0
|
627 return NGX_ERROR;
|
|
628 }
|
|
629
|
2
|
630 cycle->conf_file.data = (u_char *) argv[++i];
|
0
|
631 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
|
|
632 break;
|
|
633
|
384
|
634 case 'g':
|
|
635 if (argv[i + 1] == NULL) {
|
|
636 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
637 "the option \"-g\" requires parameter");
|
|
638 return NGX_ERROR;
|
|
639 }
|
|
640
|
|
641 cycle->conf_param.data = (u_char *) argv[++i];
|
|
642 cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);
|
|
643 break;
|
|
644
|
480
|
645 #if (NGX_WIN32)
|
|
646 case 's':
|
|
647 if (argv[++i] == NULL) {
|
|
648 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
649 "the option \"-s\" requires parameter");
|
|
650 return NGX_ERROR;
|
|
651 }
|
|
652
|
|
653 if (ngx_strcmp(argv[i], "stop") == 0
|
|
654 || ngx_strcmp(argv[i], "quit") == 0
|
|
655 || ngx_strcmp(argv[i], "reopen") == 0
|
|
656 || ngx_strcmp(argv[i], "reload") == 0)
|
|
657 {
|
|
658 ngx_process = NGX_PROCESS_SIGNALLER;
|
|
659 ngx_signal = argv[i];
|
|
660 break;
|
|
661 }
|
|
662
|
|
663 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
664 "invalid option: \"-s %s\"", argv[i]);
|
|
665 return NGX_ERROR;
|
|
666 #endif
|
|
667
|
0
|
668 default:
|
|
669 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
2
|
670 "invalid option: \"%s\"", argv[i]);
|
0
|
671 return NGX_ERROR;
|
|
672 }
|
|
673 }
|
|
674
|
|
675 if (cycle->conf_file.data == NULL) {
|
|
676 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
|
|
677 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
|
|
678 }
|
|
679
|
|
680 return NGX_OK;
|
|
681 }
|
|
682
|
|
683
|
48
|
684 static ngx_int_t
|
|
685 ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
|
2
|
686 {
|
50
|
687 #if (NGX_FREEBSD)
|
|
688
|
|
689 ngx_os_argv = (char **) argv;
|
|
690 ngx_argc = argc;
|
|
691 ngx_argv = (char **) argv;
|
|
692
|
|
693 #else
|
2
|
694 size_t len;
|
|
695 ngx_int_t i;
|
|
696
|
|
697 ngx_os_argv = (char **) argv;
|
|
698 ngx_argc = argc;
|
|
699
|
50
|
700 ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log);
|
|
701 if (ngx_argv == NULL) {
|
2
|
702 return NGX_ERROR;
|
|
703 }
|
|
704
|
|
705 for (i = 0; i < argc; i++) {
|
|
706 len = ngx_strlen(argv[i]) + 1;
|
|
707
|
50
|
708 ngx_argv[i] = ngx_alloc(len, cycle->log);
|
|
709 if (ngx_argv[i] == NULL) {
|
2
|
710 return NGX_ERROR;
|
|
711 }
|
|
712
|
66
|
713 (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
|
2
|
714 }
|
|
715
|
|
716 ngx_argv[i] = NULL;
|
|
717
|
|
718 #endif
|
|
719
|
278
|
720 ngx_os_environ = environ;
|
|
721
|
2
|
722 return NGX_OK;
|
|
723 }
|
|
724
|
|
725
|
48
|
726 static void *
|
|
727 ngx_core_module_create_conf(ngx_cycle_t *cycle)
|
0
|
728 {
|
|
729 ngx_core_conf_t *ccf;
|
|
730
|
50
|
731 ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));
|
|
732 if (ccf == NULL) {
|
0
|
733 return NULL;
|
|
734 }
|
28
|
735
|
|
736 /*
|
|
737 * set by pcalloc()
|
0
|
738 *
|
28
|
739 * ccf->pid = NULL;
|
92
|
740 * ccf->oldpid = NULL;
|
28
|
741 * ccf->priority = 0;
|
126
|
742 * ccf->cpu_affinity_n = 0;
|
|
743 * ccf->cpu_affinity = NULL;
|
0
|
744 */
|
28
|
745
|
0
|
746 ccf->daemon = NGX_CONF_UNSET;
|
|
747 ccf->master = NGX_CONF_UNSET;
|
112
|
748 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
|
|
749
|
0
|
750 ccf->worker_processes = NGX_CONF_UNSET;
|
10
|
751 ccf->debug_points = NGX_CONF_UNSET;
|
92
|
752
|
|
753 ccf->rlimit_nofile = NGX_CONF_UNSET;
|
270
|
754 ccf->rlimit_core = NGX_CONF_UNSET_SIZE;
|
92
|
755 ccf->rlimit_sigpending = NGX_CONF_UNSET;
|
|
756
|
66
|
757 ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
|
|
758 ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
|
92
|
759
|
0
|
760 #if (NGX_THREADS)
|
|
761 ccf->worker_threads = NGX_CONF_UNSET;
|
10
|
762 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
|
0
|
763 #endif
|
|
764
|
278
|
765 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
|
|
766 != NGX_OK)
|
|
767 {
|
|
768 return NULL;
|
|
769 }
|
|
770
|
0
|
771 return ccf;
|
|
772 }
|
|
773
|
|
774
|
48
|
775 static char *
|
|
776 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
777 {
|
|
778 ngx_core_conf_t *ccf = conf;
|
|
779
|
|
780 ngx_conf_init_value(ccf->daemon, 1);
|
|
781 ngx_conf_init_value(ccf->master, 1);
|
112
|
782 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
|
|
783
|
0
|
784 ngx_conf_init_value(ccf->worker_processes, 1);
|
10
|
785 ngx_conf_init_value(ccf->debug_points, 0);
|
0
|
786
|
126
|
787 #if (NGX_HAVE_SCHED_SETAFFINITY)
|
|
788
|
|
789 if (ccf->cpu_affinity_n
|
|
790 && ccf->cpu_affinity_n != 1
|
|
791 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
|
|
792 {
|
|
793 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
|
|
794 "number of the \"worker_processes\" is not equal to "
|
|
795 "the number of the \"worker_cpu_affinity\" mask, "
|
|
796 "using last mask for remaining worker processes");
|
|
797 }
|
|
798
|
|
799 #endif
|
|
800
|
0
|
801 #if (NGX_THREADS)
|
126
|
802
|
0
|
803 ngx_conf_init_value(ccf->worker_threads, 0);
|
|
804 ngx_threads_n = ccf->worker_threads;
|
|
805 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
|
126
|
806
|
0
|
807 #endif
|
|
808
|
480
|
809
|
|
810 if (ccf->pid.len == 0) {
|
|
811 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
|
|
812 ccf->pid.data = (u_char *) NGX_PID_PATH;
|
|
813 }
|
|
814
|
|
815 if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
|
|
816 return NGX_CONF_ERROR;
|
|
817 }
|
|
818
|
|
819 ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
|
|
820
|
|
821 ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
|
|
822 if (ccf->oldpid.data == NULL) {
|
|
823 return NGX_CONF_ERROR;
|
|
824 }
|
|
825
|
|
826 ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
|
|
827 NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
|
|
828
|
|
829
|
10
|
830 #if !(NGX_WIN32)
|
0
|
831
|
66
|
832 if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
|
356
|
833 struct group *grp;
|
|
834 struct passwd *pwd;
|
0
|
835
|
318
|
836 ngx_set_errno(0);
|
4
|
837 pwd = getpwnam(NGX_USER);
|
0
|
838 if (pwd == NULL) {
|
|
839 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
4
|
840 "getpwnam(\"" NGX_USER "\") failed");
|
0
|
841 return NGX_CONF_ERROR;
|
|
842 }
|
|
843
|
22
|
844 ccf->username = NGX_USER;
|
0
|
845 ccf->user = pwd->pw_uid;
|
|
846
|
318
|
847 ngx_set_errno(0);
|
4
|
848 grp = getgrnam(NGX_GROUP);
|
0
|
849 if (grp == NULL) {
|
|
850 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
4
|
851 "getgrnam(\"" NGX_GROUP "\") failed");
|
0
|
852 return NGX_CONF_ERROR;
|
|
853 }
|
|
854
|
|
855 ccf->group = grp->gr_gid;
|
|
856 }
|
|
857
|
258
|
858
|
160
|
859 if (ccf->lock_file.len == 0) {
|
|
860 ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
|
|
861 ccf->lock_file.data = (u_char *) NGX_LOCK_PATH;
|
|
862 }
|
|
863
|
454
|
864 if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) != NGX_OK) {
|
160
|
865 return NGX_CONF_ERROR;
|
|
866 }
|
|
867
|
356
|
868 {
|
|
869 ngx_str_t lock_file;
|
|
870
|
258
|
871 lock_file = cycle->old_cycle->lock_file;
|
|
872
|
|
873 if (lock_file.len) {
|
|
874 lock_file.len--;
|
|
875
|
|
876 if (ccf->lock_file.len != lock_file.len
|
|
877 || ngx_strncmp(ccf->lock_file.data, lock_file.data, lock_file.len)
|
|
878 != 0)
|
|
879 {
|
|
880 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
881 "\"lock_file\" could not be changed, ignored");
|
|
882 }
|
|
883
|
|
884 cycle->lock_file.len = lock_file.len + 1;
|
|
885 lock_file.len += sizeof(".accept");
|
|
886
|
|
887 cycle->lock_file.data = ngx_pstrdup(cycle->pool, &lock_file);
|
|
888 if (cycle->lock_file.data == NULL) {
|
|
889 return NGX_CONF_ERROR;
|
|
890 }
|
|
891
|
|
892 } else {
|
|
893 cycle->lock_file.len = ccf->lock_file.len + 1;
|
382
|
894 cycle->lock_file.data = ngx_pnalloc(cycle->pool,
|
258
|
895 ccf->lock_file.len + sizeof(".accept"));
|
|
896 if (cycle->lock_file.data == NULL) {
|
|
897 return NGX_CONF_ERROR;
|
|
898 }
|
|
899
|
|
900 ngx_memcpy(ngx_cpymem(cycle->lock_file.data, ccf->lock_file.data,
|
|
901 ccf->lock_file.len),
|
|
902 ".accept", sizeof(".accept"));
|
|
903 }
|
356
|
904 }
|
258
|
905
|
0
|
906 #endif
|
|
907
|
|
908 return NGX_CONF_OK;
|
|
909 }
|
|
910
|
|
911
|
48
|
912 static char *
|
|
913 ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
914 {
|
10
|
915 #if (NGX_WIN32)
|
0
|
916
|
|
917 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
918 "\"user\" is not supported, ignored");
|
|
919
|
|
920 return NGX_CONF_OK;
|
|
921
|
|
922 #else
|
|
923
|
|
924 ngx_core_conf_t *ccf = conf;
|
|
925
|
4
|
926 char *group;
|
0
|
927 struct passwd *pwd;
|
|
928 struct group *grp;
|
|
929 ngx_str_t *value;
|
|
930
|
66
|
931 if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
|
0
|
932 return "is duplicate";
|
|
933 }
|
|
934
|
4
|
935 if (geteuid() != 0) {
|
|
936 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
937 "the \"user\" directive makes sense only "
|
|
938 "if the master process runs "
|
|
939 "with super-user privileges, ignored");
|
|
940 return NGX_CONF_OK;
|
|
941 }
|
|
942
|
0
|
943 value = (ngx_str_t *) cf->args->elts;
|
|
944
|
22
|
945 ccf->username = (char *) value[1].data;
|
|
946
|
318
|
947 ngx_set_errno(0);
|
0
|
948 pwd = getpwnam((const char *) value[1].data);
|
|
949 if (pwd == NULL) {
|
|
950 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
2
|
951 "getpwnam(\"%s\") failed", value[1].data);
|
0
|
952 return NGX_CONF_ERROR;
|
|
953 }
|
|
954
|
|
955 ccf->user = pwd->pw_uid;
|
|
956
|
4
|
957 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
|
0
|
958
|
318
|
959 ngx_set_errno(0);
|
4
|
960 grp = getgrnam(group);
|
0
|
961 if (grp == NULL) {
|
|
962 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
4
|
963 "getgrnam(\"%s\") failed", group);
|
0
|
964 return NGX_CONF_ERROR;
|
|
965 }
|
|
966
|
|
967 ccf->group = grp->gr_gid;
|
|
968
|
|
969 return NGX_CONF_OK;
|
|
970
|
|
971 #endif
|
|
972 }
|
22
|
973
|
|
974
|
48
|
975 static char *
|
278
|
976 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
977 {
|
|
978 ngx_core_conf_t *ccf = conf;
|
|
979
|
|
980 ngx_str_t *value, *var;
|
|
981 ngx_uint_t i;
|
|
982
|
|
983 var = ngx_array_push(&ccf->env);
|
|
984 if (var == NULL) {
|
|
985 return NGX_CONF_ERROR;
|
|
986 }
|
|
987
|
|
988 value = cf->args->elts;
|
|
989 *var = value[1];
|
|
990
|
|
991 for (i = 0; i < value[1].len; i++) {
|
|
992
|
|
993 if (value[1].data[i] == '=') {
|
|
994
|
|
995 var->len = i;
|
|
996
|
|
997 return NGX_CONF_OK;
|
|
998 }
|
|
999 }
|
|
1000
|
|
1001 return NGX_CONF_OK;
|
|
1002 }
|
|
1003
|
|
1004
|
|
1005 static char *
|
48
|
1006 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
22
|
1007 {
|
|
1008 ngx_core_conf_t *ccf = conf;
|
|
1009
|
|
1010 ngx_str_t *value;
|
|
1011 ngx_uint_t n, minus;
|
|
1012
|
|
1013 if (ccf->priority != 0) {
|
|
1014 return "is duplicate";
|
|
1015 }
|
|
1016
|
|
1017 value = cf->args->elts;
|
|
1018
|
|
1019 if (value[1].data[0] == '-') {
|
|
1020 n = 1;
|
|
1021 minus = 1;
|
|
1022
|
|
1023 } else if (value[1].data[0] == '+') {
|
|
1024 n = 1;
|
|
1025 minus = 0;
|
|
1026
|
|
1027 } else {
|
|
1028 n = 0;
|
|
1029 minus = 0;
|
|
1030 }
|
|
1031
|
|
1032 ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n);
|
|
1033 if (ccf->priority == NGX_ERROR) {
|
|
1034 return "invalid number";
|
|
1035 }
|
|
1036
|
|
1037 if (minus) {
|
|
1038 ccf->priority = -ccf->priority;
|
|
1039 }
|
|
1040
|
|
1041 return NGX_CONF_OK;
|
|
1042 }
|
126
|
1043
|
|
1044
|
|
1045 static char *
|
|
1046 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1047 {
|
|
1048 #if (NGX_HAVE_SCHED_SETAFFINITY)
|
|
1049 ngx_core_conf_t *ccf = conf;
|
|
1050
|
|
1051 u_char ch;
|
|
1052 u_long *mask;
|
|
1053 ngx_str_t *value;
|
|
1054 ngx_uint_t i, n;
|
|
1055
|
|
1056 if (ccf->cpu_affinity) {
|
|
1057 return "is duplicate";
|
|
1058 }
|
|
1059
|
|
1060 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
|
|
1061 if (mask == NULL) {
|
|
1062 return NGX_CONF_ERROR;
|
|
1063 }
|
|
1064
|
|
1065 ccf->cpu_affinity_n = cf->args->nelts - 1;
|
|
1066 ccf->cpu_affinity = mask;
|
|
1067
|
|
1068 value = cf->args->elts;
|
|
1069
|
|
1070 for (n = 1; n < cf->args->nelts; n++) {
|
|
1071
|
|
1072 if (value[n].len > 32) {
|
|
1073 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1074 "\"worker_cpu_affinity\" supports up to 32 CPU only");
|
|
1075 return NGX_CONF_ERROR;
|
|
1076 }
|
|
1077
|
|
1078 mask[n - 1] = 0;
|
|
1079
|
|
1080 for (i = 0; i < value[n].len; i++) {
|
|
1081
|
|
1082 ch = value[n].data[i];
|
|
1083
|
|
1084 if (ch == ' ') {
|
|
1085 continue;
|
|
1086 }
|
|
1087
|
|
1088 mask[n - 1] <<= 1;
|
|
1089
|
|
1090 if (ch == '0') {
|
|
1091 continue;
|
|
1092 }
|
|
1093
|
|
1094 if (ch == '1') {
|
|
1095 mask[n - 1] |= 1;
|
|
1096 continue;
|
|
1097 }
|
|
1098
|
|
1099 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1100 "invalid character \"%c\" in \"worker_cpu_affinity\"",
|
|
1101 ch);
|
378
|
1102 return NGX_CONF_ERROR;
|
126
|
1103 }
|
|
1104 }
|
|
1105
|
|
1106 #else
|
|
1107
|
|
1108 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1109 "\"worker_cpu_affinity\" is not supported "
|
|
1110 "on this platform, ignored");
|
|
1111 #endif
|
|
1112
|
|
1113 return NGX_CONF_OK;
|
|
1114 }
|
|
1115
|
|
1116
|
|
1117 u_long
|
|
1118 ngx_get_cpu_affinity(ngx_uint_t n)
|
|
1119 {
|
|
1120 ngx_core_conf_t *ccf;
|
|
1121
|
|
1122 ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
|
|
1123 ngx_core_module);
|
|
1124
|
|
1125 if (ccf->cpu_affinity == NULL) {
|
|
1126 return 0;
|
|
1127 }
|
|
1128
|
|
1129 if (ccf->cpu_affinity_n > n) {
|
|
1130 return ccf->cpu_affinity[n];
|
|
1131 }
|
|
1132
|
|
1133 return ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
|
|
1134 }
|