1 | // Copyright 2020 The Libc Authors. All rights reserved.
|
---|
2 | // Use of this source code is governed by a BSD-style
|
---|
3 | // license that can be found in the LICENSE file.
|
---|
4 |
|
---|
5 | package libc // import "modernc.org/libc"
|
---|
6 |
|
---|
7 | import (
|
---|
8 | "strings"
|
---|
9 | "time"
|
---|
10 | "unsafe"
|
---|
11 |
|
---|
12 | "golang.org/x/sys/unix"
|
---|
13 | "modernc.org/libc/fcntl"
|
---|
14 | "modernc.org/libc/signal"
|
---|
15 | "modernc.org/libc/sys/types"
|
---|
16 | "modernc.org/libc/utime"
|
---|
17 | )
|
---|
18 |
|
---|
19 | // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
|
---|
20 | func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
|
---|
21 | var kact, koldact uintptr
|
---|
22 | if act != 0 {
|
---|
23 | sz := int(unsafe.Sizeof(signal.X__sigaction{}))
|
---|
24 | kact = t.Alloc(sz)
|
---|
25 | defer t.Free(sz)
|
---|
26 | (*signal.X__sigaction)(unsafe.Pointer(kact)).F__sigaction_u.F__sa_handler = (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_u.F__sa_handler
|
---|
27 | (*signal.X__sigaction)(unsafe.Pointer(kact)).Fsa_flags = (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags
|
---|
28 | Xmemcpy(t, kact+unsafe.Offsetof(signal.X__sigaction{}.Fsa_mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(signal.Sigset_t(0))))
|
---|
29 | }
|
---|
30 | if oldact != 0 {
|
---|
31 | panic(todo(""))
|
---|
32 | }
|
---|
33 |
|
---|
34 | if _, _, err := unix.Syscall6(unix.SYS_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(signal.Sigset_t(0)), 0, 0); err != 0 {
|
---|
35 | t.setErrno(err)
|
---|
36 | return -1
|
---|
37 | }
|
---|
38 |
|
---|
39 | if oldact != 0 {
|
---|
40 | panic(todo(""))
|
---|
41 | }
|
---|
42 |
|
---|
43 | return 0
|
---|
44 | }
|
---|
45 |
|
---|
46 | // int fcntl(int fd, int cmd, ... /* arg */ );
|
---|
47 | func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) (r int32) {
|
---|
48 | var err error
|
---|
49 | var p uintptr
|
---|
50 | var i int
|
---|
51 | switch cmd {
|
---|
52 | case fcntl.F_GETLK, fcntl.F_SETLK, fcntl.F_SETLKW:
|
---|
53 | p = *(*uintptr)(unsafe.Pointer(args))
|
---|
54 | err = unix.FcntlFlock(uintptr(fd), int(cmd), (*unix.Flock_t)(unsafe.Pointer(p)))
|
---|
55 | case fcntl.F_GETFL, fcntl.F_FULLFSYNC:
|
---|
56 | i, err = unix.FcntlInt(uintptr(fd), int(cmd), 0)
|
---|
57 | r = int32(i)
|
---|
58 | case fcntl.F_SETFD, fcntl.F_SETFL:
|
---|
59 | arg := *(*int32)(unsafe.Pointer(args))
|
---|
60 | _, err = unix.FcntlInt(uintptr(fd), int(cmd), int(arg))
|
---|
61 | default:
|
---|
62 | panic(todo("%v: %v %v", origin(1), fd, cmd))
|
---|
63 | }
|
---|
64 | if err != nil {
|
---|
65 | if dmesgs {
|
---|
66 | dmesg("%v: fd %v cmd %v p %#x: %v FAIL", origin(1), fcntlCmdStr(fd), cmd, p, err)
|
---|
67 | }
|
---|
68 | t.setErrno(err)
|
---|
69 | return -1
|
---|
70 | }
|
---|
71 |
|
---|
72 | if dmesgs {
|
---|
73 | dmesg("%v: %d %s %#x: ok", origin(1), fd, fcntlCmdStr(cmd), p)
|
---|
74 | }
|
---|
75 | return r
|
---|
76 | }
|
---|
77 |
|
---|
78 | // int lstat(const char *pathname, struct stat *statbuf);
|
---|
79 | func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
80 | if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
|
---|
81 | if dmesgs {
|
---|
82 | dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
|
---|
83 | }
|
---|
84 | t.setErrno(err)
|
---|
85 | return -1
|
---|
86 | }
|
---|
87 |
|
---|
88 | if dmesgs {
|
---|
89 | dmesg("%v: %q: ok", origin(1), GoString(pathname))
|
---|
90 | }
|
---|
91 | return 0
|
---|
92 | }
|
---|
93 |
|
---|
94 | // int stat(const char *pathname, struct stat *statbuf);
|
---|
95 | func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
96 | if err := unix.Stat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
|
---|
97 | if dmesgs {
|
---|
98 | dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
|
---|
99 | }
|
---|
100 | t.setErrno(err)
|
---|
101 | return -1
|
---|
102 | }
|
---|
103 |
|
---|
104 | if dmesgs {
|
---|
105 | dmesg("%v: %q: ok", origin(1), GoString(pathname))
|
---|
106 | }
|
---|
107 | return 0
|
---|
108 | }
|
---|
109 |
|
---|
110 | // int fstatfs(int fd, struct statfs *buf);
|
---|
111 | func Xfstatfs(t *TLS, fd int32, buf uintptr) int32 {
|
---|
112 | if err := unix.Fstatfs(int(fd), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
|
---|
113 | if dmesgs {
|
---|
114 | dmesg("%v: %v: %v FAIL", origin(1), fd, err)
|
---|
115 | }
|
---|
116 | t.setErrno(err)
|
---|
117 | return -1
|
---|
118 | }
|
---|
119 |
|
---|
120 | if dmesgs {
|
---|
121 | dmesg("%v: %v: ok", origin(1), fd)
|
---|
122 | }
|
---|
123 | return 0
|
---|
124 | }
|
---|
125 |
|
---|
126 | // int statfs(const char *path, struct statfs *buf);
|
---|
127 | func Xstatfs(t *TLS, path uintptr, buf uintptr) int32 {
|
---|
128 | if err := unix.Statfs(GoString(path), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
|
---|
129 | if dmesgs {
|
---|
130 | dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
|
---|
131 | }
|
---|
132 | t.setErrno(err)
|
---|
133 | return -1
|
---|
134 | }
|
---|
135 |
|
---|
136 | if dmesgs {
|
---|
137 | dmesg("%v: %q: ok", origin(1), GoString(path))
|
---|
138 | }
|
---|
139 | return 0
|
---|
140 | }
|
---|
141 |
|
---|
142 | // int fstat(int fd, struct stat *statbuf);
|
---|
143 | func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
|
---|
144 | if err := unix.Fstat(int(fd), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
|
---|
145 | if dmesgs {
|
---|
146 | dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
|
---|
147 | }
|
---|
148 | t.setErrno(err)
|
---|
149 | return -1
|
---|
150 | }
|
---|
151 |
|
---|
152 | if dmesgs {
|
---|
153 | dmesg("%v: fd %d: ok", origin(1), fd)
|
---|
154 | }
|
---|
155 | return 0
|
---|
156 | }
|
---|
157 |
|
---|
158 | // off64_t lseek64(int fd, off64_t offset, int whence);
|
---|
159 | func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
|
---|
160 | n, err := unix.Seek(int(fd), int64(offset), int(whence))
|
---|
161 | if err != nil {
|
---|
162 | if dmesgs {
|
---|
163 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
164 | }
|
---|
165 | t.setErrno(err)
|
---|
166 | return -1
|
---|
167 | }
|
---|
168 |
|
---|
169 | if dmesgs {
|
---|
170 | dmesg("%v: ok", origin(1))
|
---|
171 | }
|
---|
172 | return types.Off_t(n)
|
---|
173 | }
|
---|
174 |
|
---|
175 | // int utime(const char *filename, const struct utimbuf *times);
|
---|
176 | func Xutime(t *TLS, filename, times uintptr) int32 {
|
---|
177 | var a []unix.Timeval
|
---|
178 | if times != 0 {
|
---|
179 | a = make([]unix.Timeval, 2)
|
---|
180 | a[0].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Factime
|
---|
181 | a[1].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Fmodtime
|
---|
182 | }
|
---|
183 | if err := unix.Utimes(GoString(filename), a); err != nil {
|
---|
184 | if dmesgs {
|
---|
185 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
186 | }
|
---|
187 | t.setErrno(err)
|
---|
188 | return -1
|
---|
189 | }
|
---|
190 |
|
---|
191 | if dmesgs {
|
---|
192 | dmesg("%v: ok", origin(1))
|
---|
193 | }
|
---|
194 | return 0
|
---|
195 | }
|
---|
196 |
|
---|
197 | // unsigned int alarm(unsigned int seconds);
|
---|
198 | func Xalarm(t *TLS, seconds uint32) uint32 {
|
---|
199 | panic(todo(""))
|
---|
200 | // n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
|
---|
201 | // if err != 0 {
|
---|
202 | // panic(todo(""))
|
---|
203 | // }
|
---|
204 |
|
---|
205 | // return uint32(n)
|
---|
206 | }
|
---|
207 |
|
---|
208 | // time_t time(time_t *tloc);
|
---|
209 | func Xtime(t *TLS, tloc uintptr) types.Time_t {
|
---|
210 | n := time.Now().UTC().Unix()
|
---|
211 | if tloc != 0 {
|
---|
212 | *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
|
---|
213 | }
|
---|
214 | return types.Time_t(n)
|
---|
215 | }
|
---|
216 |
|
---|
217 | // // int getrlimit(int resource, struct rlimit *rlim);
|
---|
218 | // func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
219 | // if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
|
---|
220 | // t.setErrno(err)
|
---|
221 | // return -1
|
---|
222 | // }
|
---|
223 | //
|
---|
224 | // return 0
|
---|
225 | // }
|
---|
226 |
|
---|
227 | // int mkdir(const char *path, mode_t mode);
|
---|
228 | func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
|
---|
229 | if err := unix.Mkdir(GoString(path), uint32(mode)); err != nil {
|
---|
230 | if dmesgs {
|
---|
231 | dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
|
---|
232 | }
|
---|
233 | t.setErrno(err)
|
---|
234 | return -1
|
---|
235 | }
|
---|
236 |
|
---|
237 | if dmesgs {
|
---|
238 | dmesg("%v: %q: ok", origin(1), GoString(path))
|
---|
239 | }
|
---|
240 | return 0
|
---|
241 | }
|
---|
242 |
|
---|
243 | // int symlink(const char *target, const char *linkpath);
|
---|
244 | func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
|
---|
245 | if err := unix.Symlink(GoString(target), GoString(linkpath)); err != nil {
|
---|
246 | if dmesgs {
|
---|
247 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
248 | }
|
---|
249 | t.setErrno(err)
|
---|
250 | return -1
|
---|
251 | }
|
---|
252 |
|
---|
253 | if dmesgs {
|
---|
254 | dmesg("%v: ok", origin(1))
|
---|
255 | }
|
---|
256 | return 0
|
---|
257 | }
|
---|
258 |
|
---|
259 | // int chmod(const char *pathname, mode_t mode)
|
---|
260 | func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
|
---|
261 | if err := unix.Chmod(GoString(pathname), uint32(mode)); err != nil {
|
---|
262 | if dmesgs {
|
---|
263 | dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
|
---|
264 | }
|
---|
265 | t.setErrno(err)
|
---|
266 | return -1
|
---|
267 | }
|
---|
268 |
|
---|
269 | if dmesgs {
|
---|
270 | dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
|
---|
271 | }
|
---|
272 | return 0
|
---|
273 | }
|
---|
274 |
|
---|
275 | // int utimes(const char *filename, const struct timeval times[2]);
|
---|
276 | func Xutimes(t *TLS, filename, times uintptr) int32 {
|
---|
277 | var a []unix.Timeval
|
---|
278 | if times != 0 {
|
---|
279 | a = make([]unix.Timeval, 2)
|
---|
280 | a[0] = *(*unix.Timeval)(unsafe.Pointer(times))
|
---|
281 | a[1] = *(*unix.Timeval)(unsafe.Pointer(times + unsafe.Sizeof(unix.Timeval{})))
|
---|
282 | }
|
---|
283 | if err := unix.Utimes(GoString(filename), a); err != nil {
|
---|
284 | if dmesgs {
|
---|
285 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
286 | }
|
---|
287 | t.setErrno(err)
|
---|
288 | return -1
|
---|
289 | }
|
---|
290 |
|
---|
291 | if dmesgs {
|
---|
292 | dmesg("%v: ok", origin(1))
|
---|
293 | }
|
---|
294 | return 0
|
---|
295 | }
|
---|
296 |
|
---|
297 | // int unlink(const char *pathname);
|
---|
298 | func Xunlink(t *TLS, pathname uintptr) int32 {
|
---|
299 | if err := unix.Unlink(GoString(pathname)); err != nil {
|
---|
300 | if dmesgs {
|
---|
301 | dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
|
---|
302 | }
|
---|
303 | t.setErrno(err)
|
---|
304 | return -1
|
---|
305 | }
|
---|
306 |
|
---|
307 | if dmesgs {
|
---|
308 | dmesg("%v: ok", origin(1))
|
---|
309 | }
|
---|
310 | return 0
|
---|
311 | }
|
---|
312 |
|
---|
313 | // int access(const char *pathname, int mode);
|
---|
314 | func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
|
---|
315 | if err := unix.Access(GoString(pathname), uint32(mode)); err != nil {
|
---|
316 | if dmesgs {
|
---|
317 | dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
|
---|
318 | }
|
---|
319 | t.setErrno(err)
|
---|
320 | return -1
|
---|
321 | }
|
---|
322 |
|
---|
323 | if dmesgs {
|
---|
324 | dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
|
---|
325 | }
|
---|
326 | return 0
|
---|
327 | }
|
---|
328 |
|
---|
329 | // int rename(const char *oldpath, const char *newpath);
|
---|
330 | func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
|
---|
331 | if err := unix.Rename(GoString(oldpath), GoString(newpath)); err != nil {
|
---|
332 | if dmesgs {
|
---|
333 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
334 | }
|
---|
335 | t.setErrno(err)
|
---|
336 | return -1
|
---|
337 | }
|
---|
338 |
|
---|
339 | if dmesgs {
|
---|
340 | dmesg("%v: ok", origin(1))
|
---|
341 | }
|
---|
342 | return 0
|
---|
343 | }
|
---|
344 |
|
---|
345 | // int mknod(const char *pathname, mode_t mode, dev_t dev);
|
---|
346 | func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
|
---|
347 | panic(todo(""))
|
---|
348 | // if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
|
---|
349 | // t.setErrno(err)
|
---|
350 | // return -1
|
---|
351 | // }
|
---|
352 |
|
---|
353 | // return 0
|
---|
354 | }
|
---|
355 |
|
---|
356 | // int chown(const char *pathname, uid_t owner, gid_t group);
|
---|
357 | func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
|
---|
358 | panic(todo(""))
|
---|
359 | // if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
|
---|
360 | // t.setErrno(err)
|
---|
361 | // return -1
|
---|
362 | // }
|
---|
363 |
|
---|
364 | // return 0
|
---|
365 | }
|
---|
366 |
|
---|
367 | // int link(const char *oldpath, const char *newpath);
|
---|
368 | func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
|
---|
369 | if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
|
---|
370 | t.setErrno(err)
|
---|
371 | return -1
|
---|
372 | }
|
---|
373 |
|
---|
374 | return 0
|
---|
375 | }
|
---|
376 |
|
---|
377 | // int dup2(int oldfd, int newfd);
|
---|
378 | func Xdup2(t *TLS, oldfd, newfd int32) int32 {
|
---|
379 | panic(todo(""))
|
---|
380 | // n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
|
---|
381 | // if err != 0 {
|
---|
382 | // t.setErrno(err)
|
---|
383 | // return -1
|
---|
384 | // }
|
---|
385 |
|
---|
386 | // return int32(n)
|
---|
387 | }
|
---|
388 |
|
---|
389 | // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
|
---|
390 | func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
|
---|
391 | var n int
|
---|
392 | var err error
|
---|
393 | switch {
|
---|
394 | case buf == 0 || bufsize == 0:
|
---|
395 | n, err = unix.Readlink(GoString(path), nil)
|
---|
396 | default:
|
---|
397 | n, err = unix.Readlink(GoString(path), (*RawMem)(unsafe.Pointer(buf))[:bufsize:bufsize])
|
---|
398 | }
|
---|
399 | if err != nil {
|
---|
400 | if dmesgs {
|
---|
401 | dmesg("%v: %v FAIL", err)
|
---|
402 | }
|
---|
403 | t.setErrno(err)
|
---|
404 | return -1
|
---|
405 | }
|
---|
406 |
|
---|
407 | if dmesgs {
|
---|
408 | dmesg("%v: ok")
|
---|
409 | }
|
---|
410 | return types.Ssize_t(n)
|
---|
411 | }
|
---|
412 |
|
---|
413 | // FILE *fopen64(const char *pathname, const char *mode);
|
---|
414 | func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
|
---|
415 | m := strings.ReplaceAll(GoString(mode), "b", "")
|
---|
416 | var flags int
|
---|
417 | switch m {
|
---|
418 | case "r":
|
---|
419 | flags = fcntl.O_RDONLY
|
---|
420 | case "r+":
|
---|
421 | flags = fcntl.O_RDWR
|
---|
422 | case "w":
|
---|
423 | flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_TRUNC
|
---|
424 | case "w+":
|
---|
425 | flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_TRUNC
|
---|
426 | case "a":
|
---|
427 | flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_APPEND
|
---|
428 | case "a+":
|
---|
429 | flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_APPEND
|
---|
430 | default:
|
---|
431 | panic(m)
|
---|
432 | }
|
---|
433 | fd, err := unix.Open(GoString(pathname), int(flags), 0666)
|
---|
434 | if err != nil {
|
---|
435 | if dmesgs {
|
---|
436 | dmesg("%v: %q %q: %v FAIL", origin(1), GoString(pathname), GoString(mode), err)
|
---|
437 | }
|
---|
438 | t.setErrno(err)
|
---|
439 | return 0
|
---|
440 | }
|
---|
441 |
|
---|
442 | if dmesgs {
|
---|
443 | dmesg("%v: %q %q: fd %v", origin(1), GoString(pathname), GoString(mode), fd)
|
---|
444 | }
|
---|
445 | if p := newFile(t, int32(fd)); p != 0 {
|
---|
446 | return p
|
---|
447 | }
|
---|
448 |
|
---|
449 | panic("OOM")
|
---|
450 | }
|
---|