Mercurial > hg > nginx
comparison src/core/nginx.c @ 221:401154e21826
nginx-0.0.1-2004-01-08-20:08:10 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 08 Jan 2004 17:08:10 +0000 |
parents | 4f81b931e9ff |
children | 99df0edb63ed |
comparison
equal
deleted
inserted
replaced
220:4f81b931e9ff | 221:401154e21826 |
---|---|
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 #include <nginx.h> | 5 #include <nginx.h> |
6 | 6 |
7 | 7 |
8 static void ngx_master_process_cycle(ngx_cycle_t *cycle); | 8 typedef struct { |
9 ngx_str_t user; | |
10 int daemon; | |
11 int master; | |
12 ngx_str_t pid; | |
13 } ngx_core_conf_t; | |
14 | |
15 | |
16 typedef struct { | |
17 ngx_file_t pid; | |
18 char *const *argv; | |
19 } ngx_master_ctx_t; | |
20 | |
21 | |
22 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx); | |
9 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); | 23 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); |
10 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp); | 24 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp); |
11 static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv); | 25 static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv); |
12 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle); | 26 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle); |
13 | |
14 | |
15 typedef struct { | |
16 ngx_str_t user; | |
17 int daemon; | |
18 int single; | |
19 ngx_str_t pid; | |
20 } ngx_core_conf_t; | |
21 | 27 |
22 | 28 |
23 static ngx_str_t core_name = ngx_string("core"); | 29 static ngx_str_t core_name = ngx_string("core"); |
24 | 30 |
25 static ngx_command_t ngx_core_commands[] = { | 31 static ngx_command_t ngx_core_commands[] = { |
36 ngx_conf_set_core_flag_slot, | 42 ngx_conf_set_core_flag_slot, |
37 0, | 43 0, |
38 offsetof(ngx_core_conf_t, daemon), | 44 offsetof(ngx_core_conf_t, daemon), |
39 NULL }, | 45 NULL }, |
40 | 46 |
41 { ngx_string("single_process"), | 47 { ngx_string("master_process"), |
42 NGX_MAIN_CONF|NGX_CONF_TAKE1, | 48 NGX_MAIN_CONF|NGX_CONF_TAKE1, |
43 ngx_conf_set_core_flag_slot, | 49 ngx_conf_set_core_flag_slot, |
44 0, | 50 0, |
45 offsetof(ngx_core_conf_t, single), | 51 offsetof(ngx_core_conf_t, master), |
46 NULL }, | 52 NULL }, |
47 | 53 |
48 ngx_null_command | 54 ngx_null_command |
49 }; | 55 }; |
50 | 56 |
68 u_int ngx_connection_counter; | 74 u_int ngx_connection_counter; |
69 | 75 |
70 ngx_int_t ngx_process; | 76 ngx_int_t ngx_process; |
71 | 77 |
72 | 78 |
73 ngx_int_t ngx_respawn; | 79 ngx_int_t ngx_reap; |
74 ngx_int_t ngx_terminate; | 80 ngx_int_t ngx_terminate; |
75 ngx_int_t ngx_quit; | 81 ngx_int_t ngx_quit; |
76 ngx_int_t ngx_reconfigure; | 82 ngx_int_t ngx_reconfigure; |
77 ngx_int_t ngx_reopen; | 83 ngx_int_t ngx_reopen; |
78 ngx_int_t ngx_change_binary; | 84 ngx_int_t ngx_change_binary; |
84 ngx_int_t i; | 90 ngx_int_t i; |
85 ngx_log_t *log; | 91 ngx_log_t *log; |
86 ngx_cycle_t *cycle, init_cycle; | 92 ngx_cycle_t *cycle, init_cycle; |
87 ngx_open_file_t *file; | 93 ngx_open_file_t *file; |
88 ngx_core_conf_t *ccf; | 94 ngx_core_conf_t *ccf; |
95 ngx_master_ctx_t ctx; | |
89 #if !(WIN32) | 96 #if !(WIN32) |
90 size_t len; | 97 size_t len; |
91 char pid[/* STUB */ 10]; | 98 char pid[/* STUB */ 10]; |
92 ngx_file_t pidfile; | |
93 struct passwd *pwd; | 99 struct passwd *pwd; |
94 #endif | 100 #endif |
95 | 101 |
96 #if __FreeBSD__ | 102 #if __FreeBSD__ |
97 ngx_debug_init(); | 103 ngx_debug_init(); |
98 #endif | 104 #endif |
99 | 105 |
100 /* TODO */ ngx_max_sockets = -1; | 106 /* TODO */ ngx_max_sockets = -1; |
101 | 107 |
102 ngx_time_init(); | 108 ngx_time_init(); |
109 | |
103 #if (HAVE_PCRE) | 110 #if (HAVE_PCRE) |
104 ngx_regex_init(); | 111 ngx_regex_init(); |
105 #endif | 112 #endif |
106 | 113 |
107 log = ngx_log_init_errlog(); | 114 log = ngx_log_init_errlog(); |
136 | 143 |
137 ngx_cycle = cycle; | 144 ngx_cycle = cycle; |
138 | 145 |
139 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | 146 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
140 | 147 |
141 ngx_process = (ccf->single == 1) ? NGX_PROCESS_SINGLE : NGX_PROCESS_MASTER; | 148 ngx_process = (ccf->master != 0) ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; |
142 | 149 |
143 #if (WIN32) | 150 #if (WIN32) |
144 | 151 |
145 #if 0 | 152 #if 0 |
146 | 153 |
185 ccf->pid.len = sizeof(NGINX_PID) - 1; | 192 ccf->pid.len = sizeof(NGINX_PID) - 1; |
186 ccf->pid.data = NGINX_PID; | 193 ccf->pid.data = NGINX_PID; |
187 } | 194 } |
188 | 195 |
189 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid()); | 196 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid()); |
190 ngx_memzero(&pidfile, sizeof(ngx_file_t)); | 197 ngx_memzero(&ctx.pid, sizeof(ngx_file_t)); |
191 pidfile.name = ccf->pid; | 198 ctx.pid.name = ccf->pid; |
192 | 199 |
193 pidfile.fd = ngx_open_file(pidfile.name.data, NGX_FILE_RDWR, | 200 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR, |
194 NGX_FILE_CREATE_OR_OPEN); | 201 NGX_FILE_CREATE_OR_OPEN); |
195 | 202 |
196 if (pidfile.fd == NGX_INVALID_FILE) { | 203 if (ctx.pid.fd == NGX_INVALID_FILE) { |
197 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | 204 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
198 ngx_open_file_n " \"%s\" failed", pidfile.name.data); | 205 ngx_open_file_n " \"%s\" failed", ctx.pid.name.data); |
199 return 1; | 206 return 1; |
200 } | 207 } |
201 | 208 |
202 if (ngx_write_file(&pidfile, pid, len, 0) == NGX_ERROR) { | 209 if (ngx_write_file(&ctx.pid, pid, len, 0) == NGX_ERROR) { |
203 return 1; | 210 return 1; |
204 } | 211 } |
205 | 212 |
206 if (ngx_close_file(pidfile.fd) == NGX_FILE_ERROR) { | 213 if (ngx_close_file(ctx.pid.fd) == NGX_FILE_ERROR) { |
207 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 214 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
208 ngx_close_file_n " \"%s\" failed", pidfile.name.data); | 215 ngx_close_file_n " \"%s\" failed", ctx.pid.name.data); |
209 } | 216 } |
210 | 217 |
211 #endif | 218 #endif |
212 | 219 |
213 ngx_master_process_cycle(cycle); | 220 ctx.argv = argv; |
221 | |
222 ngx_master_process_cycle(cycle, &ctx); | |
214 | 223 |
215 return 0; | 224 return 0; |
216 } | 225 } |
217 | 226 |
218 | 227 |
219 static void ngx_master_process_cycle(ngx_cycle_t *cycle) | 228 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) |
220 { | 229 { |
230 ngx_msec_t delay; | |
221 struct timeval tv; | 231 struct timeval tv; |
222 ngx_int_t i; | 232 ngx_int_t i; |
223 ngx_err_t err; | 233 sigset_t set, wset; |
234 | |
235 delay = 1000; | |
236 | |
237 sigemptyset(&set); | |
238 sigaddset(&set, SIGCHLD); | |
239 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); | |
240 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); | |
241 sigaddset(&set, ngx_signal_value(NGX_INTERRUPT_SIGNAL)); | |
242 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL)); | |
243 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); | |
244 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); | |
245 | |
246 sigemptyset(&wset); | |
247 | |
248 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { | |
249 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
250 "sigprocmask() failed"); | |
251 } | |
224 | 252 |
225 for ( ;; ) { | 253 for ( ;; ) { |
226 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle"); | 254 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle"); |
227 | 255 |
228 if (ngx_process == NGX_PROCESS_MASTER) { | 256 if (ngx_process == NGX_PROCESS_MASTER) { |
240 } | 268 } |
241 } | 269 } |
242 } | 270 } |
243 } | 271 } |
244 | 272 |
245 | |
246 /* a cycle with the same configuration */ | 273 /* a cycle with the same configuration */ |
247 | 274 |
248 for ( ;; ) { | 275 for ( ;; ) { |
249 | 276 |
250 /* an event loop */ | 277 /* an event loop */ |
251 | 278 |
252 for ( ;; ) { | 279 for ( ;; ) { |
253 | 280 |
254 err = 0; | 281 if (ngx_process == NGX_PROCESS_MASTER) { |
255 | 282 sigsuspend(&wset); |
256 if (ngx_process == NGX_PROCESS_SINGLE) { | 283 |
284 ngx_gettimeofday(&tv); | |
285 ngx_time_update(tv.tv_sec); | |
286 | |
287 } else if (ngx_process == NGX_PROCESS_SINGLE) { | |
257 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | 288 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
258 "worker cycle"); | 289 "worker cycle"); |
259 | 290 |
260 ngx_process_events(cycle->log); | 291 ngx_process_events(cycle->log); |
261 | 292 |
262 } else { | 293 } else if (ngx_process == NGX_PROCESS_MASTER_QUIT) { |
263 ngx_set_errno(0); | 294 if (delay < 10000) { |
264 ngx_msleep(1000); | 295 delay *= 2; |
265 err = ngx_errno; | 296 } |
297 | |
298 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { | |
299 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
300 "sigprocmask() failed"); | |
301 continue; | |
302 } | |
303 | |
304 ngx_msleep(delay); | |
266 | 305 |
267 ngx_gettimeofday(&tv); | 306 ngx_gettimeofday(&tv); |
268 ngx_time_update(tv.tv_sec); | 307 ngx_time_update(tv.tv_sec); |
269 | 308 |
270 if (err) { | 309 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { |
271 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, err, | 310 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
272 "sleep() exited"); | 311 "sigprocmask() failed"); |
273 } | 312 } |
274 } | 313 } |
275 | 314 |
276 if (ngx_quit || ngx_terminate) { | 315 if (ngx_quit || ngx_terminate) { |
277 #if !(WIN32) | 316 #if !(WIN32) |
278 #if 0 | 317 if (ngx_delete_file(ctx->pid.name.data) == NGX_FILE_ERROR) { |
279 if (ngx_delete_file(pidfile.name.data) == NGX_FILE_ERROR) { | |
280 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 318 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
281 ngx_delete_file_n " \"%s\" failed", | 319 ngx_delete_file_n " \"%s\" failed", |
282 pidfile.name.data); | 320 ctx->pid.name.data); |
283 } | 321 } |
284 #endif | |
285 #endif | 322 #endif |
286 | 323 |
287 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting"); | 324 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting"); |
288 | 325 |
289 if (ngx_process == NGX_PROCESS_MASTER) { | 326 if (ngx_process == NGX_PROCESS_MASTER) { |
300 | 337 |
301 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit"); | 338 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit"); |
302 exit(0); | 339 exit(0); |
303 } | 340 } |
304 | 341 |
305 if (err == NGX_EINTR) { | 342 if (ngx_reap) { |
343 ngx_reap = 0; | |
306 ngx_respawn_processes(cycle); | 344 ngx_respawn_processes(cycle); |
307 } | 345 } |
308 | 346 |
309 #if 0 | |
310 if (ngx_change_binary) { | 347 if (ngx_change_binary) { |
311 ngx_change_binary = 0; | 348 ngx_change_binary = 0; |
312 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, | 349 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, |
313 "changing binary"); | 350 "changing binary"); |
314 ngx_exec_new_binary(cycle, argv); | 351 ngx_exec_new_binary(cycle, ctx->argv); |
352 | |
315 /* TODO: quit workers */ | 353 /* TODO: quit workers */ |
316 } | 354 } |
317 #endif | |
318 | 355 |
319 if (ngx_reconfigure) { | 356 if (ngx_reconfigure) { |
320 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring"); | 357 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring"); |
321 break; | 358 break; |
322 } | 359 } |
344 } | 381 } |
345 | 382 |
346 | 383 |
347 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) | 384 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) |
348 { | 385 { |
386 sigset_t set; | |
349 ngx_int_t i; | 387 ngx_int_t i; |
350 ngx_listening_t *ls; | 388 ngx_listening_t *ls; |
351 | 389 |
352 ngx_process = NGX_PROCESS_WORKER; | 390 ngx_process = NGX_PROCESS_WORKER; |
353 | 391 |
356 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 394 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
357 "setuid() failed"); | 395 "setuid() failed"); |
358 /* fatal */ | 396 /* fatal */ |
359 exit(1); | 397 exit(1); |
360 } | 398 } |
399 } | |
400 | |
401 sigemptyset(&set); | |
402 | |
403 if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) { | |
404 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
405 "sigprocmask() failed"); | |
361 } | 406 } |
362 | 407 |
363 ngx_init_temp_number(); | 408 ngx_init_temp_number(); |
364 | 409 |
365 /* | 410 /* |
516 /* set by pcalloc() | 561 /* set by pcalloc() |
517 * | 562 * |
518 * ccf->pid = NULL; | 563 * ccf->pid = NULL; |
519 */ | 564 */ |
520 ccf->daemon = -1; | 565 ccf->daemon = -1; |
521 ccf->single = -1; | 566 ccf->master = -1; |
522 | 567 |
523 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf; | 568 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf; |
524 | 569 |
525 return NGX_OK; | 570 return NGX_OK; |
526 } | 571 } |