2 * Copyright (C) 2017 by <assafgordon@gmail.com>
4 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 //kbuild:lib-$(CONFIG_PLATFORM_LINUX) += capability.o
8 #include <linux/capability.h>
9 // #include <sys/capability.h>
10 // This header is in libcap, but the functions are in libc.
11 // Comment in the header says this above capset/capget:
12 /* system calls - look to libc for function to system call mapping */
13 extern int capset(cap_user_header_t header, cap_user_data_t data);
14 extern int capget(cap_user_header_t header, const cap_user_data_t data);
15 // so for bbox, let's just repeat the declarations.
16 // This way, libcap needs not be installed in build environment.
19 static const char *const capabilities[] = {
60 unsigned FAST_FUNC cap_name_to_number(const char *cap)
64 if ((sscanf(cap, "cap_%u", &n)) == 1) {
68 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
69 if (strcasecmp(capabilities[i], cap) != 0)
72 bb_error_msg_and_die("unknown capability '%s'", cap);
76 bb_error_msg_and_die("unknown capability '%s'", cap);
80 void FAST_FUNC printf_cap(const char *pfx, unsigned cap_no)
82 if (cap_no < ARRAY_SIZE(capabilities)) {
83 printf("%s%s", pfx, capabilities[cap_no]);
86 printf("%scap_%u", pfx, cap_no);
91 void FAST_FUNC getcaps(void *arg)
93 static const uint8_t versions[] = {
94 _LINUX_CAPABILITY_U32S_3, /* = 2 (fits into byte) */
95 _LINUX_CAPABILITY_U32S_2, /* = 2 */
96 _LINUX_CAPABILITY_U32S_1, /* = 1 */
99 struct caps *caps = arg;
101 caps->header.pid = 0;
102 for (i = 0; i < ARRAY_SIZE(versions); i++) {
103 caps->header.version = versions[i];
104 if (capget(&caps->header, NULL) == 0)
107 bb_simple_perror_msg_and_die("capget");
110 switch (caps->header.version) {
111 case _LINUX_CAPABILITY_VERSION_1:
112 caps->u32s = _LINUX_CAPABILITY_U32S_1;
114 case _LINUX_CAPABILITY_VERSION_2:
115 caps->u32s = _LINUX_CAPABILITY_U32S_2;
117 case _LINUX_CAPABILITY_VERSION_3:
118 caps->u32s = _LINUX_CAPABILITY_U32S_3;
121 bb_error_msg_and_die("unsupported capability version");
124 if (capget(&caps->header, caps->data) != 0)
125 bb_simple_perror_msg_and_die("capget");