blob: a19695ccf48327a0b498759fbedae1abe310a2ae [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00002#include <ngx_config.h>
Igor Sysoevb2620632003-01-10 06:09:20 +00003#include <ngx_core.h>
Igor Sysoev239baac2003-06-11 15:28:34 +00004#include <ngx_event.h>
Igor Sysoevaa3436c2003-05-30 14:27:59 +00005#include <nginx.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00006
7
Igor Sysoevfc5a10a2004-03-09 19:47:07 +00008static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
Igor Sysoeve9b2cb12004-02-09 20:47:18 +00009static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle);
Igor Sysoev3c3ca172004-01-05 20:55:48 +000010static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle);
Igor Sysoev03420a62004-01-20 20:40:08 +000011static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev2b542382002-08-20 14:48:28 +000012
Igor Sysoev7f125082003-07-11 15:17:50 +000013
Igor Sysoev7f125082003-07-11 15:17:50 +000014static ngx_str_t core_name = ngx_string("core");
15
16static ngx_command_t ngx_core_commands[] = {
17
Igor Sysoev3c3ca172004-01-05 20:55:48 +000018 { ngx_string("user"),
Igor Sysoev03420a62004-01-20 20:40:08 +000019 NGX_MAIN_CONF|NGX_CONF_TAKE12,
20 ngx_set_user,
Igor Sysoev3c3ca172004-01-05 20:55:48 +000021 0,
Igor Sysoev03420a62004-01-20 20:40:08 +000022 0,
Igor Sysoev3c3ca172004-01-05 20:55:48 +000023 NULL },
24
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000025 { ngx_string("daemon"),
26 NGX_MAIN_CONF|NGX_CONF_TAKE1,
27 ngx_conf_set_core_flag_slot,
28 0,
29 offsetof(ngx_core_conf_t, daemon),
30 NULL },
Igor Sysoev7f125082003-07-11 15:17:50 +000031
Igor Sysoevf0677992004-01-08 17:08:10 +000032 { ngx_string("master_process"),
Igor Sysoev3c3ca172004-01-05 20:55:48 +000033 NGX_MAIN_CONF|NGX_CONF_TAKE1,
34 ngx_conf_set_core_flag_slot,
35 0,
Igor Sysoevf0677992004-01-08 17:08:10 +000036 offsetof(ngx_core_conf_t, master),
Igor Sysoev3c3ca172004-01-05 20:55:48 +000037 NULL },
38
Igor Sysoeve9b2cb12004-02-09 20:47:18 +000039 { ngx_string("pid"),
40 NGX_MAIN_CONF|NGX_CONF_TAKE1,
41 ngx_conf_set_core_str_slot,
42 0,
43 offsetof(ngx_core_conf_t, pid),
44 NULL },
45
Igor Sysoevd8e1f072004-01-22 06:47:28 +000046 { ngx_string("worker_reopen"),
47 NGX_MAIN_CONF|NGX_CONF_TAKE1,
48 ngx_conf_set_core_flag_slot,
49 0,
50 offsetof(ngx_core_conf_t, worker_reopen),
51 NULL },
52
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000053 ngx_null_command
Igor Sysoev7f125082003-07-11 15:17:50 +000054};
55
56
57ngx_module_t ngx_core_module = {
58 NGX_MODULE,
59 &core_name, /* module context */
60 ngx_core_commands, /* module directives */
61 NGX_CORE_MODULE, /* module type */
Igor Sysoev3c3ca172004-01-05 20:55:48 +000062 ngx_core_module_init, /* init module */
Igor Sysoev7f125082003-07-11 15:17:50 +000063 NULL /* init child */
64};
65
66
Igor Sysoevb54698b2004-02-23 20:57:12 +000067ngx_int_t ngx_max_module;
68ngx_atomic_t ngx_connection_counter;
Igor Sysoeva9830112003-05-19 16:39:14 +000069
Igor Sysoevb54698b2004-02-23 20:57:12 +000070ngx_int_t ngx_process;
71ngx_pid_t ngx_pid;
72ngx_pid_t ngx_new_binary;
Igor Sysoevb54698b2004-02-23 20:57:12 +000073ngx_int_t ngx_inherited;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000074
75
Igor Sysoevfc5a10a2004-03-09 19:47:07 +000076int main(int argc, char *const *argv)
Igor Sysoev6abfde62003-07-01 15:00:03 +000077{
Igor Sysoev3c3ca172004-01-05 20:55:48 +000078 ngx_int_t i;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000079 ngx_log_t *log;
80 ngx_cycle_t *cycle, init_cycle;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000081 ngx_core_conf_t *ccf;
Igor Sysoevf0677992004-01-08 17:08:10 +000082 ngx_master_ctx_t ctx;
Igor Sysoev562e53e2003-11-13 06:14:05 +000083#if !(WIN32)
Igor Sysoev3c3ca172004-01-05 20:55:48 +000084 size_t len;
Igor Sysoev10a543a2004-03-16 07:10:12 +000085 u_char pid[/* STUB */ 10];
Igor Sysoev562e53e2003-11-13 06:14:05 +000086#endif
Igor Sysoev6abfde62003-07-01 15:00:03 +000087
Igor Sysoev62260f22003-12-05 17:07:27 +000088#if __FreeBSD__
89 ngx_debug_init();
Igor Sysoev9d639522003-07-07 06:11:50 +000090#endif
91
Igor Sysoevbc5c2872003-07-02 05:01:53 +000092 /* TODO */ ngx_max_sockets = -1;
Igor Sysoev6abfde62003-07-01 15:00:03 +000093
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000094 ngx_time_init();
Igor Sysoevf0677992004-01-08 17:08:10 +000095
Igor Sysoeve89c4582003-12-19 08:15:11 +000096#if (HAVE_PCRE)
Igor Sysoeva8fa0a62003-11-25 20:44:56 +000097 ngx_regex_init();
Igor Sysoeve89c4582003-12-19 08:15:11 +000098#endif
Igor Sysoev562e53e2003-11-13 06:14:05 +000099
Igor Sysoev6abfde62003-07-01 15:00:03 +0000100 log = ngx_log_init_errlog();
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000101 ngx_pid = ngx_getpid();
Igor Sysoev6abfde62003-07-01 15:00:03 +0000102
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000103 /* init_cycle->log is required for signal handlers and ngx_getopt() */
Igor Sysoeve89c4582003-12-19 08:15:11 +0000104
105 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
106 init_cycle.log = log;
107 ngx_cycle = &init_cycle;
108
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000109#if 0
110 /* STUB */ log->log_level = NGX_LOG_DEBUG_ALL;
111#endif
112
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000113 ngx_memzero(&ctx, sizeof(ngx_master_ctx_t));
114 ctx.argc = argc;
115 ctx.argv = argv;
116
Igor Sysoevf2334412004-02-25 20:16:15 +0000117#if (NGX_THREADS)
118 if (ngx_time_mutex_init(log) == NGX_ERROR) {
119 return 1;
120 }
121#endif
122
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000123 if (ngx_getopt(&ctx, &init_cycle) == NGX_ERROR) {
124 return 1;
125 }
126
Igor Sysoev6abfde62003-07-01 15:00:03 +0000127 if (ngx_os_init(log) == NGX_ERROR) {
128 return 1;
129 }
130
131 ngx_max_module = 0;
132 for (i = 0; ngx_modules[i]; i++) {
133 ngx_modules[i]->index = ngx_max_module++;
134 }
135
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000136 if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
137 return 1;
138 }
139
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000140 if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000141 return 1;
142 }
143
144 cycle = ngx_init_cycle(&init_cycle);
Igor Sysoevbc5c2872003-07-02 05:01:53 +0000145 if (cycle == NULL) {
Igor Sysoev6abfde62003-07-01 15:00:03 +0000146 return 1;
147 }
148
Igor Sysoev9d639522003-07-07 06:11:50 +0000149 ngx_cycle = cycle;
Igor Sysoev340b03b2003-07-04 15:10:33 +0000150
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000151 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
152
Igor Sysoevf0677992004-01-08 17:08:10 +0000153 ngx_process = (ccf->master != 0) ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000154
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000155#if (WIN32)
156
157#if 0
158
159 if (run_as_service) {
160 if (ngx_servie(cycle->log) == NGX_ERROR) {
161 return 1;
162 }
163
164 return 0;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000165 }
166
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000167#endif
168
169#else
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000170
Igor Sysoeva4b16df2004-02-02 21:19:52 +0000171 if (!ngx_inherited && ccf->daemon != 0) {
Igor Sysoev160d7742003-11-19 16:26:41 +0000172 if (ngx_daemon(cycle->log) == NGX_ERROR) {
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000173 return 1;
174 }
175 }
176
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000177 if (ccf->pid.len == 0) {
178 ccf->pid.len = sizeof(NGINX_PID) - 1;
179 ccf->pid.data = NGINX_PID;
Igor Sysoevf2954c32004-01-08 21:02:06 +0000180 ccf->newpid.len = sizeof(NGINX_NEW_PID) - 1;
181 ccf->newpid.data = NGINX_NEW_PID;
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000182
183 } else {
184 ccf->newpid.len = ccf->pid.len + sizeof(NGINX_NEW_PID_EXT);
185 if (!(ccf->newpid.data = ngx_alloc(ccf->newpid.len, cycle->log))) {
186 return 1;
187 }
188
189 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len),
190 NGINX_NEW_PID_EXT, sizeof(NGINX_NEW_PID_EXT));
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000191 }
192
Igor Sysoev10a543a2004-03-16 07:10:12 +0000193 len = ngx_snprintf((char *) pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
Igor Sysoevf0677992004-01-08 17:08:10 +0000194 ngx_memzero(&ctx.pid, sizeof(ngx_file_t));
Igor Sysoevf2954c32004-01-08 21:02:06 +0000195 ctx.pid.name = ngx_inherited ? ccf->newpid : ccf->pid;
Igor Sysoev80340f02004-01-13 21:33:59 +0000196 ctx.name = ccf->pid.data;
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000197
Igor Sysoevf0677992004-01-08 17:08:10 +0000198 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR,
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000199 NGX_FILE_CREATE_OR_OPEN);
200
Igor Sysoevf0677992004-01-08 17:08:10 +0000201 if (ctx.pid.fd == NGX_INVALID_FILE) {
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000202 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
Igor Sysoevf0677992004-01-08 17:08:10 +0000203 ngx_open_file_n " \"%s\" failed", ctx.pid.name.data);
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000204 return 1;
205 }
206
Igor Sysoevf0677992004-01-08 17:08:10 +0000207 if (ngx_write_file(&ctx.pid, pid, len, 0) == NGX_ERROR) {
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000208 return 1;
209 }
210
Igor Sysoevf0677992004-01-08 17:08:10 +0000211 if (ngx_close_file(ctx.pid.fd) == NGX_FILE_ERROR) {
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000212 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoevf0677992004-01-08 17:08:10 +0000213 ngx_close_file_n " \"%s\" failed", ctx.pid.name.data);
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000214 }
215
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000216#endif
Igor Sysoev6abfde62003-07-01 15:00:03 +0000217
Igor Sysoevf0677992004-01-08 17:08:10 +0000218 ngx_master_process_cycle(cycle, &ctx);
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000219
220 return 0;
221}
222
223
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000224static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle)
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000225{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000226 u_char *p, *v, *inherited;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000227 ngx_socket_t s;
228 ngx_listening_t *ls;
229
Igor Sysoev10a543a2004-03-16 07:10:12 +0000230 inherited = (u_char *) getenv(NGINX_VAR);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000231
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000232 if (inherited == NULL) {
233 return NGX_OK;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000234 }
235
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000236 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
237 "using inherited sockets from \"%s\"", inherited);
238
239 ngx_init_array(cycle->listening, cycle->pool,
240 10, sizeof(ngx_listening_t), NGX_ERROR);
241
242 for (p = inherited, v = p; *p; p++) {
243 if (*p == ':' || *p == ';') {
244 s = ngx_atoi(v, p - v);
245 if (s == NGX_ERROR) {
246 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
247 "invalid socket number \"%s\" in "
248 NGINX_VAR " enviroment variable, "
249 "ignoring the rest of the variable", v);
250 break;
251 }
252
Igor Sysoev10a543a2004-03-16 07:10:12 +0000253 v = p + 1;
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000254
Igor Sysoev10a543a2004-03-16 07:10:12 +0000255 if (!(ls = ngx_push_array(&cycle->listening))) {
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000256 return NGX_ERROR;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000257 }
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000258
259 ls->fd = s;
260 }
261 }
262
263 ngx_inherited = 1;
264
265 return ngx_set_inherited_sockets(cycle);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000266}
267
268
Igor Sysoeva5362982004-03-04 07:04:55 +0000269ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000270{
271 char *env[2], *var, *p;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000272 ngx_uint_t i;
Igor Sysoev80340f02004-01-13 21:33:59 +0000273 ngx_pid_t pid;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000274 ngx_exec_ctx_t ctx;
275 ngx_listening_t *ls;
276
277 ctx.path = argv[0];
278 ctx.name = "new binary process";
279 ctx.argv = argv;
280
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000281 var = ngx_alloc(sizeof(NGINX_VAR)
282 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000283 cycle->log);
284
Igor Sysoev10a543a2004-03-16 07:10:12 +0000285 p = (char *) ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000286
287 ls = cycle->listening.elts;
288 for (i = 0; i < cycle->listening.nelts; i++) {
289 p += ngx_snprintf(p, NGX_INT32_LEN + 2, "%u;", ls[i].fd);
290 }
291
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000292 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var);
293
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000294 env[0] = var;
295 env[1] = NULL;
296 ctx.envp = (char *const *) &env;
297
Igor Sysoev6a930452004-03-04 16:34:23 +0000298 pid = ngx_execute(cycle, &ctx);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000299
300 ngx_free(var);
Igor Sysoev80340f02004-01-13 21:33:59 +0000301
302 return pid;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000303}
304
305
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000306static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle)
307{
308 ngx_int_t i;
309
310 for (i = 1; i < ctx->argc; i++) {
311 if (ctx->argv[i][0] != '-') {
312 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
313 "invalid option: \"%s\"", ctx->argv[i]);
314 return NGX_ERROR;
315 }
316
317 switch (ctx->argv[i][1]) {
318
319 case 'c':
Igor Sysoev10a543a2004-03-16 07:10:12 +0000320 cycle->conf_file.data = (u_char *) ctx->argv[++i];
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000321 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
322 break;
323
324 default:
325 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
326 "invalid option: \"%s\"", ctx->argv[i]);
327 return NGX_ERROR;
328 }
329 }
330
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000331 if (cycle->conf_file.data == NULL) {
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000332 cycle->conf_file.len = sizeof(NGINX_CONF) - 1;
333 cycle->conf_file.data = NGINX_CONF;
334 }
335
336 return NGX_OK;
337}
338
339
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000340static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle)
341{
342 ngx_core_conf_t *ccf;
343
344 /*
345 * ngx_core_module has a special init procedure: it is called by
346 * ngx_init_cycle() before the configuration file parsing to create
347 * ngx_core_module configuration and to set its default parameters
348 */
349
350 if (((void **)(cycle->conf_ctx))[ngx_core_module.index] != NULL) {
351 return NGX_OK;
352 }
353
354 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) {
355 return NGX_ERROR;
356 }
357 /* set by pcalloc()
358 *
359 * ccf->pid = NULL;
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000360 * ccf->newpid = NULL;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000361 */
Igor Sysoev03420a62004-01-20 20:40:08 +0000362 ccf->daemon = NGX_CONF_UNSET;
363 ccf->master = NGX_CONF_UNSET;
Igor Sysoevd8e1f072004-01-22 06:47:28 +0000364 ccf->worker_reopen = NGX_CONF_UNSET;
Igor Sysoeva5362982004-03-04 07:04:55 +0000365 ccf->user = (ngx_uid_t) NGX_CONF_UNSET;
366 ccf->group = (ngx_gid_t) NGX_CONF_UNSET;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000367
368 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
369
370 return NGX_OK;
371}
Igor Sysoev03420a62004-01-20 20:40:08 +0000372
373
374static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
375{
Igor Sysoeva5362982004-03-04 07:04:55 +0000376#if (WIN32)
377
378 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
379 "\"user\" is not supported, ignored");
380
381 return NGX_CONF_OK;
382
383#else
384
Igor Sysoev03420a62004-01-20 20:40:08 +0000385 struct passwd *pwd;
386 struct group *grp;
387 ngx_str_t *value;
388 ngx_core_conf_t *ccf;
389
390 ccf = *(void **)conf;
391
392 if (ccf->user != (uid_t) NGX_CONF_UNSET) {
393 return "is duplicate";
394 }
395
396 value = (ngx_str_t *) cf->args->elts;
397
Igor Sysoev10a543a2004-03-16 07:10:12 +0000398 pwd = getpwnam((const char *) value[1].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000399 if (pwd == NULL) {
400 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
401 "getpwnam(%s) failed", value[1].data);
402 return NGX_CONF_ERROR;
403 }
404
405 ccf->user = pwd->pw_uid;
406
407 if (cf->args->nelts == 2) {
408 return NGX_CONF_OK;
409 }
410
Igor Sysoev10a543a2004-03-16 07:10:12 +0000411 grp = getgrnam((const char *) value[2].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000412 if (grp == NULL) {
413 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
414 "getgrnam(%s) failed", value[1].data);
415 return NGX_CONF_ERROR;
416 }
417
418 ccf->group = grp->gr_gid;
419
420 return NGX_CONF_OK;
Igor Sysoeva5362982004-03-04 07:04:55 +0000421
422#endif
Igor Sysoev03420a62004-01-20 20:40:08 +0000423}