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