Skip to content

Commit 1133ba3

Browse files
halindromeclaude
andcommitted
fix(daemon): address QA round 2
- Classify touch failures explicitly. A file is lost only on proof that the path no longer names it: ENOENT, st_nlink 0, another inode, a symlink or other non-regular entry, or a replaced lock directory. Any other syscall failure (EIO, ESTALE, ENOMEM, EACCES on reopen) is transient, so a flaky runtime volume no longer stops the daemon. The POSIX private-lock touch and the ipc lock/marker touches share this rule; the Windows touch reports a failed handle query as transient. - The identity marker is classified by path (fstatat, no follow) before it is reopened, so replacement by a symlink is now loss, not a transient reopen failure. - Tests: symlink replacement of the identity marker (lost); an unreadable marker is transient (-1) and recovers (1) once readable; the cohort touch test ages both held files and asserts they were refreshed. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Shane McCarron <[email protected]>
1 parent 7f8d1ac commit 1133ba3

4 files changed

Lines changed: 144 additions & 34 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,9 +3229,29 @@ static int posix_touch_fold(int result, int next) {
32293229
return result == 0 || next == 0 ? 0 : (result < 0 || next < 0 ? -1 : 1);
32303230
}
32313231

3232+
/* Whether base_name still names the regular file (device, inode): 1 yes,
3233+
* 0 proven not (absent, another inode, a symlink or other non-regular
3234+
* entry), -1 undetermined (any other lookup failure, e.g. EIO or ESTALE). */
3235+
static int posix_path_names_inode(int directory_fd, const char *base_name, dev_t device,
3236+
ino_t inode) {
3237+
struct stat by_path;
3238+
if (fstatat(directory_fd, base_name, &by_path, AT_SYMLINK_NOFOLLOW) != 0) {
3239+
return errno == ENOENT ? 0 : -1;
3240+
}
3241+
return S_ISREG(by_path.st_mode) && by_path.st_dev == device && by_path.st_ino == inode ? 1 : 0;
3242+
}
3243+
32323244
static int posix_held_file_touch(int directory_fd, const char *base_name, int fd) {
3233-
if (!private_regular_file_snapshot(directory_fd, base_name, fd, 1, NULL)) {
3234-
return 0;
3245+
struct stat by_handle;
3246+
if (fstat(fd, &by_handle) != 0) {
3247+
return -1;
3248+
}
3249+
int identity =
3250+
by_handle.st_nlink == 0
3251+
? 0
3252+
: posix_path_names_inode(directory_fd, base_name, by_handle.st_dev, by_handle.st_ino);
3253+
if (identity <= 0) {
3254+
return identity;
32353255
}
32363256
return futimens(fd, NULL) == 0 ? 1 : -1;
32373257
}
@@ -3241,20 +3261,27 @@ static int posix_held_lock_touch(int directory_fd, const process_lock_entry_t *e
32413261
}
32423262

32433263
static int posix_identity_marker_touch(const cbm_daemon_ipc_listener_t *listener) {
3244-
/* The marker is not held open; reopen it and require the published inode.
3245-
* Only ENOENT proves loss: EMFILE and friends say nothing about the file. */
3264+
/* The marker is not held open. Classify by path first, so a symlink or
3265+
* other replacement is loss; only then reopen it to refresh, where a
3266+
* failure (EMFILE, EACCES) says nothing about the file and is transient. */
3267+
int identity = posix_path_names_inode(listener->dir_fd, listener->socket_identity_name,
3268+
listener->identity_device, listener->identity_inode);
3269+
if (identity <= 0) {
3270+
return identity;
3271+
}
32463272
int marker_fd = openat(listener->dir_fd, listener->socket_identity_name,
32473273
O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
32483274
if (marker_fd < 0) {
32493275
return errno == ENOENT ? 0 : -1;
32503276
}
32513277
struct stat marker_status;
3252-
int result = 0;
3253-
if (private_regular_file_snapshot(listener->dir_fd, listener->socket_identity_name, marker_fd,
3254-
1, &marker_status) &&
3255-
marker_status.st_dev == listener->identity_device &&
3256-
marker_status.st_ino == listener->identity_inode) {
3257-
result = futimens(marker_fd, NULL) == 0 ? 1 : -1;
3278+
int result = -1;
3279+
if (fstat(marker_fd, &marker_status) == 0) {
3280+
/* Replaced between the lookup and the open. */
3281+
result = marker_status.st_dev != listener->identity_device ||
3282+
marker_status.st_ino != listener->identity_inode
3283+
? 0
3284+
: (futimens(marker_fd, NULL) == 0 ? 1 : -1);
32583285
}
32593286
(void)close(marker_fd);
32603287
return result;

‎src/foundation/private_file_lock.c‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -487,24 +487,57 @@ cbm_private_file_lock_status_t cbm_private_file_lock_payload_write(cbm_private_f
487487
return valid ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_IO;
488488
}
489489

490+
/* Whether the lock's canonical path still names the held inode: 1 yes, 0 no
491+
* (proven lost), -1 undetermined. Only proof counts as loss: unlink leaves
492+
* st_nlink 0, a rename-away or replacement makes the path name another inode
493+
* or nothing, and a replaced directory changes the directory's identity. Any
494+
* other syscall failure (EIO, ESTALE, ENOMEM) says nothing about the file and
495+
* is reported as undetermined so the caller retries instead of exiting. */
496+
static int private_held_identity(const cbm_private_file_lock_t *lock) {
497+
const cbm_private_lock_directory_t *directory = lock->directory;
498+
struct stat by_handle;
499+
struct stat directory_by_path;
500+
struct stat by_path;
501+
if (fstat(lock->fd, &by_handle) != 0) {
502+
return -1;
503+
}
504+
if (by_handle.st_nlink == 0) {
505+
return 0;
506+
}
507+
if (lstat(directory->path, &directory_by_path) != 0) {
508+
return errno == ENOENT || errno == ENOTDIR ? 0 : -1;
509+
}
510+
if (!S_ISDIR(directory_by_path.st_mode) || directory_by_path.st_dev != directory->device ||
511+
directory_by_path.st_ino != directory->inode) {
512+
return 0;
513+
}
514+
if (fstatat(directory->fd, lock->base_name, &by_path, AT_SYMLINK_NOFOLLOW) != 0) {
515+
return errno == ENOENT ? 0 : -1;
516+
}
517+
return S_ISREG(by_path.st_mode) && by_path.st_dev == by_handle.st_dev &&
518+
by_path.st_ino == by_handle.st_ino
519+
? 1
520+
: 0;
521+
}
522+
490523
cbm_private_file_lock_status_t cbm_private_file_lock_touch(cbm_private_file_lock_t *lock) {
491524
if (!lock) {
492525
return CBM_PRIVATE_FILE_LOCK_UNSAFE;
493526
}
494527
if (!cbm_private_file_lock_fork_guard_enter()) {
495528
return CBM_PRIVATE_FILE_LOCK_IO;
496529
}
497-
/* Unlink leaves st_nlink 0 on the held inode, but a rename-away or a
498-
* replaced directory keeps it linked elsewhere; only the path-vs-handle
499-
* identity check proves the canonical path still names this lock. */
500-
bool linked = private_payload_fd_valid(lock, NULL) && lock->directory &&
501-
private_file_revalidate(lock->directory, lock->base_name, lock->fd, NULL);
502-
bool touched = linked && futimens(lock->fd, NULL) == 0;
503-
cbm_private_file_lock_fork_guard_leave();
504-
if (!linked) {
505-
return CBM_PRIVATE_FILE_LOCK_UNSAFE;
530+
cbm_private_file_lock_status_t status = CBM_PRIVATE_FILE_LOCK_UNSAFE;
531+
if (lock->fd >= 0 && !lock->unlocked && lock->owner_pid == getpid() && lock->directory &&
532+
private_lock_is_tracked(lock)) {
533+
int identity = private_held_identity(lock);
534+
status = identity > 0 ? (futimens(lock->fd, NULL) == 0 ? CBM_PRIVATE_FILE_LOCK_OK
535+
: CBM_PRIVATE_FILE_LOCK_IO)
536+
: identity < 0 ? CBM_PRIVATE_FILE_LOCK_IO
537+
: CBM_PRIVATE_FILE_LOCK_UNSAFE;
506538
}
507-
return touched ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_IO;
539+
cbm_private_file_lock_fork_guard_leave();
540+
return status;
508541
}
509542

510543
cbm_private_file_lock_status_t cbm_private_file_lock_release(cbm_private_file_lock_t **lock_io) {
@@ -1478,15 +1511,21 @@ cbm_private_file_lock_status_t cbm_private_file_lock_payload_write(cbm_private_f
14781511
cbm_private_file_lock_status_t cbm_private_file_lock_touch(cbm_private_file_lock_t *lock) {
14791512
/* Windows has no age-based cleaner of the private lock directory; only
14801513
* confirm the held handle still names a linked file. */
1481-
if (!lock) {
1514+
if (!lock || lock->handle == INVALID_HANDLE_VALUE || lock->unlocked) {
14821515
return CBM_PRIVATE_FILE_LOCK_UNSAFE;
14831516
}
14841517
if (!cbm_private_file_lock_fork_guard_enter()) {
14851518
return CBM_PRIVATE_FILE_LOCK_IO;
14861519
}
1487-
bool linked = private_win_payload_handle_valid(lock);
1520+
BY_HANDLE_FILE_INFORMATION information;
1521+
bool queried = GetFileInformationByHandle(lock->handle, &information) != 0;
14881522
cbm_private_file_lock_fork_guard_leave();
1489-
return linked ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_UNSAFE;
1523+
/* A failed query says nothing about the file; only a proven unlink is loss. */
1524+
if (!queried) {
1525+
return CBM_PRIVATE_FILE_LOCK_IO;
1526+
}
1527+
return information.nNumberOfLinks == 0 ? CBM_PRIVATE_FILE_LOCK_UNSAFE
1528+
: CBM_PRIVATE_FILE_LOCK_OK;
14901529
}
14911530

14921531
static bool private_win_release_unlock(cbm_private_file_lock_t *lock) {

‎tests/test_daemon_ipc.c‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,16 +2072,18 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {
20722072
#ifdef _WIN32
20732073
SKIP_PLATFORM("unlink-while-held of runtime files is POSIX behavior");
20742074
#else
2075-
enum { TOUCH_CASES = 4 };
2075+
enum { TOUCH_CASES = 5 };
2076+
enum { LOSS_UNLINK, LOSS_REPLACE_FILE, LOSS_REPLACE_SYMLINK };
20762077
static const char key[] = "2178a2178a2178a0";
20772078
static const struct {
20782079
const char *suffix;
2079-
bool replace;
2080+
int loss;
20802081
} cases[TOUCH_CASES] = {
2081-
{"lifetime.lock", false}, /* lifetime reservation */
2082-
{"lock", false}, /* listener-owned participant guard */
2083-
{"sock.identity", false}, /* identity marker, unlinked */
2084-
{"sock.identity", true}, /* identity marker, replaced by a fresh inode */
2082+
{"lifetime.lock", LOSS_UNLINK}, /* lifetime reservation */
2083+
{"lock", LOSS_UNLINK}, /* listener-owned participant guard */
2084+
{"sock.identity", LOSS_UNLINK}, /* identity marker, unlinked */
2085+
{"sock.identity", LOSS_REPLACE_FILE}, /* ... replaced by a fresh inode */
2086+
{"sock.identity", LOSS_REPLACE_SYMLINK} /* ... replaced by a symlink */,
20852087
};
20862088
const time_t stale = 1000000000;
20872089
char parent[TEST_PATH_CAP] = {0};
@@ -2091,14 +2093,29 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {
20912093
int touched_intact[TOUCH_CASES] = {0};
20922094
bool refreshed[TOUCH_CASES] = {false};
20932095
bool lost[TOUCH_CASES] = {false};
2094-
int touched_after_loss[TOUCH_CASES] = {1, 1, 1, 1};
2096+
int touched_after_loss[TOUCH_CASES] = {1, 1, 1, 1, 1};
2097+
int unreadable_touch = 0;
2098+
int readable_again_touch = 0;
20952099

20962100
if (ipc_test_parent_new(parent, "listener-touch")) {
20972101
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
20982102
}
20992103
if (endpoint) {
21002104
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
21012105
}
2106+
/* A still-valid marker that merely cannot be reopened (EACCES) is a
2107+
* transient refresh failure, not loss: the daemon must keep serving. */
2108+
if (endpoint) {
2109+
cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(endpoint);
2110+
char marker[TEST_PATH_CAP];
2111+
int written = snprintf(marker, sizeof(marker), "%s/cbm-%s.sock.identity", runtime_dir, key);
2112+
if (listener && written > 0 && written < (int)sizeof(marker) && chmod(marker, 0) == 0) {
2113+
unreadable_touch = cbm_daemon_ipc_listener_touch(listener, NULL);
2114+
readable_again_touch =
2115+
chmod(marker, 0600) == 0 ? cbm_daemon_ipc_listener_touch(listener, NULL) : -2;
2116+
}
2117+
cbm_daemon_ipc_listener_close(listener);
2118+
}
21022119
for (size_t i = 0; endpoint && i < TOUCH_CASES; i++) {
21032120
cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(endpoint);
21042121
started[i] = listener != NULL;
@@ -2113,16 +2130,30 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {
21132130
refreshed[i] = touched_intact[i] == 1 && stat(path, &after) == 0 &&
21142131
after.st_atime > stale && after.st_mtime > stale;
21152132
lost[i] = refreshed[i] && unlink(path) == 0;
2116-
if (lost[i] && cases[i].replace) {
2133+
if (lost[i] && cases[i].loss == LOSS_REPLACE_FILE) {
21172134
int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0600);
21182135
lost[i] = fd >= 0 && close(fd) == 0;
2136+
} else if (lost[i] && cases[i].loss == LOSS_REPLACE_SYMLINK) {
2137+
lost[i] = symlink("/dev/null", path) == 0;
21192138
}
21202139
touched_after_loss[i] = lost[i] ? cbm_daemon_ipc_listener_touch(listener, NULL) : -2;
21212140
cbm_daemon_ipc_listener_close(listener);
2141+
/* A tampered marker makes close (correctly) keep the socket pair; clear
2142+
* this case's simulated leftovers so the next case can listen. */
2143+
static const char *const leftovers[] = {"sock.identity", "sock", "anc"};
2144+
for (size_t j = 0; j < sizeof(leftovers) / sizeof(leftovers[0]); j++) {
2145+
written = snprintf(path, sizeof(path), "%s/cbm-%s.%s", runtime_dir, key, leftovers[j]);
2146+
if (written > 0 && written < (int)sizeof(path)) {
2147+
(void)unlink(path);
2148+
}
2149+
}
21222150
}
21232151
cbm_daemon_ipc_endpoint_free(endpoint);
21242152
ipc_test_remove_tree(runtime_dir, parent);
21252153

2154+
/* Root bypasses mode 0, so the reopen succeeds there and the touch is 1. */
2155+
ASSERT_EQ(unreadable_touch, geteuid() == 0 ? 1 : -1);
2156+
ASSERT_EQ(readable_again_touch, 1);
21262157
for (size_t i = 0; i < TOUCH_CASES; i++) {
21272158
ASSERT_TRUE(started[i]);
21282159
ASSERT_EQ(touched_intact[i], 1);

‎tests/test_version_cohort.c‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#endif
2323
#include <windows.h>
2424
#else
25+
#include <fcntl.h>
26+
#include <sys/stat.h>
2527
#include <sys/wait.h>
2628
#include <unistd.h>
2729
#endif
@@ -690,9 +692,6 @@ TEST(version_cohort_touch_reports_each_lost_file) {
690692
CBM_VERSION_COHORT_OK);
691693
ASSERT_EQ(cbm_version_cohort_daemon_claim_acquire(manager, &claim), CBM_VERSION_COHORT_OK);
692694

693-
ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);
694-
ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_OK);
695-
696695
const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(fixture.endpoint);
697696
char marker[VERSION_COHORT_TEST_PATH_CAP];
698697
char lifetime[VERSION_COHORT_TEST_PATH_CAP];
@@ -701,6 +700,20 @@ TEST(version_cohort_touch_reports_each_lost_file) {
701700
ASSERT_TRUE(snprintf(lifetime, sizeof(lifetime), "%s/cbm-version-cohort-lifetime-v1.lock",
702701
runtime_dir) < (int)sizeof(lifetime));
703702

703+
/* Age both held files the way a cleaner would see them, then prove the
704+
* touch refreshed them through the held handles. */
705+
const time_t stale = 1000000000;
706+
struct timespec stale_times[2] = {{.tv_sec = stale}, {.tv_sec = stale}};
707+
ASSERT_EQ(utimensat(AT_FDCWD, marker, stale_times, 0), 0);
708+
ASSERT_EQ(utimensat(AT_FDCWD, lifetime, stale_times, 0), 0);
709+
ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);
710+
ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_OK);
711+
struct stat refreshed;
712+
ASSERT_EQ(stat(marker, &refreshed), 0);
713+
ASSERT_TRUE(refreshed.st_mtime > stale && refreshed.st_atime > stale);
714+
ASSERT_EQ(stat(lifetime, &refreshed), 0);
715+
ASSERT_TRUE(refreshed.st_mtime > stale && refreshed.st_atime > stale);
716+
704717
ASSERT_EQ(unlink(marker), 0);
705718
ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_UNSAFE);
706719
ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);

0 commit comments

Comments
 (0)