Skip to content

Commit cb2f4c3

Browse files
committed
delme: syscall_unix workaround wip
1 parent 836d3cb commit cb2f4c3

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/syscall/syscall_libc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ func Execve(pathname string, argv []string, envv []string) (err error) {
203203
return
204204
}
205205

206+
func Truncate(path string, length int64) (err error) {
207+
data := cstring(path)
208+
fail := int(libc_truncate(&data[0], length))
209+
if fail < 0 {
210+
err = getErrno()
211+
}
212+
return
213+
}
214+
206215
func Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
207216

208217
func Kill(pid int, sig Signal) (err error) {
@@ -451,3 +460,8 @@ func libc_fork() int32
451460
//
452461
//export execve
453462
func libc_execve(filename *byte, argv **byte, envp **byte) int
463+
464+
// int truncate(const char *path, off_t length);
465+
//
466+
//export truncate
467+
func libc_truncate(path *byte, length int64) int32

src/syscall/syscall_unix.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package syscall
1+
//go: build !darwin && !nintendoswitch && !wasip1 && !wasip2
22

3-
func Exec(argv0 string, argv []string, envv []string) (err error)
3+
package syscall
44

55
// The two SockaddrInet* structs have been copied from the Go source tree.
6-
76
type SockaddrInet4 struct {
87
Port int
98
Addr [4]byte
@@ -16,3 +15,56 @@ type SockaddrInet6 struct {
1615
Addr [16]byte
1716
raw RawSockaddrInet6
1817
}
18+
19+
/*
20+
func Fork() (err error) {
21+
fail := int(libc_fork())
22+
if fail < 0 {
23+
// TODO: parse the syscall return codes
24+
return errors.New("fork failed")
25+
}
26+
return
27+
}
28+
29+
// the golang standard library does not expose interfaces for execve and fork, so we define them here the same way via the libc wrapper
30+
func Execve(pathname string, argv []string, envv []string) (err error) {
31+
argv0 := cstring(pathname)
32+
33+
// transform argv and envv into the format expected by execve
34+
argv1 := make([]*byte, len(argv)+1)
35+
for i, arg := range argv {
36+
argv1[i] = &cstring(arg)[0]
37+
}
38+
argv1[len(argv)] = nil
39+
40+
env1 := make([]*byte, len(envv)+1)
41+
for i, env := range envv {
42+
env1[i] = &cstring(env)[0]
43+
}
44+
env1[len(envv)] = nil
45+
46+
fail := int(libc_execve(&argv0[0], &argv1[0], &env1[0]))
47+
if fail < 0 {
48+
// TODO: parse the syscall return codes
49+
return errors.New("fork failed")
50+
}
51+
return
52+
}
53+
54+
func cstring(s string) []byte {
55+
data := make([]byte, len(s)+1)
56+
copy(data, s)
57+
// final byte should be zero from the initial allocation
58+
return data
59+
}
60+
61+
// pid_t fork(void);
62+
//
63+
//export fork
64+
func libc_fork() int32
65+
66+
// int execve(const char *filename, char *const argv[], char *const envp[]);
67+
//
68+
//export execve
69+
func libc_execve(filename *byte, argv **byte, envp **byte) int
70+
*/

0 commit comments

Comments
 (0)