upstream nginx-0.7.31
[nginx.git] / nginx / src / os / unix / ngx_posix_init.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <nginx.h>
10
11
12 ngx_int_t   ngx_ncpu;
13 ngx_int_t   ngx_max_sockets;
14 ngx_uint_t  ngx_inherited_nonblocking;
15 ngx_uint_t  ngx_tcp_nodelay_and_tcp_nopush;
16
17
18 struct rlimit  rlmt;
19
20
21 ngx_os_io_t ngx_os_io = {
22     ngx_unix_recv,
23     ngx_readv_chain,
24     ngx_udp_unix_recv,
25     ngx_unix_send,
26     ngx_writev_chain,
27     0
28 };
29
30
31 ngx_int_t
32 ngx_os_init(ngx_log_t *log)
33 {
34     ngx_uint_t  n;
35
36 #if (NGX_HAVE_OS_SPECIFIC_INIT)
37     if (ngx_os_specific_init(log) != NGX_OK) {
38         return NGX_ERROR;
39     }
40 #endif
41
42     ngx_init_setproctitle(log);
43
44     ngx_pagesize = getpagesize();
45     ngx_cacheline_size = NGX_CPU_CACHE_LINE;
46
47     n = ngx_pagesize;
48
49     for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }
50
51     if (ngx_ncpu == 0) {
52         ngx_ncpu = 1;
53     }
54
55     ngx_cpuinfo();
56
57     if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
58         ngx_log_error(NGX_LOG_ALERT, log, errno,
59                       "getrlimit(RLIMIT_NOFILE) failed)");
60         return NGX_ERROR;
61     }
62
63     ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;
64
65 #if (NGX_HAVE_INHERITED_NONBLOCK)
66     ngx_inherited_nonblocking = 1;
67 #else
68     ngx_inherited_nonblocking = 0;
69 #endif
70
71     srandom(ngx_time());
72
73     return NGX_OK;
74 }
75
76
77 void
78 ngx_os_status(ngx_log_t *log)
79 {
80     ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER);
81
82 #ifdef NGX_COMPILER
83     ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER);
84 #endif
85
86 #if (NGX_HAVE_OS_SPECIFIC_INIT)
87     ngx_os_specific_status(log);
88 #endif
89
90     ngx_log_error(NGX_LOG_NOTICE, log, 0,
91                   "getrlimit(RLIMIT_NOFILE): %r:%r",
92                   rlmt.rlim_cur, rlmt.rlim_max);
93 }
94
95
96 ngx_int_t
97 ngx_posix_post_conf_init(ngx_log_t *log)
98 {
99     ngx_fd_t  pp[2];
100
101     if (pipe(pp) == -1) {
102         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
103         return NGX_ERROR;
104     }
105
106     if (dup2(pp[1], STDERR_FILENO) == -1) {
107         ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed");
108         return NGX_ERROR;
109     }
110
111     if (pp[1] > STDERR_FILENO) {
112         if (close(pp[1]) == -1) {
113             ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed");
114             return NGX_ERROR;
115         }
116     }
117
118     return NGX_OK;
119 }