* Copyright (c) 1993 The Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1993 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)sysctl.c 5.10 (Berkeley) %G%";
#include <netinet/in_systm.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/ip_var.h>
#include <netinet/udp_var.h>
struct ctlname topname
[] = CTL_NAMES
;
struct ctlname kernname
[] = CTL_KERN_NAMES
;
struct ctlname vmname
[] = CTL_VM_NAMES
;
struct ctlname netname
[] = CTL_NET_NAMES
;
struct ctlname hwname
[] = CTL_HW_NAMES
;
struct ctlname username
[] = CTL_USER_NAMES
;
struct ctlname debugname
[CTL_DEBUG_MAXID
];
struct list toplist
= { topname
, CTL_MAXID
};
struct list secondlevel
[] = {
{ 0, 0 }, /* CTL_UNSPEC */
{ kernname
, KERN_MAXID
}, /* CTL_KERN */
{ vmname
, VM_MAXID
}, /* CTL_VM */
{ netname
, NET_MAXID
}, /* CTL_NET */
{ 0, CTL_DEBUG_MAXID
}, /* CTL_DEBUG */
{ hwname
, HW_MAXID
}, /* CTL_HW */
{ 0, 0 }, /* CTL_MACHDEP */
{ username
, USER_MAXID
}, /* CTL_USER_NAMES */
int Aflag
, aflag
, nflag
, wflag
;
while ((ch
= getopt(argc
, argv
, "Aanw")) != EOF
) {
for (lvl1
= 1; lvl1
< CTL_MAXID
; lvl1
++)
listall(topname
[lvl1
].ctl_name
, &secondlevel
[lvl1
]);
* List all variables known to the system.
cp
= &name
[strlen(name
)];
for (lvl2
= 0; lvl2
< lp
->size
; lvl2
++) {
if (lp
->list
[lvl2
].ctl_name
== 0)
strcpy(cp
, lp
->list
[lvl2
].ctl_name
);
* Parse a name into a MIB entry.
* Lookup and print out the MIB entry if it exists.
* Set a new value if requested.
int indx
, type
, state
, size
, len
;
char *cp
, *bufp
, buf
[BUFSIZ
], strval
[BUFSIZ
];
snprintf(buf
, BUFSIZ
, "%s", string
);
if ((cp
= strchr(string
, '=')) != NULL
) {
fprintf(stderr
, "Must specify -w to set variables\n");
*strchr(buf
, '=') = '\0';
if ((indx
= findname(string
, "top", &bufp
, &toplist
)) == -1)
fprintf(stderr
, "%s: class is not implemented\n",
listall(topname
[indx
].ctl_name
, lp
);
if ((indx
= findname(string
, "second", &bufp
, lp
)) == -1)
type
= lp
->list
[indx
].ctl_type
;
if (sysctl(mib
, 3, &state
, &size
, NULL
, 0) < 0) {
fprintf(stdout
, "%s: ", string
);
"kernel is not compiled for profiling\n");
fprintf(stdout
, "%s: %s\n", string
,
state
== GMON_PROF_OFF
? "off" : "running");
"Use pstat to view %s information\n", string
);
"Use ps to view %s information\n", string
);
if (mib
[1] == VM_LOADAVG
) {
fprintf(stdout
, "%s: ", string
);
fprintf(stdout
, "%.2f %.2f %.2f\n",
loads
[0], loads
[1], loads
[2]);
"Use vmstat or systat to view %s information\n", string
);
len
= sysctl_inet(string
, &bufp
, mib
, flags
, &type
);
fprintf(stderr
, "Use netstat to view %s information\n", string
);
mib
[2] = CTL_DEBUG_VALUE
;
fprintf(stderr
, "Illegal top level value: %d\n", mib
[0]);
fprintf(stderr
, "name %s in %s is unknown\n", *bufp
, string
);
sscanf(newval
, "%qd", &quadval
);
newsize
= sizeof quadval
;
if (sysctl(mib
, len
, buf
, &size
, newsize
? newval
: 0, newsize
) == -1) {
fprintf(stderr
, "%s: value is not available\n", string
);
fprintf(stderr
, "%s: specification is incomplete\n",
fprintf(stderr
, "%s: type is unknown to this program\n",
struct clockinfo
*clkp
= (struct clockinfo
*)buf
;
fprintf(stdout
, "%s: ", string
);
"hz = %d, tick = %d, profhz = %d, stathz = %d\n",
clkp
->hz
, clkp
->tick
, clkp
->profhz
, clkp
->stathz
);
fprintf(stdout
, "%s = ", string
);
fprintf(stdout
, "%d\n", *(int *)buf
);
fprintf(stdout
, "%s: %d -> ", string
,
fprintf(stdout
, "%d\n", *(int *)newval
);
fprintf(stdout
, "%s = ", string
);
fprintf(stdout
, "%s\n", buf
);
fprintf(stdout
, "%s: %s -> ", string
, buf
);
fprintf(stdout
, "%s\n", newval
);
fprintf(stdout
, "%s = ", string
);
fprintf(stdout
, "%qd\n", *(quad_t
*)buf
);
fprintf(stdout
, "%s: %qd -> ", string
,
fprintf(stdout
, "%qd\n", *(quad_t
*)newval
);
fprintf(stderr
, "%s: unknown structure returned\n",
fprintf(stderr
, "%s: unknown type returned\n",
* Initialize the set of debugging names
int mib
[3], size
, loc
, i
;
if (secondlevel
[CTL_DEBUG
].list
!= 0)
secondlevel
[CTL_DEBUG
].list
= debugname
;
for (loc
= 0, i
= 0; i
< CTL_DEBUG_MAXID
; i
++) {
if (sysctl(mib
, 3, &names
[loc
], &size
, NULL
, 0) == -1)
debugname
[i
].ctl_name
= &names
[loc
];
debugname
[i
].ctl_type
= CTLTYPE_INT
;
struct ctlname inetname
[] = CTL_IPPROTO_NAMES
;
struct ctlname ipname
[] = IPCTL_NAMES
;
struct ctlname icmpname
[] = ICMPCTL_NAMES
;
struct ctlname udpname
[] = UDPCTL_NAMES
;
struct list inetlist
= { inetname
, IPPROTO_MAXID
};
struct list inetvars
[] = {
{ ipname
, IPCTL_MAXID
}, /* ip */
{ icmpname
, ICMPCTL_MAXID
}, /* icmp */
{ udpname
, UDPCTL_MAXID
}, /* udp */
* handle internet requests
sysctl_inet(string
, bufpp
, mib
, flags
, typep
)
listall(string
, &inetlist
);
if ((indx
= findname(string
, "third", bufpp
, &inetlist
)) == -1)
if (indx
<= IPPROTO_UDP
&& inetvars
[indx
].list
!= NULL
)
fprintf(stderr
, "%s: no variables defined for this protocol\n",
if ((indx
= findname(string
, "fourth", bufpp
, lp
)) == -1)
*typep
= lp
->list
[indx
].ctl_type
;
* Scan a list of names searching for a particular name.
findname(string
, level
, bufp
, namelist
)
if (namelist
->list
== 0 || (name
= strsep(bufp
, ".")) == NULL
) {
fprintf(stderr
, "%s: incomplete specification\n", string
);
for (i
= 0; i
< namelist
->size
; i
++)
if (namelist
->list
[i
].ctl_name
!= NULL
&&
strcmp(name
, namelist
->list
[i
].ctl_name
) == 0)
if (i
== namelist
->size
) {
fprintf(stderr
, "%s level name %s in %s is invalid\n",
(void)fprintf(stderr
, "usage:\t%s\n\t%s\n\t%s\n\t%s\n",
"sysctl [-n] variable ...", "sysctl [-n] -w variable=value ...",
"sysctl [-n] -a", "sysctl [-n] -A");