nginx/src/core/nginx.c

96 lines
2.4 KiB
C
Raw Normal View History

#include <nginx.h>
#include <ngx_config.h>
#include <ngx_string.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
2002-08-16 23:27:03 +08:00
#include <ngx_array.h>
#include <ngx_socket.h>
#include <ngx_server.h>
#include <ngx_connection.h>
#include <ngx_listen.h>
2002-08-16 23:27:03 +08:00
/* STUB */
#include <ngx_http.h>
2002-08-16 23:27:03 +08:00
/* */
int ngx_max_conn = 512;
ngx_pool_t ngx_pool;
ngx_log_t ngx_log;
ngx_server_t ngx_server;
2002-08-16 23:27:03 +08:00
ngx_array_t ngx_listening_sockets;
int main(int argc, char *const *argv)
{
2002-08-16 23:27:03 +08:00
int i;
ngx_socket_t s;
ngx_listen_t *ls;
2002-08-16 23:27:03 +08:00
int reuseaddr = 1;
ngx_log.log_level = NGX_LOG_DEBUG;
ngx_pool.log = &ngx_log;
2002-08-16 23:27:03 +08:00
ngx_init_sockets(&ngx_log);
2002-08-16 23:27:03 +08:00
/* TODO: read config */
2002-08-16 23:27:03 +08:00
/* STUB */
/* TODO: init chain of global modules (like ngx_http.c),
they would init its modules and ngx_listening_sockets */
ngx_http_init();
2002-08-16 01:20:26 +08:00
/* for each listening socket */
2002-08-16 23:27:03 +08:00
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;
}
2002-08-16 23:27:03 +08:00
/* TODO: daemon */
2002-08-16 23:27:03 +08:00
/* TODO: fork */
2002-08-16 23:27:03 +08:00
/* TODO: events: init ngx_connections and listen slots */
2002-08-16 23:27:03 +08:00
/* TODO: threads */
2002-08-16 23:27:03 +08:00
/* STUB */
ngx_worker(&ls, 1, &ngx_pool, &ngx_log);
}