{"record":{"id":"725f3e116ed09c64","repo":"nginx/nginx","slug":"ngx-log-alert-725f3e","errorCode":"NGX_LOG_ALERT","errorMessage":"sysctlbyname(kern.ostype) failed","messagePattern":"sysctlbyname\\(kern\\.ostype\\) failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/os/unix/ngx_freebsd_init.c","lineNumber":112,"sourceCode":"    if (mo && ngx_strchr(mo, 'J')) {\n        ngx_debug_malloc = 1;\n    }\n#endif\n}\n\n\nngx_int_t\nngx_os_specific_init(ngx_log_t *log)\n{\n    int         version;\n    size_t      size;\n    ngx_err_t   err;\n    ngx_uint_t  i;\n\n    size = sizeof(ngx_freebsd_kern_ostype);\n    if (sysctlbyname(\"kern.ostype\",\n                     ngx_freebsd_kern_ostype, &size, NULL, 0) == -1) {\n        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,\n                      \"sysctlbyname(kern.ostype) failed\");\n\n        if (ngx_errno != NGX_ENOMEM) {\n            return NGX_ERROR;\n        }\n\n        ngx_freebsd_kern_ostype[size - 1] = '\\0';\n    }\n\n    size = sizeof(ngx_freebsd_kern_osrelease);\n    if (sysctlbyname(\"kern.osrelease\",\n                     ngx_freebsd_kern_osrelease, &size, NULL, 0) == -1) {\n        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,\n                      \"sysctlbyname(kern.osrelease) failed\");\n\n        if (ngx_errno != NGX_ENOMEM) {\n            return NGX_ERROR;\n        }","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/nginx/nginx/blob/3f6f7824d4e2eb1ac37dec76683d525ac0ff521c/src/os/unix/ngx_freebsd_init.c#L94-L130","documentation":"During ngx_os_specific_init() at nginx startup on FreeBSD, the kernel description string kern.ostype is read with sysctlbyname() into a fixed 32-byte buffer. Only a too-small-buffer result (ENOMEM) is tolerated by NUL-terminating the truncated value; any other errno aborts startup with NGX_ERROR.","triggerScenarios":"Running a FreeBSD nginx binary under an emulation layer (e.g. Linuxulator) that lacks kern.ostype; jails or hardened kernels restricting sysctlbyname(); EINVAL/EFAULT from a broken libc; a stripped custom kernel without the kern.ostype MIB.","commonSituations":"FreeBSD binary executed on a non-native kernel or in a restricted sandbox; exotic custom kernel builds; mismatched libc after a partial upgrade.","solutions":["Verify natively with `sysctl kern.ostype` on the same host/jail nginx runs in","Run the binary on a native FreeBSD kernel of the matching ABI instead of an emulation layer","Adjust jail/hardening policy so sysctlbyname reads of kern.* are permitted","Rebuild/reinstall the matching libc and world if sysctl is broken system-wide"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"char buf[32]; size_t len = sizeof(buf);\nif (sysctlbyname(\"kern.ostype\", buf, &len, NULL, 0) == -1\n    && errno != ENOMEM) {\n    /* nginx startup will abort here: fix the environment first */\n}","typeGuard":null,"tryCatchPattern":"Startup abort has no runtime catch: preflight the same sysctl in the deploy environment (shell or C probe above). If the probe fails under a jail/emulator but succeeds on the host, run nginx natively or open the sysctl in policy, then start nginx.","preventionTips":["Never run FreeBSD nginx binaries under OS emulation layers","Add a sysctl kern.ostype preflight to install/start scripts on FreeBSD","Keep jail security policies permissive for read-only kern.* sysctls"],"tags":["freebsd","sysctl","startup","os-detection"],"backgroundTag":"sysctl-query-failed","analyzedSha":"3f6f7824d4e2eb1ac37dec76683d525ac0ff521c","analyzedAt":"2026-08-22T03:09:46.447Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}