66package os
77
88import (
9- "errors"
109 "syscall"
1110 "unsafe"
1211)
1312
1413func fork () (pid int , err error ) {
15- ret , _ , _ := syscall .Syscall (syscall .SYS_FORK , 0 , 0 , 0 )
16- if ret < 0 {
17- // TODO: parse the syscall return codes
18- return 0 , errors . New ( "fork failed" )
14+ ret , _ , err := syscall .Syscall (syscall .SYS_FORK , 0 , 0 , 0 )
15+ if int ( ret ) != 0 {
16+ errno := err .( syscall. Errno )
17+ return 0 , errno
1918 }
2019 return int (ret ), nil
2120}
2221
2322// the golang standard library does not expose interfaces for execve and fork, so we define them here the same way via the libc wrapper
24- func execve (pathname string , argv []string , envv []string ) ( err error ) {
23+ func execve (pathname string , argv []string , envv []string ) error {
2524 argv0 := cstring (pathname )
2625
2726 // transform argv and envv into the format expected by execve
@@ -37,10 +36,11 @@ func execve(pathname string, argv []string, envv []string) (err error) {
3736 }
3837 env1 [len (envv )] = nil
3938
40- fail , _ , _ := syscall .Syscall (syscall .SYS_EXECVE , uintptr (unsafe .Pointer (& argv0 [0 ])), uintptr (unsafe .Pointer (& argv1 [0 ])), uintptr (unsafe .Pointer (& env1 [0 ])))
41- if fail < 0 {
42- // TODO: parse the syscall return codes
43- return errors .New ("execve failed" )
39+ ret , _ , err := syscall .Syscall (syscall .SYS_EXECVE , uintptr (unsafe .Pointer (& argv0 [0 ])), uintptr (unsafe .Pointer (& argv1 [0 ])), uintptr (unsafe .Pointer (& env1 [0 ])))
40+ if int (ret ) != 0 {
41+ // errno := err.(syscall.Errno)
42+ // return errno
43+ return err
4444 }
4545
4646 return nil
0 commit comments