Deleted Added
full compact
sysctl.c (26899) sysctl.c (30602)
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
1/*
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
35static const char copyright[] =
36"@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
36"@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41/*static char sccsid[] = "From: @(#)sysctl.c 8.1 (Berkeley) 6/6/93"; */
41#if 0
42static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
43#endif
42static const char rcsid[] =
44static const char rcsid[] =
43 "$Id: sysctl.c,v 1.13 1997/03/31 05:11:25 imp Exp $";
45 "$Id$";
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/resource.h>
50
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/sysctl.h>
51#include <sys/resource.h>
52
53#include <ctype.h>
54#include <err.h>
51#include <errno.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <errno.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
55#include <ctype.h>
56#include <err.h>
59#include <unistd.h>
57
58static int Aflag, aflag, nflag, wflag, Xflag, bflag;
59
60static int oidfmt(int *, int, char *, u_int *);
61static void parse(char *);
62static int show_var(int *, int);
63static int sysctl_all (int *oid, int len);
64static int name2oid(char *, int *);
65
66static void
67usage(void)
68{
69
60
61static int Aflag, aflag, nflag, wflag, Xflag, bflag;
62
63static int oidfmt(int *, int, char *, u_int *);
64static void parse(char *);
65static int show_var(int *, int);
66static int sysctl_all (int *oid, int len);
67static int name2oid(char *, int *);
68
69static void
70usage(void)
71{
72
70 (void)fprintf(stderr, "usage:\n%s",
71 "\tsysctl [-bnX] variable ...\n"
72 "\tsysctl [-bnX] -w variable=value ...\n"
73 "\tsysctl [-bnX] -a\n"
74 "\tsysctl [-bnX] -A\n"
75 );
73 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
74 "usage: sysctl [-bnX] variable ...",
75 " sysctl [-bnX] -w variable=value ...",
76 " sysctl [-bnX] -a",
77 " sysctl [-bnX] -A");
76 exit(1);
77}
78
79int
80main(int argc, char **argv)
81{
78 exit(1);
79}
80
81int
82main(int argc, char **argv)
83{
82 extern char *optarg;
83 extern int optind;
84 int ch;
85 setbuf(stdout,0);
86 setbuf(stderr,0);
87
88 while ((ch = getopt(argc, argv, "AabnwX")) != -1) {
89 switch (ch) {
90 case 'A': Aflag = 1; break;
91 case 'a': aflag = 1; break;

--- 30 unchanged lines hidden (view full) ---

122 quad_t quadval;
123 int mib[CTL_MAXNAME];
124 char *cp, *bufp, buf[BUFSIZ];
125 u_int kind;
126
127 bufp = buf;
128 snprintf(buf, BUFSIZ, "%s", string);
129 if ((cp = strchr(string, '=')) != NULL) {
84 int ch;
85 setbuf(stdout,0);
86 setbuf(stderr,0);
87
88 while ((ch = getopt(argc, argv, "AabnwX")) != -1) {
89 switch (ch) {
90 case 'A': Aflag = 1; break;
91 case 'a': aflag = 1; break;

--- 30 unchanged lines hidden (view full) ---

122 quad_t quadval;
123 int mib[CTL_MAXNAME];
124 char *cp, *bufp, buf[BUFSIZ];
125 u_int kind;
126
127 bufp = buf;
128 snprintf(buf, BUFSIZ, "%s", string);
129 if ((cp = strchr(string, '=')) != NULL) {
130 if (!wflag) {
131 fprintf(stderr, "Must specify -w to set variables\n");
132 exit(2);
133 }
130 if (!wflag)
131 errx(2, "must specify -w to set variables");
134 *strchr(buf, '=') = '\0';
135 *cp++ = '\0';
136 while (isspace(*cp))
137 cp++;
138 newval = cp;
139 newsize = strlen(cp);
140 } else {
141 if (wflag)
142 usage();
143 }
144 len = name2oid(bufp, mib);
145
146 if (len < 0)
132 *strchr(buf, '=') = '\0';
133 *cp++ = '\0';
134 while (isspace(*cp))
135 cp++;
136 newval = cp;
137 newsize = strlen(cp);
138 } else {
139 if (wflag)
140 usage();
141 }
142 len = name2oid(bufp, mib);
143
144 if (len < 0)
147 errx(1, "Unknown oid '%s'", bufp);
145 errx(1, "unknown oid '%s'", bufp);
148
149 if (oidfmt(mib, len, 0, &kind))
146
147 if (oidfmt(mib, len, 0, &kind))
150 err(1, "Couldn't find format of oid '%s'", bufp);
148 err(1, "couldn't find format of oid '%s'", bufp);
151
152 if (!wflag) {
153 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
154 sysctl_all(mib, len);
155 } else {
156 i = show_var(mib, len);
157 if (!i && !bflag)
158 putchar('\n');

--- 26 unchanged lines hidden (view full) ---

185 }
186
187 i = show_var(mib, len);
188 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
189 if (!i && !bflag)
190 putchar('\n');
191 switch (errno) {
192 case EOPNOTSUPP:
149
150 if (!wflag) {
151 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
152 sysctl_all(mib, len);
153 } else {
154 i = show_var(mib, len);
155 if (!i && !bflag)
156 putchar('\n');

--- 26 unchanged lines hidden (view full) ---

183 }
184
185 i = show_var(mib, len);
186 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
187 if (!i && !bflag)
188 putchar('\n');
189 switch (errno) {
190 case EOPNOTSUPP:
193 errx(1, "%s: value is not available\n",
191 errx(1, "%s: value is not available",
194 string);
195 case ENOTDIR:
192 string);
193 case ENOTDIR:
196 errx(1, "%s: specification is incomplete\n",
194 errx(1, "%s: specification is incomplete",
197 string);
198 case ENOMEM:
195 string);
196 case ENOMEM:
199 errx(1, "%s: type is unknown to this program\n",
197 errx(1, "%s: type is unknown to this program",
200 string);
201 default:
198 string);
199 default:
202 perror(string);
200 warn("%s", string);
203 return;
204 }
205 }
206 if (!bflag)
207 printf(" -> ");
208 i = nflag;
209 nflag = 1;
210 j = show_var(mib, len);

--- 5 unchanged lines hidden (view full) ---

216
217/* These functions will dump out various interesting structures. */
218
219static int
220S_clockinfo(int l2, void *p)
221{
222 struct clockinfo *ci = (struct clockinfo*)p;
223 if (l2 != sizeof *ci)
201 return;
202 }
203 }
204 if (!bflag)
205 printf(" -> ");
206 i = nflag;
207 nflag = 1;
208 j = show_var(mib, len);

--- 5 unchanged lines hidden (view full) ---

214
215/* These functions will dump out various interesting structures. */
216
217static int
218S_clockinfo(int l2, void *p)
219{
220 struct clockinfo *ci = (struct clockinfo*)p;
221 if (l2 != sizeof *ci)
224 err(-1, "S_clockinfo %d != %d", l2, sizeof *ci);
222 err(1, "S_clockinfo %d != %d", l2, sizeof *ci);
225 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
226 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
227 return (0);
228}
229
230static int
231S_loadavg(int l2, void *p)
232{
233 struct loadavg *tv = (struct loadavg*)p;
234
235 if (l2 != sizeof *tv)
223 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
224 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
225 return (0);
226}
227
228static int
229S_loadavg(int l2, void *p)
230{
231 struct loadavg *tv = (struct loadavg*)p;
232
233 if (l2 != sizeof *tv)
236 err(-1, "S_loadavg %d != %d", l2, sizeof *tv);
234 err(1, "S_loadavg %d != %d", l2, sizeof *tv);
237
238 printf("{ %.2f %.2f %.2f }",
239 (double)tv->ldavg[0]/(double)tv->fscale,
240 (double)tv->ldavg[1]/(double)tv->fscale,
241 (double)tv->ldavg[2]/(double)tv->fscale);
242 return (0);
243}
244
245static int
246S_timeval(int l2, void *p)
247{
248 struct timeval *tv = (struct timeval*)p;
249 char *p1, *p2;
250
251 if (l2 != sizeof *tv)
235
236 printf("{ %.2f %.2f %.2f }",
237 (double)tv->ldavg[0]/(double)tv->fscale,
238 (double)tv->ldavg[1]/(double)tv->fscale,
239 (double)tv->ldavg[2]/(double)tv->fscale);
240 return (0);
241}
242
243static int
244S_timeval(int l2, void *p)
245{
246 struct timeval *tv = (struct timeval*)p;
247 char *p1, *p2;
248
249 if (l2 != sizeof *tv)
252 err(-1, "S_timeval %d != %d", l2, sizeof *tv);
250 err(1, "S_timeval %d != %d", l2, sizeof *tv);
253 printf("{ sec = %ld, usec = %ld } ",
254 tv->tv_sec, tv->tv_usec);
255 p1 = strdup(ctime(&tv->tv_sec));
256 for (p2=p1; *p2 ; p2++)
257 if (*p2 == '\n')
258 *p2 = '\0';
259 fputs(p1, stdout);
260 return (0);
261}
262
263static int
264T_dev_t(int l2, void *p)
265{
266 dev_t *d = (dev_t *)p;
267 if (l2 != sizeof *d)
251 printf("{ sec = %ld, usec = %ld } ",
252 tv->tv_sec, tv->tv_usec);
253 p1 = strdup(ctime(&tv->tv_sec));
254 for (p2=p1; *p2 ; p2++)
255 if (*p2 == '\n')
256 *p2 = '\0';
257 fputs(p1, stdout);
258 return (0);
259}
260
261static int
262T_dev_t(int l2, void *p)
263{
264 dev_t *d = (dev_t *)p;
265 if (l2 != sizeof *d)
268 err(-1, "T_dev_T %d != %d", l2, sizeof *d);
266 err(1, "T_dev_T %d != %d", l2, sizeof *d);
269 printf("{ major = %d, minor = %d }",
270 major(*d), minor(*d));
271 return (0);
272}
273
274/*
275 * These functions uses a presently undocumented interface to the kernel
276 * to walk the tree and get the type so it can print the value.

--- 29 unchanged lines hidden (view full) ---

306
307 qoid[0] = 0;
308 qoid[1] = 4;
309 memcpy(qoid + 2, oid, len * sizeof(int));
310
311 j = sizeof buf;
312 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
313 if (i)
267 printf("{ major = %d, minor = %d }",
268 major(*d), minor(*d));
269 return (0);
270}
271
272/*
273 * These functions uses a presently undocumented interface to the kernel
274 * to walk the tree and get the type so it can print the value.

--- 29 unchanged lines hidden (view full) ---

304
305 qoid[0] = 0;
306 qoid[1] = 4;
307 memcpy(qoid + 2, oid, len * sizeof(int));
308
309 j = sizeof buf;
310 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
311 if (i)
314 err(-1, "sysctl fmt %d %d %d", i, j, errno);
312 err(1, "sysctl fmt %d %d %d", i, j, errno);
315
316 if (kind)
317 *kind = *(u_int *)buf;
318
319 if (fmt)
320 strcpy(fmt, (char *)(buf + sizeof(u_int)));
321 return 0;
322}

--- 34 unchanged lines hidden (view full) ---

357
358 qoid[0] = 0;
359 qoid[1] = 4;
360 memcpy(qoid + 2, oid, nlen * sizeof(int));
361
362 j = sizeof buf;
363 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
364 if (i || !j)
313
314 if (kind)
315 *kind = *(u_int *)buf;
316
317 if (fmt)
318 strcpy(fmt, (char *)(buf + sizeof(u_int)));
319 return 0;
320}

--- 34 unchanged lines hidden (view full) ---

355
356 qoid[0] = 0;
357 qoid[1] = 4;
358 memcpy(qoid + 2, oid, nlen * sizeof(int));
359
360 j = sizeof buf;
361 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
362 if (i || !j)
365 err(-1, "sysctl fmt %d %d %d", i, j, errno);
363 err(1, "sysctl fmt %d %d %d", i, j, errno);
366
367 kind = *(u_int *)buf;
368
369 fmt = (char *)(buf + sizeof(u_int));
370
371 qoid[1] = 1;
372 j = sizeof name;
373 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
374 if (i || !j)
364
365 kind = *(u_int *)buf;
366
367 fmt = (char *)(buf + sizeof(u_int));
368
369 qoid[1] = 1;
370 j = sizeof name;
371 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
372 if (i || !j)
375 err(-1, "sysctl name %d %d %d", i, j, errno);
373 err(1, "sysctl name %d %d %d", i, j, errno);
376
377 p = val;
378 switch (*fmt) {
379 case 'A':
380 if (!nflag)
381 printf("%s: ", name);
382 printf("%s", p);
383 return (0);

--- 53 unchanged lines hidden (view full) ---

437 }
438 while (1) {
439 l2 = sizeof name2;
440 j = sysctl(name1, l1, name2, &l2, 0, 0);
441 if (j < 0)
442 if (errno == ENOENT)
443 return 0;
444 else
374
375 p = val;
376 switch (*fmt) {
377 case 'A':
378 if (!nflag)
379 printf("%s: ", name);
380 printf("%s", p);
381 return (0);

--- 53 unchanged lines hidden (view full) ---

435 }
436 while (1) {
437 l2 = sizeof name2;
438 j = sysctl(name1, l1, name2, &l2, 0, 0);
439 if (j < 0)
440 if (errno == ENOENT)
441 return 0;
442 else
445 err(-1, "sysctl(getnext) %d %d", j, l2);
443 err(1, "sysctl(getnext) %d %d", j, l2);
446
447 l2 /= sizeof (int);
448
449 if (l2 < len)
450 return 0;
451
452 for (i = 0; i < len; i++)
453 if (name2[i] != oid[i])
454 return 0;
455
456 i = show_var(name2, l2);
457 if (!i && !bflag)
458 putchar('\n');
459
460 memcpy(name1+2, name2, l2*sizeof (int));
461 l1 = 2 + l2;
462 }
463}
444
445 l2 /= sizeof (int);
446
447 if (l2 < len)
448 return 0;
449
450 for (i = 0; i < len; i++)
451 if (name2[i] != oid[i])
452 return 0;
453
454 i = show_var(name2, l2);
455 if (!i && !bflag)
456 putchar('\n');
457
458 memcpy(name1+2, name2, l2*sizeof (int));
459 l1 = 2 + l2;
460 }
461}