1sysctl(2) System Calls Manual sysctl(2)
2
3
4
6 sysctl - read/write system parameters
7
9 #include <unistd.h>
10 #include <linux/sysctl.h>
11
12 [[deprecated]] int _sysctl(struct __sysctl_args *args);
13
15 This system call no longer exists on current kernels! See NOTES.
16
17 The _sysctl() call reads and/or writes kernel parameters. For example,
18 the hostname, or the maximum number of open files. The argument has
19 the form
20
21 struct __sysctl_args {
22 int *name; /* integer vector describing variable */
23 int nlen; /* length of this vector */
24 void *oldval; /* 0 or address where to store old value */
25 size_t *oldlenp; /* available room for old value,
26 overwritten by actual size of old value */
27 void *newval; /* 0 or address of new value */
28 size_t newlen; /* size of new value */
29 };
30
31 This call does a search in a tree structure, possibly resembling a di‐
32 rectory tree under /proc/sys, and if the requested item is found calls
33 some appropriate routine to read or modify the value.
34
36 Upon successful completion, _sysctl() returns 0. Otherwise, a value of
37 -1 is returned and errno is set to indicate the error.
38
40 EACCES, EPERM
41 No search permission for one of the encountered "directories",
42 or no read permission where oldval was nonzero, or no write per‐
43 mission where newval was nonzero.
44
45 EFAULT The invocation asked for the previous value by setting oldval
46 non-NULL, but allowed zero room in oldlenp.
47
48 ENOTDIR
49 name was not found.
50
52 Linux.
53
55 Linux 1.3.57. Removed in Linux 5.5, glibc 2.32.
56
57 It originated in 4.4BSD. Only Linux has the /proc/sys mirror, and the
58 object naming schemes differ between Linux and 4.4BSD, but the declara‐
59 tion of the sysctl() function is the same in both.
60
62 Use of this system call was long discouraged: since Linux 2.6.24, uses
63 of this system call result in warnings in the kernel log, and in Linux
64 5.5, the system call was finally removed. Use the /proc/sys interface
65 instead.
66
67 Note that on older kernels where this system call still exists, it is
68 available only if the kernel was configured with the CON‐
69 FIG_SYSCTL_SYSCALL option. Furthermore, glibc does not provide a wrap‐
70 per for this system call, necessitating the use of syscall(2).
71
73 The object names vary between kernel versions, making this system call
74 worthless for applications.
75
76 Not all available objects are properly documented.
77
78 It is not yet possible to change operating system by writing to
79 /proc/sys/kernel/ostype.
80
82 #define _GNU_SOURCE
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <sys/syscall.h>
87 #include <unistd.h>
88
89 #include <linux/sysctl.h>
90
91 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
92
93 int _sysctl(struct __sysctl_args *args);
94
95 #define OSNAMESZ 100
96
97 int
98 main(void)
99 {
100 int name[] = { CTL_KERN, KERN_OSTYPE };
101 char osname[OSNAMESZ];
102 size_t osnamelth;
103 struct __sysctl_args args;
104
105 memset(&args, 0, sizeof(args));
106 args.name = name;
107 args.nlen = ARRAY_SIZE(name);
108 args.oldval = osname;
109 args.oldlenp = &osnamelth;
110
111 osnamelth = sizeof(osname);
112
113 if (syscall(SYS__sysctl, &args) == -1) {
114 perror("_sysctl");
115 exit(EXIT_FAILURE);
116 }
117 printf("This machine is running %*s\n", (int) osnamelth, osname);
118 exit(EXIT_SUCCESS);
119 }
120
122 proc(5)
123
124
125
126Linux man-pages 6.04 2023-03-30 sysctl(2)