comparison src/core/nginx.c @ 6:669801705ab1

nginx-0.0.1-2002-08-26-19:18:19 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 26 Aug 2002 15:18:19 +0000
parents 34a521b1a148
children 708f8bb772ec
comparison
equal deleted inserted replaced
5:62b1a364857c 6:669801705ab1
83 failed = 0; 83 failed = 0;
84 84
85 /* for each listening socket */ 85 /* for each listening socket */
86 ls = (ngx_listen_t *) ngx_listening_sockets->elts; 86 ls = (ngx_listen_t *) ngx_listening_sockets->elts;
87 for (i = 0; i < ngx_listening_sockets->nelts; i++) { 87 for (i = 0; i < ngx_listening_sockets->nelts; i++) {
88
88 if (ls[i].done) 89 if (ls[i].done)
89 continue; 90 continue;
90 91
91 #if (WIN32) 92 if (ls[i].inherited) {
92 s = WSASocket(ls[i].family, ls[i].type, ls[i].protocol, NULL, 0, 0); 93
93 #else 94 /* TODO: close on exit */
94 s = socket(ls[i].family, ls[i].type, ls[i].protocol); 95 /* TODO: nonblocking */
95 #endif 96 /* TODO: deferred accept */
96 if (s == -1) 97
98 ls[i].done = 1;
99 continue;
100 }
101
102 s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol,
103 ls[i].flags);
104 if (s == -1) {
97 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 105 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
98 "nginx: socket %s falied", ls[i].addr_text); 106 ngx_socket_n " %s falied", ls[i].addr_text);
107 exit(1);
108 }
99 109
100 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 110 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
101 (const void *) &reuseaddr, sizeof(int)) == -1) 111 (const void *) &reuseaddr, sizeof(int)) == -1) {
102 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 112 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
103 "nginx: setsockopt (SO_REUSEADDR) %s failed", 113 "setsockopt(SO_REUSEADDR) %s failed",
104 ls[i].addr_text); 114 ls[i].addr_text);
115 exit(1);
116 }
105 117
106 /* TODO: close on exit */ 118 /* TODO: close on exit */
107 119
108 if (ls[i].nonblocking) { 120 if (ls[i].nonblocking) {
109 if (ngx_nonblocking(s) == -1) 121 if (ngx_nonblocking(s) == -1) {
110 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 122 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
111 ngx_nonblocking_n " %s failed", 123 ngx_nonblocking_n " %s failed",
112 ls[i].addr_text); 124 ls[i].addr_text);
125 exit(1);
126 }
113 } 127 }
114 128
115 if (bind(s, (struct sockaddr *) ls[i].addr, ls[i].addr_len) == -1) { 129 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
116 err = ngx_socket_errno; 130 err = ngx_socket_errno;
117 ngx_log_error(NGX_LOG_ALERT, log, err, 131 ngx_log_error(NGX_LOG_EMERG, log, err,
118 "bind to %s failed", ls[i].addr_text); 132 "bind() to %s failed", ls[i].addr_text);
119 133
120 if (err != NGX_EADDRINUSE) 134 if (err != NGX_EADDRINUSE)
121 exit(1); 135 exit(1);
122 136
123 if (ngx_close_socket(s) == -1) 137 if (ngx_close_socket(s) == -1)
124 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, 138 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
125 ngx_close_socket_n " %s failed", 139 ngx_close_socket_n " %s failed",
126 ls[i].addr_text); 140 ls[i].addr_text);
127 141
128 failed = 1; 142 failed = 1;
129 continue; 143 continue;
130 } 144 }
131 145
132 if (listen(s, ls[i].backlog) == -1) 146 if (listen(s, ls[i].backlog) == -1) {
133 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 147 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
134 "listen to %s failed", ls[i].addr_text); 148 "listen() to %s failed", ls[i].addr_text);
149 exit(1);
150 }
135 151
136 /* TODO: deferred accept */ 152 /* TODO: deferred accept */
137 153
138 ls[i].fd = s; 154 ls[i].fd = s;
139 ls[i].done = 1; 155 ls[i].done = 1;
140 } 156 }
141 157
142 if (!failed) 158 if (!failed)
143 break; 159 break;
144 160
145 ngx_log_error(NGX_LOG_NOTICE, log, 0, "try to bind again after 500ms"); 161 ngx_log_error(NGX_LOG_NOTICE, log, 0,
162 "try again to bind() after 500ms");
146 ngx_msleep(500); 163 ngx_msleep(500);
147 } 164 }
148 165
149 if (failed) 166 if (failed) {
150 ngx_log_error(NGX_LOG_EMERG, log, 0, "can't bind"); 167 ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting");
168 exit(1);
169 }
151 } 170 }