style(9) and cleanup fixes.

MFC after: 1 week
This commit is contained in:
Matthew D Fleming 2011-07-08 20:41:07 +00:00
parent 2f8c04ad8f
commit 0894a9bca6

View File

@ -52,6 +52,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/ctype.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/fail.h> #include <sys/fail.h>
#include <sys/kernel.h> #include <sys/kernel.h>
@ -88,16 +89,20 @@ enum fail_point_t {
FAIL_POINT_BREAK, /**< break into the debugger */ FAIL_POINT_BREAK, /**< break into the debugger */
FAIL_POINT_PRINT, /**< print a message */ FAIL_POINT_PRINT, /**< print a message */
FAIL_POINT_SLEEP, /**< sleep for some msecs */ FAIL_POINT_SLEEP, /**< sleep for some msecs */
FAIL_POINT_INVALID, /**< placeholder */ FAIL_POINT_NUMTYPES
}; };
static const char *fail_type_strings[] = { static struct {
"off", const char *name;
"panic", int nmlen;
"return", } fail_type_strings[] = {
"break", #define FP_TYPE_NM_LEN(s) { s, sizeof(s) - 1 }
"print", [FAIL_POINT_OFF] = FP_TYPE_NM_LEN("off"),
"sleep", [FAIL_POINT_PANIC] = FP_TYPE_NM_LEN("panic"),
[FAIL_POINT_RETURN] = FP_TYPE_NM_LEN("return"),
[FAIL_POINT_BREAK] = FP_TYPE_NM_LEN("break"),
[FAIL_POINT_PRINT] = FP_TYPE_NM_LEN("print"),
[FAIL_POINT_SLEEP] = FP_TYPE_NM_LEN("sleep"),
}; };
/** /**
@ -120,7 +125,7 @@ fail_point_sleep(struct fail_point *fp, struct fail_point_entry *ent,
/* convert from millisecs to ticks, rounding up */ /* convert from millisecs to ticks, rounding up */
int timo = ((msecs * hz) + 999) / 1000; int timo = ((msecs * hz) + 999) / 1000;
if (timo) { if (timo > 0) {
if (fp->fp_sleep_fn == NULL) { if (fp->fp_sleep_fn == NULL) {
msleep(fp, &g_fp_mtx, PWAIT, "failpt", timo); msleep(fp, &g_fp_mtx, PWAIT, "failpt", timo);
} else { } else {
@ -191,19 +196,13 @@ fail_point_init(struct fail_point *fp, const char *fmt, ...)
void void
fail_point_destroy(struct fail_point *fp) fail_point_destroy(struct fail_point *fp)
{ {
struct fail_point_entry *ent;
if (fp->fp_flags & FAIL_POINT_DYNAMIC_NAME && fp->fp_name != NULL) { if ((fp->fp_flags & FAIL_POINT_DYNAMIC_NAME) != 0) {
fp_free((void *)(intptr_t)fp->fp_name); fp_free(__DECONST(void *, fp->fp_name));
fp->fp_name = NULL; fp->fp_name = NULL;
} }
fp->fp_flags = 0; fp->fp_flags = 0;
clear_entries(&fp->fp_entries);
while (!TAILQ_EMPTY(&fp->fp_entries)) {
ent = TAILQ_FIRST(&fp->fp_entries);
TAILQ_REMOVE(&fp->fp_entries, ent, fe_entries);
fp_free(ent);
}
} }
/** /**
@ -222,16 +221,12 @@ fail_point_eval_nontrivial(struct fail_point *fp, int *return_value)
FP_LOCK(); FP_LOCK();
ent = TAILQ_FIRST(&fp->fp_entries); TAILQ_FOREACH_SAFE(ent, &fp->fp_entries, fe_entries, next) {
while (ent) {
int cont = 0; /* don't continue by default */ int cont = 0; /* don't continue by default */
next = TAILQ_NEXT(ent, fe_entries);
if (ent->fe_prob < PROB_MAX && if (ent->fe_prob < PROB_MAX &&
ent->fe_prob < random() % PROB_MAX) { ent->fe_prob < random() % PROB_MAX)
cont = 1; continue;
goto loop_end;
}
switch (ent->fe_type) { switch (ent->fe_type) {
case FAIL_POINT_PANIC: case FAIL_POINT_PANIC:
@ -239,13 +234,14 @@ fail_point_eval_nontrivial(struct fail_point *fp, int *return_value)
/* NOTREACHED */ /* NOTREACHED */
case FAIL_POINT_RETURN: case FAIL_POINT_RETURN:
if (return_value) if (return_value != NULL)
*return_value = ent->fe_arg; *return_value = ent->fe_arg;
ret = FAIL_POINT_RC_RETURN; ret = FAIL_POINT_RC_RETURN;
break; break;
case FAIL_POINT_BREAK: case FAIL_POINT_BREAK:
printf("fail point %s breaking to debugger\n", fp->fp_name); printf("fail point %s breaking to debugger\n",
fp->fp_name);
breakpoint(); breakpoint();
break; break;
@ -273,13 +269,9 @@ fail_point_eval_nontrivial(struct fail_point *fp, int *return_value)
break; break;
} }
if (ent && ent->fe_count > 0 && --ent->fe_count == 0) if (ent != NULL && ent->fe_count > 0 && --ent->fe_count == 0)
free_entry(&fp->fp_entries, ent); free_entry(&fp->fp_entries, ent);
if (cont == 0)
loop_end:
if (cont)
ent = next;
else
break; break;
} }
@ -290,7 +282,7 @@ loop_end:
FP_UNLOCK(); FP_UNLOCK();
return ret; return (ret);
} }
/** /**
@ -320,7 +312,7 @@ fail_point_get(struct fail_point *fp, struct sbuf *sb)
} }
if (ent->fe_count > 0) if (ent->fe_count > 0)
sbuf_printf(sb, "%d*", ent->fe_count); sbuf_printf(sb, "%d*", ent->fe_count);
sbuf_printf(sb, "%s", fail_type_strings[ent->fe_type]); sbuf_printf(sb, "%s", fail_type_strings[ent->fe_type].name);
if (ent->fe_arg) if (ent->fe_arg)
sbuf_printf(sb, "(%d)", ent->fe_arg); sbuf_printf(sb, "(%d)", ent->fe_arg);
if (TAILQ_NEXT(ent, fe_entries)) if (TAILQ_NEXT(ent, fe_entries))
@ -380,7 +372,7 @@ fail_point_set(struct fail_point *fp, char *buf)
fp->fp_name, fp->fp_location, buf); fp->fp_name, fp->fp_location, buf);
#endif /* IWARNING */ #endif /* IWARNING */
return error; return (error);
} }
#define MAX_FAIL_POINT_BUF 1023 #define MAX_FAIL_POINT_BUF 1023
@ -422,9 +414,8 @@ fail_point_sysctl(SYSCTL_HANDLER_ARGS)
} }
out: out:
if (buf) fp_free(buf);
fp_free(buf); return (error);
return error;
} }
/** /**
@ -437,12 +428,17 @@ parse_fail_point(struct fail_point_entries *ents, char *p)
/* <fail_point> :: /* <fail_point> ::
* <term> ( "->" <term> )* * <term> ( "->" <term> )*
*/ */
if (!(p = parse_term(ents, p))) p = parse_term(ents, p);
return 0; if (p == NULL)
while (*p) return (NULL);
if (p[0] != '-' || p[1] != '>' || !(p = parse_term(ents, p+2))) while (*p != '\0') {
return 0; if (p[0] != '-' || p[1] != '>')
return p; return (NULL);
p = parse_term(ents, p + 2);
if (p == NULL)
return (NULL);
}
return (p);
} }
/** /**
@ -465,11 +461,12 @@ parse_term(struct fail_point_entries *ents, char *p)
*/ */
/* ( (<float> "%") | (<integer> "*" ) )* */ /* ( (<float> "%") | (<integer> "*" ) )* */
while (('0' <= *p && *p <= '9') || *p == '.') { while (isdigit(*p) || *p == '.') {
int units, decimal; int units, decimal;
if (!(p = parse_number(&units, &decimal, p))) p = parse_number(&units, &decimal, p);
return 0; if (p == NULL)
return (NULL);
if (*p == '%') { if (*p == '%') {
if (units > 100) /* prevent overflow early */ if (units > 100) /* prevent overflow early */
@ -477,37 +474,33 @@ parse_term(struct fail_point_entries *ents, char *p)
ent->fe_prob = units * (PROB_MAX / 100) + decimal; ent->fe_prob = units * (PROB_MAX / 100) + decimal;
if (ent->fe_prob > PROB_MAX) if (ent->fe_prob > PROB_MAX)
ent->fe_prob = PROB_MAX; ent->fe_prob = PROB_MAX;
} else if (*p == '*') { } else if (*p == '*') {
if (!units || decimal) if (!units || decimal)
return 0; return (NULL);
ent->fe_count = units; ent->fe_count = units;
} else
} else { return (NULL);
return 0;
}
p++; p++;
} }
/* <type> */ /* <type> */
if (!(p = parse_type(ent, p))) p = parse_type(ent, p);
return 0; if (p == NULL)
return (NULL);
if (*p == '\0') if (*p == '\0')
return p; return (p);
/* [ "(" <integer> ")" ] */ /* [ "(" <integer> ")" ] */
if (*p != '(') if (*p != '(')
return p; return p;
p++; p++;
if (('0' <= *p && *p <= '9') || *p == '-') if (!isdigit(*p) && *p != '-')
ent->fe_arg = strtol(p, &p, 0); return (NULL);
else ent->fe_arg = strtol(p, &p, 0);
return 0;
if (*p++ != ')') if (*p++ != ')')
return 0; return (NULL);
return p; return (p);
} }
/** /**
@ -528,14 +521,14 @@ parse_number(int *out_units, int *out_decimal, char *p)
old_p = p; old_p = p;
*out_units = strtol(p, &p, 10); *out_units = strtol(p, &p, 10);
if (p == old_p && *p != '.') if (p == old_p && *p != '.')
return 0; return (NULL);
/* fractional part */ /* fractional part */
*out_decimal = 0; *out_decimal = 0;
if (*p == '.') { if (*p == '.') {
int digits = 0; int digits = 0;
p++; p++;
while ('0' <= *p && *p <= '9') { while (isdigit(*p)) {
int digit = *p - '0'; int digit = *p - '0';
if (digits < PROB_DIGITS - 2) if (digits < PROB_DIGITS - 2)
*out_decimal = *out_decimal * 10 + digit; *out_decimal = *out_decimal * 10 + digit;
@ -545,12 +538,12 @@ parse_number(int *out_units, int *out_decimal, char *p)
p++; p++;
} }
if (!digits) /* need at least one digit after '.' */ if (!digits) /* need at least one digit after '.' */
return 0; return (NULL);
while (digits++ < PROB_DIGITS - 2) /* add implicit zeros */ while (digits++ < PROB_DIGITS - 2) /* add implicit zeros */
*out_decimal *= 10; *out_decimal *= 10;
} }
return p; /* success */ return (p); /* success */
} }
/** /**
@ -560,21 +553,16 @@ static char *
parse_type(struct fail_point_entry *ent, char *beg) parse_type(struct fail_point_entry *ent, char *beg)
{ {
enum fail_point_t type; enum fail_point_t type;
char *end = beg; int len;
while ('a' <= *end && *end <= 'z')
end++; for (type = FAIL_POINT_OFF; type < FAIL_POINT_NUMTYPES; type++) {
if (beg == end) len = fail_type_strings[type].nmlen;
return 0; if (strncmp(fail_type_strings[type].name, beg, len) == 0) {
for (type = FAIL_POINT_OFF; type != FAIL_POINT_INVALID; type++) {
const char *p = fail_type_strings[type];
const char *q = beg;
while (q < end && *p++ == *q++);
if (q == end && *p == '\0') {
ent->fe_type = type; ent->fe_type = type;
return end; return (beg + len);
} }
} }
return 0; return (NULL);
} }
/** /**
@ -595,6 +583,7 @@ static void
clear_entries(struct fail_point_entries *ents) clear_entries(struct fail_point_entries *ents)
{ {
struct fail_point_entry *ent, *ent_next; struct fail_point_entry *ent, *ent_next;
TAILQ_FOREACH_SAFE(ent, ents, fe_entries, ent_next) TAILQ_FOREACH_SAFE(ent, ents, fe_entries, ent_next)
fp_free(ent); fp_free(ent);
TAILQ_INIT(ents); TAILQ_INIT(ents);