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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Hyra nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/sysctl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define BUF_SIZE 128
/* Kern var string constants */
#define NAME_OSTYPE "ostype"
#define NAME_OSRELEASE "osrelease"
#define NAME_VERSION "version"
#define NAME_VCACHE_TYPE "vcache_type"
/* Hw var string constants */
#define NAME_PAGESIZE "pagesize"
#define NAME_NCPU "ncpu"
#define NAME_MACHINE "machine"
/* Name start string constants */
#define NAME_KERN "kern"
#define NAME_HW "hw"
/* Name start int constants */
#define NAME_DEF_KERN 0
#define NAME_DEF_HW 1
/*
* Print the contents read from a sysctl
* variable depending on its type.
*
* @data: Data to print
* @is_str: True if a string
*/
static inline void
varbuf_print(char data[BUF_SIZE], bool is_str)
{
uint32_t *val;
if (is_str) {
printf("%s\n", data);
} else {
val = (uint32_t *)data;
printf("%d\n", *val);
}
}
/*
* Convert string name to a internal name
* definition.
*
* @name: Name to convert
*
* Convert to int def
* /
* kern.ostype
* ^^
*
* --
* Returns a less than zero value on failure
* (e.g., entry not found).
*/
static int
name_to_def(const char *name)
{
switch (*name) {
case 'k':
if (strcmp(name, NAME_KERN) == 0) {
return NAME_DEF_KERN;
}
return -1;
case 'h':
if (strcmp(name, NAME_HW) == 0) {
return NAME_DEF_HW;
}
return -1;
}
return -1;
}
/*
* Handle parsing of 'kern.*' node names
*
* @node: Node name to parse
* @is_str: Set to true if string
*/
static int
kern_node(const char *node, bool *is_str)
{
switch (*node) {
case 'v':
if (strcmp(node, NAME_VERSION) == 0) {
return KERN_VERSION;
}
if (strcmp(node, NAME_VCACHE_TYPE) == 0) {
return KERN_VCACHE_TYPE;
}
return -1;
case 'o':
if (strcmp(node, NAME_OSTYPE) == 0) {
return KERN_OSTYPE;
}
if (strcmp(node, NAME_OSRELEASE) == 0) {
return KERN_OSRELEASE;
}
return -1;
}
return -1;
}
/*
* Handle parsing of 'hw.*' node names
*
* @node: Node name to parse
* @is_str: Set to true if string
*/
static int
hw_node(const char *node, bool *is_str)
{
switch (*node) {
case 'p':
if (strcmp(node, NAME_PAGESIZE) == 0) {
*is_str = false;
return HW_PAGESIZE;
}
return -1;
case 'n':
if (strcmp(node, NAME_NCPU) == 0) {
*is_str = false;
return HW_NCPU;
}
return -1;
case 'm':
if (strcmp(node, NAME_MACHINE) == 0) {
return HW_MACHINE;
}
return -1;
}
return -1;
}
/*
* Convert string node to a sysctl name
* definition.
*
* @name: Name to convert
* @is_str: Set to true if string
*
* Convert to int def
* /
* kern.ostype
* ^^ name
*
* --
* Returns a less than zero value on failure
* (e.g., entry not found).
*/
static int
node_to_def(int name, const char *node, bool *is_str)
{
int retval;
bool dmmy;
/*
* If the caller did not set `is_str' just
* set it to a dummy value. Otherwise, we will
* make it *default* to a 'true' value.
*/
if (is_str == NULL) {
is_str = &dmmy;
} else {
*is_str = true;
}
switch (name) {
case NAME_DEF_KERN:
return kern_node(node, is_str);
case NAME_DEF_HW:
return hw_node(node, is_str);
}
return -1;
}
int
main(int argc, char **argv)
{
struct sysctl_args args;
char *var, *p;
int type, error;
int root, name;
size_t oldlen;
bool is_str;
char buf[BUF_SIZE];
if (argc < 2) {
printf("sysctl: usage: sysctl <var>\n");
return -1;
}
var = argv[1];
p = strtok(var, ".");
if (p == NULL) {
printf("sysctl: bad var\n");
return -1;
}
if ((root = name_to_def(p)) < 0) {
printf("sysctl: bad var \"%s\"", p);
return root;
}
p = strtok(NULL, ".");
if (p == NULL) {
printf("sysctl: bad var \"%s\"\n", p);
return -1;
}
if ((name = node_to_def(root, p, &is_str)) < 0) {
printf("sysctl: bad var \"%s\"\n", p);
return name;
}
memset(buf, 0, sizeof(buf));
oldlen = sizeof(buf);
args.name = &name;
args.nlen = 1;
args.oldp = buf;
args.oldlenp = &oldlen;
args.newp = NULL;
args.newlen = 0;
if ((error = sysctl(&args)) != 0) {
printf("sysctl returned %d\n", error);
return error;
}
varbuf_print(buf, is_str);
return 0;
}
|