comparison src/os/unix/ngx_process_cycle.c @ 360:239e37d44a34

nginx-0.0.7-2004-06-18-20:22:16 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 18 Jun 2004 16:22:16 +0000
parents e260514b9ad4
children 446782c909b3
comparison
equal deleted inserted replaced
359:a3e622ab7ee7 360:239e37d44a34
332 332
333 333
334 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, 334 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n,
335 ngx_int_t type) 335 ngx_int_t type)
336 { 336 {
337 ngx_int_t i;
338 ngx_channel_t ch;
337 struct itimerval itv; 339 struct itimerval itv;
338 340
339 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "start worker processes"); 341 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "start worker processes");
342
343 ch.command = NGX_CMD_OPEN_CHANNEL;
340 344
341 while (n--) { 345 while (n--) {
342 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL, 346 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
343 "worker process", type); 347 "worker process", type);
348
349 ch.pid = ngx_processes[ngx_process_slot].pid;
350 ch.slot = ngx_process_slot;
351 ch.fd = ngx_processes[ngx_process_slot].channel[0];
352
353 for (i = 0; i < ngx_last_process - 1; i++) {
354
355 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
356 "pass channel s: %d pid:" PID_T_FMT " fd:%d",
357 ch.slot, ch.pid, ch.fd);
358
359 /* TODO: NGX_AGAIN */
360
361 ngx_write_channel(ngx_processes[i].channel[0],
362 &ch, sizeof(ngx_channel_t), cycle->log);
363 }
344 } 364 }
345 365
346 /* 366 /*
347 * we have to limit the maximum life time of the worker processes 367 * we have to limit the maximum life time of the worker processes
348 * by 10 days because our millisecond event timer is limited 368 * by 10 days because our millisecond event timer is limited
357 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) { 377 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
358 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 378 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
359 "setitimer() failed"); 379 "setitimer() failed");
360 } 380 }
361 } 381 }
382
362 383
363 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo) 384 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo)
364 { 385 {
365 ngx_int_t i; 386 ngx_int_t i;
366 ngx_err_t err; 387 ngx_err_t err;
527 } 548 }
528 } 549 }
529 550
530 for (n = 0; n <= ngx_last_process; n++) { 551 for (n = 0; n <= ngx_last_process; n++) {
531 552
532 if (n == ngx_current_slot) { 553 if (n == ngx_process_slot) {
533 if (close(ngx_processes[n].channel[0]) == -1) { 554 if (close(ngx_processes[n].channel[0]) == -1) {
534 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 555 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
535 "close() failed"); 556 "close() failed");
536 } 557 }
537 558
679 break; 700 break;
680 701
681 case NGX_CMD_REOPEN: 702 case NGX_CMD_REOPEN:
682 ngx_reopen = 1; 703 ngx_reopen = 1;
683 break; 704 break;
705
706 case NGX_CMD_OPEN_CHANNEL:
707
708 ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
709 "get channel s:%d pid:" PID_T_FMT " fd:%d",
710 ch.slot, ch.pid, ch.fd);
711
712 ngx_processes[ch.slot].pid = ch.pid;
713 ngx_processes[ch.slot].channel[0] = ch.fd;
714 break;
684 } 715 }
685 } 716 }
686 717
687 718
688 #if (NGX_THREADS) 719 #if (NGX_THREADS)
773 msg.msg_name = NULL; 804 msg.msg_name = NULL;
774 msg.msg_namelen = 0; 805 msg.msg_namelen = 0;
775 msg.msg_iov = iov; 806 msg.msg_iov = iov;
776 msg.msg_iovlen = 1; 807 msg.msg_iovlen = 1;
777 808
778 n = sendmsg(s, &msg, MSG_DONTWAIT); 809 n = sendmsg(s, &msg, 0);
779 810
780 if (n == -1) { 811 if (n == -1) {
781 err = ngx_errno; 812 err = ngx_errno;
782 if (err == NGX_EAGAIN) { 813 if (err == NGX_EAGAIN) {
783 return NGX_AGAIN; 814 return NGX_AGAIN;
797 int fd; 828 int fd;
798 ssize_t n; 829 ssize_t n;
799 ngx_err_t err; 830 ngx_err_t err;
800 struct iovec iov[1]; 831 struct iovec iov[1];
801 struct msghdr msg; 832 struct msghdr msg;
802 struct cmsghdr *cm; 833 struct cmsghdr cm;
803 834
804 iov[0].iov_base = (char *) ch; 835 iov[0].iov_base = (char *) ch;
805 iov[0].iov_len = size; 836 iov[0].iov_len = size;
806 837
807 msg.msg_name = NULL; 838 msg.msg_name = NULL;
815 #else 846 #else
816 msg.msg_accrights = (caddr_t) &fd; 847 msg.msg_accrights = (caddr_t) &fd;
817 msg.msg_accrightslen = sizeof(int); 848 msg.msg_accrightslen = sizeof(int);
818 #endif 849 #endif
819 850
820 n = recvmsg(s, &msg, MSG_DONTWAIT); 851 n = recvmsg(s, &msg, 0);
821 852
822 if (n == -1) { 853 if (n == -1) {
823 err = ngx_errno; 854 err = ngx_errno;
824 if (err == NGX_EAGAIN) { 855 if (err == NGX_EAGAIN) {
825 return NGX_AGAIN; 856 return NGX_AGAIN;
836 } 867 }
837 868
838 #if (HAVE_MSGHDR_MSG_CONTROL) 869 #if (HAVE_MSGHDR_MSG_CONTROL)
839 870
840 if (ch->command == NGX_CMD_OPEN_CHANNEL) { 871 if (ch->command == NGX_CMD_OPEN_CHANNEL) {
841 cm = (struct cmsghdr *) msg.msg_control; 872
842 873 if (cm.cmsg_len < sizeof(struct cmsghdr) + sizeof(int)) {
843 if (cm == NULL) {
844 ngx_log_error(NGX_LOG_ALERT, log, 0,
845 "recvmsg() returned no ancillary data");
846 return NGX_ERROR;
847 }
848
849 if (cm->cmsg_len < sizeof(struct cmsghdr) + sizeof(int)) {
850 ngx_log_error(NGX_LOG_ALERT, log, 0, 874 ngx_log_error(NGX_LOG_ALERT, log, 0,
851 "recvmsg() returned too small ancillary data"); 875 "recvmsg() returned too small ancillary data");
852 return NGX_ERROR; 876 return NGX_ERROR;
853 } 877 }
854 878
855 if (cm->cmsg_level != SOL_SOCKET || cm->cmsg_type != SCM_RIGHTS) { 879 if (cm.cmsg_level != SOL_SOCKET || cm.cmsg_type != SCM_RIGHTS) {
856 ngx_log_error(NGX_LOG_ALERT, log, 0, 880 ngx_log_error(NGX_LOG_ALERT, log, 0,
857 "recvmsg() returned invalid ancillary data " 881 "recvmsg() returned invalid ancillary data "
858 "level %d or type %d", cm->cmsg_level, cm->cmsg_type); 882 "level %d or type %d", cm.cmsg_level, cm.cmsg_type);
859 return NGX_ERROR; 883 return NGX_ERROR;
860 } 884 }
861 885
862 ch->fd = *((int *) ((char *) cm + sizeof(struct cmsghdr))); 886 ch->fd = *((int *) ((char *) &cm + sizeof(struct cmsghdr)));
863 } 887 }
864 888
865 if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) { 889 if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
866 ngx_log_error(NGX_LOG_ALERT, log, 0, 890 ngx_log_error(NGX_LOG_ALERT, log, 0,
867 "recvmsg() truncated data"); 891 "recvmsg() truncated data");