Fri Jan 10 18:37:18 2014 UTC ()
use __func__, EXIT_{SUCCESS,FAILURE}


(christos)
diff -r1.150 -r1.151 src/sbin/sysctl/sysctl.c

cvs diff -r1.150 -r1.151 src/sbin/sysctl/sysctl.c (expand / switch to unified diff)

--- src/sbin/sysctl/sysctl.c 2014/01/10 09:38:56 1.150
+++ src/sbin/sysctl/sysctl.c 2014/01/10 18:37:18 1.151
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sysctl.c,v 1.150 2014/01/10 09:38:56 pooka Exp $ */ 1/* $NetBSD: sysctl.c,v 1.151 2014/01/10 18:37:18 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown. 8 * by Andrew Brown.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -58,27 +58,27 @@ @@ -58,27 +58,27 @@
58 * SUCH DAMAGE. 58 * SUCH DAMAGE.
59 */ 59 */
60 60
61#include <sys/cdefs.h> 61#include <sys/cdefs.h>
62#ifndef lint 62#ifndef lint
63__COPYRIGHT("@(#) Copyright (c) 1993\ 63__COPYRIGHT("@(#) Copyright (c) 1993\
64 The Regents of the University of California. All rights reserved."); 64 The Regents of the University of California. All rights reserved.");
65#endif /* not lint */ 65#endif /* not lint */
66 66
67#ifndef lint 67#ifndef lint
68#if 0 68#if 0
69static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93"; 69static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93";
70#else 70#else
71__RCSID("$NetBSD: sysctl.c,v 1.150 2014/01/10 09:38:56 pooka Exp $"); 71__RCSID("$NetBSD: sysctl.c,v 1.151 2014/01/10 18:37:18 christos Exp $");
72#endif 72#endif
73#endif /* not lint */ 73#endif /* not lint */
74 74
75#include <sys/types.h> 75#include <sys/types.h>
76#include <sys/param.h> 76#include <sys/param.h>
77#include <sys/sysctl.h> 77#include <sys/sysctl.h>
78#include <sys/mount.h> 78#include <sys/mount.h>
79#include <sys/resource.h> 79#include <sys/resource.h>
80#include <sys/stat.h> 80#include <sys/stat.h>
81#include <sys/sched.h> 81#include <sys/sched.h>
82#include <sys/socket.h> 82#include <sys/socket.h>
83#include <sys/bitops.h> 83#include <sys/bitops.h>
84#include <netinet/in.h> 84#include <netinet/in.h>
@@ -327,106 +327,106 @@ main(int argc, char *argv[]) @@ -327,106 +327,106 @@ main(int argc, char *argv[])
327 327
328 if (xflag && rflag) 328 if (xflag && rflag)
329 usage(); 329 usage();
330 /* if ((xflag || rflag) && wflag) 330 /* if ((xflag || rflag) && wflag)
331 usage(); */ 331 usage(); */
332 /* if (aflag && (Mflag || qflag)) 332 /* if (aflag && (Mflag || qflag))
333 usage(); */ 333 usage(); */
334 if ((aflag || Aflag) && qflag) 334 if ((aflag || Aflag) && qflag)
335 usage(); 335 usage();
336 if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL) 336 if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL)
337 aflag = 1; 337 aflag = 1;
338 338
339 if (prog_init && prog_init() == -1) 339 if (prog_init && prog_init() == -1)
340 err(1, "prog init failed"); 340 err(EXIT_FAILURE, "prog init failed");
341 341
342 if (Aflag) 342 if (Aflag)
343 warnfp = stdout; 343 warnfp = stdout;
344 stale = req = 0; 344 stale = req = 0;
345 345
346 if ((re = malloc(sizeof(*re) * __arraycount(handlers))) == NULL) 346 if ((re = malloc(sizeof(*re) * __arraycount(handlers))) == NULL)
347 err(1, "malloc regex"); 347 err(EXIT_FAILURE, "malloc regex");
348 348
349 if (aflag) { 349 if (aflag) {
350 print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1, 350 print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1,
351 re, &lastcompiled); 351 re, &lastcompiled);
352 /* if (argc == 0) */ 352 /* if (argc == 0) */
353 return (0); 353 return (0);
354 } 354 }
355 355
356 if (fn) { 356 if (fn) {
357 FILE *fp; 357 FILE *fp;
358 char *l; 358 char *l;
359 359
360 fp = fopen(fn, "r"); 360 fp = fopen(fn, "r");
361 if (fp == NULL) { 361 if (fp == NULL) {
362 err(1, "%s", fn); 362 err(EXIT_FAILURE, "%s", fn);
363 } else { 363 } else {
364 nr = 0; 364 nr = 0;
365 while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL) 365 while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL)
366 { 366 {
367 if (*l) { 367 if (*l) {
368 parse(l, re, &lastcompiled); 368 parse(l, re, &lastcompiled);
369 free(l); 369 free(l);
370 } 370 }
371 } 371 }
372 fclose(fp); 372 fclose(fp);
373 } 373 }
374 return errs ? 1 : 0; 374 return errs ? 1 : 0;
375 } 375 }
376 376
377 if (argc == 0) 377 if (argc == 0)
378 usage(); 378 usage();
379 379
380 while (argc-- > 0) 380 while (argc-- > 0)
381 parse(*argv++, re, &lastcompiled); 381 parse(*argv++, re, &lastcompiled);
382 382
383 return errs ? 1 : 0; 383 return errs ? EXIT_FAILURE : EXIT_SUCCESS;
384} 384}
385 385
386/* 386/*
387 * ******************************************************************** 387 * ********************************************************************
388 * how to find someone special to handle the reading (or maybe even 388 * how to find someone special to handle the reading (or maybe even
389 * writing) of a particular node 389 * writing) of a particular node
390 * ******************************************************************** 390 * ********************************************************************
391 */ 391 */
392static const struct handlespec * 392static const struct handlespec *
393findhandler(const char *s, regex_t *re, size_t *lastcompiled) 393findhandler(const char *s, regex_t *re, size_t *lastcompiled)
394{ 394{
395 const struct handlespec *p; 395 const struct handlespec *p;
396 size_t i, l; 396 size_t i, l;
397 int j; 397 int j;
398 char eb[64]; 398 char eb[64];
399 regmatch_t match; 399 regmatch_t match;
400 400
401 p = &handlers[0]; 401 p = &handlers[0];
402 l = strlen(s); 402 l = strlen(s);
403 for (i = 0; p[i].ps_re != NULL; i++) { 403 for (i = 0; p[i].ps_re != NULL; i++) {
404 if (i >= *lastcompiled) { 404 if (i >= *lastcompiled) {
405 j = regcomp(&re[i], p[i].ps_re, REG_EXTENDED); 405 j = regcomp(&re[i], p[i].ps_re, REG_EXTENDED);
406 if (j != 0) { 406 if (j != 0) {
407 regerror(j, &re[i], eb, sizeof(eb)); 407 regerror(j, &re[i], eb, sizeof(eb));
408 errx(1, "regcomp: %s: %s", p[i].ps_re, eb); 408 errx(EXIT_FAILURE, "regcomp: %s: %s", p[i].ps_re, eb);
409 } 409 }
410 *lastcompiled = i + 1; 410 *lastcompiled = i + 1;
411 } 411 }
412 j = regexec(&re[i], s, 1, &match, 0); 412 j = regexec(&re[i], s, 1, &match, 0);
413 if (j == 0) { 413 if (j == 0) {
414 if (match.rm_so == 0 && match.rm_eo == (int)l) 414 if (match.rm_so == 0 && match.rm_eo == (int)l)
415 return &p[i]; 415 return &p[i];
416 } 416 }
417 else if (j != REG_NOMATCH) { 417 else if (j != REG_NOMATCH) {
418 regerror(j, &re[i], eb, sizeof(eb)); 418 regerror(j, &re[i], eb, sizeof(eb));
419 errx(1, "regexec: %s: %s", p[i].ps_re, eb); 419 errx(EXIT_FAILURE, "regexec: %s: %s", p[i].ps_re, eb);
420 } 420 }
421 } 421 }
422 422
423 return NULL; 423 return NULL;
424} 424}
425 425
426/* 426/*
427 * after sysctlgetmibinfo is done with the name, we convert all 427 * after sysctlgetmibinfo is done with the name, we convert all
428 * separators to / and stuff one at the front if it was missing 428 * separators to / and stuff one at the front if it was missing
429 */ 429 */
430static void 430static void
431canonicalize(const char *i, char *o) 431canonicalize(const char *i, char *o)
432{ 432{
@@ -669,27 +669,27 @@ print_tree(int *name, u_int namelen, str @@ -669,27 +669,27 @@ print_tree(int *name, u_int namelen, str
669 * name specifically. 669 * name specifically.
670 */ 670 */
671 if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) { 671 if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) {
672 *sp = *dp = '\0'; 672 *sp = *dp = '\0';
673 return; 673 return;
674 } 674 }
675 675
676 canonicalize(gsname, canonname); 676 canonicalize(gsname, canonname);
677 p = findhandler(canonname, re, lastcompiled); 677 p = findhandler(canonname, re, lastcompiled);
678 if (type != CTLTYPE_NODE && p != NULL) { 678 if (type != CTLTYPE_NODE && p != NULL) {
679 if (p->ps_p == NULL) { 679 if (p->ps_p == NULL) {
680 sysctlperror("Cannot print `%s': %s\n", gsname,  680 sysctlperror("Cannot print `%s': %s\n", gsname,
681 strerror(EOPNOTSUPP)); 681 strerror(EOPNOTSUPP));
682 exit(1); 682 exit(EXIT_FAILURE);
683 } 683 }
684 (*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type, 684 (*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
685 __UNCONST(p->ps_d)); 685 __UNCONST(p->ps_d));
686 *sp = *dp = '\0'; 686 *sp = *dp = '\0';
687 return; 687 return;
688 } 688 }
689 689
690 if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) { 690 if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
691 rc = prog_sysctl(&name[0], namelen, NULL, &sz, NULL, 0); 691 rc = prog_sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
692 if (rc == -1) { 692 if (rc == -1) {
693 sysctlerror(1); 693 sysctlerror(1);
694 *sp = *dp = '\0'; 694 *sp = *dp = '\0';
695 return; 695 return;
@@ -880,56 +880,56 @@ parse(char *l, regex_t *re, size_t *last @@ -880,56 +880,56 @@ parse(char *l, regex_t *re, size_t *last
880 if (stale) { 880 if (stale) {
881 purge_tree(&my_root); 881 purge_tree(&my_root);
882 stale = 0; 882 stale = 0;
883 } 883 }
884 node = &my_root; 884 node = &my_root;
885 namelen = CTL_MAXNAME; 885 namelen = CTL_MAXNAME;
886 sz = sizeof(gsname); 886 sz = sizeof(gsname);
887 887
888 if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node, 888 if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node,
889 SYSCTL_VERSION) == -1) { 889 SYSCTL_VERSION) == -1) {
890 if (optional) 890 if (optional)
891 return; 891 return;
892 sysctlparseerror(namelen, l); 892 sysctlparseerror(namelen, l);
893 EXIT(1); 893 EXIT(EXIT_FAILURE);
894 } 894 }
895 895
896 type = SYSCTL_TYPE(node->sysctl_flags); 896 type = SYSCTL_TYPE(node->sysctl_flags);
897 897
898 if (value == NULL) { 898 if (value == NULL) {
899 if (dodesc) 899 if (dodesc)
900 dflag = 1; 900 dflag = 1;
901 print_tree(&name[0], namelen, node, type, 0, re, lastcompiled); 901 print_tree(&name[0], namelen, node, type, 0, re, lastcompiled);
902 if (dodesc) 902 if (dodesc)
903 dflag = 0; 903 dflag = 0;
904 gsname[0] = '\0'; 904 gsname[0] = '\0';
905 return; 905 return;
906 } 906 }
907 907
908 if (fn) 908 if (fn)
909 trim_whitespace(value, 1); 909 trim_whitespace(value, 1);
910 910
911 if (!wflag) { 911 if (!wflag) {
912 sysctlperror("Must specify -w to set variables\n"); 912 sysctlperror("Must specify -w to set variables\n");
913 exit(1); 913 exit(EXIT_FAILURE);
914 } 914 }
915 915
916 canonicalize(gsname, canonname); 916 canonicalize(gsname, canonname);
917 if (type != CTLTYPE_NODE && (w = findhandler(canonname, re, 917 if (type != CTLTYPE_NODE && (w = findhandler(canonname, re,
918 lastcompiled)) != NULL) { 918 lastcompiled)) != NULL) {
919 if (w->ps_w == NULL) { 919 if (w->ps_w == NULL) {
920 sysctlperror("Cannot write `%s': %s\n", gsname,  920 sysctlperror("Cannot write `%s': %s\n", gsname,
921 strerror(EOPNOTSUPP)); 921 strerror(EOPNOTSUPP));
922 exit(1); 922 exit(EXIT_FAILURE);
923 } 923 }
924 (*w->ps_w)(gsname, gdname, value, name, namelen, node, type, 924 (*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
925 __UNCONST(w->ps_d)); 925 __UNCONST(w->ps_d));
926 gsname[0] = '\0'; 926 gsname[0] = '\0';
927 return; 927 return;
928 } 928 }
929 929
930 switch (type) { 930 switch (type) {
931 case CTLTYPE_NODE: 931 case CTLTYPE_NODE:
932 /* 932 /*
933 * XXX old behavior is to print. should we error instead? 933 * XXX old behavior is to print. should we error instead?
934 */ 934 */
935 print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1, re, 935 print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1, re,
@@ -977,27 +977,27 @@ static void @@ -977,27 +977,27 @@ static void
977parse_create(char *l) 977parse_create(char *l)
978{ 978{
979 struct sysctlnode node; 979 struct sysctlnode node;
980 size_t sz; 980 size_t sz;
981 char *nname, *key, *value, *data, *addr, *c, *t; 981 char *nname, *key, *value, *data, *addr, *c, *t;
982 int name[CTL_MAXNAME], i, rc, method, flags, rw; 982 int name[CTL_MAXNAME], i, rc, method, flags, rw;
983 u_int namelen, type; 983 u_int namelen, type;
984 u_quad_t uq; 984 u_quad_t uq;
985 quad_t q; 985 quad_t q;
986 bool b; 986 bool b;
987 987
988 if (!wflag) { 988 if (!wflag) {
989 sysctlperror("Must specify -w to create nodes\n"); 989 sysctlperror("Must specify -w to create nodes\n");
990 exit(1); 990 exit(EXIT_FAILURE);
991 } 991 }
992 992
993 /* 993 /*
994 * these are the pieces that make up the description of a new 994 * these are the pieces that make up the description of a new
995 * node 995 * node
996 */ 996 */
997 memset(&node, 0, sizeof(node)); 997 memset(&node, 0, sizeof(node));
998 node.sysctl_num = CTL_CREATE; /* any number is fine */ 998 node.sysctl_num = CTL_CREATE; /* any number is fine */
999 flags = 0; 999 flags = 0;
1000 rw = -1; 1000 rw = -1;
1001 type = 0; 1001 type = 0;
1002 sz = 0; 1002 sz = 0;
1003 data = addr = NULL; 1003 data = addr = NULL;
@@ -1058,122 +1058,122 @@ parse_create(char *l) @@ -1058,122 +1058,122 @@ parse_create(char *l)
1058 * other to make sure it makes some sort of sense. 1058 * other to make sure it makes some sort of sense.
1059 */ 1059 */
1060 if (strcmp(key, "addr") == 0) { 1060 if (strcmp(key, "addr") == 0) {
1061 /* 1061 /*
1062 * we can't check these two. only the kernel 1062 * we can't check these two. only the kernel
1063 * can tell us when it fails to find the name 1063 * can tell us when it fails to find the name
1064 * (or if the address is invalid). 1064 * (or if the address is invalid).
1065 */ 1065 */
1066 if (method != 0) { 1066 if (method != 0) {
1067 sysctlperror( 1067 sysctlperror(
1068 "%s: already have %s for new node\n", 1068 "%s: already have %s for new node\n",
1069 nname, 1069 nname,
1070 method == CTL_CREATE ? "addr" : "symbol"); 1070 method == CTL_CREATE ? "addr" : "symbol");
1071 EXIT(1); 1071 EXIT(EXIT_FAILURE);
1072 } 1072 }
1073 if (value == NULL) { 1073 if (value == NULL) {
1074 sysctlperror("%s: missing value\n", nname); 1074 sysctlperror("%s: missing value\n", nname);
1075 EXIT(1); 1075 EXIT(EXIT_FAILURE);
1076 } 1076 }
1077 errno = 0; 1077 errno = 0;
1078 addr = (void*)strtoul(value, &t, 0); 1078 addr = (void*)strtoul(value, &t, 0);
1079 if (t == value || *t != '\0' || errno != 0) { 1079 if (t == value || *t != '\0' || errno != 0) {
1080 sysctlperror( 1080 sysctlperror(
1081 "%s: '%s' is not a valid address\n", 1081 "%s: '%s' is not a valid address\n",
1082 nname, value); 1082 nname, value);
1083 EXIT(1); 1083 EXIT(EXIT_FAILURE);
1084 } 1084 }
1085 method = CTL_CREATE; 1085 method = CTL_CREATE;
1086 } 1086 }
1087 else if (strcmp(key, "symbol") == 0) { 1087 else if (strcmp(key, "symbol") == 0) {
1088 if (method != 0) { 1088 if (method != 0) {
1089 sysctlperror( 1089 sysctlperror(
1090 "%s: already have %s for new node\n", 1090 "%s: already have %s for new node\n",
1091 nname, 1091 nname,
1092 method == CTL_CREATE ? "addr" : "symbol"); 1092 method == CTL_CREATE ? "addr" : "symbol");
1093 EXIT(1); 1093 EXIT(EXIT_FAILURE);
1094 } 1094 }
1095 addr = value; 1095 addr = value;
1096 method = CTL_CREATESYM; 1096 method = CTL_CREATESYM;
1097 } 1097 }
1098 else if (strcmp(key, "type") == 0) { 1098 else if (strcmp(key, "type") == 0) {
1099 if (value == NULL) { 1099 if (value == NULL) {
1100 sysctlperror("%s: missing value\n", nname); 1100 sysctlperror("%s: missing value\n", nname);
1101 EXIT(1); 1101 EXIT(EXIT_FAILURE);
1102 } 1102 }
1103 if (strcmp(value, "node") == 0) 1103 if (strcmp(value, "node") == 0)
1104 type = CTLTYPE_NODE; 1104 type = CTLTYPE_NODE;
1105 else if (strcmp(value, "int") == 0) { 1105 else if (strcmp(value, "int") == 0) {
1106 sz = sizeof(int); 1106 sz = sizeof(int);
1107 type = CTLTYPE_INT; 1107 type = CTLTYPE_INT;
1108 } 1108 }
1109 else if (strcmp(value, "bool") == 0) { 1109 else if (strcmp(value, "bool") == 0) {
1110 sz = sizeof(bool); 1110 sz = sizeof(bool);
1111 type = CTLTYPE_BOOL; 1111 type = CTLTYPE_BOOL;
1112 } 1112 }
1113 else if (strcmp(value, "string") == 0) 1113 else if (strcmp(value, "string") == 0)
1114 type = CTLTYPE_STRING; 1114 type = CTLTYPE_STRING;
1115 else if (strcmp(value, "quad") == 0) { 1115 else if (strcmp(value, "quad") == 0) {
1116 sz = sizeof(u_quad_t); 1116 sz = sizeof(u_quad_t);
1117 type = CTLTYPE_QUAD; 1117 type = CTLTYPE_QUAD;
1118 } 1118 }
1119 else if (strcmp(value, "struct") == 0) 1119 else if (strcmp(value, "struct") == 0)
1120 type = CTLTYPE_STRUCT; 1120 type = CTLTYPE_STRUCT;
1121 else { 1121 else {
1122 sysctlperror( 1122 sysctlperror(
1123 "%s: '%s' is not a valid type\n", 1123 "%s: '%s' is not a valid type\n",
1124 nname, value); 1124 nname, value);
1125 EXIT(1); 1125 EXIT(EXIT_FAILURE);
1126 } 1126 }
1127 } 1127 }
1128 else if (strcmp(key, "size") == 0) { 1128 else if (strcmp(key, "size") == 0) {
1129 if (value == NULL) { 1129 if (value == NULL) {
1130 sysctlperror("%s: missing value\n", nname); 1130 sysctlperror("%s: missing value\n", nname);
1131 EXIT(1); 1131 EXIT(EXIT_FAILURE);
1132 } 1132 }
1133 errno = 0; 1133 errno = 0;
1134 /* 1134 /*
1135 * yes, i know size_t is not an unsigned long, 1135 * yes, i know size_t is not an unsigned long,
1136 * but we can all agree that it ought to be, 1136 * but we can all agree that it ought to be,
1137 * right? 1137 * right?
1138 */ 1138 */
1139 sz = strtoul(value, &t, 0); 1139 sz = strtoul(value, &t, 0);
1140 if (t == value || *t != '\0' || errno != 0) { 1140 if (t == value || *t != '\0' || errno != 0) {
1141 sysctlperror( 1141 sysctlperror(
1142 "%s: '%s' is not a valid size\n", 1142 "%s: '%s' is not a valid size\n",
1143 nname, value); 1143 nname, value);
1144 EXIT(1); 1144 EXIT(EXIT_FAILURE);
1145 } 1145 }
1146 } 1146 }
1147 else if (strcmp(key, "n") == 0) { 1147 else if (strcmp(key, "n") == 0) {
1148 if (value == NULL) { 1148 if (value == NULL) {
1149 sysctlperror("%s: missing value\n", nname); 1149 sysctlperror("%s: missing value\n", nname);
1150 EXIT(1); 1150 EXIT(EXIT_FAILURE);
1151 } 1151 }
1152 errno = 0; 1152 errno = 0;
1153 q = strtoll(value, &t, 0); 1153 q = strtoll(value, &t, 0);
1154 if (t == value || *t != '\0' || errno != 0 || 1154 if (t == value || *t != '\0' || errno != 0 ||
1155 q < INT_MIN || q > UINT_MAX) { 1155 q < INT_MIN || q > UINT_MAX) {
1156 sysctlperror( 1156 sysctlperror(
1157 "%s: '%s' is not a valid mib number\n", 1157 "%s: '%s' is not a valid mib number\n",
1158 nname, value); 1158 nname, value);
1159 EXIT(1); 1159 EXIT(EXIT_FAILURE);
1160 } 1160 }
1161 node.sysctl_num = (int)q; 1161 node.sysctl_num = (int)q;
1162 } 1162 }
1163 else if (strcmp(key, "flags") == 0) { 1163 else if (strcmp(key, "flags") == 0) {
1164 if (value == NULL) { 1164 if (value == NULL) {
1165 sysctlperror("%s: missing value\n", nname); 1165 sysctlperror("%s: missing value\n", nname);
1166 EXIT(1); 1166 EXIT(EXIT_FAILURE);
1167 } 1167 }
1168 t = value; 1168 t = value;
1169 while (*t != '\0') { 1169 while (*t != '\0') {
1170 switch (*t) { 1170 switch (*t) {
1171 case 'a': 1171 case 'a':
1172 flags |= CTLFLAG_ANYWRITE; 1172 flags |= CTLFLAG_ANYWRITE;
1173 break; 1173 break;
1174 case 'h': 1174 case 'h':
1175 flags |= CTLFLAG_HIDDEN; 1175 flags |= CTLFLAG_HIDDEN;
1176 break; 1176 break;
1177 case 'i': 1177 case 'i':
1178 flags |= CTLFLAG_IMMEDIATE; 1178 flags |= CTLFLAG_IMMEDIATE;
1179 break; 1179 break;
@@ -1190,87 +1190,87 @@ parse_create(char *l) @@ -1190,87 +1190,87 @@ parse_create(char *l)
1190 flags |= CTLFLAG_HEX; 1190 flags |= CTLFLAG_HEX;
1191 break; 1191 break;
1192 1192
1193 case 'r': 1193 case 'r':
1194 rw = CTLFLAG_READONLY; 1194 rw = CTLFLAG_READONLY;
1195 break; 1195 break;
1196 case 'w': 1196 case 'w':
1197 rw = CTLFLAG_READWRITE; 1197 rw = CTLFLAG_READWRITE;
1198 break; 1198 break;
1199 default: 1199 default:
1200 sysctlperror( 1200 sysctlperror(
1201 "%s: '%c' is not a valid flag\n", 1201 "%s: '%c' is not a valid flag\n",
1202 nname, *t); 1202 nname, *t);
1203 EXIT(1); 1203 EXIT(EXIT_FAILURE);
1204 } 1204 }
1205 t++; 1205 t++;
1206 } 1206 }
1207 } 1207 }
1208 else { 1208 else {
1209 sysctlperror("%s: unrecognized keyword '%s'\n", 1209 sysctlperror("%s: unrecognized keyword '%s'\n",
1210 nname, key); 1210 nname, key);
1211 EXIT(1); 1211 EXIT(EXIT_FAILURE);
1212 } 1212 }
1213 } 1213 }
1214 1214
1215 /* 1215 /*
1216 * now that we've finished parsing the given string, fill in 1216 * now that we've finished parsing the given string, fill in
1217 * anything they didn't specify 1217 * anything they didn't specify
1218 */ 1218 */
1219 if (type == 0) 1219 if (type == 0)
1220 type = CTLTYPE_NODE; 1220 type = CTLTYPE_NODE;
1221 1221
1222 /* 1222 /*
1223 * the "data" can be interpreted various ways depending on the 1223 * the "data" can be interpreted various ways depending on the
1224 * type of node we're creating, as can the size 1224 * type of node we're creating, as can the size
1225 */ 1225 */
1226 if (data != NULL) { 1226 if (data != NULL) {
1227 if (addr != NULL) { 1227 if (addr != NULL) {
1228 sysctlperror( 1228 sysctlperror(
1229 "%s: cannot specify both value and " 1229 "%s: cannot specify both value and "
1230 "address\n", nname); 1230 "address\n", nname);
1231 EXIT(1); 1231 EXIT(EXIT_FAILURE);
1232 } 1232 }
1233 1233
1234 switch (type) { 1234 switch (type) {
1235 case CTLTYPE_INT: 1235 case CTLTYPE_INT:
1236 errno = 0; 1236 errno = 0;
1237 q = strtoll(data, &t, 0); 1237 q = strtoll(data, &t, 0);
1238 if (t == data || *t != '\0' || errno != 0 || 1238 if (t == data || *t != '\0' || errno != 0 ||
1239 q < INT_MIN || q > UINT_MAX) { 1239 q < INT_MIN || q > UINT_MAX) {
1240 sysctlperror( 1240 sysctlperror(
1241 "%s: '%s' is not a valid integer\n", 1241 "%s: '%s' is not a valid integer\n",
1242 nname, value); 1242 nname, value);
1243 EXIT(1); 1243 EXIT(EXIT_FAILURE);
1244 } 1244 }
1245 i = (int)q; 1245 i = (int)q;
1246 if (!(flags & CTLFLAG_OWNDATA)) { 1246 if (!(flags & CTLFLAG_OWNDATA)) {
1247 flags |= CTLFLAG_IMMEDIATE; 1247 flags |= CTLFLAG_IMMEDIATE;
1248 node.sysctl_idata = i; 1248 node.sysctl_idata = i;
1249 } 1249 }
1250 else 1250 else
1251 node.sysctl_data = &i; 1251 node.sysctl_data = &i;
1252 if (sz == 0) 1252 if (sz == 0)
1253 sz = sizeof(int); 1253 sz = sizeof(int);
1254 break; 1254 break;
1255 case CTLTYPE_BOOL: 1255 case CTLTYPE_BOOL:
1256 errno = 0; 1256 errno = 0;
1257 q = strtoll(data, &t, 0); 1257 q = strtoll(data, &t, 0);
1258 if (t == data || *t != '\0' || errno != 0 || 1258 if (t == data || *t != '\0' || errno != 0 ||
1259 (q != 0 && q != 1)) { 1259 (q != 0 && q != 1)) {
1260 sysctlperror( 1260 sysctlperror(
1261 "%s: '%s' is not a valid bool\n", 1261 "%s: '%s' is not a valid bool\n",
1262 nname, value); 1262 nname, value);
1263 EXIT(1); 1263 EXIT(EXIT_FAILURE);
1264 } 1264 }
1265 b = q == 1; 1265 b = q == 1;
1266 if (!(flags & CTLFLAG_OWNDATA)) { 1266 if (!(flags & CTLFLAG_OWNDATA)) {
1267 flags |= CTLFLAG_IMMEDIATE; 1267 flags |= CTLFLAG_IMMEDIATE;
1268 node.sysctl_idata = b; 1268 node.sysctl_idata = b;
1269 } 1269 }
1270 else 1270 else
1271 node.sysctl_data = &b; 1271 node.sysctl_data = &b;
1272 if (sz == 0) 1272 if (sz == 0)
1273 sz = sizeof(bool); 1273 sz = sizeof(bool);
1274 break; 1274 break;
1275 case CTLTYPE_STRING: 1275 case CTLTYPE_STRING:
1276 flags |= CTLFLAG_OWNDATA; 1276 flags |= CTLFLAG_OWNDATA;
@@ -1281,123 +1281,123 @@ parse_create(char *l) @@ -1281,123 +1281,123 @@ parse_create(char *l)
1281 sysctlperror("%s: ignoring size=%zu for " 1281 sysctlperror("%s: ignoring size=%zu for "
1282 "string node, too small for given " 1282 "string node, too small for given "
1283 "value\n", nname, sz); 1283 "value\n", nname, sz);
1284 sz = strlen(data) + 1; 1284 sz = strlen(data) + 1;
1285 } 1285 }
1286 break; 1286 break;
1287 case CTLTYPE_QUAD: 1287 case CTLTYPE_QUAD:
1288 errno = 0; 1288 errno = 0;
1289 uq = strtouq(data, &t, 0); 1289 uq = strtouq(data, &t, 0);
1290 if (t == data || *t != '\0' || errno != 0) { 1290 if (t == data || *t != '\0' || errno != 0) {
1291 sysctlperror( 1291 sysctlperror(
1292 "%s: '%s' is not a valid quad\n", 1292 "%s: '%s' is not a valid quad\n",
1293 nname, value); 1293 nname, value);
1294 EXIT(1); 1294 EXIT(EXIT_FAILURE);
1295 } 1295 }
1296 if (!(flags & CTLFLAG_OWNDATA)) { 1296 if (!(flags & CTLFLAG_OWNDATA)) {
1297 flags |= CTLFLAG_IMMEDIATE; 1297 flags |= CTLFLAG_IMMEDIATE;
1298 node.sysctl_qdata = uq; 1298 node.sysctl_qdata = uq;
1299 } 1299 }
1300 else 1300 else
1301 node.sysctl_data = &uq; 1301 node.sysctl_data = &uq;
1302 if (sz == 0) 1302 if (sz == 0)
1303 sz = sizeof(u_quad_t); 1303 sz = sizeof(u_quad_t);
1304 break; 1304 break;
1305 case CTLTYPE_STRUCT: 1305 case CTLTYPE_STRUCT:
1306 sysctlperror("%s: struct not initializable\n", 1306 sysctlperror("%s: struct not initializable\n",
1307 nname); 1307 nname);
1308 EXIT(1); 1308 EXIT(EXIT_FAILURE);
1309 } 1309 }
1310 1310
1311 /* 1311 /*
1312 * these methods have all provided local starting 1312 * these methods have all provided local starting
1313 * values that the kernel must copy in 1313 * values that the kernel must copy in
1314 */ 1314 */
1315 } 1315 }
1316 1316
1317 /* 1317 /*
1318 * hmm...no data, but we have an address of data. that's 1318 * hmm...no data, but we have an address of data. that's
1319 * fine. 1319 * fine.
1320 */ 1320 */
1321 else if (addr != 0) 1321 else if (addr != 0)
1322 node.sysctl_data = (void*)addr; 1322 node.sysctl_data = (void*)addr;
1323 1323
1324 /* 1324 /*
1325 * no data and no address? well...okay. we might be able to 1325 * no data and no address? well...okay. we might be able to
1326 * manage that. 1326 * manage that.
1327 */ 1327 */
1328 else if (type != CTLTYPE_NODE) { 1328 else if (type != CTLTYPE_NODE) {
1329 if (sz == 0) { 1329 if (sz == 0) {
1330 sysctlperror( 1330 sysctlperror(
1331 "%s: need a size or a starting value\n", 1331 "%s: need a size or a starting value\n",
1332 nname); 1332 nname);
1333 EXIT(1); 1333 EXIT(EXIT_FAILURE);
1334 } 1334 }
1335 if (!(flags & CTLFLAG_IMMEDIATE)) 1335 if (!(flags & CTLFLAG_IMMEDIATE))
1336 flags |= CTLFLAG_OWNDATA; 1336 flags |= CTLFLAG_OWNDATA;
1337 } 1337 }
1338 1338
1339 /* 1339 /*
1340 * now we do a few sanity checks on the description we've 1340 * now we do a few sanity checks on the description we've
1341 * assembled 1341 * assembled
1342 */ 1342 */
1343 if ((flags & CTLFLAG_IMMEDIATE) && 1343 if ((flags & CTLFLAG_IMMEDIATE) &&
1344 (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) { 1344 (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
1345 sysctlperror("%s: cannot make an immediate %s\n",  1345 sysctlperror("%s: cannot make an immediate %s\n",
1346 nname, 1346 nname,
1347 (type == CTLTYPE_STRING) ? "string" : "struct"); 1347 (type == CTLTYPE_STRING) ? "string" : "struct");
1348 EXIT(1); 1348 EXIT(EXIT_FAILURE);
1349 } 1349 }
1350 if (type == CTLTYPE_NODE && node.sysctl_data != NULL) { 1350 if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
1351 sysctlperror("%s: nodes do not have data\n", nname); 1351 sysctlperror("%s: nodes do not have data\n", nname);
1352 EXIT(1); 1352 EXIT(EXIT_FAILURE);
1353 } 1353 }
1354  1354
1355 /* 1355 /*
1356 * some types must have a particular size 1356 * some types must have a particular size
1357 */ 1357 */
1358 if (sz != 0) { 1358 if (sz != 0) {
1359 if ((type == CTLTYPE_INT && sz != sizeof(int)) || 1359 if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
1360 (type == CTLTYPE_BOOL && sz != sizeof(bool)) || 1360 (type == CTLTYPE_BOOL && sz != sizeof(bool)) ||
1361 (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) || 1361 (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
1362 (type == CTLTYPE_NODE && sz != 0)) { 1362 (type == CTLTYPE_NODE && sz != 0)) {
1363 sysctlperror("%s: wrong size for type\n", nname); 1363 sysctlperror("%s: wrong size for type\n", nname);
1364 EXIT(1); 1364 EXIT(EXIT_FAILURE);
1365 } 1365 }
1366 } 1366 }
1367 else if (type == CTLTYPE_STRUCT) { 1367 else if (type == CTLTYPE_STRUCT) {
1368 sysctlperror("%s: struct must have size\n", nname); 1368 sysctlperror("%s: struct must have size\n", nname);
1369 EXIT(1); 1369 EXIT(EXIT_FAILURE);
1370 } 1370 }
1371 1371
1372 /* 1372 /*
1373 * now...if no one said anything yet, we default nodes or 1373 * now...if no one said anything yet, we default nodes or
1374 * any type that owns data being writeable, and everything 1374 * any type that owns data being writeable, and everything
1375 * else being readonly. 1375 * else being readonly.
1376 */ 1376 */
1377 if (rw == -1) { 1377 if (rw == -1) {
1378 if (type == CTLTYPE_NODE || 1378 if (type == CTLTYPE_NODE ||
1379 (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE))) 1379 (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE)))
1380 rw = CTLFLAG_READWRITE; 1380 rw = CTLFLAG_READWRITE;
1381 else 1381 else
1382 rw = CTLFLAG_READONLY; 1382 rw = CTLFLAG_READONLY;
1383 } 1383 }
1384 1384
1385 /* 1385 /*
1386 * if a kernel address was specified, that can't be made 1386 * if a kernel address was specified, that can't be made
1387 * writeable by us. 1387 * writeable by us.
1388 if (rw != CTLFLAG_READONLY && addr) { 1388 if (rw != CTLFLAG_READONLY && addr) {
1389 sysctlperror("%s: kernel data can only be readable\n", nname); 1389 sysctlperror("%s: kernel data can only be readable\n", nname);
1390 EXIT(1); 1390 EXIT(EXIT_FAILURE);
1391 } 1391 }
1392 */ 1392 */
1393 1393
1394 /* 1394 /*
1395 * what separator were they using in the full name of the new 1395 * what separator were they using in the full name of the new
1396 * node? 1396 * node?
1397 */ 1397 */
1398 if ((t = strpbrk(nname, "./")) == NULL) 1398 if ((t = strpbrk(nname, "./")) == NULL)
1399 sep[0] = '.'; 1399 sep[0] = '.';
1400 else 1400 else
1401 sep[0] = t[0]; 1401 sep[0] = t[0];
1402 sep[1] = '\0'; 1402 sep[1] = '\0';
1403 1403
@@ -1425,123 +1425,123 @@ parse_create(char *l) @@ -1425,123 +1425,123 @@ parse_create(char *l)
1425 1425
1426 /* 1426 /*
1427 * on the other hand, if it's not a top-level node... 1427 * on the other hand, if it's not a top-level node...
1428 */ 1428 */
1429 else { 1429 else {
1430 namelen = sizeof(name) / sizeof(name[0]); 1430 namelen = sizeof(name) / sizeof(name[0]);
1431 sz = sizeof(gsname); 1431 sz = sizeof(gsname);
1432 *t = '\0'; 1432 *t = '\0';
1433 rc = sysctlgetmibinfo(nname, &name[0], &namelen, 1433 rc = sysctlgetmibinfo(nname, &name[0], &namelen,
1434 gsname, &sz, NULL, SYSCTL_VERSION); 1434 gsname, &sz, NULL, SYSCTL_VERSION);
1435 *t = sep[0]; 1435 *t = sep[0];
1436 if (rc == -1) { 1436 if (rc == -1) {
1437 sysctlparseerror(namelen, nname); 1437 sysctlparseerror(namelen, nname);
1438 EXIT(1); 1438 EXIT(EXIT_FAILURE);
1439 } 1439 }
1440 } 1440 }
1441 1441
1442 /* 1442 /*
1443 * yes, a new node is being created 1443 * yes, a new node is being created
1444 */ 1444 */
1445 if (method != 0) 1445 if (method != 0)
1446 name[namelen++] = method; 1446 name[namelen++] = method;
1447 else 1447 else
1448 name[namelen++] = CTL_CREATE; 1448 name[namelen++] = CTL_CREATE;
1449 1449
1450 sz = sizeof(node); 1450 sz = sizeof(node);
1451 rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node)); 1451 rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
1452 1452
1453 if (rc == -1) { 1453 if (rc == -1) {
1454 sysctlperror("%s: CTL_CREATE failed: %s\n", 1454 sysctlperror("%s: CTL_CREATE failed: %s\n",
1455 nname, strerror(errno)); 1455 nname, strerror(errno));
1456 EXIT(1); 1456 EXIT(EXIT_FAILURE);
1457 } 1457 }
1458 else { 1458 else {
1459 if (!qflag && !nflag) 1459 if (!qflag && !nflag)
1460 printf("%s(%s): (created)\n", nname, st(type)); 1460 printf("%s(%s): (created)\n", nname, st(type));
1461 stale = 1; 1461 stale = 1;
1462 } 1462 }
1463} 1463}
1464 1464
1465static void 1465static void
1466parse_destroy(char *l) 1466parse_destroy(char *l)
1467{ 1467{
1468 struct sysctlnode node; 1468 struct sysctlnode node;
1469 size_t sz; 1469 size_t sz;
1470 int name[CTL_MAXNAME], rc; 1470 int name[CTL_MAXNAME], rc;
1471 u_int namelen; 1471 u_int namelen;
1472 1472
1473 if (!wflag) { 1473 if (!wflag) {
1474 sysctlperror("Must specify -w to destroy nodes\n"); 1474 sysctlperror("Must specify -w to destroy nodes\n");
1475 exit(1); 1475 exit(EXIT_FAILURE);
1476 } 1476 }
1477 1477
1478 memset(name, 0, sizeof(name)); 1478 memset(name, 0, sizeof(name));
1479 namelen = sizeof(name) / sizeof(name[0]); 1479 namelen = sizeof(name) / sizeof(name[0]);
1480 sz = sizeof(gsname); 1480 sz = sizeof(gsname);
1481 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL, 1481 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
1482 SYSCTL_VERSION); 1482 SYSCTL_VERSION);
1483 if (rc == -1) { 1483 if (rc == -1) {
1484 sysctlparseerror(namelen, l); 1484 sysctlparseerror(namelen, l);
1485 EXIT(1); 1485 EXIT(EXIT_FAILURE);
1486 } 1486 }
1487 1487
1488 memset(&node, 0, sizeof(node)); 1488 memset(&node, 0, sizeof(node));
1489 node.sysctl_flags = SYSCTL_VERSION; 1489 node.sysctl_flags = SYSCTL_VERSION;
1490 node.sysctl_num = name[namelen - 1]; 1490 node.sysctl_num = name[namelen - 1];
1491 name[namelen - 1] = CTL_DESTROY; 1491 name[namelen - 1] = CTL_DESTROY;
1492 1492
1493 sz = sizeof(node); 1493 sz = sizeof(node);
1494 rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node)); 1494 rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
1495 1495
1496 if (rc == -1) { 1496 if (rc == -1) {
1497 sysctlperror("%s: CTL_DESTROY failed: %s\n", 1497 sysctlperror("%s: CTL_DESTROY failed: %s\n",
1498 l, strerror(errno)); 1498 l, strerror(errno));
1499 EXIT(1); 1499 EXIT(EXIT_FAILURE);
1500 } 1500 }
1501 else { 1501 else {
1502 if (!qflag && !nflag) 1502 if (!qflag && !nflag)
1503 printf("%s(%s): (destroyed)\n", gsname, 1503 printf("%s(%s): (destroyed)\n", gsname,
1504 st(SYSCTL_TYPE(node.sysctl_flags))); 1504 st(SYSCTL_TYPE(node.sysctl_flags)));
1505 stale = 1; 1505 stale = 1;
1506 } 1506 }
1507} 1507}
1508 1508
1509static void 1509static void
1510parse_describe(char *l) 1510parse_describe(char *l)
1511{ 1511{
1512 struct sysctlnode newdesc; 1512 struct sysctlnode newdesc;
1513 char buf[1024], *value; 1513 char buf[1024], *value;
1514 struct sysctldesc *d = (void*)&buf[0]; 1514 struct sysctldesc *d = (void*)&buf[0];
1515 int name[CTL_MAXNAME], rc; 1515 int name[CTL_MAXNAME], rc;
1516 u_int namelen; 1516 u_int namelen;
1517 size_t sz; 1517 size_t sz;
1518 1518
1519 if (!wflag) { 1519 if (!wflag) {
1520 sysctlperror("Must specify -w to set descriptions\n"); 1520 sysctlperror("Must specify -w to set descriptions\n");
1521 exit(1); 1521 exit(EXIT_FAILURE);
1522 } 1522 }
1523 1523
1524 value = strchr(l, '='); 1524 value = strchr(l, '=');
1525 *value++ = '\0'; 1525 *value++ = '\0';
1526 1526
1527 memset(name, 0, sizeof(name)); 1527 memset(name, 0, sizeof(name));
1528 namelen = sizeof(name) / sizeof(name[0]); 1528 namelen = sizeof(name) / sizeof(name[0]);
1529 sz = sizeof(gsname); 1529 sz = sizeof(gsname);
1530 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL, 1530 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
1531 SYSCTL_VERSION); 1531 SYSCTL_VERSION);
1532 if (rc == -1) { 1532 if (rc == -1) {
1533 sysctlparseerror(namelen, l); 1533 sysctlparseerror(namelen, l);
1534 EXIT(1); 1534 EXIT(EXIT_FAILURE);
1535 } 1535 }
1536 1536
1537 sz = sizeof(buf); 1537 sz = sizeof(buf);
1538 memset(&newdesc, 0, sizeof(newdesc)); 1538 memset(&newdesc, 0, sizeof(newdesc));
1539 newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC; 1539 newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC;
1540 newdesc.sysctl_num = name[namelen - 1]; 1540 newdesc.sysctl_num = name[namelen - 1];
1541 newdesc.sysctl_desc = value; 1541 newdesc.sysctl_desc = value;
1542 name[namelen - 1] = CTL_DESCRIBE; 1542 name[namelen - 1] = CTL_DESCRIBE;
1543 rc = prog_sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc)); 1543 rc = prog_sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc));
1544 if (rc == -1) 1544 if (rc == -1)
1545 sysctlperror("%s: CTL_DESCRIBE failed: %s\n", 1545 sysctlperror("%s: CTL_DESCRIBE failed: %s\n",
1546 gsname, strerror(errno)); 1546 gsname, strerror(errno));
1547 else if (d->descr_len == 1) 1547 else if (d->descr_len == 1)
@@ -1563,27 +1563,27 @@ usage(void) @@ -1563,27 +1563,27 @@ usage(void)
1563 (void)fprintf(stderr, 1563 (void)fprintf(stderr,
1564 "usage:\t%s %s\n" 1564 "usage:\t%s %s\n"
1565 "\t%s %s\n" 1565 "\t%s %s\n"
1566 "\t%s %s\n" 1566 "\t%s %s\n"
1567 "\t%s %s\n" 1567 "\t%s %s\n"
1568 "\t%s %s\n" 1568 "\t%s %s\n"
1569 "\t%s %s\n", 1569 "\t%s %s\n",
1570 progname, "[-dneq] [-x[x]|-r] variable ...", 1570 progname, "[-dneq] [-x[x]|-r] variable ...",
1571 progname, "[-ne] [-q] -w variable=value ...", 1571 progname, "[-ne] [-q] -w variable=value ...",
1572 progname, "[-dne] -a", 1572 progname, "[-dne] -a",
1573 progname, "[-dne] -A", 1573 progname, "[-dne] -A",
1574 progname, "[-ne] -M", 1574 progname, "[-ne] -M",
1575 progname, "[-dne] [-q] -f file"); 1575 progname, "[-dne] [-q] -f file");
1576 exit(1); 1576 exit(EXIT_FAILURE);
1577} 1577}
1578 1578
1579static void 1579static void
1580getdesc1(int *name, u_int namelen, struct sysctlnode *pnode) 1580getdesc1(int *name, u_int namelen, struct sysctlnode *pnode)
1581{ 1581{
1582 struct sysctlnode node; 1582 struct sysctlnode node;
1583 char buf[1024], *desc; 1583 char buf[1024], *desc;
1584 struct sysctldesc *d = (void*)buf; 1584 struct sysctldesc *d = (void*)buf;
1585 size_t sz = sizeof(buf); 1585 size_t sz = sizeof(buf);
1586 int rc; 1586 int rc;
1587 1587
1588 memset(&node, 0, sizeof(node)); 1588 memset(&node, 0, sizeof(node));
1589 node.sysctl_flags = SYSCTL_VERSION; 1589 node.sysctl_flags = SYSCTL_VERSION;
@@ -1698,27 +1698,27 @@ sysctlerror(int soft) @@ -1698,27 +1698,27 @@ sysctlerror(int soft)
1698 case EINVAL: 1698 case EINVAL:
1699 case EOPNOTSUPP: 1699 case EOPNOTSUPP:
1700 case EPROTONOSUPPORT: 1700 case EPROTONOSUPPORT:
1701 if (Aflag || req) 1701 if (Aflag || req)
1702 sysctlperror("%s: the value is not available\n", 1702 sysctlperror("%s: the value is not available\n",
1703 gsname); 1703 gsname);
1704 return; 1704 return;
1705 } 1705 }
1706 } 1706 }
1707 1707
1708 if (Aflag || req) 1708 if (Aflag || req)
1709 sysctlperror("%s: %s\n", gsname, strerror(errno)); 1709 sysctlperror("%s: %s\n", gsname, strerror(errno));
1710 if (!soft) 1710 if (!soft)
1711 EXIT(1); 1711 EXIT(EXIT_FAILURE);
1712} 1712}
1713 1713
1714void 1714void
1715sysctlparseerror(u_int namelen, const char *pname) 1715sysctlparseerror(u_int namelen, const char *pname)
1716{ 1716{
1717 1717
1718 if (qflag) { 1718 if (qflag) {
1719 errs++; 1719 errs++;
1720 return; 1720 return;
1721 } 1721 }
1722 sysctlperror("%s level name '%s' in '%s' is invalid\n", 1722 sysctlperror("%s level name '%s' in '%s' is invalid\n",
1723 lname[namelen], gsname, pname); 1723 lname[namelen], gsname, pname);
1724} 1724}
@@ -1754,40 +1754,40 @@ write_number(int *name, u_int namelen, s @@ -1754,40 +1754,40 @@ write_number(int *name, u_int namelen, s
1754 void *i, *o; 1754 void *i, *o;
1755 char *t; 1755 char *t;
1756 1756
1757 if (fn) 1757 if (fn)
1758 trim_whitespace(value, 3); 1758 trim_whitespace(value, 3);
1759 1759
1760 si = so = 0; 1760 si = so = 0;
1761 i = o = NULL; 1761 i = o = NULL;
1762 bi = bo = false; 1762 bi = bo = false;
1763 errno = 0; 1763 errno = 0;
1764 qi = strtouq(value, &t, 0); 1764 qi = strtouq(value, &t, 0);
1765 if (qi == UQUAD_MAX && errno == ERANGE) { 1765 if (qi == UQUAD_MAX && errno == ERANGE) {
1766 sysctlperror("%s: %s\n", value, strerror(errno)); 1766 sysctlperror("%s: %s\n", value, strerror(errno));
1767 EXIT(1); 1767 EXIT(EXIT_FAILURE);
1768 } 1768 }
1769 if (t == value || *t != '\0') { 1769 if (t == value || *t != '\0') {
1770 sysctlperror("%s: not a number\n", value); 1770 sysctlperror("%s: not a number\n", value);
1771 EXIT(1); 1771 EXIT(EXIT_FAILURE);
1772 } 1772 }
1773 1773
1774 switch (SYSCTL_TYPE(node->sysctl_flags)) { 1774 switch (SYSCTL_TYPE(node->sysctl_flags)) {
1775 case CTLTYPE_INT: 1775 case CTLTYPE_INT:
1776 ii = (u_int)qi; 1776 ii = (u_int)qi;
1777 io = (u_int)(qi >> 32); 1777 io = (u_int)(qi >> 32);
1778 if (io != (u_int)-1 && io != 0) { 1778 if (io != (u_int)-1 && io != 0) {
1779 sysctlperror("%s: %s\n", value, strerror(ERANGE)); 1779 sysctlperror("%s: %s\n", value, strerror(ERANGE));
1780 EXIT(1); 1780 EXIT(EXIT_FAILURE);
1781 } 1781 }
1782 o = &io; 1782 o = &io;
1783 so = sizeof(io); 1783 so = sizeof(io);
1784 i = &ii; 1784 i = &ii;
1785 si = sizeof(ii); 1785 si = sizeof(ii);
1786 break; 1786 break;
1787 case CTLTYPE_BOOL: 1787 case CTLTYPE_BOOL:
1788 bi = (bool)qi; 1788 bi = (bool)qi;
1789 o = &bo; 1789 o = &bo;
1790 so = sizeof(bo); 1790 so = sizeof(bo);
1791 i = &bi; 1791 i = &bi;
1792 si = sizeof(bi); 1792 si = sizeof(bi);
1793 break; 1793 break;
@@ -1823,32 +1823,32 @@ write_number(int *name, u_int namelen, s @@ -1823,32 +1823,32 @@ write_number(int *name, u_int namelen, s
1823 1823
1824static void 1824static void
1825write_string(int *name, u_int namelen, struct sysctlnode *node, char *value) 1825write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
1826{ 1826{
1827 char *i, *o; 1827 char *i, *o;
1828 size_t si, so; 1828 size_t si, so;
1829 int rc; 1829 int rc;
1830 1830
1831 i = value; 1831 i = value;
1832 si = strlen(i) + 1; 1832 si = strlen(i) + 1;
1833 so = node->sysctl_size; 1833 so = node->sysctl_size;
1834 if (si > so && so != 0) { 1834 if (si > so && so != 0) {
1835 sysctlperror("%s: string too long\n", value); 1835 sysctlperror("%s: string too long\n", value);
1836 EXIT(1); 1836 EXIT(EXIT_FAILURE);
1837 } 1837 }
1838 o = malloc(so); 1838 o = malloc(so);
1839 if (o == NULL) { 1839 if (o == NULL) {
1840 sysctlperror("%s: !malloc failed!\n", gsname); 1840 sysctlperror("%s: !malloc failed!\n", gsname);
1841 exit(1); 1841 exit(EXIT_FAILURE);
1842 } 1842 }
1843 1843
1844 rc = prog_sysctl(name, namelen, o, &so, i, si); 1844 rc = prog_sysctl(name, namelen, o, &so, i, si);
1845 if (rc == -1) { 1845 if (rc == -1) {
1846 sysctlerror(0); 1846 sysctlerror(0);
1847 return; 1847 return;
1848 } 1848 }
1849 1849
1850 display_string(node, gsname, o, so, DISPLAY_OLD); 1850 display_string(node, gsname, o, so, DISPLAY_OLD);
1851 display_string(node, gsname, i, si, DISPLAY_NEW); 1851 display_string(node, gsname, i, si, DISPLAY_NEW);
1852 free(o); 1852 free(o);
1853} 1853}
1854 1854
@@ -2141,27 +2141,27 @@ static void @@ -2141,27 +2141,27 @@ static void
2141kern_clockrate(HANDLER_ARGS) 2141kern_clockrate(HANDLER_ARGS)
2142{ 2142{
2143 struct clockinfo clkinfo; 2143 struct clockinfo clkinfo;
2144 size_t sz; 2144 size_t sz;
2145 int rc; 2145 int rc;
2146 2146
2147 sz = sizeof(clkinfo); 2147 sz = sizeof(clkinfo);
2148 rc = prog_sysctl(name, namelen, &clkinfo, &sz, NULL, 0); 2148 rc = prog_sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
2149 if (rc == -1) { 2149 if (rc == -1) {
2150 sysctlerror(1); 2150 sysctlerror(1);
2151 return; 2151 return;
2152 } 2152 }
2153 if (sz != sizeof(clkinfo)) 2153 if (sz != sizeof(clkinfo))
2154 errx(1, "%s: !returned size wrong!", sname); 2154 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2155 2155
2156 if (xflag || rflag) { 2156 if (xflag || rflag) {
2157 display_struct(pnode, sname, &clkinfo, sz, 2157 display_struct(pnode, sname, &clkinfo, sz,
2158 DISPLAY_VALUE); 2158 DISPLAY_VALUE);
2159 return; 2159 return;
2160 } 2160 }
2161 else if (!nflag) 2161 else if (!nflag)
2162 printf("%s: ", sname); 2162 printf("%s: ", sname);
2163 printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n", 2163 printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
2164 clkinfo.tick, clkinfo.tickadj, 2164 clkinfo.tick, clkinfo.tickadj,
2165 clkinfo.hz, clkinfo.profhz, clkinfo.stathz); 2165 clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
2166} 2166}
2167 2167
@@ -2171,27 +2171,27 @@ kern_boottime(HANDLER_ARGS) @@ -2171,27 +2171,27 @@ kern_boottime(HANDLER_ARGS)
2171{ 2171{
2172 struct timeval timeval; 2172 struct timeval timeval;
2173 time_t boottime; 2173 time_t boottime;
2174 size_t sz; 2174 size_t sz;
2175 int rc; 2175 int rc;
2176 2176
2177 sz = sizeof(timeval); 2177 sz = sizeof(timeval);
2178 rc = prog_sysctl(name, namelen, &timeval, &sz, NULL, 0); 2178 rc = prog_sysctl(name, namelen, &timeval, &sz, NULL, 0);
2179 if (rc == -1) { 2179 if (rc == -1) {
2180 sysctlerror(1); 2180 sysctlerror(1);
2181 return; 2181 return;
2182 } 2182 }
2183 if (sz != sizeof(timeval)) 2183 if (sz != sizeof(timeval))
2184 errx(1, "%s: !returned size wrong!", sname); 2184 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2185 2185
2186 boottime = timeval.tv_sec; 2186 boottime = timeval.tv_sec;
2187 if (xflag || rflag) 2187 if (xflag || rflag)
2188 display_struct(pnode, sname, &timeval, sz, 2188 display_struct(pnode, sname, &timeval, sz,
2189 DISPLAY_VALUE); 2189 DISPLAY_VALUE);
2190 else if (!nflag) 2190 else if (!nflag)
2191 /* ctime() provides the \n */ 2191 /* ctime() provides the \n */
2192 printf("%s%s%s", sname, eq, ctime(&boottime)); 2192 printf("%s%s%s", sname, eq, ctime(&boottime));
2193 else if (nflag == 1) 2193 else if (nflag == 1)
2194 printf("%ld\n", (long)boottime); 2194 printf("%ld\n", (long)boottime);
2195 else 2195 else
2196 printf("%ld.%06ld\n", (long)timeval.tv_sec, 2196 printf("%ld.%06ld\n", (long)timeval.tv_sec,
2197 (long)timeval.tv_usec); 2197 (long)timeval.tv_usec);
@@ -2202,27 +2202,27 @@ static void @@ -2202,27 +2202,27 @@ static void
2202kern_consdev(HANDLER_ARGS) 2202kern_consdev(HANDLER_ARGS)
2203{ 2203{
2204 dev_t cons; 2204 dev_t cons;
2205 size_t sz; 2205 size_t sz;
2206 int rc; 2206 int rc;
2207 2207
2208 sz = sizeof(cons); 2208 sz = sizeof(cons);
2209 rc = prog_sysctl(name, namelen, &cons, &sz, NULL, 0); 2209 rc = prog_sysctl(name, namelen, &cons, &sz, NULL, 0);
2210 if (rc == -1) { 2210 if (rc == -1) {
2211 sysctlerror(1); 2211 sysctlerror(1);
2212 return; 2212 return;
2213 } 2213 }
2214 if (sz != sizeof(cons)) 2214 if (sz != sizeof(cons))
2215 errx(1, "%s: !returned size wrong!", sname); 2215 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2216 2216
2217 if (xflag || rflag) 2217 if (xflag || rflag)
2218 display_struct(pnode, sname, &cons, sz, 2218 display_struct(pnode, sname, &cons, sz,
2219 DISPLAY_VALUE); 2219 DISPLAY_VALUE);
2220 else { 2220 else {
2221 if (!nflag) 2221 if (!nflag)
2222 printf("%s%s", sname, eq); 2222 printf("%s%s", sname, eq);
2223 if (nflag < 2 && (sname = devname(cons, S_IFCHR)) != NULL) 2223 if (nflag < 2 && (sname = devname(cons, S_IFCHR)) != NULL)
2224 printf("%s\n", sname); 2224 printf("%s\n", sname);
2225 else 2225 else
2226 printf("0x%llx\n", (unsigned long long)cons); 2226 printf("0x%llx\n", (unsigned long long)cons);
2227 } 2227 }
2228} 2228}
@@ -2267,27 +2267,27 @@ kern_cp_time(HANDLER_ARGS) @@ -2267,27 +2267,27 @@ kern_cp_time(HANDLER_ARGS)
2267 rc = prog_sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz, 2267 rc = prog_sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz,
2268 NULL, 0); 2268 NULL, 0);
2269 2269
2270 if (rc == -1) { 2270 if (rc == -1) {
2271 sysctlerror(1); 2271 sysctlerror(1);
2272 free(cp_time); 2272 free(cp_time);
2273 return; 2273 return;
2274 } 2274 }
2275 2275
2276 /* 2276 /*
2277 * Check, but account for space we'll occupy with the sum. 2277 * Check, but account for space we'll occupy with the sum.
2278 */ 2278 */
2279 if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t)) 2279 if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t))
2280 errx(1, "%s: !returned size wrong!", sname); 2280 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2281 2281
2282 /* 2282 /*
2283 * Compute the actual sum. Two calls would be easier (we 2283 * Compute the actual sum. Two calls would be easier (we
2284 * could just call ourselves recursively above), but the 2284 * could just call ourselves recursively above), but the
2285 * numbers wouldn't add up. 2285 * numbers wouldn't add up.
2286 */ 2286 */
2287 if (n != -1) { 2287 if (n != -1) {
2288 memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES); 2288 memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES);
2289 for (i = 1; i < n; i++) { 2289 for (i = 1; i < n; i++) {
2290 cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER]; 2290 cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER];
2291 cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE]; 2291 cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE];
2292 cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS]; 2292 cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS];
2293 cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR]; 2293 cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR];
@@ -2337,27 +2337,27 @@ kern_drivers(HANDLER_ARGS) @@ -2337,27 +2337,27 @@ kern_drivers(HANDLER_ARGS)
2337{ 2337{
2338 struct kinfo_drivers *kd; 2338 struct kinfo_drivers *kd;
2339 size_t sz, i; 2339 size_t sz, i;
2340 int rc; 2340 int rc;
2341 const char *comma; 2341 const char *comma;
2342 2342
2343 rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0); 2343 rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0);
2344 if (rc == -1) { 2344 if (rc == -1) {
2345 sysctlerror(1); 2345 sysctlerror(1);
2346 return; 2346 return;
2347 } 2347 }
2348 2348
2349 if (sz % sizeof(*kd)) 2349 if (sz % sizeof(*kd))
2350 err(1, "bad size %zu for kern.drivers", sz); 2350 err(EXIT_FAILURE, "bad size %zu for kern.drivers", sz);
2351 2351
2352 kd = malloc(sz); 2352 kd = malloc(sz);
2353 if (kd == NULL) { 2353 if (kd == NULL) {
2354 sysctlerror(1); 2354 sysctlerror(1);
2355 return; 2355 return;
2356 } 2356 }
2357 2357
2358 rc = prog_sysctl(name, namelen, kd, &sz, NULL, 0); 2358 rc = prog_sysctl(name, namelen, kd, &sz, NULL, 0);
2359 if (rc == -1) { 2359 if (rc == -1) {
2360 sysctlerror(1); 2360 sysctlerror(1);
2361 free(kd); 2361 free(kd);
2362 return; 2362 return;
2363 } 2363 }
@@ -2412,27 +2412,27 @@ kern_cp_id(HANDLER_ARGS) @@ -2412,27 +2412,27 @@ kern_cp_id(HANDLER_ARGS)
2412 2412
2413 osz = sz; 2413 osz = sz;
2414 rc = prog_sysctl(name, namelen, cp_id, &osz, NULL, 0); 2414 rc = prog_sysctl(name, namelen, cp_id, &osz, NULL, 0);
2415 if (rc == -1) { 2415 if (rc == -1) {
2416 sysctlerror(1); 2416 sysctlerror(1);
2417 free(cp_id); 2417 free(cp_id);
2418 return; 2418 return;
2419 } 2419 }
2420 2420
2421 /* 2421 /*
2422 * Check that we got back what we asked for. 2422 * Check that we got back what we asked for.
2423 */ 2423 */
2424 if (osz != sz) 2424 if (osz != sz)
2425 errx(1, "%s: !returned size wrong!", sname); 2425 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2426 2426
2427 /* pretend for output purposes */ 2427 /* pretend for output purposes */
2428 node.sysctl_flags = SYSCTL_FLAGS(pnode->sysctl_flags) | 2428 node.sysctl_flags = SYSCTL_FLAGS(pnode->sysctl_flags) |
2429 SYSCTL_TYPE(CTLTYPE_QUAD); 2429 SYSCTL_TYPE(CTLTYPE_QUAD);
2430 2430
2431 tname = sname; 2431 tname = sname;
2432 if (namelen == 3) 2432 if (namelen == 3)
2433 display_number(&node, tname, cp_id, 2433 display_number(&node, tname, cp_id,
2434 sizeof(u_int64_t), 2434 sizeof(u_int64_t),
2435 DISPLAY_VALUE); 2435 DISPLAY_VALUE);
2436 else if (Aflag) { 2436 else if (Aflag) {
2437 for (i = 0; i < n; i++) 2437 for (i = 0; i < n; i++)
2438 (void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, i); 2438 (void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, i);
@@ -2464,27 +2464,27 @@ static void @@ -2464,27 +2464,27 @@ static void
2464vm_loadavg(HANDLER_ARGS) 2464vm_loadavg(HANDLER_ARGS)
2465{ 2465{
2466 struct loadavg loadavg; 2466 struct loadavg loadavg;
2467 size_t sz; 2467 size_t sz;
2468 int rc; 2468 int rc;
2469 2469
2470 sz = sizeof(loadavg); 2470 sz = sizeof(loadavg);
2471 rc = prog_sysctl(name, namelen, &loadavg, &sz, NULL, 0); 2471 rc = prog_sysctl(name, namelen, &loadavg, &sz, NULL, 0);
2472 if (rc == -1) { 2472 if (rc == -1) {
2473 sysctlerror(1); 2473 sysctlerror(1);
2474 return; 2474 return;
2475 } 2475 }
2476 if (sz != sizeof(loadavg)) 2476 if (sz != sizeof(loadavg))
2477 errx(1, "%s: !returned size wrong!", sname); 2477 errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
2478 2478
2479 if (xflag || rflag) { 2479 if (xflag || rflag) {
2480 display_struct(pnode, sname, &loadavg, sz, 2480 display_struct(pnode, sname, &loadavg, sz,
2481 DISPLAY_VALUE); 2481 DISPLAY_VALUE);
2482 return; 2482 return;
2483 } 2483 }
2484 if (!nflag) 2484 if (!nflag)
2485 printf("%s: ", sname); 2485 printf("%s: ", sname);
2486 printf("%.2f %.2f %.2f\n", 2486 printf("%.2f %.2f %.2f\n",
2487 (double) loadavg.ldavg[0] / loadavg.fscale, 2487 (double) loadavg.ldavg[0] / loadavg.fscale,
2488 (double) loadavg.ldavg[1] / loadavg.fscale, 2488 (double) loadavg.ldavg[1] / loadavg.fscale,
2489 (double) loadavg.ldavg[2] / loadavg.fscale); 2489 (double) loadavg.ldavg[2] / loadavg.fscale);
2490} 2490}
@@ -2503,27 +2503,27 @@ proc_limit(HANDLER_ARGS) @@ -2503,27 +2503,27 @@ proc_limit(HANDLER_ARGS)
2503 2503
2504 osz = sizeof(olim); 2504 osz = sizeof(olim);
2505 if (value != NULL) { 2505 if (value != NULL) {
2506 nsz = sizeof(nlim); 2506 nsz = sizeof(nlim);
2507 newp = &nlim; 2507 newp = &nlim;
2508 if (strcmp(value, "unlimited") == 0) 2508 if (strcmp(value, "unlimited") == 0)
2509 nlim = RLIM_INFINITY; 2509 nlim = RLIM_INFINITY;
2510 else { 2510 else {
2511 errno = 0; 2511 errno = 0;
2512 nlim = strtouq(value, &t, 0); 2512 nlim = strtouq(value, &t, 0);
2513 if (t == value || *t != '\0' || errno != 0) { 2513 if (t == value || *t != '\0' || errno != 0) {
2514 sysctlperror("%s: '%s' is not a valid limit\n", 2514 sysctlperror("%s: '%s' is not a valid limit\n",
2515 sname, value); 2515 sname, value);
2516 EXIT(1); 2516 EXIT(EXIT_FAILURE);
2517 } 2517 }
2518 } 2518 }
2519 } 2519 }
2520 else { 2520 else {
2521 nsz = 0; 2521 nsz = 0;
2522 newp = NULL; 2522 newp = NULL;
2523 } 2523 }
2524 2524
2525 rc = prog_sysctl(name, namelen, &olim, &osz, newp, nsz); 2525 rc = prog_sysctl(name, namelen, &olim, &osz, newp, nsz);
2526 if (rc == -1) { 2526 if (rc == -1) {
2527 sysctlerror(newp == NULL); 2527 sysctlerror(newp == NULL);
2528 return; 2528 return;
2529 } 2529 }
@@ -2632,35 +2632,35 @@ mode_bits(HANDLER_ARGS) @@ -2632,35 +2632,35 @@ mode_bits(HANDLER_ARGS)
2632 errno = 0; 2632 errno = 0;
2633 rc = prog_sysctl(name, namelen, &tt, &ttsz, NULL, 0); 2633 rc = prog_sysctl(name, namelen, &tt, &ttsz, NULL, 0);
2634 if (rc == -1) { 2634 if (rc == -1) {
2635 sysctlperror("%s: failed query\n", sname); 2635 sysctlperror("%s: failed query\n", sname);
2636 return; 2636 return;
2637 } 2637 }
2638 2638
2639 old_umask = umask(0); 2639 old_umask = umask(0);
2640 foo = setmode(value); 2640 foo = setmode(value);
2641 umask(old_umask); 2641 umask(old_umask);
2642 if (foo == NULL) { 2642 if (foo == NULL) {
2643 sysctlperror("%s: '%s' is an invalid mode\n", sname, 2643 sysctlperror("%s: '%s' is an invalid mode\n", sname,
2644 value); 2644 value);
2645 EXIT(1); 2645 EXIT(EXIT_FAILURE);
2646 } 2646 }
2647 old_umask = umask(0); 2647 old_umask = umask(0);
2648 m = getmode(foo, (mode_t)tt); 2648 m = getmode(foo, (mode_t)tt);
2649 umask(old_umask); 2649 umask(old_umask);
2650 if (errno) { 2650 if (errno) {
2651 sysctlperror("%s: '%s' is an invalid mode\n", sname, 2651 sysctlperror("%s: '%s' is an invalid mode\n", sname,
2652 value); 2652 value);
2653 EXIT(1); 2653 EXIT(EXIT_FAILURE);
2654 } 2654 }
2655 } 2655 }
2656 else { 2656 else {
2657 nsz = 0; 2657 nsz = 0;
2658 newp = NULL; 2658 newp = NULL;
2659 } 2659 }
2660 2660
2661 rc = prog_sysctl(name, namelen, &o, &osz, newp, nsz); 2661 rc = prog_sysctl(name, namelen, &o, &osz, newp, nsz);
2662 if (rc == -1) { 2662 if (rc == -1) {
2663 sysctlerror(newp == NULL); 2663 sysctlerror(newp == NULL);
2664 return; 2664 return;
2665 } 2665 }
2666 2666
@@ -2700,51 +2700,51 @@ bitmask_print(const bitmap *o) @@ -2700,51 +2700,51 @@ bitmask_print(const bitmap *o)
2700{ 2700{
2701 char *s, *os; 2701 char *s, *os;
2702 2702
2703 s = os = NULL; 2703 s = os = NULL;
2704 for (size_t i = 0; i < MAXPORTS; i++) 2704 for (size_t i = 0; i < MAXPORTS; i++)
2705 if (__BITMAP_ISSET(i, o)) { 2705 if (__BITMAP_ISSET(i, o)) {
2706 int rv; 2706 int rv;
2707 2707
2708 if (os) 2708 if (os)
2709 rv = asprintf(&s, "%s,%zu", os, i); 2709 rv = asprintf(&s, "%s,%zu", os, i);
2710 else 2710 else
2711 rv = asprintf(&s, "%zu", i); 2711 rv = asprintf(&s, "%zu", i);
2712 if (rv == -1) 2712 if (rv == -1)
2713 err(1, "bitmask_print 1"); 2713 err(EXIT_FAILURE, "%s 1", __func__);
2714 free(os); 2714 free(os);
2715 os = s; 2715 os = s;
2716 } 2716 }
2717 if (s == NULL && (s = strdup("")) == NULL) 2717 if (s == NULL && (s = strdup("")) == NULL)
2718 err(1, "bitmask_print 2"); 2718 err(EXIT_FAILURE, "%s 2", __func__);
2719 return s; 2719 return s;
2720} 2720}
2721 2721
2722static void 2722static void
2723bitmask_scan(const void *v, bitmap *o) 2723bitmask_scan(const void *v, bitmap *o)
2724{ 2724{
2725 char *s = strdup(v); 2725 char *s = strdup(v);
2726 if (s == NULL) 2726 if (s == NULL)
2727 err(1, "bitmask_scan"); 2727 err(EXIT_FAILURE, "%s", __func__);
2728 2728
2729 __BITMAP_ZERO(o); 2729 __BITMAP_ZERO(o);
2730 for (s = strtok(s, ","); s; s = strtok(NULL, ",")) { 2730 for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
2731 char *e; 2731 char *e;
2732 errno = 0; 2732 errno = 0;
2733 unsigned long l = strtoul(s, &e, 0); 2733 unsigned long l = strtoul(s, &e, 0);
2734 if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e) 2734 if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e)
2735 errx(1, "Invalid port: %s", s); 2735 errx(EXIT_FAILURE, "Invalid port: %s", s);
2736 if (l >= MAXPORTS) 2736 if (l >= MAXPORTS)
2737 errx(1, "Port out of range: %s", s); 2737 errx(EXIT_FAILURE, "Port out of range: %s", s);
2738 __BITMAP_SET(l, o); 2738 __BITMAP_SET(l, o);
2739 } 2739 }
2740} 2740}
2741 2741
2742 2742
2743static void 2743static void
2744reserve(HANDLER_ARGS) 2744reserve(HANDLER_ARGS)
2745{ 2745{
2746 int rc; 2746 int rc;
2747 size_t osz, nsz; 2747 size_t osz, nsz;
2748 bitmap o, n; 2748 bitmap o, n;
2749 2749
2750 if (fn) 2750 if (fn)