blob: bf3a310aa3b63f26a9b69db57db9c7db66358cd1 [file] [log] [blame]
Igor Sysoevfa73aac2003-05-21 13:28:21 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd90282d2004-09-28 08:34:51 +00005 */
6
7
Igor Sysoevfa73aac2003-05-21 13:28:21 +00008#include <ngx_config.h>
9#include <ngx_core.h>
Igor Sysoevceb99292005-09-06 16:09:32 +000010#include <nginx.h>
Igor Sysoevfa73aac2003-05-21 13:28:21 +000011
12
Igor Sysoev42b12b32004-12-02 18:40:46 +000013ngx_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;
Igor Sysoevaad1b892004-10-03 20:02:06 +000017
18
19struct rlimit rlmt;
Igor Sysoevfa73aac2003-05-21 13:28:21 +000020
21
Igor Sysoevd09f7a12004-06-15 17:47:16 +000022ngx_os_io_t ngx_os_io = {
23 ngx_unix_recv,
24 ngx_readv_chain,
Igor Sysoeve67d4612007-12-03 16:46:46 +000025 ngx_udp_unix_recv,
Igor Sysoev97bf6c72008-12-25 20:07:12 +000026 ngx_unix_send,
Igor Sysoevd09f7a12004-06-15 17:47:16 +000027 ngx_writev_chain,
28 0
29};
30
31
Igor Sysoevceb99292005-09-06 16:09:32 +000032ngx_int_t
33ngx_os_init(ngx_log_t *log)
Igor Sysoevd09f7a12004-06-15 17:47:16 +000034{
Igor Sysoev67cd3362006-11-20 08:51:45 +000035 ngx_uint_t n;
36
Igor Sysoevceb99292005-09-06 16:09:32 +000037#if (NGX_HAVE_OS_SPECIFIC_INIT)
38 if (ngx_os_specific_init(log) != NGX_OK) {
39 return NGX_ERROR;
40 }
Igor Sysoevd09f7a12004-06-15 17:47:16 +000041#endif
42
Maxim Dounincb8a0322014-09-08 21:36:09 +040043 if (ngx_init_setproctitle(log) != NGX_OK) {
44 return NGX_ERROR;
45 }
Igor Sysoev899b44e2005-05-12 14:58:06 +000046
Igor Sysoev0ab91b92004-06-06 19:49:18 +000047 ngx_pagesize = getpagesize();
Igor Sysoev24025022005-12-16 15:07:08 +000048 ngx_cacheline_size = NGX_CPU_CACHE_LINE;
Igor Sysoev0ab91b92004-06-06 19:49:18 +000049
Igor Sysoev67cd3362006-11-20 08:51:45 +000050 for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }
51
Valentin Bartenev891b43d2012-02-10 11:24:19 +000052#if (NGX_HAVE_SC_NPROCESSORS_ONLN)
Igor Sysoev078d1b22004-06-30 15:30:41 +000053 if (ngx_ncpu == 0) {
Valentin Bartenev891b43d2012-02-10 11:24:19 +000054 ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN);
55 }
56#endif
57
58 if (ngx_ncpu < 1) {
Igor Sysoev078d1b22004-06-30 15:30:41 +000059 ngx_ncpu = 1;
60 }
61
Igor Sysoevffe71442006-02-08 15:33:12 +000062 ngx_cpuinfo();
63
Igor Sysoevfa73aac2003-05-21 13:28:21 +000064 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
65 ngx_log_error(NGX_LOG_ALERT, log, errno,
66 "getrlimit(RLIMIT_NOFILE) failed)");
67 return NGX_ERROR;
68 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000069
Igor Sysoevc2068d02005-10-19 12:33:58 +000070 ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;
Igor Sysoevfa73aac2003-05-21 13:28:21 +000071
Igor Sysoev40747ad2010-11-25 10:15:04 +000072#if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
Igor Sysoevfa73aac2003-05-21 13:28:21 +000073 ngx_inherited_nonblocking = 1;
74#else
75 ngx_inherited_nonblocking = 0;
76#endif
77
Igor Sysoevabeb1222006-10-23 13:10:10 +000078 srandom(ngx_time());
79
Igor Sysoevfa73aac2003-05-21 13:28:21 +000080 return NGX_OK;
81}
82
83
Igor Sysoevceb99292005-09-06 16:09:32 +000084void
85ngx_os_status(ngx_log_t *log)
Igor Sysoevaad1b892004-10-03 20:02:06 +000086{
Ruslan Ermilov1736c182014-05-20 16:10:07 +040087 ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD);
Igor Sysoeve5733802005-09-08 14:36:09 +000088
Igor Sysoev208eed22005-10-07 13:30:52 +000089#ifdef NGX_COMPILER
90 ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER);
91#endif
92
Igor Sysoevceb99292005-09-06 16:09:32 +000093#if (NGX_HAVE_OS_SPECIFIC_INIT)
94 ngx_os_specific_status(log);
95#endif
96
Igor Sysoev90c08142005-07-25 09:41:38 +000097 ngx_log_error(NGX_LOG_NOTICE, log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000098 "getrlimit(RLIMIT_NOFILE): %r:%r",
Igor Sysoevaad1b892004-10-03 20:02:06 +000099 rlmt.rlim_cur, rlmt.rlim_max);
100}
101
102
Ruslan Ermilov1efcca32012-07-24 15:09:54 +0000103#if 0
104
Igor Sysoevceb99292005-09-06 16:09:32 +0000105ngx_int_t
106ngx_posix_post_conf_init(ngx_log_t *log)
Igor Sysoevfa73aac2003-05-21 13:28:21 +0000107{
108 ngx_fd_t pp[2];
109
110 if (pipe(pp) == -1) {
111 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
112 return NGX_ERROR;
113 }
114
115 if (dup2(pp[1], STDERR_FILENO) == -1) {
116 ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed");
117 return NGX_ERROR;
118 }
119
120 if (pp[1] > STDERR_FILENO) {
121 if (close(pp[1]) == -1) {
122 ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed");
123 return NGX_ERROR;
124 }
125 }
126
127 return NGX_OK;
128}
Ruslan Ermilov1efcca32012-07-24 15:09:54 +0000129
130#endif