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