3 * The Regents of the University of California. All rights reserved.
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.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
30 * @(#)from: sysctl.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/sbin/sysctl/sysctl.c,v 1.25.2.11 2003/05/01 22:48:08 trhodes Exp $
34 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <sys/resource.h>
38 #include <sys/sensors.h>
39 #include <sys/param.h>
41 #include <machine/inttypes.h>
51 static int aflag
, bflag
, dflag
, eflag
, Nflag
, nflag
, oflag
, xflag
;
52 static int iflag
, qflag
;
54 static int oidfmt(int *, size_t, char *, u_int
*);
55 static int parse(const char *);
56 static int show_var(int *, size_t);
57 static int sysctl_all(int *, size_t);
58 static void set_T_dev_t(const char *, void **, size_t *);
59 static int set_IK(const char *, int *);
65 fprintf(stderr
, "%s\n%s\n",
66 "usage: sysctl [-bdeNnox] variable[=value] ...",
67 " sysctl [-bdeNnox] -a");
72 main(int argc
, char **argv
)
80 while ((ch
= getopt(argc
, argv
, "AabdeiNnoqwxX")) != -1) {
133 if (aflag
&& argc
== 0)
134 exit(sysctl_all(0, 0));
139 warncount
+= parse(*argv
++);
145 * Parse a name into a MIB entry.
146 * Lookup and print out the MIB entry if it exists.
147 * Set a new value if requested.
150 parse(const char *string
)
156 unsigned int uintval
;
158 unsigned long ulongval
;
162 int mib
[CTL_MAXNAME
];
163 char *cp
, fmt
[BUFSIZ
];
165 char *name_allocated
= NULL
;
168 if ((cp
= strchr(string
, '=')) != NULL
) {
169 if ((name_allocated
= malloc(cp
- string
+ 1)) == NULL
)
170 err(1, "malloc failed");
171 strlcpy(name_allocated
, string
, cp
- string
+ 1);
172 name
= name_allocated
;
174 while (isspace(*++cp
))
178 newsize
= strlen(cp
);
184 if (sysctlnametomib(name
, mib
, &len
) < 0) {
189 if (errno
== ENOENT
) {
190 errx(1, "unknown oid '%s'", name
);
192 err(1, "sysctlnametomib(\"%s\")", name
);
196 if (oidfmt(mib
, len
, fmt
, &kind
)) {
197 warn("couldn't find format of oid '%s'", name
);
203 if (newval
== NULL
) {
204 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
205 sysctl_all(mib
, len
);
207 i
= show_var(mib
, len
);
212 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
)
213 errx(1, "oid '%s' isn't a leaf node", name
);
215 if (!(kind
&CTLFLAG_WR
))
216 errx(1, "oid '%s' is read only", name
);
218 switch (kind
& CTLTYPE
) {
220 if (!(strcmp(fmt
, "IK") == 0)) {
221 if (!set_IK(newval
, &intval
))
222 errx(1, "invalid value '%s'",
225 intval
= (int) strtol(newval
, NULL
, 0);
227 newsize
= sizeof(intval
);
230 uintval
= (int) strtoul(newval
, NULL
, 0);
232 newsize
= sizeof uintval
;
235 longval
= strtol(newval
, NULL
, 0);
237 newsize
= sizeof longval
;
240 ulongval
= strtoul(newval
, NULL
, 0);
242 newsize
= sizeof ulongval
;
247 quadval
= strtoq(newval
, NULL
, 0);
249 newsize
= sizeof(quadval
);
252 uquadval
= strtouq(newval
, NULL
, 0);
254 newsize
= sizeof(uquadval
);
257 if (strcmp(fmt
, "T,dev_t") == 0 ||
258 strcmp(fmt
, "T,udev_t") == 0
260 set_T_dev_t((char*)newval
, &newval
,
266 errx(1, "oid '%s' is type %d,"
267 " cannot set that", name
,
271 i
= show_var(mib
, len
);
272 if (sysctl(mib
, len
, 0, 0, newval
, newsize
) == -1) {
277 errx(1, "%s: value is not available",
280 errx(1, "%s: specification is incomplete",
283 errx(1, "%s: type is unknown to this program",
294 j
= show_var(mib
, len
);
300 if (name_allocated
!= NULL
)
301 free(name_allocated
);
306 /* These functions will dump out various interesting structures. */
309 S_clockinfo(size_t l2
, void *p
)
311 struct clockinfo
*ci
= (struct clockinfo
*)p
;
312 if (l2
!= sizeof(*ci
))
313 err(1, "S_clockinfo %zu != %zu", l2
, sizeof(*ci
));
314 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
315 ci
->hz
, ci
->tick
, ci
->tickadj
, ci
->profhz
, ci
->stathz
);
320 S_loadavg(size_t l2
, void *p
)
322 struct loadavg
*tv
= (struct loadavg
*)p
;
324 if (l2
!= sizeof(*tv
))
325 err(1, "S_loadavg %zu != %zu", l2
, sizeof(*tv
));
327 printf("{ %.2f %.2f %.2f }",
328 (double)tv
->ldavg
[0]/(double)tv
->fscale
,
329 (double)tv
->ldavg
[1]/(double)tv
->fscale
,
330 (double)tv
->ldavg
[2]/(double)tv
->fscale
);
335 S_timespec(size_t l2
, void *p
)
337 struct timespec
*ts
= (struct timespec
*)p
;
341 if (l2
!= sizeof(*ts
))
342 err(1, "S_timespec %zu != %zu", l2
, sizeof(*ts
));
343 printf("{ sec = %ld, nsec = %ld } ",
344 ts
->tv_sec
, ts
->tv_nsec
);
346 p1
= strdup(ctime(&tv_sec
));
347 for (p2
=p1
; *p2
; p2
++)
355 S_timeval(size_t l2
, void *p
)
357 struct timeval
*tv
= (struct timeval
*)p
;
361 if (l2
!= sizeof(*tv
))
362 err(1, "S_timeval %zu != %zu", l2
, sizeof(*tv
));
363 printf("{ sec = %ld, usec = %ld } ",
364 tv
->tv_sec
, tv
->tv_usec
);
366 p1
= strdup(ctime(&tv_sec
));
367 for (p2
=p1
; *p2
; p2
++)
375 S_sensor(size_t l2
, void *p
)
377 struct sensor
*s
= (struct sensor
*)p
;
379 if (l2
!= sizeof(*s
)) {
380 warnx("S_sensor %zu != %zu", l2
, sizeof(*s
));
384 if (s
->flags
& SENSOR_FINVALID
) {
386 * XXX: with this flag, the node should be entirely ignored,
387 * but as the magic-based sysctl(8) is not too flexible, we
388 * simply have to print out that the sensor is invalid.
394 if (s
->flags
& SENSOR_FUNKNOWN
)
400 (s
->value
- 273150000) / 1000000.0);
403 printf("%jd RPM", (intmax_t)s
->value
);
405 case SENSOR_VOLTS_DC
:
406 printf("%.2f VDC", s
->value
/ 1000000.0);
409 printf("%.2f W", s
->value
/ 1000000.0);
412 printf("%.2f A", s
->value
/ 1000000.0);
414 case SENSOR_WATTHOUR
:
415 printf("%.2f Wh", s
->value
/ 1000000.0);
418 printf("%.2f Ah", s
->value
/ 1000000.0);
420 case SENSOR_INDICATOR
:
421 printf("%s", s
->value
? "On" : "Off");
424 printf("%jd Hz", (intmax_t)s
->value
);
428 printf("%jd", (intmax_t)s
->value
);
431 printf("%.2f%%", s
->value
/ 1000.0);
434 printf("%.2f lx", s
->value
/ 1000000.0);
441 case SENSOR_DRIVE_EMPTY
:
444 case SENSOR_DRIVE_READY
:
447 case SENSOR_DRIVE_POWERUP
:
448 name
= "powering up";
450 case SENSOR_DRIVE_ONLINE
:
453 case SENSOR_DRIVE_IDLE
:
456 case SENSOR_DRIVE_ACTIVE
:
459 case SENSOR_DRIVE_REBUILD
:
462 case SENSOR_DRIVE_POWERDOWN
:
463 name
= "powering down";
465 case SENSOR_DRIVE_FAIL
:
468 case SENSOR_DRIVE_PFAIL
:
478 case SENSOR_TIMEDELTA
:
479 printf("%.6f secs", s
->value
/ 1000000000.0);
486 if (s
->desc
[0] != '\0')
487 printf(" (%s)", s
->desc
);
490 case SENSOR_S_UNSPEC
:
499 printf(", CRITICAL");
501 case SENSOR_S_UNKNOWN
:
507 time_t t
= s
->tv
.tv_sec
;
512 printf(", %s.%03ld", ct
, s
->tv
.tv_usec
/ 1000);
519 T_dev_t(size_t l2
, void *p
)
521 dev_t
*d
= (dev_t
*)p
;
522 if (l2
!= sizeof(*d
))
523 err(1, "T_dev_T %zu != %zu", l2
, sizeof(*d
));
524 if ((int)(*d
) != -1) {
525 if (minor(*d
) > 255 || minor(*d
) < 0)
526 printf("{ major = %d, minor = 0x%x }",
527 major(*d
), minor(*d
));
529 printf("{ major = %d, minor = %d }",
530 major(*d
), minor(*d
));
536 set_T_dev_t(const char *path
, void **val
, size_t *size
)
538 static struct stat statb
;
540 if (strcmp(path
, "none") && strcmp(path
, "off")) {
541 int rc
= stat (path
, &statb
);
543 err(1, "cannot stat %s", path
);
546 if (!S_ISCHR(statb
.st_mode
)) {
547 errx(1, "must specify a device special file.");
550 statb
.st_rdev
= NODEV
;
552 *val
= (char*) &statb
.st_rdev
;
553 *size
= sizeof statb
.st_rdev
;
557 set_IK(const char *str
, int *val
)
564 if ((len
= strlen(str
)) == 0)
567 if (*p
== 'C' || *p
== 'F') {
568 temp
= strtof(str
, &endptr
);
569 if (endptr
== str
|| endptr
!= p
)
572 temp
= (temp
- 32) * 5 / 9;
573 kelv
= temp
* 10 + 2732;
576 * I would like to just use, 0 but it would make numbers
577 * like '023' which were interpreted as decimal before
578 * suddenly interpreted as octal.
580 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
581 kelv
= (int)strtol(str
, &endptr
, 0);
583 kelv
= (int)strtol(str
, &endptr
, 10);
584 if (endptr
== str
|| *endptr
!= '\0')
592 * These functions uses a presently undocumented interface to the kernel
593 * to walk the tree and get the type so it can print the value.
594 * This interface is under work and consideration, and should probably
595 * be killed with a big axe by the first person who can find the time.
596 * (be aware though, that the proper interface isn't as obvious as it
597 * may seem, there are various conflicting requirements.
601 oidfmt(int *oid
, size_t len
, char *fmt
, u_int
*kind
)
603 int qoid
[CTL_MAXNAME
+2];
610 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
613 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
615 err(1, "sysctl fmt %d %zu %d", i
, j
, errno
);
618 *kind
= *(u_int
*)buf
;
621 strcpy(fmt
, (char *)(buf
+ sizeof(u_int
)));
626 * This formats and outputs the value of one variable
628 * Returns zero if anything was actually output.
629 * Returns one if didn't know what to do with this.
630 * Return minus one if we had errors.
634 show_var(int *oid
, size_t nlen
)
636 u_char buf
[BUFSIZ
], *val
= NULL
, *p
, *nul
;
637 char name
[BUFSIZ
], *fmt
;
638 const char *sep
, *spacer
;
639 int qoid
[CTL_MAXNAME
+2];
643 int (*func
)(size_t, void *);
647 memcpy(qoid
+ 2, oid
, nlen
* sizeof(int));
651 i
= sysctl(qoid
, nlen
+ 2, name
, &j
, 0, 0);
653 err(1, "sysctl name %d %zu %d", i
, j
, errno
);
665 if (dflag
) { /* just print description */
668 i
= sysctl(qoid
, nlen
+ 2, buf
, &j
, 0, 0);
670 printf("%s%s", name
, sep
);
674 /* find an estimate of how much we need for this var */
676 i
= sysctl(oid
, nlen
, 0, &j
, 0, 0);
677 j
+= j
; /* we want to be sure :-) */
684 i
= sysctl(oid
, nlen
, val
, &len
, 0, 0);
691 fwrite(val
, 1, len
, stdout
);
697 oidfmt(oid
, nlen
, fmt
, &kind
);
702 printf("%s%s", name
, sep
);
703 nul
= memchr(p
, '\0', len
);
704 fwrite(p
, nul
== NULL
? (int)len
: nul
- p
, 1, stdout
);
709 printf("%s%s", name
, sep
);
712 while (len
>= sizeof(int)) {
714 printf("%s%u", spacer
, *(unsigned int *)p
);
715 else if (*fmt
== 'K' && *(int *)p
>= 0)
716 printf("%s%.1fC", spacer
, (*(int *)p
- 2732) / 10.0);
718 printf("%s%d", spacer
, *(int *)p
);
727 printf("%s%s", name
, sep
);
730 while (len
>= sizeof(long)) {
732 printf("%s%lu", spacer
, *(unsigned long *)p
);
734 printf("%s%ld", spacer
, *(long *)p
);
743 printf("%s%s", name
, sep
);
744 printf("%p", *(void **)p
);
749 printf("%s%s", name
, sep
);
752 while (len
>= sizeof(quad_t
)) {
755 spacer
, (uintmax_t)*(u_quad_t
*)p
);
758 spacer
, (intmax_t)*(quad_t
*)p
);
761 len
-= sizeof(int64_t);
762 p
+= sizeof(int64_t);
768 if (!oflag
&& !xflag
) {
770 if (strcmp(fmt
, "S,clockinfo") == 0)
772 else if (strcmp(fmt
, "S,timespec") == 0)
774 else if (strcmp(fmt
, "S,timeval") == 0)
776 else if (strcmp(fmt
, "S,loadavg") == 0)
778 else if (strcmp(fmt
, "S,sensor") == 0)
780 else if (strcmp(fmt
, "T,dev_t") == 0)
782 else if (strcmp(fmt
, "T,udev_t") == 0)
788 printf("%s%s", name
, sep
);
789 error
= (*func
)(len
, p
);
795 if (!oflag
&& !xflag
) {
800 printf("%s%s", name
, sep
);
801 printf("Format:%s Length:%zu Dump:0x", fmt
, len
);
802 while (len
-- && (xflag
|| p
< val
+ 16))
803 printf("%02x", *p
++);
804 if (!xflag
&& len
> 16)
816 sysctl_all(int *oid
, size_t len
)
818 int name1
[22], name2
[22];
826 memcpy(name1
+2, oid
, len
* sizeof(int));
834 retval
= sysctl(name1
, l1
, name2
, &l2
, 0, 0);
839 err(1, "sysctl(getnext) %d %zu", retval
, l2
);
847 for (i
= 0; i
< len
; i
++)
848 if (name2
[i
] != oid
[i
])
851 retval
= show_var(name2
, l2
);
852 if (retval
== 0 && !bflag
)
855 memcpy(name1
+2, name2
, l2
* sizeof(int));