Skip to content

Commit 269ed7c

Browse files
committed
test: verify concurrent lifetime lock queries retain kernel reservation
1 parent d48aa2d commit 269ed7c

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

‎tests/test_daemon_ipc.c‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern const char *cbm_test_program_path;
5252
#include <sys/un.h>
5353
#include <sys/wait.h>
5454
#include <pwd.h>
55+
#include <pthread.h>
5556
#include <unistd.h>
5657
#ifdef __linux__
5758
#include <sched.h> /* #1830 userns real smoke: unshare(CLONE_NEWUSER). */
@@ -3446,6 +3447,126 @@ static uint8_t ipc_test_lock_child_verdict(const char *lock_path) {
34463447
return lock_errno == EACCES || lock_errno == EAGAIN ? IPC_TEST_LOCK_HELD : IPC_TEST_LOCK_ERROR;
34473448
}
34483449

3450+
/* Exercise concurrent same-process queries while a real fcntl lifetime lock
3451+
* remains held. The child verdict below detects a lost kernel lock even when
3452+
* the in-process claim registry still reports ownership. */
3453+
typedef struct {
3454+
const cbm_daemon_ipc_endpoint_t *endpoint;
3455+
bool probe;
3456+
atomic_int failures;
3457+
} ipc_lifetime_concurrent_worker_t;
3458+
3459+
static void *ipc_lifetime_concurrent_worker(void *opaque) {
3460+
ipc_lifetime_concurrent_worker_t *worker = opaque;
3461+
for (int i = 0; i < 2000; ++i) {
3462+
if (worker->probe) {
3463+
if (cbm_daemon_ipc_lifetime_reservation_probe(worker->endpoint) != 1) {
3464+
atomic_fetch_add(&worker->failures, 1);
3465+
}
3466+
} else {
3467+
cbm_daemon_ipc_lifetime_reservation_t *extra = NULL;
3468+
int result = cbm_daemon_ipc_lifetime_reservation_try_acquire(worker->endpoint, &extra);
3469+
if (result != 0 || extra != NULL) {
3470+
atomic_fetch_add(&worker->failures, 1);
3471+
if (extra) {
3472+
cbm_daemon_ipc_lifetime_reservation_release(extra);
3473+
}
3474+
}
3475+
}
3476+
}
3477+
return NULL;
3478+
}
3479+
3480+
TEST(daemon_ipc_posix_lifetime_reservation_concurrent_queries_preserve_kernel_lock) {
3481+
static const char key[] = "e1e1f2f2a3a3b4b4";
3482+
char parent[TEST_PATH_CAP] = {0};
3483+
char runtime_dir[TEST_PATH_CAP] = {0};
3484+
char lock_path[TEST_PATH_CAP] = {0};
3485+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
3486+
cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
3487+
ipc_lifetime_concurrent_worker_t probe_worker = {.probe = true};
3488+
ipc_lifetime_concurrent_worker_t acquire_worker = {.probe = false};
3489+
pthread_t probe_thread, acquire_thread;
3490+
bool probe_started = false, acquire_started = false;
3491+
int acquired = -1, child_status = -1, free_after_release = -1;
3492+
pid_t child = -1;
3493+
uint8_t child_result = IPC_TEST_LOCK_ERROR;
3494+
int result_pipe[2] = {-1, -1};
3495+
bool lock_path_ok = false;
3496+
3497+
atomic_init(&probe_worker.failures, 0);
3498+
atomic_init(&acquire_worker.failures, 0);
3499+
if (ipc_test_parent_new(parent, "concurrent-lifetime-lock")) {
3500+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
3501+
}
3502+
if (endpoint) {
3503+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
3504+
lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
3505+
acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
3506+
}
3507+
if (reservation) {
3508+
probe_worker.endpoint = endpoint;
3509+
acquire_worker.endpoint = endpoint;
3510+
probe_started = pthread_create(&probe_thread, NULL, ipc_lifetime_concurrent_worker,
3511+
&probe_worker) == 0;
3512+
acquire_started = pthread_create(&acquire_thread, NULL, ipc_lifetime_concurrent_worker,
3513+
&acquire_worker) == 0;
3514+
}
3515+
if (probe_started) {
3516+
(void)pthread_join(probe_thread, NULL);
3517+
}
3518+
if (acquire_started) {
3519+
(void)pthread_join(acquire_thread, NULL);
3520+
}
3521+
/* Fork only after joining: no pthread mutex can be held by a vanished
3522+
* sibling thread in the child. */
3523+
if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
3524+
child = fork();
3525+
}
3526+
if (child == 0) {
3527+
(void)close(result_pipe[0]);
3528+
child_result = ipc_test_lock_child_verdict(lock_path);
3529+
bool sent = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
3530+
(void)close(result_pipe[1]);
3531+
_exit(sent ? 0 : 1);
3532+
}
3533+
if (child > 0) {
3534+
(void)close(result_pipe[1]);
3535+
result_pipe[1] = -1;
3536+
bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
3537+
(void)close(result_pipe[0]);
3538+
result_pipe[0] = -1;
3539+
if (!received) {
3540+
child_result = IPC_TEST_LOCK_ERROR;
3541+
}
3542+
while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
3543+
}
3544+
cbm_daemon_ipc_lifetime_reservation_release(reservation);
3545+
if (endpoint) {
3546+
free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3547+
}
3548+
for (size_t i = 0; i < 2; ++i) {
3549+
if (result_pipe[i] >= 0) {
3550+
(void)close(result_pipe[i]);
3551+
}
3552+
}
3553+
cbm_daemon_ipc_endpoint_free(endpoint);
3554+
ipc_test_remove_tree(runtime_dir, parent);
3555+
3556+
ASSERT_EQ(acquired, 1);
3557+
ASSERT_TRUE(lock_path_ok);
3558+
ASSERT_TRUE(probe_started);
3559+
ASSERT_TRUE(acquire_started);
3560+
ASSERT_EQ(atomic_load(&probe_worker.failures), 0);
3561+
ASSERT_EQ(atomic_load(&acquire_worker.failures), 0);
3562+
ASSERT_GT(child, 0);
3563+
ASSERT_TRUE(WIFEXITED(child_status));
3564+
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3565+
ASSERT_EQ(child_result, IPC_TEST_LOCK_HELD);
3566+
ASSERT_EQ(free_after_release, 0);
3567+
PASS();
3568+
}
3569+
34493570
TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
34503571
static const char key[] = "c1c1d2d2e3e3f4f4";
34513572
char parent[TEST_PATH_CAP] = {0};
@@ -6041,6 +6162,7 @@ SUITE(daemon_ipc) {
60416162
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
60426163
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
60436164
RUN_TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock);
6165+
RUN_TEST(daemon_ipc_posix_lifetime_reservation_concurrent_queries_preserve_kernel_lock);
60446166
RUN_TEST(daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock);
60456167
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);
60466168
RUN_TEST(daemon_ipc_posix_publication_boundaries_recover_from_crash);

0 commit comments

Comments
 (0)