Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Fork, exec, wait, etc. |
| 6 | |
| 7 | package syscall |
| 8 | |
| 9 | import ( |
| 10 | "sync" |
Rob Pike | 45e3bcb | 2011-11-08 15:41:54 -0800 | [diff] [blame] | 11 | "unicode/utf16" |
Alex Brainman | 5aad514 | 2011-04-21 10:36:27 +1000 | [diff] [blame] | 12 | "unsafe" |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 13 | ) |
| 14 | |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 15 | var ForkLock sync.RWMutex |
| 16 | |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 17 | // EscapeArg rewrites command line argument s as prescribed |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 18 | // in http://msdn.microsoft.com/en-us/library/ms880421. |
| 19 | // This function returns "" (2 double quotes) if s is empty. |
| 20 | // Alternatively, these transformations are done: |
| 21 | // - every back slash (\) is doubled, but only if immediately |
| 22 | // followed by double quote ("); |
| 23 | // - every double quote (") is escaped by back slash (\); |
| 24 | // - finally, s is wrapped with double quotes (arg -> "arg"), |
| 25 | // but only if there is space or tab inside s. |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 26 | func EscapeArg(s string) string { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 27 | if len(s) == 0 { |
| 28 | return "\"\"" |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 29 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 30 | n := len(s) |
| 31 | hasSpace := false |
| 32 | for i := 0; i < len(s); i++ { |
| 33 | switch s[i] { |
| 34 | case '"', '\\': |
| 35 | n++ |
| 36 | case ' ', '\t': |
| 37 | hasSpace = true |
| 38 | } |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 39 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 40 | if hasSpace { |
| 41 | n += 2 |
| 42 | } |
| 43 | if n == len(s) { |
| 44 | return s |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 45 | } |
| 46 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 47 | qs := make([]byte, n) |
| 48 | j := 0 |
| 49 | if hasSpace { |
| 50 | qs[j] = '"' |
| 51 | j++ |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 52 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 53 | slashes := 0 |
| 54 | for i := 0; i < len(s); i++ { |
| 55 | switch s[i] { |
| 56 | default: |
| 57 | slashes = 0 |
| 58 | qs[j] = s[i] |
| 59 | case '\\': |
| 60 | slashes++ |
| 61 | qs[j] = s[i] |
| 62 | case '"': |
| 63 | for ; slashes > 0; slashes-- { |
| 64 | qs[j] = '\\' |
| 65 | j++ |
| 66 | } |
| 67 | qs[j] = '\\' |
| 68 | j++ |
| 69 | qs[j] = s[i] |
| 70 | } |
| 71 | j++ |
| 72 | } |
| 73 | if hasSpace { |
| 74 | for ; slashes > 0; slashes-- { |
| 75 | qs[j] = '\\' |
| 76 | j++ |
| 77 | } |
| 78 | qs[j] = '"' |
| 79 | j++ |
| 80 | } |
| 81 | return string(qs[:j]) |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 82 | } |
| 83 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 84 | // makeCmdLine builds a command line out of args by escaping "special" |
| 85 | // characters and joining the arguments with spaces. |
| 86 | func makeCmdLine(args []string) string { |
| 87 | var s string |
| 88 | for _, v := range args { |
| 89 | if s != "" { |
| 90 | s += " " |
| 91 | } |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 92 | s += EscapeArg(v) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 93 | } |
| 94 | return s |
| 95 | } |
| 96 | |
| 97 | // createEnvBlock converts an array of environment strings into |
| 98 | // the representation required by CreateProcess: a sequence of NUL |
| 99 | // terminated strings followed by a nil. |
| 100 | // Last bytes are two UCS-2 NULs, or four NUL bytes. |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 101 | func createEnvBlock(envv []string) *uint16 { |
| 102 | if len(envv) == 0 { |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 103 | return &utf16.Encode([]rune("\x00\x00"))[0] |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 104 | } |
| 105 | length := 0 |
| 106 | for _, s := range envv { |
| 107 | length += len(s) + 1 |
| 108 | } |
| 109 | length += 1 |
| 110 | |
| 111 | b := make([]byte, length) |
| 112 | i := 0 |
| 113 | for _, s := range envv { |
| 114 | l := len(s) |
| 115 | copy(b[i:i+l], []byte(s)) |
| 116 | copy(b[i+l:i+l+1], []byte{0}) |
| 117 | i = i + l + 1 |
| 118 | } |
| 119 | copy(b[i:i+1], []byte{0}) |
| 120 | |
Russ Cox | db33959 | 2011-10-25 22:20:02 -0700 | [diff] [blame] | 121 | return &utf16.Encode([]rune(string(b)))[0] |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 122 | } |
| 123 | |
Wei Guangjing | 63b8b948 | 2011-07-01 10:18:07 -0400 | [diff] [blame] | 124 | func CloseOnExec(fd Handle) { |
| 125 | SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0) |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 126 | } |
| 127 | |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 128 | func SetNonblock(fd Handle, nonblocking bool) (err error) { |
| 129 | return nil |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 130 | } |
| 131 | |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 132 | // FullPath retrieves the full path of the specified file. |
| 133 | func FullPath(name string) (path string, err error) { |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 134 | p, err := UTF16PtrFromString(name) |
| 135 | if err != nil { |
| 136 | return "", err |
| 137 | } |
Alex Brainman | 32e75ba | 2015-02-13 16:12:07 +1100 | [diff] [blame] | 138 | n := uint32(100) |
| 139 | for { |
| 140 | buf := make([]uint16, n) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 141 | n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 142 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 143 | return "", err |
| 144 | } |
Alex Brainman | 32e75ba | 2015-02-13 16:12:07 +1100 | [diff] [blame] | 145 | if n <= uint32(len(buf)) { |
| 146 | return UTF16ToString(buf[:n]), nil |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 147 | } |
| 148 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | func isSlash(c uint8) bool { |
| 152 | return c == '\\' || c == '/' |
| 153 | } |
| 154 | |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 155 | func normalizeDir(dir string) (name string, err error) { |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 156 | ndir, err := FullPath(dir) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 157 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 158 | return "", err |
| 159 | } |
| 160 | if len(ndir) > 2 && isSlash(ndir[0]) && isSlash(ndir[1]) { |
| 161 | // dir cannot have \\server\share\path form |
| 162 | return "", EINVAL |
| 163 | } |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 164 | return ndir, nil |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | func volToUpper(ch int) int { |
| 168 | if 'a' <= ch && ch <= 'z' { |
| 169 | ch += 'A' - 'a' |
| 170 | } |
| 171 | return ch |
| 172 | } |
| 173 | |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 174 | func joinExeDirAndFName(dir, p string) (name string, err error) { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 175 | if len(p) == 0 { |
| 176 | return "", EINVAL |
| 177 | } |
| 178 | if len(p) > 2 && isSlash(p[0]) && isSlash(p[1]) { |
| 179 | // \\server\share\path form |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 180 | return p, nil |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 181 | } |
| 182 | if len(p) > 1 && p[1] == ':' { |
| 183 | // has drive letter |
| 184 | if len(p) == 2 { |
| 185 | return "", EINVAL |
| 186 | } |
| 187 | if isSlash(p[2]) { |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 188 | return p, nil |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 189 | } else { |
| 190 | d, err := normalizeDir(dir) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 191 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 192 | return "", err |
| 193 | } |
| 194 | if volToUpper(int(p[0])) == volToUpper(int(d[0])) { |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 195 | return FullPath(d + "\\" + p[2:]) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 196 | } else { |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 197 | return FullPath(p) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | // no drive letter |
| 202 | d, err := normalizeDir(dir) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 203 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 204 | return "", err |
| 205 | } |
| 206 | if isSlash(p[0]) { |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 207 | return FullPath(d[:2] + p) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 208 | } else { |
Alex Brainman | 2de65ca | 2014-08-19 14:59:56 +1000 | [diff] [blame] | 209 | return FullPath(d + "\\" + p) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 210 | } |
| 211 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 212 | } |
| 213 | |
Roger Peppe | aa55c05 | 2011-03-15 14:41:19 -0400 | [diff] [blame] | 214 | type ProcAttr struct { |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 215 | Dir string |
| 216 | Env []string |
Brad Fitzpatrick | fbab6d8 | 2012-02-11 08:47:19 +1100 | [diff] [blame] | 217 | Files []uintptr |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 218 | Sys *SysProcAttr |
| 219 | } |
| 220 | |
| 221 | type SysProcAttr struct { |
Alex Brainman | 0d55d98 | 2012-05-02 17:05:52 +1000 | [diff] [blame] | 222 | HideWindow bool |
| 223 | CmdLine string // used if non-empty, else the windows command line is built by escaping the arguments passed to StartProcess |
| 224 | CreationFlags uint32 |
Alex Brainman | 79f6c28 | 2017-11-07 12:09:59 +1100 | [diff] [blame] | 225 | Token Token // if set, runs new process in the security context represented by the token |
Roger Peppe | aa55c05 | 2011-03-15 14:41:19 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 228 | var zeroProcAttr ProcAttr |
| 229 | var zeroSysProcAttr SysProcAttr |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 230 | |
Wei Guangjing | 16ce2f9 | 2012-02-02 14:08:48 -0500 | [diff] [blame] | 231 | func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 232 | if len(argv0) == 0 { |
| 233 | return 0, 0, EWINDOWS |
| 234 | } |
Roger Peppe | aa55c05 | 2011-03-15 14:41:19 -0400 | [diff] [blame] | 235 | if attr == nil { |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 236 | attr = &zeroProcAttr |
Roger Peppe | aa55c05 | 2011-03-15 14:41:19 -0400 | [diff] [blame] | 237 | } |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 238 | sys := attr.Sys |
| 239 | if sys == nil { |
| 240 | sys = &zeroSysProcAttr |
| 241 | } |
| 242 | |
Roger Peppe | aa55c05 | 2011-03-15 14:41:19 -0400 | [diff] [blame] | 243 | if len(attr.Files) > 3 { |
Alex Brainman | 4ecebfe | 2011-02-04 14:41:26 +1100 | [diff] [blame] | 244 | return 0, 0, EWINDOWS |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 245 | } |
Alex Brainman | 0bafe0e | 2015-06-29 11:19:33 +1000 | [diff] [blame] | 246 | if len(attr.Files) < 3 { |
| 247 | return 0, 0, EINVAL |
| 248 | } |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 249 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 250 | if len(attr.Dir) != 0 { |
| 251 | // StartProcess assumes that argv0 is relative to attr.Dir, |
| 252 | // because it implies Chdir(attr.Dir) before executing argv0. |
| 253 | // Windows CreateProcess assumes the opposite: it looks for |
| 254 | // argv0 relative to the current directory, and, only once the new |
| 255 | // process is started, it does Chdir(attr.Dir). We are adjusting |
| 256 | // for that difference here by making argv0 absolute. |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 257 | var err error |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 258 | argv0, err = joinExeDirAndFName(attr.Dir, argv0) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 259 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 260 | return 0, 0, err |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 261 | } |
| 262 | } |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 263 | argv0p, err := UTF16PtrFromString(argv0) |
| 264 | if err != nil { |
| 265 | return 0, 0, err |
| 266 | } |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 267 | |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 268 | var cmdline string |
| 269 | // Windows CreateProcess takes the command line as a single string: |
| 270 | // use attr.CmdLine if set, else build the command line by escaping |
| 271 | // and joining each argument with spaces |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 272 | if sys.CmdLine != "" { |
| 273 | cmdline = sys.CmdLine |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 274 | } else { |
| 275 | cmdline = makeCmdLine(argv) |
| 276 | } |
| 277 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 278 | var argvp *uint16 |
Vincent Vanackere | f18a4e9 | 2011-05-31 10:21:38 -0400 | [diff] [blame] | 279 | if len(cmdline) != 0 { |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 280 | argvp, err = UTF16PtrFromString(cmdline) |
| 281 | if err != nil { |
| 282 | return 0, 0, err |
| 283 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 284 | } |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 285 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 286 | var dirp *uint16 |
| 287 | if len(attr.Dir) != 0 { |
Alexey Borzenkov | a108369 | 2012-08-05 17:24:32 -0400 | [diff] [blame] | 288 | dirp, err = UTF16PtrFromString(attr.Dir) |
| 289 | if err != nil { |
| 290 | return 0, 0, err |
| 291 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 292 | } |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 293 | |
Hector Chu | fb9fc88 | 2011-02-03 12:50:41 +1100 | [diff] [blame] | 294 | // Acquire the fork lock so that no other threads |
| 295 | // create new fds that are not yet close-on-exec |
| 296 | // before we fork. |
| 297 | ForkLock.Lock() |
| 298 | defer ForkLock.Unlock() |
| 299 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 300 | p, _ := GetCurrentProcess() |
Wei Guangjing | 63b8b948 | 2011-07-01 10:18:07 -0400 | [diff] [blame] | 301 | fd := make([]Handle, len(attr.Files)) |
Robert Griesemer | a2e2868 | 2011-04-13 15:13:59 -0700 | [diff] [blame] | 302 | for i := range attr.Files { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 303 | if attr.Files[i] > 0 { |
Wei Guangjing | 63b8b948 | 2011-07-01 10:18:07 -0400 | [diff] [blame] | 304 | err := DuplicateHandle(p, Handle(attr.Files[i]), p, &fd[i], 0, true, DUPLICATE_SAME_ACCESS) |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 305 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 306 | return 0, 0, err |
| 307 | } |
Wei Guangjing | 63b8b948 | 2011-07-01 10:18:07 -0400 | [diff] [blame] | 308 | defer CloseHandle(Handle(fd[i])) |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 309 | } |
| 310 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 311 | si := new(StartupInfo) |
Alex Brainman | 5aad514 | 2011-04-21 10:36:27 +1000 | [diff] [blame] | 312 | si.Cb = uint32(unsafe.Sizeof(*si)) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 313 | si.Flags = STARTF_USESTDHANDLES |
Russ Cox | 4d0f2e9 | 2011-06-14 10:49:34 -0400 | [diff] [blame] | 314 | if sys.HideWindow { |
Vincent Vanackere | 1f59004 | 2011-04-22 00:12:06 +1000 | [diff] [blame] | 315 | si.Flags |= STARTF_USESHOWWINDOW |
| 316 | si.ShowWindow = SW_HIDE |
| 317 | } |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 318 | si.StdInput = fd[0] |
| 319 | si.StdOutput = fd[1] |
| 320 | si.StdErr = fd[2] |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 321 | |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 322 | pi := new(ProcessInformation) |
| 323 | |
Alex Brainman | 0d55d98 | 2012-05-02 17:05:52 +1000 | [diff] [blame] | 324 | flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT |
Paul Querna | bb98331 | 2017-11-01 15:11:52 -0700 | [diff] [blame] | 325 | if sys.Token != 0 { |
| 326 | err = CreateProcessAsUser(sys.Token, argv0p, argvp, nil, nil, true, flags, createEnvBlock(attr.Env), dirp, si, pi) |
| 327 | } else { |
| 328 | err = CreateProcess(argv0p, argvp, nil, nil, true, flags, createEnvBlock(attr.Env), dirp, si, pi) |
| 329 | } |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 330 | if err != nil { |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 331 | return 0, 0, err |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 332 | } |
Wei Guangjing | 63b8b948 | 2011-07-01 10:18:07 -0400 | [diff] [blame] | 333 | defer CloseHandle(Handle(pi.Thread)) |
Alex Brainman | 913c8d7 | 2011-03-24 11:20:28 +1100 | [diff] [blame] | 334 | |
Wei Guangjing | 16ce2f9 | 2012-02-02 14:08:48 -0500 | [diff] [blame] | 335 | return int(pi.ProcessId), uintptr(pi.Process), nil |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 336 | } |
| 337 | |
Russ Cox | c017a82 | 2011-11-13 22:44:52 -0500 | [diff] [blame] | 338 | func Exec(argv0 string, argv []string, envv []string) (err error) { |
Daniel Theophanes | 7f9e247 | 2010-07-26 09:43:35 +1000 | [diff] [blame] | 339 | return EWINDOWS |
| 340 | } |