comparison src/core/nginx.c @ 98:c9b243802a17

nginx-0.0.1-2003-05-30-18:27:59 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 30 May 2003 14:27:59 +0000
parents 70d2345a903f
children 7ebc8b7fb816
comparison
equal deleted inserted replaced
97:70d2345a903f 98:c9b243802a17
1
2
3 #include <ngx_config.h>
4 #include <ngx_core.h>
5
6 #include <ngx_listen.h>
1 7
2 #include <nginx.h> 8 #include <nginx.h>
3 9
4 #include <ngx_config.h> 10
5 11
6 #include <ngx_core.h> 12 static int ngx_open_listening_sockets(ngx_log_t *log);
7 #include <ngx_connection.h> 13
8 #include <ngx_os_init.h>
9 #include <ngx_server.h>
10 #include <ngx_listen.h>
11 #include <ngx_conf_file.h>
12
13 /* STUB */
14 #include <ngx_http.h>
15 /* */
16
17
18 static void ngx_set_signals(ngx_log_t *log);
19 static void ngx_open_listening_sockets(ngx_log_t *log);
20
21
22 /* STUB */
23 int ngx_max_conn = 512;
24 u_int ngx_sendfile_flags;
25
26 ngx_server_t ngx_server;
27 /* */
28 14
29 ngx_log_t ngx_log; 15 ngx_log_t ngx_log;
30 ngx_pool_t *ngx_pool; 16 ngx_pool_t *ngx_pool;
31 void ****ngx_conf_ctx; 17 void ****ngx_conf_ctx;
32 18
33 19
34 ngx_os_io_t ngx_io; 20 ngx_os_io_t ngx_io;
35 21
36 22
37 int ngx_max_module; 23 int ngx_max_module;
38 void *ctx_conf; 24 void *ctx_conf;
39 25
40 int ngx_connection_counter; 26 int ngx_connection_counter;
41 27
42 ngx_array_t ngx_listening_sockets; 28 ngx_array_t ngx_listening_sockets;
43 29
107 return 1; 93 return 1;
108 } 94 }
109 } 95 }
110 } 96 }
111 97
112 ngx_open_listening_sockets(&ngx_log); 98 if (ngx_open_listening_sockets(&ngx_log) == NGX_ERROR) {
99 return 1;
100 }
113 101
114 /* TODO: daemon, once only */ 102 /* TODO: daemon, once only */
115 103
116 /* TODO: fork */ 104 /* TODO: fork */
117 105
125 113
126 return 0; 114 return 0;
127 } 115 }
128 116
129 117
130 static void ngx_open_listening_sockets(ngx_log_t *log) 118 static int ngx_open_listening_sockets(ngx_log_t *log)
131 { 119 {
132 int times, failed, reuseaddr, i; 120 int times, failed, reuseaddr, i;
133 ngx_err_t err; 121 ngx_err_t err;
134 ngx_socket_t s; 122 ngx_socket_t s;
135 ngx_listen_t *ls; 123 ngx_listen_t *ls;
159 s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol, 147 s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol,
160 ls[i].flags); 148 ls[i].flags);
161 if (s == -1) { 149 if (s == -1) {
162 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 150 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
163 ngx_socket_n " %s falied", ls[i].addr_text.data); 151 ngx_socket_n " %s falied", ls[i].addr_text.data);
164 exit(1); 152 return NGX_ERROR;
165 } 153 }
166 154
167 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 155 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
168 (const void *) &reuseaddr, sizeof(int)) == -1) { 156 (const void *) &reuseaddr, sizeof(int)) == -1) {
169 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 157 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
170 "setsockopt(SO_REUSEADDR) %s failed", 158 "setsockopt(SO_REUSEADDR) %s failed",
171 ls[i].addr_text.data); 159 ls[i].addr_text.data);
172 exit(1); 160 return NGX_ERROR;
173 } 161 }
174 162
175 /* TODO: close on exit */ 163 /* TODO: close on exit */
176 164
177 if (ls[i].nonblocking) { 165 if (ls[i].nonblocking) {
178 if (ngx_nonblocking(s) == -1) { 166 if (ngx_nonblocking(s) == -1) {
179 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 167 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
180 ngx_nonblocking_n " %s failed", 168 ngx_nonblocking_n " %s failed",
181 ls[i].addr_text.data); 169 ls[i].addr_text.data);
182 exit(1); 170 return NGX_ERROR;
183 } 171 }
184 } 172 }
185 173
186 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) { 174 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
187 err = ngx_socket_errno; 175 err = ngx_socket_errno;
188 ngx_log_error(NGX_LOG_EMERG, log, err, 176 ngx_log_error(NGX_LOG_EMERG, log, err,
189 "bind() to %s failed", ls[i].addr_text.data); 177 "bind() to %s failed", ls[i].addr_text.data);
190 178
191 if (err != NGX_EADDRINUSE) 179 if (err != NGX_EADDRINUSE)
192 exit(1); 180 return NGX_ERROR;
193 181
194 if (ngx_close_socket(s) == -1) 182 if (ngx_close_socket(s) == -1)
195 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 183 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
196 ngx_close_socket_n " %s failed", 184 ngx_close_socket_n " %s failed",
197 ls[i].addr_text.data); 185 ls[i].addr_text.data);
201 } 189 }
202 190
203 if (listen(s, ls[i].backlog) == -1) { 191 if (listen(s, ls[i].backlog) == -1) {
204 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, 192 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
205 "listen() to %s failed", ls[i].addr_text.data); 193 "listen() to %s failed", ls[i].addr_text.data);
206 exit(1); 194 return NGX_ERROR;
207 } 195 }
208 196
209 /* TODO: deferred accept */ 197 /* TODO: deferred accept */
210 198
211 ls[i].fd = s; 199 ls[i].fd = s;
220 ngx_msleep(500); 208 ngx_msleep(500);
221 } 209 }
222 210
223 if (failed) { 211 if (failed) {
224 ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting"); 212 ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting");
225 exit(1); 213 return NGX_ERROR;
226 } 214 }
215
216 return NGX_OK;
227 } 217 }