diff options
author | cem <cem@FreeBSD.org> | 2015-10-22 23:03:06 +0000 |
---|---|---|
committer | cem <cem@FreeBSD.org> | 2015-10-22 23:03:06 +0000 |
commit | 51e3af66cf57f1662cee12bc6d67c9a5139b829e (patch) | |
tree | 8056b746fc401f7dfac6c370d709d1561b029a29 | |
parent | a305a56ce15f5a95267a1851fd17239df4ee70a3 (diff) | |
download | FreeBSD-src-51e3af66cf57f1662cee12bc6d67c9a5139b829e.zip FreeBSD-src-51e3af66cf57f1662cee12bc6d67c9a5139b829e.tar.gz |
Sysctl: Add common support for U8, U16 types
Sponsored by: EMC / Isilon Storage Division
-rw-r--r-- | sbin/sysctl/sysctl.c | 28 | ||||
-rw-r--r-- | sys/kern/kern_sysctl.c | 64 | ||||
-rw-r--r-- | sys/sys/sysctl.h | 44 |
3 files changed, 136 insertions, 0 deletions
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 62f12b5..c877922 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -96,6 +96,8 @@ static int ctl_size[CTLTYPE+1] = { [CTLTYPE_ULONG] = sizeof(u_long), [CTLTYPE_S64] = sizeof(int64_t), [CTLTYPE_U64] = sizeof(uint64_t), + [CTLTYPE_U8] = sizeof(uint8_t), + [CTLTYPE_U16] = sizeof(uint16_t), }; static const char *ctl_typename[CTLTYPE+1] = { @@ -105,6 +107,8 @@ static const char *ctl_typename[CTLTYPE+1] = { [CTLTYPE_ULONG] = "unsigned long", [CTLTYPE_S64] = "int64_t", [CTLTYPE_U64] = "uint64_t", + [CTLTYPE_U8] = "uint8_t", + [CTLTYPE_U16] = "uint16_t", }; static void @@ -221,6 +225,8 @@ parse(const char *string, int lineno) int len, i, j; const void *newval; const char *newvalstr = NULL; + uint8_t u8val; + uint16_t u16val; int intval; unsigned int uintval; long longval; @@ -322,6 +328,8 @@ parse(const char *string, int lineno) } switch (kind & CTLTYPE) { + case CTLTYPE_U8: + case CTLTYPE_U16: case CTLTYPE_INT: case CTLTYPE_UINT: case CTLTYPE_LONG: @@ -345,6 +353,16 @@ parse(const char *string, int lineno) errno = 0; switch (kind & CTLTYPE) { + case CTLTYPE_U8: + u8val = (uint8_t)strtoul(newvalstr, &endptr, 0); + newval = &u8val; + newsize = sizeof(u8val); + break; + case CTLTYPE_U16: + u16val = (uint16_t)strtoul(newvalstr, &endptr, 0); + newval = &u16val; + newsize = sizeof(u16val); + break; case CTLTYPE_INT: if (strncmp(fmt, "IK", 2) == 0) intval = strIKtoi(newvalstr, &endptr, fmt); @@ -890,6 +908,8 @@ show_var(int *oid, int nlen) free(oval); return (0); + case CTLTYPE_U8: + case CTLTYPE_U16: case CTLTYPE_INT: case CTLTYPE_UINT: case CTLTYPE_LONG: @@ -902,6 +922,14 @@ show_var(int *oid, int nlen) sep1 = ""; while (len >= intlen) { switch (kind & CTLTYPE) { + case CTLTYPE_U8: + umv = *(uint8_t *)p; + mv = *(int8_t *)p; + break; + case CTLTYPE_U16: + umv = *(uint16_t *)p; + mv = *(int16_t *)p; + break; case CTLTYPE_INT: case CTLTYPE_UINT: umv = *(u_int *)p; diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 52075e4..0ee8a1a 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1128,6 +1128,70 @@ static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD */ /* + * Handle an int8_t, signed or unsigned. + * Two cases: + * a variable: point arg1 at it. + * a constant: pass it in arg2. + */ + +int +sysctl_handle_8(SYSCTL_HANDLER_ARGS) +{ + int8_t tmpout; + int error = 0; + + /* + * Attempt to get a coherent snapshot by making a copy of the data. + */ + if (arg1) + tmpout = *(int8_t *)arg1; + else + tmpout = arg2; + error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); + + if (error || !req->newptr) + return (error); + + if (!arg1) + error = EPERM; + else + error = SYSCTL_IN(req, arg1, sizeof(tmpout)); + return (error); +} + +/* + * Handle an int16_t, signed or unsigned. + * Two cases: + * a variable: point arg1 at it. + * a constant: pass it in arg2. + */ + +int +sysctl_handle_16(SYSCTL_HANDLER_ARGS) +{ + int16_t tmpout; + int error = 0; + + /* + * Attempt to get a coherent snapshot by making a copy of the data. + */ + if (arg1) + tmpout = *(int16_t *)arg1; + else + tmpout = arg2; + error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); + + if (error || !req->newptr) + return (error); + + if (!arg1) + error = EPERM; + else + error = SYSCTL_IN(req, arg1, sizeof(tmpout)); + return (error); +} + +/* * Handle an int, signed or unsigned. * Two cases: * a variable: point arg1 at it. diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 4c66431..4e6c556 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -73,6 +73,8 @@ struct ctlname { #define CTLTYPE_LONG 7 /* name describes a long */ #define CTLTYPE_ULONG 8 /* name describes an unsigned long */ #define CTLTYPE_U64 9 /* name describes an unsigned 64-bit number */ +#define CTLTYPE_U8 0xa /* name describes an unsigned 8-bit number */ +#define CTLTYPE_U16 0xb /* name describes an unsigned 16-bit number */ #define CTLFLAG_RD 0x80000000 /* Allow reads of variable */ #define CTLFLAG_WR 0x40000000 /* Allow writes to the variable */ @@ -188,6 +190,8 @@ struct sysctl_oid { #define SYSCTL_OUT(r, p, l) (r->oldfunc)(r, p, l) #define SYSCTL_OUT_STR(r, p) (r->oldfunc)(r, p, strlen(p) + 1) +int sysctl_handle_8(SYSCTL_HANDLER_ARGS); +int sysctl_handle_16(SYSCTL_HANDLER_ARGS); int sysctl_handle_int(SYSCTL_HANDLER_ARGS); int sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS); int sysctl_handle_long(SYSCTL_HANDLER_ARGS); @@ -319,6 +323,46 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry); __arg, len, sysctl_handle_string, "A", __DESCR(descr)); \ }) +/* Oid for an unsigned 8-bit int. If ptr is NULL, val is returned. */ +#define SYSCTL_NULL_U8_PTR ((unsigned *)NULL) +#define SYSCTL_U8(parent, nbr, name, access, ptr, val, descr) \ + SYSCTL_OID(parent, nbr, name, \ + CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \ + ptr, val, sysctl_handle_8, "CU", descr); \ + CTASSERT((((access) & CTLTYPE) == 0 || \ + ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U8) && \ + sizeof(uint8_t) == sizeof(*(ptr))) + +#define SYSCTL_ADD_U8(ctx, parent, nbr, name, access, ptr, val, descr) \ +({ \ + uint8_t *__ptr = (ptr); \ + CTASSERT(((access) & CTLTYPE) == 0 || \ + ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U8); \ + sysctl_add_oid(ctx, parent, nbr, name, \ + CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \ + __ptr, val, sysctl_handle_8, "CU", __DESCR(descr)); \ +}) + +/* Oid for an unsigned 16-bit int. If ptr is NULL, val is returned. */ +#define SYSCTL_NULL_U16_PTR ((unsigned *)NULL) +#define SYSCTL_U16(parent, nbr, name, access, ptr, val, descr) \ + SYSCTL_OID(parent, nbr, name, \ + CTLTYPE_U16 | CTLFLAG_MPSAFE | (access), \ + ptr, val, sysctl_handle_16, "SU", descr); \ + CTASSERT((((access) & CTLTYPE) == 0 || \ + ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U16) && \ + sizeof(uint16_t) == sizeof(*(ptr))) + +#define SYSCTL_ADD_U16(ctx, parent, nbr, name, access, ptr, val, descr) \ +({ \ + uint16_t *__ptr = (ptr); \ + CTASSERT(((access) & CTLTYPE) == 0 || \ + ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U16); \ + sysctl_add_oid(ctx, parent, nbr, name, \ + CTLTYPE_U16 | CTLFLAG_MPSAFE | (access), \ + __ptr, val, sysctl_handle_16, "SU", __DESCR(descr)); \ +}) + /* Oid for an int. If ptr is SYSCTL_NULL_INT_PTR, val is returned. */ #define SYSCTL_NULL_INT_PTR ((int *)NULL) #define SYSCTL_INT(parent, nbr, name, access, ptr, val, descr) \ |