comparison src/core/nginx.c @ 2:ffffe1499bce

nginx-0.0.1-2002-08-16-19:27:03 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 16 Aug 2002 15:27:03 +0000
parents d220029ac7f3
children 34a521b1a148
comparison
equal deleted inserted replaced
1:d220029ac7f3 2:ffffe1499bce
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_log.h> 6 #include <ngx_log.h>
7 #include <ngx_alloc.h> 7 #include <ngx_alloc.h>
8 #include <ngx_array.h>
9 #include <ngx_socket.h>
8 #include <ngx_server.h> 10 #include <ngx_server.h>
9 #include <ngx_connection.h> 11 #include <ngx_connection.h>
10 #include <ngx_listen.h> 12 #include <ngx_listen.h>
11 13
12 /* 14 /* STUB */
13 #include <ngx_http.h> 15 #include <ngx_http.h>
14 */ 16 /* */
15 17
16
17 #if !(WIN32)
18 static int ngx_options(int argc, char *const *argv);
19 #endif
20
21 char *ngx_root = "/home/is/work/xml/xml/html";
22
23 int ngx_http_init_connection(void *data);
24 18
25 19
26 int ngx_max_conn = 512; 20 int ngx_max_conn = 512;
27 struct sockaddr_in ngx_addr = {0, AF_INET, 0, 0, 0};
28 int ngx_backlog = 0;
29 21
30 ngx_pool_t ngx_pool; 22 ngx_pool_t ngx_pool;
31 ngx_log_t ngx_log; 23 ngx_log_t ngx_log;
32 ngx_server_t ngx_server; 24 ngx_server_t ngx_server;
33 25
34 26
27 ngx_array_t ngx_listening_sockets;
28
29
35 int main(int argc, char *const *argv) 30 int main(int argc, char *const *argv)
36 { 31 {
37 char addr_text[22]; 32 int i;
38 ngx_socket_t s; 33 ngx_socket_t s;
39 ngx_listen_t ls; 34 ngx_listen_t *ls;
40 int reuseaddr = 1;
41 #if (WIN32)
42 WSADATA wsd;
43 unsigned long nb = 1;
44 #endif
45 35
36 int reuseaddr = 1;
46 37
47 ngx_log.log_level = NGX_LOG_DEBUG; 38 ngx_log.log_level = NGX_LOG_DEBUG;
48 ngx_pool.log = &ngx_log; 39 ngx_pool.log = &ngx_log;
49 ngx_addr.sin_port = htons(8000);
50 ngx_addr.sin_family = AF_INET;
51 40
52 #if !(WIN32) 41 ngx_init_sockets(&ngx_log);
53 if (ngx_options(argc, argv) == -1)
54 ngx_log_error(NGX_LOG_EMERG, (&ngx_log), 0, "invalid argument");
55 #endif
56 42
57 ngx_log_debug((&ngx_log), "%d, %s:%d" _ ngx_max_conn _ 43 /* TODO: read config */
58 inet_ntoa(ngx_addr.sin_addr) _ ntohs(ngx_addr.sin_port));
59 44
60 #if (WIN32) 45 /* STUB */
61 if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) 46 /* TODO: init chain of global modules (like ngx_http.c),
62 ngx_log_error(NGX_LOG_EMERG, (&ngx_log), ngx_socket_errno, 47 they would init its modules and ngx_listening_sockets */
63 "WSAStartup failed"); 48 ngx_http_init();
64 #endif
65 49
66 /* for each listening socket */ 50 /* for each listening socket */
67 s = socket(AF_INET, SOCK_STREAM, 0); 51 ls = (ngx_listen_t *) ngx_listening_sockets.elts;
68 if (s == -1) 52 for (i = 0; i < ngx_listening_sockets.nelts; i++) {
69 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, 53 s = socket(ls->family, ls->type, ls->protocol);
70 "socket failed"); 54 if (s == -1)
55 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
56 "nginx: socket %s falied", ls->addr_text);
71 57
72 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 58 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
73 (const void *) &reuseaddr, sizeof(int)) == -1) 59 (const void *) &reuseaddr, sizeof(int)) == -1)
74 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, 60 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
75 "setsockopt (SO_REUSEADDR) failed"); 61 "nginx: setsockopt (SO_REUSEADDR) %s failed",
62 ls->addr_text);
76 63
77 #if (WIN32) 64 /* TODO: close on exit */
78 if (ioctlsocket(s, FIONBIO, &nb) == -1)
79 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
80 "ioctlsocket (FIONBIO) failed");
81 #else
82 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
83 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
84 "fcntl (O_NONBLOCK) failed");
85 #endif
86 65
87 ngx_snprintf(ngx_cpystrn(addr_text, inet_ntoa(ngx_addr.sin_addr), 16), 66 if (ngx_nonblocking(s) == -1)
88 7, ":%d", ntohs(ngx_addr.sin_port)); 67 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
68 ngx_nonblocking_n " %s failed", ls->addr_text);
89 69
90 if (bind(s, (struct sockaddr *) &ngx_addr, 70 if (bind(s, (struct sockaddr *) ls->addr, ls->addr_len) == -1)
91 sizeof(struct sockaddr_in)) == -1) 71 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
92 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, 72 "bind to %s failed", ls->addr_text);
93 "bind to %s failed", addr_text);
94 73
95 if (listen(s, ngx_backlog) == -1) 74 if (listen(s, ls->backlog) == -1)
96 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno, 75 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
97 "listen to %s failed", addr_text); 76 "listen to %s failed", ls->addr_text);
98 77
99 ngx_server.buff_size = 1024; 78 /* TODO: deferred accept */
100 ngx_server.handler = ngx_http_init_connection;
101 79
102 /* daemon */ 80 ls->fd = s;
81 ls->server = &ngx_http_server;
82 ls->log = &ngx_log;
83 }
103 84
104 ls.fd = s; 85 /* TODO: daemon */
105 ls.server = &ngx_server;
106 ls.log = &ngx_log;
107 86
108 /* fork */ 87 /* TODO: fork */
109 88
89 /* TODO: events: init ngx_connections and listen slots */
90
91 /* TODO: threads */
92
93 /* STUB */
110 ngx_worker(&ls, 1, &ngx_pool, &ngx_log); 94 ngx_worker(&ls, 1, &ngx_pool, &ngx_log);
111 } 95 }
112
113 #if !(WIN32)
114 extern char *optarg;
115
116 static int ngx_options(int argc, char *const *argv)
117 {
118 char ch, *pos;
119 int port;
120
121 while ((ch = getopt(argc, argv, "l:c:")) != -1) {
122 switch (ch) {
123 case 'l':
124 if (pos = strchr(optarg, ':')) {
125 *(pos) = '\0';
126 if ((port = atoi(pos + 1)) <= 0)
127 return -1;
128 ngx_addr.sin_port = htons(port);
129 }
130
131 if ((ngx_addr.sin_addr.s_addr = inet_addr(optarg)) == INADDR_NONE)
132 return -1;
133 break;
134
135 case 'c':
136 if ((ngx_max_conn = atoi(optarg)) <= 0)
137 return -1;
138 break;
139
140 case '?':
141 default:
142 return -1;
143 }
144
145 }
146
147 return 0;
148 }
149 #endif