mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(handoff): liveness check under the pidfd lock, forget only a reaped pid's record
This commit is contained in:
1 parent
784df6260a
commit
2748ae9f5a
2 files changed
+31
-9
No files matched your search
@@ -149,9 +149,13 @@ static void ddh_forget(pid_t child) {
|
||||
if (!map || child <= 0) return;
|
||||
uint64_t *slot = &map[(uint32_t)child % DDH_FORKS];
|
||||
uint64_t seen = __atomic_load_n(slot, __ATOMIC_ACQUIRE);
|
||||
while (seen && ddh_record_child(seen) == child) {
|
||||
if (__atomic_compare_exchange_n(slot, &seen, 0, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) break;
|
||||
}
|
||||
if (!seen || ddh_record_child(seen) != child) return;
|
||||
// The child is reaped: a process that has the pid now is a later one, and a fork of ours that
|
||||
// got it has recorded itself (or will, replacing this): only the record read here goes.
|
||||
int saved = errno;
|
||||
int again = syscall(SYS_kill, child, 0) == 0 || errno != ESRCH;
|
||||
errno = saved;
|
||||
if (!again) __atomic_compare_exchange_n(slot, &seen, 0, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
|
||||
// In a forked child: the thread making the table did not come along, so the child may make its own.
|
||||
@@ -1840,11 +1844,14 @@ static pid_t ddh_logical_pgrp(void) { return ddh_logical_group(getpgrp()); }
|
||||
// later process given the pid, which started after that child. child_start -1: not checked.
|
||||
// 1 yes, 0 no, -1 when /proc could not be read for another reason (out of fds, say).
|
||||
static int ddh_parent_lives(pid_t parent, long child_start) {
|
||||
int fd = __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE);
|
||||
// The dup2()/dup3() wrappers move the pidfd only under this lock, so no move falls between the
|
||||
// look and the poll; when another thread holds it, /proc answers instead.
|
||||
int locked = __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) >= 0 && pthread_mutex_trylock(&g_ddh_pidfd_lock) == 0;
|
||||
int fd = locked ? __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) : -1;
|
||||
if (parent == g_ddh_report_parent && fd >= 0) {
|
||||
// No new fd, and exited counts whether reaped or not. Only while the number still is that
|
||||
// pidfd, before and after the poll (a raw close() or another thread's dup2() can take it),
|
||||
// and only a clear answer: anything else asks /proc.
|
||||
// pidfd, before and after the poll (a raw close() can take it), and only a clear answer:
|
||||
// anything else asks /proc.
|
||||
char path[32], before[32], after[32];
|
||||
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
||||
int saved = errno;
|
||||
@@ -1855,9 +1862,12 @@ static int ddh_parent_lives(pid_t parent, long child_start) {
|
||||
errno = saved;
|
||||
if (len > 0) before[len] = 0;
|
||||
if (len2 > 0) after[len2] = 0;
|
||||
if (n >= 0 && len2 > 0 && strcmp(before, "anon_inode:[pidfd]") == 0 && strcmp(after, before) == 0 &&
|
||||
__atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) == fd && !(p.revents & (POLLNVAL | POLLERR)))
|
||||
return !(p.revents & POLLIN);
|
||||
int clear = n >= 0 && len2 > 0 && strcmp(before, "anon_inode:[pidfd]") == 0 && strcmp(after, before) == 0 &&
|
||||
__atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) == fd && !(p.revents & (POLLNVAL | POLLERR));
|
||||
pthread_mutex_unlock(&g_ddh_pidfd_lock);
|
||||
if (clear) return !(p.revents & POLLIN);
|
||||
} else if (locked) {
|
||||
pthread_mutex_unlock(&g_ddh_pidfd_lock);
|
||||
}
|
||||
char path[48], stat[512];
|
||||
snprintf(path, sizeof(path), "/proc/%d/stat", (int)parent);
|
||||
|
||||
@@ -133,6 +133,18 @@ static void *heir(void *unused) {
|
||||
static void *late_bystander(void *unused) {
|
||||
(void)unused;
|
||||
nothread_await(1);
|
||||
// both waits only once the main thread has ended (its task a zombie)
|
||||
char path[64], stat[256];
|
||||
snprintf(path, sizeof(path), "/proc/self/task/%d/stat", (int)getpid());
|
||||
for (int i = 0; i < 500; i++) {
|
||||
FILE *f = fopen(path, "r");
|
||||
size_t n = f ? fread(stat, 1, sizeof(stat) - 1, f) : 0;
|
||||
if (f) fclose(f);
|
||||
stat[n] = 0;
|
||||
char *s = strrchr(stat, ')');
|
||||
if (!f || (s && s[1] == ' ' && (s[2] == 'Z' || s[2] == 'X'))) break;
|
||||
usleep(10000);
|
||||
}
|
||||
int status = 0;
|
||||
printf("bystander\t%d\n", (int)waitpid(nothread_child, &status, WNOHANG | __WNOTHREAD));
|
||||
nothread_go(2);
|
||||
|
||||
Reference in new issue
Block a user