@@ -3373,6 +3373,46 @@ static bool ipc_test_lifetime_lock_path(char out[TEST_PATH_CAP], const char *run
33733373 return written > 0 && written < TEST_PATH_CAP ;
33743374}
33753375
3376+ /* Child-process verdict on whether the lifetime lock at lock_path is held by
3377+ * someone else, going straight through open()/fcntl() rather than any of
3378+ * this codebase's APIs, so a genuinely separate process cannot inherit the
3379+ * parent's in-process claim registry. Distinguishes three outcomes instead
3380+ * of collapsing "held" and "could not even check" into the same value:
3381+ * IPC_TEST_LOCK_HELD only when open() succeeded and F_SETLK failed with
3382+ * EACCES or EAGAIN (the lock is held by another process, as expected);
3383+ * IPC_TEST_LOCK_TAKEN when F_SETLK succeeded (the lock was free, a bug);
3384+ * IPC_TEST_LOCK_ERROR for anything else (open() failed, e.g. a wrong path,
3385+ * or F_SETLK failed with an errno that says nothing about the lock's
3386+ * state), which must never be mistaken for IPC_TEST_LOCK_HELD. */
3387+ enum {
3388+ IPC_TEST_LOCK_HELD = 0 ,
3389+ IPC_TEST_LOCK_TAKEN = 1 ,
3390+ IPC_TEST_LOCK_ERROR = 3 ,
3391+ };
3392+
3393+ static uint8_t ipc_test_lock_child_verdict (const char * lock_path ) {
3394+ int fd = open (lock_path , O_RDWR );
3395+ if (fd < 0 ) {
3396+ return IPC_TEST_LOCK_ERROR ;
3397+ }
3398+ struct flock record_lock = {
3399+ .l_type = F_WRLCK ,
3400+ .l_whence = SEEK_SET ,
3401+ .l_start = 0 ,
3402+ .l_len = 0 ,
3403+ };
3404+ int lock_result ;
3405+ do {
3406+ lock_result = fcntl (fd , F_SETLK , & record_lock );
3407+ } while (lock_result != 0 && errno == EINTR );
3408+ int lock_errno = errno ;
3409+ (void )close (fd );
3410+ if (lock_result == 0 ) {
3411+ return IPC_TEST_LOCK_TAKEN ;
3412+ }
3413+ return lock_errno == EACCES || lock_errno == EAGAIN ? IPC_TEST_LOCK_HELD : IPC_TEST_LOCK_ERROR ;
3414+ }
3415+
33763416TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock ) {
33773417 static const char key [] = "c1c1d2d2e3e3f4f4" ;
33783418 char parent [TEST_PATH_CAP ] = {0 };
@@ -3417,18 +3457,7 @@ TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
34173457 * claim registry: try to take the real OS-level lock directly. If
34183458 * the parent's fcntl lock survived the probes above, this fails. */
34193459 (void )close (result_pipe [0 ]);
3420- int fd = open (lock_path , O_RDWR );
3421- struct flock record_lock = {
3422- .l_type = F_WRLCK ,
3423- .l_whence = SEEK_SET ,
3424- .l_start = 0 ,
3425- .l_len = 0 ,
3426- };
3427- int lock_result = fd >= 0 ? fcntl (fd , F_SETLK , & record_lock ) : -1 ;
3428- child_result = lock_result == 0 ? 1 : 0 ;
3429- if (fd >= 0 ) {
3430- (void )close (fd );
3431- }
3460+ child_result = ipc_test_lock_child_verdict (lock_path );
34323461 bool reported = ipc_test_fd_write_all (result_pipe [1 ], & child_result , sizeof (child_result ));
34333462 (void )close (result_pipe [1 ]);
34343463 _exit (reported ? 0 : 1 );
@@ -3463,7 +3492,91 @@ TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
34633492 ASSERT_GT (child , 0 );
34643493 ASSERT_TRUE (WIFEXITED (child_status ));
34653494 ASSERT_EQ (WEXITSTATUS (child_status ), 0 );
3466- ASSERT_EQ (child_result , 0 );
3495+ ASSERT_EQ (child_result , IPC_TEST_LOCK_HELD );
3496+ ASSERT_EQ (free_after_release , 0 );
3497+ PASS ();
3498+ }
3499+
3500+ TEST (daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock ) {
3501+ static const char key [] = "a1a1b2b2c3c3d4d4" ;
3502+ char parent [TEST_PATH_CAP ] = {0 };
3503+ char runtime_dir [TEST_PATH_CAP ] = {0 };
3504+ char lock_path [TEST_PATH_CAP ] = {0 };
3505+ cbm_daemon_ipc_endpoint_t * endpoint = NULL ;
3506+ cbm_daemon_ipc_lifetime_reservation_t * reservation = NULL ;
3507+ cbm_daemon_ipc_lifetime_reservation_t * second_reservation =
3508+ (cbm_daemon_ipc_lifetime_reservation_t * )(uintptr_t )1 ;
3509+ int result_pipe [2 ] = {-1 , -1 };
3510+ pid_t child = -1 ;
3511+ uint8_t child_result = 0 ;
3512+ int child_status = -1 ;
3513+ int acquired = -1 ;
3514+ int reacquired = -1 ;
3515+ int free_after_release = -1 ;
3516+ bool lock_path_ok = false;
3517+
3518+ if (ipc_test_parent_new (parent , "try-acquire-again-holds-lock" )) {
3519+ endpoint = cbm_daemon_ipc_endpoint_new (key , parent );
3520+ }
3521+ if (endpoint ) {
3522+ ipc_test_copy_path (runtime_dir , cbm_daemon_ipc_endpoint_runtime_dir (endpoint ));
3523+ lock_path_ok = ipc_test_lifetime_lock_path (lock_path , runtime_dir , key );
3524+ acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & reservation );
3525+ }
3526+ if (reservation ) {
3527+ /* This process already holds the lifetime lock. Calling try_acquire
3528+ * again, still in this process, must trust the in-process registry
3529+ * alone and return 0 (already held, nothing new to hand back)
3530+ * without touching the lock file: opening and closing a second fd
3531+ * on it would drop the fcntl(2) record lock this process already
3532+ * holds, exactly the way the probe's own fast path used to. */
3533+ reacquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & second_reservation );
3534+ }
3535+ if (reservation && lock_path_ok && pipe (result_pipe ) == 0 ) {
3536+ child = fork ();
3537+ }
3538+ if (child == 0 ) {
3539+ /* A genuinely independent process, not going through any of this
3540+ * codebase's APIs so it cannot inherit the parent's in-process
3541+ * claim registry: try to take the real OS-level lock directly. If
3542+ * the repeated try_acquire above survived, this fails. */
3543+ (void )close (result_pipe [0 ]);
3544+ child_result = ipc_test_lock_child_verdict (lock_path );
3545+ bool reported = ipc_test_fd_write_all (result_pipe [1 ], & child_result , sizeof (child_result ));
3546+ (void )close (result_pipe [1 ]);
3547+ _exit (reported ? 0 : 1 );
3548+ }
3549+ if (child > 0 ) {
3550+ (void )close (result_pipe [1 ]);
3551+ result_pipe [1 ] = -1 ;
3552+ bool received = ipc_test_fd_read_all (result_pipe [0 ], & child_result , sizeof (child_result ));
3553+ (void )close (result_pipe [0 ]);
3554+ result_pipe [0 ] = -1 ;
3555+ if (!received ) {
3556+ child_result = 2 ;
3557+ }
3558+ while (waitpid (child , & child_status , 0 ) < 0 && errno == EINTR ) {}
3559+ }
3560+ cbm_daemon_ipc_lifetime_reservation_release (reservation );
3561+ if (endpoint ) {
3562+ free_after_release = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3563+ }
3564+ for (size_t index = 0 ; index < 2 ; index ++ ) {
3565+ if (result_pipe [index ] >= 0 ) {
3566+ (void )close (result_pipe [index ]);
3567+ }
3568+ }
3569+ cbm_daemon_ipc_endpoint_free (endpoint );
3570+ ipc_test_remove_tree (runtime_dir , parent );
3571+
3572+ ASSERT_EQ (acquired , 1 );
3573+ ASSERT_TRUE (lock_path_ok );
3574+ ASSERT_EQ (reacquired , 0 );
3575+ ASSERT_TRUE (second_reservation == NULL );
3576+ ASSERT_GT (child , 0 );
3577+ ASSERT_TRUE (WIFEXITED (child_status ));
3578+ ASSERT_EQ (WEXITSTATUS (child_status ), 0 );
3579+ ASSERT_EQ (child_result , IPC_TEST_LOCK_HELD );
34673580 ASSERT_EQ (free_after_release , 0 );
34683581 PASS ();
34693582}
@@ -5628,6 +5741,7 @@ SUITE(daemon_ipc) {
56285741 RUN_TEST (daemon_ipc_posix_startup_lock_is_cross_process );
56295742 RUN_TEST (daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance );
56305743 RUN_TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock );
5744+ RUN_TEST (daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock );
56315745 RUN_TEST (daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge );
56325746 RUN_TEST (daemon_ipc_posix_publication_boundaries_recover_from_crash );
56335747 RUN_TEST (daemon_ipc_posix_record_publication_windows_recover_from_crash );
0 commit comments