1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
//go:build darwin || nintendoswitch || wasi
// +build darwin nintendoswitch wasi
package syscall
import (
"unsafe"
)
type sliceHeader struct {
buf *byte
len uintptr
cap uintptr
}
func Close(fd int) (err error) {
if libc_close(int32(fd)) < 0 {
err = getErrno()
}
return
}
func Dup(fd int) (fd2 int, err error) {
fd2 = int(libc_dup(int32(fd)))
if fd2 < 0 {
err = getErrno()
}
return
}
func Write(fd int, p []byte) (n int, err error) {
buf, count := splitSlice(p)
n = libc_write(int32(fd), buf, uint(count))
if n < 0 {
err = getErrno()
}
return
}
func Read(fd int, p []byte) (n int, err error) {
buf, count := splitSlice(p)
n = libc_read(int32(fd), buf, uint(count))
if n < 0 {
err = getErrno()
}
return
}
func Pread(fd int, p []byte, offset int64) (n int, err error) {
buf, count := splitSlice(p)
n = libc_pread(int32(fd), buf, uint(count), offset)
if n < 0 {
err = getErrno()
}
return
}
func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
newoffset = libc_lseek(int32(fd), offset, whence)
if newoffset < 0 {
err = getErrno()
}
return
}
func Open(path string, flag int, mode uint32) (fd int, err error) {
data := cstring(path)
fd = int(libc_open(&data[0], int32(flag), mode))
if fd < 0 {
err = getErrno()
}
return
}
func Readlink(path string, p []byte) (n int, err error) {
data := cstring(path)
buf, count := splitSlice(p)
n = libc_readlink(&data[0], buf, uint(count))
if n < 0 {
err = getErrno()
}
return
}
func Chdir(path string) (err error) {
data := cstring(path)
fail := int(libc_chdir(&data[0]))
if fail < 0 {
err = getErrno()
}
return
}
func Chmod(path string, mode uint32) (err error) {
data := cstring(path)
fail := int(libc_chmod(&data[0], mode))
if fail < 0 {
err = getErrno()
}
return
}
func Mkdir(path string, mode uint32) (err error) {
data := cstring(path)
fail := int(libc_mkdir(&data[0], mode))
if fail < 0 {
err = getErrno()
}
return
}
func Rmdir(path string) (err error) {
data := cstring(path)
fail := int(libc_rmdir(&data[0]))
if fail < 0 {
err = getErrno()
}
return
}
func Rename(from, to string) (err error) {
fromdata := cstring(from)
todata := cstring(to)
fail := int(libc_rename(&fromdata[0], &todata[0]))
if fail < 0 {
err = getErrno()
}
return
}
func Symlink(from, to string) (err error) {
fromdata := cstring(from)
todata := cstring(to)
fail := int(libc_symlink(&fromdata[0], &todata[0]))
if fail < 0 {
err = getErrno()
}
return
}
func Unlink(path string) (err error) {
data := cstring(path)
fail := int(libc_unlink(&data[0]))
if fail < 0 {
err = getErrno()
}
return
}
func Kill(pid int, sig Signal) (err error) {
return ENOSYS // TODO
}
type SysProcAttr struct{}
// TODO
type WaitStatus uint32
func (w WaitStatus) Exited() bool { return false }
func (w WaitStatus) ExitStatus() int { return 0 }
func (w WaitStatus) Signaled() bool { return false }
func (w WaitStatus) Signal() Signal { return 0 }
func (w WaitStatus) CoreDump() bool { return false }
func (w WaitStatus) Stopped() bool { return false }
func (w WaitStatus) Continued() bool { return false }
func (w WaitStatus) StopSignal() Signal { return 0 }
func (w WaitStatus) TrapCause() int { return 0 }
func Getenv(key string) (value string, found bool) {
data := cstring(key)
raw := libc_getenv(&data[0])
if raw == nil {
return "", false
}
ptr := uintptr(unsafe.Pointer(raw))
for size := uintptr(0); ; size++ {
v := *(*byte)(unsafe.Pointer(ptr))
if v == 0 {
src := *(*[]byte)(unsafe.Pointer(&sliceHeader{buf: raw, len: size, cap: size}))
return string(src), true
}
ptr += unsafe.Sizeof(byte(0))
}
}
func Setenv(key, val string) (err error) {
if len(key) == 0 {
return EINVAL
}
for i := 0; i < len(key); i++ {
if key[i] == '=' || key[i] == 0 {
return EINVAL
}
}
for i := 0; i < len(val); i++ {
if val[i] == 0 {
return EINVAL
}
}
keydata := cstring(key)
valdata := cstring(val)
errCode := libc_setenv(&keydata[0], &valdata[0], 1)
if errCode != 0 {
err = getErrno()
}
return
}
func Unsetenv(key string) (err error) {
keydata := cstring(key)
errCode := libc_unsetenv(&keydata[0])
if errCode != 0 {
err = getErrno()
}
return
}
func Clearenv() {
for _, s := range Environ() {
for j := 0; j < len(s); j++ {
if s[j] == '=' {
Unsetenv(s[0:j])
break
}
}
}
}
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
if addr == unsafe.Pointer(^uintptr(0)) {
return nil, getErrno()
}
return (*[1 << 30]byte)(addr)[:length:length], nil
}
func Munmap(b []byte) (err error) {
errCode := libc_munmap(unsafe.Pointer(&b[0]), uintptr(len(b)))
if errCode != 0 {
err = getErrno()
}
return err
}
func Mprotect(b []byte, prot int) (err error) {
errCode := libc_mprotect(unsafe.Pointer(&b[0]), uintptr(len(b)), int32(prot))
if errCode != 0 {
err = getErrno()
}
return
}
func Environ() []string {
// This function combines all the environment into a single allocation.
// While this optimizes for memory usage and garbage collector
// overhead, it does run the risk of potentially pinning a "large"
// allocation if a user holds onto a single environment variable or
// value. Having each variable be its own allocation would make the
// trade-off in the other direction.
// calculate total memory required
var length uintptr
var vars int
for environ := libc_environ; *environ != nil; {
length += libc_strlen(*environ)
vars++
environ = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(environ)) + unsafe.Sizeof(environ)))
}
// allocate our backing slice for the strings
b := make([]byte, length)
// and the slice we're going to return
envs := make([]string, 0, vars)
// loop over the environment again, this time copying over the data to the backing slice
for environ := libc_environ; *environ != nil; {
length = libc_strlen(*environ)
// construct a Go string pointing at the libc-allocated environment variable data
var envVar string
rawEnvVar := (*struct {
ptr unsafe.Pointer
length uintptr
})(unsafe.Pointer(&envVar))
rawEnvVar.ptr = *environ
rawEnvVar.length = length
// pull off the number of bytes we need for this environment variable
var bs []byte
bs, b = b[:length], b[length:]
// copy over the bytes to the Go heap
copy(bs, envVar)
// convert trimmed slice to string
s := *(*string)(unsafe.Pointer(&bs))
// add s to our list of environment variables
envs = append(envs, s)
// environ++
environ = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(environ)) + unsafe.Sizeof(environ)))
}
return envs
}
// cstring converts a Go string to a C string.
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
copy(data, s)
// final byte should be zero from the initial allocation
return data
}
func splitSlice(p []byte) (buf *byte, len uintptr) {
slice := (*sliceHeader)(unsafe.Pointer(&p))
return slice.buf, slice.len
}
//export strlen
func libc_strlen(ptr unsafe.Pointer) uintptr
// ssize_t write(int fd, const void *buf, size_t count)
//
//export write
func libc_write(fd int32, buf *byte, count uint) int
// char *getenv(const char *name);
//
//export getenv
func libc_getenv(name *byte) *byte
// int setenv(const char *name, const char *val, int replace);
//
//export setenv
func libc_setenv(name *byte, val *byte, replace int32) int32
// int unsetenv(const char *name);
//
//export unsetenv
func libc_unsetenv(name *byte) int32
// ssize_t read(int fd, void *buf, size_t count);
//
//export read
func libc_read(fd int32, buf *byte, count uint) int
// ssize_t pread(int fd, void *buf, size_t count, off_t offset);
//
//export pread
func libc_pread(fd int32, buf *byte, count uint, offset int64) int
// ssize_t lseek(int fd, off_t offset, int whence);
//
//export lseek
func libc_lseek(fd int32, offset int64, whence int) int64
// int open(const char *pathname, int flags, mode_t mode);
//
//export open
func libc_open(pathname *byte, flags int32, mode uint32) int32
// int close(int fd)
//
//export close
func libc_close(fd int32) int32
// int dup(int fd)
//
//export dup
func libc_dup(fd int32) int32
// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
//
//export mmap
func libc_mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uintptr) unsafe.Pointer
// int munmap(void *addr, size_t length);
//
//export munmap
func libc_munmap(addr unsafe.Pointer, length uintptr) int32
// int mprotect(void *addr, size_t len, int prot);
//
//export mprotect
func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32
// int chdir(const char *pathname, mode_t mode);
//
//export chdir
func libc_chdir(pathname *byte) int32
// int chmod(const char *pathname, mode_t mode);
//
//export chmod
func libc_chmod(pathname *byte, mode uint32) int32
// int mkdir(const char *pathname, mode_t mode);
//
//export mkdir
func libc_mkdir(pathname *byte, mode uint32) int32
// int rmdir(const char *pathname);
//
//export rmdir
func libc_rmdir(pathname *byte) int32
// int rename(const char *from, *to);
//
//export rename
func libc_rename(from, to *byte) int32
// int symlink(const char *from, *to);
//
//export symlink
func libc_symlink(from, to *byte) int32
// ssize_t readlink(const char *path, void *buf, size_t count);
//
//export readlink
func libc_readlink(path *byte, buf *byte, count uint) int
// int unlink(const char *pathname);
//
//export unlink
func libc_unlink(pathname *byte) int32
//go:extern environ
var libc_environ *unsafe.Pointer
|