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