two pidref tweaks (#35918)

This commit is contained in:
Mike Yuan
2025-01-08 18:58:20 +01:00
committed by GitHub
2 changed files with 19 additions and 29 deletions
+18 -28
View File
@@ -219,37 +219,32 @@ PidRef* pidref_free(PidRef *pidref) {
return mfree(pidref);
}
int pidref_copy(const PidRef *pidref, PidRef *dest) {
_cleanup_close_ int dup_fd = -EBADF;
pid_t dup_pid = 0;
int pidref_copy(const PidRef *pidref, PidRef *ret) {
_cleanup_(pidref_done) PidRef copy = PIDREF_NULL;
/* If NULL is passed we'll generate a PidRef that refers to no process. This makes it easy to
* copy pidref fields that might or might not reference a process yet. */
assert(dest);
assert(ret);
if (pidref) {
if (pidref_is_remote(pidref)) /* Propagate remote flag */
dup_fd = -EREMOTE;
copy.fd = -EREMOTE;
else if (pidref->fd >= 0) {
dup_fd = fcntl(pidref->fd, F_DUPFD_CLOEXEC, 3);
if (dup_fd < 0) {
copy.fd = fcntl(pidref->fd, F_DUPFD_CLOEXEC, 3);
if (copy.fd < 0) {
if (!ERRNO_IS_RESOURCE(errno))
return -errno;
dup_fd = -EBADF;
copy.fd = -EBADF;
}
}
if (pidref->pid > 0)
dup_pid = pidref->pid;
copy.pid = pidref->pid;
copy.fd_id = pidref->fd_id;
}
*dest = (PidRef) {
.fd = TAKE_FD(dup_fd),
.pid = dup_pid,
};
*ret = TAKE_PIDREF(copy);
return 0;
}
@@ -406,22 +401,17 @@ int pidref_wait(const PidRef *pidref, siginfo_t *ret, int options) {
return -ECHILD;
siginfo_t si = {};
if (pidref->fd >= 0) {
if (pidref->fd >= 0)
r = RET_NERRNO(waitid(P_PIDFD, pidref->fd, &si, options));
if (r >= 0) {
if (ret)
*ret = si;
return r;
}
if (r != -EINVAL) /* P_PIDFD was added in kernel 5.4 only */
return r;
}
else
r = RET_NERRNO(waitid(P_PID, pidref->pid, &si, options));
if (r < 0)
return r;
r = RET_NERRNO(waitid(P_PID, pidref->pid, &si, options));
if (r >= 0 && ret)
if (ret)
*ret = si;
return r;
return 0;
}
int pidref_wait_for_terminate(const PidRef *pidref, siginfo_t *ret) {
+1 -1
View File
@@ -83,7 +83,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_copy(const PidRef *pidref, PidRef *ret);
int pidref_dup(const PidRef *pidref, PidRef **ret);
int pidref_new_from_pid(pid_t pid, PidRef **ret);