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>
43 #include <machine/metadata.h>
46 #include <machine/inttypes.h>
56 static int aflag
, bflag
, dflag
, eflag
, Nflag
, nflag
, oflag
, xflag
;
57 static int iflag
, qflag
;
59 static int oidfmt(int *, size_t, char *, u_int
*);
60 static int parse(const char *);
61 static int show_var(int *, size_t);
62 static int sysctl_all(int *, size_t);
63 static void set_T_dev_t(const char *, void **, size_t *);
64 static int set_IK(const char *, int *);
70 fprintf(stderr
, "%s\n%s\n",
71 "usage: sysctl [-bdeNnox] variable[=value] ...",
72 " sysctl [-bdeNnox] -a");
77 main(int argc
, char **argv
)
85 while ((ch
= getopt(argc
, argv
, "AabdeiNnoqwxX")) != -1) {
138 if (aflag
&& argc
== 0)
139 exit(sysctl_all(0, 0));
144 warncount
+= parse(*argv
++);
150 * Parse a name into a MIB entry.
151 * Lookup and print out the MIB entry if it exists.
152 * Set a new value if requested.
155 parse(const char *string
)
161 unsigned int uintval
;
163 unsigned long ulongval
;
167 int mib
[CTL_MAXNAME
];
168 char *cp
, fmt
[BUFSIZ
];
170 char *name_allocated
= NULL
;
173 if ((cp
= strchr(string
, '=')) != NULL
) {
174 if ((name_allocated
= malloc(cp
- string
+ 1)) == NULL
)
175 err(1, "malloc failed");
176 strlcpy(name_allocated
, string
, cp
- string
+ 1);
177 name
= name_allocated
;
179 while (isspace(*++cp
))
183 newsize
= strlen(cp
);
189 if (sysctlnametomib(name
, mib
, &len
) < 0) {
194 if (errno
== ENOENT
) {
195 errx(1, "unknown oid '%s'", name
);
197 err(1, "sysctlnametomib(\"%s\")", name
);
201 if (oidfmt(mib
, len
, fmt
, &kind
)) {
202 warn("couldn't find format of oid '%s'", name
);
208 if (newval
== NULL
) {
209 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
210 sysctl_all(mib
, len
);
212 i
= show_var(mib
, len
);
217 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
)
218 errx(1, "oid '%s' isn't a leaf node", name
);
220 if (!(kind
&CTLFLAG_WR
))
221 errx(1, "oid '%s' is read only", name
);
223 switch (kind
& CTLTYPE
) {
225 if (!(strcmp(fmt
, "IK") == 0)) {
226 if (!set_IK(newval
, &intval
))
227 errx(1, "invalid value '%s'",
230 intval
= (int) strtol(newval
, NULL
, 0);
232 newsize
= sizeof(intval
);
235 uintval
= (int) strtoul(newval
, NULL
, 0);
237 newsize
= sizeof uintval
;
240 longval
= strtol(newval
, NULL
, 0);
242 newsize
= sizeof longval
;
245 ulongval
= strtoul(newval
, NULL
, 0);
247 newsize
= sizeof ulongval
;
252 quadval
= strtoq(newval
, NULL
, 0);
254 newsize
= sizeof(quadval
);
257 uquadval
= strtouq(newval
, NULL
, 0);
259 newsize
= sizeof(uquadval
);
262 if (strcmp(fmt
, "T,dev_t") == 0 ||
263 strcmp(fmt
, "T,udev_t") == 0
265 set_T_dev_t((char*)newval
, &newval
,
271 errx(1, "oid '%s' is type %d,"
272 " cannot set that", name
,
276 i
= show_var(mib
, len
);
277 if (sysctl(mib
, len
, 0, 0, newval
, newsize
) == -1) {
282 errx(1, "%s: value is not available",
285 errx(1, "%s: specification is incomplete",
288 errx(1, "%s: type is unknown to this program",
299 j
= show_var(mib
, len
);
305 if (name_allocated
!= NULL
)
306 free(name_allocated
);
311 /* These functions will dump out various interesting structures. */
314 S_clockinfo(size_t l2
, void *p
)
316 struct clockinfo
*ci
= (struct clockinfo
*)p
;
317 if (l2
!= sizeof(*ci
))
318 err(1, "S_clockinfo %zu != %zu", l2
, sizeof(*ci
));
319 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
320 ci
->hz
, ci
->tick
, ci
->tickadj
, ci
->profhz
, ci
->stathz
);
325 S_loadavg(size_t l2
, void *p
)
327 struct loadavg
*tv
= (struct loadavg
*)p
;
329 if (l2
!= sizeof(*tv
))
330 err(1, "S_loadavg %zu != %zu", l2
, sizeof(*tv
));
332 printf("{ %.2f %.2f %.2f }",
333 (double)tv
->ldavg
[0]/(double)tv
->fscale
,
334 (double)tv
->ldavg
[1]/(double)tv
->fscale
,
335 (double)tv
->ldavg
[2]/(double)tv
->fscale
);
340 S_timespec(size_t l2
, void *p
)
342 struct timespec
*ts
= (struct timespec
*)p
;
346 if (l2
!= sizeof(*ts
))
347 err(1, "S_timespec %zu != %zu", l2
, sizeof(*ts
));
348 printf("{ sec = %ld, nsec = %ld } ",
349 ts
->tv_sec
, ts
->tv_nsec
);
351 p1
= strdup(ctime(&tv_sec
));
352 for (p2
=p1
; *p2
; p2
++)
360 S_timeval(size_t l2
, void *p
)
362 struct timeval
*tv
= (struct timeval
*)p
;
366 if (l2
!= sizeof(*tv
))
367 err(1, "S_timeval %zu != %zu", l2
, sizeof(*tv
));
368 printf("{ sec = %ld, usec = %ld } ",
369 tv
->tv_sec
, tv
->tv_usec
);
371 p1
= strdup(ctime(&tv_sec
));
372 for (p2
=p1
; *p2
; p2
++)
380 S_sensor(size_t l2
, void *p
)
382 struct sensor
*s
= (struct sensor
*)p
;
384 if (l2
!= sizeof(*s
)) {
385 warnx("S_sensor %zu != %zu", l2
, sizeof(*s
));
389 if (s
->flags
& SENSOR_FINVALID
) {
391 * XXX: with this flag, the node should be entirely ignored,
392 * but as the magic-based sysctl(8) is not too flexible, we
393 * simply have to print out that the sensor is invalid.
399 if (s
->flags
& SENSOR_FUNKNOWN
)
405 (s
->value
- 273150000) / 1000000.0);
408 printf("%jd RPM", (intmax_t)s
->value
);
410 case SENSOR_VOLTS_DC
:
411 printf("%.2f VDC", s
->value
/ 1000000.0);
414 printf("%.2f W", s
->value
/ 1000000.0);
417 printf("%.2f A", s
->value
/ 1000000.0);
419 case SENSOR_WATTHOUR
:
420 printf("%.2f Wh", s
->value
/ 1000000.0);
423 printf("%.2f Ah", s
->value
/ 1000000.0);
425 case SENSOR_INDICATOR
:
426 printf("%s", s
->value
? "On" : "Off");
429 printf("%jd Hz", (intmax_t)s
->value
);
433 printf("%jd", (intmax_t)s
->value
);
436 printf("%.2f%%", s
->value
/ 1000.0);
439 printf("%.2f lx", s
->value
/ 1000000.0);
446 case SENSOR_DRIVE_EMPTY
:
449 case SENSOR_DRIVE_READY
:
452 case SENSOR_DRIVE_POWERUP
:
453 name
= "powering up";
455 case SENSOR_DRIVE_ONLINE
:
458 case SENSOR_DRIVE_IDLE
:
461 case SENSOR_DRIVE_ACTIVE
:
464 case SENSOR_DRIVE_REBUILD
:
467 case SENSOR_DRIVE_POWERDOWN
:
468 name
= "powering down";
470 case SENSOR_DRIVE_FAIL
:
473 case SENSOR_DRIVE_PFAIL
:
483 case SENSOR_TIMEDELTA
:
484 printf("%.6f secs", s
->value
/ 1000000000.0);
491 if (s
->desc
[0] != '\0')
492 printf(" (%s)", s
->desc
);
495 case SENSOR_S_UNSPEC
:
504 printf(", CRITICAL");
506 case SENSOR_S_UNKNOWN
:
512 time_t t
= s
->tv
.tv_sec
;
517 printf(", %s.%03ld", ct
, s
->tv
.tv_usec
/ 1000);
525 S_efi_map(size_t l2
, void *p
)
527 struct efi_map_header
*efihdr
;
533 static const char *types
[] = {
539 "RuntimeServicesCode",
540 "RuntimeServicesData",
541 "ConventionalMemory",
546 "MemoryMappedIOPortSpace",
551 * Memory map data provided by UEFI via the GetMemoryMap
554 if (l2
< sizeof(*efihdr
)) {
555 warnx("S_efi_map length less than header");
559 efisz
= (sizeof(struct efi_map_header
) + 0xf) & ~0xf;
560 map
= (struct efi_md
*)((uint8_t *)efihdr
+ efisz
);
562 if (efihdr
->descriptor_size
== 0)
564 if (l2
!= efisz
+ efihdr
->memory_size
) {
565 warnx("S_efi_map length mismatch %zu vs %zu", l2
, efisz
+
566 efihdr
->memory_size
);
569 ndesc
= efihdr
->memory_size
/ efihdr
->descriptor_size
;
571 printf("\n%23s %12s %12s %8s %4s",
572 "Type", "Physical", "Virtual", "#Pages", "Attr");
574 for (i
= 0; i
< ndesc
; i
++,
575 map
= efi_next_descriptor(map
, efihdr
->descriptor_size
)) {
576 if (map
->md_type
<= EFI_MD_TYPE_PALCODE
)
577 type
= types
[map
->md_type
];
580 printf("\n%23s %012lx %12p %08lx ", type
, map
->md_phys
,
581 map
->md_virt
, map
->md_pages
);
582 if (map
->md_attr
& EFI_MD_ATTR_UC
)
584 if (map
->md_attr
& EFI_MD_ATTR_WC
)
586 if (map
->md_attr
& EFI_MD_ATTR_WT
)
588 if (map
->md_attr
& EFI_MD_ATTR_WB
)
590 if (map
->md_attr
& EFI_MD_ATTR_UCE
)
592 if (map
->md_attr
& EFI_MD_ATTR_WP
)
594 if (map
->md_attr
& EFI_MD_ATTR_RP
)
596 if (map
->md_attr
& EFI_MD_ATTR_XP
)
598 if (map
->md_attr
& EFI_MD_ATTR_RT
)
606 T_dev_t(size_t l2
, void *p
)
608 dev_t
*d
= (dev_t
*)p
;
609 if (l2
!= sizeof(*d
))
610 err(1, "T_dev_T %zu != %zu", l2
, sizeof(*d
));
611 if ((int)(*d
) != -1) {
612 if (minor(*d
) > 255 || minor(*d
) < 0)
613 printf("{ major = %d, minor = 0x%x }",
614 major(*d
), minor(*d
));
616 printf("{ major = %d, minor = %d }",
617 major(*d
), minor(*d
));
623 set_T_dev_t(const char *path
, void **val
, size_t *size
)
625 static struct stat statb
;
627 if (strcmp(path
, "none") && strcmp(path
, "off")) {
628 int rc
= stat (path
, &statb
);
630 err(1, "cannot stat %s", path
);
633 if (!S_ISCHR(statb
.st_mode
)) {
634 errx(1, "must specify a device special file.");
637 statb
.st_rdev
= NODEV
;
639 *val
= (char*) &statb
.st_rdev
;
640 *size
= sizeof statb
.st_rdev
;
644 set_IK(const char *str
, int *val
)
651 if ((len
= strlen(str
)) == 0)
654 if (*p
== 'C' || *p
== 'F') {
655 temp
= strtof(str
, &endptr
);
656 if (endptr
== str
|| endptr
!= p
)
659 temp
= (temp
- 32) * 5 / 9;
660 kelv
= temp
* 10 + 2732;
663 * I would like to just use, 0 but it would make numbers
664 * like '023' which were interpreted as decimal before
665 * suddenly interpreted as octal.
667 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
668 kelv
= (int)strtol(str
, &endptr
, 0);
670 kelv
= (int)strtol(str
, &endptr
, 10);
671 if (endptr
== str
|| *endptr
!= '\0')
679 * These functions uses a presently undocumented interface to the kernel
680 * to walk the tree and get the type so it can print the value.
681 * This interface is under work and consideration, and should probably
682 * be killed with a big axe by the first person who can find the time.
683 * (be aware though, that the proper interface isn't as obvious as it
684 * may seem, there are various conflicting requirements.
688 oidfmt(int *oid
, size_t len
, char *fmt
, u_int
*kind
)
690 int qoid
[CTL_MAXNAME
+2];
697 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
700 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
702 err(1, "sysctl fmt %d %zu %d", i
, j
, errno
);
705 *kind
= *(u_int
*)buf
;
708 strcpy(fmt
, (char *)(buf
+ sizeof(u_int
)));
713 * This formats and outputs the value of one variable
715 * Returns zero if anything was actually output.
716 * Returns one if didn't know what to do with this.
717 * Return minus one if we had errors.
721 show_var(int *oid
, size_t nlen
)
723 u_char buf
[BUFSIZ
], *val
= NULL
, *p
, *nul
;
724 char name
[BUFSIZ
], *fmt
;
725 const char *sep
, *spacer
;
726 int qoid
[CTL_MAXNAME
+2];
730 int (*func
)(size_t, void *);
734 memcpy(qoid
+ 2, oid
, nlen
* sizeof(int));
738 i
= sysctl(qoid
, nlen
+ 2, name
, &j
, 0, 0);
740 err(1, "sysctl name %d %zu %d", i
, j
, errno
);
752 if (dflag
) { /* just print description */
755 i
= sysctl(qoid
, nlen
+ 2, buf
, &j
, 0, 0);
757 printf("%s%s", name
, sep
);
761 /* find an estimate of how much we need for this var */
763 i
= sysctl(oid
, nlen
, 0, &j
, 0, 0);
764 j
+= j
; /* we want to be sure :-) */
771 i
= sysctl(oid
, nlen
, val
, &len
, 0, 0);
778 fwrite(val
, 1, len
, stdout
);
784 oidfmt(oid
, nlen
, fmt
, &kind
);
789 printf("%s%s", name
, sep
);
790 nul
= memchr(p
, '\0', len
);
791 fwrite(p
, nul
== NULL
? (int)len
: nul
- p
, 1, stdout
);
796 printf("%s%s", name
, sep
);
799 while (len
>= sizeof(int)) {
801 printf("%s%u", spacer
, *(unsigned int *)p
);
802 else if (*fmt
== 'K' && *(int *)p
>= 0)
803 printf("%s%.1fC", spacer
, (*(int *)p
- 2732) / 10.0);
805 printf("%s%d", spacer
, *(int *)p
);
814 printf("%s%s", name
, sep
);
817 while (len
>= sizeof(long)) {
819 printf("%s%lu", spacer
, *(unsigned long *)p
);
821 printf("%s%ld", spacer
, *(long *)p
);
830 printf("%s%s", name
, sep
);
831 printf("%p", *(void **)p
);
836 printf("%s%s", name
, sep
);
839 while (len
>= sizeof(quad_t
)) {
842 spacer
, (uintmax_t)*(u_quad_t
*)p
);
845 spacer
, (intmax_t)*(quad_t
*)p
);
848 len
-= sizeof(int64_t);
849 p
+= sizeof(int64_t);
855 if (!oflag
&& !xflag
) {
857 if (strcmp(fmt
, "S,clockinfo") == 0)
859 else if (strcmp(fmt
, "S,timespec") == 0)
861 else if (strcmp(fmt
, "S,timeval") == 0)
863 else if (strcmp(fmt
, "S,loadavg") == 0)
865 else if (strcmp(fmt
, "S,sensor") == 0)
868 else if (strcmp(fmt
, "S,efi_map_header") == 0)
871 else if (strcmp(fmt
, "T,dev_t") == 0)
873 else if (strcmp(fmt
, "T,udev_t") == 0)
879 printf("%s%s", name
, sep
);
880 error
= (*func
)(len
, p
);
886 if (!oflag
&& !xflag
) {
891 printf("%s%s", name
, sep
);
892 printf("Format:%s Length:%zu Dump:0x", fmt
, len
);
893 while (len
-- && (xflag
|| p
< val
+ 16))
894 printf("%02x", *p
++);
895 if (!xflag
&& len
> 16)
907 sysctl_all(int *oid
, size_t len
)
909 int name1
[22], name2
[22];
917 memcpy(name1
+2, oid
, len
* sizeof(int));
925 retval
= sysctl(name1
, l1
, name2
, &l2
, 0, 0);
930 err(1, "sysctl(getnext) %d %zu", retval
, l2
);
938 for (i
= 0; i
< len
; i
++)
939 if (name2
[i
] != oid
[i
])
942 retval
= show_var(name2
, l2
);
943 if (retval
== 0 && !bflag
)
946 memcpy(name1
+2, name2
, l2
* sizeof(int));