@@ -3398,6 +3398,222 @@ TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance) {
33983398 PASS ();
33993399}
34003400
3401+ static bool ipc_test_lifetime_lock_path (char out [TEST_PATH_CAP ], const char * runtime_dir ,
3402+ const char * key ) {
3403+ int written = runtime_dir && key
3404+ ? snprintf (out , TEST_PATH_CAP , "%s/cbm-%s.lifetime.lock" , runtime_dir , key )
3405+ : -1 ;
3406+ return written > 0 && written < TEST_PATH_CAP ;
3407+ }
3408+
3409+ /* Child-process verdict on whether the lifetime lock at lock_path is held by
3410+ * someone else, going straight through open()/fcntl() rather than any of
3411+ * this codebase's APIs, so a genuinely separate process cannot inherit the
3412+ * parent's in-process claim registry. Distinguishes three outcomes instead
3413+ * of collapsing "held" and "could not even check" into the same value:
3414+ * IPC_TEST_LOCK_HELD only when open() succeeded and F_SETLK failed with
3415+ * EACCES or EAGAIN (the lock is held by another process, as expected);
3416+ * IPC_TEST_LOCK_TAKEN when F_SETLK succeeded (the lock was free, a bug);
3417+ * IPC_TEST_LOCK_ERROR for anything else (open() failed, e.g. a wrong path,
3418+ * or F_SETLK failed with an errno that says nothing about the lock's
3419+ * state), which must never be mistaken for IPC_TEST_LOCK_HELD. */
3420+ enum {
3421+ IPC_TEST_LOCK_HELD = 0 ,
3422+ IPC_TEST_LOCK_TAKEN = 1 ,
3423+ IPC_TEST_LOCK_ERROR = 3 ,
3424+ };
3425+
3426+ static uint8_t ipc_test_lock_child_verdict (const char * lock_path ) {
3427+ int fd = open (lock_path , O_RDWR );
3428+ if (fd < 0 ) {
3429+ return IPC_TEST_LOCK_ERROR ;
3430+ }
3431+ struct flock record_lock = {
3432+ .l_type = F_WRLCK ,
3433+ .l_whence = SEEK_SET ,
3434+ .l_start = 0 ,
3435+ .l_len = 0 ,
3436+ };
3437+ int lock_result ;
3438+ do {
3439+ lock_result = fcntl (fd , F_SETLK , & record_lock );
3440+ } while (lock_result != 0 && errno == EINTR );
3441+ int lock_errno = errno ;
3442+ (void )close (fd );
3443+ if (lock_result == 0 ) {
3444+ return IPC_TEST_LOCK_TAKEN ;
3445+ }
3446+ return lock_errno == EACCES || lock_errno == EAGAIN ? IPC_TEST_LOCK_HELD : IPC_TEST_LOCK_ERROR ;
3447+ }
3448+
3449+ TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock ) {
3450+ static const char key [] = "c1c1d2d2e3e3f4f4" ;
3451+ char parent [TEST_PATH_CAP ] = {0 };
3452+ char runtime_dir [TEST_PATH_CAP ] = {0 };
3453+ char lock_path [TEST_PATH_CAP ] = {0 };
3454+ cbm_daemon_ipc_endpoint_t * endpoint = NULL ;
3455+ cbm_daemon_ipc_lifetime_reservation_t * reservation = NULL ;
3456+ int result_pipe [2 ] = {-1 , -1 };
3457+ pid_t child = -1 ;
3458+ uint8_t child_result = 0 ;
3459+ int child_status = -1 ;
3460+ int acquired = -1 ;
3461+ int held_after_first_probe = -1 ;
3462+ int held_after_second_probe = -1 ;
3463+ int free_after_release = -1 ;
3464+ bool lock_path_ok = false;
3465+
3466+ if (ipc_test_parent_new (parent , "probe-holds-lock" )) {
3467+ endpoint = cbm_daemon_ipc_endpoint_new (key , parent );
3468+ }
3469+ if (endpoint ) {
3470+ ipc_test_copy_path (runtime_dir , cbm_daemon_ipc_endpoint_runtime_dir (endpoint ));
3471+ lock_path_ok = ipc_test_lifetime_lock_path (lock_path , runtime_dir , key );
3472+ acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & reservation );
3473+ }
3474+ if (reservation ) {
3475+ /* Two probes, matching how a real daemon polls its own reservation
3476+ * repeatedly while running. Each probe of an already-held lock used
3477+ * to open a throwaway fd on the lock file and close it. Since
3478+ * fcntl(2) record locks are scoped to (process, inode), that close
3479+ * silently released the real lock every time, regardless of the
3480+ * still-open fd the reservation itself is holding. */
3481+ held_after_first_probe = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3482+ held_after_second_probe = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3483+ }
3484+ if (reservation && lock_path_ok && pipe (result_pipe ) == 0 ) {
3485+ child = fork ();
3486+ }
3487+ if (child == 0 ) {
3488+ /* A genuinely independent process, not going through any of this
3489+ * codebase's APIs so it cannot inherit the parent's in-process
3490+ * claim registry: try to take the real OS-level lock directly. If
3491+ * the parent's fcntl lock survived the probes above, this fails. */
3492+ (void )close (result_pipe [0 ]);
3493+ child_result = ipc_test_lock_child_verdict (lock_path );
3494+ bool reported = ipc_test_fd_write_all (result_pipe [1 ], & child_result , sizeof (child_result ));
3495+ (void )close (result_pipe [1 ]);
3496+ _exit (reported ? 0 : 1 );
3497+ }
3498+ if (child > 0 ) {
3499+ (void )close (result_pipe [1 ]);
3500+ result_pipe [1 ] = -1 ;
3501+ bool received = ipc_test_fd_read_all (result_pipe [0 ], & child_result , sizeof (child_result ));
3502+ (void )close (result_pipe [0 ]);
3503+ result_pipe [0 ] = -1 ;
3504+ if (!received ) {
3505+ child_result = 2 ;
3506+ }
3507+ while (waitpid (child , & child_status , 0 ) < 0 && errno == EINTR ) {}
3508+ }
3509+ cbm_daemon_ipc_lifetime_reservation_release (reservation );
3510+ if (endpoint ) {
3511+ free_after_release = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3512+ }
3513+ for (size_t index = 0 ; index < 2 ; index ++ ) {
3514+ if (result_pipe [index ] >= 0 ) {
3515+ (void )close (result_pipe [index ]);
3516+ }
3517+ }
3518+ cbm_daemon_ipc_endpoint_free (endpoint );
3519+ ipc_test_remove_tree (runtime_dir , parent );
3520+
3521+ ASSERT_EQ (acquired , 1 );
3522+ ASSERT_TRUE (lock_path_ok );
3523+ ASSERT_EQ (held_after_first_probe , 1 );
3524+ ASSERT_EQ (held_after_second_probe , 1 );
3525+ ASSERT_GT (child , 0 );
3526+ ASSERT_TRUE (WIFEXITED (child_status ));
3527+ ASSERT_EQ (WEXITSTATUS (child_status ), 0 );
3528+ ASSERT_EQ (child_result , IPC_TEST_LOCK_HELD );
3529+ ASSERT_EQ (free_after_release , 0 );
3530+ PASS ();
3531+ }
3532+
3533+ TEST (daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock ) {
3534+ static const char key [] = "a1a1b2b2c3c3d4d4" ;
3535+ char parent [TEST_PATH_CAP ] = {0 };
3536+ char runtime_dir [TEST_PATH_CAP ] = {0 };
3537+ char lock_path [TEST_PATH_CAP ] = {0 };
3538+ cbm_daemon_ipc_endpoint_t * endpoint = NULL ;
3539+ cbm_daemon_ipc_lifetime_reservation_t * reservation = NULL ;
3540+ cbm_daemon_ipc_lifetime_reservation_t * second_reservation =
3541+ (cbm_daemon_ipc_lifetime_reservation_t * )(uintptr_t )1 ;
3542+ int result_pipe [2 ] = {-1 , -1 };
3543+ pid_t child = -1 ;
3544+ uint8_t child_result = 0 ;
3545+ int child_status = -1 ;
3546+ int acquired = -1 ;
3547+ int reacquired = -1 ;
3548+ int free_after_release = -1 ;
3549+ bool lock_path_ok = false;
3550+
3551+ if (ipc_test_parent_new (parent , "try-acquire-again-holds-lock" )) {
3552+ endpoint = cbm_daemon_ipc_endpoint_new (key , parent );
3553+ }
3554+ if (endpoint ) {
3555+ ipc_test_copy_path (runtime_dir , cbm_daemon_ipc_endpoint_runtime_dir (endpoint ));
3556+ lock_path_ok = ipc_test_lifetime_lock_path (lock_path , runtime_dir , key );
3557+ acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & reservation );
3558+ }
3559+ if (reservation ) {
3560+ /* This process already holds the lifetime lock. Calling try_acquire
3561+ * again, still in this process, must trust the in-process registry
3562+ * alone and return 0 (already held, nothing new to hand back)
3563+ * without touching the lock file: opening and closing a second fd
3564+ * on it would drop the fcntl(2) record lock this process already
3565+ * holds, exactly the way the probe's own fast path used to. */
3566+ reacquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & second_reservation );
3567+ }
3568+ if (reservation && lock_path_ok && pipe (result_pipe ) == 0 ) {
3569+ child = fork ();
3570+ }
3571+ if (child == 0 ) {
3572+ /* A genuinely independent process, not going through any of this
3573+ * codebase's APIs so it cannot inherit the parent's in-process
3574+ * claim registry: try to take the real OS-level lock directly. If
3575+ * the repeated try_acquire above survived, this fails. */
3576+ (void )close (result_pipe [0 ]);
3577+ child_result = ipc_test_lock_child_verdict (lock_path );
3578+ bool reported = ipc_test_fd_write_all (result_pipe [1 ], & child_result , sizeof (child_result ));
3579+ (void )close (result_pipe [1 ]);
3580+ _exit (reported ? 0 : 1 );
3581+ }
3582+ if (child > 0 ) {
3583+ (void )close (result_pipe [1 ]);
3584+ result_pipe [1 ] = -1 ;
3585+ bool received = ipc_test_fd_read_all (result_pipe [0 ], & child_result , sizeof (child_result ));
3586+ (void )close (result_pipe [0 ]);
3587+ result_pipe [0 ] = -1 ;
3588+ if (!received ) {
3589+ child_result = 2 ;
3590+ }
3591+ while (waitpid (child , & child_status , 0 ) < 0 && errno == EINTR ) {}
3592+ }
3593+ cbm_daemon_ipc_lifetime_reservation_release (reservation );
3594+ if (endpoint ) {
3595+ free_after_release = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3596+ }
3597+ for (size_t index = 0 ; index < 2 ; index ++ ) {
3598+ if (result_pipe [index ] >= 0 ) {
3599+ (void )close (result_pipe [index ]);
3600+ }
3601+ }
3602+ cbm_daemon_ipc_endpoint_free (endpoint );
3603+ ipc_test_remove_tree (runtime_dir , parent );
3604+
3605+ ASSERT_EQ (acquired , 1 );
3606+ ASSERT_TRUE (lock_path_ok );
3607+ ASSERT_EQ (reacquired , 0 );
3608+ ASSERT_TRUE (second_reservation == NULL );
3609+ ASSERT_GT (child , 0 );
3610+ ASSERT_TRUE (WIFEXITED (child_status ));
3611+ ASSERT_EQ (WEXITSTATUS (child_status ), 0 );
3612+ ASSERT_EQ (child_result , IPC_TEST_LOCK_HELD );
3613+ ASSERT_EQ (free_after_release , 0 );
3614+ PASS ();
3615+ }
3616+
34013617TEST (daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge ) {
34023618#ifdef _WIN32
34033619 PASS ();
@@ -5824,6 +6040,8 @@ SUITE(daemon_ipc) {
58246040#endif
58256041 RUN_TEST (daemon_ipc_posix_startup_lock_is_cross_process );
58266042 RUN_TEST (daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance );
6043+ RUN_TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock );
6044+ RUN_TEST (daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock );
58276045 RUN_TEST (daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge );
58286046 RUN_TEST (daemon_ipc_posix_publication_boundaries_recover_from_crash );
58296047 RUN_TEST (daemon_ipc_posix_record_publication_windows_recover_from_crash );
0 commit comments