Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 /* FreeBSD 3.0 at least */
0013 char    ngx_freebsd_kern_ostype[16];
0014 char    ngx_freebsd_kern_osrelease[128];
0015 int     ngx_freebsd_kern_osreldate;
0016 int     ngx_freebsd_hw_ncpu;
0017 int     ngx_freebsd_kern_ipc_somaxconn;
0018 u_long  ngx_freebsd_net_inet_tcp_sendspace;
0019 
0020 /* FreeBSD 4.9 */
0021 int     ngx_freebsd_machdep_hlt_logical_cpus;
0022 
0023 
0024 ngx_uint_t  ngx_freebsd_sendfile_nbytes_bug;
0025 ngx_uint_t  ngx_freebsd_use_tcp_nopush;
0026 
0027 ngx_uint_t  ngx_debug_malloc;
0028 
0029 
0030 static ngx_os_io_t ngx_freebsd_io = {
0031     ngx_unix_recv,
0032     ngx_readv_chain,
0033     ngx_udp_unix_recv,
0034     ngx_unix_send,
0035     ngx_udp_unix_send,
0036     ngx_udp_unix_sendmsg_chain,
0037 #if (NGX_HAVE_SENDFILE)
0038     ngx_freebsd_sendfile_chain,
0039     NGX_IO_SENDFILE
0040 #else
0041     ngx_writev_chain,
0042     0
0043 #endif
0044 };
0045 
0046 
0047 typedef struct {
0048     char        *name;
0049     void        *value;
0050     size_t       size;
0051     ngx_uint_t   exists;
0052 } sysctl_t;
0053 
0054 
0055 sysctl_t sysctls[] = {
0056     { "hw.ncpu",
0057       &ngx_freebsd_hw_ncpu,
0058       sizeof(ngx_freebsd_hw_ncpu), 0 },
0059 
0060     { "machdep.hlt_logical_cpus",
0061       &ngx_freebsd_machdep_hlt_logical_cpus,
0062       sizeof(ngx_freebsd_machdep_hlt_logical_cpus), 0 },
0063 
0064     { "net.inet.tcp.sendspace",
0065       &ngx_freebsd_net_inet_tcp_sendspace,
0066       sizeof(ngx_freebsd_net_inet_tcp_sendspace), 0 },
0067 
0068     { "kern.ipc.somaxconn",
0069       &ngx_freebsd_kern_ipc_somaxconn,
0070       sizeof(ngx_freebsd_kern_ipc_somaxconn), 0 },
0071 
0072     { NULL, NULL, 0, 0 }
0073 };
0074 
0075 
0076 void
0077 ngx_debug_init(void)
0078 {
0079 #if (NGX_DEBUG_MALLOC)
0080 
0081 #if __FreeBSD_version >= 500014 && __FreeBSD_version < 1000011
0082     _malloc_options = "J";
0083 #elif __FreeBSD_version < 500014
0084     malloc_options = "J";
0085 #endif
0086 
0087     ngx_debug_malloc = 1;
0088 
0089 #else
0090     char  *mo;
0091 
0092     mo = getenv("MALLOC_OPTIONS");
0093 
0094     if (mo && ngx_strchr(mo, 'J')) {
0095         ngx_debug_malloc = 1;
0096     }
0097 #endif
0098 }
0099 
0100 
0101 ngx_int_t
0102 ngx_os_specific_init(ngx_log_t *log)
0103 {
0104     int         version;
0105     size_t      size;
0106     ngx_err_t   err;
0107     ngx_uint_t  i;
0108 
0109     size = sizeof(ngx_freebsd_kern_ostype);
0110     if (sysctlbyname("kern.ostype",
0111                      ngx_freebsd_kern_ostype, &size, NULL, 0) == -1) {
0112         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0113                       "sysctlbyname(kern.ostype) failed");
0114 
0115         if (ngx_errno != NGX_ENOMEM) {
0116             return NGX_ERROR;
0117         }
0118 
0119         ngx_freebsd_kern_ostype[size - 1] = '\0';
0120     }
0121 
0122     size = sizeof(ngx_freebsd_kern_osrelease);
0123     if (sysctlbyname("kern.osrelease",
0124                      ngx_freebsd_kern_osrelease, &size, NULL, 0) == -1) {
0125         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0126                       "sysctlbyname(kern.osrelease) failed");
0127 
0128         if (ngx_errno != NGX_ENOMEM) {
0129             return NGX_ERROR;
0130         }
0131 
0132         ngx_freebsd_kern_osrelease[size - 1] = '\0';
0133     }
0134 
0135 
0136     size = sizeof(int);
0137     if (sysctlbyname("kern.osreldate",
0138                      &ngx_freebsd_kern_osreldate, &size, NULL, 0) == -1) {
0139         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0140                       "sysctlbyname(kern.osreldate) failed");
0141         return NGX_ERROR;
0142     }
0143 
0144     version = ngx_freebsd_kern_osreldate;
0145 
0146 
0147 #if (NGX_HAVE_SENDFILE)
0148 
0149     /*
0150      * The determination of the sendfile() "nbytes bug" is complex enough.
0151      * There are two sendfile() syscalls: a new #393 has no bug while
0152      * an old #336 has the bug in some versions and has not in others.
0153      * Besides libc_r wrapper also emulates the bug in some versions.
0154      * There is no way to say exactly if syscall #336 in FreeBSD circa 4.6
0155      * has the bug.  We use the algorithm that is correct at least for
0156      * RELEASEs and for syscalls only (not libc_r wrapper).
0157      *
0158      * 4.6.1-RELEASE and below have the bug
0159      * 4.6.2-RELEASE and above have the new syscall
0160      *
0161      * We detect the new sendfile() syscall available at the compile time
0162      * to allow an old binary to run correctly on an updated FreeBSD system.
0163      */
0164 
0165 #if (__FreeBSD__ == 4 && __FreeBSD_version >= 460102) \
0166     || __FreeBSD_version == 460002 || __FreeBSD_version >= 500039
0167 
0168     /* a new syscall without the bug */
0169 
0170     ngx_freebsd_sendfile_nbytes_bug = 0;
0171 
0172 #else
0173 
0174     /* an old syscall that may have the bug */
0175 
0176     ngx_freebsd_sendfile_nbytes_bug = 1;
0177 
0178 #endif
0179 
0180 #endif /* NGX_HAVE_SENDFILE */
0181 
0182 
0183     if ((version < 500000 && version >= 440003) || version >= 500017) {
0184         ngx_freebsd_use_tcp_nopush = 1;
0185     }
0186 
0187 
0188     for (i = 0; sysctls[i].name; i++) {
0189         size = sysctls[i].size;
0190 
0191         if (sysctlbyname(sysctls[i].name, sysctls[i].value, &size, NULL, 0)
0192             == 0)
0193         {
0194             sysctls[i].exists = 1;
0195             continue;
0196         }
0197 
0198         err = ngx_errno;
0199 
0200         if (err == NGX_ENOENT) {
0201             continue;
0202         }
0203 
0204         ngx_log_error(NGX_LOG_ALERT, log, err,
0205                       "sysctlbyname(%s) failed", sysctls[i].name);
0206         return NGX_ERROR;
0207     }
0208 
0209     if (ngx_freebsd_machdep_hlt_logical_cpus) {
0210         ngx_ncpu = ngx_freebsd_hw_ncpu / 2;
0211 
0212     } else {
0213         ngx_ncpu = ngx_freebsd_hw_ncpu;
0214     }
0215 
0216     if (version < 600008 && ngx_freebsd_kern_ipc_somaxconn > 32767) {
0217         ngx_log_error(NGX_LOG_ALERT, log, 0,
0218                       "sysctl kern.ipc.somaxconn must be less than 32768");
0219         return NGX_ERROR;
0220     }
0221 
0222     ngx_tcp_nodelay_and_tcp_nopush = 1;
0223 
0224     ngx_os_io = ngx_freebsd_io;
0225 
0226     return NGX_OK;
0227 }
0228 
0229 
0230 void
0231 ngx_os_specific_status(ngx_log_t *log)
0232 {
0233     u_long      value;
0234     ngx_uint_t  i;
0235 
0236     ngx_log_error(NGX_LOG_NOTICE, log, 0, "OS: %s %s",
0237                   ngx_freebsd_kern_ostype, ngx_freebsd_kern_osrelease);
0238 
0239 #ifdef __DragonFly_version
0240     ngx_log_error(NGX_LOG_NOTICE, log, 0,
0241                   "kern.osreldate: %d, built on %d",
0242                   ngx_freebsd_kern_osreldate, __DragonFly_version);
0243 #else
0244     ngx_log_error(NGX_LOG_NOTICE, log, 0,
0245                   "kern.osreldate: %d, built on %d",
0246                   ngx_freebsd_kern_osreldate, __FreeBSD_version);
0247 #endif
0248 
0249     for (i = 0; sysctls[i].name; i++) {
0250         if (sysctls[i].exists) {
0251             if (sysctls[i].size == sizeof(long)) {
0252                 value = *(long *) sysctls[i].value;
0253 
0254             } else {
0255                 value = *(int *) sysctls[i].value;
0256             }
0257 
0258             ngx_log_error(NGX_LOG_NOTICE, log, 0, "%s: %l",
0259                           sysctls[i].name, value);
0260         }
0261     }
0262 }