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
;
53 static int oidfmt(int *, size_t, char *, u_int
*);
54 static void parse(const char *);
55 static int show_var(int *, size_t);
56 static int sysctl_all(int *, size_t);
57 static void set_T_dev_t(const char *, void **, size_t *);
58 static int set_IK(const char *, int *);
64 fprintf(stderr
, "%s\n%s\n",
65 "usage: sysctl [-bdeNnox] variable[=value] ...",
66 " sysctl [-bdeNnox] -a");
71 main(int argc
, char **argv
)
77 while ((ch
= getopt(argc
, argv
, "AabdeNnowxX")) != -1) {
124 if (aflag
&& argc
== 0)
125 exit(sysctl_all(0, 0));
134 * Parse a name into a MIB entry.
135 * Lookup and print out the MIB entry if it exists.
136 * Set a new value if requested.
139 parse(const char *string
)
145 unsigned int uintval
;
147 unsigned long ulongval
;
151 int mib
[CTL_MAXNAME
];
152 char *cp
, fmt
[BUFSIZ
];
154 char *name_allocated
= NULL
;
157 if ((cp
= strchr(string
, '=')) != NULL
) {
158 if ((name_allocated
= malloc(cp
- string
+ 1)) == NULL
)
159 err(1, "malloc failed");
160 strlcpy(name_allocated
, string
, cp
- string
+ 1);
161 name
= name_allocated
;
163 while (isspace(*++cp
))
167 newsize
= strlen(cp
);
173 if (sysctlnametomib(name
, mib
, &len
) < 0) {
174 if (errno
== ENOENT
) {
175 errx(1, "unknown oid '%s'", name
);
177 err(1, "sysctlnametomib(\"%s\")", name
);
181 if (oidfmt(mib
, len
, fmt
, &kind
))
182 err(1, "couldn't find format of oid '%s'", name
);
184 if (newval
== NULL
) {
185 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
186 sysctl_all(mib
, len
);
188 i
= show_var(mib
, len
);
193 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
)
194 errx(1, "oid '%s' isn't a leaf node", name
);
196 if (!(kind
&CTLFLAG_WR
))
197 errx(1, "oid '%s' is read only", name
);
199 switch (kind
& CTLTYPE
) {
201 if (!(strcmp(fmt
, "IK") == 0)) {
202 if (!set_IK(newval
, &intval
))
203 errx(1, "invalid value '%s'",
206 intval
= (int) strtol(newval
, NULL
, 0);
208 newsize
= sizeof(intval
);
211 uintval
= (int) strtoul(newval
, NULL
, 0);
213 newsize
= sizeof uintval
;
216 longval
= strtol(newval
, NULL
, 0);
218 newsize
= sizeof longval
;
221 ulongval
= strtoul(newval
, NULL
, 0);
223 newsize
= sizeof ulongval
;
228 quadval
= strtoq(newval
, NULL
, 0);
230 newsize
= sizeof(quadval
);
233 uquadval
= strtouq(newval
, NULL
, 0);
235 newsize
= sizeof(uquadval
);
238 if (strcmp(fmt
, "T,dev_t") == 0 ||
239 strcmp(fmt
, "T,udev_t") == 0
241 set_T_dev_t((char*)newval
, &newval
,
247 errx(1, "oid '%s' is type %d,"
248 " cannot set that", name
,
252 i
= show_var(mib
, len
);
253 if (sysctl(mib
, len
, 0, 0, newval
, newsize
) == -1) {
258 errx(1, "%s: value is not available",
261 errx(1, "%s: specification is incomplete",
264 errx(1, "%s: type is unknown to this program",
275 j
= show_var(mib
, len
);
281 if (name_allocated
!= NULL
)
282 free(name_allocated
);
285 /* These functions will dump out various interesting structures. */
288 S_clockinfo(int l2
, void *p
)
290 struct clockinfo
*ci
= (struct clockinfo
*)p
;
291 if (l2
!= sizeof(*ci
))
292 err(1, "S_clockinfo %d != %zu", l2
, sizeof(*ci
));
293 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
294 ci
->hz
, ci
->tick
, ci
->tickadj
, ci
->profhz
, ci
->stathz
);
299 S_loadavg(int l2
, void *p
)
301 struct loadavg
*tv
= (struct loadavg
*)p
;
303 if (l2
!= sizeof(*tv
))
304 err(1, "S_loadavg %d != %zu", l2
, sizeof(*tv
));
306 printf("{ %.2f %.2f %.2f }",
307 (double)tv
->ldavg
[0]/(double)tv
->fscale
,
308 (double)tv
->ldavg
[1]/(double)tv
->fscale
,
309 (double)tv
->ldavg
[2]/(double)tv
->fscale
);
314 S_timespec(int l2
, void *p
)
316 struct timespec
*ts
= (struct timespec
*)p
;
320 if (l2
!= sizeof(*ts
))
321 err(1, "S_timespec %d != %zu", l2
, sizeof(*ts
));
322 printf("{ sec = %ld, nsec = %ld } ",
323 ts
->tv_sec
, ts
->tv_nsec
);
325 p1
= strdup(ctime(&tv_sec
));
326 for (p2
=p1
; *p2
; p2
++)
334 S_timeval(int l2
, void *p
)
336 struct timeval
*tv
= (struct timeval
*)p
;
340 if (l2
!= sizeof(*tv
))
341 err(1, "S_timeval %d != %zu", l2
, sizeof(*tv
));
342 printf("{ sec = %ld, usec = %ld } ",
343 tv
->tv_sec
, tv
->tv_usec
);
345 p1
= strdup(ctime(&tv_sec
));
346 for (p2
=p1
; *p2
; p2
++)
354 S_sensor(int l2
, void *p
)
356 struct sensor
*s
= (struct sensor
*)p
;
358 if (l2
!= sizeof(*s
)) {
359 warnx("S_sensor %d != %zu", l2
, sizeof(*s
));
363 if (s
->flags
& SENSOR_FINVALID
) {
365 * XXX: with this flag, the node should be entirely ignored,
366 * but as the magic-based sysctl(8) is not too flexible, we
367 * simply have to print out that the sensor is invalid.
373 if (s
->flags
& SENSOR_FUNKNOWN
)
379 (s
->value
- 273150000) / 1000000.0);
382 printf("%jd RPM", (intmax_t)s
->value
);
384 case SENSOR_VOLTS_DC
:
385 printf("%.2f VDC", s
->value
/ 1000000.0);
388 printf("%.2f W", s
->value
/ 1000000.0);
391 printf("%.2f A", s
->value
/ 1000000.0);
393 case SENSOR_WATTHOUR
:
394 printf("%.2f Wh", s
->value
/ 1000000.0);
397 printf("%.2f Ah", s
->value
/ 1000000.0);
399 case SENSOR_INDICATOR
:
400 printf("%s", s
->value
? "On" : "Off");
403 printf("%jd Hz", (intmax_t)s
->value
);
407 printf("%jd", (intmax_t)s
->value
);
410 printf("%.2f%%", s
->value
/ 1000.0);
413 printf("%.2f lx", s
->value
/ 1000000.0);
420 case SENSOR_DRIVE_EMPTY
:
423 case SENSOR_DRIVE_READY
:
426 case SENSOR_DRIVE_POWERUP
:
427 name
= "powering up";
429 case SENSOR_DRIVE_ONLINE
:
432 case SENSOR_DRIVE_IDLE
:
435 case SENSOR_DRIVE_ACTIVE
:
438 case SENSOR_DRIVE_REBUILD
:
441 case SENSOR_DRIVE_POWERDOWN
:
442 name
= "powering down";
444 case SENSOR_DRIVE_FAIL
:
447 case SENSOR_DRIVE_PFAIL
:
457 case SENSOR_TIMEDELTA
:
458 printf("%.6f secs", s
->value
/ 1000000000.0);
465 if (s
->desc
[0] != '\0')
466 printf(" (%s)", s
->desc
);
469 case SENSOR_S_UNSPEC
:
478 printf(", CRITICAL");
480 case SENSOR_S_UNKNOWN
:
486 time_t t
= s
->tv
.tv_sec
;
491 printf(", %s.%03ld", ct
, s
->tv
.tv_usec
/ 1000);
498 T_dev_t(int l2
, void *p
)
500 dev_t
*d
= (dev_t
*)p
;
501 if (l2
!= sizeof(*d
))
502 err(1, "T_dev_T %d != %zu", l2
, sizeof(*d
));
503 if ((int)(*d
) != -1) {
504 if (minor(*d
) > 255 || minor(*d
) < 0)
505 printf("{ major = %d, minor = 0x%x }",
506 major(*d
), minor(*d
));
508 printf("{ major = %d, minor = %d }",
509 major(*d
), minor(*d
));
515 set_T_dev_t(const char *path
, void **val
, size_t *size
)
517 static struct stat statb
;
519 if (strcmp(path
, "none") && strcmp(path
, "off")) {
520 int rc
= stat (path
, &statb
);
522 err(1, "cannot stat %s", path
);
525 if (!S_ISCHR(statb
.st_mode
)) {
526 errx(1, "must specify a device special file.");
529 statb
.st_rdev
= NODEV
;
531 *val
= (char*) &statb
.st_rdev
;
532 *size
= sizeof statb
.st_rdev
;
536 set_IK(const char *str
, int *val
)
543 if ((len
= strlen(str
)) == 0)
546 if (*p
== 'C' || *p
== 'F') {
547 temp
= strtof(str
, &endptr
);
548 if (endptr
== str
|| endptr
!= p
)
551 temp
= (temp
- 32) * 5 / 9;
552 kelv
= temp
* 10 + 2732;
555 * I would like to just use, 0 but it would make numbers
556 * like '023' which were interpreted as decimal before
557 * suddenly interpreted as octal.
559 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
560 kelv
= (int)strtol(str
, &endptr
, 0);
562 kelv
= (int)strtol(str
, &endptr
, 10);
563 if (endptr
== str
|| *endptr
!= '\0')
571 * These functions uses a presently undocumented interface to the kernel
572 * to walk the tree and get the type so it can print the value.
573 * This interface is under work and consideration, and should probably
574 * be killed with a big axe by the first person who can find the time.
575 * (be aware though, that the proper interface isn't as obvious as it
576 * may seem, there are various conflicting requirements.
580 oidfmt(int *oid
, size_t len
, char *fmt
, u_int
*kind
)
582 int qoid
[CTL_MAXNAME
+2];
589 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
592 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
594 err(1, "sysctl fmt %d %zu %d", i
, j
, errno
);
597 *kind
= *(u_int
*)buf
;
600 strcpy(fmt
, (char *)(buf
+ sizeof(u_int
)));
605 * This formats and outputs the value of one variable
607 * Returns zero if anything was actually output.
608 * Returns one if didn't know what to do with this.
609 * Return minus one if we had errors.
613 show_var(int *oid
, size_t nlen
)
615 u_char buf
[BUFSIZ
], *val
= NULL
, *p
, *nul
;
616 char name
[BUFSIZ
], *fmt
;
617 const char *sep
, *spacer
;
618 int qoid
[CTL_MAXNAME
+2];
622 int (*func
)(int, void *);
626 memcpy(qoid
+ 2, oid
, nlen
* sizeof(int));
630 i
= sysctl(qoid
, nlen
+ 2, name
, &j
, 0, 0);
632 err(1, "sysctl name %d %zu %d", i
, j
, errno
);
644 if (dflag
) { /* just print description */
647 i
= sysctl(qoid
, nlen
+ 2, buf
, &j
, 0, 0);
649 printf("%s%s", name
, sep
);
653 /* find an estimate of how much we need for this var */
655 i
= sysctl(oid
, nlen
, 0, &j
, 0, 0);
656 j
+= j
; /* we want to be sure :-) */
663 i
= sysctl(oid
, nlen
, val
, &len
, 0, 0);
670 fwrite(val
, 1, len
, stdout
);
676 oidfmt(oid
, nlen
, fmt
, &kind
);
681 printf("%s%s", name
, sep
);
682 nul
= memchr(p
, '\0', len
);
683 fwrite(p
, nul
== NULL
? (int)len
: nul
- p
, 1, stdout
);
688 printf("%s%s", name
, sep
);
691 while (len
>= sizeof(int)) {
693 printf("%s%u", spacer
, *(unsigned int *)p
);
694 else if (*fmt
== 'K' && *(int *)p
>= 0)
695 printf("%s%.1fC", spacer
, (*(int *)p
- 2732) / 10.0);
697 printf("%s%d", spacer
, *(int *)p
);
706 printf("%s%s", name
, sep
);
709 while (len
>= sizeof(long)) {
711 printf("%s%lu", spacer
, *(unsigned long *)p
);
713 printf("%s%ld", spacer
, *(long *)p
);
722 printf("%s%s", name
, sep
);
723 printf("%p", *(void **)p
);
728 printf("%s%s", name
, sep
);
731 while (len
>= sizeof(quad_t
)) {
734 spacer
, (uintmax_t)*(u_quad_t
*)p
);
737 spacer
, (intmax_t)*(quad_t
*)p
);
740 len
-= sizeof(int64_t);
741 p
+= sizeof(int64_t);
747 if (!oflag
&& !xflag
) {
749 if (strcmp(fmt
, "S,clockinfo") == 0)
751 else if (strcmp(fmt
, "S,timespec") == 0)
753 else if (strcmp(fmt
, "S,timeval") == 0)
755 else if (strcmp(fmt
, "S,loadavg") == 0)
757 else if (strcmp(fmt
, "S,sensor") == 0)
759 else if (strcmp(fmt
, "T,dev_t") == 0)
761 else if (strcmp(fmt
, "T,udev_t") == 0)
767 printf("%s%s", name
, sep
);
768 error
= (*func
)(len
, p
);
774 if (!oflag
&& !xflag
) {
779 printf("%s%s", name
, sep
);
780 printf("Format:%s Length:%zu Dump:0x", fmt
, len
);
781 while (len
-- && (xflag
|| p
< val
+ 16))
782 printf("%02x", *p
++);
783 if (!xflag
&& len
> 16)
795 sysctl_all(int *oid
, size_t len
)
797 int name1
[22], name2
[22];
805 memcpy(name1
+2, oid
, len
* sizeof(int));
813 retval
= sysctl(name1
, l1
, name2
, &l2
, 0, 0);
818 err(1, "sysctl(getnext) %d %zu", retval
, l2
);
826 for (i
= 0; i
< len
; i
++)
827 if (name2
[i
] != oid
[i
])
830 retval
= show_var(name2
, l2
);
831 if (retval
== 0 && !bflag
)
834 memcpy(name1
+2, name2
, l2
* sizeof(int));