syscall.Wait4
This commit is contained in:
45
c/os/os.go
45
c/os/os.go
@@ -263,21 +263,50 @@ func Execve(path *c.Char, argv **c.Char, envp **c.Char) c.Int
|
|||||||
//go:linkname Execvp C.execvp
|
//go:linkname Execvp C.execvp
|
||||||
func Execvp(file *c.Char, argv **c.Char) c.Int
|
func Execvp(file *c.Char, argv **c.Char) c.Int
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type PidT c.Int
|
||||||
|
|
||||||
//go:linkname Fork C.fork
|
//go:linkname Fork C.fork
|
||||||
func Fork() c.Int
|
func Fork() PidT
|
||||||
|
|
||||||
|
//go:linkname Getpid C.getpid
|
||||||
|
func Getpid() PidT
|
||||||
|
|
||||||
|
//go:linkname Getppid C.getppid
|
||||||
|
func Getppid() PidT
|
||||||
|
|
||||||
//go:linkname Kill C.kill
|
//go:linkname Kill C.kill
|
||||||
func Kill(pid c.Int, sig c.Int) c.Int
|
func Kill(pid PidT, sig c.Int) c.Int
|
||||||
|
|
||||||
|
// If wait() returns due to a stopped or terminated child process, the process ID
|
||||||
|
// of the child is returned to the calling process. Otherwise, a value of -1 is
|
||||||
|
// returned and errno is set to indicate the error.
|
||||||
|
//
|
||||||
|
//go:linkname Wait C.wait
|
||||||
|
func Wait(statLoc *c.Int) PidT
|
||||||
|
|
||||||
|
// If wait3(), wait4(), or waitpid() returns due to a stopped or terminated child
|
||||||
|
// process, the process ID of the child is returned to the calling process. If
|
||||||
|
// there are no children not previously awaited, -1 is returned with errno set to
|
||||||
|
// [ECHILD]. Otherwise, if WNOHANG is specified and there are no stopped or exited
|
||||||
|
// children, 0 is returned. If an error is detected or a caught signal aborts the
|
||||||
|
// call, a value of -1 is returned and errno is set to indicate the error.
|
||||||
|
//
|
||||||
|
//go:linkname Wait3 C.wait3
|
||||||
|
func Wait3(statLoc *c.Int, options c.Int, rusage *syscall.Rusage) PidT
|
||||||
|
|
||||||
|
//go:linkname Wait4 C.wait4
|
||||||
|
func Wait4(pid PidT, statLoc *c.Int, options c.Int, rusage *syscall.Rusage) PidT
|
||||||
|
|
||||||
|
//go:linkname Waitpid C.waitpid
|
||||||
|
func Waitpid(pid PidT, statLoc *c.Int, options c.Int) PidT
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
//go:linkname Exit C.exit
|
//go:linkname Exit C.exit
|
||||||
func Exit(c.Int)
|
func Exit(c.Int)
|
||||||
|
|
||||||
//go:linkname Getpid C.getpid
|
|
||||||
func Getpid() c.Int
|
|
||||||
|
|
||||||
//go:linkname Getppid C.getppid
|
|
||||||
func Getppid() c.Int
|
|
||||||
|
|
||||||
//go:linkname Getuid C.getuid
|
//go:linkname Getuid C.getuid
|
||||||
func Getuid() UidT
|
func Getuid() UidT
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,13 @@
|
|||||||
|
|
||||||
package syscall
|
package syscall
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
)
|
||||||
|
|
||||||
type SysProcAttr struct {
|
type SysProcAttr struct {
|
||||||
Chroot string // Chroot.
|
Chroot string // Chroot.
|
||||||
Credential *Credential // Credential.
|
Credential *Credential // Credential.
|
||||||
@@ -48,146 +55,165 @@ func runtime_AfterForkInChild()
|
|||||||
// functions that do not grow the stack.
|
// functions that do not grow the stack.
|
||||||
//
|
//
|
||||||
//go:norace
|
//go:norace
|
||||||
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) {
|
func forkAndExecInChild(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) {
|
||||||
/* TODO(xsw):
|
// Declare all variables at top in case any
|
||||||
// Declare all variables at top in case any
|
// declarations require heap allocation (e.g., err1).
|
||||||
// declarations require heap allocation (e.g., err1).
|
var (
|
||||||
var (
|
r1 uintptr
|
||||||
r1 uintptr
|
nextfd int
|
||||||
nextfd int
|
i int
|
||||||
i int
|
err error
|
||||||
err error
|
pgrp c.Int
|
||||||
pgrp _C_int
|
cred *Credential
|
||||||
cred *Credential
|
ngroups, groups uintptr
|
||||||
ngroups, groups uintptr
|
)
|
||||||
)
|
|
||||||
|
|
||||||
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
|
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
|
||||||
|
|
||||||
// guard against side effects of shuffling fds below.
|
// guard against side effects of shuffling fds below.
|
||||||
// Make sure that nextfd is beyond any currently open files so
|
// Make sure that nextfd is beyond any currently open files so
|
||||||
// that we can't run the risk of overwriting any of them.
|
// that we can't run the risk of overwriting any of them.
|
||||||
fd := make([]int, len(attr.Files))
|
fd := make([]int, len(attr.Files))
|
||||||
nextfd = len(attr.Files)
|
nextfd = len(attr.Files)
|
||||||
for i, ufd := range attr.Files {
|
for i, ufd := range attr.Files {
|
||||||
if nextfd < int(ufd) {
|
if nextfd < int(ufd) {
|
||||||
nextfd = int(ufd)
|
nextfd = int(ufd)
|
||||||
}
|
|
||||||
fd[i] = int(ufd)
|
|
||||||
}
|
}
|
||||||
nextfd++
|
fd[i] = int(ufd)
|
||||||
|
}
|
||||||
|
nextfd++
|
||||||
|
|
||||||
// About to call fork.
|
// About to call fork.
|
||||||
// No more allocation or calls of non-assembly functions.
|
// No more allocation or calls of non-assembly functions.
|
||||||
runtime_BeforeFork()
|
runtime_BeforeFork()
|
||||||
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
|
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
|
||||||
|
if err1 != 0 {
|
||||||
|
runtime_AfterFork()
|
||||||
|
return 0, err1
|
||||||
|
}
|
||||||
|
|
||||||
|
if r1 != 0 {
|
||||||
|
// parent; return PID
|
||||||
|
runtime_AfterFork()
|
||||||
|
return int(r1), 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fork succeeded, now in child.
|
||||||
|
|
||||||
|
// Enable tracing if requested.
|
||||||
|
if sys.Ptrace {
|
||||||
|
if err = ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
|
||||||
|
err1 = err.(Errno)
|
||||||
|
goto childerror
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Session ID
|
||||||
|
if sys.Setsid {
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0)
|
||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
runtime_AfterFork()
|
goto childerror
|
||||||
return 0, err1
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if r1 != 0 {
|
// Set process group
|
||||||
// parent; return PID
|
if sys.Setpgid || sys.Foreground {
|
||||||
runtime_AfterFork()
|
// Place child in process group.
|
||||||
return int(r1), 0
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0)
|
||||||
|
if err1 != 0 {
|
||||||
|
goto childerror
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Fork succeeded, now in child.
|
if sys.Foreground {
|
||||||
|
// This should really be pid_t, however _C_int (aka int32) is
|
||||||
// Enable tracing if requested.
|
// generally equivalent.
|
||||||
if sys.Ptrace {
|
pgrp = _C_int(sys.Pgid)
|
||||||
if err = ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
|
if pgrp == 0 {
|
||||||
err1 = err.(Errno)
|
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
|
||||||
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
|
pgrp = _C_int(r1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Session ID
|
// Place process group in foreground.
|
||||||
if sys.Setsid {
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0)
|
if err1 != 0 {
|
||||||
|
goto childerror
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
||||||
|
// having the kernel send a SIGTTOU signal to the process group.
|
||||||
|
runtime_AfterForkInChild()
|
||||||
|
|
||||||
|
// Chroot
|
||||||
|
if chroot != nil {
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
|
||||||
|
if err1 != 0 {
|
||||||
|
goto childerror
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// User and groups
|
||||||
|
if cred = sys.Credential; cred != nil {
|
||||||
|
ngroups = uintptr(len(cred.Groups))
|
||||||
|
groups = uintptr(0)
|
||||||
|
if ngroups > 0 {
|
||||||
|
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
|
||||||
|
}
|
||||||
|
if !cred.NoSetGroups {
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgroups_trampoline), ngroups, groups, 0)
|
||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgid_trampoline), uintptr(cred.Gid), 0, 0)
|
||||||
// Set process group
|
if err1 != 0 {
|
||||||
if sys.Setpgid || sys.Foreground {
|
goto childerror
|
||||||
// Place child in process group.
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 0, 0)
|
||||||
if sys.Foreground {
|
if err1 != 0 {
|
||||||
// This should really be pid_t, however _C_int (aka int32) is
|
goto childerror
|
||||||
// generally equivalent.
|
|
||||||
pgrp = _C_int(sys.Pgid)
|
|
||||||
if pgrp == 0 {
|
|
||||||
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
pgrp = _C_int(r1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Place process group in foreground.
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
// Chdir
|
||||||
// having the kernel send a SIGTTOU signal to the process group.
|
if dir != nil {
|
||||||
runtime_AfterForkInChild()
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0)
|
||||||
|
if err1 != 0 {
|
||||||
// Chroot
|
goto childerror
|
||||||
if chroot != nil {
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// User and groups
|
// Pass 1: look for fd[i] < i and move those up above len(fd)
|
||||||
if cred = sys.Credential; cred != nil {
|
// so that pass 2 won't stomp on an fd it needs later.
|
||||||
ngroups = uintptr(len(cred.Groups))
|
if pipe < nextfd {
|
||||||
groups = uintptr(0)
|
if runtime.GOOS == "openbsd" {
|
||||||
if ngroups > 0 {
|
_, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
|
||||||
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
|
} else {
|
||||||
}
|
_, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0)
|
||||||
if !cred.NoSetGroups {
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgroups_trampoline), ngroups, groups, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgid_trampoline), uintptr(cred.Gid), 0, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 0, 0)
|
|
||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
|
||||||
}
|
}
|
||||||
|
if err1 != 0 {
|
||||||
// Chdir
|
goto childerror
|
||||||
if dir != nil {
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
pipe = nextfd
|
||||||
// Pass 1: look for fd[i] < i and move those up above len(fd)
|
nextfd++
|
||||||
// so that pass 2 won't stomp on an fd it needs later.
|
}
|
||||||
if pipe < nextfd {
|
for i = 0; i < len(fd); i++ {
|
||||||
|
if fd[i] >= 0 && fd[i] < i {
|
||||||
|
if nextfd == pipe { // don't stomp on pipe
|
||||||
|
nextfd++
|
||||||
|
}
|
||||||
if runtime.GOOS == "openbsd" {
|
if runtime.GOOS == "openbsd" {
|
||||||
_, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
|
_, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
|
||||||
} else {
|
} else {
|
||||||
_, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0)
|
_, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), 0)
|
||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
@@ -196,95 +222,73 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
|||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
pipe = nextfd
|
fd[i] = nextfd
|
||||||
nextfd++
|
nextfd++
|
||||||
}
|
}
|
||||||
for i = 0; i < len(fd); i++ {
|
}
|
||||||
if fd[i] >= 0 && fd[i] < i {
|
|
||||||
if nextfd == pipe { // don't stomp on pipe
|
|
||||||
nextfd++
|
|
||||||
}
|
|
||||||
if runtime.GOOS == "openbsd" {
|
|
||||||
_, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
|
|
||||||
} else {
|
|
||||||
_, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
|
|
||||||
}
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
fd[i] = nextfd
|
|
||||||
nextfd++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass 2: dup fd[i] down onto i.
|
// Pass 2: dup fd[i] down onto i.
|
||||||
for i = 0; i < len(fd); i++ {
|
for i = 0; i < len(fd); i++ {
|
||||||
if fd[i] == -1 {
|
if fd[i] == -1 {
|
||||||
rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fd[i] == i {
|
|
||||||
// dup2(i, i) won't clear close-on-exec flag on Linux,
|
|
||||||
// probably not elsewhere either.
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(fd[i]), F_SETFD, 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// The new fd is created NOT close-on-exec,
|
|
||||||
// which is exactly what we want.
|
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(i), 0)
|
|
||||||
if err1 != 0 {
|
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// By convention, we don't close-on-exec the fds we are
|
|
||||||
// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
|
|
||||||
// Programs that know they inherit fds >= 3 will need
|
|
||||||
// to set them close-on-exec.
|
|
||||||
for i = len(fd); i < 3; i++ {
|
|
||||||
rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
|
rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if fd[i] == i {
|
||||||
// Detach fd 0 from tty
|
// dup2(i, i) won't clear close-on-exec flag on Linux,
|
||||||
if sys.Noctty {
|
// probably not elsewhere either.
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(fd[i]), F_SETFD, 0)
|
||||||
if err1 != 0 {
|
if err1 != 0 {
|
||||||
goto childerror
|
goto childerror
|
||||||
}
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
// The new fd is created NOT close-on-exec,
|
||||||
// Set the controlling TTY to Ctty
|
// which is exactly what we want.
|
||||||
if sys.Setctty {
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(i), 0)
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
|
if err1 != 0 {
|
||||||
if err1 != 0 {
|
goto childerror
|
||||||
goto childerror
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Restore original rlimit.
|
// By convention, we don't close-on-exec the fds we are
|
||||||
if rlimOK && rlim.Cur != 0 {
|
// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
|
||||||
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
|
// Programs that know they inherit fds >= 3 will need
|
||||||
|
// to set them close-on-exec.
|
||||||
|
for i = len(fd); i < 3; i++ {
|
||||||
|
rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detach fd 0 from tty
|
||||||
|
if sys.Noctty {
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
|
||||||
|
if err1 != 0 {
|
||||||
|
goto childerror
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Time to exec.
|
// Set the controlling TTY to Ctty
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_execve_trampoline),
|
if sys.Setctty {
|
||||||
uintptr(unsafe.Pointer(argv0)),
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
|
||||||
uintptr(unsafe.Pointer(&argv[0])),
|
if err1 != 0 {
|
||||||
uintptr(unsafe.Pointer(&envv[0])))
|
goto childerror
|
||||||
|
|
||||||
childerror:
|
|
||||||
// send error code on pipe
|
|
||||||
rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
|
|
||||||
for {
|
|
||||||
rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
|
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
panic("todo: syscall.forkAndExecInChild")
|
|
||||||
|
// Restore original rlimit.
|
||||||
|
if rlimOK && rlim.Cur != 0 {
|
||||||
|
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time to exec.
|
||||||
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_execve_trampoline),
|
||||||
|
uintptr(unsafe.Pointer(argv0)),
|
||||||
|
uintptr(unsafe.Pointer(&argv[0])),
|
||||||
|
uintptr(unsafe.Pointer(&envv[0])))
|
||||||
|
|
||||||
|
childerror:
|
||||||
|
// send error code on pipe
|
||||||
|
rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
|
||||||
|
for {
|
||||||
|
rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/os"
|
"github.com/goplus/llgo/c/os"
|
||||||
|
"github.com/goplus/llgo/c/syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ForkLock is used to synchronize creation of new file descriptors
|
// ForkLock is used to synchronize creation of new file descriptors
|
||||||
@@ -175,7 +175,7 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error)
|
|||||||
Close(p[1])
|
Close(p[1])
|
||||||
for {
|
for {
|
||||||
n, err = readlen(p[0], (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
|
n, err = readlen(p[0], (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
|
||||||
if err != EINTR {
|
if err != Errno(syscall.EINTR) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,13 +185,13 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error)
|
|||||||
err = Errno(err1)
|
err = Errno(err1)
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = EPIPE
|
err = Errno(syscall.EPIPE)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Child failed; wait for it to exit, to make sure
|
// Child failed; wait for it to exit, to make sure
|
||||||
// the zombies don't accumulate.
|
// the zombies don't accumulate.
|
||||||
_, err1 := Wait4(pid, &wstatus, 0, nil)
|
_, err1 := Wait4(pid, &wstatus, 0, nil)
|
||||||
for err1 == EINTR {
|
for err1 == Errno(syscall.EINTR) {
|
||||||
_, err1 = Wait4(pid, &wstatus, 0, nil)
|
_, err1 = Wait4(pid, &wstatus, 0, nil)
|
||||||
}
|
}
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -230,5 +230,5 @@ func Exec(argv0 string, argv []string, envv []string) (err error) {
|
|||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return syscall.Errno(ret)
|
return Errno(ret)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,13 +78,21 @@ func Getpid() (pid int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Kill(pid int, signum Signal) (err error) {
|
func Kill(pid int, signum Signal) (err error) {
|
||||||
ret := os.Kill(c.Int(pid), c.Int(signum))
|
ret := os.Kill(os.PidT(pid), c.Int(signum))
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return Errno(ret)
|
return Errno(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wait4(pid int, wstatus *c.Int, options int, rusage *syscall.Rusage) (wpid int, err error) {
|
||||||
|
ret := os.Wait4(os.PidT(pid), wstatus, c.Int(options), rusage)
|
||||||
|
if ret >= 0 {
|
||||||
|
return int(ret), nil
|
||||||
|
}
|
||||||
|
return 0, Errno(os.Errno)
|
||||||
|
}
|
||||||
|
|
||||||
func Open(path string, mode int, perm uint32) (fd int, err error) {
|
func Open(path string, mode int, perm uint32) (fd int, err error) {
|
||||||
ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm))
|
ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm))
|
||||||
if ret >= 0 {
|
if ret >= 0 {
|
||||||
@@ -109,6 +117,14 @@ func Read(fd int, p []byte) (n int, err error) {
|
|||||||
return 0, Errno(os.Errno)
|
return 0, Errno(os.Errno)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
|
||||||
|
ret := os.Read(c.Int(fd), unsafe.Pointer(buf), uintptr(nbuf))
|
||||||
|
if ret >= 0 {
|
||||||
|
return ret, nil // TODO(xsw): confirm err == nil (not io.EOF) when ret == 0
|
||||||
|
}
|
||||||
|
return 0, Errno(os.Errno)
|
||||||
|
}
|
||||||
|
|
||||||
func Close(fd int) (err error) {
|
func Close(fd int) (err error) {
|
||||||
ret := os.Close(c.Int(fd))
|
ret := os.Close(c.Int(fd))
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
package syscall
|
package syscall
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/syscall"
|
"github.com/goplus/llgo/c/syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -127,9 +128,8 @@ func (w WaitStatus) StopSignal() Signal {
|
|||||||
|
|
||||||
func (w WaitStatus) TrapCause() int { return -1 }
|
func (w WaitStatus) TrapCause() int { return -1 }
|
||||||
|
|
||||||
/* TODO(xsw):
|
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *syscall.Rusage) (wpid int, err error) {
|
||||||
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
|
var status c.Int
|
||||||
var status _C_int
|
|
||||||
wpid, err = wait4(pid, &status, options, rusage)
|
wpid, err = wait4(pid, &status, options, rusage)
|
||||||
if wstatus != nil {
|
if wstatus != nil {
|
||||||
*wstatus = WaitStatus(status)
|
*wstatus = WaitStatus(status)
|
||||||
@@ -137,6 +137,7 @@ func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* TODO(xsw):
|
||||||
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
||||||
if sa.Port < 0 || sa.Port > 0xFFFF {
|
if sa.Port < 0 || sa.Port > 0xFFFF {
|
||||||
return nil, 0, EINVAL
|
return nil, 0, EINVAL
|
||||||
|
|||||||
Reference in New Issue
Block a user