exec_freebsd.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2021 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 syscall
  5. import (
  6. "runtime"
  7. "unsafe"
  8. )
  9. //sysnb raw_procctl(idtype int, id int64, cmd int, data unsafe.Pointer) (ret int, err Errno)
  10. //procctl(idtype _C_int, id int64, cmd int, data unsafe.Pointer) _C_int
  11. type SysProcAttr struct {
  12. Chroot string // Chroot.
  13. Credential *Credential // Credential.
  14. Ptrace bool // Enable tracing.
  15. Setsid bool // Create session.
  16. // Setpgid sets the process group ID of the child to Pgid,
  17. // or, if Pgid == 0, to the new child's process ID.
  18. Setpgid bool
  19. // Setctty sets the controlling terminal of the child to
  20. // file descriptor Ctty. Ctty must be a descriptor number
  21. // in the child process: an index into ProcAttr.Files.
  22. // This is only meaningful if Setsid is true.
  23. Setctty bool
  24. Noctty bool // Detach fd 0 from controlling terminal
  25. Ctty int // Controlling TTY fd
  26. // Foreground places the child process group in the foreground.
  27. // This implies Setpgid. The Ctty field must be set to
  28. // the descriptor of the controlling TTY.
  29. // Unlike Setctty, in this case Ctty must be a descriptor
  30. // number in the parent process.
  31. Foreground bool
  32. Pgid int // Child's process group ID if Setpgid.
  33. Pdeathsig Signal // Signal that the process will get when its parent dies (Linux and FreeBSD only)
  34. }
  35. const (
  36. _P_PID = 0
  37. _PROC_PDEATHSIG_CTL = 11
  38. )
  39. // Implemented in runtime package.
  40. func runtime_BeforeFork()
  41. func runtime_AfterFork()
  42. func runtime_AfterForkInChild()
  43. // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
  44. // If a dup or exec fails, write the errno error to pipe.
  45. // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
  46. // In the child, this function must not acquire any locks, because
  47. // they might have been locked at the time of the fork. This means
  48. // no rescheduling, no malloc calls, and no new stack segments.
  49. // For the same reason compiler does not race instrument it.
  50. // The calls to RawSyscall are okay because they are assembly
  51. // functions that do not grow the stack.
  52. //go:norace
  53. func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
  54. // Declare all variables at top in case any
  55. // declarations require heap allocation (e.g., err1).
  56. var (
  57. r1 Pid_t
  58. err1 Errno
  59. nextfd int
  60. i int
  61. )
  62. // Record parent PID so child can test if it has died.
  63. ppid := raw_getpid()
  64. // guard against side effects of shuffling fds below.
  65. // Make sure that nextfd is beyond any currently open files so
  66. // that we can't run the risk of overwriting any of them.
  67. fd := make([]int, len(attr.Files))
  68. nextfd = len(attr.Files)
  69. for i, ufd := range attr.Files {
  70. if nextfd < int(ufd) {
  71. nextfd = int(ufd)
  72. }
  73. fd[i] = int(ufd)
  74. }
  75. nextfd++
  76. // About to call fork.
  77. // No more allocation or calls of non-assembly functions.
  78. runtime_BeforeFork()
  79. r1, err1 = raw_fork()
  80. if err1 != 0 {
  81. runtime_AfterFork()
  82. return 0, err1
  83. }
  84. if r1 != 0 {
  85. // parent; return PID
  86. runtime_AfterFork()
  87. return int(r1), 0
  88. }
  89. // Fork succeeded, now in child.
  90. // Enable tracing if requested.
  91. if sys.Ptrace {
  92. err1 = raw_ptrace(_PTRACE_TRACEME, 0, 0, 0)
  93. if err1 != 0 {
  94. goto childerror
  95. }
  96. }
  97. // Session ID
  98. if sys.Setsid {
  99. err1 = raw_setsid()
  100. if err1 != 0 {
  101. goto childerror
  102. }
  103. }
  104. // Set process group
  105. if sys.Setpgid || sys.Foreground {
  106. // Place child in process group.
  107. err1 = raw_setpgid(0, sys.Pgid)
  108. if err1 != 0 {
  109. goto childerror
  110. }
  111. }
  112. if sys.Foreground {
  113. pgrp := Pid_t(sys.Pgid)
  114. if pgrp == 0 {
  115. pgrp = raw_getpid()
  116. }
  117. // Place process group in foreground.
  118. err1 = raw_ioctl(sys.Ctty, TIOCSPGRP, unsafe.Pointner(&pgrp))
  119. if err1 != 0 {
  120. goto childerror
  121. }
  122. }
  123. // Restore the signal mask. We do this after TIOCSPGRP to avoid
  124. // having the kernel send a SIGTTOU signal to the process group.
  125. runtime_AfterForkInChild()
  126. // Chroot
  127. if chroot != nil {
  128. err1 = raw_chroot(chroot)
  129. if err1 != 0 {
  130. goto childerror
  131. }
  132. }
  133. // User and groups
  134. if cred := sys.Credential; cred != nil {
  135. ngroups := len(cred.Groups)
  136. var groups *Gid_t
  137. if ngroups > 0 {
  138. gids := make([]Gid_t, ngroups)
  139. for i, v := range cred.Groups {
  140. gids[i] = Gid_t(v)
  141. }
  142. groups = &gids[0]
  143. }
  144. if !cred.NoSetGroups {
  145. err1 = raw_setgroups(ngroups, groups)
  146. if err1 != 0 {
  147. goto childerror
  148. }
  149. }
  150. err2 := Setgid(int(cred.Gid))
  151. if err2 != nil {
  152. err1 = err2.(Errno)
  153. goto childerror
  154. }
  155. err2 = Setuid(int(cred.Uid))
  156. if err2 != nil {
  157. err1 = err2.(Errno)
  158. goto childerror
  159. }
  160. }
  161. // Chdir
  162. if dir != nil {
  163. err1 = raw_chdir(dir)
  164. if err1 != 0 {
  165. goto childerror
  166. }
  167. }
  168. // Parent death signal
  169. if sys.Pdeathsig != 0 {
  170. _, err1 = raw_procctl(_P_PID, 0, _PROC_PDEATHSIG_CTL, unsafe.Pointer(&sys.Pdeathsig))
  171. if err1 != 0 {
  172. goto childerror
  173. }
  174. // Signal self if parent is already dead. This might cause a
  175. // duplicate signal in rare cases, but it won't matter when
  176. // using SIGKILL.
  177. r1 = raw_getppid()
  178. if r1 != ppid {
  179. pid := raw_getpid()
  180. err1 = raw_kill(pid, sys.Pdeathsig)
  181. if err1 != 0 {
  182. goto childerror
  183. }
  184. }
  185. }
  186. // Pass 1: look for fd[i] < i and move those up above len(fd)
  187. // so that pass 2 won't stomp on an fd it needs later.
  188. if pipe < nextfd {
  189. _, err1 = raw_fcntl(pipe, _F_DUP2FD_CLOEXEC, nextfd)
  190. if err1 != 0 {
  191. goto childerror
  192. }
  193. pipe = nextfd
  194. nextfd++
  195. }
  196. for i = 0; i < len(fd); i++ {
  197. if fd[i] >= 0 && fd[i] < int(i) {
  198. if nextfd == pipe { // don't stomp on pipe
  199. nextfd++
  200. }
  201. err1 = raw_fcntl(fd[i], _F_DUP2FD_CLOEXEC, nextfd)
  202. if err1 != 0 {
  203. goto childerror
  204. }
  205. fd[i] = nextfd
  206. nextfd++
  207. }
  208. }
  209. // Pass 2: dup fd[i] down onto i.
  210. for i = 0; i < len(fd); i++ {
  211. if fd[i] == -1 {
  212. raw_close(i)
  213. continue
  214. }
  215. if fd[i] == int(i) {
  216. // dup2(i, i) won't clear close-on-exec flag on Linux,
  217. // probably not elsewhere either.
  218. _, err1 = raw_fcntl(fd[i], _F_SETFD, 0)
  219. if err1 != 0 {
  220. goto childerror
  221. }
  222. continue
  223. }
  224. // The new fd is created NOT close-on-exec,
  225. // which is exactly what we want.
  226. err1 = raw_dup2(fd[i], i)
  227. if err1 != 0 {
  228. goto childerror
  229. }
  230. }
  231. // By convention, we don't close-on-exec the fds we are
  232. // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
  233. // Programs that know they inherit fds >= 3 will need
  234. // to set them close-on-exec.
  235. for i = len(fd); i < 3; i++ {
  236. raw_close(i)
  237. }
  238. // Detach fd 0 from tty
  239. if sys.Noctty {
  240. _, err = raw_ioctl(0, TIOCNOTTY, 0)
  241. if err1 != 0 {
  242. goto childerror
  243. }
  244. }
  245. // Set the controlling TTY to Ctty
  246. if sys.Setctty {
  247. _, err1 = raw_ioctl(sys.Cty, TIOCSCTTY, 0)
  248. if err1 != 0 {
  249. goto childerror
  250. }
  251. }
  252. // Time to exec.
  253. err1 = raw_execve(argv0, &argv[0], &envv[0])
  254. childerror:
  255. // send error code on pipe
  256. raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
  257. for {
  258. raw_exit(253)
  259. }
  260. }