Source file src/syscall/exec_libc2.go

     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  
     5  //go:build darwin || (openbsd && !mips64)
     6  
     7  package syscall
     8  
     9  import (
    10  	"internal/abi"
    11  	"unsafe"
    12  )
    13  
    14  type SysProcAttr struct {
    15  	Chroot     string      // Chroot.
    16  	Credential *Credential // Credential.
    17  	Ptrace     bool        // Enable tracing.
    18  	Setsid     bool        // Create session.
    19  	// Setpgid sets the process group ID of the child to Pgid,
    20  	// or, if Pgid == 0, to the new child's process ID.
    21  	Setpgid bool
    22  	// Setctty sets the controlling terminal of the child to
    23  	// file descriptor Ctty. Ctty must be a descriptor number
    24  	// in the child process: an index into ProcAttr.Files.
    25  	// This is only meaningful if Setsid is true.
    26  	Setctty bool
    27  	Noctty  bool // Detach fd 0 from controlling terminal
    28  	Ctty    int  // Controlling TTY fd
    29  	// Foreground places the child process group in the foreground.
    30  	// This implies Setpgid. The Ctty field must be set to
    31  	// the descriptor of the controlling TTY.
    32  	// Unlike Setctty, in this case Ctty must be a descriptor
    33  	// number in the parent process.
    34  	Foreground bool
    35  	Pgid       int // Child's process group ID if Setpgid.
    36  }
    37  
    38  // Implemented in runtime package.
    39  func runtime_BeforeFork()
    40  func runtime_AfterFork()
    41  func runtime_AfterForkInChild()
    42  
    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     uintptr
    58  		err1   Errno
    59  		nextfd int
    60  		i      int
    61  	)
    62  
    63  	// guard against side effects of shuffling fds below.
    64  	// Make sure that nextfd is beyond any currently open files so
    65  	// that we can't run the risk of overwriting any of them.
    66  	fd := make([]int, len(attr.Files))
    67  	nextfd = len(attr.Files)
    68  	for i, ufd := range attr.Files {
    69  		if nextfd < int(ufd) {
    70  			nextfd = int(ufd)
    71  		}
    72  		fd[i] = int(ufd)
    73  	}
    74  	nextfd++
    75  
    76  	// About to call fork.
    77  	// No more allocation or calls of non-assembly functions.
    78  	runtime_BeforeFork()
    79  	r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
    80  	if err1 != 0 {
    81  		runtime_AfterFork()
    82  		return 0, err1
    83  	}
    84  
    85  	if r1 != 0 {
    86  		// parent; return PID
    87  		runtime_AfterFork()
    88  		return int(r1), 0
    89  	}
    90  
    91  	// Fork succeeded, now in child.
    92  
    93  	// Enable tracing if requested.
    94  	if sys.Ptrace {
    95  		if err := ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
    96  			err1 = err.(Errno)
    97  			goto childerror
    98  		}
    99  	}
   100  
   101  	// Session ID
   102  	if sys.Setsid {
   103  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0)
   104  		if err1 != 0 {
   105  			goto childerror
   106  		}
   107  	}
   108  
   109  	// Set process group
   110  	if sys.Setpgid || sys.Foreground {
   111  		// Place child in process group.
   112  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0)
   113  		if err1 != 0 {
   114  			goto childerror
   115  		}
   116  	}
   117  
   118  	if sys.Foreground {
   119  		// This should really be pid_t, however _C_int (aka int32) is
   120  		// generally equivalent.
   121  		pgrp := _C_int(sys.Pgid)
   122  		if pgrp == 0 {
   123  			r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
   124  			if err1 != 0 {
   125  				goto childerror
   126  			}
   127  			pgrp = _C_int(r1)
   128  		}
   129  
   130  		// Place process group in foreground.
   131  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
   132  		if err1 != 0 {
   133  			goto childerror
   134  		}
   135  	}
   136  
   137  	// Restore the signal mask. We do this after TIOCSPGRP to avoid
   138  	// having the kernel send a SIGTTOU signal to the process group.
   139  	runtime_AfterForkInChild()
   140  
   141  	// Chroot
   142  	if chroot != nil {
   143  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
   144  		if err1 != 0 {
   145  			goto childerror
   146  		}
   147  	}
   148  
   149  	// User and groups
   150  	if cred := sys.Credential; cred != nil {
   151  		ngroups := uintptr(len(cred.Groups))
   152  		groups := uintptr(0)
   153  		if ngroups > 0 {
   154  			groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
   155  		}
   156  		if !cred.NoSetGroups {
   157  			_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgroups_trampoline), ngroups, groups, 0)
   158  			if err1 != 0 {
   159  				goto childerror
   160  			}
   161  		}
   162  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgid_trampoline), uintptr(cred.Gid), 0, 0)
   163  		if err1 != 0 {
   164  			goto childerror
   165  		}
   166  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 0, 0)
   167  		if err1 != 0 {
   168  			goto childerror
   169  		}
   170  	}
   171  
   172  	// Chdir
   173  	if dir != nil {
   174  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0)
   175  		if err1 != 0 {
   176  			goto childerror
   177  		}
   178  	}
   179  
   180  	// Pass 1: look for fd[i] < i and move those up above len(fd)
   181  	// so that pass 2 won't stomp on an fd it needs later.
   182  	if pipe < nextfd {
   183  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(pipe), uintptr(nextfd), 0)
   184  		if err1 != 0 {
   185  			goto childerror
   186  		}
   187  		rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   188  		pipe = nextfd
   189  		nextfd++
   190  	}
   191  	for i = 0; i < len(fd); i++ {
   192  		if fd[i] >= 0 && fd[i] < int(i) {
   193  			if nextfd == pipe { // don't stomp on pipe
   194  				nextfd++
   195  			}
   196  			_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(nextfd), 0)
   197  			if err1 != 0 {
   198  				goto childerror
   199  			}
   200  			rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   201  			fd[i] = nextfd
   202  			nextfd++
   203  		}
   204  	}
   205  
   206  	// Pass 2: dup fd[i] down onto i.
   207  	for i = 0; i < len(fd); i++ {
   208  		if fd[i] == -1 {
   209  			rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
   210  			continue
   211  		}
   212  		if fd[i] == int(i) {
   213  			// dup2(i, i) won't clear close-on-exec flag on Linux,
   214  			// probably not elsewhere either.
   215  			_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(fd[i]), F_SETFD, 0)
   216  			if err1 != 0 {
   217  				goto childerror
   218  			}
   219  			continue
   220  		}
   221  		// The new fd is created NOT close-on-exec,
   222  		// which is exactly what we want.
   223  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(i), 0)
   224  		if err1 != 0 {
   225  			goto childerror
   226  		}
   227  	}
   228  
   229  	// By convention, we don't close-on-exec the fds we are
   230  	// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
   231  	// Programs that know they inherit fds >= 3 will need
   232  	// to set them close-on-exec.
   233  	for i = len(fd); i < 3; i++ {
   234  		rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
   235  	}
   236  
   237  	// Detach fd 0 from tty
   238  	if sys.Noctty {
   239  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
   240  		if err1 != 0 {
   241  			goto childerror
   242  		}
   243  	}
   244  
   245  	// Set the controlling TTY to Ctty
   246  	if sys.Setctty {
   247  		_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
   248  		if err1 != 0 {
   249  			goto childerror
   250  		}
   251  	}
   252  
   253  	// Time to exec.
   254  	_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_execve_trampoline),
   255  		uintptr(unsafe.Pointer(argv0)),
   256  		uintptr(unsafe.Pointer(&argv[0])),
   257  		uintptr(unsafe.Pointer(&envv[0])))
   258  
   259  childerror:
   260  	// send error code on pipe
   261  	rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
   262  	for {
   263  		rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
   264  	}
   265  }
   266  

View as plain text