Deleted Added
full compact
sysctl.c (244133) sysctl.c (244198)
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

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

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
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

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

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
41 "$FreeBSD: head/sbin/sysctl/sysctl.c 244133 2012-12-12 02:55:40Z alfred $";
41 "$FreeBSD: head/sbin/sysctl/sysctl.c 244198 2012-12-13 23:32:47Z delphij $";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/resource.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/vmmeter.h>
50
51#include <ctype.h>
52#include <err.h>
53#include <errno.h>
54#include <inttypes.h>
55#include <locale.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/resource.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/vmmeter.h>
50
51#include <ctype.h>
52#include <err.h>
53#include <errno.h>
54#include <inttypes.h>
55#include <locale.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <sysexits.h>
59#include <unistd.h>
60
60#include <unistd.h>
61
62static const char *conffile;
63
61static int aflag, bflag, dflag, eflag, hflag, iflag;
64static int aflag, bflag, dflag, eflag, hflag, iflag;
62static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag, warncount;
65static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
63
64static int oidfmt(int *, int, char *, u_int *);
66
67static int oidfmt(int *, int, char *, u_int *);
65static void parse(const char *);
68static int parsefile(const char *);
69static int parse(const char *, int);
66static int show_var(int *, int);
67static int sysctl_all(int *oid, int len);
68static int name2oid(char *, int *);
69
70static int set_IK(const char *, int *);
71
72static void
73usage(void)
74{
75
76 (void)fprintf(stderr, "%s\n%s\n",
70static int show_var(int *, int);
71static int sysctl_all(int *oid, int len);
72static int name2oid(char *, int *);
73
74static int set_IK(const char *, int *);
75
76static void
77usage(void)
78{
79
80 (void)fprintf(stderr, "%s\n%s\n",
77 "usage: sysctl [-bdehiNnoqTWx] name[=value] ...",
81 "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
78 " sysctl [-bdehNnoqTWx] -a");
79 exit(1);
80}
81
82int
83main(int argc, char **argv)
84{
85 int ch;
82 " sysctl [-bdehNnoqTWx] -a");
83 exit(1);
84}
85
86int
87main(int argc, char **argv)
88{
89 int ch;
90 int warncount = 0;
86
87 setlocale(LC_NUMERIC, "");
88 setbuf(stdout,0);
89 setbuf(stderr,0);
90
91
92 setlocale(LC_NUMERIC, "");
93 setbuf(stdout,0);
94 setbuf(stderr,0);
95
91 while ((ch = getopt(argc, argv, "AabdehiNnoqTwWxX")) != -1) {
96 while ((ch = getopt(argc, argv, "Aabdef:hiNnoqTwWxX")) != -1) {
92 switch (ch) {
93 case 'A':
94 /* compatibility */
95 aflag = oflag = 1;
96 break;
97 case 'a':
98 aflag = 1;
99 break;
100 case 'b':
101 bflag = 1;
102 break;
103 case 'd':
104 dflag = 1;
105 break;
106 case 'e':
107 eflag = 1;
108 break;
97 switch (ch) {
98 case 'A':
99 /* compatibility */
100 aflag = oflag = 1;
101 break;
102 case 'a':
103 aflag = 1;
104 break;
105 case 'b':
106 bflag = 1;
107 break;
108 case 'd':
109 dflag = 1;
110 break;
111 case 'e':
112 eflag = 1;
113 break;
114 case 'f':
115 conffile = optarg;
116 break;
109 case 'h':
110 hflag = 1;
111 break;
112 case 'i':
113 iflag = 1;
114 break;
115 case 'N':
116 Nflag = 1;

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

147 }
148 argc -= optind;
149 argv += optind;
150
151 if (Nflag && nflag)
152 usage();
153 if (aflag && argc == 0)
154 exit(sysctl_all(0, 0));
117 case 'h':
118 hflag = 1;
119 break;
120 case 'i':
121 iflag = 1;
122 break;
123 case 'N':
124 Nflag = 1;

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

155 }
156 argc -= optind;
157 argv += optind;
158
159 if (Nflag && nflag)
160 usage();
161 if (aflag && argc == 0)
162 exit(sysctl_all(0, 0));
155 if (argc == 0)
163 if (argc == 0 && conffile == NULL)
156 usage();
157
158 warncount = 0;
164 usage();
165
166 warncount = 0;
167 if (conffile != NULL)
168 warncount += parsefile(conffile);
169
159 while (argc-- > 0)
170 while (argc-- > 0)
160 parse(*argv++);
161 exit(warncount);
171 warncount += parse(*argv++, 0);
172
173 return (warncount);
162}
163
164/*
165 * Parse a name into a MIB entry.
166 * Lookup and print out the MIB entry if it exists.
167 * Set a new value if requested.
168 */
174}
175
176/*
177 * Parse a name into a MIB entry.
178 * Lookup and print out the MIB entry if it exists.
179 * Set a new value if requested.
180 */
169static void
170parse(const char *string)
181static int
182parse(const char *string, int lineno)
171{
172 int len, i, j;
173 void *newval = 0;
174 int intval;
175 unsigned int uintval;
176 long longval;
177 unsigned long ulongval;
178 size_t newsize = 0;
179 int64_t i64val;
180 uint64_t u64val;
181 int mib[CTL_MAXNAME];
183{
184 int len, i, j;
185 void *newval = 0;
186 int intval;
187 unsigned int uintval;
188 long longval;
189 unsigned long ulongval;
190 size_t newsize = 0;
191 int64_t i64val;
192 uint64_t u64val;
193 int mib[CTL_MAXNAME];
182 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
194 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ], line[BUFSIZ];
183 u_int kind;
184
195 u_int kind;
196
197 if (lineno)
198 snprintf(line, sizeof(line), " at line %d", lineno);
199 else
200 line[0] = '\0';
201
185 cp = buf;
202 cp = buf;
186 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
187 errx(1, "oid too long: '%s'", string);
188 bufp = strsep(&cp, "=");
203 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
204 warn("oid too long: '%s'%s", string, line);
205 return (1);
206 }
207 bufp = strsep(&cp, "=:");
189 if (cp != NULL) {
190 /* Tflag just lists tunables, do not allow assignment */
191 if (Tflag || Wflag) {
192 warnx("Can't set variables when using -T or -W");
193 usage();
194 }
195 while (isspace(*cp))
196 cp++;
208 if (cp != NULL) {
209 /* Tflag just lists tunables, do not allow assignment */
210 if (Tflag || Wflag) {
211 warnx("Can't set variables when using -T or -W");
212 usage();
213 }
214 while (isspace(*cp))
215 cp++;
216 /* Strip a pair of " or ' if any. */
217 switch (*cp) {
218 case '\"':
219 case '\'':
220 if (cp[strlen(cp) - 1] == *cp)
221 cp[strlen(cp) - 1] = '\0';
222 cp++;
223 }
197 newval = cp;
198 newsize = strlen(cp);
199 }
200 len = name2oid(bufp, mib);
201
202 if (len < 0) {
203 if (iflag)
224 newval = cp;
225 newsize = strlen(cp);
226 }
227 len = name2oid(bufp, mib);
228
229 if (len < 0) {
230 if (iflag)
204 return;
231 return (0);
205 if (qflag)
232 if (qflag)
206 exit(1);
233 return (1);
234 else {
235 warn("unknown oid '%s'%s", bufp, line);
236 return (1);
237 }
238 }
239
240 if (oidfmt(mib, len, fmt, &kind)) {
241 warn("couldn't find format of oid '%s'%s", bufp, line);
242 if (iflag)
243 return (1);
207 else
244 else
208 errx(1, "unknown oid '%s'", bufp);
245 exit(1);
209 }
210
246 }
247
211 if (oidfmt(mib, len, fmt, &kind))
212 err(1, "couldn't find format of oid '%s'", bufp);
213
214 if (newval == NULL || dflag) {
215 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
216 if (dflag) {
217 i = show_var(mib, len);
218 if (!i && !bflag)
219 putchar('\n');
220 }
221 sysctl_all(mib, len);
222 } else {
223 i = show_var(mib, len);
224 if (!i && !bflag)
225 putchar('\n');
226 }
227 } else {
248 if (newval == NULL || dflag) {
249 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
250 if (dflag) {
251 i = show_var(mib, len);
252 if (!i && !bflag)
253 putchar('\n');
254 }
255 sysctl_all(mib, len);
256 } else {
257 i = show_var(mib, len);
258 if (!i && !bflag)
259 putchar('\n');
260 }
261 } else {
228 if ((kind & CTLTYPE) == CTLTYPE_NODE)
229 errx(1, "oid '%s' isn't a leaf node", bufp);
262 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
263 warn("oid '%s' isn't a leaf node%s", bufp, line);
264 return (1);
265 }
230
231 if (!(kind & CTLFLAG_WR)) {
232 if (kind & CTLFLAG_TUN) {
266
267 if (!(kind & CTLFLAG_WR)) {
268 if (kind & CTLFLAG_TUN) {
233 warnx("oid '%s' is a read only tunable", bufp);
234 errx(1, "Tunable values are set in /boot/loader.conf");
235 } else {
236 errx(1, "oid '%s' is read only", bufp);
237 }
269 warnx("oid '%s' is a read only tunable%p", bufp, line);
270 warnx("Tunable values are set in /boot/loader.conf");
271 } else
272 warnx("oid '%s' is read only%s", bufp, line);
273 return (1);
238 }
239
240 if ((kind & CTLTYPE) == CTLTYPE_INT ||
241 (kind & CTLTYPE) == CTLTYPE_UINT ||
242 (kind & CTLTYPE) == CTLTYPE_LONG ||
243 (kind & CTLTYPE) == CTLTYPE_ULONG ||
244 (kind & CTLTYPE) == CTLTYPE_S64 ||
245 (kind & CTLTYPE) == CTLTYPE_U64) {
274 }
275
276 if ((kind & CTLTYPE) == CTLTYPE_INT ||
277 (kind & CTLTYPE) == CTLTYPE_UINT ||
278 (kind & CTLTYPE) == CTLTYPE_LONG ||
279 (kind & CTLTYPE) == CTLTYPE_ULONG ||
280 (kind & CTLTYPE) == CTLTYPE_S64 ||
281 (kind & CTLTYPE) == CTLTYPE_U64) {
246 if (strlen(newval) == 0)
247 errx(1, "empty numeric value");
282 if (strlen(newval) == 0) {
283 warnx("empty numeric value");
284 return (1);
285 }
248 }
249
250 switch (kind & CTLTYPE) {
251 case CTLTYPE_INT:
252 if (strcmp(fmt, "IK") == 0) {
286 }
287
288 switch (kind & CTLTYPE) {
289 case CTLTYPE_INT:
290 if (strcmp(fmt, "IK") == 0) {
253 if (!set_IK(newval, &intval))
254 errx(1, "invalid value '%s'",
255 (char *)newval);
291 if (!set_IK(newval, &intval)) {
292 warnx("invalid value '%s'%s",
293 (char *)newval, line);
294 return (1);
295 }
256 } else {
257 intval = (int)strtol(newval, &endptr,
258 0);
296 } else {
297 intval = (int)strtol(newval, &endptr,
298 0);
259 if (endptr == newval || *endptr != '\0')
260 errx(1, "invalid integer '%s'",
261 (char *)newval);
299 if (endptr == newval || *endptr != '\0') {
300 warnx("invalid integer '%s'%s",
301 (char *)newval, line);
302 return (1);
303 }
262 }
263 newval = &intval;
264 newsize = sizeof(intval);
265 break;
266 case CTLTYPE_UINT:
267 uintval = (int) strtoul(newval, &endptr, 0);
304 }
305 newval = &intval;
306 newsize = sizeof(intval);
307 break;
308 case CTLTYPE_UINT:
309 uintval = (int) strtoul(newval, &endptr, 0);
268 if (endptr == newval || *endptr != '\0')
269 errx(1, "invalid unsigned integer '%s'",
270 (char *)newval);
310 if (endptr == newval || *endptr != '\0') {
311 warnx("invalid unsigned integer '%s'%s",
312 (char *)newval, line);
313 return (1);
314 }
271 newval = &uintval;
272 newsize = sizeof(uintval);
273 break;
274 case CTLTYPE_LONG:
275 longval = strtol(newval, &endptr, 0);
315 newval = &uintval;
316 newsize = sizeof(uintval);
317 break;
318 case CTLTYPE_LONG:
319 longval = strtol(newval, &endptr, 0);
276 if (endptr == newval || *endptr != '\0')
277 errx(1, "invalid long integer '%s'",
278 (char *)newval);
320 if (endptr == newval || *endptr != '\0') {
321 warnx("invalid long integer '%s'%s",
322 (char *)newval, line);
323 return (1);
324 }
279 newval = &longval;
280 newsize = sizeof(longval);
281 break;
282 case CTLTYPE_ULONG:
283 ulongval = strtoul(newval, &endptr, 0);
325 newval = &longval;
326 newsize = sizeof(longval);
327 break;
328 case CTLTYPE_ULONG:
329 ulongval = strtoul(newval, &endptr, 0);
284 if (endptr == newval || *endptr != '\0')
285 errx(1, "invalid unsigned long integer"
286 " '%s'", (char *)newval);
330 if (endptr == newval || *endptr != '\0') {
331 warnx("invalid unsigned long integer"
332 " '%s'%s", (char *)newval, line);
333 return (1);
334 }
287 newval = &ulongval;
288 newsize = sizeof(ulongval);
289 break;
290 case CTLTYPE_STRING:
291 break;
292 case CTLTYPE_S64:
293 i64val = strtoimax(newval, &endptr, 0);
335 newval = &ulongval;
336 newsize = sizeof(ulongval);
337 break;
338 case CTLTYPE_STRING:
339 break;
340 case CTLTYPE_S64:
341 i64val = strtoimax(newval, &endptr, 0);
294 if (endptr == newval || *endptr != '\0')
295 errx(1, "invalid int64_t '%s'",
296 (char *)newval);
342 if (endptr == newval || *endptr != '\0') {
343 warnx("invalid int64_t '%s'%s",
344 (char *)newval, line);
345 return (1);
346 }
297 newval = &i64val;
298 newsize = sizeof(i64val);
299 break;
300 case CTLTYPE_U64:
301 u64val = strtoumax(newval, &endptr, 0);
347 newval = &i64val;
348 newsize = sizeof(i64val);
349 break;
350 case CTLTYPE_U64:
351 u64val = strtoumax(newval, &endptr, 0);
302 if (endptr == newval || *endptr != '\0')
303 errx(1, "invalid uint64_t '%s'",
304 (char *)newval);
352 if (endptr == newval || *endptr != '\0') {
353 warnx("invalid uint64_t '%s'%s",
354 (char *)newval, line);
355 return (1);
356 }
305 newval = &u64val;
306 newsize = sizeof(u64val);
307 break;
308 case CTLTYPE_OPAQUE:
309 /* FALLTHROUGH */
310 default:
357 newval = &u64val;
358 newsize = sizeof(u64val);
359 break;
360 case CTLTYPE_OPAQUE:
361 /* FALLTHROUGH */
362 default:
311 errx(1, "oid '%s' is type %d,"
312 " cannot set that", bufp,
313 kind & CTLTYPE);
363 warnx("oid '%s' is type %d,"
364 " cannot set that%s", bufp,
365 kind & CTLTYPE, line);
366 return (1);
314 }
315
316 i = show_var(mib, len);
317 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
318 if (!i && !bflag)
319 putchar('\n');
320 switch (errno) {
321 case EOPNOTSUPP:
367 }
368
369 i = show_var(mib, len);
370 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
371 if (!i && !bflag)
372 putchar('\n');
373 switch (errno) {
374 case EOPNOTSUPP:
322 errx(1, "%s: value is not available",
323 string);
375 warnx("%s: value is not available%s",
376 string, line);
377 return (1);
324 case ENOTDIR:
378 case ENOTDIR:
325 errx(1, "%s: specification is incomplete",
326 string);
379 warnx("%s: specification is incomplete%s",
380 string, line);
381 return (1);
327 case ENOMEM:
382 case ENOMEM:
328 errx(1, "%s: type is unknown to this program",
329 string);
383 warnx("%s: type is unknown to this program%s",
384 string, line);
385 return (1);
330 default:
386 default:
331 warn("%s", string);
332 warncount++;
333 return;
387 warn("%s%s", string, line);
388 return (1);
334 }
335 }
336 if (!bflag)
337 printf(" -> ");
338 i = nflag;
339 nflag = 1;
340 j = show_var(mib, len);
341 if (!j && !bflag)
342 putchar('\n');
343 nflag = i;
344 }
389 }
390 }
391 if (!bflag)
392 printf(" -> ");
393 i = nflag;
394 nflag = 1;
395 j = show_var(mib, len);
396 if (!j && !bflag)
397 putchar('\n');
398 nflag = i;
399 }
400
401 return (0);
345}
346
402}
403
404static int
405parsefile(const char *filename)
406{
407 FILE *file;
408 char line[BUFSIZ], *p, *pq, *pdq;
409 int warncount = 0, lineno = 0;
410
411 file = fopen(filename, "r");
412 if (file == NULL)
413 err(EX_NOINPUT, "%s", filename);
414 while (fgets(line, sizeof(line), file) != NULL) {
415 lineno++;
416 p = line;
417 pq = strchr(line, '\'');
418 pdq = strchr(line, '\"');
419 /* Replace the first # with \0. */
420 while((p = strchr(p, '#')) != NULL) {
421 if (pq != NULL && p > pq) {
422 if ((p = strchr(pq+1, '\'')) != NULL)
423 *(++p) = '\0';
424 break;
425 } else if (pdq != NULL && p > pdq) {
426 if ((p = strchr(pdq+1, '\"')) != NULL)
427 *(++p) = '\0';
428 break;
429 } else if (p == line || *(p-1) != '\\') {
430 *p = '\0';
431 break;
432 }
433 p++;
434 }
435 /* Trim spaces */
436 p = line + strlen(line) - 1;
437 while (p >= line && isspace((int)*p)) {
438 *p = '\0';
439 p--;
440 }
441 p = line;
442 while (isspace((int)*p))
443 p++;
444 if (*p == '\0')
445 continue;
446 else
447 warncount += parse(p, lineno);
448 }
449 fclose(file);
450
451 return (warncount);
452}
453
347/* These functions will dump out various interesting structures. */
348
349static int
350S_clockinfo(int l2, void *p)
351{
352 struct clockinfo *ci = (struct clockinfo*)p;
353
354 if (l2 != sizeof(*ci)) {

--- 407 unchanged lines hidden ---
454/* These functions will dump out various interesting structures. */
455
456static int
457S_clockinfo(int l2, void *p)
458{
459 struct clockinfo *ci = (struct clockinfo*)p;
460
461 if (l2 != sizeof(*ci)) {

--- 407 unchanged lines hidden ---