aboutsummaryrefslogtreecommitdiff
path: root/src/core/nginx.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2002-08-16 15:27:03 +0000
committerIgor Sysoev <igor@sysoev.ru>2002-08-16 15:27:03 +0000
commite0af1b89dcd100462a3195534b2f78a838ca85b5 (patch)
tree9c9eb63c71b13cc9159209e39eb099c54c6cb6b1 /src/core/nginx.c
parent0c331d9f666b4f9be91009b7caea457e58a80779 (diff)
downloadnginx-e0af1b89dcd100462a3195534b2f78a838ca85b5.tar.gz
nginx-e0af1b89dcd100462a3195534b2f78a838ca85b5.zip
nginx-0.0.1-2002-08-16-19:27:03 import
Diffstat (limited to 'src/core/nginx.c')
-rw-r--r--src/core/nginx.c164
1 files changed, 55 insertions, 109 deletions
diff --git a/src/core/nginx.c b/src/core/nginx.c
index ab287f283..a90b21ed3 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -5,145 +5,91 @@
#include <ngx_string.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
+#include <ngx_array.h>
+#include <ngx_socket.h>
#include <ngx_server.h>
#include <ngx_connection.h>
#include <ngx_listen.h>
-/*
+/* STUB */
#include <ngx_http.h>
-*/
+/* */
-#if !(WIN32)
-static int ngx_options(int argc, char *const *argv);
-#endif
-
-char *ngx_root = "/home/is/work/xml/xml/html";
-
-int ngx_http_init_connection(void *data);
-
int ngx_max_conn = 512;
-struct sockaddr_in ngx_addr = {0, AF_INET, 0, 0, 0};
-int ngx_backlog = 0;
ngx_pool_t ngx_pool;
ngx_log_t ngx_log;
ngx_server_t ngx_server;
+ngx_array_t ngx_listening_sockets;
+
+
int main(int argc, char *const *argv)
{
- char addr_text[22];
- ngx_socket_t s;
- ngx_listen_t ls;
- int reuseaddr = 1;
-#if (WIN32)
- WSADATA wsd;
- unsigned long nb = 1;
-#endif
+ int i;
+ ngx_socket_t s;
+ ngx_listen_t *ls;
+ int reuseaddr = 1;
ngx_log.log_level = NGX_LOG_DEBUG;
ngx_pool.log = &ngx_log;
- ngx_addr.sin_port = htons(8000);
- ngx_addr.sin_family = AF_INET;
-#if !(WIN32)
- if (ngx_options(argc, argv) == -1)
- ngx_log_error(NGX_LOG_EMERG, (&ngx_log), 0, "invalid argument");
-#endif
+ ngx_init_sockets(&ngx_log);
- ngx_log_debug((&ngx_log), "%d, %s:%d" _ ngx_max_conn _
- inet_ntoa(ngx_addr.sin_addr) _ ntohs(ngx_addr.sin_port));
+ /* TODO: read config */
-#if (WIN32)
- if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
- ngx_log_error(NGX_LOG_EMERG, (&ngx_log), ngx_socket_errno,
- "WSAStartup failed");
-#endif
+ /* STUB */
+ /* TODO: init chain of global modules (like ngx_http.c),
+ they would init its modules and ngx_listening_sockets */
+ ngx_http_init();
/* for each listening socket */
- s = socket(AF_INET, SOCK_STREAM, 0);
- if (s == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "socket failed");
-
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- (const void *) &reuseaddr, sizeof(int)) == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "setsockopt (SO_REUSEADDR) failed");
-
-#if (WIN32)
- if (ioctlsocket(s, FIONBIO, &nb) == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "ioctlsocket (FIONBIO) failed");
-#else
- if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "fcntl (O_NONBLOCK) failed");
-#endif
-
- ngx_snprintf(ngx_cpystrn(addr_text, inet_ntoa(ngx_addr.sin_addr), 16),
- 7, ":%d", ntohs(ngx_addr.sin_port));
-
- if (bind(s, (struct sockaddr *) &ngx_addr,
- sizeof(struct sockaddr_in)) == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "bind to %s failed", addr_text);
-
- if (listen(s, ngx_backlog) == -1)
- ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
- "listen to %s failed", addr_text);
+ ls = (ngx_listen_t *) ngx_listening_sockets.elts;
+ for (i = 0; i < ngx_listening_sockets.nelts; i++) {
+ s = socket(ls->family, ls->type, ls->protocol);
+ if (s == -1)
+ ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
+ "nginx: socket %s falied", ls->addr_text);
+
+ if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
+ (const void *) &reuseaddr, sizeof(int)) == -1)
+ ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
+ "nginx: setsockopt (SO_REUSEADDR) %s failed",
+ ls->addr_text);
+
+ /* TODO: close on exit */
+
+ if (ngx_nonblocking(s) == -1)
+ ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
+ ngx_nonblocking_n " %s failed", ls->addr_text);
+
+ if (bind(s, (struct sockaddr *) ls->addr, ls->addr_len) == -1)
+ ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
+ "bind to %s failed", ls->addr_text);
+
+ if (listen(s, ls->backlog) == -1)
+ ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
+ "listen to %s failed", ls->addr_text);
+
+ /* TODO: deferred accept */
+
+ ls->fd = s;
+ ls->server = &ngx_http_server;
+ ls->log = &ngx_log;
+ }
- ngx_server.buff_size = 1024;
- ngx_server.handler = ngx_http_init_connection;
+ /* TODO: daemon */
- /* daemon */
+ /* TODO: fork */
- ls.fd = s;
- ls.server = &ngx_server;
- ls.log = &ngx_log;
+ /* TODO: events: init ngx_connections and listen slots */
- /* fork */
+ /* TODO: threads */
+ /* STUB */
ngx_worker(&ls, 1, &ngx_pool, &ngx_log);
}
-
-#if !(WIN32)
-extern char *optarg;
-
-static int ngx_options(int argc, char *const *argv)
-{
- char ch, *pos;
- int port;
-
- while ((ch = getopt(argc, argv, "l:c:")) != -1) {
- switch (ch) {
- case 'l':
- if (pos = strchr(optarg, ':')) {
- *(pos) = '\0';
- if ((port = atoi(pos + 1)) <= 0)
- return -1;
- ngx_addr.sin_port = htons(port);
- }
-
- if ((ngx_addr.sin_addr.s_addr = inet_addr(optarg)) == INADDR_NONE)
- return -1;
- break;
-
- case 'c':
- if ((ngx_max_conn = atoi(optarg)) <= 0)
- return -1;
- break;
-
- case '?':
- default:
- return -1;
- }
-
- }
-
- return 0;
-}
-#endif