syscall.Wait4

This commit is contained in:
xushiwei
2024-07-18 17:08:24 +08:00
parent daf0a9dc9a
commit db8cc8eb7b
5 changed files with 255 additions and 205 deletions

View File

@@ -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

View File

@@ -6,6 +6,13 @@
package syscall
import (
"runtime"
"unsafe"
"github.com/goplus/llgo/c"
)
type SysProcAttr struct {
Chroot string // Chroot.
Credential *Credential // Credential.
@@ -48,8 +55,7 @@ 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):
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 (
@@ -57,7 +63,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
nextfd int
i int
err error
pgrp _C_int
pgrp c.Int
cred *Credential
ngroups, groups uintptr
)
@@ -279,12 +285,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
uintptr(unsafe.Pointer(&argv[0])),
uintptr(unsafe.Pointer(&envv[0])))
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")
}

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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