Deleted Added
full compact
sysctl.c (272145) sysctl.c (272154)
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
41 "$FreeBSD: head/sbin/sysctl/sysctl.c 272145 2014-09-25 22:47:19Z delphij $";
41 "$FreeBSD: head/sbin/sysctl/sysctl.c 272154 2014-09-26 05:05:34Z delphij $";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/resource.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/vmmeter.h>
50
51#ifdef __amd64__
52#include <sys/efi.h>
53#include <machine/metadata.h>
54#endif
55
56#if defined(__amd64__) || defined(__i386__)
57#include <machine/pc/bios.h>
58#endif
59
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/resource.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/vmmeter.h>
50
51#ifdef __amd64__
52#include <sys/efi.h>
53#include <machine/metadata.h>
54#endif
55
56#if defined(__amd64__) || defined(__i386__)
57#include <machine/pc/bios.h>
58#endif
59
60#include <assert.h>
60#include <ctype.h>
61#include <err.h>
62#include <errno.h>
63#include <inttypes.h>
64#include <locale.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>

--- 7 unchanged lines hidden (view full) ---

75
76static int oidfmt(int *, int, char *, u_int *);
77static int parsefile(const char *);
78static int parse(const char *, int);
79static int show_var(int *, int);
80static int sysctl_all(int *oid, int len);
81static int name2oid(const char *, int *);
82
61#include <ctype.h>
62#include <err.h>
63#include <errno.h>
64#include <inttypes.h>
65#include <locale.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>

--- 7 unchanged lines hidden (view full) ---

76
77static int oidfmt(int *, int, char *, u_int *);
78static int parsefile(const char *);
79static int parse(const char *, int);
80static int show_var(int *, int);
81static int sysctl_all(int *oid, int len);
82static int name2oid(const char *, int *);
83
83static int set_IK(const char *, int *);
84static int strIKtoi(const char *, char **);
84
85
86static int ctl_sign[CTLTYPE+1] = {
87 [CTLTYPE_INT] = 1,
88 [CTLTYPE_LONG] = 1,
89 [CTLTYPE_S64] = 1,
90};
91
92static int ctl_size[CTLTYPE+1] = {
93 [CTLTYPE_INT] = sizeof(int),
94 [CTLTYPE_UINT] = sizeof(u_int),
95 [CTLTYPE_LONG] = sizeof(long),
96 [CTLTYPE_ULONG] = sizeof(u_long),
97 [CTLTYPE_S64] = sizeof(int64_t),
98 [CTLTYPE_U64] = sizeof(uint64_t),
99};
100
101static const char *ctl_typename[CTLTYPE+1] = {
102 [CTLTYPE_INT] = "integer",
103 [CTLTYPE_UINT] = "unsigned integer",
104 [CTLTYPE_LONG] = "long integer",
105 [CTLTYPE_ULONG] = "unsigned long",
106 [CTLTYPE_S64] = "int64_t",
107 [CTLTYPE_U64] = "uint64_t",
108};
109
85static void
86usage(void)
87{
88
89 (void)fprintf(stderr, "%s\n%s\n",
90 "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
91 " sysctl [-bdehNnoqTWx] -a");
92 exit(1);

--- 93 unchanged lines hidden (view full) ---

186 * Parse a name into a MIB entry.
187 * Lookup and print out the MIB entry if it exists.
188 * Set a new value if requested.
189 */
190static int
191parse(const char *string, int lineno)
192{
193 int len, i, j;
110static void
111usage(void)
112{
113
114 (void)fprintf(stderr, "%s\n%s\n",
115 "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
116 " sysctl [-bdehNnoqTWx] -a");
117 exit(1);

--- 93 unchanged lines hidden (view full) ---

211 * Parse a name into a MIB entry.
212 * Lookup and print out the MIB entry if it exists.
213 * Set a new value if requested.
214 */
215static int
216parse(const char *string, int lineno)
217{
218 int len, i, j;
194 void *newval = 0;
219 const void *newval;
220 const char *newvalstr = NULL;
195 int intval;
196 unsigned int uintval;
197 long longval;
198 unsigned long ulongval;
199 size_t newsize = 0;
200 int64_t i64val;
201 uint64_t u64val;
202 int mib[CTL_MAXNAME];
221 int intval;
222 unsigned int uintval;
223 long longval;
224 unsigned long ulongval;
225 size_t newsize = 0;
226 int64_t i64val;
227 uint64_t u64val;
228 int mib[CTL_MAXNAME];
203 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ], line[BUFSIZ];
229 char *cp, *bufp, buf[BUFSIZ], *endptr = NULL, fmt[BUFSIZ], line[BUFSIZ];
204 u_int kind;
205
206 if (lineno)
207 snprintf(line, sizeof(line), " at line %d", lineno);
208 else
209 line[0] = '\0';
210
211 cp = buf;

--- 13 unchanged lines hidden (view full) ---

225 /* Strip a pair of " or ' if any. */
226 switch (*cp) {
227 case '\"':
228 case '\'':
229 if (cp[strlen(cp) - 1] == *cp)
230 cp[strlen(cp) - 1] = '\0';
231 cp++;
232 }
230 u_int kind;
231
232 if (lineno)
233 snprintf(line, sizeof(line), " at line %d", lineno);
234 else
235 line[0] = '\0';
236
237 cp = buf;

--- 13 unchanged lines hidden (view full) ---

251 /* Strip a pair of " or ' if any. */
252 switch (*cp) {
253 case '\"':
254 case '\'':
255 if (cp[strlen(cp) - 1] == *cp)
256 cp[strlen(cp) - 1] = '\0';
257 cp++;
258 }
233 newval = cp;
259 newvalstr = cp;
234 newsize = strlen(cp);
235 }
236 len = name2oid(bufp, mib);
237
238 if (len < 0) {
239 if (iflag)
240 return (0);
241 if (qflag)

--- 7 unchanged lines hidden (view full) ---

249 if (oidfmt(mib, len, fmt, &kind)) {
250 warn("couldn't find format of oid '%s'%s", bufp, line);
251 if (iflag)
252 return (1);
253 else
254 exit(1);
255 }
256
260 newsize = strlen(cp);
261 }
262 len = name2oid(bufp, mib);
263
264 if (len < 0) {
265 if (iflag)
266 return (0);
267 if (qflag)

--- 7 unchanged lines hidden (view full) ---

275 if (oidfmt(mib, len, fmt, &kind)) {
276 warn("couldn't find format of oid '%s'%s", bufp, line);
277 if (iflag)
278 return (1);
279 else
280 exit(1);
281 }
282
257 if (newval == NULL || dflag) {
283 if (newvalstr == NULL || dflag) {
258 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
259 if (dflag) {
260 i = show_var(mib, len);
261 if (!i && !bflag)
262 putchar('\n');
263 }
264 sysctl_all(mib, len);
265 } else {

--- 11 unchanged lines hidden (view full) ---

277 if (kind & CTLFLAG_TUN) {
278 warnx("oid '%s' is a read only tunable%s", bufp, line);
279 warnx("Tunable values are set in /boot/loader.conf");
280 } else
281 warnx("oid '%s' is read only%s", bufp, line);
282 return (1);
283 }
284
284 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
285 if (dflag) {
286 i = show_var(mib, len);
287 if (!i && !bflag)
288 putchar('\n');
289 }
290 sysctl_all(mib, len);
291 } else {

--- 11 unchanged lines hidden (view full) ---

303 if (kind & CTLFLAG_TUN) {
304 warnx("oid '%s' is a read only tunable%s", bufp, line);
305 warnx("Tunable values are set in /boot/loader.conf");
306 } else
307 warnx("oid '%s' is read only%s", bufp, line);
308 return (1);
309 }
310
285 if ((kind & CTLTYPE) == CTLTYPE_INT ||
286 (kind & CTLTYPE) == CTLTYPE_UINT ||
287 (kind & CTLTYPE) == CTLTYPE_LONG ||
288 (kind & CTLTYPE) == CTLTYPE_ULONG ||
289 (kind & CTLTYPE) == CTLTYPE_S64 ||
290 (kind & CTLTYPE) == CTLTYPE_U64) {
291 if (strlen(newval) == 0) {
311 switch (kind & CTLTYPE) {
312 case CTLTYPE_INT:
313 case CTLTYPE_UINT:
314 case CTLTYPE_LONG:
315 case CTLTYPE_ULONG:
316 case CTLTYPE_S64:
317 case CTLTYPE_U64:
318 if (strlen(newvalstr) == 0) {
292 warnx("empty numeric value");
293 return (1);
294 }
319 warnx("empty numeric value");
320 return (1);
321 }
322 /* FALLTHROUGH */
323 case CTLTYPE_STRING:
324 break;
325 default:
326 warnx("oid '%s' is type %d,"
327 " cannot set that%s", bufp,
328 kind & CTLTYPE, line);
329 return (1);
295 }
296
297 errno = 0;
298
299 switch (kind & CTLTYPE) {
300 case CTLTYPE_INT:
330 }
331
332 errno = 0;
333
334 switch (kind & CTLTYPE) {
335 case CTLTYPE_INT:
301 if (strcmp(fmt, "IK") == 0) {
302 if (!set_IK(newval, &intval)) {
303 warnx("invalid value '%s'%s",
304 (char *)newval, line);
305 return (1);
306 }
307 } else {
308 intval = (int)strtol(newval, &endptr,
336 if (strcmp(fmt, "IK") == 0)
337 intval = strIKtoi(newvalstr, &endptr);
338 else
339 intval = (int)strtol(newvalstr, &endptr,
309 0);
340 0);
310 if (errno != 0 || endptr == newval ||
311 *endptr != '\0') {
312 warnx("invalid integer '%s'%s",
313 (char *)newval, line);
314 return (1);
315 }
316 }
317 newval = &intval;
318 newsize = sizeof(intval);
319 break;
320 case CTLTYPE_UINT:
341 newval = &intval;
342 newsize = sizeof(intval);
343 break;
344 case CTLTYPE_UINT:
321 uintval = (int) strtoul(newval, &endptr, 0);
322 if (errno != 0 || endptr == newval ||
323 *endptr != '\0') {
324 warnx("invalid unsigned integer '%s'%s",
325 (char *)newval, line);
326 return (1);
327 }
345 uintval = (int) strtoul(newvalstr, &endptr, 0);
328 newval = &uintval;
329 newsize = sizeof(uintval);
330 break;
331 case CTLTYPE_LONG:
346 newval = &uintval;
347 newsize = sizeof(uintval);
348 break;
349 case CTLTYPE_LONG:
332 longval = strtol(newval, &endptr, 0);
333 if (errno != 0 || endptr == newval ||
334 *endptr != '\0') {
335 warnx("invalid long integer '%s'%s",
336 (char *)newval, line);
337 return (1);
338 }
350 longval = strtol(newvalstr, &endptr, 0);
339 newval = &longval;
340 newsize = sizeof(longval);
341 break;
342 case CTLTYPE_ULONG:
351 newval = &longval;
352 newsize = sizeof(longval);
353 break;
354 case CTLTYPE_ULONG:
343 ulongval = strtoul(newval, &endptr, 0);
344 if (errno != 0 || endptr == newval ||
345 *endptr != '\0') {
346 warnx("invalid unsigned long integer"
347 " '%s'%s", (char *)newval, line);
348 return (1);
349 }
355 ulongval = strtoul(newvalstr, &endptr, 0);
350 newval = &ulongval;
351 newsize = sizeof(ulongval);
352 break;
353 case CTLTYPE_STRING:
356 newval = &ulongval;
357 newsize = sizeof(ulongval);
358 break;
359 case CTLTYPE_STRING:
360 newval = newvalstr;
354 break;
355 case CTLTYPE_S64:
361 break;
362 case CTLTYPE_S64:
356 i64val = strtoimax(newval, &endptr, 0);
357 if (errno != 0 || endptr == newval ||
358 *endptr != '\0') {
359 warnx("invalid int64_t '%s'%s",
360 (char *)newval, line);
361 return (1);
362 }
363 i64val = strtoimax(newvalstr, &endptr, 0);
363 newval = &i64val;
364 newsize = sizeof(i64val);
365 break;
366 case CTLTYPE_U64:
364 newval = &i64val;
365 newsize = sizeof(i64val);
366 break;
367 case CTLTYPE_U64:
367 u64val = strtoumax(newval, &endptr, 0);
368 if (errno != 0 || endptr == newval ||
369 *endptr != '\0') {
370 warnx("invalid uint64_t '%s'%s",
371 (char *)newval, line);
372 return (1);
373 }
368 u64val = strtoumax(newvalstr, &endptr, 0);
374 newval = &u64val;
375 newsize = sizeof(u64val);
376 break;
369 newval = &u64val;
370 newsize = sizeof(u64val);
371 break;
377 case CTLTYPE_OPAQUE:
378 /* FALLTHROUGH */
379 default:
372 default:
380 warnx("oid '%s' is type %d,"
381 " cannot set that%s", bufp,
382 kind & CTLTYPE, line);
383 return (1);
373 /* NOTREACHED */
374 abort();
384 }
385
375 }
376
377 if (errno != 0 || endptr == newvalstr ||
378 (endptr != NULL && *endptr != '\0')) {
379 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE],
380 newvalstr, line);
381 return (1);
382 }
383
386 i = show_var(mib, len);
387 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
388 if (!i && !bflag)
389 putchar('\n');
390 switch (errno) {
391 case EOPNOTSUPP:
392 warnx("%s: value is not available%s",
393 string, line);

--- 266 unchanged lines hidden (view full) ---

660 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
661 smap->type, smap->xattr, (uintmax_t)smap->base,
662 (uintmax_t)smap->length);
663 return (0);
664}
665#endif
666
667static int
384 i = show_var(mib, len);
385 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
386 if (!i && !bflag)
387 putchar('\n');
388 switch (errno) {
389 case EOPNOTSUPP:
390 warnx("%s: value is not available%s",
391 string, line);

--- 266 unchanged lines hidden (view full) ---

658 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
659 smap->type, smap->xattr, (uintmax_t)smap->base,
660 (uintmax_t)smap->length);
661 return (0);
662}
663#endif
664
665static int
668set_IK(const char *str, int *val)
666strIKtoi(const char *str, char **endptrp)
669{
667{
668 int kelv;
670 float temp;
669 float temp;
671 int len, kelv;
670 size_t len;
672 const char *p;
671 const char *p;
673 char *endptr;
674
672
675 if ((len = strlen(str)) == 0)
676 return (0);
673 assert(errno == 0);
674
675 len = strlen(str);
676 /* caller already checked this */
677 assert(len > 0);
678
677 p = &str[len - 1];
679 p = &str[len - 1];
678 errno = 0;
679 if (*p == 'C' || *p == 'F') {
680 if (*p == 'C' || *p == 'F') {
680 temp = strtof(str, &endptr);
681 if (errno != 0 || endptr == str ||
682 endptr != p)
683 return (0);
684 if (*p == 'F')
685 temp = (temp - 32) * 5 / 9;
686 kelv = temp * 10 + 2732;
681 temp = strtof(str, endptrp);
682 if (*endptrp != str && *endptrp == p && errno != 0) {
683 if (*p == 'F')
684 temp = (temp - 32) * 5 / 9;
685 return (temp * 10 + 2732);
686 }
687 } else {
687 } else {
688 kelv = (int)strtol(str, &endptr, 10);
689 if (errno != 0 || endptr == str ||
690 *endptr != '\0')
691 return (0);
688 kelv = (int)strtol(str, endptrp, 10);
689 if (*endptrp != str && *endptrp == p && errno != 0)
690 return (kelv);
692 }
691 }
693 *val = kelv;
694 return (1);
692
693 errno = ERANGE;
694 return (0);
695}
696
697/*
698 * These functions uses a presently undocumented interface to the kernel
699 * to walk the tree and get the type so it can print the value.
700 * This interface is under work and consideration, and should probably
701 * be killed with a big axe by the first person who can find the time.
702 * (be aware though, that the proper interface isn't as obvious as it

--- 38 unchanged lines hidden (view full) ---

741 if (kind)
742 *kind = *(u_int *)buf;
743
744 if (fmt)
745 strcpy(fmt, (char *)(buf + sizeof(u_int)));
746 return (0);
747}
748
695}
696
697/*
698 * These functions uses a presently undocumented interface to the kernel
699 * to walk the tree and get the type so it can print the value.
700 * This interface is under work and consideration, and should probably
701 * be killed with a big axe by the first person who can find the time.
702 * (be aware though, that the proper interface isn't as obvious as it

--- 38 unchanged lines hidden (view full) ---

741 if (kind)
742 *kind = *(u_int *)buf;
743
744 if (fmt)
745 strcpy(fmt, (char *)(buf + sizeof(u_int)));
746 return (0);
747}
748
749static int ctl_sign[CTLTYPE+1] = {
750 [CTLTYPE_INT] = 1,
751 [CTLTYPE_LONG] = 1,
752 [CTLTYPE_S64] = 1,
753};
754
755static int ctl_size[CTLTYPE+1] = {
756 [CTLTYPE_INT] = sizeof(int),
757 [CTLTYPE_UINT] = sizeof(u_int),
758 [CTLTYPE_LONG] = sizeof(long),
759 [CTLTYPE_ULONG] = sizeof(u_long),
760 [CTLTYPE_S64] = sizeof(int64_t),
761 [CTLTYPE_U64] = sizeof(int64_t),
762};
763
764/*
765 * This formats and outputs the value of one variable
766 *
767 * Returns zero if anything was actually output.
768 * Returns one if didn't know what to do with this.
769 * Return minus one if we had errors.
770 */
771static int

--- 231 unchanged lines hidden ---
749/*
750 * This formats and outputs the value of one variable
751 *
752 * Returns zero if anything was actually output.
753 * Returns one if didn't know what to do with this.
754 * Return minus one if we had errors.
755 */
756static int

--- 231 unchanged lines hidden ---