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);
|
482
|
13 static ngx_int_t ngx_get_options(int argc, char *const *argv);
|
|
14 static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
|
2
|
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
|
480
|
184 ngx_uint_t ngx_max_module;
|
0
|
185
|
482
|
186 static ngx_uint_t ngx_show_help;
|
480
|
187 static ngx_uint_t ngx_show_version;
|
|
188 static ngx_uint_t ngx_show_configure;
|
482
|
189 static u_char *ngx_prefix;
|
|
190 static u_char *ngx_conf_file;
|
|
191 static u_char *ngx_conf_params;
|
480
|
192 static char *ngx_signal;
|
|
193
|
188
|
194
|
278
|
195 static char **ngx_os_environ;
|
150
|
196
|
0
|
197
|
52
|
198 int ngx_cdecl
|
48
|
199 main(int argc, char *const *argv)
|
0
|
200 {
|
2
|
201 ngx_int_t i;
|
|
202 ngx_log_t *log;
|
|
203 ngx_cycle_t *cycle, init_cycle;
|
|
204 ngx_core_conf_t *ccf;
|
0
|
205
|
482
|
206 if (ngx_get_options(argc, argv) != NGX_OK) {
|
|
207 return 1;
|
|
208 }
|
|
209
|
|
210 if (ngx_show_version) {
|
|
211 ngx_log_stderr(0, "nginx version: " NGINX_VER);
|
|
212
|
|
213 if (ngx_show_help) {
|
|
214 ngx_log_stderr(0,
|
|
215 "Usage: nginx [-?hvVt] [-s signal] [-c filename] "
|
484
|
216 "[-p prefix] [-g directives]" CRLF CRLF
|
482
|
217 "Options:" CRLF
|
|
218 " -?,-h : this help" CRLF
|
|
219 " -v : show version and exit" CRLF
|
|
220 " -V : show version and configure options then exit"
|
|
221 CRLF
|
|
222 " -t : test configuration and exit" CRLF
|
|
223 " -s signal : send signal to a master process: "
|
|
224 "stop, quit, reopen, reload" CRLF
|
|
225 #ifdef NGX_PREFIX
|
|
226 " -p prefix : set prefix path (default: "
|
|
227 NGX_PREFIX ")" CRLF
|
|
228 #else
|
|
229 " -p prefix : set prefix path (default: NONE)" CRLF
|
|
230 #endif
|
|
231 " -c filename : set configuration file (default: "
|
|
232 NGX_CONF_PATH ")" CRLF
|
|
233 " -g directives : set global directives out of configuration "
|
|
234 "file" CRLF
|
|
235 );
|
|
236 }
|
|
237
|
|
238 if (ngx_show_configure) {
|
|
239 #ifdef NGX_COMPILER
|
|
240 ngx_log_stderr(0, "built by " NGX_COMPILER);
|
|
241 #endif
|
502
|
242 #if (NGX_SSL)
|
|
243 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
|
244 ngx_log_stderr(0, "TLS SNI support enabled");
|
|
245 #else
|
|
246 ngx_log_stderr(0, "TLS SNI support disabled");
|
|
247 #endif
|
|
248 #endif
|
482
|
249 ngx_log_stderr(0, "configure arguments:" NGX_CONFIGURE);
|
|
250 }
|
|
251
|
|
252 if (!ngx_test_config) {
|
|
253 return 0;
|
|
254 }
|
|
255 }
|
|
256
|
4
|
257 #if (NGX_FREEBSD)
|
0
|
258 ngx_debug_init();
|
|
259 #endif
|
|
260
|
|
261 /* TODO */ ngx_max_sockets = -1;
|
|
262
|
|
263 ngx_time_init();
|
|
264
|
4
|
265 #if (NGX_PCRE)
|
0
|
266 ngx_regex_init();
|
|
267 #endif
|
|
268
|
|
269 ngx_pid = ngx_getpid();
|
|
270
|
482
|
271 log = ngx_log_init(ngx_prefix);
|
50
|
272 if (log == NULL) {
|
0
|
273 return 1;
|
|
274 }
|
|
275
|
90
|
276 /* STUB */
|
0
|
277 #if (NGX_OPENSSL)
|
|
278 ngx_ssl_init(log);
|
|
279 #endif
|
|
280
|
482
|
281 /*
|
|
282 * init_cycle->log is required for signal handlers and
|
|
283 * ngx_process_options()
|
|
284 */
|
0
|
285
|
|
286 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
|
|
287 init_cycle.log = log;
|
|
288 ngx_cycle = &init_cycle;
|
|
289
|
50
|
290 init_cycle.pool = ngx_create_pool(1024, log);
|
|
291 if (init_cycle.pool == NULL) {
|
0
|
292 return 1;
|
|
293 }
|
|
294
|
160
|
295 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
|
2
|
296 return 1;
|
|
297 }
|
|
298
|
482
|
299 if (ngx_process_options(&init_cycle) != NGX_OK) {
|
0
|
300 return 1;
|
|
301 }
|
|
302
|
160
|
303 if (ngx_os_init(log) != NGX_OK) {
|
0
|
304 return 1;
|
|
305 }
|
|
306
|
356
|
307 /*
|
|
308 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
|
|
309 */
|
270
|
310
|
356
|
311 if (ngx_crc32_table_init() != NGX_OK) {
|
270
|
312 return 1;
|
|
313 }
|
|
314
|
160
|
315 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
|
0
|
316 return 1;
|
|
317 }
|
|
318
|
|
319 ngx_max_module = 0;
|
|
320 for (i = 0; ngx_modules[i]; i++) {
|
|
321 ngx_modules[i]->index = ngx_max_module++;
|
|
322 }
|
|
323
|
|
324 cycle = ngx_init_cycle(&init_cycle);
|
|
325 if (cycle == NULL) {
|
|
326 if (ngx_test_config) {
|
482
|
327 ngx_log_stderr(0, "configuration file %s test failed",
|
480
|
328 init_cycle.conf_file.data);
|
0
|
329 }
|
|
330
|
|
331 return 1;
|
|
332 }
|
|
333
|
|
334 if (ngx_test_config) {
|
482
|
335 ngx_log_stderr(0, "configuration file %s test is successful",
|
480
|
336 cycle->conf_file.data);
|
0
|
337 return 0;
|
|
338 }
|
|
339
|
496
|
340 if (ngx_signal) {
|
|
341 return ngx_signal_process(cycle, ngx_signal);
|
|
342 }
|
|
343
|
0
|
344 ngx_os_status(cycle->log);
|
|
345
|
|
346 ngx_cycle = cycle;
|
|
347
|
|
348 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
349
|
480
|
350 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
|
|
351 ngx_process = NGX_PROCESS_MASTER;
|
|
352 }
|
0
|
353
|
482
|
354 #if !(NGX_WIN32)
|
0
|
355
|
160
|
356 if (ngx_init_signals(cycle->log) != NGX_OK) {
|
88
|
357 return 1;
|
|
358 }
|
|
359
|
0
|
360 if (!ngx_inherited && ccf->daemon) {
|
160
|
361 if (ngx_daemon(cycle->log) != NGX_OK) {
|
0
|
362 return 1;
|
|
363 }
|
|
364
|
|
365 ngx_daemonized = 1;
|
|
366 }
|
|
367
|
480
|
368 #endif
|
|
369
|
160
|
370 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
|
0
|
371 return 1;
|
|
372 }
|
|
373
|
482
|
374 if (cycle->log->file->fd != ngx_stderr) {
|
|
375
|
|
376 if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
|
|
377 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
378 ngx_set_stderr_n " failed");
|
|
379 return 1;
|
|
380 }
|
|
381 }
|
|
382
|
|
383 ngx_use_stderr = 0;
|
|
384
|
480
|
385 if (ngx_process == NGX_PROCESS_SINGLE) {
|
|
386 ngx_single_process_cycle(cycle);
|
0
|
387
|
|
388 } else {
|
480
|
389 ngx_master_process_cycle(cycle);
|
0
|
390 }
|
|
391
|
|
392 return 0;
|
|
393 }
|
|
394
|
|
395
|
48
|
396 static ngx_int_t
|
|
397 ngx_add_inherited_sockets(ngx_cycle_t *cycle)
|
0
|
398 {
|
50
|
399 u_char *p, *v, *inherited;
|
|
400 ngx_int_t s;
|
|
401 ngx_listening_t *ls;
|
0
|
402
|
|
403 inherited = (u_char *) getenv(NGINX_VAR);
|
|
404
|
|
405 if (inherited == NULL) {
|
|
406 return NGX_OK;
|
|
407 }
|
|
408
|
10
|
409 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
0
|
410 "using inherited sockets from \"%s\"", inherited);
|
|
411
|
10
|
412 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
|
242
|
413 sizeof(ngx_listening_t))
|
454
|
414 != NGX_OK)
|
10
|
415 {
|
|
416 return NGX_ERROR;
|
|
417 }
|
0
|
418
|
|
419 for (p = inherited, v = p; *p; p++) {
|
|
420 if (*p == ':' || *p == ';') {
|
|
421 s = ngx_atoi(v, p - v);
|
|
422 if (s == NGX_ERROR) {
|
|
423 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
86
|
424 "invalid socket number \"%s\" in " NGINX_VAR
|
|
425 " environment variable, ignoring the rest"
|
|
426 " of the variable", v);
|
0
|
427 break;
|
|
428 }
|
|
429
|
|
430 v = p + 1;
|
|
431
|
50
|
432 ls = ngx_array_push(&cycle->listening);
|
|
433 if (ls == NULL) {
|
0
|
434 return NGX_ERROR;
|
|
435 }
|
|
436
|
72
|
437 ngx_memzero(ls, sizeof(ngx_listening_t));
|
|
438
|
50
|
439 ls->fd = (ngx_socket_t) s;
|
0
|
440 }
|
|
441 }
|
|
442
|
|
443 ngx_inherited = 1;
|
|
444
|
|
445 return ngx_set_inherited_sockets(cycle);
|
|
446 }
|
|
447
|
|
448
|
278
|
449 char **
|
|
450 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
|
|
451 {
|
|
452 char **p, **env;
|
|
453 ngx_str_t *var;
|
|
454 ngx_uint_t i, n;
|
|
455 ngx_core_conf_t *ccf;
|
|
456
|
|
457 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
458
|
280
|
459 if (last == NULL && ccf->environment) {
|
|
460 return ccf->environment;
|
278
|
461 }
|
|
462
|
|
463 var = ccf->env.elts;
|
|
464
|
|
465 for (i = 0; i < ccf->env.nelts; i++) {
|
|
466 if (ngx_strcmp(var[i].data, "TZ") == 0
|
|
467 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
|
|
468 {
|
|
469 goto tz_found;
|
|
470 }
|
|
471 }
|
|
472
|
|
473 var = ngx_array_push(&ccf->env);
|
322
|
474 if (var == NULL) {
|
|
475 return NULL;
|
|
476 }
|
278
|
477
|
|
478 var->len = 2;
|
|
479 var->data = (u_char *) "TZ";
|
|
480
|
|
481 var = ccf->env.elts;
|
|
482
|
|
483 tz_found:
|
|
484
|
280
|
485 n = 0;
|
|
486
|
278
|
487 for (i = 0; i < ccf->env.nelts; i++) {
|
|
488
|
|
489 if (var[i].data[var[i].len] == '=') {
|
|
490 n++;
|
|
491 continue;
|
|
492 }
|
|
493
|
|
494 for (p = ngx_os_environ; *p; p++) {
|
|
495
|
|
496 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
|
|
497 && (*p)[var[i].len] == '=')
|
|
498 {
|
|
499 n++;
|
|
500 break;
|
|
501 }
|
|
502 }
|
|
503 }
|
|
504
|
|
505 if (last) {
|
280
|
506 env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle->log);
|
278
|
507 *last = n;
|
|
508
|
|
509 } else {
|
|
510 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
|
|
511 }
|
|
512
|
|
513 if (env == NULL) {
|
|
514 return NULL;
|
|
515 }
|
|
516
|
|
517 n = 0;
|
|
518
|
|
519 for (i = 0; i < ccf->env.nelts; i++) {
|
|
520
|
|
521 if (var[i].data[var[i].len] == '=') {
|
|
522 env[n++] = (char *) var[i].data;
|
|
523 continue;
|
|
524 }
|
|
525
|
|
526 for (p = ngx_os_environ; *p; p++) {
|
|
527
|
|
528 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
|
|
529 && (*p)[var[i].len] == '=')
|
|
530 {
|
|
531 env[n++] = *p;
|
|
532 break;
|
|
533 }
|
|
534 }
|
|
535 }
|
|
536
|
|
537 env[n] = NULL;
|
|
538
|
|
539 if (last == NULL) {
|
|
540 ccf->environment = env;
|
|
541 environ = env;
|
|
542 }
|
|
543
|
|
544 return env;
|
|
545 }
|
|
546
|
|
547
|
92
|
548 ngx_pid_t
|
|
549 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
|
0
|
550 {
|
278
|
551 char **env, *var;
|
|
552 u_char *p;
|
|
553 ngx_uint_t i, n;
|
|
554 ngx_pid_t pid;
|
|
555 ngx_exec_ctx_t ctx;
|
|
556 ngx_core_conf_t *ccf;
|
|
557 ngx_listening_t *ls;
|
0
|
558
|
482
|
559 ngx_memzero(&ctx, sizeof(ngx_exec_ctx_t));
|
|
560
|
0
|
561 ctx.path = argv[0];
|
|
562 ctx.name = "new binary process";
|
|
563 ctx.argv = argv;
|
|
564
|
278
|
565 n = 2;
|
|
566 env = ngx_set_environment(cycle, &n);
|
|
567 if (env == NULL) {
|
|
568 return NGX_INVALID_PID;
|
|
569 }
|
|
570
|
0
|
571 var = ngx_alloc(sizeof(NGINX_VAR)
|
50
|
572 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
|
0
|
573 cycle->log);
|
|
574
|
10
|
575 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
|
0
|
576
|
|
577 ls = cycle->listening.elts;
|
|
578 for (i = 0; i < cycle->listening.nelts; i++) {
|
10
|
579 p = ngx_sprintf(p, "%ud;", ls[i].fd);
|
0
|
580 }
|
|
581
|
10
|
582 *p = '\0';
|
|
583
|
278
|
584 env[n++] = var;
|
2
|
585
|
|
586 #if (NGX_SETPROCTITLE_USES_ENV)
|
|
587
|
10
|
588 /* allocate the spare 300 bytes for the new binary process title */
|
2
|
589
|
278
|
590 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
591 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
592 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
593 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
594 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
|
2
|
595
|
|
596 #endif
|
|
597
|
278
|
598 env[n] = NULL;
|
|
599
|
|
600 #if (NGX_DEBUG)
|
|
601 {
|
|
602 char **e;
|
|
603 for (e = env; *e; e++) {
|
|
604 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
|
|
605 }
|
|
606 }
|
|
607 #endif
|
|
608
|
|
609 ctx.envp = (char *const *) env;
|
0
|
610
|
92
|
611 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
612
|
278
|
613 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
|
92
|
614 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
615 ngx_rename_file_n " %s to %s failed "
|
|
616 "before executing new binary process \"%s\"",
|
|
617 ccf->pid.data, ccf->oldpid.data, argv[0]);
|
|
618
|
278
|
619 ngx_free(env);
|
92
|
620 ngx_free(var);
|
|
621
|
|
622 return NGX_INVALID_PID;
|
|
623 }
|
|
624
|
0
|
625 pid = ngx_execute(cycle, &ctx);
|
|
626
|
92
|
627 if (pid == NGX_INVALID_PID) {
|
278
|
628 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
|
92
|
629 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
278
|
630 ngx_rename_file_n " %s back to %s failed after "
|
|
631 "the try to execute the new binary process \"%s\"",
|
92
|
632 ccf->oldpid.data, ccf->pid.data, argv[0]);
|
|
633 }
|
|
634 }
|
|
635
|
278
|
636 ngx_free(env);
|
0
|
637 ngx_free(var);
|
|
638
|
|
639 return pid;
|
|
640 }
|
|
641
|
|
642
|
92
|
643 static ngx_int_t
|
482
|
644 ngx_get_options(int argc, char *const *argv)
|
0
|
645 {
|
482
|
646 u_char *p;
|
|
647 ngx_int_t i;
|
0
|
648
|
2
|
649 for (i = 1; i < argc; i++) {
|
482
|
650
|
|
651 p = (u_char *) argv[i];
|
|
652
|
|
653 if (*p++ != '-') {
|
|
654 ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
|
0
|
655 return NGX_ERROR;
|
|
656 }
|
|
657
|
482
|
658 while (*p) {
|
|
659
|
|
660 switch (*p++) {
|
0
|
661
|
482
|
662 case '?':
|
|
663 case 'h':
|
|
664 ngx_show_version = 1;
|
|
665 ngx_show_help = 1;
|
|
666 break;
|
|
667
|
|
668 case 'v':
|
|
669 ngx_show_version = 1;
|
|
670 break;
|
|
671
|
|
672 case 'V':
|
|
673 ngx_show_version = 1;
|
|
674 ngx_show_configure = 1;
|
|
675 break;
|
188
|
676
|
482
|
677 case 't':
|
|
678 ngx_test_config = 1;
|
|
679 break;
|
|
680
|
|
681 case 'p':
|
|
682 if (*p) {
|
|
683 ngx_prefix = p;
|
|
684 goto next;
|
|
685 }
|
270
|
686
|
482
|
687 if (argv[++i]) {
|
|
688 ngx_prefix = (u_char *) argv[i];
|
|
689 goto next;
|
|
690 }
|
|
691
|
|
692 ngx_log_stderr(0, "option \"-p\" requires directory name");
|
|
693 return NGX_ERROR;
|
|
694
|
|
695 case 'c':
|
|
696 if (*p) {
|
|
697 ngx_conf_file = p;
|
|
698 goto next;
|
|
699 }
|
0
|
700
|
482
|
701 if (argv[++i]) {
|
|
702 ngx_conf_file = (u_char *) argv[i];
|
|
703 goto next;
|
|
704 }
|
|
705
|
|
706 ngx_log_stderr(0, "option \"-c\" requires file name");
|
0
|
707 return NGX_ERROR;
|
482
|
708
|
|
709 case 'g':
|
|
710 if (*p) {
|
|
711 ngx_conf_params = p;
|
|
712 goto next;
|
|
713 }
|
|
714
|
|
715 if (argv[++i]) {
|
|
716 ngx_conf_params = (u_char *) argv[i];
|
|
717 goto next;
|
|
718 }
|
|
719
|
|
720 ngx_log_stderr(0, "option \"-g\" requires parameter");
|
|
721 return NGX_ERROR;
|
0
|
722
|
482
|
723 case 's':
|
|
724 if (*p) {
|
|
725 ngx_signal = (char *) p;
|
|
726
|
|
727 } else if (argv[++i]) {
|
|
728 ngx_signal = argv[i];
|
|
729
|
|
730 } else {
|
|
731 ngx_log_stderr(0, "option \"-s\" requires parameter");
|
|
732 return NGX_ERROR;
|
|
733 }
|
0
|
734
|
482
|
735 if (ngx_strcmp(ngx_signal, "stop") == 0
|
|
736 || ngx_strcmp(ngx_signal, "quit") == 0
|
|
737 || ngx_strcmp(ngx_signal, "reopen") == 0
|
|
738 || ngx_strcmp(ngx_signal, "reload") == 0)
|
|
739 {
|
|
740 ngx_process = NGX_PROCESS_SIGNALLER;
|
|
741 goto next;
|
|
742 }
|
|
743
|
|
744 ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
|
|
745 return NGX_ERROR;
|
|
746
|
|
747 default:
|
|
748 ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
|
384
|
749 return NGX_ERROR;
|
|
750 }
|
482
|
751 }
|
480
|
752
|
482
|
753 next:
|
480
|
754
|
482
|
755 continue;
|
0
|
756 }
|
|
757
|
|
758 return NGX_OK;
|
|
759 }
|
|
760
|
|
761
|
48
|
762 static ngx_int_t
|
|
763 ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
|
2
|
764 {
|
50
|
765 #if (NGX_FREEBSD)
|
|
766
|
|
767 ngx_os_argv = (char **) argv;
|
|
768 ngx_argc = argc;
|
|
769 ngx_argv = (char **) argv;
|
|
770
|
|
771 #else
|
2
|
772 size_t len;
|
|
773 ngx_int_t i;
|
|
774
|
|
775 ngx_os_argv = (char **) argv;
|
|
776 ngx_argc = argc;
|
|
777
|
50
|
778 ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log);
|
|
779 if (ngx_argv == NULL) {
|
2
|
780 return NGX_ERROR;
|
|
781 }
|
|
782
|
|
783 for (i = 0; i < argc; i++) {
|
|
784 len = ngx_strlen(argv[i]) + 1;
|
|
785
|
50
|
786 ngx_argv[i] = ngx_alloc(len, cycle->log);
|
|
787 if (ngx_argv[i] == NULL) {
|
2
|
788 return NGX_ERROR;
|
|
789 }
|
|
790
|
66
|
791 (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
|
2
|
792 }
|
|
793
|
|
794 ngx_argv[i] = NULL;
|
|
795
|
|
796 #endif
|
|
797
|
278
|
798 ngx_os_environ = environ;
|
|
799
|
2
|
800 return NGX_OK;
|
|
801 }
|
|
802
|
|
803
|
482
|
804 static ngx_int_t
|
|
805 ngx_process_options(ngx_cycle_t *cycle)
|
|
806 {
|
|
807 u_char *p;
|
|
808 size_t len;
|
|
809
|
|
810 if (ngx_prefix) {
|
|
811 len = ngx_strlen(ngx_prefix);
|
|
812 p = ngx_prefix;
|
|
813
|
|
814 if (!ngx_path_separator(*p)) {
|
|
815 p = ngx_pnalloc(cycle->pool, len + 1);
|
|
816 if (p == NULL) {
|
|
817 return NGX_ERROR;
|
|
818 }
|
|
819
|
|
820 ngx_memcpy(p, ngx_prefix, len);
|
|
821 p[len++] = '/';
|
|
822 }
|
|
823
|
|
824 cycle->conf_prefix.len = len;
|
|
825 cycle->conf_prefix.data = p;
|
|
826 cycle->prefix.len = len;
|
|
827 cycle->prefix.data = p;
|
|
828
|
|
829 } else {
|
|
830
|
|
831 #ifndef NGX_PREFIX
|
|
832
|
|
833 p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
|
|
834 if (p == NULL) {
|
|
835 return NGX_ERROR;
|
|
836 }
|
|
837
|
|
838 if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
|
|
839 ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
|
|
840 return NGX_ERROR;
|
|
841 }
|
|
842
|
|
843 len = ngx_strlen(p);
|
|
844
|
|
845 p[len++] = '/';
|
|
846
|
|
847 cycle->conf_prefix.len = len;
|
|
848 cycle->conf_prefix.data = p;
|
|
849 cycle->prefix.len = len;
|
|
850 cycle->prefix.data = p;
|
|
851
|
|
852 #else
|
|
853
|
|
854 #ifdef NGX_CONF_PREFIX
|
|
855 cycle->conf_prefix.len = sizeof(NGX_CONF_PREFIX) - 1;
|
|
856 cycle->conf_prefix.data = (u_char *) NGX_CONF_PREFIX;
|
|
857 #else
|
|
858 cycle->conf_prefix.len = sizeof(NGX_PREFIX) - 1;
|
|
859 cycle->conf_prefix.data = (u_char *) NGX_PREFIX;
|
|
860 #endif
|
|
861 cycle->prefix.len = sizeof(NGX_PREFIX) - 1;
|
|
862 cycle->prefix.data = (u_char *) NGX_PREFIX;
|
|
863
|
|
864 #endif
|
|
865 }
|
|
866
|
|
867 if (ngx_conf_file) {
|
|
868 cycle->conf_file.len = ngx_strlen(ngx_conf_file);
|
|
869 cycle->conf_file.data = ngx_conf_file;
|
|
870
|
|
871 } else {
|
|
872 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
|
|
873 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
|
|
874 }
|
|
875
|
|
876 if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
|
|
877 return NGX_ERROR;
|
|
878 }
|
|
879
|
|
880 for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
|
|
881 p > cycle->conf_file.data;
|
|
882 p--)
|
|
883 {
|
|
884 if (ngx_path_separator(*p)) {
|
|
885 cycle->conf_prefix.len = p - ngx_cycle->conf_file.data + 1;
|
|
886 cycle->conf_prefix.data = ngx_cycle->conf_file.data;
|
|
887 break;
|
|
888 }
|
|
889 }
|
|
890
|
|
891 if (ngx_conf_params) {
|
|
892 cycle->conf_param.len = ngx_strlen(ngx_conf_params);
|
|
893 cycle->conf_param.data = ngx_conf_params;
|
|
894 }
|
|
895
|
|
896 if (ngx_test_config) {
|
|
897 cycle->log->log_level = NGX_LOG_INFO;
|
|
898 }
|
|
899
|
|
900 return NGX_OK;
|
|
901 }
|
|
902
|
|
903
|
48
|
904 static void *
|
|
905 ngx_core_module_create_conf(ngx_cycle_t *cycle)
|
0
|
906 {
|
|
907 ngx_core_conf_t *ccf;
|
|
908
|
50
|
909 ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));
|
|
910 if (ccf == NULL) {
|
0
|
911 return NULL;
|
|
912 }
|
28
|
913
|
|
914 /*
|
|
915 * set by pcalloc()
|
0
|
916 *
|
28
|
917 * ccf->pid = NULL;
|
92
|
918 * ccf->oldpid = NULL;
|
28
|
919 * ccf->priority = 0;
|
126
|
920 * ccf->cpu_affinity_n = 0;
|
|
921 * ccf->cpu_affinity = NULL;
|
0
|
922 */
|
28
|
923
|
0
|
924 ccf->daemon = NGX_CONF_UNSET;
|
|
925 ccf->master = NGX_CONF_UNSET;
|
112
|
926 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
|
|
927
|
0
|
928 ccf->worker_processes = NGX_CONF_UNSET;
|
10
|
929 ccf->debug_points = NGX_CONF_UNSET;
|
92
|
930
|
|
931 ccf->rlimit_nofile = NGX_CONF_UNSET;
|
270
|
932 ccf->rlimit_core = NGX_CONF_UNSET_SIZE;
|
92
|
933 ccf->rlimit_sigpending = NGX_CONF_UNSET;
|
|
934
|
66
|
935 ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
|
|
936 ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
|
92
|
937
|
0
|
938 #if (NGX_THREADS)
|
|
939 ccf->worker_threads = NGX_CONF_UNSET;
|
10
|
940 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
|
0
|
941 #endif
|
|
942
|
278
|
943 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
|
|
944 != NGX_OK)
|
|
945 {
|
|
946 return NULL;
|
|
947 }
|
|
948
|
0
|
949 return ccf;
|
|
950 }
|
|
951
|
|
952
|
48
|
953 static char *
|
|
954 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
955 {
|
|
956 ngx_core_conf_t *ccf = conf;
|
|
957
|
|
958 ngx_conf_init_value(ccf->daemon, 1);
|
|
959 ngx_conf_init_value(ccf->master, 1);
|
112
|
960 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
|
|
961
|
0
|
962 ngx_conf_init_value(ccf->worker_processes, 1);
|
10
|
963 ngx_conf_init_value(ccf->debug_points, 0);
|
0
|
964
|
126
|
965 #if (NGX_HAVE_SCHED_SETAFFINITY)
|
|
966
|
|
967 if (ccf->cpu_affinity_n
|
|
968 && ccf->cpu_affinity_n != 1
|
|
969 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
|
|
970 {
|
|
971 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
|
|
972 "number of the \"worker_processes\" is not equal to "
|
|
973 "the number of the \"worker_cpu_affinity\" mask, "
|
|
974 "using last mask for remaining worker processes");
|
|
975 }
|
|
976
|
|
977 #endif
|
|
978
|
0
|
979 #if (NGX_THREADS)
|
126
|
980
|
0
|
981 ngx_conf_init_value(ccf->worker_threads, 0);
|
|
982 ngx_threads_n = ccf->worker_threads;
|
|
983 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
|
126
|
984
|
0
|
985 #endif
|
|
986
|
480
|
987
|
|
988 if (ccf->pid.len == 0) {
|
|
989 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
|
|
990 ccf->pid.data = (u_char *) NGX_PID_PATH;
|
|
991 }
|
|
992
|
|
993 if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
|
|
994 return NGX_CONF_ERROR;
|
|
995 }
|
|
996
|
|
997 ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
|
|
998
|
|
999 ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
|
|
1000 if (ccf->oldpid.data == NULL) {
|
|
1001 return NGX_CONF_ERROR;
|
|
1002 }
|
|
1003
|
|
1004 ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
|
|
1005 NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
|
|
1006
|
|
1007
|
10
|
1008 #if !(NGX_WIN32)
|
0
|
1009
|
66
|
1010 if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
|
356
|
1011 struct group *grp;
|
|
1012 struct passwd *pwd;
|
0
|
1013
|
318
|
1014 ngx_set_errno(0);
|
4
|
1015 pwd = getpwnam(NGX_USER);
|
0
|
1016 if (pwd == NULL) {
|
|
1017 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
4
|
1018 "getpwnam(\"" NGX_USER "\") failed");
|
0
|
1019 return NGX_CONF_ERROR;
|
|
1020 }
|
|
1021
|
22
|
1022 ccf->username = NGX_USER;
|
0
|
1023 ccf->user = pwd->pw_uid;
|
|
1024
|
318
|
1025 ngx_set_errno(0);
|
4
|
1026 grp = getgrnam(NGX_GROUP);
|
0
|
1027 if (grp == NULL) {
|
|
1028 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
4
|
1029 "getgrnam(\"" NGX_GROUP "\") failed");
|
0
|
1030 return NGX_CONF_ERROR;
|
|
1031 }
|
|
1032
|
|
1033 ccf->group = grp->gr_gid;
|
|
1034 }
|
|
1035
|
258
|
1036
|
160
|
1037 if (ccf->lock_file.len == 0) {
|
|
1038 ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
|
|
1039 ccf->lock_file.data = (u_char *) NGX_LOCK_PATH;
|
|
1040 }
|
|
1041
|
454
|
1042 if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) != NGX_OK) {
|
160
|
1043 return NGX_CONF_ERROR;
|
|
1044 }
|
|
1045
|
356
|
1046 {
|
|
1047 ngx_str_t lock_file;
|
|
1048
|
258
|
1049 lock_file = cycle->old_cycle->lock_file;
|
|
1050
|
|
1051 if (lock_file.len) {
|
|
1052 lock_file.len--;
|
|
1053
|
|
1054 if (ccf->lock_file.len != lock_file.len
|
|
1055 || ngx_strncmp(ccf->lock_file.data, lock_file.data, lock_file.len)
|
|
1056 != 0)
|
|
1057 {
|
|
1058 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
1059 "\"lock_file\" could not be changed, ignored");
|
|
1060 }
|
|
1061
|
|
1062 cycle->lock_file.len = lock_file.len + 1;
|
|
1063 lock_file.len += sizeof(".accept");
|
|
1064
|
|
1065 cycle->lock_file.data = ngx_pstrdup(cycle->pool, &lock_file);
|
|
1066 if (cycle->lock_file.data == NULL) {
|
|
1067 return NGX_CONF_ERROR;
|
|
1068 }
|
|
1069
|
|
1070 } else {
|
|
1071 cycle->lock_file.len = ccf->lock_file.len + 1;
|
382
|
1072 cycle->lock_file.data = ngx_pnalloc(cycle->pool,
|
258
|
1073 ccf->lock_file.len + sizeof(".accept"));
|
|
1074 if (cycle->lock_file.data == NULL) {
|
|
1075 return NGX_CONF_ERROR;
|
|
1076 }
|
|
1077
|
|
1078 ngx_memcpy(ngx_cpymem(cycle->lock_file.data, ccf->lock_file.data,
|
|
1079 ccf->lock_file.len),
|
|
1080 ".accept", sizeof(".accept"));
|
|
1081 }
|
356
|
1082 }
|
258
|
1083
|
0
|
1084 #endif
|
|
1085
|
|
1086 return NGX_CONF_OK;
|
|
1087 }
|
|
1088
|
|
1089
|
48
|
1090 static char *
|
|
1091 ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1092 {
|
10
|
1093 #if (NGX_WIN32)
|
0
|
1094
|
|
1095 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1096 "\"user\" is not supported, ignored");
|
|
1097
|
|
1098 return NGX_CONF_OK;
|
|
1099
|
|
1100 #else
|
|
1101
|
|
1102 ngx_core_conf_t *ccf = conf;
|
|
1103
|
4
|
1104 char *group;
|
0
|
1105 struct passwd *pwd;
|
|
1106 struct group *grp;
|
|
1107 ngx_str_t *value;
|
|
1108
|
66
|
1109 if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
|
0
|
1110 return "is duplicate";
|
|
1111 }
|
|
1112
|
4
|
1113 if (geteuid() != 0) {
|
|
1114 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1115 "the \"user\" directive makes sense only "
|
|
1116 "if the master process runs "
|
|
1117 "with super-user privileges, ignored");
|
|
1118 return NGX_CONF_OK;
|
|
1119 }
|
|
1120
|
0
|
1121 value = (ngx_str_t *) cf->args->elts;
|
|
1122
|
22
|
1123 ccf->username = (char *) value[1].data;
|
|
1124
|
318
|
1125 ngx_set_errno(0);
|
0
|
1126 pwd = getpwnam((const char *) value[1].data);
|
|
1127 if (pwd == NULL) {
|
|
1128 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
2
|
1129 "getpwnam(\"%s\") failed", value[1].data);
|
0
|
1130 return NGX_CONF_ERROR;
|
|
1131 }
|
|
1132
|
|
1133 ccf->user = pwd->pw_uid;
|
|
1134
|
4
|
1135 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
|
0
|
1136
|
318
|
1137 ngx_set_errno(0);
|
4
|
1138 grp = getgrnam(group);
|
0
|
1139 if (grp == NULL) {
|
|
1140 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
4
|
1141 "getgrnam(\"%s\") failed", group);
|
0
|
1142 return NGX_CONF_ERROR;
|
|
1143 }
|
|
1144
|
|
1145 ccf->group = grp->gr_gid;
|
|
1146
|
|
1147 return NGX_CONF_OK;
|
|
1148
|
|
1149 #endif
|
|
1150 }
|
22
|
1151
|
|
1152
|
48
|
1153 static char *
|
278
|
1154 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1155 {
|
|
1156 ngx_core_conf_t *ccf = conf;
|
|
1157
|
|
1158 ngx_str_t *value, *var;
|
|
1159 ngx_uint_t i;
|
|
1160
|
|
1161 var = ngx_array_push(&ccf->env);
|
|
1162 if (var == NULL) {
|
|
1163 return NGX_CONF_ERROR;
|
|
1164 }
|
|
1165
|
|
1166 value = cf->args->elts;
|
|
1167 *var = value[1];
|
|
1168
|
|
1169 for (i = 0; i < value[1].len; i++) {
|
|
1170
|
|
1171 if (value[1].data[i] == '=') {
|
|
1172
|
|
1173 var->len = i;
|
|
1174
|
|
1175 return NGX_CONF_OK;
|
|
1176 }
|
|
1177 }
|
|
1178
|
|
1179 return NGX_CONF_OK;
|
|
1180 }
|
|
1181
|
|
1182
|
|
1183 static char *
|
48
|
1184 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
22
|
1185 {
|
|
1186 ngx_core_conf_t *ccf = conf;
|
|
1187
|
|
1188 ngx_str_t *value;
|
|
1189 ngx_uint_t n, minus;
|
|
1190
|
|
1191 if (ccf->priority != 0) {
|
|
1192 return "is duplicate";
|
|
1193 }
|
|
1194
|
|
1195 value = cf->args->elts;
|
|
1196
|
|
1197 if (value[1].data[0] == '-') {
|
|
1198 n = 1;
|
|
1199 minus = 1;
|
|
1200
|
|
1201 } else if (value[1].data[0] == '+') {
|
|
1202 n = 1;
|
|
1203 minus = 0;
|
|
1204
|
|
1205 } else {
|
|
1206 n = 0;
|
|
1207 minus = 0;
|
|
1208 }
|
|
1209
|
|
1210 ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n);
|
|
1211 if (ccf->priority == NGX_ERROR) {
|
|
1212 return "invalid number";
|
|
1213 }
|
|
1214
|
|
1215 if (minus) {
|
|
1216 ccf->priority = -ccf->priority;
|
|
1217 }
|
|
1218
|
|
1219 return NGX_CONF_OK;
|
|
1220 }
|
126
|
1221
|
|
1222
|
|
1223 static char *
|
|
1224 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1225 {
|
|
1226 #if (NGX_HAVE_SCHED_SETAFFINITY)
|
|
1227 ngx_core_conf_t *ccf = conf;
|
|
1228
|
|
1229 u_char ch;
|
|
1230 u_long *mask;
|
|
1231 ngx_str_t *value;
|
|
1232 ngx_uint_t i, n;
|
|
1233
|
|
1234 if (ccf->cpu_affinity) {
|
|
1235 return "is duplicate";
|
|
1236 }
|
|
1237
|
|
1238 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
|
|
1239 if (mask == NULL) {
|
|
1240 return NGX_CONF_ERROR;
|
|
1241 }
|
|
1242
|
|
1243 ccf->cpu_affinity_n = cf->args->nelts - 1;
|
|
1244 ccf->cpu_affinity = mask;
|
|
1245
|
|
1246 value = cf->args->elts;
|
|
1247
|
|
1248 for (n = 1; n < cf->args->nelts; n++) {
|
|
1249
|
|
1250 if (value[n].len > 32) {
|
|
1251 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1252 "\"worker_cpu_affinity\" supports up to 32 CPU only");
|
|
1253 return NGX_CONF_ERROR;
|
|
1254 }
|
|
1255
|
|
1256 mask[n - 1] = 0;
|
|
1257
|
|
1258 for (i = 0; i < value[n].len; i++) {
|
|
1259
|
|
1260 ch = value[n].data[i];
|
|
1261
|
|
1262 if (ch == ' ') {
|
|
1263 continue;
|
|
1264 }
|
|
1265
|
|
1266 mask[n - 1] <<= 1;
|
|
1267
|
|
1268 if (ch == '0') {
|
|
1269 continue;
|
|
1270 }
|
|
1271
|
|
1272 if (ch == '1') {
|
|
1273 mask[n - 1] |= 1;
|
|
1274 continue;
|
|
1275 }
|
|
1276
|
|
1277 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1278 "invalid character \"%c\" in \"worker_cpu_affinity\"",
|
|
1279 ch);
|
378
|
1280 return NGX_CONF_ERROR;
|
126
|
1281 }
|
|
1282 }
|
|
1283
|
|
1284 #else
|
|
1285
|
|
1286 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1287 "\"worker_cpu_affinity\" is not supported "
|
|
1288 "on this platform, ignored");
|
|
1289 #endif
|
|
1290
|
|
1291 return NGX_CONF_OK;
|
|
1292 }
|
|
1293
|
|
1294
|
|
1295 u_long
|
|
1296 ngx_get_cpu_affinity(ngx_uint_t n)
|
|
1297 {
|
|
1298 ngx_core_conf_t *ccf;
|
|
1299
|
|
1300 ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
|
|
1301 ngx_core_module);
|
|
1302
|
|
1303 if (ccf->cpu_affinity == NULL) {
|
|
1304 return 0;
|
|
1305 }
|
|
1306
|
|
1307 if (ccf->cpu_affinity_n > n) {
|
|
1308 return ccf->cpu_affinity[n];
|
|
1309 }
|
|
1310
|
|
1311 return ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
|
|
1312 }
|