Skip to content

Commit 39f608e

Browse files
committed
capability: add new ambient_capabilities_supported() helper
This new function reports whether ambient caps are available, and should be quick because the result is cached.
1 parent 6067611 commit 39f608e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/basic/capability-util.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,18 @@ int drop_capability(cap_value_t cv) {
370370

371371
return 0;
372372
}
373+
374+
bool ambient_capabilities_supported(void) {
375+
static int cache = -1;
376+
377+
if (cache >= 0)
378+
return cache;
379+
380+
/* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are
381+
* available. */
382+
383+
cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 ||
384+
!IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS);
385+
386+
return cache;
387+
}

src/basic/capability-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ static inline bool cap_test_all(uint64_t caps) {
5555
m = (UINT64_C(1) << (cap_last_cap() + 1)) - 1;
5656
return (caps & m) == m;
5757
}
58+
59+
bool ambient_capabilities_supported(void);

src/test/test-capability.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ int main(int argc, char *argv[]) {
205205
log_parse_environment();
206206
log_open();
207207

208+
log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported()));
209+
208210
if (getuid() != 0)
209211
return EXIT_TEST_SKIP;
210212

0 commit comments

Comments
 (0)