174
static int _real_caps_last_cap(void)
179
/* try to get the maximum capability over the kernel
180
* interface introduced in v3.2 */
181
fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY);
187
if ((n = read(fd, buf, 31)) >= 0) {
189
result = strtol(buf, &ptr, 10);
190
if (!ptr || (*ptr != '\0' && *ptr != '\n') ||
191
result == LONG_MIN || result == LONG_MAX)
198
/* try to get it manually by trying to get the status of
199
* each capability indiviually from the kernel */
202
while (prctl(PR_CAPBSET_READ, cap) >= 0) cap++;
209
int lxc_caps_last_cap(void)
211
static int last_cap = -1;
212
if (last_cap < 0) last_cap = _real_caps_last_cap();
218
* check if we have the caps needed to start a container. returns 1 on
219
* success, 0 on error. (I'd prefer this be a bool, but am afraid that
220
* might fail to build on some distros).
222
int lxc_caps_check(void)
224
uid_t uid = getuid();
226
cap_flag_value_t value;
229
cap_value_t needed_caps[] = { CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_SETUID, CAP_SETGID };
231
#define NUMCAPS ((int) (sizeof(needed_caps) / sizeof(cap_t)))
236
caps = cap_get_proc();
238
ERROR("failed to cap_get_proc: %m");
242
for (i=0; i<NUMCAPS; i++) {
243
ret = cap_get_flag(caps, needed_caps[i], CAP_EFFECTIVE, &value);
245
ERROR("Failed to cap_get_flag: %m");