Skip to content

Commit 2cb6fcf

Browse files
albertscixtor
authored andcommitted
syscall: Linux-only support for parent death signal
As discussed in this thread: https://groups.google.com/group/golang-dev/browse_thread/thread/5b76b7700265a787 I've tried to come up with a solution that is minimally invasive for the platforms that don't support "parent death signal", without splitting up exec_unix.go. See also: http://www.win.tue.nl/~aeb/linux/lk/lk-5.html#ss5.8 R=rsc, dave, borman, iant, mikioh.mikioh CC=golang-dev https://golang.org/cl/5487061
1 parent 468e692 commit 2cb6fcf

19 files changed

+643
-221
lines changed

src/buildscript_darwin_386.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/darwin_386/encoding/json.a
262262

263263
mkdir -p "$WORK"/syscall/_obj/
264264
cd "$GOROOT"/src/pkg/syscall
265-
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_darwin.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_darwin.go ./syscall_darwin_386.go ./syscall_unix.go ./zerrors_darwin_386.go ./zsyscall_darwin_386.go ./zsysnum_darwin_386.go ./ztypes_darwin_386.go
265+
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_darwin.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_darwin.go ./syscall_darwin_386.go ./syscall_unix.go ./zerrors_darwin_386.go ./zsyscall_darwin_386.go ./zsysnum_darwin_386.go ./ztypes_darwin_386.go
266266
8a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_darwin_386.8 -DGOOS_darwin -DGOARCH_386 ./asm_darwin_386.s
267267
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.8 "$WORK"/syscall/_obj/asm_darwin_386.8
268268
cp "$WORK"/syscall.a "$GOROOT"/pkg/darwin_386/syscall.a

src/buildscript_darwin_amd64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/darwin_amd64/encoding/json.a
261261

262262
mkdir -p "$WORK"/syscall/_obj/
263263
cd "$GOROOT"/src/pkg/syscall
264-
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_darwin.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_darwin.go ./syscall_darwin_amd64.go ./syscall_unix.go ./zerrors_darwin_amd64.go ./zsyscall_darwin_amd64.go ./zsysnum_darwin_amd64.go ./ztypes_darwin_amd64.go
264+
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_darwin.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_darwin.go ./syscall_darwin_amd64.go ./syscall_unix.go ./zerrors_darwin_amd64.go ./zsyscall_darwin_amd64.go ./zsysnum_darwin_amd64.go ./ztypes_darwin_amd64.go
265265
6a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_darwin_amd64.6 -DGOOS_darwin -DGOARCH_amd64 ./asm_darwin_amd64.s
266266
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.6 "$WORK"/syscall/_obj/asm_darwin_amd64.6
267267
cp "$WORK"/syscall.a "$GOROOT"/pkg/darwin_amd64/syscall.a

src/buildscript_freebsd_386.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/freebsd_386/encoding/json.a
262262

263263
mkdir -p "$WORK"/syscall/_obj/
264264
cd "$GOROOT"/src/pkg/syscall
265-
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_freebsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_freebsd.go ./syscall_freebsd_386.go ./syscall_unix.go ./zerrors_freebsd_386.go ./zsyscall_freebsd_386.go ./zsysnum_freebsd_386.go ./ztypes_freebsd_386.go
265+
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_freebsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_freebsd.go ./syscall_freebsd_386.go ./syscall_unix.go ./zerrors_freebsd_386.go ./zsyscall_freebsd_386.go ./zsysnum_freebsd_386.go ./ztypes_freebsd_386.go
266266
8a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_freebsd_386.8 -DGOOS_freebsd -DGOARCH_386 ./asm_freebsd_386.s
267267
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.8 "$WORK"/syscall/_obj/asm_freebsd_386.8
268268
cp "$WORK"/syscall.a "$GOROOT"/pkg/freebsd_386/syscall.a

src/buildscript_freebsd_amd64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/freebsd_amd64/encoding/json.a
261261

262262
mkdir -p "$WORK"/syscall/_obj/
263263
cd "$GOROOT"/src/pkg/syscall
264-
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_freebsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_freebsd.go ./syscall_freebsd_amd64.go ./syscall_unix.go ./zerrors_freebsd_amd64.go ./zsyscall_freebsd_amd64.go ./zsysnum_freebsd_amd64.go ./ztypes_freebsd_amd64.go
264+
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_freebsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_freebsd.go ./syscall_freebsd_amd64.go ./syscall_unix.go ./zerrors_freebsd_amd64.go ./zsyscall_freebsd_amd64.go ./zsysnum_freebsd_amd64.go ./ztypes_freebsd_amd64.go
265265
6a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_freebsd_amd64.6 -DGOOS_freebsd -DGOARCH_amd64 ./asm_freebsd_amd64.s
266266
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.6 "$WORK"/syscall/_obj/asm_freebsd_amd64.6
267267
cp "$WORK"/syscall.a "$GOROOT"/pkg/freebsd_amd64/syscall.a

src/buildscript_linux_386.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/linux_386/encoding/json.a
262262

263263
mkdir -p "$WORK"/syscall/_obj/
264264
cd "$GOROOT"/src/pkg/syscall
265-
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./env_unix.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_linux.go ./syscall_linux_386.go ./syscall_unix.go ./zerrors_linux_386.go ./zsyscall_linux_386.go ./zsysnum_linux_386.go ./ztypes_linux_386.go
265+
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./env_unix.go ./exec_linux.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_linux.go ./syscall_linux_386.go ./syscall_unix.go ./zerrors_linux_386.go ./zsyscall_linux_386.go ./zsysnum_linux_386.go ./ztypes_linux_386.go
266266
8a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_linux_386.8 -DGOOS_linux -DGOARCH_386 ./asm_linux_386.s
267267
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.8 "$WORK"/syscall/_obj/asm_linux_386.8
268268
cp "$WORK"/syscall.a "$GOROOT"/pkg/linux_386/syscall.a

src/buildscript_linux_amd64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/linux_amd64/encoding/json.a
261261

262262
mkdir -p "$WORK"/syscall/_obj/
263263
cd "$GOROOT"/src/pkg/syscall
264-
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./env_unix.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_linux.go ./syscall_linux_amd64.go ./syscall_unix.go ./zerrors_linux_amd64.go ./zsyscall_linux_amd64.go ./zsysnum_linux_amd64.go ./ztypes_linux_amd64.go
264+
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./env_unix.go ./exec_linux.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_linux.go ./syscall_linux_amd64.go ./syscall_unix.go ./zerrors_linux_amd64.go ./zsyscall_linux_amd64.go ./zsysnum_linux_amd64.go ./ztypes_linux_amd64.go
265265
6a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_linux_amd64.6 -DGOOS_linux -DGOARCH_amd64 ./asm_linux_amd64.s
266266
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.6 "$WORK"/syscall/_obj/asm_linux_amd64.6
267267
cp "$WORK"/syscall.a "$GOROOT"/pkg/linux_amd64/syscall.a

src/buildscript_linux_arm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/linux_arm/encoding/json.a
265265

266266
mkdir -p "$WORK"/syscall/_obj/
267267
cd "$GOROOT"/src/pkg/syscall
268-
5g -o "$WORK"/syscall/_obj/_go_.5 -p syscall -I "$WORK" ./env_unix.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_arm.go ./syscall_linux.go ./syscall_linux_arm.go ./syscall_unix.go ./zerrors_linux_arm.go ./zsyscall_linux_arm.go ./zsysnum_linux_arm.go ./ztypes_linux_arm.go
268+
5g -o "$WORK"/syscall/_obj/_go_.5 -p syscall -I "$WORK" ./env_unix.go ./exec_linux.go ./exec_unix.go ./lsf_linux.go ./netlink_linux.go ./sockcmsg_linux.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_arm.go ./syscall_linux.go ./syscall_linux_arm.go ./syscall_unix.go ./zerrors_linux_arm.go ./zsyscall_linux_arm.go ./zsysnum_linux_arm.go ./ztypes_linux_arm.go
269269
5a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_linux_arm.5 -DGOOS_linux -DGOARCH_arm ./asm_linux_arm.s
270270
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.5 "$WORK"/syscall/_obj/asm_linux_arm.5
271271
cp "$WORK"/syscall.a "$GOROOT"/pkg/linux_arm/syscall.a

src/buildscript_netbsd_386.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/netbsd_386/encoding/json.a
262262

263263
mkdir -p "$WORK"/syscall/_obj/
264264
cd "$GOROOT"/src/pkg/syscall
265-
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_netbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_netbsd.go ./syscall_netbsd_386.go ./syscall_unix.go ./zerrors_netbsd_386.go ./zsyscall_netbsd_386.go ./zsysnum_netbsd_386.go ./ztypes_netbsd_386.go
265+
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_netbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_netbsd.go ./syscall_netbsd_386.go ./syscall_unix.go ./zerrors_netbsd_386.go ./zsyscall_netbsd_386.go ./zsysnum_netbsd_386.go ./ztypes_netbsd_386.go
266266
8a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_netbsd_386.8 -DGOOS_netbsd -DGOARCH_386 ./asm_netbsd_386.s
267267
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.8 "$WORK"/syscall/_obj/asm_netbsd_386.8
268268
cp "$WORK"/syscall.a "$GOROOT"/pkg/netbsd_386/syscall.a

src/buildscript_netbsd_amd64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/netbsd_amd64/encoding/json.a
261261

262262
mkdir -p "$WORK"/syscall/_obj/
263263
cd "$GOROOT"/src/pkg/syscall
264-
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_netbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_netbsd.go ./syscall_netbsd_amd64.go ./syscall_unix.go ./zerrors_netbsd_amd64.go ./zsyscall_netbsd_amd64.go ./zsysnum_netbsd_amd64.go ./ztypes_netbsd_amd64.go
264+
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_netbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_netbsd.go ./syscall_netbsd_amd64.go ./syscall_unix.go ./zerrors_netbsd_amd64.go ./zsyscall_netbsd_amd64.go ./zsysnum_netbsd_amd64.go ./ztypes_netbsd_amd64.go
265265
6a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_netbsd_amd64.6 -DGOOS_netbsd -DGOARCH_amd64 ./asm_netbsd_amd64.s
266266
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.6 "$WORK"/syscall/_obj/asm_netbsd_amd64.6
267267
cp "$WORK"/syscall.a "$GOROOT"/pkg/netbsd_amd64/syscall.a

src/buildscript_openbsd_386.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/openbsd_386/encoding/json.a
262262

263263
mkdir -p "$WORK"/syscall/_obj/
264264
cd "$GOROOT"/src/pkg/syscall
265-
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_openbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_openbsd.go ./syscall_openbsd_386.go ./syscall_unix.go ./zerrors_openbsd_386.go ./zsyscall_openbsd_386.go ./zsysctl_openbsd.go ./zsysnum_openbsd_386.go ./ztypes_openbsd_386.go
265+
8g -o "$WORK"/syscall/_obj/_go_.8 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_openbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_386.go ./syscall_bsd.go ./syscall_openbsd.go ./syscall_openbsd_386.go ./syscall_unix.go ./zerrors_openbsd_386.go ./zsyscall_openbsd_386.go ./zsysctl_openbsd.go ./zsysnum_openbsd_386.go ./ztypes_openbsd_386.go
266266
8a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_openbsd_386.8 -DGOOS_openbsd -DGOARCH_386 ./asm_openbsd_386.s
267267
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.8 "$WORK"/syscall/_obj/asm_openbsd_386.8
268268
cp "$WORK"/syscall.a "$GOROOT"/pkg/openbsd_386/syscall.a

src/buildscript_openbsd_amd64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ cp "$WORK"/encoding/json.a "$GOROOT"/pkg/openbsd_amd64/encoding/json.a
261261

262262
mkdir -p "$WORK"/syscall/_obj/
263263
cd "$GOROOT"/src/pkg/syscall
264-
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_unix.go ./route_bsd.go ./route_openbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_openbsd.go ./syscall_openbsd_amd64.go ./syscall_unix.go ./zerrors_openbsd_amd64.go ./zsyscall_openbsd_amd64.go ./zsysctl_openbsd.go ./zsysnum_openbsd_amd64.go ./ztypes_openbsd_amd64.go
264+
6g -o "$WORK"/syscall/_obj/_go_.6 -p syscall -I "$WORK" ./bpf_bsd.go ./env_unix.go ./exec_bsd.go ./exec_unix.go ./route_bsd.go ./route_openbsd.go ./sockcmsg_unix.go ./str.go ./syscall.go ./syscall_amd64.go ./syscall_bsd.go ./syscall_openbsd.go ./syscall_openbsd_amd64.go ./syscall_unix.go ./zerrors_openbsd_amd64.go ./zsyscall_openbsd_amd64.go ./zsysctl_openbsd.go ./zsysnum_openbsd_amd64.go ./ztypes_openbsd_amd64.go
265265
6a -I "$WORK"/syscall/_obj/ -o "$WORK"/syscall/_obj/asm_openbsd_amd64.6 -DGOOS_openbsd -DGOARCH_amd64 ./asm_openbsd_amd64.s
266266
gopack grc "$WORK"/syscall.a "$WORK"/syscall/_obj/_go_.6 "$WORK"/syscall/_obj/asm_openbsd_amd64.6
267267
cp "$WORK"/syscall.a "$GOROOT"/pkg/openbsd_amd64/syscall.a

src/pkg/syscall/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GOFILES=\
1919
GOFILES_darwin=\
2020
bpf_bsd.go\
2121
env_unix.go\
22+
exec_bsd.go\
2223
exec_unix.go\
2324
route_bsd.go\
2425
route_darwin.go\
@@ -29,6 +30,7 @@ GOFILES_darwin=\
2930
GOFILES_freebsd=\
3031
bpf_bsd.go\
3132
env_unix.go\
33+
exec_bsd.go\
3234
exec_unix.go\
3335
route_bsd.go\
3436
route_freebsd.go\
@@ -39,6 +41,7 @@ GOFILES_freebsd=\
3941
GOFILES_linux=\
4042
env_unix.go\
4143
exec_unix.go\
44+
exec_linux.go\
4245
lsf_linux.go\
4346
netlink_linux.go\
4447
sockcmsg_linux.go\
@@ -48,6 +51,7 @@ GOFILES_linux=\
4851
GOFILES_netbsd=\
4952
bpf_bsd.go\
5053
env_unix.go\
54+
exec_bsd.go\
5155
exec_unix.go\
5256
route_bsd.go\
5357
route_netbsd.go\
@@ -58,6 +62,7 @@ GOFILES_netbsd=\
5862
GOFILES_openbsd=\
5963
bpf_bsd.go\
6064
env_unix.go\
65+
exec_bsd.go\
6166
exec_unix.go\
6267
route_bsd.go\
6368
route_openbsd.go\

src/pkg/syscall/exec_bsd.go

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
// +build darwin freebsd netbsd openbsd
6+
7+
package syscall
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
type SysProcAttr struct {
14+
Chroot string // Chroot.
15+
Credential *Credential // Credential.
16+
Ptrace bool // Enable tracing.
17+
Setsid bool // Create session.
18+
Setpgid bool // Set process group ID to new pid (SYSV setpgrp)
19+
Setctty bool // Set controlling terminal to fd 0
20+
Noctty bool // Detach fd 0 from controlling terminal
21+
}
22+
23+
// Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
24+
// If a dup or exec fails, write the errno error to pipe.
25+
// (Pipe is close-on-exec so if exec succeeds, it will be closed.)
26+
// In the child, this function must not acquire any locks, because
27+
// they might have been locked at the time of the fork. This means
28+
// no rescheduling, no malloc calls, and no new stack segments.
29+
// The calls to RawSyscall are okay because they are assembly
30+
// functions that do not grow the stack.
31+
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
32+
// Declare all variables at top in case any
33+
// declarations require heap allocation (e.g., err1).
34+
var (
35+
r1, r2 uintptr
36+
err1 Errno
37+
nextfd int
38+
i int
39+
)
40+
41+
// guard against side effects of shuffling fds below.
42+
fd := append([]int(nil), attr.Files...)
43+
44+
darwin := OS == "darwin"
45+
46+
// About to call fork.
47+
// No more allocation or calls of non-assembly functions.
48+
r1, r2, err1 = RawSyscall(SYS_FORK, 0, 0, 0)
49+
if err1 != 0 {
50+
return 0, err1
51+
}
52+
53+
// On Darwin:
54+
// r1 = child pid in both parent and child.
55+
// r2 = 0 in parent, 1 in child.
56+
// Convert to normal Unix r1 = 0 in child.
57+
if darwin && r2 == 1 {
58+
r1 = 0
59+
}
60+
61+
if r1 != 0 {
62+
// parent; return PID
63+
return int(r1), 0
64+
}
65+
66+
// Fork succeeded, now in child.
67+
68+
// Enable tracing if requested.
69+
if sys.Ptrace {
70+
_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
71+
if err1 != 0 {
72+
goto childerror
73+
}
74+
}
75+
76+
// Session ID
77+
if sys.Setsid {
78+
_, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
79+
if err1 != 0 {
80+
goto childerror
81+
}
82+
}
83+
84+
// Set process group
85+
if sys.Setpgid {
86+
_, _, err1 = RawSyscall(SYS_SETPGID, 0, 0, 0)
87+
if err1 != 0 {
88+
goto childerror
89+
}
90+
}
91+
92+
// Chroot
93+
if chroot != nil {
94+
_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
95+
if err1 != 0 {
96+
goto childerror
97+
}
98+
}
99+
100+
// User and groups
101+
if cred := sys.Credential; cred != nil {
102+
ngroups := uintptr(len(cred.Groups))
103+
groups := uintptr(0)
104+
if ngroups > 0 {
105+
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
106+
}
107+
_, _, err1 = RawSyscall(SYS_SETGROUPS, ngroups, groups, 0)
108+
if err1 != 0 {
109+
goto childerror
110+
}
111+
_, _, err1 = RawSyscall(SYS_SETGID, uintptr(cred.Gid), 0, 0)
112+
if err1 != 0 {
113+
goto childerror
114+
}
115+
_, _, err1 = RawSyscall(SYS_SETUID, uintptr(cred.Uid), 0, 0)
116+
if err1 != 0 {
117+
goto childerror
118+
}
119+
}
120+
121+
// Chdir
122+
if dir != nil {
123+
_, _, err1 = RawSyscall(SYS_CHDIR, uintptr(unsafe.Pointer(dir)), 0, 0)
124+
if err1 != 0 {
125+
goto childerror
126+
}
127+
}
128+
129+
// Pass 1: look for fd[i] < i and move those up above len(fd)
130+
// so that pass 2 won't stomp on an fd it needs later.
131+
nextfd = int(len(fd))
132+
if pipe < nextfd {
133+
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
134+
if err1 != 0 {
135+
goto childerror
136+
}
137+
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
138+
pipe = nextfd
139+
nextfd++
140+
}
141+
for i = 0; i < len(fd); i++ {
142+
if fd[i] >= 0 && fd[i] < int(i) {
143+
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
144+
if err1 != 0 {
145+
goto childerror
146+
}
147+
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
148+
fd[i] = nextfd
149+
nextfd++
150+
if nextfd == pipe { // don't stomp on pipe
151+
nextfd++
152+
}
153+
}
154+
}
155+
156+
// Pass 2: dup fd[i] down onto i.
157+
for i = 0; i < len(fd); i++ {
158+
if fd[i] == -1 {
159+
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
160+
continue
161+
}
162+
if fd[i] == int(i) {
163+
// dup2(i, i) won't clear close-on-exec flag on Linux,
164+
// probably not elsewhere either.
165+
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
166+
if err1 != 0 {
167+
goto childerror
168+
}
169+
continue
170+
}
171+
// The new fd is created NOT close-on-exec,
172+
// which is exactly what we want.
173+
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(i), 0)
174+
if err1 != 0 {
175+
goto childerror
176+
}
177+
}
178+
179+
// By convention, we don't close-on-exec the fds we are
180+
// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
181+
// Programs that know they inherit fds >= 3 will need
182+
// to set them close-on-exec.
183+
for i = len(fd); i < 3; i++ {
184+
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
185+
}
186+
187+
// Detach fd 0 from tty
188+
if sys.Noctty {
189+
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
190+
if err1 != 0 {
191+
goto childerror
192+
}
193+
}
194+
195+
// Make fd 0 the tty
196+
if sys.Setctty {
197+
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCSCTTY), 0)
198+
if err1 != 0 {
199+
goto childerror
200+
}
201+
}
202+
203+
// Time to exec.
204+
_, _, err1 = RawSyscall(SYS_EXECVE,
205+
uintptr(unsafe.Pointer(argv0)),
206+
uintptr(unsafe.Pointer(&argv[0])),
207+
uintptr(unsafe.Pointer(&envv[0])))
208+
209+
childerror:
210+
// send error code on pipe
211+
RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
212+
for {
213+
RawSyscall(SYS_EXIT, 253, 0, 0)
214+
}
215+
216+
// Calling panic is not actually safe,
217+
// but the for loop above won't break
218+
// and this shuts up the compiler.
219+
panic("unreached")
220+
}

0 commit comments

Comments
 (0)