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
)
162 unsigned int uintval
;
164 unsigned long ulongval
;
168 int mib
[CTL_MAXNAME
];
169 char *cp
, fmt
[BUFSIZ
];
171 char *name_allocated
= NULL
;
174 if ((cp
= strchr(string
, '=')) != NULL
) {
175 if ((name_allocated
= malloc(cp
- string
+ 1)) == NULL
)
176 err(1, "malloc failed");
177 strlcpy(name_allocated
, string
, cp
- string
+ 1);
178 name
= name_allocated
;
180 while (isspace(*++cp
))
184 newsize
= strlen(cp
);
190 if (sysctlnametomib(name
, mib
, &len
) < 0) {
195 if (errno
== ENOENT
) {
196 errx(1, "unknown oid '%s'", name
);
198 err(1, "sysctlnametomib(\"%s\")", name
);
202 if (oidfmt(mib
, len
, fmt
, &kind
)) {
203 warn("couldn't find format of oid '%s'", name
);
209 if (newval
== NULL
) {
210 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
211 sysctl_all(mib
, len
);
213 i
= show_var(mib
, len
);
218 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
)
219 errx(1, "oid '%s' isn't a leaf node", name
);
221 if (!(kind
&CTLFLAG_WR
))
222 errx(1, "oid '%s' is read only", name
);
224 switch (kind
& CTLTYPE
) {
226 if (!(strcmp(fmt
, "IK") == 0)) {
227 if (!set_IK(newval
, &intval
))
228 errx(1, "invalid value '%s'",
231 intval
= (int) strtol(newval
, NULL
, 0);
233 newsize
= sizeof(intval
);
236 uintval
= (int) strtoul(newval
, NULL
, 0);
238 newsize
= sizeof uintval
;
241 longval
= strtol(newval
, NULL
, 0);
243 newsize
= sizeof longval
;
246 ulongval
= strtoul(newval
, NULL
, 0);
248 newsize
= sizeof ulongval
;
253 quadval
= strtoq(newval
, NULL
, 0);
255 newsize
= sizeof(quadval
);
258 uquadval
= strtouq(newval
, NULL
, 0);
260 newsize
= sizeof(uquadval
);
263 u32val
= (uint32_t)strtoul(newval
, NULL
, 0);
265 newsize
= sizeof(u32val
);
268 if (strcmp(fmt
, "T,dev_t") == 0 ||
269 strcmp(fmt
, "T,udev_t") == 0
271 set_T_dev_t((char*)newval
, &newval
,
277 errx(1, "oid '%s' is type %d,"
278 " cannot set that", name
,
282 i
= show_var(mib
, len
);
283 if (sysctl(mib
, len
, 0, 0, newval
, newsize
) == -1) {
288 errx(1, "%s: value is not available",
291 errx(1, "%s: specification is incomplete",
294 errx(1, "%s: type is unknown to this program",
305 j
= show_var(mib
, len
);
311 if (name_allocated
!= NULL
)
312 free(name_allocated
);
317 /* These functions will dump out various interesting structures. */
320 S_clockinfo(size_t l2
, void *p
)
322 struct clockinfo
*ci
= (struct clockinfo
*)p
;
323 if (l2
!= sizeof(*ci
))
324 err(1, "S_clockinfo %zu != %zu", l2
, sizeof(*ci
));
325 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
326 ci
->hz
, ci
->tick
, ci
->tickadj
, ci
->profhz
, ci
->stathz
);
331 S_loadavg(size_t l2
, void *p
)
333 struct loadavg
*tv
= (struct loadavg
*)p
;
335 if (l2
!= sizeof(*tv
))
336 err(1, "S_loadavg %zu != %zu", l2
, sizeof(*tv
));
338 printf("{ %.2f %.2f %.2f }",
339 (double)tv
->ldavg
[0]/(double)tv
->fscale
,
340 (double)tv
->ldavg
[1]/(double)tv
->fscale
,
341 (double)tv
->ldavg
[2]/(double)tv
->fscale
);
346 S_timespec(size_t l2
, void *p
)
348 struct timespec
*ts
= (struct timespec
*)p
;
352 if (l2
!= sizeof(*ts
))
353 err(1, "S_timespec %zu != %zu", l2
, sizeof(*ts
));
354 printf("{ sec = %ld, nsec = %ld } ",
355 ts
->tv_sec
, ts
->tv_nsec
);
357 p1
= strdup(ctime(&tv_sec
));
358 for (p2
=p1
; *p2
; p2
++)
366 S_timeval(size_t l2
, void *p
)
368 struct timeval
*tv
= (struct timeval
*)p
;
372 if (l2
!= sizeof(*tv
))
373 err(1, "S_timeval %zu != %zu", l2
, sizeof(*tv
));
374 printf("{ sec = %ld, usec = %ld } ",
375 tv
->tv_sec
, tv
->tv_usec
);
377 p1
= strdup(ctime(&tv_sec
));
378 for (p2
=p1
; *p2
; p2
++)
386 S_sensor(size_t l2
, void *p
)
388 struct sensor
*s
= (struct sensor
*)p
;
390 if (l2
!= sizeof(*s
)) {
391 warnx("S_sensor %zu != %zu", l2
, sizeof(*s
));
395 if (s
->flags
& SENSOR_FINVALID
) {
397 * XXX: with this flag, the node should be entirely ignored,
398 * but as the magic-based sysctl(8) is not too flexible, we
399 * simply have to print out that the sensor is invalid.
405 if (s
->flags
& SENSOR_FUNKNOWN
)
411 (s
->value
- 273150000) / 1000000.0);
414 printf("%jd RPM", (intmax_t)s
->value
);
416 case SENSOR_VOLTS_DC
:
417 printf("%.2f VDC", s
->value
/ 1000000.0);
420 printf("%.2f W", s
->value
/ 1000000.0);
423 printf("%.2f A", s
->value
/ 1000000.0);
425 case SENSOR_WATTHOUR
:
426 printf("%.2f Wh", s
->value
/ 1000000.0);
429 printf("%.2f Ah", s
->value
/ 1000000.0);
431 case SENSOR_INDICATOR
:
432 printf("%s", s
->value
? "On" : "Off");
435 printf("%jd Hz", (intmax_t)s
->value
);
439 printf("%jd", (intmax_t)s
->value
);
442 printf("%.2f%%", s
->value
/ 1000.0);
445 printf("%.2f lx", s
->value
/ 1000000.0);
452 case SENSOR_DRIVE_EMPTY
:
455 case SENSOR_DRIVE_READY
:
458 case SENSOR_DRIVE_POWERUP
:
459 name
= "powering up";
461 case SENSOR_DRIVE_ONLINE
:
464 case SENSOR_DRIVE_IDLE
:
467 case SENSOR_DRIVE_ACTIVE
:
470 case SENSOR_DRIVE_REBUILD
:
473 case SENSOR_DRIVE_POWERDOWN
:
474 name
= "powering down";
476 case SENSOR_DRIVE_FAIL
:
479 case SENSOR_DRIVE_PFAIL
:
489 case SENSOR_TIMEDELTA
:
490 printf("%.6f secs", s
->value
/ 1000000000.0);
497 if (s
->desc
[0] != '\0')
498 printf(" (%s)", s
->desc
);
501 case SENSOR_S_UNSPEC
:
510 printf(", CRITICAL");
512 case SENSOR_S_UNKNOWN
:
518 time_t t
= s
->tv
.tv_sec
;
523 printf(", %s.%03ld", ct
, s
->tv
.tv_usec
/ 1000);
531 S_efi_map(size_t l2
, void *p
)
533 struct efi_map_header
*efihdr
;
539 static const char *types
[] = {
545 "RuntimeServicesCode",
546 "RuntimeServicesData",
547 "ConventionalMemory",
552 "MemoryMappedIOPortSpace",
557 * Memory map data provided by UEFI via the GetMemoryMap
560 if (l2
< sizeof(*efihdr
)) {
561 warnx("S_efi_map length less than header");
565 efisz
= (sizeof(struct efi_map_header
) + 0xf) & ~0xf;
566 map
= (struct efi_md
*)((uint8_t *)efihdr
+ efisz
);
568 if (efihdr
->descriptor_size
== 0)
570 if (l2
!= efisz
+ efihdr
->memory_size
) {
571 warnx("S_efi_map length mismatch %zu vs %zu", l2
, efisz
+
572 efihdr
->memory_size
);
575 ndesc
= efihdr
->memory_size
/ efihdr
->descriptor_size
;
577 printf("\n%23s %12s %12s %8s %4s",
578 "Type", "Physical", "Virtual", "#Pages", "Attr");
580 for (i
= 0; i
< ndesc
; i
++,
581 map
= efi_next_descriptor(map
, efihdr
->descriptor_size
)) {
582 if (map
->md_type
<= EFI_MD_TYPE_PALCODE
)
583 type
= types
[map
->md_type
];
586 printf("\n%23s %012lx %12p %08lx ", type
, map
->md_phys
,
587 map
->md_virt
, map
->md_pages
);
588 if (map
->md_attr
& EFI_MD_ATTR_UC
)
590 if (map
->md_attr
& EFI_MD_ATTR_WC
)
592 if (map
->md_attr
& EFI_MD_ATTR_WT
)
594 if (map
->md_attr
& EFI_MD_ATTR_WB
)
596 if (map
->md_attr
& EFI_MD_ATTR_UCE
)
598 if (map
->md_attr
& EFI_MD_ATTR_WP
)
600 if (map
->md_attr
& EFI_MD_ATTR_RP
)
602 if (map
->md_attr
& EFI_MD_ATTR_XP
)
604 if (map
->md_attr
& EFI_MD_ATTR_RT
)
612 T_dev_t(size_t l2
, void *p
)
614 dev_t
*d
= (dev_t
*)p
;
615 if (l2
!= sizeof(*d
))
616 err(1, "T_dev_T %zu != %zu", l2
, sizeof(*d
));
617 if ((int)(*d
) != -1) {
618 if (minor(*d
) > 255 || minor(*d
) < 0)
619 printf("{ major = %d, minor = 0x%x }",
620 major(*d
), minor(*d
));
622 printf("{ major = %d, minor = %d }",
623 major(*d
), minor(*d
));
629 set_T_dev_t(const char *path
, void **val
, size_t *size
)
631 static struct stat statb
;
633 if (strcmp(path
, "none") && strcmp(path
, "off")) {
634 int rc
= stat (path
, &statb
);
636 err(1, "cannot stat %s", path
);
639 if (!S_ISCHR(statb
.st_mode
)) {
640 errx(1, "must specify a device special file.");
643 statb
.st_rdev
= NODEV
;
645 *val
= (char*) &statb
.st_rdev
;
646 *size
= sizeof statb
.st_rdev
;
650 set_IK(const char *str
, int *val
)
657 if ((len
= strlen(str
)) == 0)
660 if (*p
== 'C' || *p
== 'F') {
661 temp
= strtof(str
, &endptr
);
662 if (endptr
== str
|| endptr
!= p
)
665 temp
= (temp
- 32) * 5 / 9;
666 kelv
= temp
* 10 + 2732;
669 * I would like to just use, 0 but it would make numbers
670 * like '023' which were interpreted as decimal before
671 * suddenly interpreted as octal.
673 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
674 kelv
= (int)strtol(str
, &endptr
, 0);
676 kelv
= (int)strtol(str
, &endptr
, 10);
677 if (endptr
== str
|| *endptr
!= '\0')
685 * These functions uses a presently undocumented interface to the kernel
686 * to walk the tree and get the type so it can print the value.
687 * This interface is under work and consideration, and should probably
688 * be killed with a big axe by the first person who can find the time.
689 * (be aware though, that the proper interface isn't as obvious as it
690 * may seem, there are various conflicting requirements.
694 oidfmt(int *oid
, size_t len
, char *fmt
, u_int
*kind
)
696 int qoid
[CTL_MAXNAME
+2];
703 memcpy(qoid
+ 2, oid
, len
* sizeof(int));
706 i
= sysctl(qoid
, len
+ 2, buf
, &j
, 0, 0);
708 err(1, "sysctl fmt %d %zu %d", i
, j
, errno
);
711 *kind
= *(u_int
*)buf
;
714 strcpy(fmt
, (char *)(buf
+ sizeof(u_int
)));
719 * This formats and outputs the value of one variable
721 * Returns zero if anything was actually output.
722 * Returns one if didn't know what to do with this.
723 * Return minus one if we had errors.
727 show_var(int *oid
, size_t nlen
)
729 u_char buf
[BUFSIZ
], *val
= NULL
, *p
, *nul
;
730 char name
[BUFSIZ
], *fmt
;
731 const char *sep
, *spacer
;
732 int qoid
[CTL_MAXNAME
+2];
736 int (*func
)(size_t, void *);
740 memcpy(qoid
+ 2, oid
, nlen
* sizeof(int));
744 i
= sysctl(qoid
, nlen
+ 2, name
, &j
, 0, 0);
746 err(1, "sysctl name %d %zu %d", i
, j
, errno
);
758 if (dflag
) { /* just print description */
761 i
= sysctl(qoid
, nlen
+ 2, buf
, &j
, 0, 0);
763 printf("%s%s", name
, sep
);
767 /* find an estimate of how much we need for this var */
769 i
= sysctl(oid
, nlen
, 0, &j
, 0, 0);
770 j
+= j
; /* we want to be sure :-) */
777 i
= sysctl(oid
, nlen
, val
, &len
, 0, 0);
784 fwrite(val
, 1, len
, stdout
);
790 oidfmt(oid
, nlen
, fmt
, &kind
);
795 printf("%s%s", name
, sep
);
796 nul
= memchr(p
, '\0', len
);
797 fwrite(p
, nul
== NULL
? (int)len
: nul
- p
, 1, stdout
);
802 printf("%s%s", name
, sep
);
805 while (len
>= sizeof(int)) {
807 printf("%s%u", spacer
, *(unsigned int *)p
);
808 else if (*fmt
== 'K' && *(int *)p
>= 0)
809 printf("%s%.1fC", spacer
, (*(int *)p
- 2732) / 10.0);
811 printf("%s%d", spacer
, *(int *)p
);
820 printf("%s%s", name
, sep
);
823 while (len
>= sizeof(long)) {
825 printf("%s%lu", spacer
, *(unsigned long *)p
);
827 printf("%s%ld", spacer
, *(long *)p
);
836 printf("%s%s", name
, sep
);
837 printf("%p", *(void **)p
);
842 printf("%s%s", name
, sep
);
845 while (len
>= sizeof(quad_t
)) {
848 spacer
, (uintmax_t)*(u_quad_t
*)p
);
851 spacer
, (intmax_t)*(quad_t
*)p
);
854 len
-= sizeof(int64_t);
855 p
+= sizeof(int64_t);
861 if (!oflag
&& !xflag
) {
863 if (strcmp(fmt
, "S,clockinfo") == 0)
865 else if (strcmp(fmt
, "S,timespec") == 0)
867 else if (strcmp(fmt
, "S,timeval") == 0)
869 else if (strcmp(fmt
, "S,loadavg") == 0)
871 else if (strcmp(fmt
, "S,sensor") == 0)
874 else if (strcmp(fmt
, "S,efi_map_header") == 0)
877 else if (strcmp(fmt
, "T,dev_t") == 0)
879 else if (strcmp(fmt
, "T,udev_t") == 0)
885 printf("%s%s", name
, sep
);
886 error
= (*func
)(len
, p
);
892 if (!oflag
&& !xflag
) {
897 printf("%s%s", name
, sep
);
898 printf("Format:%s Length:%zu Dump:0x", fmt
, len
);
899 while (len
-- && (xflag
|| p
< val
+ 16))
900 printf("%02x", *p
++);
901 if (!xflag
&& len
> 16)
913 sysctl_all(int *oid
, size_t len
)
915 int name1
[22], name2
[22];
923 memcpy(name1
+2, oid
, len
* sizeof(int));
931 retval
= sysctl(name1
, l1
, name2
, &l2
, 0, 0);
936 err(1, "sysctl(getnext) %d %zu", retval
, l2
);
944 for (i
= 0; i
< len
; i
++)
945 if (name2
[i
] != oid
[i
])
948 retval
= show_var(name2
, l2
);
949 if (retval
== 0 && !bflag
)
952 memcpy(name1
+2, name2
, l2
* sizeof(int));