]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: split out pidref_copy() from pidref_dup() 31061/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 23 Jan 2024 13:07:47 +0000 (22:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 23 Jan 2024 13:21:44 +0000 (22:21 +0900)
src/basic/pidref.c
src/basic/pidref.h
src/test/test-pidref.c

index 972853bbd6b99c7ed6cd76c8ccf50ac49e5ee0d4..abe372be2edcaae22f9e0ff628fdbe635e164e9d 100644 (file)
@@ -156,11 +156,11 @@ PidRef *pidref_free(PidRef *pidref) {
         return mfree(pidref);
 }
 
-int pidref_dup(const PidRef *pidref, PidRef **ret) {
+int pidref_copy(const PidRef *pidref, PidRef *dest) {
         _cleanup_close_ int dup_fd = -EBADF;
         pid_t dup_pid = 0;
 
-        assert(ret);
+        assert(dest);
 
         /* Allocates a new PidRef on the heap, making it a copy of the specified pidref. This does not try to
          * acquire a pidfd if we don't have one yet!
@@ -183,15 +183,28 @@ int pidref_dup(const PidRef *pidref, PidRef **ret) {
                         dup_pid = pidref->pid;
         }
 
-        PidRef *dup_pidref = new(PidRef, 1);
-        if (!dup_pidref)
-                return -ENOMEM;
-
-        *dup_pidref = (PidRef) {
+        *dest = (PidRef) {
                 .fd = TAKE_FD(dup_fd),
                 .pid = dup_pid,
         };
 
+        return 0;
+}
+
+int pidref_dup(const PidRef *pidref, PidRef **ret) {
+        _cleanup_(pidref_freep) PidRef *dup_pidref = NULL;
+        int r;
+
+        assert(ret);
+
+        dup_pidref = newdup(PidRef, &PIDREF_NULL, 1);
+        if (!dup_pidref)
+                return -ENOMEM;
+
+        r = pidref_copy(pidref, dup_pidref);
+        if (r < 0)
+                return r;
+
         *ret = TAKE_PTR(dup_pidref);
         return 0;
 }
index 0fbffb332064fb4debf95df57e7bef8ec67ec57c..c440c8b0e034599b4ab981af30af578562e2e823 100644 (file)
@@ -49,6 +49,7 @@ void pidref_done(PidRef *pidref);
 PidRef *pidref_free(PidRef *pidref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(PidRef*, pidref_free);
 
+int pidref_copy(const PidRef *pidref, PidRef *dest);
 int pidref_dup(const PidRef *pidref, PidRef **ret);
 
 int pidref_new_from_pid(pid_t pid, PidRef **ret);
index 1c1586b4fec57df6d55972e4f08fa1b184cbde9b..2c4d894e77fe610c5f32715bc9b60e22e7307c73 100644 (file)
@@ -86,6 +86,27 @@ TEST(pidref_is_self) {
         assert_se(!pidref_is_self(&PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
 }
 
+TEST(pidref_copy) {
+        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
+        int r;
+
+        assert_se(pidref_copy(NULL, &pidref) >= 0);
+        assert_se(!pidref_is_set(&pidref));
+
+        assert_se(pidref_copy(&PIDREF_NULL, &pidref) >= 0);
+        assert_se(!pidref_is_set(&pidref));
+
+        assert_se(pidref_copy(&PIDREF_MAKE_FROM_PID(getpid_cached()), &pidref) >= 0);
+        assert_se(pidref_is_self(&pidref));
+        pidref_done(&pidref);
+
+        r = pidref_copy(&PIDREF_MAKE_FROM_PID(1), &pidref);
+        if (r == -ESRCH)
+                return (void) log_tests_skipped_errno(r, "PID1 does not exist");
+        assert_se(r >= 0);
+        assert_se(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(1)));
+}
+
 TEST(pidref_dup) {
         _cleanup_(pidref_freep) PidRef *pidref = NULL;
         int r;