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