mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(handoff): one given-up table at most, whole stop codes and rusage, WNOWAIT exits rechecked
This commit is contained in:
1 parent
081e4e1223
commit
be7f59d983
4 files changed
+249
-66
No files matched your search
@@ -51,6 +51,10 @@ struct ddh_table {
|
||||
};
|
||||
|
||||
static struct ddh_table *g_ddh; // this process's table, for the children it forks
|
||||
// A table given up while a close from before it was kept may still be under way: open, unkept, and
|
||||
// tried again (not another made) until that close is over, so a close that never ends costs one fd.
|
||||
static struct ddh_table *g_ddh_candidate;
|
||||
static int g_ddh_candidate_fd = -1;
|
||||
static int g_ddh_fd = -1;
|
||||
static pid_t g_ddh_owner;
|
||||
// The parent's table, when this image was exec'd by a child the parent forked (a wrapper such as
|
||||
@@ -227,6 +231,9 @@ static pid_t ddh_forking_thread(pid_t child, long child_start) {
|
||||
// cannot map one, and then hands nothing off).
|
||||
static void ddh_forked(pid_t thread) {
|
||||
__atomic_store_n(&g_ddh_preparing, 0, __ATOMIC_RELAXED);
|
||||
if (g_ddh_candidate) munmap(g_ddh_candidate, sizeof(struct ddh_table)); // its fd: close-on-exec
|
||||
g_ddh_candidate = NULL;
|
||||
g_ddh_candidate_fd = -1;
|
||||
uint64_t *old = __atomic_exchange_n(&g_ddh_forks, NULL, __ATOMIC_ACQ_REL);
|
||||
if (!old) return;
|
||||
if (__atomic_load_n(&g_ddh, __ATOMIC_ACQUIRE)) {
|
||||
@@ -253,6 +260,7 @@ static void ddh_table_kept_gate(void) {
|
||||
}
|
||||
|
||||
static int ddh_prepare_table(void);
|
||||
static int ddh_new_table(int *made_fd, struct ddh_table **made);
|
||||
static int ddh_is_table(int fd, const struct ddh_table *t);
|
||||
|
||||
static void ddh_prepare(void) {
|
||||
@@ -271,23 +279,58 @@ static void ddh_prepare(void) {
|
||||
}
|
||||
|
||||
// 1 when the table is made (g_ddh), 0 when its fd went to another thread's close before that one
|
||||
// could see it was kept; the other failures are logged and end the trying too (1).
|
||||
// could see it was kept, or such a close was not over in time; the other failures are logged and end
|
||||
// the trying too (1).
|
||||
static int ddh_prepare_table(void) {
|
||||
struct ddh_table *p = g_ddh_candidate;
|
||||
int fd = g_ddh_candidate_fd;
|
||||
g_ddh_candidate = NULL;
|
||||
g_ddh_candidate_fd = -1;
|
||||
if (p && ddh_is_table(fd, p)) {
|
||||
ddh_keep(fd);
|
||||
} else {
|
||||
if (p) munmap(p, sizeof(struct ddh_table)); // closed since: a new one
|
||||
if (!ddh_new_table(&fd, &p)) return 1;
|
||||
}
|
||||
// Another thread's close() or close_range() that looked at the kept fds before this one was among
|
||||
// them may have closed it (and an open() taken the number): once those are over, it is still this
|
||||
// table, or it is given up. Given up, the number is not closed here: a close still under way may
|
||||
// close it any time, and another thread then reuse it before a close here would come.
|
||||
ddh_table_kept_gate();
|
||||
int settled = kept_settle();
|
||||
if (!settled || !ddh_is_table(fd, p)) {
|
||||
ddh_unkeep(fd);
|
||||
if (!settled && ddh_is_table(fd, p)) {
|
||||
g_ddh_candidate = p;
|
||||
g_ddh_candidate_fd = fd;
|
||||
} else {
|
||||
munmap(p, sizeof(struct ddh_table));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
g_ddh_fd = fd;
|
||||
g_ddh_owner = getpid();
|
||||
__atomic_store_n(&g_ddh, p, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// A new table, kept: 0 when one cannot be made (logged).
|
||||
static int ddh_new_table(int *made_fd, struct ddh_table **made) {
|
||||
int fd = (int)syscall(SYS_memfd_create, "ddnatv-handoff", 1 /* MFD_CLOEXEC */);
|
||||
if (fd < 0) {
|
||||
ddh_prepare_failed("memfd_create");
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
if (ftruncate(fd, sizeof(struct ddh_table)) != 0) {
|
||||
ddh_prepare_failed("ftruncate");
|
||||
close(fd);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
struct ddh_table *p = mmap(NULL, sizeof(struct ddh_table), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED) {
|
||||
ddh_prepare_failed("mmap");
|
||||
close(fd);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
// Out of the way of the numbers programs redirect, and kept open when a child closes every
|
||||
// inherited fd before exec (Python's subprocess does): the child needs it for its handoff.
|
||||
@@ -304,21 +347,8 @@ static int ddh_prepare_table(void) {
|
||||
p->pad[1] = (int32_t)time(NULL);
|
||||
p->pad[2] = (int32_t)(intptr_t)p;
|
||||
}
|
||||
// Another thread's close() or close_range() that looked at the kept fds before this one was among
|
||||
// them may have closed it (and an open() taken the number): once those are over, it is still this
|
||||
// table, or it is given up and made again.
|
||||
// Given up, the number is not closed here: a close still under way may close it any time, and
|
||||
// another thread then reuse it before a close here would come. Left to that close, or open and
|
||||
// unkept, close-on-exec, until the program closes it or execs.
|
||||
ddh_table_kept_gate();
|
||||
if (!kept_settle() || !ddh_is_table(fd, p)) {
|
||||
ddh_unkeep(fd);
|
||||
munmap(p, sizeof(struct ddh_table));
|
||||
return 0;
|
||||
}
|
||||
g_ddh_fd = fd;
|
||||
g_ddh_owner = getpid();
|
||||
__atomic_store_n(&g_ddh, p, __ATOMIC_RELEASE);
|
||||
*made_fd = fd;
|
||||
*made = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1934,14 +1964,18 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) {
|
||||
// Only the kind of event looked at: the child may have gone on since, handed its
|
||||
// program off and exited, and a wait4() would take that exit, the forked child's.
|
||||
siginfo_t got;
|
||||
struct rusage usage;
|
||||
memset(&got, 0, sizeof(got));
|
||||
memset(&usage, 0, sizeof(usage));
|
||||
int kind = seen.si_code == CLD_CONTINUED ? WCONTINUED : WSTOPPED;
|
||||
if (call_orig_syscall(SYS_waitid, P_PID, (unsigned long)child, (unsigned long)&got,
|
||||
(unsigned long)(kind | WNOHANG | (options & (__WALL | __WNOTHREAD))), 0, 0) != 0 ||
|
||||
(unsigned long)(kind | WNOHANG | (options & (__WALL | __WNOTHREAD))), (unsigned long)&usage,
|
||||
0) != 0 ||
|
||||
got.si_pid != child)
|
||||
continue; // another wait took it
|
||||
wstatus = got.si_code == CLD_CONTINUED ? 0xffff : ((got.si_status & 0xff) << 8) | 0x7f;
|
||||
if (ru) memset(ru, 0, sizeof(*ru));
|
||||
// as wait4() has it: a stop's whole code (a ptrace event's bits above the signal too)
|
||||
wstatus = got.si_code == CLD_CONTINUED ? 0xffff : (got.si_status << 8) | 0x7f;
|
||||
if (ru) *ru = usage;
|
||||
if (status) *status = wstatus;
|
||||
return child;
|
||||
}
|
||||
@@ -2049,6 +2083,7 @@ static int ddh_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
|
||||
}
|
||||
} else if (seen.si_pid > 0) {
|
||||
pid_t child = seen.si_pid;
|
||||
ddh_wait_gate();
|
||||
int exited = seen.si_code == CLD_EXITED || seen.si_code == CLD_KILLED || seen.si_code == CLD_DUMPED;
|
||||
struct ddh_entry *h = exited ? ddh_find(child) : NULL;
|
||||
int32_t gen = h ? ddh_gen_of(h, child) : 0;
|
||||
@@ -2058,6 +2093,16 @@ static int ddh_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
|
||||
continue;
|
||||
}
|
||||
if (options & WNOWAIT) {
|
||||
// An exit with no entry found may be a forked child whose entry another wait has
|
||||
// released since the look, taking it with its program's status: still there now, it
|
||||
// is no such child (its entry would have been found), else the look starts over.
|
||||
if (exited) {
|
||||
siginfo_t again;
|
||||
memset(&again, 0, sizeof(again));
|
||||
if (real(P_PID, (id_t)child, &again, WEXITED | WNOWAIT | WNOHANG | carried) < 0 || again.si_pid != child)
|
||||
continue;
|
||||
seen = again;
|
||||
}
|
||||
if (info) *info = seen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -240,10 +240,10 @@ static void clear_dns_redirect(int fd) {
|
||||
int close(int fd) {
|
||||
static int (*orig_close)(int) = NULL;
|
||||
if (!orig_close) orig_close = (int (*)(int))dlsym(RTLD_NEXT, "close");
|
||||
unsigned epoch = kept_close_begin();
|
||||
uint64_t ticket = kept_close_begin();
|
||||
// The ARM translator's memory file (privileged.c): left open, as if it were closed
|
||||
if (g_kept_fd_count > 0 && is_kept_fd(fd)) {
|
||||
kept_close_end(epoch);
|
||||
kept_close_end(ticket);
|
||||
return 0;
|
||||
}
|
||||
clear_dns_redirect(fd);
|
||||
@@ -251,7 +251,7 @@ int close(int fd) {
|
||||
forget_proc_root_fd(fd);
|
||||
int r = orig_close ? orig_close(fd) : -1;
|
||||
int saved = errno;
|
||||
kept_close_end(epoch);
|
||||
kept_close_end(ticket);
|
||||
errno = saved;
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -75,37 +75,47 @@ static int is_kept_fd(int fd) {
|
||||
// while others run (hook/handoff.c's table) moves the epoch on afterwards, waits for the closes of
|
||||
// the one before (kept_settle()), and only then trusts the fd to be its own still.
|
||||
static unsigned g_close_epoch;
|
||||
static int g_closers[2];
|
||||
// This thread's own, a close a signal's handler interrupted among them
|
||||
static __thread int t_closing[2];
|
||||
// Per epoch, generation << 32 | count, one word: every change is one compare-and-swap, so a fork from
|
||||
// a handler that interrupted a close's begin or end never splits one in two. A forked child moves the
|
||||
// generation on and starts from none (kept_close_forked()): an end whose generation is gone counts
|
||||
// nothing, and a begin whose swap failed takes the child's.
|
||||
static uint64_t g_closers[2];
|
||||
|
||||
static unsigned kept_close_begin(void) {
|
||||
static void kept_close_end(uint64_t ticket);
|
||||
|
||||
// The close's ticket: generation << 1 | epoch
|
||||
static uint64_t kept_close_begin(void) {
|
||||
for (;;) {
|
||||
unsigned epoch = __atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1;
|
||||
__atomic_add_fetch(&g_closers[epoch], 1, __ATOMIC_SEQ_CST);
|
||||
if ((__atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1) == epoch) {
|
||||
t_closing[epoch]++;
|
||||
return epoch;
|
||||
}
|
||||
__atomic_sub_fetch(&g_closers[epoch], 1, __ATOMIC_SEQ_CST);
|
||||
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
|
||||
if (!__atomic_compare_exchange_n(&g_closers[epoch], &word, word + 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
|
||||
continue;
|
||||
uint64_t ticket = (word >> 32) << 1 | epoch;
|
||||
if ((__atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1) == epoch) return ticket;
|
||||
kept_close_end(ticket); // the epoch moved on meanwhile: counted in the new one instead
|
||||
}
|
||||
}
|
||||
|
||||
static void kept_close_end(unsigned epoch) {
|
||||
t_closing[epoch]--;
|
||||
__atomic_sub_fetch(&g_closers[epoch], 1, __ATOMIC_SEQ_CST);
|
||||
static void kept_close_end(uint64_t ticket) {
|
||||
unsigned epoch = (unsigned)(ticket & 1);
|
||||
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
|
||||
while ((word >> 32) == (ticket >> 1) && (word & 0xffffffffu) != 0 &&
|
||||
!__atomic_compare_exchange_n(&g_closers[epoch], &word, word - 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
|
||||
}
|
||||
}
|
||||
|
||||
// In a forked child: the closes other threads of the parent were making did not come along, and
|
||||
// only the forking thread's own (one a handler interrupted to fork) end here.
|
||||
// In a forked child: the closes the parent's threads were making are not the child's to wait for.
|
||||
// (The forking thread's own, a close a handler interrupted to fork, then goes uncounted.)
|
||||
static void kept_close_forked(void) {
|
||||
__atomic_store_n(&g_closers[0], t_closing[0], __ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&g_closers[1], t_closing[1], __ATOMIC_SEQ_CST);
|
||||
for (int epoch = 0; epoch < 2; epoch++) {
|
||||
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&g_closers[epoch], ((word >> 32) + 1) << 32, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
}
|
||||
|
||||
static int kept_drained(unsigned epoch) {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
if (__atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST) == 0) return 1;
|
||||
if ((__atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST) & 0xffffffffu) == 0) return 1;
|
||||
struct timespec pause = {0, 1000 * 1000};
|
||||
nanosleep(&pause, NULL);
|
||||
}
|
||||
@@ -588,10 +598,10 @@ static int close_range_kept(unsigned int first, unsigned int last, int flags) {
|
||||
|
||||
int close_range(unsigned int first, unsigned int last, int flags) {
|
||||
if (!(flags & CLOSE_RANGE_CLOEXEC)) forget_proc_root_range(first, last);
|
||||
unsigned epoch = kept_close_begin();
|
||||
uint64_t ticket = kept_close_begin();
|
||||
int r = close_range_kept(first, last, flags);
|
||||
int saved = errno;
|
||||
kept_close_end(epoch);
|
||||
kept_close_end(ticket);
|
||||
errno = saved;
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -157,14 +157,16 @@ static int late_record(const char *arg, int live) {
|
||||
if (!f) return 1;
|
||||
fprintf(f, "%d", (int)first - 1);
|
||||
if (fclose(f) != 0) return 1;
|
||||
int hold[2];
|
||||
if (live == 2 && pipe(hold) != 0) return 1;
|
||||
int hold[2], at[2];
|
||||
if (live == 2 && (pipe(hold) != 0 || pipe(at) != 0)) return 1;
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
if (live == 2) {
|
||||
char c;
|
||||
// back from fork(), whose hook has recorded it: the parent's word to go on comes after the
|
||||
// late record
|
||||
char c = 1;
|
||||
close(hold[1]);
|
||||
if (read(hold[0], &c, 1) != 1) _exit(126); // recorded itself already, in the fork
|
||||
if (write(at[1], &c, 1) != 1 || read(hold[0], &c, 1) != 1) _exit(126);
|
||||
}
|
||||
execl("/bin/target", "target", arg, (char *)NULL);
|
||||
_exit(127);
|
||||
@@ -178,6 +180,10 @@ static int late_record(const char *arg, int live) {
|
||||
if (write(gate[1], "1", 1) != 1 || read(late_done[0], &err, sizeof(err)) != sizeof(err)) return 1;
|
||||
}
|
||||
if (live == 2) {
|
||||
char c;
|
||||
struct pollfd p = {at[0], POLLIN, 0};
|
||||
int ready = poll(&p, 1, 10000) == 1 && read(at[0], &c, 1) == 1;
|
||||
printf("recorded\t%d\n", ready);
|
||||
if (write(gate[1], "1", 1) != 1 || read(late_done[0], &err, sizeof(err)) != sizeof(err)) return 1;
|
||||
close(hold[0]);
|
||||
if (write(hold[1], "1", 1) != 1) return 1;
|
||||
@@ -250,15 +256,38 @@ static void *prep_release(void *unused) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Whether a table of the hook's is open here, at any number
|
||||
static int table_open(void) {
|
||||
// How many of the hook's tables are open here, at any numbers
|
||||
static int tables_open(void) {
|
||||
char path[32], target[64];
|
||||
int count = 0;
|
||||
for (int fd = 3; fd < 4096; fd++) {
|
||||
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
||||
ssize_t n = readlink(path, target, sizeof(target) - 1);
|
||||
if (n > 0 && (target[n] = 0, strncmp(target, "/memfd:ddnatv-handoff", 21) == 0)) return 1;
|
||||
if (n > 0 && (target[n] = 0, strncmp(target, "/memfd:ddnatv-handoff", 21) == 0)) count++;
|
||||
}
|
||||
return 0;
|
||||
return count;
|
||||
}
|
||||
|
||||
static int table_open(void) { return tables_open() > 0; }
|
||||
|
||||
// nowaitpeek: one thread's waitid(WNOWAIT) has seen the exit of a forked child that handed its
|
||||
// program off (held there by the hook's wait gate) when another wait takes the program's exit and
|
||||
// frees the entry; that look reports nothing, not the forked child's exit
|
||||
static int nowait_in[2], nowait_out[2];
|
||||
|
||||
static void *nowait_waiter(void *unused) {
|
||||
(void)unused;
|
||||
char gate[48];
|
||||
snprintf(gate, sizeof(gate), "%d %d %d", nowait_in[0], nowait_out[1], (int)syscall(SYS_gettid));
|
||||
setenv("DDH_TEST_WAIT_GATE", gate, 1);
|
||||
siginfo_t si;
|
||||
memset(&si, 0, sizeof(si));
|
||||
errno = 0;
|
||||
int r = waitid(P_ALL, 0, &si, WEXITED | WNOWAIT);
|
||||
printf("looked\t%d\n", r);
|
||||
printf("echild\t%d\n", r == -1 && errno == ECHILD);
|
||||
printf("lookedpid\t%d\n", r == 0 ? (int)si.si_pid : 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// twowaits: one thread's wait has found the forked child (held there by the hook's test gate, for
|
||||
@@ -319,14 +348,27 @@ static void *peek_waiter(void *unused) {
|
||||
snprintf(gate, sizeof(gate), "%d %d %d", peek_in[0], peek_out[1], (int)syscall(SYS_gettid));
|
||||
setenv("DDH_TEST_WAIT_GATE", gate, 1);
|
||||
int status = 0;
|
||||
// the stop is gone by the time it takes the event: this one wait reports the program's end
|
||||
pid_t r = waitpid(peek_child, &status, WUNTRACED);
|
||||
// the stop is gone by the time it takes the event: what it reports is the program's end
|
||||
if (r == peek_child && WIFSTOPPED(status)) r = waitpid(peek_child, &status, 0);
|
||||
printf("waited\t%d\n", r == peek_child);
|
||||
printf("code\t%d\n", WIFEXITED(status) ? WEXITSTATUS(status) : -1);
|
||||
printf("stopped\t%d\n", r == peek_child && WIFSTOPPED(status));
|
||||
printf("code\t%d\n", r == peek_child && WIFEXITED(status) ? WEXITSTATUS(status) : -1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Whether pid is a zombie here, its exit not taken (the kernel's word, no wait of any kind)
|
||||
static int zombie(pid_t pid) {
|
||||
char path[64], stat[256];
|
||||
snprintf(path, sizeof(path), "/proc/%d/stat", (int)pid);
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return 0;
|
||||
size_t n = fread(stat, 1, sizeof(stat) - 1, f);
|
||||
fclose(f);
|
||||
stat[n] = 0;
|
||||
char *end = strrchr(stat, ')');
|
||||
return end && end[1] == ' ' && end[2] == 'Z';
|
||||
}
|
||||
|
||||
static void *bystander(void *unused) {
|
||||
(void)unused;
|
||||
int status = 0;
|
||||
@@ -480,6 +522,51 @@ int main(int argc, char **argv) {
|
||||
printf("table\t%d\n", table_open());
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "prepcloseheld") == 0) {
|
||||
// the close stays under way through three forks, each of whose tries gives the table up: one
|
||||
// table's fd at most is left for it, tried again, not another made each time
|
||||
if (pipe(prep_in) != 0 || pipe(prep_out) != 0) return 1;
|
||||
pthread_t closer;
|
||||
pthread_create(&closer, NULL, prep_closer, NULL);
|
||||
char c;
|
||||
if (read(prep_out[0], &c, 1) != 1) return 1;
|
||||
int status = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
pid_t quick = fork();
|
||||
if (quick == 0) _exit(0);
|
||||
waitpid(quick, &status, 0);
|
||||
}
|
||||
printf("tables\t%d\n", tables_open());
|
||||
if (write(prep_in[1], "1", 1) != 1) return 1;
|
||||
pthread_join(closer, NULL);
|
||||
pid_t again = fork(); // the close is over: the table is made now
|
||||
if (again == 0) {
|
||||
execl("/bin/target", "target", argv[2], (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
printf("again\t%d\n", waitpid(again, &status, 0) == again && WIFEXITED(status) ? WEXITSTATUS(status) : -1);
|
||||
printf("table\t%d\n", table_open());
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "nowaitpeek") == 0) {
|
||||
if (pipe(nowait_in) != 0 || pipe(nowait_out) != 0) return 1;
|
||||
pid_t child = fork();
|
||||
if (child == 0) {
|
||||
execl("/bin/target", "target", "sleep", (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
pthread_t waiter;
|
||||
pthread_create(&waiter, NULL, nowait_waiter, NULL);
|
||||
char c;
|
||||
if (read(nowait_out[0], &c, 1) != 1) return 1; // it has seen the forked child's exit
|
||||
kill(child, SIGTERM);
|
||||
int status = 0;
|
||||
printf("taken\t%d\n", waitpid(child, &status, 0) == child && WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
|
||||
if (write(nowait_in[1], "1111", 4) != 4) return 1;
|
||||
pthread_join(waiter, NULL);
|
||||
printf("child\t%d\n", (int)child);
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "forkclose") == 0) {
|
||||
// another thread's close_range() is under way (held on the test gate) when this forks a child,
|
||||
// which then makes a table of its own for a child of its own: it waits for no close of its parent's
|
||||
@@ -547,7 +634,10 @@ int main(int argc, char **argv) {
|
||||
char c;
|
||||
if (read(peek_out[0], &c, 1) != 1) return 1; // it has seen the stop
|
||||
syscall(SYS_tgkill, peek_child, peek_child, SIGCONT);
|
||||
usleep(2000000); // the reply has come, the program runs, the forked child has exited
|
||||
// the reply has come, the program runs, the forked child has exited (its exit still not taken)
|
||||
int gone = 0;
|
||||
for (int i = 0; i < 500 && !(gone = zombie(peek_child)); i++) usleep(10000);
|
||||
printf("shimexited\t%d\n", gone);
|
||||
if (write(peek_in[1], "11111111", 8) != 8) return 1; // for each look
|
||||
pthread_join(waiter, NULL);
|
||||
return 0;
|
||||
@@ -821,7 +911,9 @@ class FakeSpawner(threading.Thread):
|
||||
self.root = root
|
||||
self.reports = reports # False: as an app that died with its worker, no exit report
|
||||
self.requests = []
|
||||
self.pids = [] # the programs it ran, ended with the test whatever becomes of them
|
||||
# The programs it ran, as pidfds: ended with the test whatever becomes of them, and never another
|
||||
# process given a pid of theirs once they have been reaped
|
||||
self.pidfds = []
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
@@ -856,7 +948,10 @@ class FakeSpawner(threading.Thread):
|
||||
gate_r, gate_w = os.pipe()
|
||||
pid = os.fork()
|
||||
if pid:
|
||||
self.pids.append(pid)
|
||||
try:
|
||||
self.pidfds.append(os.pidfd_open(pid))
|
||||
except (AttributeError, OSError):
|
||||
pass # no pidfds here: such a program is only ended by its own end
|
||||
if pid == 0:
|
||||
os.close(gate_w)
|
||||
if os.read(gate_r, 1) == b"1":
|
||||
@@ -943,11 +1038,13 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
|
||||
@staticmethod
|
||||
def end_programs(spawner):
|
||||
for pid in spawner.pids:
|
||||
while spawner.pidfds:
|
||||
fd = spawner.pidfds.pop()
|
||||
try:
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
except ProcessLookupError:
|
||||
signal.pidfd_send_signal(fd, signal.SIGKILL)
|
||||
except (AttributeError, OSError):
|
||||
pass
|
||||
os.close(fd)
|
||||
|
||||
def run_parent(self, *argv, handoff="target", hook=True, reports=True, pressure=None, as_shell=False, terminal=False,
|
||||
delay=0, extra_env=None):
|
||||
@@ -1013,10 +1110,20 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
try:
|
||||
stdout, err = proc.communicate(given.get("input"), timeout=120)
|
||||
except subprocess.TimeoutExpired:
|
||||
# the programs the spawner ran hold the fixture's output open too
|
||||
# the programs the spawner ran, and those the fixture ran itself, hold its output open too
|
||||
self.end_programs(spawner)
|
||||
proc.kill()
|
||||
stdout, err = proc.communicate()
|
||||
try:
|
||||
os.killpg(proc.pid, signal.SIGKILL) # its own session's group, as it was started
|
||||
except OSError:
|
||||
proc.kill()
|
||||
try:
|
||||
stdout, err = proc.communicate(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
for pipe in (proc.stdout, proc.stderr):
|
||||
if pipe:
|
||||
pipe.close()
|
||||
proc.wait(timeout=10)
|
||||
stdout, err = "", "(its output still open after it was ended)"
|
||||
self.fail("the fixture did not finish:\n%s\n%s" % (stdout, err))
|
||||
out = subprocess.CompletedProcess(proc.args, proc.returncode, stdout, err)
|
||||
result = dict(line.split("\t", 1) for line in out.stdout.splitlines() if "\t" in line)
|
||||
@@ -1192,6 +1299,22 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.assertEqual("1", result.get("own"), out.stdout)
|
||||
self.assertEqual("7", result.get("exit"), out.stdout)
|
||||
|
||||
def test_a_table_given_up_again_and_again_costs_one_fd(self):
|
||||
if self.child_status != "3":
|
||||
self.skipTest("a child here cannot exec an aarch64 program (no binfmt_misc)")
|
||||
result, requests, out = self.run_parent("prepcloseheld", "5", extra_env={"DDH_TEST_LATE_TABLE": "1"})
|
||||
self.assertEqual("1", result.get("tables"), out.stdout + out.stderr)
|
||||
self.assertEqual("5", result.get("again"), out.stdout)
|
||||
self.assertEqual("1", result.get("table"), out.stdout)
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
|
||||
def test_a_waitid_nowait_reports_no_child_whose_entry_went_meanwhile(self):
|
||||
result, requests, out = self.run_parent("nowaitpeek", "sleep")
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
self.assertEqual("1", result.get("taken"), out.stdout + out.stderr)
|
||||
self.assertEqual("1", result.get("echild"), out.stdout)
|
||||
self.assertEqual("0", result.get("lookedpid"), out.stdout)
|
||||
|
||||
def test_a_forked_child_waits_for_no_close_of_its_parents(self):
|
||||
# no table in the parent (a close held across its tries), so the child makes one
|
||||
result, requests, out = self.run_parent("forkclose", "3", extra_env={"DDH_TEST_LATE_TABLE": "1"})
|
||||
@@ -1209,7 +1332,10 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
def test_a_stop_seen_before_the_child_hands_off_reports_its_program(self):
|
||||
result, requests, out = self.run_parent("stoppeek", "6", delay=1.0)
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
self.assertEqual("1", result.get("waited"), out.stdout + out.stderr)
|
||||
self.assertEqual("1", result.get("shimexited"), out.stdout + out.stderr)
|
||||
# the one wait that saw the stop reports the program's end, not a stop and not the forked child's
|
||||
self.assertEqual("1", result.get("waited"), out.stdout)
|
||||
self.assertEqual("0", result.get("stopped"), out.stdout)
|
||||
self.assertEqual("6", result.get("code"), out.stdout)
|
||||
|
||||
def test_two_waits_report_a_handed_off_child_once(self):
|
||||
@@ -1317,6 +1443,8 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.assertEqual("1", result.get("reused"), out.stdout + out.stderr)
|
||||
if live == "live":
|
||||
self.assertEqual("1", result.get("claimed"), out.stdout)
|
||||
if live == "early":
|
||||
self.assertEqual("1", result.get("recorded"), out.stdout)
|
||||
self.assertEqual("0", result.get("spawned"), out.stdout)
|
||||
self.assertEqual("1", result.get("reaped"), out.stdout)
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
|
||||
Reference in new issue
Block a user