Skip to content

Commit 1e3b535

Browse files
Johan Knutzenianlancetaylor
Johan Knutzen
authored andcommitted
syscall: expose bInheritHandles of CreateProcess
Certain use cases require this parameter to be false. This includes spawning a child process in a different windows session than session 0. Docs regarding the behavior of this parameter to CreateProcess: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa Fixes #42098 Change-Id: If998f57d6f2962824aacbee75e1b508b255ab293 GitHub-Last-Rev: 584eb13 GitHub-Pull-Request: #41957 Reviewed-on: https://go-review.googlesource.com/c/go/+/261917 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Tobias Klauser <[email protected]> Trust: Alex Brainman <[email protected]>
1 parent c018eec commit 1e3b535

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

doc/go1.16.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
378378
</dd>
379379
</dl><!-- runtime/debug -->
380380

381+
<dl id="syscall"><dt><a href="/pkg/syscall/">syscall</a></dt>
382+
<dd>
383+
<p><!-- CL 261917 -->
384+
<a href="/pkg/syscall/#SysProcAttr"><code>SysProcAttr</code></a> on Windows has a new NoInheritHandles field that disables inheriting handles when creating a new process.
385+
</p>
386+
</dd>
387+
</dl><!-- syscall -->
388+
381389
<dl id="strconv"><dt><a href="/pkg/strconv/">strconv</a></dt>
382390
<dd>
383391
<p><!-- CL 260858 -->

src/syscall/exec_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ type SysProcAttr struct {
241241
Token Token // if set, runs new process in the security context represented by the token
242242
ProcessAttributes *SecurityAttributes // if set, applies these security attributes as the descriptor for the new process
243243
ThreadAttributes *SecurityAttributes // if set, applies these security attributes as the descriptor for the main thread of the new process
244+
NoInheritHandles bool // if set, each inheritable handle in the calling process is not inherited by the new process
244245
}
245246

246247
var zeroProcAttr ProcAttr
@@ -341,9 +342,9 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
341342

342343
flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT
343344
if sys.Token != 0 {
344-
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, true, flags, createEnvBlock(attr.Env), dirp, si, pi)
345+
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, si, pi)
345346
} else {
346-
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, true, flags, createEnvBlock(attr.Env), dirp, si, pi)
347+
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, si, pi)
347348
}
348349
if err != nil {
349350
return 0, 0, err

0 commit comments

Comments
 (0)