mirror of
https://github.com/systemd/systemd
synced 2024-10-14 20:17:52 +00:00
Merge pull request #31061 from yuwata/pidref_copy
pidref: add basic tests for PidRef, and split out pidref_copy() from pidref_dup()
This commit is contained in:
commit
9ebd1153ef
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -135,6 +135,7 @@ simple_tests += files(
|
|||
'test-path-lookup.c',
|
||||
'test-path-util.c',
|
||||
'test-percent-util.c',
|
||||
'test-pidref.c',
|
||||
'test-pretty-print.c',
|
||||
'test-prioq.c',
|
||||
'test-proc-cmdline.c',
|
||||
|
|
225
src/test/test-pidref.c
Normal file
225
src/test/test-pidref.c
Normal file
|
@ -0,0 +1,225 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "fd-util.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST(pidref_is_set) {
|
||||
assert_se(!pidref_is_set(NULL));
|
||||
assert_se(!pidref_is_set(&PIDREF_NULL));
|
||||
assert_se(pidref_is_set(&PIDREF_MAKE_FROM_PID(1)));
|
||||
}
|
||||
|
||||
TEST(pidref_equal) {
|
||||
assert_se(pidref_equal(NULL, NULL));
|
||||
assert_se(pidref_equal(NULL, &PIDREF_NULL));
|
||||
assert_se(pidref_equal(&PIDREF_NULL, NULL));
|
||||
assert_se(pidref_equal(&PIDREF_NULL, &PIDREF_NULL));
|
||||
|
||||
assert_se(!pidref_equal(NULL, &PIDREF_MAKE_FROM_PID(1)));
|
||||
assert_se(!pidref_equal(&PIDREF_MAKE_FROM_PID(1), NULL));
|
||||
assert_se(!pidref_equal(&PIDREF_NULL, &PIDREF_MAKE_FROM_PID(1)));
|
||||
assert_se(!pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_NULL));
|
||||
assert_se(pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_MAKE_FROM_PID(1)));
|
||||
assert_se(!pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_MAKE_FROM_PID(2)));
|
||||
}
|
||||
|
||||
TEST(pidref_set_pid) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
int r;
|
||||
|
||||
r = pidref_set_pid(&pidref, 1);
|
||||
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)));
|
||||
assert_se(!pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(2)));
|
||||
}
|
||||
|
||||
TEST(pidref_set_self) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
|
||||
assert_se(pidref_set_self(&pidref) >= 0);
|
||||
assert_se(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached())));
|
||||
assert_se(!pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
|
||||
}
|
||||
|
||||
TEST(pidref_set_pidstr) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
char buf[DECIMAL_STR_MAX(pid_t)];
|
||||
|
||||
xsprintf(buf, PID_FMT, getpid_cached());
|
||||
assert_se(pidref_set_pidstr(&pidref, buf) >= 0);
|
||||
assert_se(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached())));
|
||||
assert_se(!pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
|
||||
}
|
||||
|
||||
TEST(pidref_set_pidfd) {
|
||||
_cleanup_(pidref_done) PidRef a = PIDREF_NULL, b = PIDREF_NULL, c = PIDREF_NULL, d = PIDREF_NULL;
|
||||
|
||||
assert_se(pidref_set_self(&a) >= 0);
|
||||
if (a.fd < 0)
|
||||
return (void) log_tests_skipped("PIDFD not supported");
|
||||
|
||||
assert_se(pidref_set_pidfd(&b, a.fd) >= 0);
|
||||
assert_se(pidref_equal(&a, &b));
|
||||
assert_se(pidref_set_pidfd_take(&c, b.fd) >= 0);
|
||||
b.fd = -EBADF;
|
||||
assert_se(pidref_equal(&a, &c));
|
||||
assert_se(pidref_set_pidfd_consume(&d, TAKE_FD(c.fd)) >= 0);
|
||||
assert_se(pidref_equal(&a, &d));
|
||||
}
|
||||
|
||||
TEST(pidref_is_self) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
|
||||
assert_se(pidref_set_self(&pidref) >= 0);
|
||||
assert_se(pidref_is_self(&pidref));
|
||||
|
||||
assert_se(!pidref_is_self(NULL));
|
||||
assert_se(!pidref_is_self(&PIDREF_NULL));
|
||||
assert_se(pidref_is_self(&PIDREF_MAKE_FROM_PID(getpid_cached())));
|
||||
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;
|
||||
|
||||
assert_se(pidref_dup(NULL, &pidref) >= 0);
|
||||
assert_se(pidref);
|
||||
assert_se(!pidref_is_set(pidref));
|
||||
pidref = pidref_free(pidref);
|
||||
|
||||
assert_se(pidref_dup(&PIDREF_NULL, &pidref) >= 0);
|
||||
assert_se(pidref);
|
||||
assert_se(!pidref_is_set(pidref));
|
||||
pidref = pidref_free(pidref);
|
||||
|
||||
assert_se(pidref_dup(&PIDREF_MAKE_FROM_PID(getpid_cached()), &pidref) >= 0);
|
||||
assert_se(pidref_is_self(pidref));
|
||||
pidref = pidref_free(pidref);
|
||||
|
||||
r = pidref_dup(&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_new_from_pid) {
|
||||
_cleanup_(pidref_freep) PidRef *pidref = NULL;
|
||||
int r;
|
||||
|
||||
assert_se(pidref_new_from_pid(-1, &pidref) == -ESRCH);
|
||||
assert_se(!pidref);
|
||||
|
||||
assert_se(pidref_new_from_pid(0, &pidref) >= 0);
|
||||
assert_se(pidref_is_self(pidref));
|
||||
pidref = pidref_free(pidref);
|
||||
|
||||
assert_se(pidref_new_from_pid(getpid_cached(), &pidref) >= 0);
|
||||
assert_se(pidref_is_self(pidref));
|
||||
pidref = pidref_free(pidref);
|
||||
|
||||
r = pidref_new_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_kill) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
siginfo_t si;
|
||||
int r;
|
||||
|
||||
r = pidref_safe_fork("(test-pidref-kill)", FORK_DEATHSIG_SIGKILL, &pidref);
|
||||
assert_se(r >= 0);
|
||||
if (r == 0)
|
||||
freeze();
|
||||
|
||||
assert_se(pidref_kill(&pidref, SIGKILL) >= 0);
|
||||
assert_se(pidref_wait_for_terminate(&pidref, &si) >= 0);
|
||||
assert_se(si.si_signo == SIGCHLD);
|
||||
}
|
||||
|
||||
TEST(pidref_kill_and_sigcont) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
siginfo_t si;
|
||||
int r;
|
||||
|
||||
r = pidref_safe_fork("(test-pidref-kill-and-sigcont)", FORK_DEATHSIG_SIGTERM, &pidref);
|
||||
assert_se(r >= 0);
|
||||
if (r == 0)
|
||||
freeze();
|
||||
|
||||
assert_se(pidref_kill_and_sigcont(&pidref, SIGTERM) >= 0);
|
||||
assert_se(pidref_wait_for_terminate(&pidref, &si) >= 0);
|
||||
assert_se(si.si_signo == SIGCHLD);
|
||||
}
|
||||
|
||||
TEST(pidref_sigqueue) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
siginfo_t si;
|
||||
int r;
|
||||
|
||||
r = pidref_safe_fork("(test-pidref-sigqueue)", FORK_DEATHSIG_SIGTERM, &pidref);
|
||||
assert_se(r >= 0);
|
||||
if (r == 0)
|
||||
freeze();
|
||||
|
||||
assert_se(pidref_sigqueue(&pidref, SIGTERM, 42) >= 0);
|
||||
assert_se(pidref_wait_for_terminate(&pidref, &si) >= 0);
|
||||
assert_se(si.si_signo == SIGCHLD);
|
||||
}
|
||||
|
||||
TEST(pidref_done_sigkill_wait) {
|
||||
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
|
||||
int r;
|
||||
|
||||
r = pidref_safe_fork("(test-pidref-done-sigkill-wait)", FORK_DEATHSIG_SIGKILL, &pidref);
|
||||
assert_se(r >= 0);
|
||||
if (r == 0)
|
||||
freeze();
|
||||
}
|
||||
|
||||
TEST(pidref_verify) {
|
||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||
|
||||
assert_se(pidref_verify(NULL) == -ESRCH);
|
||||
assert_se(pidref_verify(&PIDREF_NULL) == -ESRCH);
|
||||
|
||||
assert_se(pidref_verify(&PIDREF_MAKE_FROM_PID(1)) == 1);
|
||||
assert_se(pidref_verify(&PIDREF_MAKE_FROM_PID(getpid_cached())) == 0);
|
||||
|
||||
assert_se(pidref_set_self(&pidref) >= 0);
|
||||
assert_se(pidref_verify(&pidref) == (pidref.fd >= 0));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
Loading…
Reference in a new issue