2009-02-15 19:35:52 -08:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2011-12-20 03:57:58 +11:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 16:48:57 -04:00
|
|
|
|
2009-02-15 19:35:52 -08:00
|
|
|
// Fork, exec, wait, etc.
|
|
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
|
|
|
|
|
import (
|
2009-12-15 15:40:16 -08:00
|
|
|
"sync"
|
|
|
|
|
"unsafe"
|
2009-02-15 19:35:52 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Lock synchronizing creation of new file descriptors with fork.
|
|
|
|
|
//
|
|
|
|
|
// We want the child in a fork/exec sequence to inherit only the
|
|
|
|
|
// file descriptors we intend. To do that, we mark all file
|
|
|
|
|
// descriptors close-on-exec and then, in the child, explicitly
|
|
|
|
|
// unmark the ones we want the exec'ed program to keep.
|
|
|
|
|
// Unix doesn't make this easy: there is, in general, no way to
|
|
|
|
|
// allocate a new file descriptor close-on-exec. Instead you
|
|
|
|
|
// have to allocate the descriptor and then mark it close-on-exec.
|
|
|
|
|
// If a fork happens between those two events, the child's exec
|
|
|
|
|
// will inherit an unwanted file descriptor.
|
|
|
|
|
//
|
|
|
|
|
// This lock solves that race: the create new fd/mark close-on-exec
|
|
|
|
|
// operation is done holding ForkLock for reading, and the fork itself
|
|
|
|
|
// is done holding ForkLock for writing. At least, that's the idea.
|
|
|
|
|
// There are some complications.
|
|
|
|
|
//
|
|
|
|
|
// Some system calls that create new file descriptors can block
|
|
|
|
|
// for arbitrarily long times: open on a hung NFS server or named
|
|
|
|
|
// pipe, accept on a socket, and so on. We can't reasonably grab
|
|
|
|
|
// the lock across those operations.
|
|
|
|
|
//
|
|
|
|
|
// It is worse to inherit some file descriptors than others.
|
|
|
|
|
// If a non-malicious child accidentally inherits an open ordinary file,
|
|
|
|
|
// that's not a big deal. On the other hand, if a long-lived child
|
|
|
|
|
// accidentally inherits the write end of a pipe, then the reader
|
|
|
|
|
// of that pipe will not see EOF until that child exits, potentially
|
|
|
|
|
// causing the parent program to hang. This is a common problem
|
|
|
|
|
// in threaded C programs that use popen.
|
|
|
|
|
//
|
|
|
|
|
// Luckily, the file descriptors that are most important not to
|
|
|
|
|
// inherit are not the ones that can take an arbitrarily long time
|
|
|
|
|
// to create: pipe returns instantly, and the net package uses
|
|
|
|
|
// non-blocking I/O to accept on a listening socket.
|
|
|
|
|
// The rules for which file descriptor-creating operations use the
|
|
|
|
|
// ForkLock are as follows:
|
|
|
|
|
//
|
|
|
|
|
// 1) Pipe. Does not block. Use the ForkLock.
|
|
|
|
|
// 2) Socket. Does not block. Use the ForkLock.
|
|
|
|
|
// 3) Accept. If using non-blocking mode, use the ForkLock.
|
|
|
|
|
// Otherwise, live with the race.
|
|
|
|
|
// 4) Open. Can block. Use O_CLOEXEC if available (Linux).
|
|
|
|
|
// Otherwise, live with the race.
|
|
|
|
|
// 5) Dup. Does not block. Use the ForkLock.
|
|
|
|
|
// On Linux, could use fcntl F_DUPFD_CLOEXEC
|
|
|
|
|
// instead of the ForkLock, but only for dup(fd, -1).
|
|
|
|
|
|
|
|
|
|
var ForkLock sync.RWMutex
|
|
|
|
|
|
|
|
|
|
// Convert array of string to array
|
|
|
|
|
// of NUL-terminated byte pointer.
|
2011-06-11 09:25:18 +10:00
|
|
|
func StringSlicePtr(ss []string) []*byte {
|
2009-12-15 15:40:16 -08:00
|
|
|
bb := make([]*byte, len(ss)+1)
|
2009-02-15 19:35:52 -08:00
|
|
|
for i := 0; i < len(ss); i++ {
|
2009-11-09 12:07:39 -08:00
|
|
|
bb[i] = StringBytePtr(ss[i])
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
bb[len(ss)] = nil
|
|
|
|
|
return bb
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
|
2009-06-01 22:14:57 -07:00
|
|
|
|
2011-11-13 22:44:52 -05:00
|
|
|
func SetNonblock(fd int, nonblocking bool) (err error) {
|
2009-12-15 15:40:16 -08:00
|
|
|
flag, err := fcntl(fd, F_GETFL, 0)
|
2011-11-13 22:44:52 -05:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
return err
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2009-06-01 22:14:57 -07:00
|
|
|
if nonblocking {
|
2009-11-09 12:07:39 -08:00
|
|
|
flag |= O_NONBLOCK
|
2009-06-01 22:14:57 -07:00
|
|
|
} else {
|
2009-11-09 12:07:39 -08:00
|
|
|
flag &= ^O_NONBLOCK
|
2009-06-01 22:14:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
_, err = fcntl(fd, F_SETFL, flag)
|
|
|
|
|
return err
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
2011-06-27 19:07:49 -04:00
|
|
|
// Credential holds user and group identities to be assumed
|
|
|
|
|
// by a child process started by StartProcess.
|
2011-03-29 14:29:22 -04:00
|
|
|
type Credential struct {
|
|
|
|
|
Uid uint32 // User ID.
|
|
|
|
|
Gid uint32 // Group ID.
|
|
|
|
|
Groups []uint32 // Supplementary group IDs.
|
|
|
|
|
}
|
2011-03-15 14:41:19 -04:00
|
|
|
|
2011-06-27 19:07:49 -04:00
|
|
|
// ProcAttr holds attributes that will be applied to a new process started
|
|
|
|
|
// by StartProcess.
|
2011-03-15 14:41:19 -04:00
|
|
|
type ProcAttr struct {
|
2011-06-14 10:49:34 -04:00
|
|
|
Dir string // Current working directory.
|
|
|
|
|
Env []string // Environment.
|
|
|
|
|
Files []int // File descriptors.
|
|
|
|
|
Sys *SysProcAttr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var zeroProcAttr ProcAttr
|
|
|
|
|
var zeroSysProcAttr SysProcAttr
|
2011-03-15 14:41:19 -04:00
|
|
|
|
2011-11-13 22:44:52 -05:00
|
|
|
func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) {
|
2009-12-15 15:40:16 -08:00
|
|
|
var p [2]int
|
|
|
|
|
var n int
|
2011-11-13 22:44:52 -05:00
|
|
|
var err1 Errno
|
2009-12-15 15:40:16 -08:00
|
|
|
var wstatus WaitStatus
|
2009-02-15 19:35:52 -08:00
|
|
|
|
2011-03-15 14:41:19 -04:00
|
|
|
if attr == nil {
|
2011-06-14 10:49:34 -04:00
|
|
|
attr = &zeroProcAttr
|
|
|
|
|
}
|
|
|
|
|
sys := attr.Sys
|
|
|
|
|
if sys == nil {
|
|
|
|
|
sys = &zeroSysProcAttr
|
2011-03-15 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
p[0] = -1
|
|
|
|
|
p[1] = -1
|
2009-02-15 19:35:52 -08:00
|
|
|
|
|
|
|
|
// Convert args to C form.
|
2009-12-15 15:40:16 -08:00
|
|
|
argv0p := StringBytePtr(argv0)
|
2011-06-11 09:25:18 +10:00
|
|
|
argvp := StringSlicePtr(argv)
|
|
|
|
|
envvp := StringSlicePtr(attr.Env)
|
2009-02-15 19:35:52 -08:00
|
|
|
|
2011-03-06 14:57:05 -05:00
|
|
|
if OS == "freebsd" && len(argv[0]) > len(argv0) {
|
|
|
|
|
argvp[0] = argv0p
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-29 14:29:22 -04:00
|
|
|
var chroot *byte
|
2011-06-14 10:49:34 -04:00
|
|
|
if sys.Chroot != "" {
|
|
|
|
|
chroot = StringBytePtr(sys.Chroot)
|
2011-03-29 14:29:22 -04:00
|
|
|
}
|
2011-03-15 14:41:19 -04:00
|
|
|
var dir *byte
|
|
|
|
|
if attr.Dir != "" {
|
|
|
|
|
dir = StringBytePtr(attr.Dir)
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 19:35:52 -08:00
|
|
|
// Acquire the fork lock so that no other threads
|
|
|
|
|
// create new fds that are not yet close-on-exec
|
|
|
|
|
// before we fork.
|
2009-12-15 15:40:16 -08:00
|
|
|
ForkLock.Lock()
|
2009-02-15 19:35:52 -08:00
|
|
|
|
|
|
|
|
// Allocate child status pipe close on exec.
|
2011-11-13 22:44:52 -05:00
|
|
|
if err = Pipe(p[0:]); err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
goto error
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2011-11-13 22:44:52 -05:00
|
|
|
if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
goto error
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2011-11-13 22:44:52 -05:00
|
|
|
if _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC); err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
goto error
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Kick off child.
|
2011-11-13 22:44:52 -05:00
|
|
|
pid, err1 = forkAndExecInChild(argv0p, argvp, envvp, chroot, dir, attr, sys, p[1])
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
err = Errno(err1)
|
2011-06-17 06:07:13 -04:00
|
|
|
goto error
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
ForkLock.Unlock()
|
2009-02-15 19:35:52 -08:00
|
|
|
|
|
|
|
|
// Read child error status from pipe.
|
2009-12-15 15:40:16 -08:00
|
|
|
Close(p[1])
|
2011-06-17 16:12:14 -04:00
|
|
|
n, err = read(p[0], (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
|
2009-12-15 15:40:16 -08:00
|
|
|
Close(p[0])
|
2011-11-13 22:44:52 -05:00
|
|
|
if err != nil || n != 0 {
|
2011-06-17 16:12:14 -04:00
|
|
|
if n == int(unsafe.Sizeof(err1)) {
|
2011-11-13 22:44:52 -05:00
|
|
|
err = Errno(err1)
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2011-11-13 22:44:52 -05:00
|
|
|
if err == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
err = EPIPE
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Child failed; wait for it to exit, to make sure
|
|
|
|
|
// the zombies don't accumulate.
|
2009-12-15 15:40:16 -08:00
|
|
|
_, err1 := Wait4(pid, &wstatus, 0, nil)
|
2009-02-15 19:35:52 -08:00
|
|
|
for err1 == EINTR {
|
2009-11-09 12:07:39 -08:00
|
|
|
_, err1 = Wait4(pid, &wstatus, 0, nil)
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
return 0, err
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read got EOF, so pipe closed on exec, so exec succeeded.
|
2011-11-13 22:44:52 -05:00
|
|
|
return pid, nil
|
2011-06-17 06:07:13 -04:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if p[0] >= 0 {
|
|
|
|
|
Close(p[0])
|
|
|
|
|
Close(p[1])
|
|
|
|
|
}
|
|
|
|
|
ForkLock.Unlock()
|
|
|
|
|
return 0, err
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|
|
|
|
|
|
2009-07-14 15:09:39 -07:00
|
|
|
// Combination of fork and exec, careful to be thread safe.
|
2011-11-13 22:44:52 -05:00
|
|
|
func ForkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) {
|
2011-03-15 14:41:19 -04:00
|
|
|
return forkExec(argv0, argv, attr)
|
2009-07-14 15:09:39 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-15 14:41:19 -04:00
|
|
|
// StartProcess wraps ForkExec for package os.
|
2011-11-13 22:44:52 -05:00
|
|
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid, handle int, err error) {
|
2011-03-15 14:41:19 -04:00
|
|
|
pid, err = forkExec(argv0, argv, attr)
|
|
|
|
|
return pid, 0, err
|
2009-07-14 15:09:39 -07:00
|
|
|
}
|
|
|
|
|
|
2009-02-15 19:35:52 -08:00
|
|
|
// Ordinary exec.
|
2011-11-13 22:44:52 -05:00
|
|
|
func Exec(argv0 string, argv []string, envv []string) (err error) {
|
2009-09-15 09:41:59 -07:00
|
|
|
_, _, err1 := RawSyscall(SYS_EXECVE,
|
2009-06-01 22:14:57 -07:00
|
|
|
uintptr(unsafe.Pointer(StringBytePtr(argv0))),
|
2011-06-11 09:25:18 +10:00
|
|
|
uintptr(unsafe.Pointer(&StringSlicePtr(argv)[0])),
|
|
|
|
|
uintptr(unsafe.Pointer(&StringSlicePtr(envv)[0])))
|
2011-11-13 22:44:52 -05:00
|
|
|
return Errno(err1)
|
2009-02-15 19:35:52 -08:00
|
|
|
}
|