exec_linux.go 6.8 KB

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