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