@@ -3365,6 +3365,109 @@ TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance) {
33653365 PASS ();
33663366}
33673367
3368+ static bool ipc_test_lifetime_lock_path (char out [TEST_PATH_CAP ], const char * runtime_dir ,
3369+ const char * key ) {
3370+ int written = runtime_dir && key
3371+ ? snprintf (out , TEST_PATH_CAP , "%s/cbm-%s.lifetime.lock" , runtime_dir , key )
3372+ : -1 ;
3373+ return written > 0 && written < TEST_PATH_CAP ;
3374+ }
3375+
3376+ TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock ) {
3377+ static const char key [] = "c1c1d2d2e3e3f4f4" ;
3378+ char parent [TEST_PATH_CAP ] = {0 };
3379+ char runtime_dir [TEST_PATH_CAP ] = {0 };
3380+ char lock_path [TEST_PATH_CAP ] = {0 };
3381+ cbm_daemon_ipc_endpoint_t * endpoint = NULL ;
3382+ cbm_daemon_ipc_lifetime_reservation_t * reservation = NULL ;
3383+ int result_pipe [2 ] = {-1 , -1 };
3384+ pid_t child = -1 ;
3385+ uint8_t child_result = 0 ;
3386+ int child_status = -1 ;
3387+ int acquired = -1 ;
3388+ int held_after_first_probe = -1 ;
3389+ int held_after_second_probe = -1 ;
3390+ int free_after_release = -1 ;
3391+ bool lock_path_ok = false;
3392+
3393+ if (ipc_test_parent_new (parent , "probe-holds-lock" )) {
3394+ endpoint = cbm_daemon_ipc_endpoint_new (key , parent );
3395+ }
3396+ if (endpoint ) {
3397+ ipc_test_copy_path (runtime_dir , cbm_daemon_ipc_endpoint_runtime_dir (endpoint ));
3398+ lock_path_ok = ipc_test_lifetime_lock_path (lock_path , runtime_dir , key );
3399+ acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire (endpoint , & reservation );
3400+ }
3401+ if (reservation ) {
3402+ /* Two probes, matching how a real daemon polls its own reservation
3403+ * repeatedly while running. Each probe of an already-held lock used
3404+ * to open a throwaway fd on the lock file and close it. Since
3405+ * fcntl(2) record locks are scoped to (process, inode), that close
3406+ * silently released the real lock every time, regardless of the
3407+ * still-open fd the reservation itself is holding. */
3408+ held_after_first_probe = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3409+ held_after_second_probe = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3410+ }
3411+ if (reservation && lock_path_ok && pipe (result_pipe ) == 0 ) {
3412+ child = fork ();
3413+ }
3414+ if (child == 0 ) {
3415+ /* A genuinely independent process, not going through any of this
3416+ * codebase's APIs so it cannot inherit the parent's in-process
3417+ * claim registry: try to take the real OS-level lock directly. If
3418+ * the parent's fcntl lock survived the probes above, this fails. */
3419+ (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+ }
3432+ bool reported = ipc_test_fd_write_all (result_pipe [1 ], & child_result , sizeof (child_result ));
3433+ (void )close (result_pipe [1 ]);
3434+ _exit (reported ? 0 : 1 );
3435+ }
3436+ if (child > 0 ) {
3437+ (void )close (result_pipe [1 ]);
3438+ result_pipe [1 ] = -1 ;
3439+ bool received = ipc_test_fd_read_all (result_pipe [0 ], & child_result , sizeof (child_result ));
3440+ (void )close (result_pipe [0 ]);
3441+ result_pipe [0 ] = -1 ;
3442+ if (!received ) {
3443+ child_result = 2 ;
3444+ }
3445+ while (waitpid (child , & child_status , 0 ) < 0 && errno == EINTR ) {}
3446+ }
3447+ cbm_daemon_ipc_lifetime_reservation_release (reservation );
3448+ if (endpoint ) {
3449+ free_after_release = cbm_daemon_ipc_lifetime_reservation_probe (endpoint );
3450+ }
3451+ for (size_t index = 0 ; index < 2 ; index ++ ) {
3452+ if (result_pipe [index ] >= 0 ) {
3453+ (void )close (result_pipe [index ]);
3454+ }
3455+ }
3456+ cbm_daemon_ipc_endpoint_free (endpoint );
3457+ ipc_test_remove_tree (runtime_dir , parent );
3458+
3459+ ASSERT_EQ (acquired , 1 );
3460+ ASSERT_TRUE (lock_path_ok );
3461+ ASSERT_EQ (held_after_first_probe , 1 );
3462+ ASSERT_EQ (held_after_second_probe , 1 );
3463+ ASSERT_GT (child , 0 );
3464+ ASSERT_TRUE (WIFEXITED (child_status ));
3465+ ASSERT_EQ (WEXITSTATUS (child_status ), 0 );
3466+ ASSERT_EQ (child_result , 0 );
3467+ ASSERT_EQ (free_after_release , 0 );
3468+ PASS ();
3469+ }
3470+
33683471TEST (daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge ) {
33693472#ifdef _WIN32
33703473 PASS ();
@@ -5524,6 +5627,7 @@ SUITE(daemon_ipc) {
55245627#endif
55255628 RUN_TEST (daemon_ipc_posix_startup_lock_is_cross_process );
55265629 RUN_TEST (daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance );
5630+ RUN_TEST (daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock );
55275631 RUN_TEST (daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge );
55285632 RUN_TEST (daemon_ipc_posix_publication_boundaries_recover_from_crash );
55295633 RUN_TEST (daemon_ipc_posix_record_publication_windows_recover_from_crash );
0 commit comments