comparison src/event/ngx_event.c @ 92:45945fa8b8ba NGINX_0_2_0

nginx 0.2.0 *) The pid-file names used during online upgrade was changed and now is not required a manual rename operation. The old master process adds the ".oldbin" suffix to its pid-file and executes a new binary file. The new master process creates usual pid-file without the ".newbin" suffix. If the master process exits, then old master process renames back its pid-file with the ".oldbin" suffix to the pid-file without suffix. *) Change: the "worker_connections" directive, new name of the "connections" directive; now the directive specifies maximum number of connections, but not maximum socket descriptor number. *) Feature: SSL supports the session cache inside one worker process. *) Feature: the "satisfy_any" directive. *) Change: the ngx_http_access_module and ngx_http_auth_basic_module do not run for subrequests. *) Feature: the "worker_rlimit_nofile" and "worker_rlimit_sigpending" directives. *) Bugfix: if all backend using in load-balancing failed after one error, then nginx did not try do connect to them during 60 seconds. *) Bugfix: in IMAP/POP3 command argument parsing. Thanks to Rob Mueller. *) Bugfix: errors while using SSL in IMAP/POP3 proxy. *) Bugfix: errors while using SSI and gzipping. *) Bugfix: the "Expires" and "Cache-Control" header lines were omitted from the 304 responses. Thanks to Alexandr Kukushkin.
author Igor Sysoev <http://sysoev.ru>
date Fri, 23 Sep 2005 00:00:00 +0400
parents 71c46860eb55
children dad2fe8ecf08
comparison
equal deleted inserted replaced
91:c3eee83ea942 92:45945fa8b8ba
111 static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ; 111 static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ;
112 112
113 113
114 static ngx_command_t ngx_event_core_commands[] = { 114 static ngx_command_t ngx_event_core_commands[] = {
115 115
116 { ngx_string("worker_connections"),
117 NGX_EVENT_CONF|NGX_CONF_TAKE1,
118 ngx_event_connections,
119 0,
120 0,
121 NULL },
122
116 { ngx_string("connections"), 123 { ngx_string("connections"),
117 NGX_EVENT_CONF|NGX_CONF_TAKE1, 124 NGX_EVENT_CONF|NGX_CONF_TAKE1,
118 ngx_event_connections, 125 ngx_event_connections,
119 0, 126 0,
120 0, 127 0,
320 327
321 328
322 static ngx_int_t 329 static ngx_int_t
323 ngx_event_module_init(ngx_cycle_t *cycle) 330 ngx_event_module_init(ngx_cycle_t *cycle)
324 { 331 {
332 void ***cf;
333 ngx_event_conf_t *ecf;
325 #if !(NGX_WIN32) 334 #if !(NGX_WIN32)
326 335 char *shared;
327 size_t size; 336 size_t size;
328 void ***cf; 337 ngx_int_t limit;
329 char *shared; 338 struct rlimit rlmt;
330 ngx_core_conf_t *ccf; 339 ngx_core_conf_t *ccf;
331 ngx_event_conf_t *ecf; 340 #endif
332 341
333 cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module); 342 cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
334 343
335 if (cf == NULL) { 344 if (cf == NULL) {
336 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, 345 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
341 ecf = (*cf)[ngx_event_core_module.ctx_index]; 350 ecf = (*cf)[ngx_event_core_module.ctx_index];
342 351
343 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 352 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
344 "using the \"%s\" event method", ecf->name); 353 "using the \"%s\" event method", ecf->name);
345 354
355 #if !(NGX_WIN32)
346 356
347 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 357 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
358
359 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
360 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
361 "getrlimit(RLIMIT_NOFILE) failed, ignored");
362
363 } else {
364 if (ecf->connections > (ngx_uint_t) rlmt.rlim_cur
365 && (ccf->rlimit_nofile == NGX_CONF_UNSET
366 || ecf->connections > (ngx_uint_t) ccf->rlimit_nofile))
367 {
368 limit = (ccf->rlimit_nofile == NGX_CONF_UNSET) ?
369 (ngx_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
370
371 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
372 "%ui worker_connections are more than "
373 "open file resource limit: %i",
374 ecf->connections, limit);
375 }
376 }
377
348 378
349 if (ccf->master == 0 || ngx_accept_mutex_ptr) { 379 if (ccf->master == 0 || ngx_accept_mutex_ptr) {
350 return NGX_OK; 380 return NGX_OK;
351 } 381 }
352 382
388 418
389 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 419 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
390 "counter: %p, %d", 420 "counter: %p, %d",
391 ngx_connection_counter, *ngx_connection_counter); 421 ngx_connection_counter, *ngx_connection_counter);
392 422
393 #endif 423 #endif /* !(NGX_WIN32) */
394 424
395 return NGX_OK; 425 return NGX_OK;
396 } 426 }
397 427
398 428
399 static ngx_int_t 429 static ngx_int_t
400 ngx_event_process_init(ngx_cycle_t *cycle) 430 ngx_event_process_init(ngx_cycle_t *cycle)
401 { 431 {
402 ngx_uint_t m, i; 432 ngx_uint_t m, i;
403 ngx_socket_t fd;
404 ngx_event_t *rev, *wev; 433 ngx_event_t *rev, *wev;
405 ngx_listening_t *s; 434 ngx_listening_t *ls;
406 ngx_connection_t *c; 435 ngx_connection_t *c, *next, *old;
407 ngx_core_conf_t *ccf; 436 ngx_core_conf_t *ccf;
408 ngx_event_conf_t *ecf; 437 ngx_event_conf_t *ecf;
409 ngx_event_module_t *module; 438 ngx_event_module_t *module;
410 #if (NGX_WIN32) 439 #if (NGX_WIN32)
411 ngx_iocp_conf_t *iocpcf; 440 ngx_iocp_conf_t *iocpcf;
441 #else
442 struct rlimit rlmt;
412 #endif 443 #endif
413 444
414 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 445 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
415 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); 446 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
416 447
447 } 478 }
448 break; 479 break;
449 } 480 }
450 } 481 }
451 482
452 cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, 483 #if !(NGX_WIN32)
484
485 if (ngx_event_flags & NGX_USE_FD_EVENT) {
486
487 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
488 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
489 "getrlimit(RLIMIT_NOFILE) failed");
490 return NGX_ERROR;
491 }
492
493 cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
494
495 cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
496 cycle->log);
497 if (cycle->files == NULL) {
498 return NGX_ERROR;
499 }
500 }
501
502 #endif
503
504 cycle->connections0 = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
453 cycle->log); 505 cycle->log);
454 if (cycle->connections == NULL) { 506 if (cycle->connections0 == NULL) {
455 return NGX_ERROR; 507 return NGX_ERROR;
456 } 508 }
457 509
458 c = cycle->connections; 510 c = cycle->connections0;
459 for (i = 0; i < cycle->connection_n; i++) { 511
460 c[i].fd = (ngx_socket_t) -1; 512 cycle->read_events0 = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
461 c[i].data = NULL;
462 #if (NGX_THREADS)
463 c[i].lock = 0;
464 #endif
465 }
466
467 cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
468 cycle->log); 513 cycle->log);
469 if (cycle->read_events == NULL) { 514 if (cycle->read_events0 == NULL) {
470 return NGX_ERROR; 515 return NGX_ERROR;
471 } 516 }
472 517
473 rev = cycle->read_events; 518 rev = cycle->read_events0;
474 for (i = 0; i < cycle->connection_n; i++) { 519 for (i = 0; i < cycle->connection_n; i++) {
475 rev[i].closed = 1; 520 rev[i].closed = 1;
476 rev[i].instance = 1; 521 rev[i].instance = 1;
477 #if (NGX_THREADS) 522 #if (NGX_THREADS)
478 rev[i].lock = &c[i].lock; 523 rev[i].lock = &c[i].lock;
479 rev[i].own_lock = &c[i].lock; 524 rev[i].own_lock = &c[i].lock;
480 #endif 525 #endif
481 } 526 }
482 527
483 cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections, 528 cycle->write_events0 = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
484 cycle->log); 529 cycle->log);
485 if (cycle->write_events == NULL) { 530 if (cycle->write_events0 == NULL) {
486 return NGX_ERROR; 531 return NGX_ERROR;
487 } 532 }
488 533
489 wev = cycle->write_events; 534 wev = cycle->write_events0;
490 for (i = 0; i < cycle->connection_n; i++) { 535 for (i = 0; i < cycle->connection_n; i++) {
491 wev[i].closed = 1; 536 wev[i].closed = 1;
492 #if (NGX_THREADS) 537 #if (NGX_THREADS)
493 wev[i].lock = &c[i].lock; 538 wev[i].lock = &c[i].lock;
494 wev[i].own_lock = &c[i].lock; 539 wev[i].own_lock = &c[i].lock;
495 #endif 540 #endif
496 } 541 }
497 542
543 i = cycle->connection_n;
544 next = NULL;
545
546 do {
547 i--;
548
549 c[i].data = next;
550 c[i].read = &cycle->read_events0[i];
551 c[i].write = &cycle->write_events0[i];
552 c[i].fd = (ngx_socket_t) -1;
553
554 next = &c[i];
555
556 #if (NGX_THREADS)
557 c[i].lock = 0;
558 #endif
559 } while (i);
560
561 cycle->free_connections = next;
562 cycle->free_connection_n = ecf->connections;
563
498 /* for each listening socket */ 564 /* for each listening socket */
499 565
500 s = cycle->listening.elts; 566 ls = cycle->listening.elts;
501 for (i = 0; i < cycle->listening.nelts; i++) { 567 for (i = 0; i < cycle->listening.nelts; i++) {
502 568
503 fd = s[i].fd; 569 c = ngx_get_connection(ls[i].fd, cycle->log);
504 570
505 #if (NGX_WIN32) 571 if (c == NULL) {
506 /* 572 return NGX_ERROR;
507 * Winsock assignes a socket number divisible by 4 573 }
508 * so to find a connection we divide a socket number by 4. 574
509 */ 575 rev = c->read;
510 576 wev = c->write;
511 fd /= 4;
512 #endif
513
514 c = &cycle->connections[fd];
515 rev = &cycle->read_events[fd];
516 wev = &cycle->write_events[fd];
517 577
518 ngx_memzero(c, sizeof(ngx_connection_t)); 578 ngx_memzero(c, sizeof(ngx_connection_t));
579
580 c->read = rev;
581 c->write = wev;
582 c->fd = ls[i].fd;
583 c->log = &ls[i].log;
584
585 c->listening = &ls[i];
586 ls[i].connection = c;
587
588 c->ctx = ls[i].ctx;
589 c->servers = ls[i].servers;
590
519 ngx_memzero(rev, sizeof(ngx_event_t)); 591 ngx_memzero(rev, sizeof(ngx_event_t));
520 ngx_memzero(wev, sizeof(ngx_event_t)); 592 ngx_memzero(wev, sizeof(ngx_event_t));
521
522 c->fd = s[i].fd;
523 c->listening = &s[i];
524
525 c->ctx = s[i].ctx;
526 c->servers = s[i].servers;
527 c->log = s[i].log;
528 c->read = rev;
529
530 /* required by iocp in "c->write->active = 1" */
531 c->write = wev;
532 593
533 /* required by poll */ 594 /* required by poll */
534 wev->index = NGX_INVALID_INDEX; 595 wev->index = NGX_INVALID_INDEX;
535 596
536 rev->log = c->log; 597 rev->log = c->log;
540 rev->available = 0; 601 rev->available = 0;
541 602
542 rev->accept = 1; 603 rev->accept = 1;
543 604
544 #if (NGX_HAVE_DEFERRED_ACCEPT) 605 #if (NGX_HAVE_DEFERRED_ACCEPT)
545 rev->deferred_accept = s[i].deferred_accept; 606 rev->deferred_accept = ls[i].deferred_accept;
546 #endif 607 #endif
547 608
548 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) { 609 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
549 if (s[i].remain) { 610 if (ls[i].previous) {
550 611
551 /* 612 /*
552 * delete the old accept events that were bound to 613 * delete the old accept events that were bound to
553 * the old cycle read events array 614 * the old cycle read events array
554 */ 615 */
555 616
556 if (ngx_del_event(&cycle->old_cycle->read_events[fd], 617 old = ls[i].previous->connection;
557 NGX_READ_EVENT, NGX_CLOSE_EVENT) == NGX_ERROR) 618
619 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
620 == NGX_ERROR)
558 { 621 {
559 return NGX_ERROR; 622 return NGX_ERROR;
560 } 623 }
561 624
562 cycle->old_cycle->connections[fd].fd = (ngx_socket_t) -1; 625 old->fd = (ngx_socket_t) -1;
563 } 626 }
564 } 627 }
565 628
566 #if (NGX_WIN32) 629 #if (NGX_WIN32)
567 630
570 633
571 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) { 634 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
572 return NGX_ERROR; 635 return NGX_ERROR;
573 } 636 }
574 637
638 ls[i].log.handler = ngx_acceptex_log_error;
639
575 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module); 640 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
576 if (ngx_event_post_acceptex(&s[i], iocpcf->post_acceptex) 641 if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
577 == NGX_ERROR) 642 == NGX_ERROR)
578 { 643 {
579 return NGX_ERROR; 644 return NGX_ERROR;
580 } 645 }
581 646
582 } else { 647 } else {
605 return NGX_ERROR; 670 return NGX_ERROR;
606 } 671 }
607 } 672 }
608 673
609 #endif 674 #endif
675
610 } 676 }
611 677
612 return NGX_OK; 678 return NGX_OK;
613 } 679 }
614 680