Changeset View
Changeset View
Standalone View
Standalone View
sbin/sysctl/sysctl.c
Show First 20 Lines • Show All 944 Lines • ▼ Show 20 Lines | if (kind) | |||||||||
*kind = *(u_int *)buf; | *kind = *(u_int *)buf; | |||||||||
if (fmt) | if (fmt) | |||||||||
strcpy(fmt, (char *)(buf + sizeof(u_int))); | strcpy(fmt, (char *)(buf + sizeof(u_int))); | |||||||||
return (0); | return (0); | |||||||||
} | } | |||||||||
/* | /* | |||||||||
* This displays information aout a sysctl | ||||||||||
* | ||||||||||
* Returns zero if anything was actually output. | ||||||||||
* Returns one if didn't know what to do with this. | ||||||||||
* Return minus one if we had errors. | ||||||||||
*/ | ||||||||||
static int | ||||||||||
show_info(char *name, const char *sep, int ctltype, int *qoid, int nlen) | ||||||||||
{ | ||||||||||
u_char buf[BUFSIZ]; | ||||||||||
const char *prntype; | ||||||||||
int error = 0, i; | ||||||||||
size_t j; | ||||||||||
if (!nflag) | ||||||||||
printf("%s%s", name, sep); | ||||||||||
if (ctl_typename[ctltype] != NULL) | ||||||||||
prntype = ctl_typename[ctltype]; | ||||||||||
else { | ||||||||||
prntype = "unknown"; | ||||||||||
error++; | ||||||||||
} | ||||||||||
if (tflag && dflag) | ||||||||||
printf("%s%s", prntype, sep); | ||||||||||
else if (tflag) { | ||||||||||
printf("%s", prntype); | ||||||||||
return (error); | ||||||||||
} | ||||||||||
qoid[1] = CTL_SYSCTL_OIDDESCR; | ||||||||||
bzero(buf, BUFSIZ); | ||||||||||
delphij: `i` is assigned but not used. I think it's quite likely that this `sysctl` call would always… | ||||||||||
j = sizeof(buf); | ||||||||||
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); | ||||||||||
if (i < 0) | ||||||||||
return (1); | ||||||||||
printf("%s", buf); | ||||||||||
return (error); | ||||||||||
} | ||||||||||
/* | ||||||||||
* This formats and outputs the value of one variable | * This formats and outputs the value of one variable | |||||||||
* | * | |||||||||
* Returns zero if anything was actually output. | * Returns zero if anything was actually output. | |||||||||
* Returns one if didn't know what to do with this. | * Returns one if didn't know what to do with this. | |||||||||
* Return minus one if we had errors. | * Return minus one if we had errors. | |||||||||
*/ | */ | |||||||||
Not Done Inline Actionserror is always 0, can we get rid of either error or i? markj: `error` is always 0, can we get rid of either `error` or `i`? | ||||||||||
static int | static int | |||||||||
show_var(int *oid, int nlen, bool honor_skip) | show_var(int *oid, int nlen, bool honor_skip) | |||||||||
{ | { | |||||||||
static int skip_len = 0, skip_oid[CTL_MAXNAME]; | static int skip_len = 0, skip_oid[CTL_MAXNAME]; | |||||||||
u_char buf[BUFSIZ], *val, *oval, *p; | u_char *val, *oval, *p; | |||||||||
char name[BUFSIZ], fmt[BUFSIZ]; | char name[BUFSIZ], fmt[BUFSIZ]; | |||||||||
const char *sep, *sep1, *prntype; | const char *sep, *sep1; | |||||||||
int qoid[CTL_MAXNAME+2]; | int qoid[CTL_MAXNAME+2]; | |||||||||
uintmax_t umv; | uintmax_t umv; | |||||||||
intmax_t mv; | intmax_t mv; | |||||||||
int i, hexlen, sign, ctltype; | int i, hexlen, sign, ctltype; | |||||||||
size_t intlen; | size_t intlen; | |||||||||
size_t j, len; | size_t j, len; | |||||||||
u_int kind; | u_int kind; | |||||||||
float base; | float base; | |||||||||
int (*func)(size_t, void *); | int (*func)(size_t, void *); | |||||||||
int prec; | int prec; | |||||||||
/* Silence GCC. */ | /* Silence GCC. */ | |||||||||
umv = mv = intlen = 0; | umv = mv = intlen = 0; | |||||||||
bzero(buf, BUFSIZ); | ||||||||||
bzero(fmt, BUFSIZ); | bzero(fmt, BUFSIZ); | |||||||||
bzero(name, BUFSIZ); | bzero(name, BUFSIZ); | |||||||||
qoid[0] = CTL_SYSCTL; | qoid[0] = CTL_SYSCTL; | |||||||||
qoid[1] = CTL_SYSCTL_NAME; | qoid[1] = CTL_SYSCTL_NAME; | |||||||||
memcpy(qoid + 2, oid, nlen * sizeof(int)); | memcpy(qoid + 2, oid, nlen * sizeof(int)); | |||||||||
j = sizeof(name); | j = sizeof(name); | |||||||||
i = sysctl(qoid, nlen + 2, name, &j, 0, 0); | i = sysctl(qoid, nlen + 2, name, &j, 0, 0); | |||||||||
if (i || !j) | if (i || !j) | |||||||||
Show All 14 Lines | show_var(int *oid, int nlen, bool honor_skip) | |||||||||
} | } | |||||||||
if (eflag) | if (eflag) | |||||||||
sep = "="; | sep = "="; | |||||||||
else | else | |||||||||
sep = ": "; | sep = ": "; | |||||||||
ctltype = (kind & CTLTYPE); | ctltype = (kind & CTLTYPE); | |||||||||
if (tflag || dflag) { | if (tflag || dflag) | |||||||||
if (!nflag) | return show_info(name, sep, ctltype, qoid, nlen); | |||||||||
Not Done Inline Actions
markj: | ||||||||||
printf("%s%s", name, sep); | ||||||||||
if (ctl_typename[ctltype] != NULL) | ||||||||||
prntype = ctl_typename[ctltype]; | ||||||||||
else | ||||||||||
prntype = "unknown"; | ||||||||||
if (tflag && dflag) | ||||||||||
printf("%s%s", prntype, sep); | ||||||||||
else if (tflag) { | ||||||||||
printf("%s", prntype); | ||||||||||
return (0); | ||||||||||
} | ||||||||||
qoid[1] = CTL_SYSCTL_OIDDESCR; | ||||||||||
j = sizeof(buf); | ||||||||||
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); | ||||||||||
printf("%s", buf); | ||||||||||
return (0); | ||||||||||
} | ||||||||||
/* keep track of encountered skip nodes, ignoring descendants */ | /* keep track of encountered skip nodes, ignoring descendants */ | |||||||||
if ((skip_len == 0 || skip_len >= nlen * (int)sizeof(int)) && | if ((skip_len == 0 || skip_len >= nlen * (int)sizeof(int)) && | |||||||||
(kind & CTLFLAG_SKIP) != 0) { | (kind & CTLFLAG_SKIP) != 0) { | |||||||||
/* Save this oid so we can skip descendants. */ | /* Save this oid so we can skip descendants. */ | |||||||||
skip_len = nlen * sizeof(int); | skip_len = nlen * sizeof(int); | |||||||||
memcpy(skip_oid, oid, skip_len); | memcpy(skip_oid, oid, skip_len); | |||||||||
} | } | |||||||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | #endif | |||||||||
val = oval = malloc(j + 1); | val = oval = malloc(j + 1); | |||||||||
if (val == NULL) { | if (val == NULL) { | |||||||||
warnx("malloc failed"); | warnx("malloc failed"); | |||||||||
return (1); | return (1); | |||||||||
} | } | |||||||||
len = j; | len = j; | |||||||||
i = sysctl(oid, nlen, val, &len, 0, 0); | i = sysctl(oid, nlen, val, &len, 0, 0); | |||||||||
if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { | if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { | |||||||||
free(oval); | free(oval); | |||||||||
Done Inline ActionsLooking at how another sysctl failure is handled, it silently skips printing. I suppose this is for -a, all option. I will follow the same error handling. ota_j.email.ne.jp: Looking at how another sysctl failure is handled, it silently skips printing. I suppose this… | ||||||||||
return (1); | return (1); | |||||||||
} | } | |||||||||
if (bflag) { | if (bflag) { | |||||||||
fwrite(val, 1, len, stdout); | fwrite(val, 1, len, stdout); | |||||||||
free(oval); | free(oval); | |||||||||
return (0); | return (0); | |||||||||
} | } | |||||||||
▲ Show 20 Lines • Show All 165 Lines • Show Last 20 Lines |
i is assigned but not used. I think it's quite likely that this sysctl call would always succeed as buf is sufficiently large, so if I was you I'd check the return value or maybe convert it to an assertion here.