LCOV - code coverage report
Current view: top level - test - test-cgroup-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 315 335 94.0 %
Date: 2019-08-22 15:41:25 Functions: 31 31 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include "alloc-util.h"
       4             : #include "build.h"
       5             : #include "cgroup-util.h"
       6             : #include "dirent-util.h"
       7             : #include "fd-util.h"
       8             : #include "format-util.h"
       9             : #include "parse-util.h"
      10             : #include "proc-cmdline.h"
      11             : #include "process-util.h"
      12             : #include "special.h"
      13             : #include "stat-util.h"
      14             : #include "string-util.h"
      15             : #include "strv.h"
      16             : #include "test-helper.h"
      17             : #include "tests.h"
      18             : #include "user-util.h"
      19             : #include "util.h"
      20             : 
      21           9 : static void check_p_d_u(const char *path, int code, const char *result) {
      22           9 :         _cleanup_free_ char *unit = NULL;
      23             :         int r;
      24             : 
      25           9 :         r = cg_path_decode_unit(path, &unit);
      26           9 :         printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
      27           9 :         assert_se(r == code);
      28           9 :         assert_se(streq_ptr(unit, result));
      29           9 : }
      30             : 
      31           1 : static void test_path_decode_unit(void) {
      32           1 :         check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
      33           1 :         check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
      34           1 :         check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
      35           1 :         check_p_d_u("getty@.service/", -ENXIO, NULL);
      36           1 :         check_p_d_u("getty@.service", -ENXIO, NULL);
      37           1 :         check_p_d_u("getty.service", 0, "getty.service");
      38           1 :         check_p_d_u("getty", -ENXIO, NULL);
      39           1 :         check_p_d_u("getty/waldo", -ENXIO, NULL);
      40           1 :         check_p_d_u("_cpu.service", 0, "cpu.service");
      41           1 : }
      42             : 
      43          11 : static void check_p_g_u(const char *path, int code, const char *result) {
      44          11 :         _cleanup_free_ char *unit = NULL;
      45             :         int r;
      46             : 
      47          11 :         r = cg_path_get_unit(path, &unit);
      48          11 :         printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
      49          11 :         assert_se(r == code);
      50          11 :         assert_se(streq_ptr(unit, result));
      51          11 : }
      52             : 
      53           1 : static void test_path_get_unit(void) {
      54           1 :         check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
      55           1 :         check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
      56           1 :         check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
      57           1 :         check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
      58           1 :         check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
      59           1 :         check_p_g_u("sadfdsafsda", -ENXIO, NULL);
      60           1 :         check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL);
      61           1 :         check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
      62           1 :         check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
      63           1 :         check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
      64           1 :         check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
      65           1 : }
      66             : 
      67          13 : static void check_p_g_u_u(const char *path, int code, const char *result) {
      68          13 :         _cleanup_free_ char *unit = NULL;
      69             :         int r;
      70             : 
      71          13 :         r = cg_path_get_user_unit(path, &unit);
      72          13 :         printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
      73          13 :         assert_se(r == code);
      74          13 :         assert_se(streq_ptr(unit, result));
      75          13 : }
      76             : 
      77           1 : static void test_path_get_user_unit(void) {
      78           1 :         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
      79           1 :         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
      80           1 :         check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
      81           1 :         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
      82           1 :         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL);
      83           1 :         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
      84           1 :         check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
      85           1 :         check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
      86           1 :         check_p_g_u_u("/meh.service", -ENXIO, NULL);
      87           1 :         check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
      88           1 :         check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
      89           1 :         check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
      90           1 :         check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
      91           1 : }
      92             : 
      93           4 : static void check_p_g_s(const char *path, int code, const char *result) {
      94           4 :         _cleanup_free_ char *s = NULL;
      95             : 
      96           4 :         assert_se(cg_path_get_session(path, &s) == code);
      97           4 :         assert_se(streq_ptr(s, result));
      98           4 : }
      99             : 
     100           1 : static void test_path_get_session(void) {
     101           1 :         check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
     102           1 :         check_p_g_s("/session-3.scope", 0, "3");
     103           1 :         check_p_g_s("/session-.scope", -ENXIO, NULL);
     104           1 :         check_p_g_s("", -ENXIO, NULL);
     105           1 : }
     106             : 
     107           3 : static void check_p_g_o_u(const char *path, int code, uid_t result) {
     108           3 :         uid_t uid = 0;
     109             : 
     110           3 :         assert_se(cg_path_get_owner_uid(path, &uid) == code);
     111           3 :         assert_se(uid == result);
     112           3 : }
     113             : 
     114           1 : static void test_path_get_owner_uid(void) {
     115           1 :         check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
     116           1 :         check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
     117           1 :         check_p_g_o_u("", -ENXIO, 0);
     118           1 : }
     119             : 
     120           7 : static void check_p_g_slice(const char *path, int code, const char *result) {
     121           7 :         _cleanup_free_ char *s = NULL;
     122             : 
     123           7 :         assert_se(cg_path_get_slice(path, &s) == code);
     124           7 :         assert_se(streq_ptr(s, result));
     125           7 : }
     126             : 
     127           1 : static void test_path_get_slice(void) {
     128           1 :         check_p_g_slice("/user.slice", 0, "user.slice");
     129           1 :         check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE);
     130           1 :         check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
     131           1 :         check_p_g_slice("", 0, SPECIAL_ROOT_SLICE);
     132           1 :         check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE);
     133           1 :         check_p_g_slice("foobar.slice", 0, "foobar.slice");
     134           1 :         check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
     135           1 : }
     136             : 
     137          13 : static void check_p_g_u_slice(const char *path, int code, const char *result) {
     138          13 :         _cleanup_free_ char *s = NULL;
     139             : 
     140          13 :         assert_se(cg_path_get_user_slice(path, &s) == code);
     141          13 :         assert_se(streq_ptr(s, result));
     142          13 : }
     143             : 
     144           1 : static void test_path_get_user_slice(void) {
     145           1 :         check_p_g_u_slice("/user.slice", -ENXIO, NULL);
     146           1 :         check_p_g_u_slice("/foobar", -ENXIO, NULL);
     147           1 :         check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL);
     148           1 :         check_p_g_u_slice("", -ENXIO, NULL);
     149           1 :         check_p_g_u_slice("foobar", -ENXIO, NULL);
     150           1 :         check_p_g_u_slice("foobar.slice", -ENXIO, NULL);
     151           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL);
     152             : 
     153           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, SPECIAL_ROOT_SLICE);
     154           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, SPECIAL_ROOT_SLICE);
     155           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, SPECIAL_ROOT_SLICE);
     156           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE);
     157           1 :         check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
     158           1 :         check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice");
     159           1 : }
     160             : 
     161           1 : static void test_get_paths(void) {
     162           2 :         _cleanup_free_ char *a = NULL;
     163             : 
     164           1 :         assert_se(cg_get_root_path(&a) >= 0);
     165           1 :         log_info("Root = %s", a);
     166           1 : }
     167             : 
     168           1 : static void test_proc(void) {
     169           1 :         _cleanup_closedir_ DIR *d = NULL;
     170             :         struct dirent *de;
     171             :         int r;
     172             : 
     173           1 :         d = opendir("/proc");
     174           1 :         assert_se(d);
     175             : 
     176         332 :         FOREACH_DIRENT(de, d, break) {
     177        1271 :                 _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
     178             :                 pid_t pid;
     179         329 :                 uid_t uid = UID_INVALID;
     180             : 
     181         329 :                 if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
     182          50 :                         continue;
     183             : 
     184         279 :                 r = parse_pid(de->d_name, &pid);
     185         279 :                 if (r < 0)
     186          11 :                         continue;
     187             : 
     188         268 :                 if (is_kernel_thread(pid))
     189          96 :                         continue;
     190             : 
     191         172 :                 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
     192         172 :                 cg_pid_get_path_shifted(pid, NULL, &path_shifted);
     193         172 :                 cg_pid_get_owner_uid(pid, &uid);
     194         172 :                 cg_pid_get_session(pid, &session);
     195         172 :                 cg_pid_get_unit(pid, &unit);
     196         172 :                 cg_pid_get_user_unit(pid, &user_unit);
     197         172 :                 cg_pid_get_machine_name(pid, &machine);
     198         172 :                 cg_pid_get_slice(pid, &slice);
     199             : 
     200         172 :                 printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
     201             :                        pid,
     202             :                        path,
     203             :                        path_shifted,
     204             :                        uid,
     205             :                        session,
     206             :                        unit,
     207             :                        user_unit,
     208             :                        machine,
     209             :                        slice);
     210             :         }
     211           1 : }
     212             : 
     213          10 : static void test_escape_one(const char *s, const char *r) {
     214          10 :         _cleanup_free_ char *b;
     215             : 
     216          10 :         b = cg_escape(s);
     217          10 :         assert_se(b);
     218          10 :         assert_se(streq(b, r));
     219             : 
     220          10 :         assert_se(streq(cg_unescape(b), s));
     221          10 : }
     222             : 
     223           1 : static void test_escape(void) {
     224           1 :         test_escape_one("foobar", "foobar");
     225           1 :         test_escape_one(".foobar", "_.foobar");
     226           1 :         test_escape_one("foobar.service", "foobar.service");
     227           1 :         test_escape_one("cgroup.service", "_cgroup.service");
     228           1 :         test_escape_one("tasks", "_tasks");
     229           1 :         if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
     230           1 :                 test_escape_one("cpu.service", "_cpu.service");
     231           1 :         test_escape_one("_foobar", "__foobar");
     232           1 :         test_escape_one("", "_");
     233           1 :         test_escape_one("_", "__");
     234           1 :         test_escape_one(".", "_.");
     235           1 : }
     236             : 
     237           1 : static void test_controller_is_valid(void) {
     238           1 :         assert_se(cg_controller_is_valid("foobar"));
     239           1 :         assert_se(cg_controller_is_valid("foo_bar"));
     240           1 :         assert_se(cg_controller_is_valid("name=foo"));
     241           1 :         assert_se(!cg_controller_is_valid(""));
     242           1 :         assert_se(!cg_controller_is_valid("name="));
     243           1 :         assert_se(!cg_controller_is_valid("="));
     244           1 :         assert_se(!cg_controller_is_valid("cpu,cpuacct"));
     245           1 :         assert_se(!cg_controller_is_valid("_"));
     246           1 :         assert_se(!cg_controller_is_valid("_foobar"));
     247           1 :         assert_se(!cg_controller_is_valid("tatü"));
     248           1 : }
     249             : 
     250          21 : static void test_slice_to_path_one(const char *unit, const char *path, int error) {
     251          21 :         _cleanup_free_ char *ret = NULL;
     252             :         int r;
     253             : 
     254          21 :         log_info("unit: %s", unit);
     255             : 
     256          21 :         r = cg_slice_to_path(unit, &ret);
     257          21 :         log_info("actual: %s / %d", strnull(ret), r);
     258          21 :         log_info("expect: %s / %d", strnull(path), error);
     259          21 :         assert_se(r == error);
     260          21 :         assert_se(streq_ptr(ret, path));
     261          21 : }
     262             : 
     263           1 : static void test_slice_to_path(void) {
     264           1 :         test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
     265           1 :         test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
     266           1 :         test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
     267           1 :         test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0);
     268           1 :         test_slice_to_path_one("--.slice", NULL, -EINVAL);
     269           1 :         test_slice_to_path_one("-", NULL, -EINVAL);
     270           1 :         test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
     271           1 :         test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
     272           1 :         test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
     273           1 :         test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
     274           1 :         test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
     275           1 :         test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
     276           1 :         test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0);
     277             : 
     278           1 :         test_slice_to_path_one("foobar@.slice", NULL, -EINVAL);
     279           1 :         test_slice_to_path_one("foobar@waldo.slice", NULL, -EINVAL);
     280           1 :         test_slice_to_path_one("foobar@waldo.service", NULL, -EINVAL);
     281           1 :         test_slice_to_path_one("-foo@-.slice", NULL, -EINVAL);
     282           1 :         test_slice_to_path_one("-foo@.slice", NULL, -EINVAL);
     283           1 :         test_slice_to_path_one("foo@-.slice", NULL, -EINVAL);
     284           1 :         test_slice_to_path_one("foo@@bar.slice", NULL, -EINVAL);
     285           1 :         test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL, -EINVAL);
     286           1 : }
     287             : 
     288           4 : static void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
     289           4 :         const char *s = NULL;
     290             : 
     291           4 :         assert_se(cg_shift_path(raw, root, &s) >= 0);
     292           4 :         assert_se(streq(s, shifted));
     293           4 : }
     294             : 
     295           1 : static void test_shift_path(void) {
     296             : 
     297           1 :         test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo");
     298           1 :         test_shift_path_one("/foobar/waldo", "", "/foobar/waldo");
     299           1 :         test_shift_path_one("/foobar/waldo", "/foobar", "/waldo");
     300           1 :         test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo");
     301           1 : }
     302             : 
     303           1 : static void test_mask_supported(void) {
     304             : 
     305             :         CGroupMask m;
     306             :         CGroupController c;
     307             : 
     308           1 :         assert_se(cg_mask_supported(&m) >= 0);
     309             : 
     310          10 :         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
     311           9 :                 printf("'%s' is supported: %s\n", cgroup_controller_to_string(c), yes_no(m & CGROUP_CONTROLLER_TO_MASK(c)));
     312           1 : }
     313             : 
     314           1 : static void test_is_cgroup_fs(void) {
     315             :         struct statfs sfs;
     316           1 :         assert_se(statfs("/sys/fs/cgroup", &sfs) == 0);
     317           1 :         if (is_temporary_fs(&sfs))
     318           1 :                 assert_se(statfs("/sys/fs/cgroup/systemd", &sfs) == 0);
     319           1 :         assert_se(is_cgroup_fs(&sfs));
     320           1 : }
     321             : 
     322           1 : static void test_fd_is_cgroup_fs(void) {
     323             :         int fd;
     324             : 
     325           1 :         fd = open("/sys/fs/cgroup", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
     326           1 :         assert_se(fd >= 0);
     327           1 :         if (fd_is_temporary_fs(fd)) {
     328           1 :                 fd = safe_close(fd);
     329           1 :                 fd = open("/sys/fs/cgroup/systemd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
     330           1 :                 assert_se(fd >= 0);
     331             :         }
     332           1 :         assert_se(fd_is_cgroup_fs(fd));
     333           1 :         fd = safe_close(fd);
     334           1 : }
     335             : 
     336           8 : static void test_is_wanted_print(bool header) {
     337          16 :         _cleanup_free_ char *cmdline = NULL;
     338             : 
     339           8 :         log_info("-- %s --", __func__);
     340           8 :         assert_se(proc_cmdline(&cmdline) >= 0);
     341           8 :         log_info("cmdline: %s", cmdline);
     342           8 :         if (header) {
     343             : 
     344           1 :                 log_info(_CGROUP_HIERARCHY_);
     345           1 :                 (void) system("findmnt -n /sys/fs/cgroup");
     346             :         }
     347             : 
     348           8 :         log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted()));
     349           8 :         log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted()));
     350           8 :         log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted()));
     351           8 :         log_info(" ");
     352           8 : }
     353             : 
     354           1 : static void test_is_wanted(void) {
     355           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     356             :                          "systemd.unified_cgroup_hierarchy", 1) >= 0);
     357           1 :         test_is_wanted_print(false);
     358             : 
     359           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     360             :                          "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
     361           1 :         test_is_wanted_print(false);
     362             : 
     363           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     364             :                          "systemd.unified_cgroup_hierarchy=0 "
     365             :                          "systemd.legacy_systemd_cgroup_controller", 1) >= 0);
     366           1 :         test_is_wanted_print(false);
     367             : 
     368           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     369             :                          "systemd.unified_cgroup_hierarchy=0 "
     370             :                          "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
     371           1 :         test_is_wanted_print(false);
     372             : 
     373             :         /* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise
     374             :          * explicitly specified. */
     375           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     376             :                          "cgroup_no_v1=all", 1) >= 0);
     377           1 :         test_is_wanted_print(false);
     378             : 
     379           1 :         assert_se(setenv("SYSTEMD_PROC_CMDLINE",
     380             :                          "cgroup_no_v1=all "
     381             :                          "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
     382           1 :         test_is_wanted_print(false);
     383           1 : }
     384             : 
     385           1 : static void test_cg_tests(void) {
     386             :         int all, hybrid, systemd, r;
     387             : 
     388           1 :         r = cg_unified_flush();
     389           1 :         if (r == -ENOMEDIUM) {
     390           0 :                 log_notice_errno(r, "Skipping cg hierarchy tests: %m");
     391           0 :                 return;
     392             :         }
     393           1 :         assert_se(r == 0);
     394             : 
     395           1 :         all = cg_all_unified();
     396           1 :         assert_se(IN_SET(all, 0, 1));
     397             : 
     398           1 :         hybrid = cg_hybrid_unified();
     399           1 :         assert_se(IN_SET(hybrid, 0, 1));
     400             : 
     401           1 :         systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
     402           1 :         assert_se(IN_SET(systemd, 0, 1));
     403             : 
     404           1 :         if (all) {
     405           0 :                 assert_se(systemd);
     406           0 :                 assert_se(!hybrid);
     407             : 
     408           1 :         } else if (hybrid) {
     409           1 :                 assert_se(systemd);
     410           1 :                 assert_se(!all);
     411             : 
     412             :         } else
     413           0 :                 assert_se(!systemd);
     414             : }
     415             : 
     416           1 : static void test_cg_get_keyed_attribute(void) {
     417           1 :         _cleanup_free_ char *val = NULL;
     418           1 :         char *vals3[3] = {}, *vals3a[3] = {};
     419             :         int i, r;
     420             : 
     421           1 :         r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val);
     422           1 :         if (r == -ENOMEDIUM) {
     423           0 :                 log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__);
     424           0 :                 return;
     425             :         }
     426             : 
     427           1 :         assert_se(r == -ENOENT);
     428           1 :         assert_se(val == NULL);
     429             : 
     430           1 :         if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK) < 0) {
     431           1 :                 log_info_errno(errno, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__);
     432           1 :                 return;
     433             :         }
     434             : 
     435           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO);
     436           0 :         assert_se(val == NULL);
     437             : 
     438           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0);
     439           0 :         log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val);
     440             : 
     441           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO);
     442             : 
     443           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == -ENXIO);
     444             : 
     445           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
     446             :                                          STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0);
     447           0 :         log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"",
     448             :                  vals3[0], vals3[1], vals3[2]);
     449             : 
     450           0 :         assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
     451             :                                          STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0);
     452           0 :         log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"",
     453             :                  vals3a[0], vals3a[1], vals3a[2]);
     454             : 
     455           0 :         for (i = 0; i < 3; i++) {
     456           0 :                 free(vals3[i]);
     457           0 :                 free(vals3a[i]);
     458             :         }
     459             : }
     460             : 
     461           1 : int main(void) {
     462           1 :         test_setup_logging(LOG_DEBUG);
     463             : 
     464           1 :         test_path_decode_unit();
     465           1 :         test_path_get_unit();
     466           1 :         test_path_get_user_unit();
     467           1 :         test_path_get_session();
     468           1 :         test_path_get_owner_uid();
     469           1 :         test_path_get_slice();
     470           1 :         test_path_get_user_slice();
     471           1 :         TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
     472           1 :         test_proc();
     473           1 :         TEST_REQ_RUNNING_SYSTEMD(test_escape());
     474           1 :         test_controller_is_valid();
     475           1 :         test_slice_to_path();
     476           1 :         test_shift_path();
     477           1 :         TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
     478           1 :         TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
     479           1 :         TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
     480           1 :         test_is_wanted_print(true);
     481           1 :         test_is_wanted_print(false); /* run twice to test caching */
     482           1 :         test_is_wanted();
     483           1 :         test_cg_tests();
     484           1 :         test_cg_get_keyed_attribute();
     485             : 
     486           1 :         return 0;
     487             : }

Generated by: LCOV version 1.14