aboutsummaryrefslogtreecommitdiff
path: root/src/os/unix/ngx_freebsd_init.c
blob: a24fc413240ad6d7a5bf95b065077a190c536c3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <ngx_freebsd_init.h>


int freebsd_kern_osreldate;
int freebsd_hw_ncpu;

int freebsd_sendfile_nbytes_bug;


int ngx_os_init(ngx_log_t *log)
{
    size_t  size;

    size = 4;
    if (sysctlbyname("kern.osreldate",
                     &freebsd_kern_osreldate, &size, NULL, 0) == -1) {
        ngx_log_error(NGX_LOG_ALERT, log, errno,
                      "sysctlbyname(kern.osreldate) failed");
        return NGX_ERROR;
    }

    ngx_log_error(NGX_LOG_INFO, log, 0,
                  "kern.osreldate: %d, built on %d",
                  freebsd_kern_osreldate, __FreeBSD_version);


#if HAVE_FREEBSD_SENDFILE

    /* The determination of the sendfile() nbytes bug is complex enough.
       There're two sendfile() syscalls: a new 393 has no bug while
       an old 336 has the bug in some versions and has not in others.
       libc_r wrapper also emulates the bug in some versions.
       There's no way to say exactly if a given FreeBSD version has bug.
       Here is the algorithm that work at least for RELEASEs
       and for syscalls only (not libc_r wrapper). */

    /* detect was the new sendfile() version available at the compile time
       to allow an old binary to run correctly on an updated FreeBSD system. */

#if (__FreeBSD__ == 4 && __FreeBSD_version >= 460102) \
    || __FreeBSD_version == 460002 || __FreeBSD_version >= 500039

    /* a new syscall without the bug */
    freebsd_sendfile_nbytes_bug = 0;

#else

    /* an old syscall that can have the bug */
    freebsd_sendfile_nbytes_bug = 1;

#endif

#endif /* HAVE_FREEBSD_SENDFILE */


    size = 4;
    if (sysctlbyname("hw.ncpu", &freebsd_hw_ncpu, &size, NULL, 0) == -1) {
        ngx_log_error(NGX_LOG_ALERT, log, errno,
                      "sysctlbyname(hw.ncpu) failed");
        return NGX_ERROR;
    }

    ngx_log_error(NGX_LOG_INFO, log, 0, "hw.ncpu: %d", freebsd_hw_ncpu);

    return NGX_OK;
}