exec_bsd.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2011 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. // +build darwin dragonfly freebsd netbsd openbsd
  5. package unix
  6. import (
  7. "runtime"
  8. "syscall"
  9. "unsafe"
  10. )
  11. type SysProcAttr struct {
  12. Chroot string // Chroot.
  13. Credential *Credential // Credential.
  14. Ptrace bool // Enable tracing.
  15. Setsid bool // Create session.
  16. Setpgid bool // Set process group ID to new pid (SYSV setpgrp)
  17. Setctty bool // Set controlling terminal to fd 0
  18. Noctty bool // Detach fd 0 from controlling terminal
  19. }
  20. // Implemented in runtime package.
  21. func runtime_BeforeFork()
  22. func runtime_AfterFork()
  23. // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
  24. // If a dup or exec fails, write the errno error to pipe.
  25. // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
  26. // In the child, this function must not acquire any locks, because
  27. // they might have been locked at the time of the fork. This means
  28. // no rescheduling, no malloc calls, and no new stack segments.
  29. // For the same reason compiler does not race instrument it.
  30. // The calls to RawSyscall are okay because they are assembly
  31. // functions that do not grow the stack.
  32. func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err syscall.Errno) {
  33. // Declare all variables at top in case any
  34. // declarations require heap allocation (e.g., err1).
  35. var (
  36. r1, r2 uintptr
  37. err1 syscall.Errno
  38. nextfd int
  39. i int
  40. )
  41. // guard against side effects of shuffling fds below.
  42. // Make sure that nextfd is beyond any currently open files so
  43. // that we can't run the risk of overwriting any of them.
  44. fd := make([]int, len(attr.Files))
  45. nextfd = len(attr.Files)
  46. for i, ufd := range attr.Files {
  47. if nextfd < int(ufd) {
  48. nextfd = int(ufd)
  49. }
  50. fd[i] = int(ufd)
  51. }
  52. nextfd++
  53. darwin := runtime.GOOS == "darwin"
  54. // About to call fork.
  55. // No more allocation or calls of non-assembly functions.
  56. runtime_BeforeFork()
  57. r1, r2, err1 = RawSyscall(SYS_FORK, 0, 0, 0)
  58. if err1 != 0 {
  59. runtime_AfterFork()
  60. return 0, err1
  61. }
  62. // On Darwin:
  63. // r1 = child pid in both parent and child.
  64. // r2 = 0 in parent, 1 in child.
  65. // Convert to normal Unix r1 = 0 in child.
  66. if darwin && r2 == 1 {
  67. r1 = 0
  68. }
  69. if r1 != 0 {
  70. // parent; return PID
  71. runtime_AfterFork()
  72. return int(r1), 0
  73. }
  74. // Fork succeeded, now in child.
  75. // Enable tracing if requested.
  76. if sys.Ptrace {
  77. _, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
  78. if err1 != 0 {
  79. goto childerror
  80. }
  81. }
  82. // Session ID
  83. if sys.Setsid {
  84. _, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
  85. if err1 != 0 {
  86. goto childerror
  87. }
  88. }
  89. // Set process group
  90. if sys.Setpgid {
  91. _, _, err1 = RawSyscall(SYS_SETPGID, 0, 0, 0)
  92. if err1 != 0 {
  93. goto childerror
  94. }
  95. }
  96. // Chroot
  97. if chroot != nil {
  98. _, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
  99. if err1 != 0 {
  100. goto childerror
  101. }
  102. }
  103. // User and groups
  104. if cred := sys.Credential; cred != nil {
  105. ngroups := uintptr(len(cred.Groups))
  106. groups := uintptr(0)
  107. if ngroups > 0 {
  108. groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
  109. }
  110. _, _, err1 = RawSyscall(SYS_SETGROUPS, ngroups, groups, 0)
  111. if err1 != 0 {
  112. goto childerror
  113. }
  114. _, _, err1 = RawSyscall(SYS_SETGID, uintptr(cred.Gid), 0, 0)
  115. if err1 != 0 {
  116. goto childerror
  117. }
  118. _, _, err1 = RawSyscall(SYS_SETUID, uintptr(cred.Uid), 0, 0)
  119. if err1 != 0 {
  120. goto childerror
  121. }
  122. }
  123. // Chdir
  124. if dir != nil {
  125. _, _, err1 = RawSyscall(SYS_CHDIR, uintptr(unsafe.Pointer(dir)), 0, 0)
  126. if err1 != 0 {
  127. goto childerror
  128. }
  129. }
  130. // Pass 1: look for fd[i] < i and move those up above len(fd)
  131. // so that pass 2 won't stomp on an fd it needs later.
  132. if pipe < nextfd {
  133. _, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
  134. if err1 != 0 {
  135. goto childerror
  136. }
  137. RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
  138. pipe = nextfd
  139. nextfd++
  140. }
  141. for i = 0; i < len(fd); i++ {
  142. if fd[i] >= 0 && fd[i] < int(i) {
  143. _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
  144. if err1 != 0 {
  145. goto childerror
  146. }
  147. RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
  148. fd[i] = nextfd
  149. nextfd++
  150. if nextfd == pipe { // don't stomp on pipe
  151. nextfd++
  152. }
  153. }
  154. }
  155. // Pass 2: dup fd[i] down onto i.
  156. for i = 0; i < len(fd); i++ {
  157. if fd[i] == -1 {
  158. RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
  159. continue
  160. }
  161. if fd[i] == int(i) {
  162. // dup2(i, i) won't clear close-on-exec flag on Linux,
  163. // probably not elsewhere either.
  164. _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
  165. if err1 != 0 {
  166. goto childerror
  167. }
  168. continue
  169. }
  170. // The new fd is created NOT close-on-exec,
  171. // which is exactly what we want.
  172. _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(i), 0)
  173. if err1 != 0 {
  174. goto childerror
  175. }
  176. }
  177. // By convention, we don't close-on-exec the fds we are
  178. // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
  179. // Programs that know they inherit fds >= 3 will need
  180. // to set them close-on-exec.
  181. for i = len(fd); i < 3; i++ {
  182. RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
  183. }
  184. // Detach fd 0 from tty
  185. if sys.Noctty {
  186. _, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
  187. if err1 != 0 {
  188. goto childerror
  189. }
  190. }
  191. // Make fd 0 the tty
  192. if sys.Setctty {
  193. _, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCSCTTY), 0)
  194. if err1 != 0 {
  195. goto childerror
  196. }
  197. }
  198. // Time to exec.
  199. _, _, err1 = RawSyscall(SYS_EXECVE,
  200. uintptr(unsafe.Pointer(argv0)),
  201. uintptr(unsafe.Pointer(&argv[0])),
  202. uintptr(unsafe.Pointer(&envv[0])))
  203. childerror:
  204. // send error code on pipe
  205. RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
  206. for {
  207. RawSyscall(SYS_EXIT, 253, 0, 0)
  208. }
  209. }
  210. // Try to open a pipe with O_CLOEXEC set on both file descriptors.
  211. func forkExecPipe(p []int) error {
  212. err := Pipe(p)
  213. if err != nil {
  214. return err
  215. }
  216. _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC)
  217. if err != nil {
  218. return err
  219. }
  220. _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
  221. return err
  222. }