From 44b3f2ffbc01c0a9fcfb5d60af3e292f505ac67c Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 21 Aug 2017 02:14:19 +0200 Subject: libbb: move capability names code to libbb function old new delta cap_name_to_number - 77 +77 parse_cap 117 29 -88 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 0/1 up/down: 77/-88) Total: -11 bytes Signed-off-by: Denys Vlasenko --- libbb/capability.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 libbb/capability.c (limited to 'libbb') diff --git a/libbb/capability.c b/libbb/capability.c new file mode 100644 index 0000000..692024f --- /dev/null +++ b/libbb/capability.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2017 by + * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. + */ +//kbuild:lib-$(CONFIG_PLATFORM_LINUX) += capability.o + +#include +#include "libbb.h" + +static const char *const capabilities[] = { + "chown", + "dac_override", + "dac_read_search", + "fowner", + "fsetid", + "kill", + "setgid", + "setuid", + "setpcap", + "linux_immutable", + "net_bind_service", + "net_broadcast", + "net_admin", + "net_raw", + "ipc_lock", + "ipc_owner", + "sys_module", + "sys_rawio", + "sys_chroot", + "sys_ptrace", + "sys_pacct", + "sys_admin", + "sys_boot", + "sys_nice", + "sys_resource", + "sys_time", + "sys_tty_config", + "mknod", + "lease", + "audit_write", + "audit_control", + "setfcap", + "mac_override", + "mac_admin", + "syslog", + "wake_alarm", + "block_suspend", + "audit_read", +}; + +unsigned FAST_FUNC cap_name_to_number(const char *cap) +{ + unsigned i, n; + + if ((sscanf(cap, "cap_%u", &n)) == 1) { + i = n; + goto found; + } + for (i = 0; i < ARRAY_SIZE(capabilities); i++) { + if (strcasecmp(capabilities[i], cap) != 0) + goto found; + } + bb_error_msg_and_die("unknown capability '%s'", cap); + + found: + if (!cap_valid(i)) + bb_error_msg_and_die("unknown capability '%s'", cap); + return i; +} + +void FAST_FUNC printf_cap(const char *pfx, unsigned cap_no) +{ + if (cap_no < ARRAY_SIZE(capabilities)) { + printf("%s%s", pfx, capabilities[cap_no]); + return; + } + printf("%scap_%u", pfx, cap_no); +} -- cgit v1.1