blob: 9a4de022d6d949a57e508693af95093d34225bd1 [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
Igor Sysoev899b44e2005-05-12 14:58:06 +000043 ngx_init_setproctitle(log);
44
Igor Sysoev0ab91b92004-06-06 19:49:18 +000045 ngx_pagesize = getpagesize();
Igor Sysoev24025022005-12-16 15:07:08 +000046 ngx_cacheline_size = NGX_CPU_CACHE_LINE;
Igor Sysoev0ab91b92004-06-06 19:49:18 +000047
Igor Sysoev67cd3362006-11-20 08:51:45 +000048 for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }
49
Valentin Bartenev891b43d2012-02-10 11:24:19 +000050#if (NGX_HAVE_SC_NPROCESSORS_ONLN)
Igor Sysoev078d1b22004-06-30 15:30:41 +000051 if (ngx_ncpu == 0) {
Valentin Bartenev891b43d2012-02-10 11:24:19 +000052 ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN);
53 }
54#endif
55
56 if (ngx_ncpu < 1) {
Igor Sysoev078d1b22004-06-30 15:30:41 +000057 ngx_ncpu = 1;
58 }
59
Igor Sysoevffe71442006-02-08 15:33:12 +000060 ngx_cpuinfo();
61
Igor Sysoevfa73aac2003-05-21 13:28:21 +000062 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
63 ngx_log_error(NGX_LOG_ALERT, log, errno,
64 "getrlimit(RLIMIT_NOFILE) failed)");
65 return NGX_ERROR;
66 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000067
Igor Sysoevc2068d02005-10-19 12:33:58 +000068 ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;
Igor Sysoevfa73aac2003-05-21 13:28:21 +000069
Igor Sysoev40747ad2010-11-25 10:15:04 +000070#if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
Igor Sysoevfa73aac2003-05-21 13:28:21 +000071 ngx_inherited_nonblocking = 1;
72#else
73 ngx_inherited_nonblocking = 0;
74#endif
75
Igor Sysoevabeb1222006-10-23 13:10:10 +000076 srandom(ngx_time());
77
Igor Sysoevfa73aac2003-05-21 13:28:21 +000078 return NGX_OK;
79}
80
81
Igor Sysoevceb99292005-09-06 16:09:32 +000082void
83ngx_os_status(ngx_log_t *log)
Igor Sysoevaad1b892004-10-03 20:02:06 +000084{
Ruslan Ermilov1736c182014-05-20 16:10:07 +040085 ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD);
Igor Sysoeve5733802005-09-08 14:36:09 +000086
Igor Sysoev208eed22005-10-07 13:30:52 +000087#ifdef NGX_COMPILER
88 ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER);
89#endif
90
Igor Sysoevceb99292005-09-06 16:09:32 +000091#if (NGX_HAVE_OS_SPECIFIC_INIT)
92 ngx_os_specific_status(log);
93#endif
94
Igor Sysoev90c08142005-07-25 09:41:38 +000095 ngx_log_error(NGX_LOG_NOTICE, log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000096 "getrlimit(RLIMIT_NOFILE): %r:%r",
Igor Sysoevaad1b892004-10-03 20:02:06 +000097 rlmt.rlim_cur, rlmt.rlim_max);
98}
99
100
Ruslan Ermilov1efcca32012-07-24 15:09:54 +0000101#if 0
102
Igor Sysoevceb99292005-09-06 16:09:32 +0000103ngx_int_t
104ngx_posix_post_conf_init(ngx_log_t *log)
Igor Sysoevfa73aac2003-05-21 13:28:21 +0000105{
106 ngx_fd_t pp[2];
107
108 if (pipe(pp) == -1) {
109 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
110 return NGX_ERROR;
111 }
112
113 if (dup2(pp[1], STDERR_FILENO) == -1) {
114 ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed");
115 return NGX_ERROR;
116 }
117
118 if (pp[1] > STDERR_FILENO) {
119 if (close(pp[1]) == -1) {
120 ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed");
121 return NGX_ERROR;
122 }
123 }
124
125 return NGX_OK;
126}
Ruslan Ermilov1efcca32012-07-24 15:09:54 +0000127
128#endif