Use err(3). Change err(-1,... to err(1,...

This commit is contained in:
Philippe Charnier 1997-10-20 12:53:54 +00:00
parent 0fc00685c3
commit d9b1bc7779
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=30602
6 changed files with 172 additions and 164 deletions

View File

@ -32,23 +32,29 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1993\n\ "@(#) Copyright (c) 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
#if 0
static char sccsid[] = "@(#)pathconf.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)pathconf.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/unistd.h> #include <sys/unistd.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#define PC_NAMES { \ #define PC_NAMES { \
{ 0, 0 }, \ { 0, 0 }, \
@ -75,13 +81,16 @@ struct list pclist = { pcnames, PC_MAXID };
int Aflag, aflag, nflag, wflag, stdinflag; int Aflag, aflag, nflag, wflag, stdinflag;
int findname __P((char *, char *, char**, struct list *));
void listall __P((char *, struct list *));
void parse __P((char *, char *, int));
static void usage __P((void));
int int
main(argc, argv) main(argc, argv)
int argc; int argc;
char *argv[]; char *argv[];
{ {
extern char *optarg;
extern int optind;
char *path; char *path;
int ch; int ch;
@ -127,6 +136,7 @@ main(argc, argv)
/* /*
* List all variables known to the system. * List all variables known to the system.
*/ */
void
listall(path, lp) listall(path, lp)
char *path; char *path;
struct list *lp; struct list *lp;
@ -146,6 +156,7 @@ listall(path, lp)
* Parse a name into an index. * Parse a name into an index.
* Lookup and print out the attribute if it exists. * Lookup and print out the attribute if it exists.
*/ */
void
parse(pathname, string, flags) parse(pathname, string, flags)
char *pathname; char *pathname;
char *string; char *string;
@ -159,7 +170,7 @@ parse(pathname, string, flags)
if ((indx = findname(string, "top", &bufp, &pclist)) == -1) if ((indx = findname(string, "top", &bufp, &pclist)) == -1)
return; return;
if (bufp) { if (bufp) {
fprintf(stderr, "name %s in %s is unknown\n", *bufp, string); warnx("name %s in %s is unknown", *bufp, string);
return; return;
} }
if (stdinflag) if (stdinflag)
@ -171,18 +182,16 @@ parse(pathname, string, flags)
return; return;
switch (errno) { switch (errno) {
case EOPNOTSUPP: case EOPNOTSUPP:
fprintf(stderr, "%s: value is not available\n", string); warnx("%s: value is not available", string);
return; return;
case ENOTDIR: case ENOTDIR:
fprintf(stderr, "%s: specification is incomplete\n", warnx("%s: specification is incomplete", string);
string);
return; return;
case ENOMEM: case ENOMEM:
fprintf(stderr, "%s: type is unknown to this program\n", warnx("%s: type is unknown to this program", string);
string);
return; return;
default: default:
perror(string); warn("%s", string);
return; return;
} }
} }
@ -194,6 +203,7 @@ parse(pathname, string, flags)
/* /*
* Scan a list of names searching for a particular name. * Scan a list of names searching for a particular name.
*/ */
int
findname(string, level, bufp, namelist) findname(string, level, bufp, namelist)
char *string; char *string;
char *level; char *level;
@ -204,7 +214,7 @@ findname(string, level, bufp, namelist)
int i; int i;
if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
fprintf(stderr, "%s: incomplete specification\n", string); warnx("%s: incomplete specification", string);
return (-1); return (-1);
} }
for (i = 0; i < namelist->size; i++) for (i = 0; i < namelist->size; i++)
@ -212,18 +222,19 @@ findname(string, level, bufp, namelist)
strcmp(name, namelist->list[i].ctl_name) == 0) strcmp(name, namelist->list[i].ctl_name) == 0)
break; break;
if (i == namelist->size) { if (i == namelist->size) {
fprintf(stderr, "%s level name %s in %s is invalid\n", warnx("%s level name %s in %s is invalid", level, name, string);
level, name, string);
return (-1); return (-1);
} }
return (i); return (i);
} }
static void
usage() usage()
{ {
(void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n", (void)fprintf(stderr, "%s\n%s\n%s\n",
"pathname [-n] variable ...", "usage: pathname [-n] variable ...",
"pathname [-n] -a", "pathname [-n] -A"); " pathname [-n] -a",
" pathname [-n] -A");
exit(1); exit(1);
} }

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93 .\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93
.\" $Id: sysctl.8,v 1.12 1997/02/22 16:13:58 peter Exp $ .\" $Id: sysctl.8,v 1.13 1997/08/30 02:28:00 kato Exp $
.\" .\"
.Dd September 23, 1994 .Dd September 23, 1994
.Dt SYSCTL 8 .Dt SYSCTL 8
@ -51,59 +51,55 @@
.Fl aAX .Fl aAX
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm sysctl .Nm
utility retrieves kernel state and allows processes with utility retrieves kernel state and allows processes with
appropriate privilege to set kernel state. appropriate privilege to set kernel state.
The state to be retrieved or set is described using a The state to be retrieved or set is described using a
``Management Information Base'' (``MIB'') style name, ``Management Information Base'' (``MIB'') style name,
described as a dotted set of components. described as a dotted set of components.
.Pp .Pp
The The following options are available:
.Fl a .Bl -tag -width indent
flag can be used to list all the currently available string or integer values. .It Fl a
.Pp List all the currently available string or integer values.
The .It Fl A
.Fl A List all the known MIB names including opaques.
flag will list all the known MIB names including opaques.
Those with string or integer values will be printed as with the Those with string or integer values will be printed as with the
.Fl a .Fl a
flag; for the opaque values, flag; for the opaque values,
information about the format and the length is printed in addition the first information about the format and the length is printed in addition the first
few bytes is dumped in hex. few bytes is dumped in hex.
.Pp .It Fl X
The Same as
.Fl X
flag is the same as
.Fl A .Fl A
except the entire value of opaque variables is hexdumped. except the entire value of opaque variables is hexdumped.
.Pp .It Fl n
The Specify that the printing of the field name should be
.Fl n
flag specifies that the printing of the field name should be
suppressed and that only its value should be output. suppressed and that only its value should be output.
This flag is useful for setting shell variables. This flag is useful for setting shell variables.
For example, to save the pagesize in variable psize, use: For example, to save the pagesize in variable psize, use:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
set psize=`sysctl -n hw.pagesize` set psize=`sysctl -n hw.pagesize`
.Ed .Ed
.Pp .It Fl b
The Force the value of the variable(s) to be output in raw, binary
.Fl b
flag forces the value of the variable(s) to be output in raw, binary
format. No names are printed and no terminating newlines are output. format. No names are printed and no terminating newlines are output.
This is mostly useful with a single variable. This is mostly useful with a single variable.
.Pp .It Fl w Ar name=value ...
If just a MIB style name is given, Set the MIB
.Ar name
to the new
.Ar value .
If just a MIB style
.Ar name
is given,
the corresponding value is retrieved. the corresponding value is retrieved.
If a value is to be set, the .El
.Fl w
flag must be specified and the MIB name followed
by an equal sign and the new value to be used.
.Pp .Pp
The information available from The information available from
.Nm sysctl .Nm
consists of integers, strings, and opaques. consists of integers, strings, and opaques.
.Nm sysctl .Nm Sysctl
only knows about a couple of opaque types, and will resort to hexdumps only knows about a couple of opaque types, and will resort to hexdumps
for the rest. for the rest.
The opaque information is much more useful if retrieved by special The opaque information is much more useful if retrieved by special
@ -179,7 +175,6 @@ privilege can change the value.
.It user.tzname_max integer no .It user.tzname_max integer no
.El .El
.Sh EXAMPLES .Sh EXAMPLES
.Pp
For example, to retrieve the maximum number of processes allowed For example, to retrieve the maximum number of processes allowed
in the system, one would use the follow request: in the system, one would use the follow request:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
@ -197,7 +192,7 @@ Information about the system clock rate may be obtained with:
sysctl kern.clockrate sysctl kern.clockrate
.Ed .Ed
.Pp .Pp
Information about the load average history may be obtained with Information about the load average history may be obtained with:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
sysctl vm.loadavg sysctl vm.loadavg
.Ed .Ed
@ -227,17 +222,17 @@ definitions for fourth level UDP identifiers
.Sh SEE ALSO .Sh SEE ALSO
.Xr sysctl 3 .Xr sysctl 3
.Sh BUGS .Sh BUGS
.Nm sysctl .Nm Sysctl
presently exploits an undocumented interface to the kernel presently exploits an undocumented interface to the kernel
sysctl facility to traverse the sysctl tree and to retrieve format sysctl facility to traverse the sysctl tree and to retrieve format
and name information. and name information.
This correct interface is being thought about for the time being. This correct interface is being thought about for the time being.
.Sh HISTORY .Sh HISTORY
.Nm sysctl .Nm Sysctl
first appeared in first appeared in
.Bx 4.4 . .Bx 4.4 .
.Pp .Pp
In In
.Fx 2.2 .Fx 2.2 ,
.Nm sysctl .Nm
was significantly remodeled. was significantly remodeled.

View File

@ -32,15 +32,17 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1993\n\ "@(#) Copyright (c) 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
/*static char sccsid[] = "From: @(#)sysctl.c 8.1 (Berkeley) 6/6/93"; */ #if 0
static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] = static const char rcsid[] =
"$Id: sysctl.c,v 1.13 1997/03/31 05:11:25 imp Exp $"; "$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
@ -48,12 +50,13 @@ static const char rcsid[] =
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <ctype.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <unistd.h>
#include <err.h>
static int Aflag, aflag, nflag, wflag, Xflag, bflag; static int Aflag, aflag, nflag, wflag, Xflag, bflag;
@ -67,20 +70,17 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "usage:\n%s", (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
"\tsysctl [-bnX] variable ...\n" "usage: sysctl [-bnX] variable ...",
"\tsysctl [-bnX] -w variable=value ...\n" " sysctl [-bnX] -w variable=value ...",
"\tsysctl [-bnX] -a\n" " sysctl [-bnX] -a",
"\tsysctl [-bnX] -A\n" " sysctl [-bnX] -A");
);
exit(1); exit(1);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
extern char *optarg;
extern int optind;
int ch; int ch;
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
@ -127,10 +127,8 @@ parse(char *string)
bufp = buf; bufp = buf;
snprintf(buf, BUFSIZ, "%s", string); snprintf(buf, BUFSIZ, "%s", string);
if ((cp = strchr(string, '=')) != NULL) { if ((cp = strchr(string, '=')) != NULL) {
if (!wflag) { if (!wflag)
fprintf(stderr, "Must specify -w to set variables\n"); errx(2, "must specify -w to set variables");
exit(2);
}
*strchr(buf, '=') = '\0'; *strchr(buf, '=') = '\0';
*cp++ = '\0'; *cp++ = '\0';
while (isspace(*cp)) while (isspace(*cp))
@ -144,10 +142,10 @@ parse(char *string)
len = name2oid(bufp, mib); len = name2oid(bufp, mib);
if (len < 0) if (len < 0)
errx(1, "Unknown oid '%s'", bufp); errx(1, "unknown oid '%s'", bufp);
if (oidfmt(mib, len, 0, &kind)) if (oidfmt(mib, len, 0, &kind))
err(1, "Couldn't find format of oid '%s'", bufp); err(1, "couldn't find format of oid '%s'", bufp);
if (!wflag) { if (!wflag) {
if ((kind & CTLTYPE) == CTLTYPE_NODE) { if ((kind & CTLTYPE) == CTLTYPE_NODE) {
@ -190,16 +188,16 @@ parse(char *string)
putchar('\n'); putchar('\n');
switch (errno) { switch (errno) {
case EOPNOTSUPP: case EOPNOTSUPP:
errx(1, "%s: value is not available\n", errx(1, "%s: value is not available",
string); string);
case ENOTDIR: case ENOTDIR:
errx(1, "%s: specification is incomplete\n", errx(1, "%s: specification is incomplete",
string); string);
case ENOMEM: case ENOMEM:
errx(1, "%s: type is unknown to this program\n", errx(1, "%s: type is unknown to this program",
string); string);
default: default:
perror(string); warn("%s", string);
return; return;
} }
} }
@ -221,7 +219,7 @@ S_clockinfo(int l2, void *p)
{ {
struct clockinfo *ci = (struct clockinfo*)p; struct clockinfo *ci = (struct clockinfo*)p;
if (l2 != sizeof *ci) if (l2 != sizeof *ci)
err(-1, "S_clockinfo %d != %d", l2, sizeof *ci); err(1, "S_clockinfo %d != %d", l2, sizeof *ci);
printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }", printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz); ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
return (0); return (0);
@ -233,7 +231,7 @@ S_loadavg(int l2, void *p)
struct loadavg *tv = (struct loadavg*)p; struct loadavg *tv = (struct loadavg*)p;
if (l2 != sizeof *tv) if (l2 != sizeof *tv)
err(-1, "S_loadavg %d != %d", l2, sizeof *tv); err(1, "S_loadavg %d != %d", l2, sizeof *tv);
printf("{ %.2f %.2f %.2f }", printf("{ %.2f %.2f %.2f }",
(double)tv->ldavg[0]/(double)tv->fscale, (double)tv->ldavg[0]/(double)tv->fscale,
@ -249,7 +247,7 @@ S_timeval(int l2, void *p)
char *p1, *p2; char *p1, *p2;
if (l2 != sizeof *tv) if (l2 != sizeof *tv)
err(-1, "S_timeval %d != %d", l2, sizeof *tv); err(1, "S_timeval %d != %d", l2, sizeof *tv);
printf("{ sec = %ld, usec = %ld } ", printf("{ sec = %ld, usec = %ld } ",
tv->tv_sec, tv->tv_usec); tv->tv_sec, tv->tv_usec);
p1 = strdup(ctime(&tv->tv_sec)); p1 = strdup(ctime(&tv->tv_sec));
@ -265,7 +263,7 @@ T_dev_t(int l2, void *p)
{ {
dev_t *d = (dev_t *)p; dev_t *d = (dev_t *)p;
if (l2 != sizeof *d) if (l2 != sizeof *d)
err(-1, "T_dev_T %d != %d", l2, sizeof *d); err(1, "T_dev_T %d != %d", l2, sizeof *d);
printf("{ major = %d, minor = %d }", printf("{ major = %d, minor = %d }",
major(*d), minor(*d)); major(*d), minor(*d));
return (0); return (0);
@ -311,7 +309,7 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind)
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, len + 2, buf, &j, 0, 0); i = sysctl(qoid, len + 2, buf, &j, 0, 0);
if (i) if (i)
err(-1, "sysctl fmt %d %d %d", i, j, errno); err(1, "sysctl fmt %d %d %d", i, j, errno);
if (kind) if (kind)
*kind = *(u_int *)buf; *kind = *(u_int *)buf;
@ -362,7 +360,7 @@ show_var(int *oid, int nlen)
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
if (i || !j) if (i || !j)
err(-1, "sysctl fmt %d %d %d", i, j, errno); err(1, "sysctl fmt %d %d %d", i, j, errno);
kind = *(u_int *)buf; kind = *(u_int *)buf;
@ -372,7 +370,7 @@ show_var(int *oid, int nlen)
j = sizeof name; j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0); i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j) if (i || !j)
err(-1, "sysctl name %d %d %d", i, j, errno); err(1, "sysctl name %d %d %d", i, j, errno);
p = val; p = val;
switch (*fmt) { switch (*fmt) {
@ -442,7 +440,7 @@ sysctl_all (int *oid, int len)
if (errno == ENOENT) if (errno == ENOENT)
return 0; return 0;
else else
err(-1, "sysctl(getnext) %d %d", j, l2); err(1, "sysctl(getnext) %d %d", j, l2);
l2 /= sizeof (int); l2 /= sizeof (int);

View File

@ -32,23 +32,29 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1993\n\ "@(#) Copyright (c) 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
#if 0
static char sccsid[] = "@(#)pathconf.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)pathconf.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/unistd.h> #include <sys/unistd.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#define PC_NAMES { \ #define PC_NAMES { \
{ 0, 0 }, \ { 0, 0 }, \
@ -75,13 +81,16 @@ struct list pclist = { pcnames, PC_MAXID };
int Aflag, aflag, nflag, wflag, stdinflag; int Aflag, aflag, nflag, wflag, stdinflag;
int findname __P((char *, char *, char**, struct list *));
void listall __P((char *, struct list *));
void parse __P((char *, char *, int));
static void usage __P((void));
int int
main(argc, argv) main(argc, argv)
int argc; int argc;
char *argv[]; char *argv[];
{ {
extern char *optarg;
extern int optind;
char *path; char *path;
int ch; int ch;
@ -127,6 +136,7 @@ main(argc, argv)
/* /*
* List all variables known to the system. * List all variables known to the system.
*/ */
void
listall(path, lp) listall(path, lp)
char *path; char *path;
struct list *lp; struct list *lp;
@ -146,6 +156,7 @@ listall(path, lp)
* Parse a name into an index. * Parse a name into an index.
* Lookup and print out the attribute if it exists. * Lookup and print out the attribute if it exists.
*/ */
void
parse(pathname, string, flags) parse(pathname, string, flags)
char *pathname; char *pathname;
char *string; char *string;
@ -159,7 +170,7 @@ parse(pathname, string, flags)
if ((indx = findname(string, "top", &bufp, &pclist)) == -1) if ((indx = findname(string, "top", &bufp, &pclist)) == -1)
return; return;
if (bufp) { if (bufp) {
fprintf(stderr, "name %s in %s is unknown\n", *bufp, string); warnx("name %s in %s is unknown", *bufp, string);
return; return;
} }
if (stdinflag) if (stdinflag)
@ -171,18 +182,16 @@ parse(pathname, string, flags)
return; return;
switch (errno) { switch (errno) {
case EOPNOTSUPP: case EOPNOTSUPP:
fprintf(stderr, "%s: value is not available\n", string); warnx("%s: value is not available", string);
return; return;
case ENOTDIR: case ENOTDIR:
fprintf(stderr, "%s: specification is incomplete\n", warnx("%s: specification is incomplete", string);
string);
return; return;
case ENOMEM: case ENOMEM:
fprintf(stderr, "%s: type is unknown to this program\n", warnx("%s: type is unknown to this program", string);
string);
return; return;
default: default:
perror(string); warn("%s", string);
return; return;
} }
} }
@ -194,6 +203,7 @@ parse(pathname, string, flags)
/* /*
* Scan a list of names searching for a particular name. * Scan a list of names searching for a particular name.
*/ */
int
findname(string, level, bufp, namelist) findname(string, level, bufp, namelist)
char *string; char *string;
char *level; char *level;
@ -204,7 +214,7 @@ findname(string, level, bufp, namelist)
int i; int i;
if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
fprintf(stderr, "%s: incomplete specification\n", string); warnx("%s: incomplete specification", string);
return (-1); return (-1);
} }
for (i = 0; i < namelist->size; i++) for (i = 0; i < namelist->size; i++)
@ -212,18 +222,19 @@ findname(string, level, bufp, namelist)
strcmp(name, namelist->list[i].ctl_name) == 0) strcmp(name, namelist->list[i].ctl_name) == 0)
break; break;
if (i == namelist->size) { if (i == namelist->size) {
fprintf(stderr, "%s level name %s in %s is invalid\n", warnx("%s level name %s in %s is invalid", level, name, string);
level, name, string);
return (-1); return (-1);
} }
return (i); return (i);
} }
static void
usage() usage()
{ {
(void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n", (void)fprintf(stderr, "%s\n%s\n%s\n",
"pathname [-n] variable ...", "usage: pathname [-n] variable ...",
"pathname [-n] -a", "pathname [-n] -A"); " pathname [-n] -a",
" pathname [-n] -A");
exit(1); exit(1);
} }

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93 .\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93
.\" $Id: sysctl.8,v 1.12 1997/02/22 16:13:58 peter Exp $ .\" $Id: sysctl.8,v 1.13 1997/08/30 02:28:00 kato Exp $
.\" .\"
.Dd September 23, 1994 .Dd September 23, 1994
.Dt SYSCTL 8 .Dt SYSCTL 8
@ -51,59 +51,55 @@
.Fl aAX .Fl aAX
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm sysctl .Nm
utility retrieves kernel state and allows processes with utility retrieves kernel state and allows processes with
appropriate privilege to set kernel state. appropriate privilege to set kernel state.
The state to be retrieved or set is described using a The state to be retrieved or set is described using a
``Management Information Base'' (``MIB'') style name, ``Management Information Base'' (``MIB'') style name,
described as a dotted set of components. described as a dotted set of components.
.Pp .Pp
The The following options are available:
.Fl a .Bl -tag -width indent
flag can be used to list all the currently available string or integer values. .It Fl a
.Pp List all the currently available string or integer values.
The .It Fl A
.Fl A List all the known MIB names including opaques.
flag will list all the known MIB names including opaques.
Those with string or integer values will be printed as with the Those with string or integer values will be printed as with the
.Fl a .Fl a
flag; for the opaque values, flag; for the opaque values,
information about the format and the length is printed in addition the first information about the format and the length is printed in addition the first
few bytes is dumped in hex. few bytes is dumped in hex.
.Pp .It Fl X
The Same as
.Fl X
flag is the same as
.Fl A .Fl A
except the entire value of opaque variables is hexdumped. except the entire value of opaque variables is hexdumped.
.Pp .It Fl n
The Specify that the printing of the field name should be
.Fl n
flag specifies that the printing of the field name should be
suppressed and that only its value should be output. suppressed and that only its value should be output.
This flag is useful for setting shell variables. This flag is useful for setting shell variables.
For example, to save the pagesize in variable psize, use: For example, to save the pagesize in variable psize, use:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
set psize=`sysctl -n hw.pagesize` set psize=`sysctl -n hw.pagesize`
.Ed .Ed
.Pp .It Fl b
The Force the value of the variable(s) to be output in raw, binary
.Fl b
flag forces the value of the variable(s) to be output in raw, binary
format. No names are printed and no terminating newlines are output. format. No names are printed and no terminating newlines are output.
This is mostly useful with a single variable. This is mostly useful with a single variable.
.Pp .It Fl w Ar name=value ...
If just a MIB style name is given, Set the MIB
.Ar name
to the new
.Ar value .
If just a MIB style
.Ar name
is given,
the corresponding value is retrieved. the corresponding value is retrieved.
If a value is to be set, the .El
.Fl w
flag must be specified and the MIB name followed
by an equal sign and the new value to be used.
.Pp .Pp
The information available from The information available from
.Nm sysctl .Nm
consists of integers, strings, and opaques. consists of integers, strings, and opaques.
.Nm sysctl .Nm Sysctl
only knows about a couple of opaque types, and will resort to hexdumps only knows about a couple of opaque types, and will resort to hexdumps
for the rest. for the rest.
The opaque information is much more useful if retrieved by special The opaque information is much more useful if retrieved by special
@ -179,7 +175,6 @@ privilege can change the value.
.It user.tzname_max integer no .It user.tzname_max integer no
.El .El
.Sh EXAMPLES .Sh EXAMPLES
.Pp
For example, to retrieve the maximum number of processes allowed For example, to retrieve the maximum number of processes allowed
in the system, one would use the follow request: in the system, one would use the follow request:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
@ -197,7 +192,7 @@ Information about the system clock rate may be obtained with:
sysctl kern.clockrate sysctl kern.clockrate
.Ed .Ed
.Pp .Pp
Information about the load average history may be obtained with Information about the load average history may be obtained with:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
sysctl vm.loadavg sysctl vm.loadavg
.Ed .Ed
@ -227,17 +222,17 @@ definitions for fourth level UDP identifiers
.Sh SEE ALSO .Sh SEE ALSO
.Xr sysctl 3 .Xr sysctl 3
.Sh BUGS .Sh BUGS
.Nm sysctl .Nm Sysctl
presently exploits an undocumented interface to the kernel presently exploits an undocumented interface to the kernel
sysctl facility to traverse the sysctl tree and to retrieve format sysctl facility to traverse the sysctl tree and to retrieve format
and name information. and name information.
This correct interface is being thought about for the time being. This correct interface is being thought about for the time being.
.Sh HISTORY .Sh HISTORY
.Nm sysctl .Nm Sysctl
first appeared in first appeared in
.Bx 4.4 . .Bx 4.4 .
.Pp .Pp
In In
.Fx 2.2 .Fx 2.2 ,
.Nm sysctl .Nm
was significantly remodeled. was significantly remodeled.

View File

@ -32,15 +32,17 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1993\n\ "@(#) Copyright (c) 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
/*static char sccsid[] = "From: @(#)sysctl.c 8.1 (Berkeley) 6/6/93"; */ #if 0
static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] = static const char rcsid[] =
"$Id: sysctl.c,v 1.13 1997/03/31 05:11:25 imp Exp $"; "$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
@ -48,12 +50,13 @@ static const char rcsid[] =
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <ctype.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <unistd.h>
#include <err.h>
static int Aflag, aflag, nflag, wflag, Xflag, bflag; static int Aflag, aflag, nflag, wflag, Xflag, bflag;
@ -67,20 +70,17 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "usage:\n%s", (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
"\tsysctl [-bnX] variable ...\n" "usage: sysctl [-bnX] variable ...",
"\tsysctl [-bnX] -w variable=value ...\n" " sysctl [-bnX] -w variable=value ...",
"\tsysctl [-bnX] -a\n" " sysctl [-bnX] -a",
"\tsysctl [-bnX] -A\n" " sysctl [-bnX] -A");
);
exit(1); exit(1);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
extern char *optarg;
extern int optind;
int ch; int ch;
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
@ -127,10 +127,8 @@ parse(char *string)
bufp = buf; bufp = buf;
snprintf(buf, BUFSIZ, "%s", string); snprintf(buf, BUFSIZ, "%s", string);
if ((cp = strchr(string, '=')) != NULL) { if ((cp = strchr(string, '=')) != NULL) {
if (!wflag) { if (!wflag)
fprintf(stderr, "Must specify -w to set variables\n"); errx(2, "must specify -w to set variables");
exit(2);
}
*strchr(buf, '=') = '\0'; *strchr(buf, '=') = '\0';
*cp++ = '\0'; *cp++ = '\0';
while (isspace(*cp)) while (isspace(*cp))
@ -144,10 +142,10 @@ parse(char *string)
len = name2oid(bufp, mib); len = name2oid(bufp, mib);
if (len < 0) if (len < 0)
errx(1, "Unknown oid '%s'", bufp); errx(1, "unknown oid '%s'", bufp);
if (oidfmt(mib, len, 0, &kind)) if (oidfmt(mib, len, 0, &kind))
err(1, "Couldn't find format of oid '%s'", bufp); err(1, "couldn't find format of oid '%s'", bufp);
if (!wflag) { if (!wflag) {
if ((kind & CTLTYPE) == CTLTYPE_NODE) { if ((kind & CTLTYPE) == CTLTYPE_NODE) {
@ -190,16 +188,16 @@ parse(char *string)
putchar('\n'); putchar('\n');
switch (errno) { switch (errno) {
case EOPNOTSUPP: case EOPNOTSUPP:
errx(1, "%s: value is not available\n", errx(1, "%s: value is not available",
string); string);
case ENOTDIR: case ENOTDIR:
errx(1, "%s: specification is incomplete\n", errx(1, "%s: specification is incomplete",
string); string);
case ENOMEM: case ENOMEM:
errx(1, "%s: type is unknown to this program\n", errx(1, "%s: type is unknown to this program",
string); string);
default: default:
perror(string); warn("%s", string);
return; return;
} }
} }
@ -221,7 +219,7 @@ S_clockinfo(int l2, void *p)
{ {
struct clockinfo *ci = (struct clockinfo*)p; struct clockinfo *ci = (struct clockinfo*)p;
if (l2 != sizeof *ci) if (l2 != sizeof *ci)
err(-1, "S_clockinfo %d != %d", l2, sizeof *ci); err(1, "S_clockinfo %d != %d", l2, sizeof *ci);
printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }", printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz); ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
return (0); return (0);
@ -233,7 +231,7 @@ S_loadavg(int l2, void *p)
struct loadavg *tv = (struct loadavg*)p; struct loadavg *tv = (struct loadavg*)p;
if (l2 != sizeof *tv) if (l2 != sizeof *tv)
err(-1, "S_loadavg %d != %d", l2, sizeof *tv); err(1, "S_loadavg %d != %d", l2, sizeof *tv);
printf("{ %.2f %.2f %.2f }", printf("{ %.2f %.2f %.2f }",
(double)tv->ldavg[0]/(double)tv->fscale, (double)tv->ldavg[0]/(double)tv->fscale,
@ -249,7 +247,7 @@ S_timeval(int l2, void *p)
char *p1, *p2; char *p1, *p2;
if (l2 != sizeof *tv) if (l2 != sizeof *tv)
err(-1, "S_timeval %d != %d", l2, sizeof *tv); err(1, "S_timeval %d != %d", l2, sizeof *tv);
printf("{ sec = %ld, usec = %ld } ", printf("{ sec = %ld, usec = %ld } ",
tv->tv_sec, tv->tv_usec); tv->tv_sec, tv->tv_usec);
p1 = strdup(ctime(&tv->tv_sec)); p1 = strdup(ctime(&tv->tv_sec));
@ -265,7 +263,7 @@ T_dev_t(int l2, void *p)
{ {
dev_t *d = (dev_t *)p; dev_t *d = (dev_t *)p;
if (l2 != sizeof *d) if (l2 != sizeof *d)
err(-1, "T_dev_T %d != %d", l2, sizeof *d); err(1, "T_dev_T %d != %d", l2, sizeof *d);
printf("{ major = %d, minor = %d }", printf("{ major = %d, minor = %d }",
major(*d), minor(*d)); major(*d), minor(*d));
return (0); return (0);
@ -311,7 +309,7 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind)
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, len + 2, buf, &j, 0, 0); i = sysctl(qoid, len + 2, buf, &j, 0, 0);
if (i) if (i)
err(-1, "sysctl fmt %d %d %d", i, j, errno); err(1, "sysctl fmt %d %d %d", i, j, errno);
if (kind) if (kind)
*kind = *(u_int *)buf; *kind = *(u_int *)buf;
@ -362,7 +360,7 @@ show_var(int *oid, int nlen)
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
if (i || !j) if (i || !j)
err(-1, "sysctl fmt %d %d %d", i, j, errno); err(1, "sysctl fmt %d %d %d", i, j, errno);
kind = *(u_int *)buf; kind = *(u_int *)buf;
@ -372,7 +370,7 @@ show_var(int *oid, int nlen)
j = sizeof name; j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0); i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j) if (i || !j)
err(-1, "sysctl name %d %d %d", i, j, errno); err(1, "sysctl name %d %d %d", i, j, errno);
p = val; p = val;
switch (*fmt) { switch (*fmt) {
@ -442,7 +440,7 @@ sysctl_all (int *oid, int len)
if (errno == ENOENT) if (errno == ENOENT)
return 0; return 0;
else else
err(-1, "sysctl(getnext) %d %d", j, l2); err(1, "sysctl(getnext) %d %d", j, l2);
l2 /= sizeof (int); l2 /= sizeof (int);