blob: 06721269b7d42329ecff7c75fa8aa165baa0516f [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007#include <ngx_config.h>
Igor Sysoevb2620632003-01-10 06:09:20 +00008#include <ngx_core.h>
Igor Sysoevaa3436c2003-05-30 14:27:59 +00009#include <nginx.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000010
11
Igor Sysoevfc5a10a2004-03-09 19:47:07 +000012static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
Igor Sysoev924bd792004-10-11 15:07:03 +000013static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
14static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
Igor Sysoev43f13192004-04-12 16:38:09 +000015static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
16static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
Igor Sysoev03420a62004-01-20 20:40:08 +000017static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev8885f872007-01-20 19:26:48 +000018static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev42b12b32004-12-02 18:40:46 +000019static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000020static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
21 void *conf);
Igor Sysoev2b542382002-08-20 14:48:28 +000022
Igor Sysoev7f125082003-07-11 15:17:50 +000023
Igor Sysoev1b735832004-11-11 14:07:14 +000024static ngx_conf_enum_t ngx_debug_points[] = {
25 { ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
26 { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
27 { ngx_null_string, 0 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000028};
Igor Sysoev1b735832004-11-11 14:07:14 +000029
30
Igor Sysoev7f125082003-07-11 15:17:50 +000031static ngx_command_t ngx_core_commands[] = {
32
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000033 { ngx_string("daemon"),
Igor Sysoevc2068d02005-10-19 12:33:58 +000034 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
Igor Sysoev43f13192004-04-12 16:38:09 +000035 ngx_conf_set_flag_slot,
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000036 0,
37 offsetof(ngx_core_conf_t, daemon),
38 NULL },
Igor Sysoev7f125082003-07-11 15:17:50 +000039
Igor Sysoevf0677992004-01-08 17:08:10 +000040 { ngx_string("master_process"),
Igor Sysoevc2068d02005-10-19 12:33:58 +000041 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
Igor Sysoev43f13192004-04-12 16:38:09 +000042 ngx_conf_set_flag_slot,
Igor Sysoev3c3ca172004-01-05 20:55:48 +000043 0,
Igor Sysoevf0677992004-01-08 17:08:10 +000044 offsetof(ngx_core_conf_t, master),
Igor Sysoev3c3ca172004-01-05 20:55:48 +000045 NULL },
46
Igor Sysoevc2068d02005-10-19 12:33:58 +000047 { ngx_string("timer_resolution"),
48 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
49 ngx_conf_set_msec_slot,
50 0,
51 offsetof(ngx_core_conf_t, timer_resolution),
52 NULL },
53
Igor Sysoev31eb8c02005-09-23 11:02:22 +000054 { ngx_string("pid"),
55 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
56 ngx_conf_set_str_slot,
57 0,
58 offsetof(ngx_core_conf_t, pid),
59 NULL },
60
Igor Sysoevffe71442006-02-08 15:33:12 +000061 { ngx_string("lock_file"),
62 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
63 ngx_conf_set_str_slot,
64 0,
65 offsetof(ngx_core_conf_t, lock_file),
66 NULL },
67
Igor Sysoeva741f8d2004-03-30 20:31:58 +000068 { ngx_string("worker_processes"),
Igor Sysoev43f13192004-04-12 16:38:09 +000069 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
70 ngx_conf_set_num_slot,
Igor Sysoeva741f8d2004-03-30 20:31:58 +000071 0,
72 offsetof(ngx_core_conf_t, worker_processes),
73 NULL },
74
Igor Sysoev1b735832004-11-11 14:07:14 +000075 { ngx_string("debug_points"),
76 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
77 ngx_conf_set_enum_slot,
78 0,
79 offsetof(ngx_core_conf_t, debug_points),
80 &ngx_debug_points },
81
Igor Sysoev31eb8c02005-09-23 11:02:22 +000082 { ngx_string("user"),
83 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
84 ngx_set_user,
85 0,
86 0,
87 NULL },
88
89 { ngx_string("worker_priority"),
90 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
91 ngx_set_priority,
92 0,
93 0,
94 NULL },
95
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000096 { ngx_string("worker_cpu_affinity"),
97 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
98 ngx_set_cpu_affinity,
99 0,
100 0,
101 NULL },
102
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000103 { ngx_string("worker_rlimit_nofile"),
104 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
105 ngx_conf_set_num_slot,
106 0,
107 offsetof(ngx_core_conf_t, rlimit_nofile),
108 NULL },
109
Igor Sysoev24025022005-12-16 15:07:08 +0000110 { ngx_string("worker_rlimit_core"),
111 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
Igor Sysoev888ea842006-12-24 18:13:06 +0000112 ngx_conf_set_size_slot,
Igor Sysoev24025022005-12-16 15:07:08 +0000113 0,
114 offsetof(ngx_core_conf_t, rlimit_core),
115 NULL },
116
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000117 { ngx_string("worker_rlimit_sigpending"),
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_sigpending),
122 NULL },
123
124 { ngx_string("working_directory"),
125 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
126 ngx_conf_set_str_slot,
127 0,
128 offsetof(ngx_core_conf_t, working_directory),
129 NULL },
130
Igor Sysoev8885f872007-01-20 19:26:48 +0000131 { ngx_string("env"),
132 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
133 ngx_set_env,
134 0,
135 0,
136 NULL },
137
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000138#if (NGX_THREADS)
139
140 { ngx_string("worker_threads"),
141 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
142 ngx_conf_set_num_slot,
143 0,
144 offsetof(ngx_core_conf_t, worker_threads),
145 NULL },
146
147 { ngx_string("thread_stack_size"),
148 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
149 ngx_conf_set_size_slot,
150 0,
151 offsetof(ngx_core_conf_t, thread_stack_size),
152 NULL },
153
154#endif
155
Igor Sysoev2b58fbf2003-12-09 15:08:11 +0000156 ngx_null_command
Igor Sysoev7f125082003-07-11 15:17:50 +0000157};
158
159
Igor Sysoev43f13192004-04-12 16:38:09 +0000160static ngx_core_module_t ngx_core_module_ctx = {
161 ngx_string("core"),
162 ngx_core_module_create_conf,
163 ngx_core_module_init_conf
164};
165
166
Igor Sysoev7f125082003-07-11 15:17:50 +0000167ngx_module_t ngx_core_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000168 NGX_MODULE_V1,
Igor Sysoev43f13192004-04-12 16:38:09 +0000169 &ngx_core_module_ctx, /* module context */
Igor Sysoev7f125082003-07-11 15:17:50 +0000170 ngx_core_commands, /* module directives */
171 NGX_CORE_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000172 NULL, /* init master */
Igor Sysoev43f13192004-04-12 16:38:09 +0000173 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000174 NULL, /* init process */
175 NULL, /* init thread */
176 NULL, /* exit thread */
177 NULL, /* exit process */
178 NULL, /* exit master */
179 NGX_MODULE_V1_PADDING
Igor Sysoev7f125082003-07-11 15:17:50 +0000180};
181
182
Igor Sysoev3f4685f2004-04-25 20:13:21 +0000183ngx_uint_t ngx_max_module;
Igor Sysoeva9830112003-05-19 16:39:14 +0000184
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000185static ngx_uint_t ngx_show_version;
Igor Sysoevb8fec512006-12-23 19:54:21 +0000186static ngx_uint_t ngx_show_configure;
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000187
Igor Sysoev8885f872007-01-20 19:26:48 +0000188static char **ngx_os_environ;
Igor Sysoevae33d012006-01-17 20:04:32 +0000189
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000190
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000191int ngx_cdecl
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000192main(int argc, char *const *argv)
Igor Sysoev6abfde62003-07-01 15:00:03 +0000193{
Igor Sysoevf2884e12008-11-11 16:17:45 +0000194 char *p;
195 ssize_t n;
Igor Sysoev924bd792004-10-11 15:07:03 +0000196 ngx_int_t i;
197 ngx_log_t *log;
198 ngx_cycle_t *cycle, init_cycle;
199 ngx_core_conf_t *ccf;
Igor Sysoev6abfde62003-07-01 15:00:03 +0000200
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000201#if (NGX_FREEBSD)
Igor Sysoev62260f22003-12-05 17:07:27 +0000202 ngx_debug_init();
Igor Sysoev9d639522003-07-07 06:11:50 +0000203#endif
204
Igor Sysoevbc5c2872003-07-02 05:01:53 +0000205 /* TODO */ ngx_max_sockets = -1;
Igor Sysoev6abfde62003-07-01 15:00:03 +0000206
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000207 ngx_time_init();
Igor Sysoevf0677992004-01-08 17:08:10 +0000208
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000209#if (NGX_PCRE)
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000210 ngx_regex_init();
Igor Sysoeve89c4582003-12-19 08:15:11 +0000211#endif
Igor Sysoev562e53e2003-11-13 06:14:05 +0000212
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000213 ngx_pid = ngx_getpid();
Igor Sysoev6abfde62003-07-01 15:00:03 +0000214
Igor Sysoevc1571722005-03-19 12:38:37 +0000215 log = ngx_log_init();
216 if (log == NULL) {
Igor Sysoevff8da912004-09-29 16:00:49 +0000217 return 1;
218 }
Igor Sysoev55168f62004-09-28 20:09:22 +0000219
Igor Sysoeve5733802005-09-08 14:36:09 +0000220 /* STUB */
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000221#if (NGX_OPENSSL)
222 ngx_ssl_init(log);
223#endif
224
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000225 /* init_cycle->log is required for signal handlers and ngx_getopt() */
Igor Sysoeve89c4582003-12-19 08:15:11 +0000226
227 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
228 init_cycle.log = log;
229 ngx_cycle = &init_cycle;
230
Igor Sysoevc1571722005-03-19 12:38:37 +0000231 init_cycle.pool = ngx_create_pool(1024, log);
232 if (init_cycle.pool == NULL) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000233 return 1;
234 }
235
Igor Sysoevffe71442006-02-08 15:33:12 +0000236 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
Igor Sysoev924bd792004-10-11 15:07:03 +0000237 return 1;
238 }
239
Igor Sysoevffe71442006-02-08 15:33:12 +0000240 if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) {
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000241 return 1;
242 }
243
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000244 if (ngx_show_version) {
Igor Sysoevf2884e12008-11-11 16:17:45 +0000245
246 p = "nginx version: " NGINX_VER CRLF;
247 n = sizeof("nginx version: " NGINX_VER CRLF) - 1;
248
249 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
250 return 1;
251 }
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000252
Igor Sysoevb8fec512006-12-23 19:54:21 +0000253 if (ngx_show_configure) {
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000254#ifdef NGX_COMPILER
Igor Sysoevf2884e12008-11-11 16:17:45 +0000255 p = "built by " NGX_COMPILER CRLF;
256 n = sizeof("built by " NGX_COMPILER CRLF) - 1;
257
258 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
259 return 1;
260 }
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000261#endif
Igor Sysoevb8fec512006-12-23 19:54:21 +0000262
Igor Sysoevf2884e12008-11-11 16:17:45 +0000263 p = "configure arguments: " NGX_CONFIGURE CRLF;
264 n = sizeof("configure arguments :" NGX_CONFIGURE CRLF) - 1;
Igor Sysoevb8fec512006-12-23 19:54:21 +0000265
Igor Sysoevf2884e12008-11-11 16:17:45 +0000266 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
267 return 1;
268 }
Igor Sysoev674ebb02006-12-24 13:11:41 +0000269 }
Igor Sysoevb8fec512006-12-23 19:54:21 +0000270
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000271 if (!ngx_test_config) {
272 return 0;
273 }
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000274 }
275
Igor Sysoev8035fd22004-10-01 15:53:53 +0000276 if (ngx_test_config) {
277 log->log_level = NGX_LOG_INFO;
Igor Sysoev8035fd22004-10-01 15:53:53 +0000278 }
279
Igor Sysoevffe71442006-02-08 15:33:12 +0000280 if (ngx_os_init(log) != NGX_OK) {
Igor Sysoev55168f62004-09-28 20:09:22 +0000281 return 1;
282 }
283
Igor Sysoevac7586e2007-12-07 20:19:41 +0000284 /*
285 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
286 */
Igor Sysoevc6906752006-12-15 21:31:03 +0000287
Igor Sysoevac7586e2007-12-07 20:19:41 +0000288 if (ngx_crc32_table_init() != NGX_OK) {
Igor Sysoevc6906752006-12-15 21:31:03 +0000289 return 1;
290 }
291
Igor Sysoevffe71442006-02-08 15:33:12 +0000292 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000293 return 1;
294 }
295
Igor Sysoev68df19d2004-04-15 15:34:36 +0000296 ngx_max_module = 0;
297 for (i = 0; ngx_modules[i]; i++) {
298 ngx_modules[i]->index = ngx_max_module++;
299 }
300
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000301 cycle = ngx_init_cycle(&init_cycle);
Igor Sysoevbc5c2872003-07-02 05:01:53 +0000302 if (cycle == NULL) {
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000303 if (ngx_test_config) {
304 ngx_log_error(NGX_LOG_EMERG, log, 0,
Igor Sysoev208eed22005-10-07 13:30:52 +0000305 "the configuration file %s test failed",
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000306 init_cycle.conf_file.data);
307 }
308
Igor Sysoev6abfde62003-07-01 15:00:03 +0000309 return 1;
310 }
311
Igor Sysoev68df19d2004-04-15 15:34:36 +0000312 if (ngx_test_config) {
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000313 ngx_log_error(NGX_LOG_INFO, log, 0,
Igor Sysoev208eed22005-10-07 13:30:52 +0000314 "the configuration file %s was tested successfully",
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000315 cycle->conf_file.data);
Igor Sysoev68df19d2004-04-15 15:34:36 +0000316 return 0;
317 }
318
Igor Sysoevaad1b892004-10-03 20:02:06 +0000319 ngx_os_status(cycle->log);
320
Igor Sysoev9d639522003-07-07 06:11:50 +0000321 ngx_cycle = cycle;
Igor Sysoev340b03b2003-07-04 15:10:33 +0000322
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000323 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
324
Igor Sysoev43f13192004-04-12 16:38:09 +0000325 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000326
Igor Sysoev1b735832004-11-11 14:07:14 +0000327#if (NGX_WIN32)
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000328
329#if 0
Igor Sysoeve0207bb2004-06-23 15:18:17 +0000330
331 TODO:
332
Igor Sysoev43f13192004-04-12 16:38:09 +0000333 if (ccf->run_as_service) {
Igor Sysoevffe71442006-02-08 15:33:12 +0000334 if (ngx_service(cycle->log) != NGX_OK) {
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000335 return 1;
336 }
337
338 return 0;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000339 }
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000340#endif
341
342#else
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000343
Igor Sysoevffe71442006-02-08 15:33:12 +0000344 if (ngx_init_signals(cycle->log) != NGX_OK) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000345 return 1;
346 }
347
Igor Sysoev43f13192004-04-12 16:38:09 +0000348 if (!ngx_inherited && ccf->daemon) {
Igor Sysoevffe71442006-02-08 15:33:12 +0000349 if (ngx_daemon(cycle->log) != NGX_OK) {
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000350 return 1;
351 }
Igor Sysoeve0207bb2004-06-23 15:18:17 +0000352
353 ngx_daemonized = 1;
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000354 }
355
Igor Sysoevffe71442006-02-08 15:33:12 +0000356 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000357 return 1;
358 }
359
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000360#endif
Igor Sysoev6abfde62003-07-01 15:00:03 +0000361
Igor Sysoev630ad0c2004-04-16 05:14:16 +0000362 if (ngx_process == NGX_PROCESS_MASTER) {
Igor Sysoev924bd792004-10-11 15:07:03 +0000363 ngx_master_process_cycle(cycle);
Igor Sysoev630ad0c2004-04-16 05:14:16 +0000364
365 } else {
Igor Sysoev924bd792004-10-11 15:07:03 +0000366 ngx_single_process_cycle(cycle);
Igor Sysoev630ad0c2004-04-16 05:14:16 +0000367 }
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000368
369 return 0;
370}
371
372
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000373static ngx_int_t
374ngx_add_inherited_sockets(ngx_cycle_t *cycle)
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000375{
Igor Sysoevc1571722005-03-19 12:38:37 +0000376 u_char *p, *v, *inherited;
377 ngx_int_t s;
378 ngx_listening_t *ls;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000379
Igor Sysoev10a543a2004-03-16 07:10:12 +0000380 inherited = (u_char *) getenv(NGINX_VAR);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000381
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000382 if (inherited == NULL) {
383 return NGX_OK;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000384 }
385
Igor Sysoev1b735832004-11-11 14:07:14 +0000386 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000387 "using inherited sockets from \"%s\"", inherited);
388
Igor Sysoev1b735832004-11-11 14:07:14 +0000389 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
Igor Sysoev852cde42006-10-06 13:02:23 +0000390 sizeof(ngx_listening_t))
391 == NGX_ERROR)
Igor Sysoev1b735832004-11-11 14:07:14 +0000392 {
393 return NGX_ERROR;
394 }
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000395
396 for (p = inherited, v = p; *p; p++) {
397 if (*p == ':' || *p == ';') {
398 s = ngx_atoi(v, p - v);
399 if (s == NGX_ERROR) {
400 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
Igor Sysoeve5035392005-08-30 10:55:07 +0000401 "invalid socket number \"%s\" in " NGINX_VAR
402 " environment variable, ignoring the rest"
403 " of the variable", v);
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000404 break;
405 }
406
Igor Sysoev10a543a2004-03-16 07:10:12 +0000407 v = p + 1;
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000408
Igor Sysoevc1571722005-03-19 12:38:37 +0000409 ls = ngx_array_push(&cycle->listening);
410 if (ls == NULL) {
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000411 return NGX_ERROR;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000412 }
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000413
Igor Sysoevb145b062005-06-15 18:33:41 +0000414 ngx_memzero(ls, sizeof(ngx_listening_t));
415
Igor Sysoevc1571722005-03-19 12:38:37 +0000416 ls->fd = (ngx_socket_t) s;
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000417 }
418 }
419
420 ngx_inherited = 1;
421
422 return ngx_set_inherited_sockets(cycle);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000423}
424
425
Igor Sysoev8885f872007-01-20 19:26:48 +0000426char **
427ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
428{
429 char **p, **env;
430 ngx_str_t *var;
431 ngx_uint_t i, n;
432 ngx_core_conf_t *ccf;
433
434 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
435
Igor Sysoev95ec2582007-01-25 21:48:28 +0000436 if (last == NULL && ccf->environment) {
437 return ccf->environment;
Igor Sysoev8885f872007-01-20 19:26:48 +0000438 }
439
440 var = ccf->env.elts;
441
442 for (i = 0; i < ccf->env.nelts; i++) {
443 if (ngx_strcmp(var[i].data, "TZ") == 0
444 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
445 {
446 goto tz_found;
447 }
448 }
449
450 var = ngx_array_push(&ccf->env);
Igor Sysoev43ae46f2007-07-19 19:08:22 +0000451 if (var == NULL) {
452 return NULL;
453 }
Igor Sysoev8885f872007-01-20 19:26:48 +0000454
455 var->len = 2;
456 var->data = (u_char *) "TZ";
457
458 var = ccf->env.elts;
459
460tz_found:
461
Igor Sysoev95ec2582007-01-25 21:48:28 +0000462 n = 0;
463
Igor Sysoev8885f872007-01-20 19:26:48 +0000464 for (i = 0; i < ccf->env.nelts; i++) {
465
466 if (var[i].data[var[i].len] == '=') {
467 n++;
468 continue;
469 }
470
471 for (p = ngx_os_environ; *p; p++) {
472
473 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
474 && (*p)[var[i].len] == '=')
475 {
476 n++;
477 break;
478 }
479 }
480 }
481
482 if (last) {
Igor Sysoev95ec2582007-01-25 21:48:28 +0000483 env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle->log);
Igor Sysoev8885f872007-01-20 19:26:48 +0000484 *last = n;
Igor Sysoev8885f872007-01-20 19:26:48 +0000485
486 } else {
487 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
488 }
489
490 if (env == NULL) {
491 return NULL;
492 }
493
494 n = 0;
495
496 for (i = 0; i < ccf->env.nelts; i++) {
497
498 if (var[i].data[var[i].len] == '=') {
499 env[n++] = (char *) var[i].data;
500 continue;
501 }
502
503 for (p = ngx_os_environ; *p; p++) {
504
505 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
506 && (*p)[var[i].len] == '=')
507 {
508 env[n++] = *p;
509 break;
510 }
511 }
512 }
513
514 env[n] = NULL;
515
516 if (last == NULL) {
517 ccf->environment = env;
518 environ = env;
519 }
520
521 return env;
522}
523
524
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000525ngx_pid_t
526ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000527{
Igor Sysoev8885f872007-01-20 19:26:48 +0000528 char **env, *var;
529 u_char *p;
530 ngx_uint_t i, n;
531 ngx_pid_t pid;
532 ngx_exec_ctx_t ctx;
533 ngx_core_conf_t *ccf;
534 ngx_listening_t *ls;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000535
536 ctx.path = argv[0];
537 ctx.name = "new binary process";
538 ctx.argv = argv;
539
Igor Sysoev8885f872007-01-20 19:26:48 +0000540 n = 2;
541 env = ngx_set_environment(cycle, &n);
542 if (env == NULL) {
543 return NGX_INVALID_PID;
544 }
545
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000546 var = ngx_alloc(sizeof(NGINX_VAR)
Igor Sysoevc1571722005-03-19 12:38:37 +0000547 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000548 cycle->log);
549
Igor Sysoev1b735832004-11-11 14:07:14 +0000550 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000551
552 ls = cycle->listening.elts;
553 for (i = 0; i < cycle->listening.nelts; i++) {
Igor Sysoev1b735832004-11-11 14:07:14 +0000554 p = ngx_sprintf(p, "%ud;", ls[i].fd);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000555 }
556
Igor Sysoev1b735832004-11-11 14:07:14 +0000557 *p = '\0';
558
Igor Sysoev8885f872007-01-20 19:26:48 +0000559 env[n++] = var;
Igor Sysoev924bd792004-10-11 15:07:03 +0000560
561#if (NGX_SETPROCTITLE_USES_ENV)
562
Igor Sysoev1b735832004-11-11 14:07:14 +0000563 /* allocate the spare 300 bytes for the new binary process title */
Igor Sysoev924bd792004-10-11 15:07:03 +0000564
Igor Sysoev8885f872007-01-20 19:26:48 +0000565 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
566 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
567 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
568 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
569 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Igor Sysoev924bd792004-10-11 15:07:03 +0000570
571#endif
572
Igor Sysoev8885f872007-01-20 19:26:48 +0000573 env[n] = NULL;
574
575#if (NGX_DEBUG)
576 {
577 char **e;
578 for (e = env; *e; e++) {
579 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
580 }
581 }
582#endif
583
584 ctx.envp = (char *const *) env;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000585
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000586 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
587
Igor Sysoev8885f872007-01-20 19:26:48 +0000588 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000589 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
590 ngx_rename_file_n " %s to %s failed "
591 "before executing new binary process \"%s\"",
592 ccf->pid.data, ccf->oldpid.data, argv[0]);
593
Igor Sysoev8885f872007-01-20 19:26:48 +0000594 ngx_free(env);
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000595 ngx_free(var);
596
597 return NGX_INVALID_PID;
598 }
599
Igor Sysoev6a930452004-03-04 16:34:23 +0000600 pid = ngx_execute(cycle, &ctx);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000601
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000602 if (pid == NGX_INVALID_PID) {
Igor Sysoev8885f872007-01-20 19:26:48 +0000603 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000604 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev8885f872007-01-20 19:26:48 +0000605 ngx_rename_file_n " %s back to %s failed after "
606 "the try to execute the new binary process \"%s\"",
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000607 ccf->oldpid.data, ccf->pid.data, argv[0]);
608 }
609 }
610
Igor Sysoev8885f872007-01-20 19:26:48 +0000611 ngx_free(env);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000612 ngx_free(var);
Igor Sysoev80340f02004-01-13 21:33:59 +0000613
614 return pid;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000615}
616
617
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000618static ngx_int_t
619ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000620{
621 ngx_int_t i;
622
Igor Sysoev924bd792004-10-11 15:07:03 +0000623 for (i = 1; i < argc; i++) {
624 if (argv[i][0] != '-') {
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000625 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
Igor Sysoev924bd792004-10-11 15:07:03 +0000626 "invalid option: \"%s\"", argv[i]);
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000627 return NGX_ERROR;
628 }
629
Igor Sysoev924bd792004-10-11 15:07:03 +0000630 switch (argv[i][1]) {
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000631
Igor Sysoev4ecb4d72006-04-21 12:06:44 +0000632 case 'v':
633 ngx_show_version = 1;
634 break;
635
Igor Sysoevb8fec512006-12-23 19:54:21 +0000636 case 'V':
637 ngx_show_version = 1;
638 ngx_show_configure = 1;
639 break;
640
Igor Sysoev68df19d2004-04-15 15:34:36 +0000641 case 't':
642 ngx_test_config = 1;
643 break;
644
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000645 case 'c':
Igor Sysoev924bd792004-10-11 15:07:03 +0000646 if (argv[i + 1] == NULL) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000647 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
Igor Sysoevb4fbdcf2008-06-30 12:35:16 +0000648 "the option \"-c\" requires file name");
Igor Sysoevcccc5522004-04-14 20:34:05 +0000649 return NGX_ERROR;
650 }
651
Igor Sysoev924bd792004-10-11 15:07:03 +0000652 cycle->conf_file.data = (u_char *) argv[++i];
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000653 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
654 break;
655
Igor Sysoevb4fbdcf2008-06-30 12:35:16 +0000656 case 'g':
657 if (argv[i + 1] == NULL) {
658 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
659 "the option \"-g\" requires parameter");
660 return NGX_ERROR;
661 }
662
663 cycle->conf_param.data = (u_char *) argv[++i];
664 cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);
665 break;
666
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000667 default:
668 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
Igor Sysoev924bd792004-10-11 15:07:03 +0000669 "invalid option: \"%s\"", argv[i]);
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000670 return NGX_ERROR;
671 }
672 }
673
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000674 if (cycle->conf_file.data == NULL) {
Igor Sysoev090849d2004-05-18 20:28:54 +0000675 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
676 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000677 }
678
679 return NGX_OK;
680}
681
682
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000683static ngx_int_t
684ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
Igor Sysoev924bd792004-10-11 15:07:03 +0000685{
Igor Sysoevc1571722005-03-19 12:38:37 +0000686#if (NGX_FREEBSD)
687
688 ngx_os_argv = (char **) argv;
689 ngx_argc = argc;
690 ngx_argv = (char **) argv;
691
692#else
Igor Sysoev924bd792004-10-11 15:07:03 +0000693 size_t len;
694 ngx_int_t i;
695
696 ngx_os_argv = (char **) argv;
Igor Sysoev924bd792004-10-11 15:07:03 +0000697 ngx_argc = argc;
698
Igor Sysoevc1571722005-03-19 12:38:37 +0000699 ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log);
700 if (ngx_argv == NULL) {
Igor Sysoev924bd792004-10-11 15:07:03 +0000701 return NGX_ERROR;
702 }
703
704 for (i = 0; i < argc; i++) {
705 len = ngx_strlen(argv[i]) + 1;
706
Igor Sysoevc1571722005-03-19 12:38:37 +0000707 ngx_argv[i] = ngx_alloc(len, cycle->log);
708 if (ngx_argv[i] == NULL) {
Igor Sysoev924bd792004-10-11 15:07:03 +0000709 return NGX_ERROR;
710 }
711
Igor Sysoev4959ec42005-05-23 12:07:45 +0000712 (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
Igor Sysoev924bd792004-10-11 15:07:03 +0000713 }
714
715 ngx_argv[i] = NULL;
716
717#endif
718
Igor Sysoev8885f872007-01-20 19:26:48 +0000719 ngx_os_environ = environ;
720
Igor Sysoev924bd792004-10-11 15:07:03 +0000721 return NGX_OK;
722}
723
724
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000725static void *
726ngx_core_module_create_conf(ngx_cycle_t *cycle)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000727{
728 ngx_core_conf_t *ccf;
729
Igor Sysoevc1571722005-03-19 12:38:37 +0000730 ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));
731 if (ccf == NULL) {
Igor Sysoev43f13192004-04-12 16:38:09 +0000732 return NULL;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000733 }
Igor Sysoev02025fd2005-01-18 13:03:58 +0000734
735 /*
736 * set by pcalloc()
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000737 *
Igor Sysoev02025fd2005-01-18 13:03:58 +0000738 * ccf->pid = NULL;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000739 * ccf->oldpid = NULL;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000740 * ccf->priority = 0;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000741 * ccf->cpu_affinity_n = 0;
742 * ccf->cpu_affinity = NULL;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000743 */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000744
Igor Sysoev03420a62004-01-20 20:40:08 +0000745 ccf->daemon = NGX_CONF_UNSET;
746 ccf->master = NGX_CONF_UNSET;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000747 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
748
Igor Sysoeva741f8d2004-03-30 20:31:58 +0000749 ccf->worker_processes = NGX_CONF_UNSET;
Igor Sysoev1b735832004-11-11 14:07:14 +0000750 ccf->debug_points = NGX_CONF_UNSET;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000751
752 ccf->rlimit_nofile = NGX_CONF_UNSET;
Igor Sysoev888ea842006-12-24 18:13:06 +0000753 ccf->rlimit_core = NGX_CONF_UNSET_SIZE;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000754 ccf->rlimit_sigpending = NGX_CONF_UNSET;
755
Igor Sysoev4959ec42005-05-23 12:07:45 +0000756 ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
757 ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000758
Igor Sysoev1b735832004-11-11 14:07:14 +0000759#if (NGX_THREADS)
760 ccf->worker_threads = NGX_CONF_UNSET;
761 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
762#endif
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000763
Igor Sysoev8885f872007-01-20 19:26:48 +0000764 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
765 != NGX_OK)
766 {
767 return NULL;
768 }
769
Igor Sysoev43f13192004-04-12 16:38:09 +0000770 return ccf;
771}
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000772
Igor Sysoev43f13192004-04-12 16:38:09 +0000773
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000774static char *
775ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
Igor Sysoev43f13192004-04-12 16:38:09 +0000776{
777 ngx_core_conf_t *ccf = conf;
778
779 ngx_conf_init_value(ccf->daemon, 1);
780 ngx_conf_init_value(ccf->master, 1);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000781 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
782
Igor Sysoev43f13192004-04-12 16:38:09 +0000783 ngx_conf_init_value(ccf->worker_processes, 1);
Igor Sysoev1b735832004-11-11 14:07:14 +0000784 ngx_conf_init_value(ccf->debug_points, 0);
Igor Sysoev43f13192004-04-12 16:38:09 +0000785
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000786#if (NGX_HAVE_SCHED_SETAFFINITY)
787
788 if (ccf->cpu_affinity_n
789 && ccf->cpu_affinity_n != 1
790 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
791 {
792 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
793 "number of the \"worker_processes\" is not equal to "
794 "the number of the \"worker_cpu_affinity\" mask, "
795 "using last mask for remaining worker processes");
796 }
797
798#endif
799
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000800#if (NGX_THREADS)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000801
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000802 ngx_conf_init_value(ccf->worker_threads, 0);
803 ngx_threads_n = ccf->worker_threads;
804 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000805
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000806#endif
807
Igor Sysoev1b735832004-11-11 14:07:14 +0000808#if !(NGX_WIN32)
Igor Sysoev43f13192004-04-12 16:38:09 +0000809
Igor Sysoev4959ec42005-05-23 12:07:45 +0000810 if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
Igor Sysoev86ef6aa2007-12-10 12:09:51 +0000811 struct group *grp;
812 struct passwd *pwd;
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000813
Igor Sysoevd2d6bd52007-07-11 10:46:21 +0000814 ngx_set_errno(0);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000815 pwd = getpwnam(NGX_USER);
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000816 if (pwd == NULL) {
817 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000818 "getpwnam(\"" NGX_USER "\") failed");
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000819 return NGX_CONF_ERROR;
820 }
821
Igor Sysoev42b12b32004-12-02 18:40:46 +0000822 ccf->username = NGX_USER;
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000823 ccf->user = pwd->pw_uid;
824
Igor Sysoevd2d6bd52007-07-11 10:46:21 +0000825 ngx_set_errno(0);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000826 grp = getgrnam(NGX_GROUP);
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000827 if (grp == NULL) {
828 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000829 "getgrnam(\"" NGX_GROUP "\") failed");
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000830 return NGX_CONF_ERROR;
831 }
832
833 ccf->group = grp->gr_gid;
834 }
Igor Sysoev43f13192004-04-12 16:38:09 +0000835
836 if (ccf->pid.len == 0) {
Igor Sysoev090849d2004-05-18 20:28:54 +0000837 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
Igor Sysoev924bd792004-10-11 15:07:03 +0000838 ccf->pid.data = (u_char *) NGX_PID_PATH;
Igor Sysoev43f13192004-04-12 16:38:09 +0000839 }
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000840
Igor Sysoeva1df4162007-07-29 18:05:45 +0000841 if (ngx_conf_full_name(cycle, &ccf->pid, 0) == NGX_ERROR) {
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000842 return NGX_CONF_ERROR;
843 }
844
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000845 ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000846
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000847 ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000848 if (ccf->oldpid.data == NULL) {
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000849 return NGX_CONF_ERROR;
850 }
851
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000852 ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
853 NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
Igor Sysoev6d2a14a2004-09-27 16:03:21 +0000854
Igor Sysoev67cd3362006-11-20 08:51:45 +0000855
Igor Sysoevffe71442006-02-08 15:33:12 +0000856 if (ccf->lock_file.len == 0) {
857 ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
858 ccf->lock_file.data = (u_char *) NGX_LOCK_PATH;
859 }
860
Igor Sysoeva1df4162007-07-29 18:05:45 +0000861 if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) == NGX_ERROR) {
Igor Sysoevffe71442006-02-08 15:33:12 +0000862 return NGX_CONF_ERROR;
863 }
864
Igor Sysoev86ef6aa2007-12-10 12:09:51 +0000865 {
866 ngx_str_t lock_file;
867
Igor Sysoev67cd3362006-11-20 08:51:45 +0000868 lock_file = cycle->old_cycle->lock_file;
869
870 if (lock_file.len) {
871 lock_file.len--;
872
873 if (ccf->lock_file.len != lock_file.len
874 || ngx_strncmp(ccf->lock_file.data, lock_file.data, lock_file.len)
875 != 0)
876 {
877 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
878 "\"lock_file\" could not be changed, ignored");
879 }
880
881 cycle->lock_file.len = lock_file.len + 1;
882 lock_file.len += sizeof(".accept");
883
884 cycle->lock_file.data = ngx_pstrdup(cycle->pool, &lock_file);
885 if (cycle->lock_file.data == NULL) {
886 return NGX_CONF_ERROR;
887 }
888
889 } else {
890 cycle->lock_file.len = ccf->lock_file.len + 1;
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000891 cycle->lock_file.data = ngx_pnalloc(cycle->pool,
Igor Sysoev67cd3362006-11-20 08:51:45 +0000892 ccf->lock_file.len + sizeof(".accept"));
893 if (cycle->lock_file.data == NULL) {
894 return NGX_CONF_ERROR;
895 }
896
897 ngx_memcpy(ngx_cpymem(cycle->lock_file.data, ccf->lock_file.data,
898 ccf->lock_file.len),
899 ".accept", sizeof(".accept"));
900 }
Igor Sysoev86ef6aa2007-12-10 12:09:51 +0000901 }
Igor Sysoev67cd3362006-11-20 08:51:45 +0000902
Igor Sysoev43f13192004-04-12 16:38:09 +0000903#endif
904
905 return NGX_CONF_OK;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000906}
Igor Sysoev03420a62004-01-20 20:40:08 +0000907
908
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000909static char *
910ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev03420a62004-01-20 20:40:08 +0000911{
Igor Sysoev1b735832004-11-11 14:07:14 +0000912#if (NGX_WIN32)
Igor Sysoeva5362982004-03-04 07:04:55 +0000913
914 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
915 "\"user\" is not supported, ignored");
916
917 return NGX_CONF_OK;
918
919#else
920
Igor Sysoev43f13192004-04-12 16:38:09 +0000921 ngx_core_conf_t *ccf = conf;
922
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000923 char *group;
Igor Sysoev03420a62004-01-20 20:40:08 +0000924 struct passwd *pwd;
925 struct group *grp;
926 ngx_str_t *value;
Igor Sysoev03420a62004-01-20 20:40:08 +0000927
Igor Sysoev4959ec42005-05-23 12:07:45 +0000928 if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
Igor Sysoev03420a62004-01-20 20:40:08 +0000929 return "is duplicate";
930 }
931
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000932 if (geteuid() != 0) {
933 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
934 "the \"user\" directive makes sense only "
935 "if the master process runs "
936 "with super-user privileges, ignored");
937 return NGX_CONF_OK;
938 }
939
Igor Sysoev03420a62004-01-20 20:40:08 +0000940 value = (ngx_str_t *) cf->args->elts;
941
Igor Sysoev42b12b32004-12-02 18:40:46 +0000942 ccf->username = (char *) value[1].data;
943
Igor Sysoevd2d6bd52007-07-11 10:46:21 +0000944 ngx_set_errno(0);
Igor Sysoev10a543a2004-03-16 07:10:12 +0000945 pwd = getpwnam((const char *) value[1].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000946 if (pwd == NULL) {
947 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
Igor Sysoev924bd792004-10-11 15:07:03 +0000948 "getpwnam(\"%s\") failed", value[1].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000949 return NGX_CONF_ERROR;
950 }
951
952 ccf->user = pwd->pw_uid;
953
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000954 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000955
Igor Sysoevd2d6bd52007-07-11 10:46:21 +0000956 ngx_set_errno(0);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000957 grp = getgrnam(group);
Igor Sysoev03420a62004-01-20 20:40:08 +0000958 if (grp == NULL) {
959 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000960 "getgrnam(\"%s\") failed", group);
Igor Sysoev03420a62004-01-20 20:40:08 +0000961 return NGX_CONF_ERROR;
962 }
963
964 ccf->group = grp->gr_gid;
965
966 return NGX_CONF_OK;
Igor Sysoeva5362982004-03-04 07:04:55 +0000967
968#endif
Igor Sysoev03420a62004-01-20 20:40:08 +0000969}
Igor Sysoev42b12b32004-12-02 18:40:46 +0000970
971
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000972static char *
Igor Sysoev8885f872007-01-20 19:26:48 +0000973ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
974{
975 ngx_core_conf_t *ccf = conf;
976
977 ngx_str_t *value, *var;
978 ngx_uint_t i;
979
980 var = ngx_array_push(&ccf->env);
981 if (var == NULL) {
982 return NGX_CONF_ERROR;
983 }
984
985 value = cf->args->elts;
986 *var = value[1];
987
988 for (i = 0; i < value[1].len; i++) {
989
990 if (value[1].data[i] == '=') {
991
992 var->len = i;
993
994 return NGX_CONF_OK;
995 }
996 }
997
998 return NGX_CONF_OK;
999}
1000
1001
1002static char *
Igor Sysoev8184d1b2005-03-04 14:06:57 +00001003ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev42b12b32004-12-02 18:40:46 +00001004{
1005 ngx_core_conf_t *ccf = conf;
1006
1007 ngx_str_t *value;
1008 ngx_uint_t n, minus;
1009
1010 if (ccf->priority != 0) {
1011 return "is duplicate";
1012 }
1013
1014 value = cf->args->elts;
1015
1016 if (value[1].data[0] == '-') {
1017 n = 1;
1018 minus = 1;
1019
1020 } else if (value[1].data[0] == '+') {
1021 n = 1;
1022 minus = 0;
1023
1024 } else {
1025 n = 0;
1026 minus = 0;
1027 }
1028
1029 ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n);
1030 if (ccf->priority == NGX_ERROR) {
1031 return "invalid number";
1032 }
1033
1034 if (minus) {
1035 ccf->priority = -ccf->priority;
1036 }
1037
1038 return NGX_CONF_OK;
1039}
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001040
1041
1042static char *
1043ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1044{
1045#if (NGX_HAVE_SCHED_SETAFFINITY)
1046 ngx_core_conf_t *ccf = conf;
1047
1048 u_char ch;
1049 u_long *mask;
1050 ngx_str_t *value;
1051 ngx_uint_t i, n;
1052
1053 if (ccf->cpu_affinity) {
1054 return "is duplicate";
1055 }
1056
1057 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
1058 if (mask == NULL) {
1059 return NGX_CONF_ERROR;
1060 }
1061
1062 ccf->cpu_affinity_n = cf->args->nelts - 1;
1063 ccf->cpu_affinity = mask;
1064
1065 value = cf->args->elts;
1066
1067 for (n = 1; n < cf->args->nelts; n++) {
1068
1069 if (value[n].len > 32) {
1070 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1071 "\"worker_cpu_affinity\" supports up to 32 CPU only");
1072 return NGX_CONF_ERROR;
1073 }
1074
1075 mask[n - 1] = 0;
1076
1077 for (i = 0; i < value[n].len; i++) {
1078
1079 ch = value[n].data[i];
1080
1081 if (ch == ' ') {
1082 continue;
1083 }
1084
1085 mask[n - 1] <<= 1;
1086
1087 if (ch == '0') {
1088 continue;
1089 }
1090
1091 if (ch == '1') {
1092 mask[n - 1] |= 1;
1093 continue;
1094 }
1095
1096 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1097 "invalid character \"%c\" in \"worker_cpu_affinity\"",
1098 ch);
Igor Sysoev3b304762008-05-22 12:09:41 +00001099 return NGX_CONF_ERROR;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001100 }
1101 }
1102
1103#else
1104
1105 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1106 "\"worker_cpu_affinity\" is not supported "
1107 "on this platform, ignored");
1108#endif
1109
1110 return NGX_CONF_OK;
1111}
1112
1113
1114u_long
1115ngx_get_cpu_affinity(ngx_uint_t n)
1116{
1117 ngx_core_conf_t *ccf;
1118
1119 ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
1120 ngx_core_module);
1121
1122 if (ccf->cpu_affinity == NULL) {
1123 return 0;
1124 }
1125
1126 if (ccf->cpu_affinity_n > n) {
1127 return ccf->cpu_affinity[n];
1128 }
1129
1130 return ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
1131}