Mercurial > hg > nginx
comparison src/core/nginx.c @ 311:11ff50a35d6d
nginx-0.0.3-2004-04-12-20:38:09 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 12 Apr 2004 16:38:09 +0000 |
parents | a9a9af2c7370 |
children | d71c87d11b16 |
comparison
equal
deleted
inserted
replaced
310:a9a9af2c7370 | 311:11ff50a35d6d |
---|---|
5 #include <nginx.h> | 5 #include <nginx.h> |
6 | 6 |
7 | 7 |
8 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); | 8 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); |
9 static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle); | 9 static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle); |
10 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle); | 10 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); |
11 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); | |
11 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 12 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
12 | 13 |
13 | 14 |
14 static ngx_str_t core_name = ngx_string("core"); | |
15 | |
16 static ngx_command_t ngx_core_commands[] = { | 15 static ngx_command_t ngx_core_commands[] = { |
17 | 16 |
18 { ngx_string("user"), | |
19 NGX_MAIN_CONF|NGX_CONF_TAKE12, | |
20 ngx_set_user, | |
21 0, | |
22 0, | |
23 NULL }, | |
24 | |
25 { ngx_string("daemon"), | 17 { ngx_string("daemon"), |
26 NGX_MAIN_CONF|NGX_CONF_TAKE1, | 18 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
27 ngx_conf_set_core_flag_slot, | 19 ngx_conf_set_flag_slot, |
28 0, | 20 0, |
29 offsetof(ngx_core_conf_t, daemon), | 21 offsetof(ngx_core_conf_t, daemon), |
30 NULL }, | 22 NULL }, |
31 | 23 |
32 { ngx_string("master_process"), | 24 { ngx_string("master_process"), |
33 NGX_MAIN_CONF|NGX_CONF_TAKE1, | 25 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
34 ngx_conf_set_core_flag_slot, | 26 ngx_conf_set_flag_slot, |
35 0, | 27 0, |
36 offsetof(ngx_core_conf_t, master), | 28 offsetof(ngx_core_conf_t, master), |
37 NULL }, | 29 NULL }, |
38 | 30 |
39 { ngx_string("worker_processes"), | 31 { ngx_string("worker_processes"), |
40 NGX_MAIN_CONF|NGX_CONF_TAKE1, | 32 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
41 ngx_conf_set_core_num_slot, | 33 ngx_conf_set_num_slot, |
42 0, | 34 0, |
43 offsetof(ngx_core_conf_t, worker_processes), | 35 offsetof(ngx_core_conf_t, worker_processes), |
44 NULL }, | 36 NULL }, |
45 | 37 |
38 { ngx_string("user"), | |
39 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12, | |
40 ngx_set_user, | |
41 0, | |
42 0, | |
43 NULL }, | |
44 | |
46 { ngx_string("pid"), | 45 { ngx_string("pid"), |
47 NGX_MAIN_CONF|NGX_CONF_TAKE1, | 46 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
48 ngx_conf_set_core_str_slot, | 47 ngx_conf_set_str_slot, |
49 0, | 48 0, |
50 offsetof(ngx_core_conf_t, pid), | 49 offsetof(ngx_core_conf_t, pid), |
51 NULL }, | 50 NULL }, |
52 | 51 |
53 ngx_null_command | 52 ngx_null_command |
54 }; | 53 }; |
55 | 54 |
56 | 55 |
56 static ngx_core_module_t ngx_core_module_ctx = { | |
57 ngx_string("core"), | |
58 ngx_core_module_create_conf, | |
59 ngx_core_module_init_conf | |
60 }; | |
61 | |
62 | |
57 ngx_module_t ngx_core_module = { | 63 ngx_module_t ngx_core_module = { |
58 NGX_MODULE, | 64 NGX_MODULE, |
59 &core_name, /* module context */ | 65 &ngx_core_module_ctx, /* module context */ |
60 ngx_core_commands, /* module directives */ | 66 ngx_core_commands, /* module directives */ |
61 NGX_CORE_MODULE, /* module type */ | 67 NGX_CORE_MODULE, /* module type */ |
62 ngx_core_module_init, /* init module */ | 68 NULL, /* init module */ |
63 NULL /* init child */ | 69 NULL /* init child */ |
64 }; | 70 }; |
65 | 71 |
66 | 72 |
67 ngx_int_t ngx_max_module; | 73 ngx_int_t ngx_max_module; |
68 | 74 |
69 ngx_int_t ngx_process; | 75 ngx_int_t ngx_process; |
70 ngx_pid_t ngx_pid; | 76 ngx_pid_t ngx_pid; |
71 ngx_pid_t ngx_new_binary; | 77 ngx_pid_t ngx_new_binary; |
72 ngx_int_t ngx_inherited; | 78 ngx_int_t ngx_inherited; |
73 | 79 |
74 | 80 |
75 int main(int argc, char *const *argv) | 81 int main(int argc, char *const *argv) |
76 { | 82 { |
77 ngx_int_t i; | 83 ngx_int_t i; |
147 | 153 |
148 ngx_cycle = cycle; | 154 ngx_cycle = cycle; |
149 | 155 |
150 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | 156 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
151 | 157 |
152 ngx_process = (ccf->master != 0) ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; | 158 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; |
153 | 159 |
154 #if (WIN32) | 160 #if (WIN32) |
155 | 161 |
156 #if 0 | 162 #if 0 |
157 | 163 if (ccf->run_as_service) { |
158 if (run_as_service) { | 164 if (ngx_service(cycle->log) == NGX_ERROR) { |
159 if (ngx_servie(cycle->log) == NGX_ERROR) { | |
160 return 1; | 165 return 1; |
161 } | 166 } |
162 | 167 |
163 return 0; | 168 return 0; |
164 } | 169 } |
165 | |
166 #endif | 170 #endif |
167 | 171 |
168 #else | 172 #else |
169 | 173 |
170 if (!ngx_inherited && ccf->daemon != 0) { | 174 if (!ngx_inherited && ccf->daemon) { |
171 if (ngx_daemon(cycle->log) == NGX_ERROR) { | 175 if (ngx_daemon(cycle->log) == NGX_ERROR) { |
172 return 1; | 176 return 1; |
173 } | 177 } |
174 } | 178 } |
175 | 179 |
176 if (ccf->pid.len == 0) { | 180 if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) { |
177 ccf->pid.len = sizeof(NGINX_PID) - 1; | 181 return 1; |
178 ccf->pid.data = NGINX_PID; | |
179 ccf->newpid.len = sizeof(NGINX_NEWPID) - 1; | |
180 ccf->newpid.data = NGINX_NEWPID; | |
181 | |
182 } else { | |
183 ccf->newpid.len = ccf->pid.len + sizeof(NGINX_NEWPID_EXT); | |
184 if (!(ccf->newpid.data = ngx_alloc(ccf->newpid.len, cycle->log))) { | |
185 return 1; | |
186 } | |
187 | |
188 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len), | |
189 NGINX_NEWPID_EXT, sizeof(NGINX_NEWPID_EXT)); | |
190 } | |
191 | |
192 len = ngx_snprintf((char *) pid, /* STUB */ 10, PID_T_FMT, ngx_getpid()); | |
193 ngx_memzero(&ctx.pid, sizeof(ngx_file_t)); | |
194 ctx.pid.name = ngx_inherited ? ccf->newpid : ccf->pid; | |
195 ctx.name = ccf->pid.data; | |
196 | |
197 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR, | |
198 NGX_FILE_CREATE_OR_OPEN); | |
199 | |
200 if (ctx.pid.fd == NGX_INVALID_FILE) { | |
201 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
202 ngx_open_file_n " \"%s\" failed", ctx.pid.name.data); | |
203 return 1; | |
204 } | |
205 | |
206 if (ngx_write_file(&ctx.pid, pid, len, 0) == NGX_ERROR) { | |
207 return 1; | |
208 } | |
209 | |
210 if (ngx_close_file(ctx.pid.fd) == NGX_FILE_ERROR) { | |
211 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
212 ngx_close_file_n " \"%s\" failed", ctx.pid.name.data); | |
213 } | 182 } |
214 | 183 |
215 #endif | 184 #endif |
216 | 185 |
217 ngx_master_process_cycle(cycle, &ctx); | 186 ngx_master_process_cycle(cycle, &ctx); |
334 | 303 |
335 return NGX_OK; | 304 return NGX_OK; |
336 } | 305 } |
337 | 306 |
338 | 307 |
339 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle) | 308 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle) |
340 { | 309 { |
341 ngx_core_conf_t *ccf; | 310 ngx_core_conf_t *ccf; |
342 | 311 |
343 /* | |
344 * ngx_core_module has a special init procedure: it is called by | |
345 * ngx_init_cycle() before the configuration file parsing to create | |
346 * ngx_core_module configuration and to set its default parameters | |
347 */ | |
348 | |
349 if (((void **)(cycle->conf_ctx))[ngx_core_module.index] != NULL) { | |
350 return NGX_OK; | |
351 } | |
352 | |
353 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) { | 312 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) { |
354 return NGX_ERROR; | 313 return NULL; |
355 } | 314 } |
356 /* set by pcalloc() | 315 /* set by pcalloc() |
357 * | 316 * |
358 * ccf->pid = NULL; | 317 * ccf->pid = NULL; |
359 * ccf->newpid = NULL; | 318 * ccf->newpid = NULL; |
362 ccf->master = NGX_CONF_UNSET; | 321 ccf->master = NGX_CONF_UNSET; |
363 ccf->worker_processes = NGX_CONF_UNSET; | 322 ccf->worker_processes = NGX_CONF_UNSET; |
364 ccf->user = (ngx_uid_t) NGX_CONF_UNSET; | 323 ccf->user = (ngx_uid_t) NGX_CONF_UNSET; |
365 ccf->group = (ngx_gid_t) NGX_CONF_UNSET; | 324 ccf->group = (ngx_gid_t) NGX_CONF_UNSET; |
366 | 325 |
367 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf; | 326 return ccf; |
368 | 327 } |
369 return NGX_OK; | 328 |
329 | |
330 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf) | |
331 { | |
332 ngx_core_conf_t *ccf = conf; | |
333 | |
334 ngx_conf_init_value(ccf->daemon, 1); | |
335 ngx_conf_init_value(ccf->master, 1); | |
336 ngx_conf_init_value(ccf->worker_processes, 1); | |
337 | |
338 #if !(WIN32) | |
339 | |
340 /* TODO: default "nobody" user */ | |
341 | |
342 if (ccf->pid.len == 0) { | |
343 ccf->pid.len = sizeof(NGINX_PID) - 1; | |
344 ccf->pid.data = NGINX_PID; | |
345 ccf->newpid.len = sizeof(NGINX_NEWPID) - 1; | |
346 ccf->newpid.data = NGINX_NEWPID; | |
347 | |
348 } else { | |
349 ccf->newpid.len = ccf->pid.len + sizeof(NGINX_NEWPID_EXT); | |
350 | |
351 if (!(ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len))) { | |
352 return NGX_CONF_ERROR; | |
353 } | |
354 | |
355 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len), | |
356 NGINX_NEWPID_EXT, sizeof(NGINX_NEWPID_EXT)); | |
357 } | |
358 #endif | |
359 | |
360 return NGX_CONF_OK; | |
370 } | 361 } |
371 | 362 |
372 | 363 |
373 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 364 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
374 { | 365 { |
378 "\"user\" is not supported, ignored"); | 369 "\"user\" is not supported, ignored"); |
379 | 370 |
380 return NGX_CONF_OK; | 371 return NGX_CONF_OK; |
381 | 372 |
382 #else | 373 #else |
374 | |
375 ngx_core_conf_t *ccf = conf; | |
383 | 376 |
384 struct passwd *pwd; | 377 struct passwd *pwd; |
385 struct group *grp; | 378 struct group *grp; |
386 ngx_str_t *value; | 379 ngx_str_t *value; |
387 ngx_core_conf_t *ccf; | |
388 | |
389 ccf = *(void **)conf; | |
390 | 380 |
391 if (ccf->user != (uid_t) NGX_CONF_UNSET) { | 381 if (ccf->user != (uid_t) NGX_CONF_UNSET) { |
392 return "is duplicate"; | 382 return "is duplicate"; |
393 } | 383 } |
394 | 384 |