]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: port tests over to new /proc/ enumeration API
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 11:43:59 +0000 (13:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:49:40 +0000 (14:49 +0200)
src/test/test-cgroup-util.c
src/test/test-process-util.c

index 57facfe632df2cbbb0071d20de610ae344b0a17a..51f52d9a4ef933c1febc7e186370c390d87a40bf 100644 (file)
@@ -194,35 +194,33 @@ TEST(proc) {
         _cleanup_closedir_ DIR *d = NULL;
         int r;
 
-        d = opendir("/proc");
-        assert_se(d);
+        assert_se(proc_dir_open(&d) >= 0);
 
-        FOREACH_DIRENT(de, d, break) {
+        for (;;) {
                 _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
-                pid_t pid;
+                _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
                 uid_t uid = UID_INVALID;
 
-                if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
-                        continue;
+                r = proc_dir_read_pidref(d, &pid);
+                assert_se(r >= 0);
 
-                r = parse_pid(de->d_name, &pid);
-                if (r < 0)
-                        continue;
+                if (r == 0)
+                        break;
 
-                if (pid_is_kernel_thread(pid) != 0)
+                if (pidref_is_kernel_thread(&pid) != 0)
                         continue;
 
-                cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
-                cg_pid_get_path_shifted(pid, NULL, &path_shifted);
-                cg_pid_get_owner_uid(pid, &uid);
-                cg_pid_get_session(pid, &session);
-                cg_pid_get_unit(pid, &unit);
-                cg_pid_get_user_unit(pid, &user_unit);
-                cg_pid_get_machine_name(pid, &machine);
-                cg_pid_get_slice(pid, &slice);
+                cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid.pid, &path);
+                cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted);
+                cg_pid_get_owner_uid(pid.pid, &uid);
+                cg_pid_get_session(pid.pid, &session);
+                cg_pid_get_unit(pid.pid, &unit);
+                cg_pid_get_user_unit(pid.pid, &user_unit);
+                cg_pid_get_machine_name(pid.pid, &machine);
+                cg_pid_get_slice(pid.pid, &slice);
 
                 printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
-                       pid,
+                       pid.pid,
                        path,
                        path_shifted,
                        uid,
index 2ff9f1fad5a1c1d10ae05a703937cb2116556935..524bcceb970e8725386c0de20e9236d85dece64e 100644 (file)
@@ -149,17 +149,18 @@ static void test_pid_get_cmdline_one(pid_t pid) {
 
 TEST(pid_get_cmdline) {
         _cleanup_closedir_ DIR *d = NULL;
+        int r;
 
-        assert_se(d = opendir("/proc"));
+        assert_se(proc_dir_open(&d) >= 0);
 
-        FOREACH_DIRENT(de, d, return) {
+        for (;;) {
                 pid_t pid;
 
-                if (de->d_type != DT_DIR)
-                        continue;
+                r = proc_dir_read(d, &pid);
+                assert_se(r >= 0);
 
-                if (parse_pid(de->d_name, &pid) < 0)
-                        continue;
+                if (r == 0) /* EOF */
+                        break;
 
                 test_pid_get_cmdline_one(pid);
         }