Mercurial > hg > nginx-quic
comparison src/core/nginx.c @ 3:34a521b1a148
nginx-0.0.1-2002-08-20-18:48:28 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 20 Aug 2002 14:48:28 +0000 |
parents | ffffe1499bce |
children | 669801705ab1 |
comparison
equal
deleted
inserted
replaced
2:ffffe1499bce | 3:34a521b1a148 |
---|---|
1 | 1 |
2 #include <nginx.h> | 2 #include <nginx.h> |
3 | 3 |
4 #include <ngx_config.h> | 4 #include <ngx_config.h> |
5 #include <ngx_string.h> | 5 #include <ngx_string.h> |
6 #include <ngx_errno.h> | |
7 #include <ngx_time.h> | |
6 #include <ngx_log.h> | 8 #include <ngx_log.h> |
7 #include <ngx_alloc.h> | 9 #include <ngx_alloc.h> |
8 #include <ngx_array.h> | 10 #include <ngx_array.h> |
9 #include <ngx_socket.h> | 11 #include <ngx_socket.h> |
10 #include <ngx_server.h> | 12 #include <ngx_server.h> |
14 /* STUB */ | 16 /* STUB */ |
15 #include <ngx_http.h> | 17 #include <ngx_http.h> |
16 /* */ | 18 /* */ |
17 | 19 |
18 | 20 |
21 static void ngx_open_listening_sockets(ngx_log_t *log); | |
19 | 22 |
23 | |
24 /* STUB */ | |
20 int ngx_max_conn = 512; | 25 int ngx_max_conn = 512; |
21 | 26 |
22 ngx_pool_t ngx_pool; | 27 ngx_server_t ngx_server; |
23 ngx_log_t ngx_log; | 28 /* */ |
24 ngx_server_t ngx_server; | 29 |
30 ngx_log_t ngx_log; | |
31 ngx_pool_t *ngx_pool; | |
25 | 32 |
26 | 33 |
27 ngx_array_t ngx_listening_sockets; | 34 ngx_array_t *ngx_listening_sockets; |
28 | 35 |
29 | 36 |
30 int main(int argc, char *const *argv) | 37 int main(int argc, char *const *argv) |
31 { | 38 { |
32 int i; | 39 int i; |
33 ngx_socket_t s; | |
34 ngx_listen_t *ls; | |
35 | 40 |
36 int reuseaddr = 1; | 41 /* STUB */ |
42 ngx_log.log_level = NGX_LOG_DEBUG; | |
37 | 43 |
38 ngx_log.log_level = NGX_LOG_DEBUG; | 44 ngx_pool = ngx_create_pool(16 * 1024, &ngx_log); |
39 ngx_pool.log = &ngx_log; | 45 /* */ |
40 | 46 |
41 ngx_init_sockets(&ngx_log); | 47 ngx_init_sockets(&ngx_log); |
42 | 48 |
43 /* TODO: read config */ | 49 /* TODO: read config */ |
44 | 50 |
51 ngx_test_null(ngx_listening_sockets, | |
52 ngx_create_array(ngx_pool, 10, sizeof(ngx_listen_t)), 1); | |
53 | |
45 /* STUB */ | 54 /* STUB */ |
46 /* TODO: init chain of global modules (like ngx_http.c), | 55 /* TODO: init chain of global modules (like ngx_http.c), |
47 they would init its modules and ngx_listening_sockets */ | 56 they would init its modules and ngx_listening_sockets */ |
48 ngx_http_init(); | 57 ngx_http_init(ngx_pool, &ngx_log); |
49 | 58 |
50 /* for each listening socket */ | 59 ngx_open_listening_sockets(&ngx_log); |
51 ls = (ngx_listen_t *) ngx_listening_sockets.elts; | |
52 for (i = 0; i < ngx_listening_sockets.nelts; i++) { | |
53 s = socket(ls->family, ls->type, ls->protocol); | |
54 if (s == -1) | |
55 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, | |
56 "nginx: socket %s falied", ls->addr_text); | |
57 | |
58 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | |
59 (const void *) &reuseaddr, sizeof(int)) == -1) | |
60 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, | |
61 "nginx: setsockopt (SO_REUSEADDR) %s failed", | |
62 ls->addr_text); | |
63 | |
64 /* TODO: close on exit */ | |
65 | |
66 if (ngx_nonblocking(s) == -1) | |
67 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, | |
68 ngx_nonblocking_n " %s failed", ls->addr_text); | |
69 | |
70 if (bind(s, (struct sockaddr *) ls->addr, ls->addr_len) == -1) | |
71 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, | |
72 "bind to %s failed", ls->addr_text); | |
73 | |
74 if (listen(s, ls->backlog) == -1) | |
75 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, | |
76 "listen to %s failed", ls->addr_text); | |
77 | |
78 /* TODO: deferred accept */ | |
79 | |
80 ls->fd = s; | |
81 ls->server = &ngx_http_server; | |
82 ls->log = &ngx_log; | |
83 } | |
84 | 60 |
85 /* TODO: daemon */ | 61 /* TODO: daemon */ |
86 | 62 |
87 /* TODO: fork */ | 63 /* TODO: fork */ |
88 | 64 |
89 /* TODO: events: init ngx_connections and listen slots */ | 65 ngx_pre_thread(ngx_listening_sockets, ngx_pool, &ngx_log); |
90 | 66 |
91 /* TODO: threads */ | 67 /* TODO: threads */ |
92 | 68 |
93 /* STUB */ | 69 /* STUB */ |
94 ngx_worker(&ls, 1, &ngx_pool, &ngx_log); | 70 ngx_worker(&ngx_log); |
95 } | 71 } |
72 | |
73 static void ngx_open_listening_sockets(ngx_log_t *log) | |
74 { | |
75 int times, failed, reuseaddr, i; | |
76 ngx_err_t err; | |
77 ngx_socket_t s; | |
78 ngx_listen_t *ls; | |
79 | |
80 reuseaddr = 1; | |
81 | |
82 for (times = 10; times; times--) { | |
83 failed = 0; | |
84 | |
85 /* for each listening socket */ | |
86 ls = (ngx_listen_t *) ngx_listening_sockets->elts; | |
87 for (i = 0; i < ngx_listening_sockets->nelts; i++) { | |
88 if (ls[i].done) | |
89 continue; | |
90 | |
91 #if (WIN32) | |
92 s = WSASocket(ls[i].family, ls[i].type, ls[i].protocol, NULL, 0, 0); | |
93 #else | |
94 s = socket(ls[i].family, ls[i].type, ls[i].protocol); | |
95 #endif | |
96 if (s == -1) | |
97 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
98 "nginx: socket %s falied", ls[i].addr_text); | |
99 | |
100 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | |
101 (const void *) &reuseaddr, sizeof(int)) == -1) | |
102 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
103 "nginx: setsockopt (SO_REUSEADDR) %s failed", | |
104 ls[i].addr_text); | |
105 | |
106 /* TODO: close on exit */ | |
107 | |
108 if (ls[i].nonblocking) { | |
109 if (ngx_nonblocking(s) == -1) | |
110 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
111 ngx_nonblocking_n " %s failed", | |
112 ls[i].addr_text); | |
113 } | |
114 | |
115 if (bind(s, (struct sockaddr *) ls[i].addr, ls[i].addr_len) == -1) { | |
116 err = ngx_socket_errno; | |
117 ngx_log_error(NGX_LOG_ALERT, log, err, | |
118 "bind to %s failed", ls[i].addr_text); | |
119 | |
120 if (err != NGX_EADDRINUSE) | |
121 exit(1); | |
122 | |
123 if (ngx_close_socket(s) == -1) | |
124 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, | |
125 ngx_close_socket_n " %s failed", | |
126 ls[i].addr_text); | |
127 | |
128 failed = 1; | |
129 continue; | |
130 } | |
131 | |
132 if (listen(s, ls[i].backlog) == -1) | |
133 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
134 "listen to %s failed", ls[i].addr_text); | |
135 | |
136 /* TODO: deferred accept */ | |
137 | |
138 ls[i].fd = s; | |
139 ls[i].done = 1; | |
140 } | |
141 | |
142 if (!failed) | |
143 break; | |
144 | |
145 ngx_log_error(NGX_LOG_NOTICE, log, 0, "try to bind again after 500ms"); | |
146 ngx_msleep(500); | |
147 } | |
148 | |
149 if (failed) | |
150 ngx_log_error(NGX_LOG_EMERG, log, 0, "can't bind"); | |
151 } |