comparison src/core/nginx.c @ 226:b0c1e21e68db

nginx-0.0.1-2004-01-14-00:33:59 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 13 Jan 2004 21:33:59 +0000
parents 2e9a8a14a0cf
children 2ba3477070ac
comparison
equal deleted inserted replaced
225:2e9a8a14a0cf 226:b0c1e21e68db
14 } ngx_core_conf_t; 14 } ngx_core_conf_t;
15 15
16 16
17 typedef struct { 17 typedef struct {
18 ngx_file_t pid; 18 ngx_file_t pid;
19 char *name;
19 char *const *argv; 20 char *const *argv;
20 } ngx_master_ctx_t; 21 } ngx_master_ctx_t;
21 22
22 23
23 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx); 24 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx);
24 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); 25 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
25 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp); 26 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp);
26 static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv); 27 static ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv);
27 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle); 28 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle);
28 29
29 30
30 static ngx_str_t core_name = ngx_string("core"); 31 static ngx_str_t core_name = ngx_string("core");
31 32
73 uid_t user; 74 uid_t user;
74 75
75 u_int ngx_connection_counter; 76 u_int ngx_connection_counter;
76 77
77 ngx_int_t ngx_process; 78 ngx_int_t ngx_process;
79 ngx_pid_t ngx_new_binary;
80
78 ngx_int_t ngx_inherited; 81 ngx_int_t ngx_inherited;
79
80 ngx_int_t ngx_signal; 82 ngx_int_t ngx_signal;
81 ngx_int_t ngx_reap; 83 ngx_int_t ngx_reap;
82 ngx_int_t ngx_terminate; 84 ngx_int_t ngx_terminate;
83 ngx_int_t ngx_quit; 85 ngx_int_t ngx_quit;
84 ngx_int_t ngx_noaccept; 86 ngx_int_t ngx_noaccept;
199 } 201 }
200 202
201 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid()); 203 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
202 ngx_memzero(&ctx.pid, sizeof(ngx_file_t)); 204 ngx_memzero(&ctx.pid, sizeof(ngx_file_t));
203 ctx.pid.name = ngx_inherited ? ccf->newpid : ccf->pid; 205 ctx.pid.name = ngx_inherited ? ccf->newpid : ccf->pid;
206 ctx.name = ccf->pid.data;
204 207
205 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR, 208 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR,
206 NGX_FILE_CREATE_OR_OPEN); 209 NGX_FILE_CREATE_OR_OPEN);
207 210
208 if (ctx.pid.fd == NGX_INVALID_FILE) { 211 if (ctx.pid.fd == NGX_INVALID_FILE) {
228 231
229 return 0; 232 return 0;
230 } 233 }
231 234
232 235
236 /* TODO: broken single process */
237
233 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) 238 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
234 { 239 {
235 int signo; 240 int signo;
236 ngx_msec_t delay; 241 char *name;
237 struct timeval tv; 242 sigset_t set, wset;
238 ngx_uint_t i, live, mark; 243 struct timeval tv;
239 sigset_t set, wset; 244 ngx_uint_t i, live, mark;
245 ngx_msec_t delay;
240 246
241 delay = 125; 247 delay = 125;
242 248
243 sigemptyset(&set); 249 sigemptyset(&set);
244 sigaddset(&set, SIGCHLD); 250 sigaddset(&set, SIGCHLD);
255 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 261 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
256 "sigprocmask() failed"); 262 "sigprocmask() failed");
257 } 263 }
258 264
259 ngx_signal = 0; 265 ngx_signal = 0;
266 ngx_new_binary = 0;
260 signo = 0; 267 signo = 0;
261 mark = 1; 268 mark = 1;
262 269
263 for ( ;; ) { 270 for ( ;; ) {
264 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle"); 271 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle");
265 272
266 if (ngx_process == NGX_PROCESS_MASTER) { 273 if (ngx_process == NGX_PROCESS_MASTER) {
267 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL, 274 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
268 "worker process", NGX_PROCESS_RESPAWN); 275 "worker process", NGX_PROCESS_RESPAWN);
276 mark = 1;
269 277
270 } else { 278 } else {
271 ngx_init_temp_number(); 279 ngx_init_temp_number();
272 280
273 for (i = 0; ngx_modules[i]; i++) { 281 for (i = 0; ngx_modules[i]; i++) {
278 } 286 }
279 } 287 }
280 } 288 }
281 } 289 }
282 290
283 /* a cycle with the same configuration */ 291 /* a cycle with the same configuration because a new one is invalid */
284 292
285 for ( ;; ) { 293 for ( ;; ) {
286 294
287 /* an event loop */ 295 /* an event loop */
288 296
289 for ( ;; ) { 297 for ( ;; ) {
290 298
291 if (ngx_process == NGX_PROCESS_MASTER) { 299 if (ngx_process == NGX_PROCESS_MASTER) {
292 if (signo) { 300 if (signo) {
293 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 301 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
294 "signal cycle"); 302 "signal cycle: %d, %d", signo, mark);
295 303
296 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { 304 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
297 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 305 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
298 "sigprocmask() failed"); 306 "sigprocmask() failed");
299 continue; 307 continue;
328 } 336 }
329 337
330 ngx_signal = 0; 338 ngx_signal = 0;
331 339
332 } else { 340 } else {
341 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
342 "sigsuspend");
343
333 sigsuspend(&wset); 344 sigsuspend(&wset);
334 345
335 ngx_gettimeofday(&tv); 346 ngx_gettimeofday(&tv);
336 ngx_time_update(tv.tv_sec); 347 ngx_time_update(tv.tv_sec);
337 } 348
338 349 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
339 /* TODO: broken */ 350 "wake up");
340 } else if (ngx_process == NGX_PROCESS_SINGLE) { 351 }
352
353 } else { /* NGX_PROCESS_SINGLE */
341 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 354 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
342 "worker cycle"); 355 "worker cycle");
343 356
344 ngx_process_events(cycle->log); 357 ngx_process_events(cycle->log);
345 } 358 }
346 359
347 if (ngx_reap) { 360 if (ngx_reap) {
361 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
362 "reap childs");
363
348 live = 0; 364 live = 0;
349 for (i = 0; i < ngx_last_process; i++) { 365 for (i = 0; i < ngx_last_process; i++) {
366
350 if (ngx_processes[i].exiting 367 if (ngx_processes[i].exiting
351 && !ngx_processes[i].exited) 368 && !ngx_processes[i].exited)
352 { 369 {
353 live = 1; 370 live = 1;
354 continue; 371 continue;
355 } 372 }
356 373
357 if (i != --ngx_last_process) { 374 if (ngx_processes[i].exited) {
358 ngx_processes[i--] = 375 if (ngx_processes[i].pid == ngx_new_binary) {
376 ngx_new_binary = 0;
377 }
378
379 if (i != --ngx_last_process) {
380 ngx_processes[i--] =
359 ngx_processes[ngx_last_process]; 381 ngx_processes[ngx_last_process];
382 }
360 } 383 }
361 } 384 }
362 385
363 if (!live) { 386 if (!live) {
364 if (ngx_terminate || ngx_quit) { 387 if (ngx_terminate || ngx_quit) {
365 if (ngx_delete_file(ctx->pid.name.data) 388
366 == NGX_FILE_ERROR) 389 if (ngx_inherited && getppid() > 1) {
367 { 390 name = ctx->pid.name.data;
391
392 } else {
393 name = ctx->name;
394 }
395
396 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
368 ngx_log_error(NGX_LOG_ALERT, cycle->log, 397 ngx_log_error(NGX_LOG_ALERT, cycle->log,
369 ngx_errno, 398 ngx_errno,
370 ngx_delete_file_n 399 ngx_delete_file_n
371 " \"%s\" failed", 400 " \"%s\" failed", name);
372 ctx->pid.name.data);
373 } 401 }
374 402
375 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit"); 403 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
376 exit(0); 404 exit(0);
377 405
378 } else { 406 } else {
379 signo = 0; 407 signo = 0;
408 mark = 0;
380 } 409 }
381 } 410 }
382 } 411 }
383 412
384 if (ngx_terminate) { 413 if (ngx_terminate) {
385 if (delay > 10000) { 414 if (delay > 10000) {
386 signo = SIGKILL; 415 signo = SIGKILL;
387 } else { 416 } else {
388 signo = ngx_signal_value(NGX_TERMINATE_SIGNAL); 417 signo = ngx_signal_value(NGX_TERMINATE_SIGNAL);
389 } 418 if (mark == 0) {
419 mark = 1;
420 }
421 }
422 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
423 "mark: %d", mark);
390 424
391 } else if (ngx_quit) { 425 } else if (ngx_quit) {
392 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 426 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
427 if (mark == 0) {
428 mark = 1;
429 }
393 430
394 } else { 431 } else {
395 432
396 if (ngx_reap) { 433 if (ngx_reap) {
434 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
435 "respawn processes");
397 ngx_respawn_processes(cycle); 436 ngx_respawn_processes(cycle);
398 } 437 }
399 438
400 if (ngx_noaccept) { 439 if (ngx_noaccept) {
401 if (mark == 0) { 440 if (mark == 0) {
406 445
407 if (ngx_change_binary) { 446 if (ngx_change_binary) {
408 ngx_change_binary = 0; 447 ngx_change_binary = 0;
409 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 448 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
410 "changing binary"); 449 "changing binary");
411 ngx_exec_new_binary(cycle, ctx->argv); 450 ngx_new_binary = ngx_exec_new_binary(cycle, ctx->argv);
412 } 451 }
413 452
414 if (ngx_reconfigure) { 453 if (ngx_reconfigure) {
415 mark = 1; 454 mark = 1;
416 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 455 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
417 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 456 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
418 "reconfiguring"); 457 "reconfiguring");
419 } 458 }
420 459
421 if (ngx_reopen) { 460 if (ngx_reopen) {
461 ngx_reopen = 0;
462
463 /* STUB */
422 mark = 1; 464 mark = 1;
423 ngx_reopen = 0;
424 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 465 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
466
425 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 467 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
426 "reopening logs"); 468 "reopening logs");
427 ngx_reopen_files(cycle); 469 ngx_reopen_files(cycle);
428 } 470 }
429 } 471 }
430 472
431 if (signo) { 473 if (signo) {
432 if (mark == 1) { 474 if (mark == 1) {
433 for (i = 0; i < ngx_last_process; i++) { 475 for (i = 0; i < ngx_last_process; i++) {
476 ngx_log_debug1(NGX_LOG_DEBUG_EVENT,
477 cycle->log, 0,
478 "proc " PID_T_FMT,
479 ngx_processes[i].pid);
480
434 if (!ngx_processes[i].detached) { 481 if (!ngx_processes[i].detached) {
435 ngx_processes[i].signal = 1; 482 ngx_processes[i].signal = 1;
483 ngx_log_debug1(NGX_LOG_DEBUG_EVENT,
484 cycle->log, 0,
485 "mark " PID_T_FMT,
486 ngx_processes[i].pid);
436 } 487 }
437 } 488 }
438 mark = -1; 489 mark = -1;
439 delay = 125; 490 delay = 125;
440 } 491 }
442 ngx_signal_processes(cycle, signo); 493 ngx_signal_processes(cycle, signo);
443 } 494 }
444 495
445 if (ngx_reap) { 496 if (ngx_reap) {
446 ngx_reap = 0; 497 ngx_reap = 0;
498 }
499
500 /* STUB */
501 if (ngx_reopen) {
502 break;
447 } 503 }
448 504
449 if (ngx_reconfigure) { 505 if (ngx_reconfigure) {
450 break; 506 break;
451 } 507 }
600 656
601 return NGX_OK; 657 return NGX_OK;
602 } 658 }
603 659
604 660
605 static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv) 661 static ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
606 { 662 {
607 char *env[2], *var, *p; 663 char *env[2], *var, *p;
608 ngx_int_t i; 664 ngx_int_t i;
665 ngx_pid_t pid;
609 ngx_exec_ctx_t ctx; 666 ngx_exec_ctx_t ctx;
610 ngx_listening_t *ls; 667 ngx_listening_t *ls;
611 668
612 ctx.path = argv[0]; 669 ctx.path = argv[0];
613 ctx.name = "new binary process"; 670 ctx.name = "new binary process";
626 683
627 env[0] = var; 684 env[0] = var;
628 env[1] = NULL; 685 env[1] = NULL;
629 ctx.envp = (char *const *) &env; 686 ctx.envp = (char *const *) &env;
630 687
631 ngx_exec(cycle, &ctx); 688 pid = ngx_exec(cycle, &ctx);
632 689
633 ngx_free(var); 690 ngx_free(var);
691
692 return pid;
634 } 693 }
635 694
636 695
637 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle) 696 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle)
638 { 697 {