mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(handoff): one lock over a pidfd move, its dup and the undo
This commit is contained in:
1 parent
87fbe91940
commit
784df6260a
2 files changed
+32
-4
No files matched your search
@@ -312,6 +312,8 @@ static void ddh_keep(int fd) {
|
||||
// then fails, the move is undone (ddh_parent_pidfd_settle()). Returns the number moved from, or -1.
|
||||
static int ddh_parent_pidfd_moves(int target);
|
||||
static void ddh_parent_pidfd_settle(int from, int done);
|
||||
static int ddh_parent_pidfd_lock(void);
|
||||
static void ddh_parent_pidfd_unlock(int locked);
|
||||
|
||||
static void ddh_drop_worker_binder(void) {
|
||||
if (g_ddh_nbinder == 0 || getpid() == g_ddh_worker) return;
|
||||
@@ -370,17 +372,36 @@ static int ddh_parent_pidfd_moves(int target) {
|
||||
|
||||
static void ddh_parent_pidfd_settle(int from, int done) {
|
||||
if (from < 0 || done) return;
|
||||
// the dup failed and left the number as it was: the pidfd is still there
|
||||
// the dup failed and left the number as it was: the pidfd is still there. The copy's slot in
|
||||
// the kept list goes first, so that a full list has room for the original again.
|
||||
int moved = __atomic_exchange_n(&g_ddh_parent_pidfd, from, __ATOMIC_ACQ_REL);
|
||||
if (moved >= 0 && moved != from) ddh_unkeep(moved);
|
||||
ddh_keep(from);
|
||||
if (moved >= 0 && moved != from) {
|
||||
ddh_unkeep(moved);
|
||||
int saved = errno;
|
||||
syscall(SYS_close, moved);
|
||||
errno = saved;
|
||||
}
|
||||
}
|
||||
|
||||
// Held from the move through the dup to the settling, in a handed-off program (the one process with
|
||||
// the pidfd): another thread's dup2() onto the number in between would otherwise be taken for the
|
||||
// pidfd when a failed dup is undone. Released in a forked child.
|
||||
static pthread_mutex_t g_ddh_pidfd_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void ddh_pidfd_hold(void) { pthread_mutex_lock(&g_ddh_pidfd_lock); }
|
||||
static void ddh_pidfd_release(void) { pthread_mutex_unlock(&g_ddh_pidfd_lock); }
|
||||
|
||||
static int ddh_parent_pidfd_lock(void) {
|
||||
if (__atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) < 0) return 0;
|
||||
ddh_pidfd_hold();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ddh_parent_pidfd_unlock(int locked) {
|
||||
if (locked) ddh_pidfd_release();
|
||||
}
|
||||
|
||||
// The line of /proc/PID/cgroup that names the app's per-process cgroup ("...:/uid_U/pid_P").
|
||||
static int ddh_cgroup_line(pid_t pid, const char *want, char *out, size_t size) {
|
||||
char path[48], buf[512];
|
||||
@@ -589,6 +610,7 @@ static void ddh_setup_report(void) {
|
||||
}
|
||||
ddh_keep(pidfd);
|
||||
g_ddh_parent_pidfd = pidfd;
|
||||
pthread_atfork(ddh_pidfd_hold, ddh_pidfd_release, ddh_pidfd_release);
|
||||
}
|
||||
on_exit(ddh_report_exit, (void *)1);
|
||||
ddh_register_group();
|
||||
|
||||
@@ -251,7 +251,7 @@ int close(int fd) {
|
||||
int dup2(int oldfd, int newfd) {
|
||||
static int (*orig_dup2)(int, int) = NULL;
|
||||
if (!orig_dup2) orig_dup2 = (int (*)(int, int))dlsym(RTLD_NEXT, "dup2");
|
||||
int pidfd_from = -1;
|
||||
int pidfd_from = -1, locked = ddh_parent_pidfd_lock();
|
||||
if (oldfd != newfd) {
|
||||
pidfd_from = ddh_parent_pidfd_moves(newfd);
|
||||
clear_dns_redirect(newfd);
|
||||
@@ -259,6 +259,9 @@ int dup2(int oldfd, int newfd) {
|
||||
}
|
||||
int ret = orig_dup2 ? orig_dup2(oldfd, newfd) : -1;
|
||||
ddh_parent_pidfd_settle(pidfd_from, ret >= 0);
|
||||
int saved = errno;
|
||||
ddh_parent_pidfd_unlock(locked);
|
||||
errno = saved;
|
||||
if (ret >= 0) copy_proc_root_fd(oldfd, ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -266,7 +269,7 @@ int dup2(int oldfd, int newfd) {
|
||||
int dup3(int oldfd, int newfd, int flags) {
|
||||
static int (*orig_dup3)(int, int, int) = NULL;
|
||||
if (!orig_dup3) orig_dup3 = (int (*)(int, int, int))dlsym(RTLD_NEXT, "dup3");
|
||||
int pidfd_from = -1;
|
||||
int pidfd_from = -1, locked = ddh_parent_pidfd_lock();
|
||||
if (oldfd != newfd) {
|
||||
pidfd_from = ddh_parent_pidfd_moves(newfd);
|
||||
clear_dns_redirect(newfd);
|
||||
@@ -274,6 +277,9 @@ int dup3(int oldfd, int newfd, int flags) {
|
||||
}
|
||||
int ret = orig_dup3 ? orig_dup3(oldfd, newfd, flags) : -1;
|
||||
ddh_parent_pidfd_settle(pidfd_from, ret >= 0);
|
||||
int saved = errno;
|
||||
ddh_parent_pidfd_unlock(locked);
|
||||
errno = saved;
|
||||
if (ret >= 0) copy_proc_root_fd(oldfd, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user