]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cap-list: add capability_set_to_strv()
authorLennart Poettering <lennart@poettering.net>
Mon, 20 Feb 2023 10:46:08 +0000 (11:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 22 Feb 2023 22:45:40 +0000 (23:45 +0100)
src/basic/cap-list.c
src/basic/cap-list.h
src/test/test-cap-list.c

index 45a5b371e84677895c4c1fee66300f0b431ef922..b95b9b140eedda1df9619d678656f85b0f792a3e 100644 (file)
@@ -11,6 +11,7 @@
 #include "parse-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
+#include "strv.h"
 
 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
 
@@ -102,6 +103,30 @@ int capability_set_to_string(uint64_t set, char **ret) {
         return 0;
 }
 
+int capability_set_to_strv(uint64_t set, char ***ret) {
+        _cleanup_strv_free_ char **l = NULL;
+        int r;
+
+        assert(ret);
+
+        for (unsigned i = 0; i <= cap_last_cap(); i++) {
+                const char *p;
+
+                if (!FLAGS_SET(set, UINT64_C(1) << i))
+                        continue;
+
+                p = CAPABILITY_TO_STRING(i);
+                assert(p);
+
+                r = strv_extend(&l, p);
+                if (r < 0)
+                        return r;
+        }
+
+        *ret = TAKE_PTR(l);
+        return 0;
+}
+
 int capability_set_from_string(const char *s, uint64_t *ret) {
         uint64_t val = 0;
         bool good = true;
index bddadcbae5f171ba3917ef1b78dca832680fba07..fdf4366de0e11e6d7b3298d91369fd5c08b04172 100644 (file)
@@ -16,4 +16,5 @@ int capability_from_name(const char *name);
 int capability_list_length(void);
 
 int capability_set_to_string(uint64_t set, char **ret);
+int capability_set_to_strv(uint64_t set, char ***ret);
 int capability_set_from_string(const char *s, uint64_t *ret);
index 8df425bdd29f9d21ed27fae82e2bc51ed6430228..42503d294c6a2086745e7b2ff480007aafa65094 100644 (file)
@@ -8,6 +8,7 @@
 #include "capability-util.h"
 #include "parse-util.h"
 #include "string-util.h"
+#include "strv.h"
 #include "tests.h"
 
 /* verify the capability parser */
@@ -99,6 +100,23 @@ TEST(capability_set_from_string) {
         assert_se(c == (UINT64_C(1) << 4) - 1);
 }
 
+static void test_capability_set_to_strv_one(uint64_t m, char **l) {
+        _cleanup_strv_free_ char **b = NULL;
+
+        assert_se(capability_set_to_strv(m, &b) >= 0);
+        assert_se(strv_equal(l, b));
+}
+
+TEST(capability_set_to_strv) {
+        test_capability_set_to_strv_one(0, STRV_MAKE(NULL));
+        test_capability_set_to_strv_one(UINT64_C(1) << CAP_MKNOD, STRV_MAKE("cap_mknod"));
+        test_capability_set_to_strv_one((UINT64_C(1) << CAP_MKNOD) |
+                                        (UINT64_C(1) << CAP_NET_BIND_SERVICE), STRV_MAKE("cap_net_bind_service", "cap_mknod"));
+        test_capability_set_to_strv_one((UINT64_C(1) << CAP_MKNOD) |
+                                        (UINT64_C(1) << CAP_NET_BIND_SERVICE) |
+                                        (UINT64_C(1) << CAP_IPC_OWNER), STRV_MAKE("cap_net_bind_service", "cap_ipc_owner", "cap_mknod"));
+}
+
 static void test_capability_set_to_string_invalid(uint64_t invalid_cap_set) {
         uint64_t c;