commit 762a2f5afff16d20f9f2e36edd1b5a5ec3b4cb93
parent eda4a10d379af69a267fc56cf9c88cac57616050
Author: Nika Layzell <nika@thelayzells.com>
Date: Tue, 16 Dec 2025 04:53:44 +0000
Bug 1908693 - Part 1: Propagate MOZ_ environment variables into child processes on iOS, r=glandium
On desktop platforms, environment variables are implicitly inherited by content
processes, and on Android, custom environment variables are generally set as
part of GeckoThread initialization, which is performed in every process.
As these env vars are not inherited on iOS, we need to manually specify them
for child processes. This patch does this in a fairly hacky way, by enumerating
all environment variables, and copying ones with a `MOZ_` prefix to child
processes as they are created.
We probably want to have a better system for this in the future.
Differential Revision: https://phabricator.services.mozilla.com/D217131
Diffstat:
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/ipc/glue/GeckoChildProcessHost.cpp b/ipc/glue/GeckoChildProcessHost.cpp
@@ -121,6 +121,11 @@ static bool ShouldHaveDirectoryService() {
return GeckoProcessType_Default == XRE_GetProcessType();
}
+#ifdef XP_IOS
+// Hack to ensure environment variables are copied into child processes on iOS.
+extern char** environ;
+#endif
+
namespace mozilla {
namespace ipc {
@@ -345,6 +350,7 @@ class IosProcessLauncher : public PosixProcessLauncher {
: PosixProcessLauncher(aHost, std::move(aExtraOpts)) {}
protected:
+ virtual Result<Ok, LaunchError> DoSetup() override;
virtual RefPtr<ProcessLaunchPromise> DoLaunch() override;
DarwinObjectPtr<xpc_object_t> mBootstrapMessage;
@@ -1357,6 +1363,38 @@ RefPtr<ProcessLaunchPromise> PosixProcessLauncher::DoLaunch() {
#endif // XP_UNIX
#ifdef XP_IOS
+Result<Ok, LaunchError> IosProcessLauncher::DoSetup() {
+ Result<Ok, LaunchError> aError = PosixProcessLauncher::DoSetup();
+ if (aError.isErr()) {
+ return aError;
+ }
+
+ // Unlike on other platforms, the child process will not inherit the parent
+ // process's environment, so we need to manually copy over environment
+ // variables.
+ for (char** envp = environ; *envp != 0; ++envp) {
+ // Only copy over `MOZ_` keys.
+ if (strncmp(*envp, "MOZ_", 4) != 0) {
+ continue;
+ }
+
+ std::string_view env{*envp};
+ size_t eqIdx = env.find('=');
+ if (eqIdx == std::string_view::npos) {
+ continue;
+ }
+
+ std::string key{env.substr(0, eqIdx)};
+ if (mLaunchOptions->env_map.count(key)) {
+ // If the key already exists in the map, we don't want to overwrite it.
+ continue;
+ }
+
+ mLaunchOptions->env_map[key] = env.substr(eqIdx + 1);
+ }
+ return Ok();
+}
+
RefPtr<ProcessLaunchPromise> IosProcessLauncher::DoLaunch() {
ExtensionKitProcess::Kind kind = ExtensionKitProcess::Kind::WebContent;
if (mProcessType == GeckoProcessType_GPU) {