@@ -60,8 +60,8 @@ pub const ChildProcess = struct {
6060 /// Use this for additional posix spawn attributes.
6161 /// Do not set spawn attribute flags and do not modify stdin, stdout, stderr
6262 /// behavior, because those are set in the platform-specific spawn method.
63- posix_attr : if (builtin . target . isDarwin () ) ? os .posix_spawn .Attr else void ,
64- posix_actions : if (builtin . target . isDarwin () ) ? os .posix_spawn .Actions else void ,
63+ posix_attr : if (os . hasPosixSpawn ) ? os .posix_spawn .Attr else void ,
64+ posix_actions : if (os . hasPosixSpawn ) ? os .posix_spawn .Actions else void ,
6565
6666 /// Darwin-only. Disable ASLR for the child process.
6767 disable_aslr : bool = false ,
@@ -111,11 +111,6 @@ pub const ChildProcess = struct {
111111 child_to_parent ,
112112 };
113113
114- pub const PosixData = union (enum ) {
115- Attribute : os.posix_spawn.Attr ,
116- Actions : os.posix_spawn.Actions ,
117- };
118-
119114 /// First argument in argv is the executable.
120115 pub fn init (argv : []const []const u8 , allocator : mem.Allocator ) ChildProcess {
121116 return .{
@@ -137,8 +132,8 @@ pub const ChildProcess = struct {
137132 .stdout_behavior = StdIo .Inherit ,
138133 .stderr_behavior = StdIo .Inherit ,
139134 .expand_arg0 = .no_expand ,
140- .posix_attr = if (comptime builtin . target . isDarwin () ) null else undefined ,
141- .posix_actions = if (comptime builtin . target . isDarwin () ) null else undefined ,
135+ .posix_attr = if (os . hasPosixSpawn ) null else undefined ,
136+ .posix_actions = if (os . hasPosixSpawn ) null else undefined ,
142137 };
143138 }
144139
@@ -154,7 +149,7 @@ pub const ChildProcess = struct {
154149 @compileError ("the target operating system cannot spawn processes" );
155150 }
156151
157- if (comptime builtin . target . isDarwin () ) {
152+ if (os . hasPosixSpawn ) {
158153 return self .spawnMacos ();
159154 }
160155
@@ -486,7 +481,7 @@ pub const ChildProcess = struct {
486481 }
487482
488483 fn waitUnwrapped (self : * ChildProcess ) ! void {
489- const res : os.WaitPidResult = if (comptime builtin . target . isDarwin () )
484+ const res : os.WaitPidResult = if (os . hasPosixSpawn )
490485 try os .posix_spawn .waitpid (self .pid , 0 )
491486 else
492487 os .waitpid (self .pid , 0 );
@@ -566,9 +561,9 @@ pub const ChildProcess = struct {
566561 }
567562
568563 fn spawnMacos (self : * ChildProcess ) SpawnError ! void {
569- // cleanup user-initialization and initialization in this function
570- defer if ( self .posix_attr != null ) self . posix_attr .? . deinit () ;
571- defer if ( self .posix_actions != null ) self . posix_actions .? . deinit () ;
564+ // dont cleanup structure owned == initialized by user
565+ const user_attr : bool = self .posix_attr != null ;
566+ const user_actions : bool = self .posix_actions != null ;
572567
573568 const pipe_flags = if (io .is_async ) os .O .NONBLOCK else 0 ;
574569 const stdin_pipe = if (self .stdin_behavior == StdIo .Pipe ) try os .pipe2 (pipe_flags ) else undefined ;
@@ -597,8 +592,8 @@ pub const ChildProcess = struct {
597592 undefined ;
598593 defer if (any_ignore ) os .close (dev_null_fd );
599594
600- if (self .posix_attr == null )
601- self .posix_attr = try os . posix_spawn . Attr . init ();
595+ if (user_attr == false ) self .posix_attr = try os . posix_spawn . Attr . init ();
596+ defer if ( user_attr == false ) self .posix_attr .? . deinit ();
602597 var flags : u16 = os .darwin .POSIX_SPAWN_SETSIGDEF | os .darwin .POSIX_SPAWN_SETSIGMASK ;
603598 if (self .disable_aslr ) {
604599 flags |= os .darwin ._POSIX_SPAWN_DISABLE_ASLR ;
@@ -608,8 +603,8 @@ pub const ChildProcess = struct {
608603 }
609604 try self .posix_attr .? .set (flags );
610605
611- if (self .posix_actions == null )
612- self .posix_actions = try os . posix_spawn . Actions . init ();
606+ if (user_actions == false ) self .posix_actions = try os . posix_spawn . Actions . init ();
607+ defer if ( user_actions == false ) self .posix_actions .? . deinit ();
613608
614609 try setUpChildIoPosixSpawn (self .stdin_behavior , & self .posix_actions .? , stdin_pipe , os .STDIN_FILENO , dev_null_fd );
615610 try setUpChildIoPosixSpawn (self .stdout_behavior , & self .posix_actions .? , stdout_pipe , os .STDOUT_FILENO , dev_null_fd );
@@ -947,6 +942,9 @@ pub const ChildProcess = struct {
947942 const cmd_line = try windowsCreateCommandLine (self .allocator , self .argv );
948943 defer self .allocator .free (cmd_line );
949944
945+ // TODO use explicit list of file handles
946+ // https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873
947+
950948 var siStartInfo = windows.STARTUPINFOW {
951949 .cb = @sizeOf (windows .STARTUPINFOW ),
952950 .hStdError = g_hChildStd_ERR_Wr ,
0 commit comments