From 9db32752eea3202b7935d614a34532c43ccb3905 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sun, 6 Dec 2026 09:21:31 +0000 Subject: [PATCH] fix(audio): keep RunningBoard from suspending actively playing background-audio apps Previously shim_rb_audio() in host/simshim/compat/simshim.c unconditionally returned NO to runningboardd at -[RBProcess _lock_suspend]. Because x86Sim does not run mediaserverd (libsimaudio.dylib interposes CoreAudio HAL directly), runningboardd suspended UIBackgroundModes:["audio"] apps immediately upon backgrounding even while audio was actively playing. Have libsimaudio.dylib hold a SIGKILL-safe advisory flock on /simaudio-out-bundle/.lock while an output IO stream is active (dev_start -> dev_stop), and have shim_rb_audio() return YES when that lock is held while still returning NO to suspend idle background-audio apps (Maps, Health) once playback stops or after process exit. --- host/simaudio/simaudio.c | 50 +++++++++++++++++++++++++++++++-- host/simshim/compat/simshim.c | 52 +++++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/host/simaudio/simaudio.c b/host/simaudio/simaudio.c index 36bf1359..701792d8 100644 --- a/host/simaudio/simaudio.c +++ b/host/simaudio/simaudio.c @@ -27,7 +27,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -141,6 +143,7 @@ struct dev { struct ioproc procs[MAXPROCS]; pthread_t thread; int threadrun; int sock; // Linux connection + int active_lock_fd; // flock'd marker for RunningBoard active-audio gating (output only) uint64_t t0; double sample0; // host time at sample time sample0 (anchor of the clock) float* ring; size_t ringcap, ringr, ringw; // input: frames read from Linux pthread_mutex_t lock; @@ -148,8 +151,8 @@ struct dev { float appvol; int appmute; // the process's own volume / mute ('appv' / 'appm', AudioSession sets it from sim_volume) }; static struct dev devs[2] = { - { kOutDev, kOutStream, 0, 2, 48000, 512, .sock = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 }, - { kInDev, kInStream, 1, 1, 48000, 512, .sock = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 }, + { kOutDev, kOutStream, 0, 2, 48000, 512, .sock = -1, .active_lock_fd = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 }, + { kInDev, kInStream, 1, 1, 48000, 512, .sock = -1, .active_lock_fd = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 }, }; static struct dev* devof(AudioObjectID o) { if (o == kOutDev || o == kOutStream || o == kOutAgg || o == kIOAgg) return &devs[0]; @@ -363,6 +366,46 @@ static void* iothread(void* arg) { return NULL; } +static int valid_bundle_component(const char* s) { + if (!s || !*s) return 0; + for (const char* p = s; *p; p++) { + unsigned char c = (unsigned char)*p; + int ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'; + if (!ok) return 0; + if (c == '.' && p[1] == '.') return 0; + } + return 1; +} + +static void audio_lock_acquire(struct dev* d) { + if (d->input || d->active_lock_fd >= 0) return; + const char* sock = getenv("X86SIM_AUDIO_SOCKET"); + if (!sock || !*sock) return; + const char* slash = strrchr(sock, '/'); + if (!slash || slash == sock) return; + CFBundleRef mb = CFBundleGetMainBundle(); + CFStringRef bid = mb ? CFBundleGetIdentifier(mb) : NULL; + char bstr[128]; + if (!bid || !CFStringGetCString(bid, bstr, sizeof(bstr), kCFStringEncodingUTF8) || !valid_bundle_component(bstr)) return; + char dir_path[512], lock_path[640]; + int n = snprintf(dir_path, sizeof(dir_path), "%.*s/simaudio-out-bundle", (int)(slash - sock), sock); + if (n <= 0 || (size_t)n >= sizeof(dir_path)) return; + mkdir(dir_path, 0755); + n = snprintf(lock_path, sizeof(lock_path), "%s/%s.lock", dir_path, bstr); + if (n <= 0 || (size_t)n >= sizeof(lock_path)) return; + int fd = open(lock_path, O_CREAT | O_RDWR | O_CLOEXEC, 0644); + if (fd < 0) return; + if (flock(fd, LOCK_EX | LOCK_NB) == 0) d->active_lock_fd = fd; + else close(fd); +} + +static void audio_lock_release(struct dev* d) { + if (d->active_lock_fd < 0) return; + flock(d->active_lock_fd, LOCK_UN); + close(d->active_lock_fd); + d->active_lock_fd = -1; +} + static void dev_start(struct dev* d) { if (d->threadrun) return; timebase(); @@ -372,6 +415,8 @@ static void dev_start(struct dev* d) { d->ringr = d->ringw = 0; d->sock = connect_linux(d); if (d->sock >= 0) { pthread_t t; pthread_create(&t, NULL, input_reader, d); pthread_detach(t); } + } else { + audio_lock_acquire(d); } d->threadrun = 1; pthread_create(&d->thread, NULL, iothread, d); @@ -384,6 +429,7 @@ static void dev_stop(struct dev* d) { if (!pthread_equal(pthread_self(), d->thread)) pthread_join(d->thread, NULL); else pthread_detach(d->thread); d->sample0 += 0; // the sample clock continues from the anchor on the next start if (d->sock >= 0) { shutdown(d->sock, SHUT_RDWR); close(d->sock); d->sock = -1; } + audio_lock_release(d); notify(d->id, FCC("goin"), FCC("glob")); } diff --git a/host/simshim/compat/simshim.c b/host/simshim/compat/simshim.c index c63bab22..9d207b05 100644 --- a/host/simshim/compat/simshim.c +++ b/host/simshim/compat/simshim.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -1000,13 +1001,60 @@ __attribute__((constructor)) static void shim_owner_init(void) // real value. static uintptr_t shim_rb_site; // return address of _lock_suspend's [props supportsBackgroundAudio] static IMP shim_rb_audio_orig[2]; +static int shim_rb_valid_bundle(const char* s) +{ + if (!s || !*s) + return 0; + for (const char* p = s; *p; p++) { + unsigned char c = (unsigned char) *p; + int ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'; + if (!ok || (c == '.' && p[1] == '.')) + return 0; + } + return 1; +} +// Keep background-audio apps running while libsimaudio holds an active output stream lock +// (standing in for mediaserverd's RunningBoard running assertion on a real iPhone); still suspend +// idle UIBackgroundModes:["audio"] apps (Maps, Health) once playback stops or after SIGKILL. +static int shim_rb_audio_stream_active(id self) +{ + const char* sock = getenv("X86SIM_AUDIO_SOCKET"); + if (!sock || !*sock) + return 0; + const char* slash = strrchr(sock, '/'); + if (!slash || slash == sock) + return 0; + SEL bsel = sel_registerName("bundleIdentifier"); + if (!((BOOL (*)(id, SEL, SEL)) objc_msgSend)(self, sel_registerName("respondsToSelector:"), bsel)) + return 0; + id bid = ((id (*)(id, SEL)) objc_msgSend)(self, bsel); + if (!bid || CFGetTypeID((CFTypeRef) bid) != CFStringGetTypeID()) + return 0; + char bstr[128]; + if (!CFStringGetCString((CFStringRef) bid, bstr, sizeof bstr, kCFStringEncodingUTF8) || !shim_rb_valid_bundle(bstr)) + return 0; + char lock_path[640]; + int n = snprintf(lock_path, sizeof lock_path, "%.*s/simaudio-out-bundle/%s.lock", (int) (slash - sock), sock, bstr); + if (n <= 0 || (size_t) n >= sizeof lock_path) + return 0; + int fd = open(lock_path, O_RDONLY | O_CLOEXEC); + if (fd < 0) + return 0; + int busy = (flock(fd, LOCK_EX | LOCK_NB) != 0 && (errno == EWOULDBLOCK || errno == EAGAIN)); + if (!busy) + flock(fd, LOCK_UN); + close(fd); + return busy; +} static BOOL shim_rb_audio(id self, SEL sel, int k, uintptr_t ret) { BOOL r = ((BOOL (*)(id, SEL)) shim_rb_audio_orig[k])(self, sel); - if (ret != shim_rb_site) + if (ret != shim_rb_site || !r) return r; + if (shim_rb_audio_stream_active(self)) + return YES; static int n; - if (r && n++ < 20) + if (n++ < 20) write(2, "simshim: RunningBoard suspends a background-audio app\n", 54); return NO; }