You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce preExecProcessAction on Darwin, rename preSpawnProcessConfigurator to preExecProcessAction on Linux (#140)
This patch introduces a preExecProcessAction on Darwin, which runs immediately before posix_spawn-as-exec, just as preSpawnProcessConfigurator (now called preExecProcessAction) runs immediately before exe on Linux.
The reason to introduce this API is because there is presently no way to run code _after_ fork on Darwin in the case where pre-forking occurs (which is true when setting a uid, gid, sgroups, or creating a session). This can be useful when performing operations that modify process global state and wanting to do so without affecting the parent.
The reason to rename the Linux API is that these are fundamentally two different logical operations: one is modifying flags to the process spawning mechanism (CreateProcessW, posix_spawn), while the other is performing an action at a specific point in the process lifecycle (before exec). Only Darwin uses posix_spawn, so there is no opportunity to modify any flags, and preSpawnProcessConfigurator is therefore not available on Linux/Android/FreeBSD/etc. On Windows, there is no fork/exec concept, so preExecProcessAction is not available on Windows.
This brings the set of APIs to:
- preSpawnProcessConfigurator (Windows, Darwin) - modifies flags bitset / STARTUPINFOW, or posix_spawnattr_t / posix_spawn_file_actions_t
- preExecProcessAction (Darwin, Linux/Android/FreeBSD/etc.) - runs before exec or posix_spawn-as-exec
or
- Windows: preSpawnProcessConfigurator
- Darwin: preSpawnProcessConfigurator + preExecProcessAction
- Linux/Android/FreeBSD/etc.: preExecProcessAction
This means that Darwin has both preSpawnProcessConfigurator and preExecProcessAction, because the pre-forking case uses posix_spawn _and_ fork. It also give us the opportunity to introduce preSpawnProcessConfigurator on Linux and other platforms in the future if they introduce a similar POSIX_SPAWN_CLOEXEC_DEFAULT flag or POSIX otherwise standardizes a mechanism to disable file descriptor inheritance by default, since that would allow us to use posix_spawn on those platforms.
Note that merely setting preExecProcessAction will also cause pre-forking to occur.
Closes#25
Copy file name to clipboardExpand all lines: Sources/Subprocess/Platforms/Subprocess+Darwin.swift
+26-1Lines changed: 26 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,29 @@ public struct PlatformOptions: Sendable {
81
81
inoutposix_spawn_file_actions_t?
82
82
)throws->Void
83
83
)?=nil
84
+
/// A closure to configure platform-specific
85
+
/// spawning constructs. This closure enables direct
86
+
/// configuration or override of underlying platform-specific
87
+
/// spawn settings that `Subprocess` utilizes internally,
88
+
/// in cases where Subprocess does not provide higher-level
89
+
/// APIs for such modifications.
90
+
///
91
+
/// On Darwin, Subprocess uses `posix_spawn()` as the
92
+
/// underlying spawning mechanism, but may require an initial `fork()`
93
+
/// depending on the configured `PlatformOptions`.
94
+
/// This closure is called after `fork()` but before `posix_spawn()`
95
+
/// (with the `POSIX_SPAWN_SETEXEC` flag set).
96
+
/// You may use it to call any necessary process setup functions.
97
+
///
98
+
/// - note: You can set both `preExecProcessAction` and
99
+
/// `preSpawnProcessConfigurator` and both will be called.
100
+
/// Setting `preExecProcessAction` will always cause Subprocess
101
+
/// to pre-`fork()` before calling `posix_spawn()` (with the
102
+
/// `POSIX_SPAWN_SETEXEC` flag set) even if it would not have otherwise
103
+
/// done so based on the configured `PlatformOptions`.
104
+
///
105
+
/// - warning: You may ONLY call [async-signal-safe functions](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html) within this closure (note _"The following table defines a set of functions and function-like macros that shall be async-signal-safe."_).
/// - warning: You may ONLY call [async-signal-safe functions](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html) within this closure (note _"The following table defines a set of functions and function-like macros that shall be async-signal-safe."_).
letmatch=try #require(try#/\s*PID\s*PGID\s*TPGID\s*(?<pid>[\-]?[0-9]+)\s*(?<pgid>[\-]?[0-9]+)\s*(?<tpgid>[\-]?[0-9]+)\s*/#.wholeMatch(in: psValue),"ps output was in an unexpected format:\n\n\(psValue)")
989
1023
// If setsid() has been called successfully, we should observe:
0 commit comments