From db8cc8eb7b945d5b08db771475b7f40d0ad2da95 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 18 Jul 2024 17:08:24 +0800 Subject: [PATCH] syscall.Wait4 --- c/os/os.go | 45 +++- internal/lib/syscall/exec_libc2.go | 380 ++++++++++++++-------------- internal/lib/syscall/exec_unix.go | 10 +- internal/lib/syscall/syscall.go | 18 +- internal/lib/syscall/syscall_bsd.go | 7 +- 5 files changed, 255 insertions(+), 205 deletions(-) diff --git a/c/os/os.go b/c/os/os.go index 583cf631..563f0071 100644 --- a/c/os/os.go +++ b/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 func Execvp(file *c.Char, argv **c.Char) c.Int +// ----------------------------------------------------------------------------- + +type PidT c.Int + //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 -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 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 func Getuid() UidT diff --git a/internal/lib/syscall/exec_libc2.go b/internal/lib/syscall/exec_libc2.go index 95f259d3..074632ac 100644 --- a/internal/lib/syscall/exec_libc2.go +++ b/internal/lib/syscall/exec_libc2.go @@ -6,6 +6,13 @@ package syscall +import ( + "runtime" + "unsafe" + + "github.com/goplus/llgo/c" +) + type SysProcAttr struct { Chroot string // Chroot. Credential *Credential // Credential. @@ -48,146 +55,165 @@ func runtime_AfterForkInChild() // functions that do not grow the stack. // //go:norace -func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) { - /* TODO(xsw): - // Declare all variables at top in case any - // declarations require heap allocation (e.g., err1). - var ( - r1 uintptr - nextfd int - i int - err error - pgrp _C_int - cred *Credential - ngroups, groups uintptr - ) +func forkAndExecInChild(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) { + // Declare all variables at top in case any + // declarations require heap allocation (e.g., err1). + var ( + r1 uintptr + nextfd int + i int + err error + pgrp c.Int + cred *Credential + ngroups, groups uintptr + ) - rlim, rlimOK := origRlimitNofile.Load().(Rlimit) + rlim, rlimOK := origRlimitNofile.Load().(Rlimit) - // guard against side effects of shuffling fds below. - // Make sure that nextfd is beyond any currently open files so - // that we can't run the risk of overwriting any of them. - fd := make([]int, len(attr.Files)) - nextfd = len(attr.Files) - for i, ufd := range attr.Files { - if nextfd < int(ufd) { - nextfd = int(ufd) - } - fd[i] = int(ufd) + // guard against side effects of shuffling fds below. + // Make sure that nextfd is beyond any currently open files so + // that we can't run the risk of overwriting any of them. + fd := make([]int, len(attr.Files)) + nextfd = len(attr.Files) + for i, ufd := range attr.Files { + if nextfd < int(ufd) { + nextfd = int(ufd) } - nextfd++ + fd[i] = int(ufd) + } + nextfd++ - // About to call fork. - // No more allocation or calls of non-assembly functions. - runtime_BeforeFork() - r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0) + // About to call fork. + // No more allocation or calls of non-assembly functions. + runtime_BeforeFork() + 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 { - runtime_AfterFork() - return 0, err1 + goto childerror } + } - if r1 != 0 { - // parent; return PID - runtime_AfterFork() - return int(r1), 0 + // Set process group + if sys.Setpgid || sys.Foreground { + // Place child in process group. + _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0) + if err1 != 0 { + goto childerror } + } - // 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) + if sys.Foreground { + // This should really be pid_t, however _C_int (aka int32) is + // 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) } - // Session ID - if sys.Setsid { - _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0) + // 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 + // 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 { goto childerror } } - - // Set process group - if sys.Setpgid || sys.Foreground { - // 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_setgid_trampoline), uintptr(cred.Gid), 0, 0) + if err1 != 0 { + goto childerror } - - if sys.Foreground { - // This should really be pid_t, however _C_int (aka int32) is - // 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 - } + _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 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 - } + // Chdir + if dir != nil { + _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 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 { - 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) + // Pass 1: look for fd[i] < i and move those up above len(fd) + // so that pass 2 won't stomp on an fd it needs later. + if pipe < nextfd { + if runtime.GOOS == "openbsd" { + _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), O_CLOEXEC) + } else { + _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0) if err1 != 0 { goto childerror } + _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC) } - - // Chdir - if dir != nil { - _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0) - if err1 != 0 { - goto childerror - } + if err1 != 0 { + goto childerror } - - // Pass 1: look for fd[i] < i and move those up above len(fd) - // so that pass 2 won't stomp on an fd it needs later. - if pipe < nextfd { + pipe = 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(pipe), uintptr(nextfd), O_CLOEXEC) + _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC) } else { - _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0) + _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), 0) if err1 != 0 { goto childerror } @@ -196,95 +222,73 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr if err1 != 0 { goto childerror } - pipe = nextfd + fd[i] = 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. - for i = 0; i < len(fd); i++ { - 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++ { + // Pass 2: dup fd[i] down onto i. + for i = 0; i < len(fd); i++ { + if fd[i] == -1 { rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0) + continue } - - // Detach fd 0 from tty - if sys.Noctty { - _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0) + 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 } - - // Set the controlling TTY to Ctty - if sys.Setctty { - _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0) - if err1 != 0 { - goto childerror - } + // 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 } + } - // Restore original rlimit. - if rlimOK && rlim.Cur != 0 { - rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0) + // 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) + } + + // 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. - _, _, 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) + // Set the controlling TTY to Ctty + if sys.Setctty { + _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0) + if err1 != 0 { + goto childerror } - */ - 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) + } } diff --git a/internal/lib/syscall/exec_unix.go b/internal/lib/syscall/exec_unix.go index 7ab4d6b4..d979d891 100644 --- a/internal/lib/syscall/exec_unix.go +++ b/internal/lib/syscall/exec_unix.go @@ -12,11 +12,11 @@ import ( "errors" "runtime" "sync" - "syscall" "unsafe" "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/os" + "github.com/goplus/llgo/c/syscall" ) // 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]) for { n, err = readlen(p[0], (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1))) - if err != EINTR { + if err != Errno(syscall.EINTR) { break } } @@ -185,13 +185,13 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) err = Errno(err1) } if err == nil { - err = EPIPE + err = Errno(syscall.EPIPE) } // Child failed; wait for it to exit, to make sure // the zombies don't accumulate. _, err1 := Wait4(pid, &wstatus, 0, nil) - for err1 == EINTR { + for err1 == Errno(syscall.EINTR) { _, err1 = Wait4(pid, &wstatus, 0, nil) } return 0, err @@ -230,5 +230,5 @@ func Exec(argv0 string, argv []string, envv []string) (err error) { if ret == 0 { return nil } - return syscall.Errno(ret) + return Errno(ret) } diff --git a/internal/lib/syscall/syscall.go b/internal/lib/syscall/syscall.go index 4877478c..bf940729 100644 --- a/internal/lib/syscall/syscall.go +++ b/internal/lib/syscall/syscall.go @@ -78,13 +78,21 @@ func Getpid() (pid int) { } 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 { return nil } 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) { ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm)) if ret >= 0 { @@ -109,6 +117,14 @@ func Read(fd int, p []byte) (n int, err error) { 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) { ret := os.Close(c.Int(fd)) if ret == 0 { diff --git a/internal/lib/syscall/syscall_bsd.go b/internal/lib/syscall/syscall_bsd.go index 95eafe19..b74cb8ea 100644 --- a/internal/lib/syscall/syscall_bsd.go +++ b/internal/lib/syscall/syscall_bsd.go @@ -13,6 +13,7 @@ package syscall import ( + "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/syscall" ) @@ -127,9 +128,8 @@ func (w WaitStatus) StopSignal() Signal { func (w WaitStatus) TrapCause() int { return -1 } -/* TODO(xsw): -func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { - var status _C_int +func Wait4(pid int, wstatus *WaitStatus, options int, rusage *syscall.Rusage) (wpid int, err error) { + var status c.Int wpid, err = wait4(pid, &status, options, rusage) if wstatus != nil { *wstatus = WaitStatus(status) @@ -137,6 +137,7 @@ func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, return } +/* TODO(xsw): func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) { if sa.Port < 0 || sa.Port > 0xFFFF { return nil, 0, EINVAL