exec_bsd.go 6.1 KB

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