@@ -231,6 +231,8 @@ static char *string_format(const char *format, ...) {
231231#ifndef _WIN32
232232static cbm_daemon_ipc_posix_publication_hook_fn g_posix_publication_hook_for_test ;
233233static void * g_posix_publication_hook_context_for_test ;
234+ static cbm_daemon_ipc_posix_lifetime_probe_hook_fn g_posix_lifetime_probe_hook_for_test ;
235+ static void * g_posix_lifetime_probe_hook_context_for_test ;
234236#endif
235237static atomic_uint g_windows_legacy_guard_release_failures_for_test ;
236238
@@ -245,6 +247,17 @@ void cbm_daemon_ipc_posix_publication_hook_set_for_test(
245247#endif
246248}
247249
250+ void cbm_daemon_ipc_posix_lifetime_probe_hook_set_for_test (
251+ cbm_daemon_ipc_posix_lifetime_probe_hook_fn hook , void * context ) {
252+ #ifndef _WIN32
253+ g_posix_lifetime_probe_hook_context_for_test = context ;
254+ g_posix_lifetime_probe_hook_for_test = hook ;
255+ #else
256+ (void )hook ;
257+ (void )context ;
258+ #endif
259+ }
260+
248261void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test (unsigned int count ) {
249262 atomic_store_explicit (& g_windows_legacy_guard_release_failures_for_test , count ,
250263 memory_order_release );
@@ -1261,6 +1274,13 @@ static int posix_named_shared_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *
12611274 return posix_named_lock_try_acquire_mode (endpoint , lock_name , true, fd_out , process_entry_out );
12621275}
12631276
1277+ /* Never open and close the lifetime lock file in a process that may already
1278+ * hold it: fcntl(2) record locks are scoped to (process, inode), so that
1279+ * close would silently release every lock this process holds on the file,
1280+ * regardless of which fd took it. This lock is fcntl rather than flock for
1281+ * exactly this reason in reverse: flock locks survive a fork, fcntl locks
1282+ * do not, and a forked child must never appear to hold its parent's
1283+ * reservation. */
12641284static int posix_record_lock_set (int fd , short lock_type ) {
12651285 struct flock record_lock = {
12661286 .l_type = lock_type ,
@@ -1284,6 +1304,19 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12841304 if (process_claimed < 0 ) {
12851305 return -1 ;
12861306 }
1307+ if (process_claimed == 1 ) {
1308+ /* Already held by this process per the in-process registry: trust
1309+ * that alone. Opening a throwaway fd on the lock file here and then
1310+ * closing it would release the real fcntl(2) record lock, which is
1311+ * scoped to (process, inode) rather than (fd, inode): closing any
1312+ * fd on this inode drops every lock this process holds on it, even
1313+ * one held via a different, still-open fd. */
1314+ return endpoint_runtime_still_valid (endpoint ) ? 1 : -1 ;
1315+ }
1316+
1317+ if (g_posix_lifetime_probe_hook_for_test ) {
1318+ g_posix_lifetime_probe_hook_for_test (g_posix_lifetime_probe_hook_context_for_test );
1319+ }
12871320
12881321 int fd = openat (endpoint -> dir_fd , lock_name , O_RDWR | O_CLOEXEC | O_NOFOLLOW );
12891322 if (fd < 0 ) {
@@ -1295,10 +1328,6 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12951328 (void )close (fd );
12961329 return -1 ;
12971330 }
1298- if (process_claimed == 1 ) {
1299- bool still_private = endpoint_runtime_still_valid (endpoint );
1300- return close (fd ) == 0 && still_private ? 1 : -1 ;
1301- }
13021331 struct flock record_lock = {
13031332 .l_type = F_WRLCK ,
13041333 .l_whence = SEEK_SET ,
@@ -1336,11 +1365,13 @@ static int posix_lifetime_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *endp
13361365 process_lock_entry_t * process_entry = NULL ;
13371366 int process_result = process_lock_claim (endpoint , lock_name , & process_entry );
13381367 if (process_result != 1 ) {
1339- return process_result == 0 &&
1340- private_regular_file_at_is_safe (endpoint -> dir_fd , lock_name , 1 ) &&
1341- endpoint_runtime_still_valid (endpoint )
1342- ? 0
1343- : -1 ;
1368+ /* Already held by this process per the in-process registry: trust
1369+ * that alone, the same as the probe's early return above. Calling
1370+ * private_regular_file_at_is_safe() here would open a throwaway fd
1371+ * on the lock file and close it, releasing the real fcntl(2) record
1372+ * lock this process holds, because that lock is scoped to (process,
1373+ * inode) rather than (fd, inode). */
1374+ return process_result == 0 && endpoint_runtime_still_valid (endpoint ) ? 0 : -1 ;
13441375 }
13451376 int fd = openat (endpoint -> dir_fd , lock_name , O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW , 0600 );
13461377 if (fd < 0 || !fd_set_cloexec (fd )) {
@@ -1386,6 +1417,10 @@ static void posix_lifetime_lock_release(int fd, process_lock_entry_t *process_en
13861417 process_lock_unclaim (process_entry );
13871418}
13881419
1420+ /* Serialize lifetime operations with fork and each other. The shared guard's
1421+ * atfork handlers prevent a child from inheriting it locked by a vanished
1422+ * thread. Lifetime operations do not call private-file-lock APIs while held. */
1423+
13891424int cbm_daemon_ipc_lifetime_reservation_try_acquire (
13901425 const cbm_daemon_ipc_endpoint_t * endpoint ,
13911426 cbm_daemon_ipc_lifetime_reservation_t * * reservation_out ) {
@@ -1395,18 +1430,24 @@ int cbm_daemon_ipc_lifetime_reservation_try_acquire(
13951430 if (!endpoint || !reservation_out ) {
13961431 return -1 ;
13971432 }
1433+ if (!cbm_private_file_lock_fork_guard_enter ()) {
1434+ return -1 ;
1435+ }
13981436 int fd = -1 ;
13991437 process_lock_entry_t * process_entry = NULL ;
14001438 int result = posix_lifetime_lock_try_acquire (endpoint , endpoint -> lifetime_lock_name , & fd ,
14011439 & process_entry );
14021440 if (result != 1 ) {
1441+ cbm_private_file_lock_fork_guard_leave ();
14031442 return result ;
14041443 }
14051444 cbm_daemon_ipc_lifetime_reservation_t * reservation = malloc (sizeof (* reservation ));
14061445 if (!reservation ) {
14071446 posix_lifetime_lock_release (fd , process_entry );
1447+ cbm_private_file_lock_fork_guard_leave ();
14081448 return -1 ;
14091449 }
1450+ cbm_private_file_lock_fork_guard_leave ();
14101451 reservation -> fd = fd ;
14111452 reservation -> process_entry = process_entry ;
14121453 reservation -> owner_pid = getpid ();
@@ -1419,7 +1460,11 @@ void cbm_daemon_ipc_lifetime_reservation_release(
14191460 if (!reservation ) {
14201461 return ;
14211462 }
1463+ if (!cbm_private_file_lock_fork_guard_enter ()) {
1464+ ipc_coordination_cleanup_fail_stop ("lifetime_lock_fork_guard" );
1465+ }
14221466 posix_lifetime_lock_release (reservation -> fd , reservation -> process_entry );
1467+ cbm_private_file_lock_fork_guard_leave ();
14231468 free (reservation );
14241469}
14251470
@@ -1445,7 +1490,12 @@ static bool lifetime_reservation_matches_endpoint(
14451490}
14461491
14471492int cbm_daemon_ipc_lifetime_reservation_probe (const cbm_daemon_ipc_endpoint_t * endpoint ) {
1448- return endpoint ? posix_lifetime_lock_probe (endpoint , endpoint -> lifetime_lock_name ) : -1 ;
1493+ if (!endpoint || !cbm_private_file_lock_fork_guard_enter ()) {
1494+ return -1 ;
1495+ }
1496+ int result = posix_lifetime_lock_probe (endpoint , endpoint -> lifetime_lock_name );
1497+ cbm_private_file_lock_fork_guard_leave ();
1498+ return result ;
14491499}
14501500
14511501static int posix_startup_lock_probe (const cbm_daemon_ipc_endpoint_t * endpoint ) {
0 commit comments