0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
|
11 /* FreeBSD 3.0 at least */
|
222
|
12 char ngx_freebsd_kern_ostype[16];
|
|
13 char ngx_freebsd_kern_osrelease[128];
|
|
14 int ngx_freebsd_kern_osreldate;
|
|
15 int ngx_freebsd_hw_ncpu;
|
|
16 int ngx_freebsd_kern_ipc_somaxconn;
|
|
17 u_long ngx_freebsd_net_inet_tcp_sendspace;
|
0
|
18
|
|
19 /* FreeBSD 4.9 */
|
222
|
20 int ngx_freebsd_machdep_hlt_logical_cpus;
|
0
|
21
|
|
22 /* FreeBSD 5.0 */
|
222
|
23 int ngx_freebsd_kern_ipc_zero_copy_send;
|
0
|
24
|
|
25
|
|
26 ngx_uint_t ngx_freebsd_sendfile_nbytes_bug;
|
|
27 ngx_uint_t ngx_freebsd_use_tcp_nopush;
|
|
28
|
|
29
|
88
|
30 static ngx_os_io_t ngx_freebsd_io = {
|
0
|
31 ngx_unix_recv,
|
|
32 ngx_readv_chain,
|
354
|
33 ngx_udp_unix_recv,
|
0
|
34 ngx_unix_send,
|
18
|
35 #if (NGX_HAVE_SENDFILE)
|
0
|
36 ngx_freebsd_sendfile_chain,
|
|
37 NGX_IO_SENDFILE
|
|
38 #else
|
|
39 ngx_writev_chain,
|
|
40 0
|
|
41 #endif
|
|
42 };
|
|
43
|
|
44
|
|
45 typedef struct {
|
|
46 char *name;
|
222
|
47 void *value;
|
0
|
48 size_t size;
|
|
49 ngx_uint_t exists;
|
|
50 } sysctl_t;
|
|
51
|
|
52
|
|
53 sysctl_t sysctls[] = {
|
|
54 { "hw.ncpu",
|
|
55 &ngx_freebsd_hw_ncpu,
|
222
|
56 sizeof(ngx_freebsd_hw_ncpu), 0 },
|
0
|
57
|
|
58 { "machdep.hlt_logical_cpus",
|
|
59 &ngx_freebsd_machdep_hlt_logical_cpus,
|
222
|
60 sizeof(ngx_freebsd_machdep_hlt_logical_cpus), 0 },
|
0
|
61
|
|
62 { "net.inet.tcp.sendspace",
|
|
63 &ngx_freebsd_net_inet_tcp_sendspace,
|
222
|
64 sizeof(ngx_freebsd_net_inet_tcp_sendspace), 0 },
|
0
|
65
|
50
|
66 { "kern.ipc.somaxconn",
|
|
67 &ngx_freebsd_kern_ipc_somaxconn,
|
222
|
68 sizeof(ngx_freebsd_kern_ipc_somaxconn), 0 },
|
50
|
69
|
0
|
70 { "kern.ipc.zero_copy.send",
|
|
71 &ngx_freebsd_kern_ipc_zero_copy_send,
|
222
|
72 sizeof(ngx_freebsd_kern_ipc_zero_copy_send), 0 },
|
0
|
73
|
|
74 { NULL, NULL, 0, 0 }
|
|
75 };
|
|
76
|
|
77
|
88
|
78 void
|
|
79 ngx_debug_init()
|
0
|
80 {
|
96
|
81 #if (NGX_DEBUG_MALLOC)
|
0
|
82
|
|
83 #if __FreeBSD_version >= 500014
|
|
84 _malloc_options = "J";
|
|
85 #else
|
|
86 malloc_options = "J";
|
|
87 #endif
|
|
88
|
|
89 #endif
|
|
90 }
|
|
91
|
|
92
|
88
|
93 ngx_int_t
|
|
94 ngx_os_specific_init(ngx_log_t *log)
|
0
|
95 {
|
50
|
96 int version, somaxconn;
|
0
|
97 size_t size;
|
|
98 ngx_err_t err;
|
|
99 ngx_uint_t i;
|
|
100
|
|
101 size = sizeof(ngx_freebsd_kern_ostype);
|
|
102 if (sysctlbyname("kern.ostype",
|
|
103 ngx_freebsd_kern_ostype, &size, NULL, 0) == -1) {
|
|
104 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
105 "sysctlbyname(kern.ostype) failed");
|
10
|
106
|
|
107 if (ngx_errno != NGX_ENOMEM) {
|
|
108 return NGX_ERROR;
|
|
109 }
|
|
110
|
|
111 ngx_freebsd_kern_ostype[size - 1] = '\0';
|
0
|
112 }
|
|
113
|
|
114 size = sizeof(ngx_freebsd_kern_osrelease);
|
|
115 if (sysctlbyname("kern.osrelease",
|
|
116 ngx_freebsd_kern_osrelease, &size, NULL, 0) == -1) {
|
|
117 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
118 "sysctlbyname(kern.osrelease) failed");
|
10
|
119
|
|
120 if (ngx_errno != NGX_ENOMEM) {
|
|
121 return NGX_ERROR;
|
|
122 }
|
|
123
|
|
124 ngx_freebsd_kern_osrelease[size - 1] = '\0';
|
0
|
125 }
|
|
126
|
|
127
|
|
128 size = sizeof(int);
|
|
129 if (sysctlbyname("kern.osreldate",
|
|
130 &ngx_freebsd_kern_osreldate, &size, NULL, 0) == -1) {
|
|
131 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
132 "sysctlbyname(kern.osreldate) failed");
|
|
133 return NGX_ERROR;
|
|
134 }
|
|
135
|
|
136 version = ngx_freebsd_kern_osreldate;
|
|
137
|
|
138
|
18
|
139 #if (NGX_HAVE_SENDFILE)
|
0
|
140
|
|
141 /*
|
|
142 * The determination of the sendfile() "nbytes bug" is complex enough.
|
|
143 * There are two sendfile() syscalls: a new #393 has no bug while
|
|
144 * an old #336 has the bug in some versions and has not in others.
|
|
145 * Besides libc_r wrapper also emulates the bug in some versions.
|
|
146 * There is no way to say exactly if syscall #336 in FreeBSD circa 4.6
|
|
147 * has the bug. We use the algorithm that is correct at least for
|
|
148 * RELEASEs and for syscalls only (not libc_r wrapper).
|
|
149 *
|
|
150 * 4.6.1-RELEASE and below have the bug
|
|
151 * 4.6.2-RELEASE and above have the new syscall
|
|
152 *
|
|
153 * We detect the new sendfile() syscall available at the compile time
|
|
154 * to allow an old binary to run correctly on an updated FreeBSD system.
|
|
155 */
|
|
156
|
|
157 #if (__FreeBSD__ == 4 && __FreeBSD_version >= 460102) \
|
|
158 || __FreeBSD_version == 460002 || __FreeBSD_version >= 500039
|
|
159
|
|
160 /* a new syscall without the bug */
|
|
161
|
|
162 ngx_freebsd_sendfile_nbytes_bug = 0;
|
|
163
|
|
164 #else
|
|
165
|
|
166 /* an old syscall that may have the bug */
|
|
167
|
|
168 ngx_freebsd_sendfile_nbytes_bug = 1;
|
|
169
|
|
170 #endif
|
|
171
|
18
|
172 #endif /* NGX_HAVE_SENDFILE */
|
0
|
173
|
|
174
|
|
175 if ((version < 500000 && version >= 440003) || version >= 500017) {
|
|
176 ngx_freebsd_use_tcp_nopush = 1;
|
|
177 }
|
|
178
|
|
179
|
|
180 for (i = 0; sysctls[i].name; i++) {
|
|
181 size = sysctls[i].size;
|
|
182
|
|
183 if (sysctlbyname(sysctls[i].name, sysctls[i].value, &size, NULL, 0)
|
222
|
184 == 0)
|
0
|
185 {
|
|
186 sysctls[i].exists = 1;
|
|
187 continue;
|
|
188 }
|
|
189
|
|
190 err = ngx_errno;
|
|
191
|
|
192 if (err == NGX_ENOENT) {
|
|
193 continue;
|
|
194 }
|
|
195
|
|
196 ngx_log_error(NGX_LOG_ALERT, log, err,
|
|
197 "sysctlbyname(%s) failed", sysctls[i].name);
|
|
198 return NGX_ERROR;
|
|
199 }
|
|
200
|
|
201 if (ngx_freebsd_machdep_hlt_logical_cpus) {
|
|
202 ngx_ncpu = ngx_freebsd_hw_ncpu / 2;
|
222
|
203
|
0
|
204 } else {
|
|
205 ngx_ncpu = ngx_freebsd_hw_ncpu;
|
|
206 }
|
|
207
|
222
|
208 somaxconn = version < 600008 ? 32676 : 65535;
|
50
|
209
|
|
210 if (ngx_freebsd_kern_ipc_somaxconn > somaxconn) {
|
|
211 ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
212 "sysctl kern.ipc.somaxconn must be no more than %d",
|
|
213 somaxconn);
|
|
214 return NGX_ERROR;
|
|
215 }
|
2
|
216
|
22
|
217 ngx_tcp_nodelay_and_tcp_nopush = 1;
|
|
218
|
88
|
219 ngx_os_io = ngx_freebsd_io;
|
22
|
220
|
88
|
221 return NGX_OK;
|
0
|
222 }
|
|
223
|
|
224
|
88
|
225 void
|
|
226 ngx_os_specific_status(ngx_log_t *log)
|
0
|
227 {
|
222
|
228 u_long value;
|
0
|
229 ngx_uint_t i;
|
|
230
|
80
|
231 ngx_log_error(NGX_LOG_NOTICE, log, 0, "OS: %s %s",
|
0
|
232 ngx_freebsd_kern_ostype, ngx_freebsd_kern_osrelease);
|
|
233
|
|
234 #ifdef __DragonFly_version
|
80
|
235 ngx_log_error(NGX_LOG_NOTICE, log, 0,
|
0
|
236 "kern.osreldate: %d, built on %d",
|
|
237 ngx_freebsd_kern_osreldate, __DragonFly_version);
|
|
238 #else
|
80
|
239 ngx_log_error(NGX_LOG_NOTICE, log, 0,
|
0
|
240 "kern.osreldate: %d, built on %d",
|
|
241 ngx_freebsd_kern_osreldate, __FreeBSD_version);
|
|
242 #endif
|
|
243
|
|
244 for (i = 0; sysctls[i].name; i++) {
|
|
245 if (sysctls[i].exists) {
|
222
|
246 if (sysctls[i].size == sizeof(long)) {
|
|
247 value = *(long *) sysctls[i].value;
|
|
248
|
|
249 } else {
|
|
250 value = *(int *) sysctls[i].value;
|
|
251 }
|
|
252
|
|
253 ngx_log_error(NGX_LOG_NOTICE, log, 0, "%s: %l",
|
|
254 sysctls[i].name, value);
|
0
|
255 }
|
|
256 }
|
|
257 }
|