]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: add pidref-based version of wait_for_terminate_and_check()
authorLennart Poettering <lennart@poettering.net>
Sun, 23 Mar 2025 22:35:36 +0000 (18:35 -0400)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Apr 2025 15:11:27 +0000 (00:11 +0900)
src/basic/process-util.c
src/basic/process-util.h

index ee15f72df4c76a6b54ea37dfadda7858adb6c8f2..df547ccada3fac0f15c1e71a590bed7f83027deb 100644 (file)
@@ -916,24 +916,29 @@ int wait_for_terminate(pid_t pid, siginfo_t *status) {
  * A warning is emitted if the process terminates abnormally,
  * and also if it returns non-zero unless check_exit_code is true.
  */
-int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
-        _cleanup_free_ char *buffer = NULL;
-        siginfo_t status;
-        int r, prio;
+int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags) {
+        int r;
 
-        assert(pid > 1);
+        if (!pidref_is_set(pidref))
+                return -ESRCH;
+        if (pidref_is_remote(pidref))
+                return -EREMOTE;
+        if (pidref->pid == 1 || pidref_is_self(pidref))
+                return -ECHILD;
 
+        _cleanup_free_ char *buffer = NULL;
         if (!name) {
-                r = pid_get_comm(pid, &buffer);
+                r = pidref_get_comm(pidref, &buffer);
                 if (r < 0)
-                        log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pid);
+                        log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pidref->pid);
                 else
                         name = buffer;
         }
 
-        prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
+        int prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
 
-        r = wait_for_terminate(pid, &status);
+        siginfo_t status;
+        r = pidref_wait_for_terminate(pidref, &status);
         if (r < 0)
                 return log_full_errno(prio, r, "Failed to wait for %s: %m", strna(name));
 
@@ -956,6 +961,10 @@ int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
         return -EPROTO;
 }
 
+int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
+        return pidref_wait_for_terminate_and_check(name, &PIDREF_MAKE_FROM_PID(pid), flags);
+}
+
 /*
  * Return values:
  *
index c3b86f064fa7c33940783ceaa37625e9ea3fd7a8..910fc604cd3bf043dc29e708e897dd71802489a9 100644 (file)
@@ -75,7 +75,9 @@ typedef enum WaitFlags {
         WAIT_LOG = WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS,
 } WaitFlags;
 
+int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags);
 int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags);
+
 int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout);
 
 void sigkill_wait(pid_t pid);