1
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <nginx.h>
11
12
13 static void ngx_show_version_info(void);
14 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
15 static void ngx_cleanup_environment(void *data);
16 static ngx_int_t ngx_get_options(int argc, char *const *argv);
17 static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
18 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
19 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
20 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
21 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
22 static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
23 static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
24 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
25 void *conf);
26 static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd,
27 void *conf);
28 static char *ngx_load_module(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
29 #if (NGX_HAVE_DLOPEN)
30 static void ngx_unload_module(void *data);
31 #endif
32
33 #if (NGX_HAVE_FSTACK)
34 static char *ngx_set_fstack_conf(ngx_conf_t *cf, ngx_command_t *cmd,
35 void *conf);
36 #endif
37
38 static ngx_conf_enum_t ngx_debug_points[] = {
39 { ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
40 { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
41 { ngx_null_string, 0 }
42 };
43
44
45 static ngx_command_t ngx_core_commands[] = {
46
47 { ngx_string("daemon"),
48 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
49 ngx_conf_set_flag_slot,
50 0,
51 offsetof(ngx_core_conf_t, daemon),
52 NULL },
53
54 { ngx_string("master_process"),
55 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
56 ngx_conf_set_flag_slot,
57 0,
58 offsetof(ngx_core_conf_t, master),
59 NULL },
60
61 { ngx_string("timer_resolution"),
62 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
63 ngx_conf_set_msec_slot,
64 0,
65 offsetof(ngx_core_conf_t, timer_resolution),
66 NULL },
67
68 { ngx_string("pid"),
69 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
70 ngx_conf_set_str_slot,
71 0,
72 offsetof(ngx_core_conf_t, pid),
73 NULL },
74
75 { ngx_string("lock_file"),
76 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
77 ngx_conf_set_str_slot,
78 0,
79 offsetof(ngx_core_conf_t, lock_file),
80 NULL },
81
82 { ngx_string("worker_processes"),
83 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
84 ngx_set_worker_processes,
85 0,
86 0,
87 NULL },
88
89 { ngx_string("debug_points"),
90 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
91 ngx_conf_set_enum_slot,
92 0,
93 offsetof(ngx_core_conf_t, debug_points),
94 &ngx_debug_points },
95
96 { ngx_string("user"),
97 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
98 ngx_set_user,
99 0,
100 0,
101 NULL },
102
103 { ngx_string("worker_priority"),
104 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
105 ngx_set_priority,
106 0,
107 0,
108 NULL },
109
110 { ngx_string("worker_cpu_affinity"),
111 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
112 ngx_set_cpu_affinity,
113 0,
114 0,
115 NULL },
116
117 { ngx_string("worker_rlimit_nofile"),
118 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
119 ngx_conf_set_num_slot,
120 0,
121 offsetof(ngx_core_conf_t, rlimit_nofile),
122 NULL },
123
124 { ngx_string("worker_rlimit_core"),
125 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
126 ngx_conf_set_off_slot,
127 0,
128 offsetof(ngx_core_conf_t, rlimit_core),
129 NULL },
130
131 { ngx_string("worker_shutdown_timeout"),
132 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
133 ngx_conf_set_msec_slot,
134 0,
135 offsetof(ngx_core_conf_t, shutdown_timeout),
136 NULL },
137
138 { ngx_string("working_directory"),
139 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
140 ngx_conf_set_str_slot,
141 0,
142 offsetof(ngx_core_conf_t, working_directory),
143 NULL },
144
145 { ngx_string("env"),
146 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
147 ngx_set_env,
148 0,
149 0,
150 NULL },
151
152 { ngx_string("load_module"),
153 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
154 ngx_load_module,
155 0,
156 0,
157 NULL },
158
159 #if (NGX_HAVE_FSTACK)
160 { ngx_string("fstack_conf"),
161 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
162 ngx_set_fstack_conf,
163 0,
164 offsetof(ngx_core_conf_t, fstack_conf),
165 NULL },
166
167 { ngx_string("schedule_timeout"),
168 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
169 ngx_conf_set_msec_slot,
170 0,
171 offsetof(ngx_core_conf_t, schedule_timeout),
172 NULL },
173 #endif
174
175 ngx_null_command
176 };
177
178
179 static ngx_core_module_t ngx_core_module_ctx = {
180 ngx_string("core"),
181 ngx_core_module_create_conf,
182 ngx_core_module_init_conf
183 };
184
185
186 ngx_module_t ngx_core_module = {
187 NGX_MODULE_V1,
188 &ngx_core_module_ctx, /* module context */
189 ngx_core_commands, /* module directives */
190 NGX_CORE_MODULE, /* module type */
191 NULL, /* init master */
192 NULL, /* init module */
193 NULL, /* init process */
194 NULL, /* init thread */
195 NULL, /* exit thread */
196 NULL, /* exit process */
197 NULL, /* exit master */
198 NGX_MODULE_V1_PADDING
199 };
200
201
202 static ngx_uint_t ngx_show_help;
203 static ngx_uint_t ngx_show_version;
204 static ngx_uint_t ngx_show_configure;
205 static u_char *ngx_prefix;
206 static u_char *ngx_conf_file;
207 static u_char *ngx_conf_params;
208 static char *ngx_signal;
209
210
211 static char **ngx_os_environ;
212
213
214 int ngx_cdecl
main(int argc,char * const * argv)215 main(int argc, char *const *argv)
216 {
217 ngx_buf_t *b;
218 ngx_log_t *log;
219 ngx_uint_t i;
220 ngx_cycle_t *cycle, init_cycle;
221 ngx_conf_dump_t *cd;
222 ngx_core_conf_t *ccf;
223
224 ngx_debug_init();
225
226 if (ngx_strerror_init() != NGX_OK) {
227 return 1;
228 }
229
230 if (ngx_get_options(argc, argv) != NGX_OK) {
231 return 1;
232 }
233
234 if (ngx_show_version) {
235 ngx_show_version_info();
236
237 if (!ngx_test_config) {
238 return 0;
239 }
240 }
241
242 /* TODO */ ngx_max_sockets = -1;
243
244 ngx_time_init();
245
246 #if (NGX_PCRE)
247 ngx_regex_init();
248 #endif
249
250 ngx_pid = ngx_getpid();
251 ngx_parent = ngx_getppid();
252
253 log = ngx_log_init(ngx_prefix);
254 if (log == NULL) {
255 return 1;
256 }
257
258 /* STUB */
259 #if (NGX_OPENSSL)
260 ngx_ssl_init(log);
261 #endif
262
263 /*
264 * init_cycle->log is required for signal handlers and
265 * ngx_process_options()
266 */
267
268 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
269 init_cycle.log = log;
270 ngx_cycle = &init_cycle;
271
272 init_cycle.pool = ngx_create_pool(1024, log);
273 if (init_cycle.pool == NULL) {
274 return 1;
275 }
276
277 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
278 return 1;
279 }
280
281 if (ngx_process_options(&init_cycle) != NGX_OK) {
282 return 1;
283 }
284
285 if (ngx_os_init(log) != NGX_OK) {
286 return 1;
287 }
288
289 /*
290 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
291 */
292
293 if (ngx_crc32_table_init() != NGX_OK) {
294 return 1;
295 }
296
297 /*
298 * ngx_slab_sizes_init() requires ngx_pagesize set in ngx_os_init()
299 */
300
301 ngx_slab_sizes_init();
302
303 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
304 return 1;
305 }
306
307 if (ngx_preinit_modules() != NGX_OK) {
308 return 1;
309 }
310
311 cycle = ngx_init_cycle(&init_cycle);
312 if (cycle == NULL) {
313 if (ngx_test_config) {
314 ngx_log_stderr(0, "configuration file %s test failed",
315 init_cycle.conf_file.data);
316 }
317
318 return 1;
319 }
320
321 if (ngx_test_config) {
322 if (!ngx_quiet_mode) {
323 ngx_log_stderr(0, "configuration file %s test is successful",
324 cycle->conf_file.data);
325 }
326
327 if (ngx_dump_config) {
328 cd = cycle->config_dump.elts;
329
330 for (i = 0; i < cycle->config_dump.nelts; i++) {
331
332 ngx_write_stdout("# configuration file ");
333 (void) ngx_write_fd(ngx_stdout, cd[i].name.data,
334 cd[i].name.len);
335 ngx_write_stdout(":" NGX_LINEFEED);
336
337 b = cd[i].buffer;
338
339 (void) ngx_write_fd(ngx_stdout, b->pos, b->last - b->pos);
340 ngx_write_stdout(NGX_LINEFEED);
341 }
342 }
343
344 return 0;
345 }
346
347 if (ngx_signal) {
348 return ngx_signal_process(cycle, ngx_signal);
349 }
350
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
357 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
358 ngx_process = NGX_PROCESS_MASTER;
359 }
360
361 #if !(NGX_WIN32)
362
363 if (ngx_init_signals(cycle->log) != NGX_OK) {
364 return 1;
365 }
366
367 if (!ngx_inherited && ccf->daemon) {
368 if (ngx_daemon(cycle->log) != NGX_OK) {
369 return 1;
370 }
371
372 ngx_daemonized = 1;
373 }
374
375 if (ngx_inherited) {
376 ngx_daemonized = 1;
377 }
378
379 #endif
380
381 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
382 return 1;
383 }
384
385 if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
386 return 1;
387 }
388
389 if (log->file->fd != ngx_stderr) {
390 if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {
391 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
392 ngx_close_file_n " built-in log failed");
393 }
394 }
395
396 ngx_use_stderr = 0;
397
398 if (ngx_process == NGX_PROCESS_SINGLE) {
399 ngx_single_process_cycle(cycle);
400
401 } else {
402 ngx_master_process_cycle(cycle);
403 }
404
405 return 0;
406 }
407
408
409 static void
ngx_show_version_info(void)410 ngx_show_version_info(void)
411 {
412 ngx_write_stderr("nginx version: " NGINX_VER_BUILD NGX_LINEFEED);
413
414 if (ngx_show_help) {
415 ngx_write_stderr(
416 "Usage: nginx [-?hvVtTq] [-s signal] [-c filename] "
417 "[-p prefix] [-g directives]" NGX_LINEFEED
418 NGX_LINEFEED
419 "Options:" NGX_LINEFEED
420 " -?,-h : this help" NGX_LINEFEED
421 " -v : show version and exit" NGX_LINEFEED
422 " -V : show version and configure options then exit"
423 NGX_LINEFEED
424 " -t : test configuration and exit" NGX_LINEFEED
425 " -T : test configuration, dump it and exit"
426 NGX_LINEFEED
427 " -q : suppress non-error messages "
428 "during configuration testing" NGX_LINEFEED
429 " -s signal : send signal to a master process: "
430 "stop, quit, reopen, reload" NGX_LINEFEED
431 #ifdef NGX_PREFIX
432 " -p prefix : set prefix path (default: " NGX_PREFIX ")"
433 NGX_LINEFEED
434 #else
435 " -p prefix : set prefix path (default: NONE)" NGX_LINEFEED
436 #endif
437 " -c filename : set configuration file (default: " NGX_CONF_PATH
438 ")" NGX_LINEFEED
439 " -g directives : set global directives out of configuration "
440 "file" NGX_LINEFEED NGX_LINEFEED
441 );
442 }
443
444 if (ngx_show_configure) {
445
446 #ifdef NGX_COMPILER
447 ngx_write_stderr("built by " NGX_COMPILER NGX_LINEFEED);
448 #endif
449
450 #if (NGX_SSL)
451 if (ngx_strcmp(ngx_ssl_version(), OPENSSL_VERSION_TEXT) == 0) {
452 ngx_write_stderr("built with " OPENSSL_VERSION_TEXT NGX_LINEFEED);
453 } else {
454 ngx_write_stderr("built with " OPENSSL_VERSION_TEXT
455 " (running with ");
456 ngx_write_stderr((char *) (uintptr_t) ngx_ssl_version());
457 ngx_write_stderr(")" NGX_LINEFEED);
458 }
459 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
460 ngx_write_stderr("TLS SNI support enabled" NGX_LINEFEED);
461 #else
462 ngx_write_stderr("TLS SNI support disabled" NGX_LINEFEED);
463 #endif
464 #endif
465
466 ngx_write_stderr("configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
467 }
468 }
469
470
471 static ngx_int_t
ngx_add_inherited_sockets(ngx_cycle_t * cycle)472 ngx_add_inherited_sockets(ngx_cycle_t *cycle)
473 {
474 u_char *p, *v, *inherited;
475 ngx_int_t s;
476 ngx_listening_t *ls;
477
478 inherited = (u_char *) getenv(NGINX_VAR);
479
480 if (inherited == NULL) {
481 return NGX_OK;
482 }
483
484 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
485 "using inherited sockets from \"%s\"", inherited);
486
487 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
488 sizeof(ngx_listening_t))
489 != NGX_OK)
490 {
491 return NGX_ERROR;
492 }
493
494 for (p = inherited, v = p; *p; p++) {
495 if (*p == ':' || *p == ';') {
496 s = ngx_atoi(v, p - v);
497 if (s == NGX_ERROR) {
498 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
499 "invalid socket number \"%s\" in " NGINX_VAR
500 " environment variable, ignoring the rest"
501 " of the variable", v);
502 break;
503 }
504
505 v = p + 1;
506
507 ls = ngx_array_push(&cycle->listening);
508 if (ls == NULL) {
509 return NGX_ERROR;
510 }
511
512 ngx_memzero(ls, sizeof(ngx_listening_t));
513
514 ls->fd = (ngx_socket_t) s;
515 }
516 }
517
518 if (v != p) {
519 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
520 "invalid socket number \"%s\" in " NGINX_VAR
521 " environment variable, ignoring", v);
522 }
523
524 ngx_inherited = 1;
525
526 return ngx_set_inherited_sockets(cycle);
527 }
528
529
530 char **
ngx_set_environment(ngx_cycle_t * cycle,ngx_uint_t * last)531 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
532 {
533 char **p, **env;
534 ngx_str_t *var;
535 ngx_uint_t i, n;
536 ngx_core_conf_t *ccf;
537 ngx_pool_cleanup_t *cln;
538
539 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
540
541 if (last == NULL && ccf->environment) {
542 return ccf->environment;
543 }
544
545 var = ccf->env.elts;
546
547 for (i = 0; i < ccf->env.nelts; i++) {
548 if (ngx_strcmp(var[i].data, "TZ") == 0
549 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
550 {
551 goto tz_found;
552 }
553 }
554
555 var = ngx_array_push(&ccf->env);
556 if (var == NULL) {
557 return NULL;
558 }
559
560 var->len = 2;
561 var->data = (u_char *) "TZ";
562
563 var = ccf->env.elts;
564
565 tz_found:
566
567 n = 0;
568
569 for (i = 0; i < ccf->env.nelts; i++) {
570
571 if (var[i].data[var[i].len] == '=') {
572 n++;
573 continue;
574 }
575
576 for (p = ngx_os_environ; *p; p++) {
577
578 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
579 && (*p)[var[i].len] == '=')
580 {
581 n++;
582 break;
583 }
584 }
585 }
586
587 if (last) {
588 env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle->log);
589 if (env == NULL) {
590 return NULL;
591 }
592
593 *last = n;
594
595 } else {
596 cln = ngx_pool_cleanup_add(cycle->pool, 0);
597 if (cln == NULL) {
598 return NULL;
599 }
600
601 env = ngx_alloc((n + 1) * sizeof(char *), cycle->log);
602 if (env == NULL) {
603 return NULL;
604 }
605
606 cln->handler = ngx_cleanup_environment;
607 cln->data = env;
608 }
609
610 n = 0;
611
612 for (i = 0; i < ccf->env.nelts; i++) {
613
614 if (var[i].data[var[i].len] == '=') {
615 env[n++] = (char *) var[i].data;
616 continue;
617 }
618
619 for (p = ngx_os_environ; *p; p++) {
620
621 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
622 && (*p)[var[i].len] == '=')
623 {
624 env[n++] = *p;
625 break;
626 }
627 }
628 }
629
630 env[n] = NULL;
631
632 if (last == NULL) {
633 ccf->environment = env;
634 environ = env;
635 }
636
637 return env;
638 }
639
640
641 static void
ngx_cleanup_environment(void * data)642 ngx_cleanup_environment(void *data)
643 {
644 char **env = data;
645
646 if (environ == env) {
647
648 /*
649 * if the environment is still used, as it happens on exit,
650 * the only option is to leak it
651 */
652
653 return;
654 }
655
656 ngx_free(env);
657 }
658
659
660 ngx_pid_t
ngx_exec_new_binary(ngx_cycle_t * cycle,char * const * argv)661 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
662 {
663 char **env, *var;
664 u_char *p;
665 ngx_uint_t i, n;
666 ngx_pid_t pid;
667 ngx_exec_ctx_t ctx;
668 ngx_core_conf_t *ccf;
669 ngx_listening_t *ls;
670
671 ngx_memzero(&ctx, sizeof(ngx_exec_ctx_t));
672
673 ctx.path = argv[0];
674 ctx.name = "new binary process";
675 ctx.argv = argv;
676
677 n = 2;
678 env = ngx_set_environment(cycle, &n);
679 if (env == NULL) {
680 return NGX_INVALID_PID;
681 }
682
683 var = ngx_alloc(sizeof(NGINX_VAR)
684 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
685 cycle->log);
686 if (var == NULL) {
687 ngx_free(env);
688 return NGX_INVALID_PID;
689 }
690
691 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
692
693 ls = cycle->listening.elts;
694 for (i = 0; i < cycle->listening.nelts; i++) {
695 p = ngx_sprintf(p, "%ud;", ls[i].fd);
696 }
697
698 *p = '\0';
699
700 env[n++] = var;
701
702 #if (NGX_SETPROCTITLE_USES_ENV)
703
704 /* allocate the spare 300 bytes for the new binary process title */
705
706 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
707 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
708 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
709 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
710 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
711
712 #endif
713
714 env[n] = NULL;
715
716 #if (NGX_DEBUG)
717 {
718 char **e;
719 for (e = env; *e; e++) {
720 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
721 }
722 }
723 #endif
724
725 ctx.envp = (char *const *) env;
726
727 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
728
729 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) == NGX_FILE_ERROR) {
730 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
731 ngx_rename_file_n " %s to %s failed "
732 "before executing new binary process \"%s\"",
733 ccf->pid.data, ccf->oldpid.data, argv[0]);
734
735 ngx_free(env);
736 ngx_free(var);
737
738 return NGX_INVALID_PID;
739 }
740
741 pid = ngx_execute(cycle, &ctx);
742
743 if (pid == NGX_INVALID_PID) {
744 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data)
745 == NGX_FILE_ERROR)
746 {
747 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
748 ngx_rename_file_n " %s back to %s failed after "
749 "an attempt to execute new binary process \"%s\"",
750 ccf->oldpid.data, ccf->pid.data, argv[0]);
751 }
752 }
753
754 ngx_free(env);
755 ngx_free(var);
756
757 return pid;
758 }
759
760
761 static ngx_int_t
ngx_get_options(int argc,char * const * argv)762 ngx_get_options(int argc, char *const *argv)
763 {
764 u_char *p;
765 ngx_int_t i;
766
767 for (i = 1; i < argc; i++) {
768
769 p = (u_char *) argv[i];
770
771 if (*p++ != '-') {
772 ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
773 return NGX_ERROR;
774 }
775
776 while (*p) {
777
778 switch (*p++) {
779
780 case '?':
781 case 'h':
782 ngx_show_version = 1;
783 ngx_show_help = 1;
784 break;
785
786 case 'v':
787 ngx_show_version = 1;
788 break;
789
790 case 'V':
791 ngx_show_version = 1;
792 ngx_show_configure = 1;
793 break;
794
795 case 't':
796 ngx_test_config = 1;
797 break;
798
799 case 'T':
800 ngx_test_config = 1;
801 ngx_dump_config = 1;
802 break;
803
804 case 'q':
805 ngx_quiet_mode = 1;
806 break;
807
808 case 'p':
809 if (*p) {
810 ngx_prefix = p;
811 goto next;
812 }
813
814 if (argv[++i]) {
815 ngx_prefix = (u_char *) argv[i];
816 goto next;
817 }
818
819 ngx_log_stderr(0, "option \"-p\" requires directory name");
820 return NGX_ERROR;
821
822 case 'c':
823 if (*p) {
824 ngx_conf_file = p;
825 goto next;
826 }
827
828 if (argv[++i]) {
829 ngx_conf_file = (u_char *) argv[i];
830 goto next;
831 }
832
833 ngx_log_stderr(0, "option \"-c\" requires file name");
834 return NGX_ERROR;
835
836 case 'g':
837 if (*p) {
838 ngx_conf_params = p;
839 goto next;
840 }
841
842 if (argv[++i]) {
843 ngx_conf_params = (u_char *) argv[i];
844 goto next;
845 }
846
847 ngx_log_stderr(0, "option \"-g\" requires parameter");
848 return NGX_ERROR;
849
850 case 's':
851 if (*p) {
852 ngx_signal = (char *) p;
853
854 } else if (argv[++i]) {
855 ngx_signal = argv[i];
856
857 } else {
858 ngx_log_stderr(0, "option \"-s\" requires parameter");
859 return NGX_ERROR;
860 }
861
862 if (ngx_strcmp(ngx_signal, "stop") == 0
863 || ngx_strcmp(ngx_signal, "quit") == 0
864 || ngx_strcmp(ngx_signal, "reopen") == 0
865 || ngx_strcmp(ngx_signal, "reload") == 0)
866 {
867 ngx_process = NGX_PROCESS_SIGNALLER;
868 goto next;
869 }
870
871 ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
872 return NGX_ERROR;
873
874 default:
875 ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
876 return NGX_ERROR;
877 }
878 }
879
880 next:
881
882 continue;
883 }
884
885 return NGX_OK;
886 }
887
888
889 static ngx_int_t
ngx_save_argv(ngx_cycle_t * cycle,int argc,char * const * argv)890 ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
891 {
892 #if (NGX_FREEBSD)
893
894 ngx_os_argv = (char **) argv;
895 ngx_argc = argc;
896 ngx_argv = (char **) argv;
897
898 #else
899 size_t len;
900 ngx_int_t i;
901
902 ngx_os_argv = (char **) argv;
903 ngx_argc = argc;
904
905 ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log);
906 if (ngx_argv == NULL) {
907 return NGX_ERROR;
908 }
909
910 for (i = 0; i < argc; i++) {
911 len = ngx_strlen(argv[i]) + 1;
912
913 ngx_argv[i] = ngx_alloc(len, cycle->log);
914 if (ngx_argv[i] == NULL) {
915 return NGX_ERROR;
916 }
917
918 (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
919 }
920
921 ngx_argv[i] = NULL;
922
923 #endif
924
925 ngx_os_environ = environ;
926
927 return NGX_OK;
928 }
929
930
931 static ngx_int_t
ngx_process_options(ngx_cycle_t * cycle)932 ngx_process_options(ngx_cycle_t *cycle)
933 {
934 u_char *p;
935 size_t len;
936
937 if (ngx_prefix) {
938 len = ngx_strlen(ngx_prefix);
939 p = ngx_prefix;
940
941 if (len && !ngx_path_separator(p[len - 1])) {
942 p = ngx_pnalloc(cycle->pool, len + 1);
943 if (p == NULL) {
944 return NGX_ERROR;
945 }
946
947 ngx_memcpy(p, ngx_prefix, len);
948 p[len++] = '/';
949 }
950
951 cycle->conf_prefix.len = len;
952 cycle->conf_prefix.data = p;
953 cycle->prefix.len = len;
954 cycle->prefix.data = p;
955
956 } else {
957
958 #ifndef NGX_PREFIX
959
960 p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
961 if (p == NULL) {
962 return NGX_ERROR;
963 }
964
965 if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
966 ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
967 return NGX_ERROR;
968 }
969
970 len = ngx_strlen(p);
971
972 p[len++] = '/';
973
974 cycle->conf_prefix.len = len;
975 cycle->conf_prefix.data = p;
976 cycle->prefix.len = len;
977 cycle->prefix.data = p;
978
979 #else
980
981 #ifdef NGX_CONF_PREFIX
982 ngx_str_set(&cycle->conf_prefix, NGX_CONF_PREFIX);
983 #else
984 ngx_str_set(&cycle->conf_prefix, NGX_PREFIX);
985 #endif
986 ngx_str_set(&cycle->prefix, NGX_PREFIX);
987
988 #endif
989 }
990
991 if (ngx_conf_file) {
992 cycle->conf_file.len = ngx_strlen(ngx_conf_file);
993 cycle->conf_file.data = ngx_conf_file;
994
995 } else {
996 ngx_str_set(&cycle->conf_file, NGX_CONF_PATH);
997 }
998
999 if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
1000 return NGX_ERROR;
1001 }
1002
1003 for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
1004 p > cycle->conf_file.data;
1005 p--)
1006 {
1007 if (ngx_path_separator(*p)) {
1008 cycle->conf_prefix.len = p - cycle->conf_file.data + 1;
1009 cycle->conf_prefix.data = cycle->conf_file.data;
1010 break;
1011 }
1012 }
1013
1014 if (ngx_conf_params) {
1015 cycle->conf_param.len = ngx_strlen(ngx_conf_params);
1016 cycle->conf_param.data = ngx_conf_params;
1017 }
1018
1019 if (ngx_test_config) {
1020 cycle->log->log_level = NGX_LOG_INFO;
1021 }
1022
1023 return NGX_OK;
1024 }
1025
1026
1027 static void *
ngx_core_module_create_conf(ngx_cycle_t * cycle)1028 ngx_core_module_create_conf(ngx_cycle_t *cycle)
1029 {
1030 ngx_core_conf_t *ccf;
1031
1032 ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));
1033 if (ccf == NULL) {
1034 return NULL;
1035 }
1036
1037 /*
1038 * set by ngx_pcalloc()
1039 *
1040 * ccf->pid = NULL;
1041 * ccf->oldpid = NULL;
1042 * ccf->priority = 0;
1043 * ccf->cpu_affinity_auto = 0;
1044 * ccf->cpu_affinity_n = 0;
1045 * ccf->cpu_affinity = NULL;
1046 */
1047
1048 ccf->daemon = NGX_CONF_UNSET;
1049 ccf->master = NGX_CONF_UNSET;
1050 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
1051 ccf->shutdown_timeout = NGX_CONF_UNSET_MSEC;
1052
1053 ccf->worker_processes = NGX_CONF_UNSET;
1054 ccf->debug_points = NGX_CONF_UNSET;
1055
1056 ccf->rlimit_nofile = NGX_CONF_UNSET;
1057 ccf->rlimit_core = NGX_CONF_UNSET;
1058
1059 ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
1060 ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
1061
1062 #if (NGX_HAVE_FSTACK)
1063 ccf->schedule_timeout = NGX_CONF_UNSET_MSEC;
1064 #endif
1065
1066 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
1067 != NGX_OK)
1068 {
1069 return NULL;
1070 }
1071
1072 return ccf;
1073 }
1074
1075
1076 static char *
ngx_core_module_init_conf(ngx_cycle_t * cycle,void * conf)1077 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
1078 {
1079 ngx_core_conf_t *ccf = conf;
1080
1081 ngx_conf_init_value(ccf->daemon, 1);
1082 ngx_conf_init_value(ccf->master, 1);
1083 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
1084 ngx_conf_init_msec_value(ccf->shutdown_timeout, 0);
1085
1086 ngx_conf_init_value(ccf->worker_processes, 1);
1087 ngx_conf_init_value(ccf->debug_points, 0);
1088
1089 #if (NGX_HAVE_FSTACK)
1090 ngx_conf_init_msec_value(ccf->schedule_timeout, 30);
1091 #endif
1092
1093 #if (NGX_HAVE_CPU_AFFINITY)
1094
1095 if (!ccf->cpu_affinity_auto
1096 && ccf->cpu_affinity_n
1097 && ccf->cpu_affinity_n != 1
1098 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
1099 {
1100 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
1101 "the number of \"worker_processes\" is not equal to "
1102 "the number of \"worker_cpu_affinity\" masks, "
1103 "using last mask for remaining worker processes");
1104 }
1105
1106 #endif
1107
1108
1109 if (ccf->pid.len == 0) {
1110 ngx_str_set(&ccf->pid, NGX_PID_PATH);
1111 }
1112
1113 if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
1114 return NGX_CONF_ERROR;
1115 }
1116
1117 ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
1118
1119 ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
1120 if (ccf->oldpid.data == NULL) {
1121 return NGX_CONF_ERROR;
1122 }
1123
1124 ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
1125 NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
1126
1127
1128 #if !(NGX_WIN32)
1129
1130 if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
1131 struct group *grp;
1132 struct passwd *pwd;
1133
1134 ngx_set_errno(0);
1135 pwd = getpwnam(NGX_USER);
1136 if (pwd == NULL) {
1137 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1138 "getpwnam(\"" NGX_USER "\") failed");
1139 return NGX_CONF_ERROR;
1140 }
1141
1142 ccf->username = NGX_USER;
1143 ccf->user = pwd->pw_uid;
1144
1145 ngx_set_errno(0);
1146 grp = getgrnam(NGX_GROUP);
1147 if (grp == NULL) {
1148 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1149 "getgrnam(\"" NGX_GROUP "\") failed");
1150 return NGX_CONF_ERROR;
1151 }
1152
1153 ccf->group = grp->gr_gid;
1154 }
1155
1156
1157 if (ccf->lock_file.len == 0) {
1158 ngx_str_set(&ccf->lock_file, NGX_LOCK_PATH);
1159 }
1160
1161 if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) != NGX_OK) {
1162 return NGX_CONF_ERROR;
1163 }
1164
1165 {
1166 ngx_str_t lock_file;
1167
1168 lock_file = cycle->old_cycle->lock_file;
1169
1170 if (lock_file.len) {
1171 lock_file.len--;
1172
1173 if (ccf->lock_file.len != lock_file.len
1174 || ngx_strncmp(ccf->lock_file.data, lock_file.data, lock_file.len)
1175 != 0)
1176 {
1177 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
1178 "\"lock_file\" could not be changed, ignored");
1179 }
1180
1181 cycle->lock_file.len = lock_file.len + 1;
1182 lock_file.len += sizeof(".accept");
1183
1184 cycle->lock_file.data = ngx_pstrdup(cycle->pool, &lock_file);
1185 if (cycle->lock_file.data == NULL) {
1186 return NGX_CONF_ERROR;
1187 }
1188
1189 } else {
1190 cycle->lock_file.len = ccf->lock_file.len + 1;
1191 cycle->lock_file.data = ngx_pnalloc(cycle->pool,
1192 ccf->lock_file.len + sizeof(".accept"));
1193 if (cycle->lock_file.data == NULL) {
1194 return NGX_CONF_ERROR;
1195 }
1196
1197 ngx_memcpy(ngx_cpymem(cycle->lock_file.data, ccf->lock_file.data,
1198 ccf->lock_file.len),
1199 ".accept", sizeof(".accept"));
1200 }
1201 }
1202
1203 #endif
1204
1205 return NGX_CONF_OK;
1206 }
1207
1208
1209 static char *
ngx_set_user(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1210 ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1211 {
1212 #if (NGX_WIN32)
1213
1214 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1215 "\"user\" is not supported, ignored");
1216
1217 return NGX_CONF_OK;
1218
1219 #else
1220
1221 ngx_core_conf_t *ccf = conf;
1222
1223 char *group;
1224 struct passwd *pwd;
1225 struct group *grp;
1226 ngx_str_t *value;
1227
1228 if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
1229 return "is duplicate";
1230 }
1231
1232 if (geteuid() != 0) {
1233 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1234 "the \"user\" directive makes sense only "
1235 "if the master process runs "
1236 "with super-user privileges, ignored");
1237 return NGX_CONF_OK;
1238 }
1239
1240 value = cf->args->elts;
1241
1242 ccf->username = (char *) value[1].data;
1243
1244 ngx_set_errno(0);
1245 pwd = getpwnam((const char *) value[1].data);
1246 if (pwd == NULL) {
1247 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
1248 "getpwnam(\"%s\") failed", value[1].data);
1249 return NGX_CONF_ERROR;
1250 }
1251
1252 ccf->user = pwd->pw_uid;
1253
1254 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
1255
1256 ngx_set_errno(0);
1257 grp = getgrnam(group);
1258 if (grp == NULL) {
1259 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
1260 "getgrnam(\"%s\") failed", group);
1261 return NGX_CONF_ERROR;
1262 }
1263
1264 ccf->group = grp->gr_gid;
1265
1266 return NGX_CONF_OK;
1267
1268 #endif
1269 }
1270
1271
1272 static char *
ngx_set_env(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1273 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1274 {
1275 ngx_core_conf_t *ccf = conf;
1276
1277 ngx_str_t *value, *var;
1278 ngx_uint_t i;
1279
1280 var = ngx_array_push(&ccf->env);
1281 if (var == NULL) {
1282 return NGX_CONF_ERROR;
1283 }
1284
1285 value = cf->args->elts;
1286 *var = value[1];
1287
1288 for (i = 0; i < value[1].len; i++) {
1289
1290 if (value[1].data[i] == '=') {
1291
1292 var->len = i;
1293
1294 return NGX_CONF_OK;
1295 }
1296 }
1297
1298 return NGX_CONF_OK;
1299 }
1300
1301
1302 static char *
ngx_set_priority(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1303 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1304 {
1305 ngx_core_conf_t *ccf = conf;
1306
1307 ngx_str_t *value;
1308 ngx_uint_t n, minus;
1309
1310 if (ccf->priority != 0) {
1311 return "is duplicate";
1312 }
1313
1314 value = cf->args->elts;
1315
1316 if (value[1].data[0] == '-') {
1317 n = 1;
1318 minus = 1;
1319
1320 } else if (value[1].data[0] == '+') {
1321 n = 1;
1322 minus = 0;
1323
1324 } else {
1325 n = 0;
1326 minus = 0;
1327 }
1328
1329 ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n);
1330 if (ccf->priority == NGX_ERROR) {
1331 return "invalid number";
1332 }
1333
1334 if (minus) {
1335 ccf->priority = -ccf->priority;
1336 }
1337
1338 return NGX_CONF_OK;
1339 }
1340
1341
1342 static char *
ngx_set_cpu_affinity(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1343 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1344 {
1345 #if (NGX_HAVE_CPU_AFFINITY)
1346 ngx_core_conf_t *ccf = conf;
1347
1348 u_char ch, *p;
1349 ngx_str_t *value;
1350 ngx_uint_t i, n;
1351 ngx_cpuset_t *mask;
1352
1353 if (ccf->cpu_affinity) {
1354 return "is duplicate";
1355 }
1356
1357 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(ngx_cpuset_t));
1358 if (mask == NULL) {
1359 return NGX_CONF_ERROR;
1360 }
1361
1362 ccf->cpu_affinity_n = cf->args->nelts - 1;
1363 ccf->cpu_affinity = mask;
1364
1365 value = cf->args->elts;
1366
1367 if (ngx_strcmp(value[1].data, "auto") == 0) {
1368
1369 if (cf->args->nelts > 3) {
1370 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1371 "invalid number of arguments in "
1372 "\"worker_cpu_affinity\" directive");
1373 return NGX_CONF_ERROR;
1374 }
1375
1376 ccf->cpu_affinity_auto = 1;
1377
1378 CPU_ZERO(&mask[0]);
1379 for (i = 0; i < (ngx_uint_t) ngx_min(ngx_ncpu, CPU_SETSIZE); i++) {
1380 CPU_SET(i, &mask[0]);
1381 }
1382
1383 n = 2;
1384
1385 } else {
1386 n = 1;
1387 }
1388
1389 for ( /* void */ ; n < cf->args->nelts; n++) {
1390
1391 if (value[n].len > CPU_SETSIZE) {
1392 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1393 "\"worker_cpu_affinity\" supports up to %d CPUs only",
1394 CPU_SETSIZE);
1395 return NGX_CONF_ERROR;
1396 }
1397
1398 i = 0;
1399 CPU_ZERO(&mask[n - 1]);
1400
1401 for (p = value[n].data + value[n].len - 1;
1402 p >= value[n].data;
1403 p--)
1404 {
1405 ch = *p;
1406
1407 if (ch == ' ') {
1408 continue;
1409 }
1410
1411 i++;
1412
1413 if (ch == '0') {
1414 continue;
1415 }
1416
1417 if (ch == '1') {
1418 CPU_SET(i - 1, &mask[n - 1]);
1419 continue;
1420 }
1421
1422 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1423 "invalid character \"%c\" in \"worker_cpu_affinity\"",
1424 ch);
1425 return NGX_CONF_ERROR;
1426 }
1427 }
1428
1429 #else
1430
1431 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1432 "\"worker_cpu_affinity\" is not supported "
1433 "on this platform, ignored");
1434 #endif
1435
1436 return NGX_CONF_OK;
1437 }
1438
1439
1440 ngx_cpuset_t *
ngx_get_cpu_affinity(ngx_uint_t n)1441 ngx_get_cpu_affinity(ngx_uint_t n)
1442 {
1443 #if (NGX_HAVE_CPU_AFFINITY)
1444 ngx_uint_t i, j;
1445 ngx_cpuset_t *mask;
1446 ngx_core_conf_t *ccf;
1447
1448 static ngx_cpuset_t result;
1449
1450 ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
1451 ngx_core_module);
1452
1453 if (ccf->cpu_affinity == NULL) {
1454 return NULL;
1455 }
1456
1457 if (ccf->cpu_affinity_auto) {
1458 mask = &ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
1459
1460 for (i = 0, j = n; /* void */ ; i++) {
1461
1462 if (CPU_ISSET(i % CPU_SETSIZE, mask) && j-- == 0) {
1463 break;
1464 }
1465
1466 if (i == CPU_SETSIZE && j == n) {
1467 /* empty mask */
1468 return NULL;
1469 }
1470
1471 /* void */
1472 }
1473
1474 CPU_ZERO(&result);
1475 CPU_SET(i % CPU_SETSIZE, &result);
1476
1477 return &result;
1478 }
1479
1480 if (ccf->cpu_affinity_n > n) {
1481 return &ccf->cpu_affinity[n];
1482 }
1483
1484 return &ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
1485
1486 #else
1487
1488 return NULL;
1489
1490 #endif
1491 }
1492
1493
1494 static char *
ngx_set_worker_processes(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1495 ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1496 {
1497 ngx_str_t *value;
1498 ngx_core_conf_t *ccf;
1499
1500 ccf = (ngx_core_conf_t *) conf;
1501
1502 if (ccf->worker_processes != NGX_CONF_UNSET) {
1503 return "is duplicate";
1504 }
1505
1506 value = cf->args->elts;
1507
1508 if (ngx_strcmp(value[1].data, "auto") == 0) {
1509 ccf->worker_processes = ngx_ncpu;
1510 return NGX_CONF_OK;
1511 }
1512
1513 ccf->worker_processes = ngx_atoi(value[1].data, value[1].len);
1514
1515 if (ccf->worker_processes == NGX_ERROR) {
1516 return "invalid value";
1517 }
1518
1519 return NGX_CONF_OK;
1520 }
1521
1522
1523 static char *
ngx_load_module(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1524 ngx_load_module(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1525 {
1526 #if (NGX_HAVE_DLOPEN)
1527 void *handle;
1528 char **names, **order;
1529 ngx_str_t *value, file;
1530 ngx_uint_t i;
1531 ngx_module_t *module, **modules;
1532 ngx_pool_cleanup_t *cln;
1533
1534 if (cf->cycle->modules_used) {
1535 return "is specified too late";
1536 }
1537
1538 value = cf->args->elts;
1539
1540 file = value[1];
1541
1542 if (ngx_conf_full_name(cf->cycle, &file, 0) != NGX_OK) {
1543 return NGX_CONF_ERROR;
1544 }
1545
1546 cln = ngx_pool_cleanup_add(cf->cycle->pool, 0);
1547 if (cln == NULL) {
1548 return NGX_CONF_ERROR;
1549 }
1550
1551 handle = ngx_dlopen(file.data);
1552 if (handle == NULL) {
1553 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1554 ngx_dlopen_n " \"%s\" failed (%s)",
1555 file.data, ngx_dlerror());
1556 return NGX_CONF_ERROR;
1557 }
1558
1559 cln->handler = ngx_unload_module;
1560 cln->data = handle;
1561
1562 modules = ngx_dlsym(handle, "ngx_modules");
1563 if (modules == NULL) {
1564 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1565 ngx_dlsym_n " \"%V\", \"%s\" failed (%s)",
1566 &value[1], "ngx_modules", ngx_dlerror());
1567 return NGX_CONF_ERROR;
1568 }
1569
1570 names = ngx_dlsym(handle, "ngx_module_names");
1571 if (names == NULL) {
1572 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1573 ngx_dlsym_n " \"%V\", \"%s\" failed (%s)",
1574 &value[1], "ngx_module_names", ngx_dlerror());
1575 return NGX_CONF_ERROR;
1576 }
1577
1578 order = ngx_dlsym(handle, "ngx_module_order");
1579
1580 for (i = 0; modules[i]; i++) {
1581 module = modules[i];
1582 module->name = names[i];
1583
1584 if (ngx_add_module(cf, &file, module, order) != NGX_OK) {
1585 return NGX_CONF_ERROR;
1586 }
1587
1588 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cf->log, 0, "module: %s i:%ui",
1589 module->name, module->index);
1590 }
1591
1592 return NGX_CONF_OK;
1593
1594 #else
1595
1596 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1597 "\"load_module\" is not supported "
1598 "on this platform");
1599 return NGX_CONF_ERROR;
1600
1601 #endif
1602 }
1603
1604
1605 #if (NGX_HAVE_DLOPEN)
1606
1607 static void
ngx_unload_module(void * data)1608 ngx_unload_module(void *data)
1609 {
1610 void *handle = data;
1611
1612 if (ngx_dlclose(handle) != 0) {
1613 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
1614 ngx_dlclose_n " failed (%s)", ngx_dlerror());
1615 }
1616 }
1617
1618 #endif
1619
1620 #if (NGX_HAVE_FSTACK)
1621 static
ngx_set_fstack_conf(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)1622 char *ngx_set_fstack_conf(ngx_conf_t *cf, ngx_command_t *cmd,
1623 void *conf)
1624 {
1625 char *p = conf;
1626
1627 ngx_str_t *field, *value;
1628 ngx_str_t full;
1629
1630 field = (ngx_str_t *)(p + cmd->offset);
1631
1632 if (field->data) {
1633 return "is duplicate";
1634 }
1635
1636 value = cf->args->elts;
1637 full = value[1];
1638
1639 if (ngx_conf_full_name(cf->cycle, &full, 1) != NGX_OK) {
1640 return NGX_CONF_ERROR;
1641 }
1642
1643 *field = full;
1644
1645 return NGX_CONF_OK;
1646 }
1647 #endif
1648
1649