comparison src/core/nginx.c @ 225:2e9a8a14a0cf

nginx-0.0.1-2004-01-13-19:43:23 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 13 Jan 2004 16:43:23 +0000
parents d427f1c3fe95
children b0c1e21e68db
comparison
equal deleted inserted replaced
224:d427f1c3fe95 225:2e9a8a14a0cf
74 74
75 u_int ngx_connection_counter; 75 u_int ngx_connection_counter;
76 76
77 ngx_int_t ngx_process; 77 ngx_int_t ngx_process;
78 ngx_int_t ngx_inherited; 78 ngx_int_t ngx_inherited;
79
80 ngx_int_t ngx_signal;
79 ngx_int_t ngx_reap; 81 ngx_int_t ngx_reap;
80 ngx_int_t ngx_terminate; 82 ngx_int_t ngx_terminate;
81 ngx_int_t ngx_quit; 83 ngx_int_t ngx_quit;
82 ngx_int_t ngx_pause; 84 ngx_int_t ngx_noaccept;
83 ngx_int_t ngx_reconfigure; 85 ngx_int_t ngx_reconfigure;
84 ngx_int_t ngx_reopen; 86 ngx_int_t ngx_reopen;
85 ngx_int_t ngx_change_binary; 87 ngx_int_t ngx_change_binary;
86 88
87 89
231 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) 233 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
232 { 234 {
233 int signo; 235 int signo;
234 ngx_msec_t delay; 236 ngx_msec_t delay;
235 struct timeval tv; 237 struct timeval tv;
236 ngx_uint_t i, live, first; 238 ngx_uint_t i, live, mark;
237 sigset_t set, wset; 239 sigset_t set, wset;
238 240
239 delay = 125; 241 delay = 125;
240 242
241 sigemptyset(&set); 243 sigemptyset(&set);
242 sigaddset(&set, SIGCHLD); 244 sigaddset(&set, SIGCHLD);
243 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); 245 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
244 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); 246 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
245 sigaddset(&set, ngx_signal_value(NGX_PAUSE_SIGNAL)); 247 sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL));
246 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL)); 248 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
247 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); 249 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
248 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); 250 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
249 251
250 sigemptyset(&wset); 252 sigemptyset(&wset);
251 253
252 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { 254 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
253 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 255 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
254 "sigprocmask() failed"); 256 "sigprocmask() failed");
255 } 257 }
258
259 ngx_signal = 0;
260 signo = 0;
261 mark = 1;
256 262
257 for ( ;; ) { 263 for ( ;; ) {
258 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle"); 264 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle");
259 265
260 if (ngx_process == NGX_PROCESS_MASTER) { 266 if (ngx_process == NGX_PROCESS_MASTER) {
276 282
277 /* a cycle with the same configuration */ 283 /* a cycle with the same configuration */
278 284
279 for ( ;; ) { 285 for ( ;; ) {
280 286
281 signo = 0;
282 first = 1;
283
284 /* an event loop */ 287 /* an event loop */
285 288
286 for ( ;; ) { 289 for ( ;; ) {
287 290
288 if (ngx_process == NGX_PROCESS_MASTER) { 291 if (ngx_process == NGX_PROCESS_MASTER) {
289 sigsuspend(&wset); 292 if (signo) {
290 293 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
291 ngx_gettimeofday(&tv); 294 "signal cycle");
292 ngx_time_update(tv.tv_sec); 295
293 296 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
297 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
298 "sigprocmask() failed");
299 continue;
300 }
301
302 /*
303 * there is very big chance that the pending signals
304 * would be delivered right on the sigprocmask() return
305 */
306
307 if (!ngx_signal) {
308
309 if (delay < 15000) {
310 delay *= 2;
311 }
312
313 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
314 "msleep %d", delay);
315
316 ngx_msleep(delay);
317
318 ngx_gettimeofday(&tv);
319 ngx_time_update(tv.tv_sec);
320
321 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
322 "wake up");
323 }
324
325 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
326 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
327 "sigprocmask() failed");
328 }
329
330 ngx_signal = 0;
331
332 } else {
333 sigsuspend(&wset);
334
335 ngx_gettimeofday(&tv);
336 ngx_time_update(tv.tv_sec);
337 }
338
339 /* TODO: broken */
294 } else if (ngx_process == NGX_PROCESS_SINGLE) { 340 } else if (ngx_process == NGX_PROCESS_SINGLE) {
295 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 341 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
296 "worker cycle"); 342 "worker cycle");
297 343
298 ngx_process_events(cycle->log); 344 ngx_process_events(cycle->log);
299 345 }
300 } else if (ngx_process == NGX_PROCESS_QUITING 346
301 || ngx_process == NGX_PROCESS_PAUSED) 347 if (ngx_reap) {
302 { 348 live = 0;
303 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 349 for (i = 0; i < ngx_last_process; i++) {
304 "quit cycle"); 350 if (ngx_processes[i].exiting
305 351 && !ngx_processes[i].exited)
306 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { 352 {
307 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 353 live = 1;
308 "sigprocmask() failed"); 354 continue;
309 continue; 355 }
310 } 356
311 357 if (i != --ngx_last_process) {
312 if (ngx_reap == 0) { 358 ngx_processes[i--] =
313 359 ngx_processes[ngx_last_process];
314 if (delay < 15000) { 360 }
315 delay *= 2; 361 }
316 } 362
317 363 if (!live) {
318 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 364 if (ngx_terminate || ngx_quit) {
319 "msleep %d", delay); 365 if (ngx_delete_file(ctx->pid.name.data)
320 366 == NGX_FILE_ERROR)
321 ngx_msleep(delay);
322
323 ngx_gettimeofday(&tv);
324 ngx_time_update(tv.tv_sec);
325
326 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
327 "wake up");
328 }
329
330 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
331 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
332 "sigprocmask() failed");
333 }
334
335 if (ngx_reap) {
336 ngx_reap = 0;
337
338 live = 0;
339 for (i = 0; i < ngx_last_process; i++) {
340 if (ngx_processes[i].exiting
341 && !ngx_processes[i].exited)
342 { 367 {
343 live = 1; 368 ngx_log_error(NGX_LOG_ALERT, cycle->log,
344 continue; 369 ngx_errno,
345 } 370 ngx_delete_file_n
346 371 " \"%s\" failed",
347 if (i != --ngx_last_process) { 372 ctx->pid.name.data);
348 ngx_processes[i--] =
349 ngx_processes[ngx_last_process];
350 }
351 }
352
353 if (live == 0) {
354 if (ngx_process == NGX_PROCESS_QUITING) {
355 if (ngx_delete_file(ctx->pid.name.data)
356 == NGX_FILE_ERROR)
357 {
358 ngx_log_error(NGX_LOG_ALERT, cycle->log,
359 ngx_errno,
360 ngx_delete_file_n
361 " \"%s\" failed",
362 ctx->pid.name.data);
363 }
364
365 } else { /* NGX_PROCESS_PAUSED */
366 ngx_pause = 0;
367 } 373 }
368 374
369 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit"); 375 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
370 exit(0); 376 exit(0);
377
378 } else {
379 signo = 0;
371 } 380 }
372 } 381 }
373 } 382 }
374 383
375 if (ngx_terminate) { 384 if (ngx_terminate) {
376 if (delay > 10000) { 385 if (delay > 10000) {
377 signo = SIGKILL; 386 signo = SIGKILL;
378 } else { 387 } else {
379 signo = ngx_signal_value(NGX_TERMINATE_SIGNAL); 388 signo = ngx_signal_value(NGX_TERMINATE_SIGNAL);
380 } 389 }
381 ngx_process = NGX_PROCESS_QUITING; 390
382 } 391 } else if (ngx_quit) {
383
384 if (ngx_quit) {
385 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 392 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
386 ngx_process = NGX_PROCESS_QUITING; 393
387 } 394 } else {
388 395
389 if (ngx_pause) { 396 if (ngx_reap) {
390 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 397 ngx_respawn_processes(cycle);
391 ngx_process = NGX_PROCESS_PAUSED; 398 }
392 } 399
393 400 if (ngx_noaccept) {
394 if (ngx_reap) { 401 if (mark == 0) {
395 ngx_reap = 0; 402 mark = 1;
396 ngx_respawn_processes(cycle); 403 }
397 } 404 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
398 405 }
399 if (ngx_change_binary) { 406
400 ngx_change_binary = 0; 407 if (ngx_change_binary) {
401 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 408 ngx_change_binary = 0;
402 "changing binary"); 409 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
403 ngx_exec_new_binary(cycle, ctx->argv); 410 "changing binary");
404 } 411 ngx_exec_new_binary(cycle, ctx->argv);
405 412 }
406 if (ngx_reconfigure) { 413
407 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 414 if (ngx_reconfigure) {
408 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring"); 415 mark = 1;
409 } 416 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
410 417 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
411 if (ngx_reopen) { 418 "reconfiguring");
412 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); 419 }
413 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 420
414 "reopening logs"); 421 if (ngx_reopen) {
415 ngx_reopen_files(cycle); 422 mark = 1;
416 ngx_reopen = 0; 423 ngx_reopen = 0;
424 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
425 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
426 "reopening logs");
427 ngx_reopen_files(cycle);
428 }
417 } 429 }
418 430
419 if (signo) { 431 if (signo) {
420 if (first) { 432 if (mark == 1) {
421 for (i = 0; i < ngx_last_process; i++) { 433 for (i = 0; i < ngx_last_process; i++) {
422 if (!ngx_processes[i].detached) { 434 if (!ngx_processes[i].detached) {
423 ngx_processes[i].signal = 1; 435 ngx_processes[i].signal = 1;
424 } 436 }
425 } 437 }
426 first = 0; 438 mark = -1;
427 delay = 125; 439 delay = 125;
428 } 440 }
429 441
430 ngx_signal_processes(cycle, signo); 442 ngx_signal_processes(cycle, signo);
443 }
444
445 if (ngx_reap) {
446 ngx_reap = 0;
431 } 447 }
432 448
433 if (ngx_reconfigure) { 449 if (ngx_reconfigure) {
434 break; 450 break;
435 } 451 }
436 } 452 }
437 453
438 if (ngx_process == NGX_PROCESS_PAUSED) { 454 if (ngx_noaccept) {
439 ngx_process = NGX_PROCESS_MASTER; 455 ngx_noaccept = 0;
440 456
441 } else { 457 } else {
442 cycle = ngx_init_cycle(cycle); 458 cycle = ngx_init_cycle(cycle);
443 if (cycle == NULL) { 459 if (cycle == NULL) {
444 cycle = (ngx_cycle_t *) ngx_cycle; 460 cycle = (ngx_cycle_t *) ngx_cycle;