exec_solaris.go 6.0 KB

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