Mercurial > hg > nginx
comparison src/os/unix/ngx_process_cycle.c @ 366:e411b1482ee3
nginx-0.0.7-2004-06-23-19:18:17 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 23 Jun 2004 15:18:17 +0000 |
parents | fd24ba70e1b3 |
children | 54f76b0b8dca |
comparison
equal
deleted
inserted
replaced
365:fd24ba70e1b3 | 366:e411b1482ee3 |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 | 5 #include <ngx_channel.h> |
6 #include <nginx.h> | |
7 | 6 |
8 | 7 |
9 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, | 8 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, |
10 ngx_int_t type); | 9 ngx_int_t type); |
11 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo); | 10 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo); |
32 sig_atomic_t ngx_reopen; | 31 sig_atomic_t ngx_reopen; |
33 | 32 |
34 sig_atomic_t ngx_change_binary; | 33 sig_atomic_t ngx_change_binary; |
35 ngx_pid_t ngx_new_binary; | 34 ngx_pid_t ngx_new_binary; |
36 ngx_uint_t ngx_inherited; | 35 ngx_uint_t ngx_inherited; |
36 ngx_uint_t ngx_daemonized; | |
37 | 37 |
38 sig_atomic_t ngx_noaccept; | 38 sig_atomic_t ngx_noaccept; |
39 ngx_uint_t ngx_noaccepting; | 39 ngx_uint_t ngx_noaccepting; |
40 ngx_uint_t ngx_restart; | 40 ngx_uint_t ngx_restart; |
41 | 41 |
607 | 607 |
608 #if 0 | 608 #if 0 |
609 ngx_last_process = 0; | 609 ngx_last_process = 0; |
610 #endif | 610 #endif |
611 | 611 |
612 c = &cycle->connections[ngx_channel]; | 612 if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT, |
613 ngx_memzero(c, sizeof(ngx_connection_t)); | 613 ngx_channel_handler) == NGX_ERROR) |
614 | 614 { |
615 c->fd = ngx_channel; | 615 /* fatal */ |
616 c->pool = cycle->pool; | 616 exit(2); |
617 c->read = &cycle->read_events[ngx_channel]; | |
618 c->write = &cycle->write_events[ngx_channel]; | |
619 | |
620 ngx_memzero(c->read, sizeof(ngx_event_t)); | |
621 ngx_memzero(c->write, sizeof(ngx_event_t)); | |
622 | |
623 c->log = cycle->log; | |
624 c->read->log = cycle->log; | |
625 c->write->log = cycle->log; | |
626 c->read->index = NGX_INVALID_INDEX; | |
627 c->write->index = NGX_INVALID_INDEX; | |
628 c->read->data = c; | |
629 c->write->data = c; | |
630 c->read->event_handler = ngx_channel_handler; | |
631 | |
632 if (ngx_add_conn) { | |
633 if (ngx_add_conn(c) == NGX_ERROR) { | |
634 /* fatal */ | |
635 exit(2); | |
636 } | |
637 | |
638 } else { | |
639 if (ngx_add_event(c->read, NGX_READ_EVENT, 0) == NGX_ERROR) { | |
640 /* fatal */ | |
641 exit(2); | |
642 } | |
643 } | 617 } |
644 | 618 |
645 ngx_setproctitle("worker process"); | 619 ngx_setproctitle("worker process"); |
646 | 620 |
647 #if (NGX_THREADS) | 621 #if (NGX_THREADS) |
811 | 785 |
812 return 1; | 786 return 1; |
813 } | 787 } |
814 | 788 |
815 #endif | 789 #endif |
816 | |
817 | |
818 ngx_int_t ngx_write_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size, | |
819 ngx_log_t *log) | |
820 { | |
821 ssize_t n; | |
822 ngx_err_t err; | |
823 struct iovec iov[1]; | |
824 struct msghdr msg; | |
825 | |
826 #if (HAVE_MSGHDR_MSG_CONTROL) | |
827 | |
828 union { | |
829 struct cmsghdr cm; | |
830 char space[CMSG_SPACE(sizeof(int))]; | |
831 } cmsg; | |
832 | |
833 if (ch->fd == -1) { | |
834 msg.msg_control = NULL; | |
835 msg.msg_controllen = 0; | |
836 | |
837 } else { | |
838 msg.msg_control = (caddr_t) &cmsg; | |
839 msg.msg_controllen = sizeof(cmsg); | |
840 | |
841 cmsg.cm.cmsg_len = sizeof(cmsg); | |
842 cmsg.cm.cmsg_level = SOL_SOCKET; | |
843 cmsg.cm.cmsg_type = SCM_RIGHTS; | |
844 *(int *) CMSG_DATA(&cmsg.cm) = ch->fd; | |
845 } | |
846 | |
847 #else | |
848 | |
849 if (ch->fd == -1) { | |
850 msg.msg_accrights = NULL; | |
851 msg.msg_accrightslen = 0; | |
852 | |
853 } else { | |
854 msg.msg_accrights = (caddr_t) &ch->fd; | |
855 msg.msg_accrightslen = sizeof(int); | |
856 } | |
857 | |
858 #endif | |
859 | |
860 iov[0].iov_base = (char *) ch; | |
861 iov[0].iov_len = size; | |
862 | |
863 msg.msg_name = NULL; | |
864 msg.msg_namelen = 0; | |
865 msg.msg_iov = iov; | |
866 msg.msg_iovlen = 1; | |
867 | |
868 n = sendmsg(s, &msg, 0); | |
869 | |
870 if (n == -1) { | |
871 err = ngx_errno; | |
872 if (err == NGX_EAGAIN) { | |
873 return NGX_AGAIN; | |
874 } | |
875 | |
876 ngx_log_error(NGX_LOG_ALERT, log, err, "sendmsg() failed"); | |
877 return NGX_ERROR; | |
878 } | |
879 | |
880 return NGX_OK; | |
881 } | |
882 | |
883 | |
884 ngx_int_t ngx_read_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size, | |
885 ngx_log_t *log) | |
886 { | |
887 ssize_t n; | |
888 ngx_err_t err; | |
889 struct iovec iov[1]; | |
890 struct msghdr msg; | |
891 | |
892 #if (HAVE_MSGHDR_MSG_CONTROL) | |
893 union { | |
894 struct cmsghdr cm; | |
895 char space[CMSG_SPACE(sizeof(int))]; | |
896 } cmsg; | |
897 #else | |
898 int fd; | |
899 #endif | |
900 | |
901 iov[0].iov_base = (char *) ch; | |
902 iov[0].iov_len = size; | |
903 | |
904 msg.msg_name = NULL; | |
905 msg.msg_namelen = 0; | |
906 msg.msg_iov = iov; | |
907 msg.msg_iovlen = 1; | |
908 | |
909 #if (HAVE_MSGHDR_MSG_CONTROL) | |
910 msg.msg_control = (caddr_t) &cmsg; | |
911 msg.msg_controllen = sizeof(cmsg); | |
912 #else | |
913 msg.msg_accrights = (caddr_t) &fd; | |
914 msg.msg_accrightslen = sizeof(int); | |
915 #endif | |
916 | |
917 n = recvmsg(s, &msg, 0); | |
918 | |
919 if (n == -1) { | |
920 err = ngx_errno; | |
921 if (err == NGX_EAGAIN) { | |
922 return NGX_AGAIN; | |
923 } | |
924 | |
925 ngx_log_error(NGX_LOG_ALERT, log, err, "recvmsg() failed"); | |
926 return NGX_ERROR; | |
927 } | |
928 | |
929 if ((size_t) n < sizeof(ngx_channel_t)) { | |
930 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
931 "recvmsg() returned not enough data"); | |
932 return NGX_ERROR; | |
933 } | |
934 | |
935 #if (HAVE_MSGHDR_MSG_CONTROL) | |
936 | |
937 if (ch->command == NGX_CMD_OPEN_CHANNEL) { | |
938 | |
939 if (cmsg.cm.cmsg_len < sizeof(cmsg)) { | |
940 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
941 "recvmsg() returned too small ancillary data"); | |
942 return NGX_ERROR; | |
943 } | |
944 | |
945 if (cmsg.cm.cmsg_level != SOL_SOCKET || cmsg.cm.cmsg_type != SCM_RIGHTS) | |
946 { | |
947 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
948 "recvmsg() returned invalid ancillary data " | |
949 "level %d or type %d", | |
950 cmsg.cm.cmsg_level, cmsg.cm.cmsg_type); | |
951 return NGX_ERROR; | |
952 } | |
953 | |
954 ch->fd = *(int *) CMSG_DATA(&cmsg.cm); | |
955 } | |
956 | |
957 if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) { | |
958 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
959 "recvmsg() truncated data"); | |
960 } | |
961 | |
962 #else | |
963 | |
964 if (ch->command == NGX_CMD_OPEN_CHANNEL) { | |
965 if (msg.msg_accrightslen != sizeof(int)) { | |
966 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
967 "recvmsg() returned no ancillary data"); | |
968 return NGX_ERROR; | |
969 } | |
970 | |
971 ch->fd = fd; | |
972 } | |
973 | |
974 #endif | |
975 | |
976 return n; | |
977 } |