2 * Copyright (c) 2024 Rozhuk Ivan <rozhuk.im@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Author: Rozhuk Ivan <rozhuk.im@gmail.com>
31 * From the FreeBSD Bugzilla
32 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278566
37 #include <stdio.h> /* snprintf, fprintf */
38 #include <string.h> /* memset */
39 #include <sys/param.h>
40 #include <sys/sysctl.h>
41 #include <sys/types.h>
43 /* cc sysctlbyname_broken.c -O0 -DDEBUG -o sysctlbyname_broken */
44 /* valgrind --tool=memcheck --leak-check=yes --leak-resolution=high
45 * --track-origins=yes --undef-value-errors=yes --show-leak-kinds=all
46 * --track-fds=yes --trace-children=no --vgdb=no --show-reachable=yes --verbose
47 * --error-exitcode=1 ./sysctlbyname_broken */
49 /* Syntetic exapmle based on libdrm: xf86drm.c code. */
51 int main(int argc
, char** argv
)
53 char sysctl_name
[32], sysctl_val
[256];
56 snprintf(sysctl_name
, sizeof(sysctl_name
), "kern.compiler_version");
57 sysctl_len
= sizeof(sysctl_val
);
58 memset(sysctl_val
, 0x00, sizeof(sysctl_val
));
59 if (sysctlbyname(sysctl_name
, sysctl_val
, &sysctl_len
, NULL
, 0)) {
60 fprintf(stdout
, "sysctlbyname() - FAIL!\n");
61 } else if (argc
> 1) {
62 fprintf(stdout
, "%s\n", sysctl_val
);