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