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 | "os"
|
---|
9 | "strings"
|
---|
10 | "unicode"
|
---|
11 | "unsafe"
|
---|
12 |
|
---|
13 | "golang.org/x/sys/unix"
|
---|
14 | "modernc.org/libc/errno"
|
---|
15 | "modernc.org/libc/fcntl"
|
---|
16 | "modernc.org/libc/signal"
|
---|
17 | "modernc.org/libc/sys/types"
|
---|
18 | "modernc.org/libc/utime"
|
---|
19 | "modernc.org/libc/wctype"
|
---|
20 | )
|
---|
21 |
|
---|
22 | // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
|
---|
23 | func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
|
---|
24 | // musl/arch/x86_64/ksigaction.h
|
---|
25 | //
|
---|
26 | // struct k_sigaction {
|
---|
27 | // void (*handler)(int);
|
---|
28 | // unsigned long flags;
|
---|
29 | // void (*restorer)(void);
|
---|
30 | // unsigned mask[2];
|
---|
31 | // };
|
---|
32 | type k_sigaction struct {
|
---|
33 | handler uintptr
|
---|
34 | flags ulong
|
---|
35 | restorer uintptr
|
---|
36 | mask [2]uint32
|
---|
37 | }
|
---|
38 |
|
---|
39 | var kact, koldact uintptr
|
---|
40 | if act != 0 {
|
---|
41 | sz := int(unsafe.Sizeof(k_sigaction{}))
|
---|
42 | kact = t.Alloc(sz)
|
---|
43 | defer t.Free(sz)
|
---|
44 | *(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
|
---|
45 | handler: (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
|
---|
46 | flags: ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
|
---|
47 | restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
|
---|
48 | }
|
---|
49 | Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
|
---|
50 | }
|
---|
51 | if oldact != 0 {
|
---|
52 | panic(todo(""))
|
---|
53 | }
|
---|
54 |
|
---|
55 | if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
|
---|
56 | t.setErrno(err)
|
---|
57 | return -1
|
---|
58 | }
|
---|
59 |
|
---|
60 | if oldact != 0 {
|
---|
61 | panic(todo(""))
|
---|
62 | }
|
---|
63 |
|
---|
64 | return 0
|
---|
65 | }
|
---|
66 |
|
---|
67 | // int fcntl(int fd, int cmd, ... /* arg */ );
|
---|
68 | func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
|
---|
69 | var arg uintptr
|
---|
70 | if args != 0 {
|
---|
71 | arg = *(*uintptr)(unsafe.Pointer(args))
|
---|
72 | }
|
---|
73 | if cmd == fcntl.F_SETFL {
|
---|
74 | arg |= unix.O_LARGEFILE
|
---|
75 | }
|
---|
76 | n, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(cmd), arg)
|
---|
77 | if err != 0 {
|
---|
78 | // if dmesgs {
|
---|
79 | // dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
|
---|
80 | // }
|
---|
81 | t.setErrno(err)
|
---|
82 | return -1
|
---|
83 | }
|
---|
84 |
|
---|
85 | // if dmesgs {
|
---|
86 | // dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
|
---|
87 | // }
|
---|
88 | return int32(n)
|
---|
89 | }
|
---|
90 |
|
---|
91 | // int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
|
---|
92 | func Xfstatat(t *TLS, dirfd int32, pathname, statbuf uintptr, flags int32) int32 {
|
---|
93 | // From golang.org/x/sys/unix/zsyscall_linux_riscv64.go
|
---|
94 | if _, _, err := unix.Syscall6(unix.SYS_FSTATAT, uintptr(dirfd), pathname, statbuf, uintptr(flags), 0, 0); err != 0 {
|
---|
95 | t.setErrno(err)
|
---|
96 | return -1
|
---|
97 | }
|
---|
98 |
|
---|
99 | return 0
|
---|
100 | }
|
---|
101 |
|
---|
102 | // int lstat(const char *pathname, struct stat *statbuf);
|
---|
103 | func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
104 | // From golang.org/x/sys/unix/syscall_linux_riscv64.go
|
---|
105 | return Xfstatat(t, unix.AT_FDCWD, pathname, statbuf, unix.AT_SYMLINK_NOFOLLOW)
|
---|
106 | }
|
---|
107 |
|
---|
108 | // int stat(const char *pathname, struct stat *statbuf);
|
---|
109 | func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
110 | // From golang.org/x/sys/unix/syscall_linux_riscv64.go
|
---|
111 | return Xfstatat(t, unix.AT_FDCWD, pathname, statbuf, 0)
|
---|
112 | }
|
---|
113 |
|
---|
114 | // int fstat(int fd, struct stat *statbuf);
|
---|
115 | func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
|
---|
116 | if _, _, err := unix.Syscall(unix.SYS_FSTAT, uintptr(fd), statbuf, 0); err != 0 {
|
---|
117 | // if dmesgs {
|
---|
118 | // dmesg("%v: fd %d: %v", origin(1), fd, err)
|
---|
119 | // }
|
---|
120 | t.setErrno(err)
|
---|
121 | return -1
|
---|
122 | }
|
---|
123 |
|
---|
124 | // if dmesgs {
|
---|
125 | // dmesg("%v: %d size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
|
---|
126 | // }
|
---|
127 | return 0
|
---|
128 | }
|
---|
129 |
|
---|
130 | func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
|
---|
131 | return Xmmap64(t, addr, length, prot, flags, fd, offset)
|
---|
132 | }
|
---|
133 |
|
---|
134 | // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
|
---|
135 | func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
|
---|
136 | data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
|
---|
137 | if err != 0 {
|
---|
138 | // if dmesgs {
|
---|
139 | // dmesg("%v: %v", origin(1), err)
|
---|
140 | // }
|
---|
141 | t.setErrno(err)
|
---|
142 | return ^uintptr(0) // (void*)-1
|
---|
143 | }
|
---|
144 |
|
---|
145 | // if dmesgs {
|
---|
146 | // dmesg("%v: %#x", origin(1), data)
|
---|
147 | // }
|
---|
148 | return data
|
---|
149 | }
|
---|
150 |
|
---|
151 | // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
|
---|
152 | func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
|
---|
153 | var arg uintptr
|
---|
154 | if args != 0 {
|
---|
155 | arg = *(*uintptr)(unsafe.Pointer(args))
|
---|
156 | }
|
---|
157 | data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
|
---|
158 | if err != 0 {
|
---|
159 | // if dmesgs {
|
---|
160 | // dmesg("%v: %v", origin(1), err)
|
---|
161 | // }
|
---|
162 | t.setErrno(err)
|
---|
163 | return ^uintptr(0) // (void*)-1
|
---|
164 | }
|
---|
165 |
|
---|
166 | // if dmesgs {
|
---|
167 | // dmesg("%v: %#x", origin(1), data)
|
---|
168 | // }
|
---|
169 | return data
|
---|
170 | }
|
---|
171 |
|
---|
172 | // int ftruncate(int fd, off_t length);
|
---|
173 | func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
|
---|
174 | if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE, uintptr(fd), uintptr(length), 0); err != 0 {
|
---|
175 | // if dmesgs {
|
---|
176 | // dmesg("%v: fd %d: %v", origin(1), fd, err)
|
---|
177 | // }
|
---|
178 | t.setErrno(err)
|
---|
179 | return -1
|
---|
180 | }
|
---|
181 |
|
---|
182 | // if dmesgs {
|
---|
183 | // dmesg("%v: %d %#x: ok", origin(1), fd, length)
|
---|
184 | // }
|
---|
185 | return 0
|
---|
186 | }
|
---|
187 |
|
---|
188 | // off64_t lseek64(int fd, off64_t offset, int whence);
|
---|
189 | func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
|
---|
190 | n, _, err := unix.Syscall(unix.SYS_LSEEK, uintptr(fd), uintptr(offset), uintptr(whence))
|
---|
191 | if err != 0 {
|
---|
192 | // if dmesgs {
|
---|
193 | // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
|
---|
194 | // }
|
---|
195 | t.setErrno(err)
|
---|
196 | return -1
|
---|
197 | }
|
---|
198 |
|
---|
199 | // if dmesgs {
|
---|
200 | // dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), n)
|
---|
201 | // }
|
---|
202 | return types.Off_t(n)
|
---|
203 | }
|
---|
204 |
|
---|
205 | // From man utime executed on linux/riscv64:
|
---|
206 | //
|
---|
207 | // The utimbuf structure is:
|
---|
208 | //
|
---|
209 | // struct utimbuf {
|
---|
210 | // time_t actime; /* access time */
|
---|
211 | // time_t modtime; /* modification time */
|
---|
212 | // };
|
---|
213 |
|
---|
214 | type utimbuf struct {
|
---|
215 | actime utime.Time_t
|
---|
216 | modtime utime.Time_t
|
---|
217 | }
|
---|
218 |
|
---|
219 | // int utime(const char *filename, const struct utimbuf *times);
|
---|
220 | func Xutime(t *TLS, filename, times uintptr) int32 {
|
---|
221 | if times == 0 {
|
---|
222 | return Xutimes(t, filename, 0)
|
---|
223 | }
|
---|
224 |
|
---|
225 | n := int(unsafe.Sizeof([2]types.Timeval{}))
|
---|
226 | p := t.Alloc(n)
|
---|
227 | defer t.Free(n)
|
---|
228 | *(*[2]types.Timeval)(unsafe.Pointer(p)) = [2]types.Timeval{
|
---|
229 | {Ftv_sec: (*utimbuf)(unsafe.Pointer(times)).actime},
|
---|
230 | {Ftv_sec: (*utimbuf)(unsafe.Pointer(times)).modtime},
|
---|
231 | }
|
---|
232 | return Xutimes(t, filename, p)
|
---|
233 | }
|
---|
234 |
|
---|
235 | // unsigned int alarm(unsigned int seconds);
|
---|
236 | func Xalarm(t *TLS, seconds uint32) uint32 {
|
---|
237 | panic(todo(""))
|
---|
238 | // No alarm syscall on linux/riscv64. And cannot implement with setitimer as in musl,
|
---|
239 | // because of missing defination to constant ITIMER_REAL in types_linux_riscv64.go.
|
---|
240 | }
|
---|
241 |
|
---|
242 | // time_t time(time_t *tloc);
|
---|
243 | func Xtime(t *TLS, tloc uintptr) types.Time_t {
|
---|
244 | // From golang.org/x/sys/unix/syscall_linux_riscv64.go
|
---|
245 | var tv types.Timeval
|
---|
246 | if err := Xgettimeofday(t, uintptr(unsafe.Pointer(&tv)), 0); err != 0 {
|
---|
247 | t.setErrno(err)
|
---|
248 | return -1
|
---|
249 | }
|
---|
250 |
|
---|
251 | if tloc != 0 {
|
---|
252 | *(*types.Time_t)(unsafe.Pointer(tloc)) = tv.Ftv_sec
|
---|
253 | }
|
---|
254 | return tv.Ftv_sec
|
---|
255 | }
|
---|
256 |
|
---|
257 | // int getrlimit(int resource, struct rlimit *rlim);
|
---|
258 | func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
259 | if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
|
---|
260 | t.setErrno(err)
|
---|
261 | return -1
|
---|
262 | }
|
---|
263 |
|
---|
264 | return 0
|
---|
265 | }
|
---|
266 |
|
---|
267 | // int mkdir(const char *path, mode_t mode);
|
---|
268 | func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
|
---|
269 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
270 | return Xmkdirat(t, unix.AT_FDCWD, path, mode)
|
---|
271 | }
|
---|
272 |
|
---|
273 | // int symlink(const char *target, const char *linkpath);
|
---|
274 | func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
|
---|
275 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
276 | return Xsymlinkat(t, target, unix.AT_FDCWD, linkpath)
|
---|
277 | }
|
---|
278 |
|
---|
279 | // int chmod(const char *pathname, mode_t mode)
|
---|
280 | func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
|
---|
281 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
282 | return Xfchmodat(t, unix.AT_FDCWD, pathname, mode, 0)
|
---|
283 | }
|
---|
284 |
|
---|
285 | // int utimes(const char *filename, const struct timeval times[2]);
|
---|
286 | func Xutimes(t *TLS, filename, times uintptr) int32 {
|
---|
287 | return Xutimensat(t, unix.AT_FDCWD, filename, times, 0)
|
---|
288 | }
|
---|
289 |
|
---|
290 | // int unlink(const char *pathname);
|
---|
291 | func Xunlink(t *TLS, pathname uintptr) int32 {
|
---|
292 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
293 | return Xunlinkat(t, unix.AT_FDCWD, pathname, 0)
|
---|
294 | }
|
---|
295 |
|
---|
296 | // int access(const char *pathname, int mode);
|
---|
297 | func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
|
---|
298 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
299 | return Xfaccessat(t, unix.AT_FDCWD, pathname, mode, 0)
|
---|
300 | }
|
---|
301 |
|
---|
302 | // int rmdir(const char *pathname);
|
---|
303 | func Xrmdir(t *TLS, pathname uintptr) int32 {
|
---|
304 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
305 | return Xunlinkat(t, unix.AT_FDCWD, pathname, unix.AT_REMOVEDIR)
|
---|
306 | }
|
---|
307 |
|
---|
308 | // int rename(const char *oldpath, const char *newpath);
|
---|
309 | func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
|
---|
310 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
311 | return Xrenameat(t, unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath)
|
---|
312 | }
|
---|
313 |
|
---|
314 | // int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
|
---|
315 | func Xrenameat(t *TLS, olddirfd int32, oldpath uintptr, newdirfd int32, newpath uintptr) int32 {
|
---|
316 | // From golang.org/x/sys/unix/syscall_linux_riscv64.go
|
---|
317 | return Xrenameat2(t, olddirfd, oldpath, newdirfd, newpath, 0)
|
---|
318 | }
|
---|
319 |
|
---|
320 | // int mknod(const char *pathname, mode_t mode, dev_t dev);
|
---|
321 | func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
|
---|
322 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
323 | return Xmknodat(t, unix.AT_FDCWD, pathname, mode, dev)
|
---|
324 | }
|
---|
325 |
|
---|
326 | // int chown(const char *pathname, uid_t owner, gid_t group);
|
---|
327 | func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
|
---|
328 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
329 | return Xfchownat(t, unix.AT_FDCWD, pathname, owner, group, 0)
|
---|
330 | }
|
---|
331 |
|
---|
332 | // int link(const char *oldpath, const char *newpath);
|
---|
333 | func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
|
---|
334 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
335 | return Xlinkat(t, unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath, 0)
|
---|
336 | }
|
---|
337 |
|
---|
338 | // int pipe(int pipefd[2]);
|
---|
339 | func Xpipe(t *TLS, pipefd uintptr) int32 {
|
---|
340 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
341 | return Xpipe2(t, pipefd, 0)
|
---|
342 | }
|
---|
343 |
|
---|
344 | // int dup2(int oldfd, int newfd);
|
---|
345 | func Xdup2(t *TLS, oldfd, newfd int32) int32 {
|
---|
346 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
347 | return Xdup3(t, oldfd, newfd, 0)
|
---|
348 | }
|
---|
349 |
|
---|
350 | // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
|
---|
351 | func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
|
---|
352 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
353 | return Xreadlinkat(t, unix.AT_FDCWD, path, buf, bufsize)
|
---|
354 | }
|
---|
355 |
|
---|
356 | // FILE *fopen64(const char *pathname, const char *mode);
|
---|
357 | func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
|
---|
358 | m := strings.ReplaceAll(GoString(mode), "b", "")
|
---|
359 | var flags int
|
---|
360 | switch m {
|
---|
361 | case "r":
|
---|
362 | flags = os.O_RDONLY
|
---|
363 | case "r+":
|
---|
364 | flags = os.O_RDWR
|
---|
365 | case "w":
|
---|
366 | flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
|
---|
367 | case "w+":
|
---|
368 | flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
|
---|
369 | case "a":
|
---|
370 | flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
|
---|
371 | case "a+":
|
---|
372 | flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
|
---|
373 | default:
|
---|
374 | panic(m)
|
---|
375 | }
|
---|
376 | //TODO- flags |= fcntl.O_LARGEFILE
|
---|
377 |
|
---|
378 | // From golang.org/x/sys/unix/syscall_linux.go
|
---|
379 | fd := Xopenat(t, unix.AT_FDCWD, pathname, int32(flags|unix.O_LARGEFILE), 0666)
|
---|
380 | if fd == -1 {
|
---|
381 | return 0
|
---|
382 | }
|
---|
383 |
|
---|
384 | if p := newFile(t, fd); p != 0 {
|
---|
385 | return p
|
---|
386 | }
|
---|
387 |
|
---|
388 | Xclose(t, fd)
|
---|
389 | t.setErrno(errno.ENOMEM)
|
---|
390 | return 0
|
---|
391 | }
|
---|
392 |
|
---|
393 | // int iswspace(wint_t wc);
|
---|
394 | func Xiswspace(t *TLS, wc wctype.Wint_t) int32 {
|
---|
395 | return Bool32(unicode.IsSpace(rune(wc)))
|
---|
396 | }
|
---|
397 |
|
---|
398 | // int iswalnum(wint_t wc);
|
---|
399 | func Xiswalnum(t *TLS, wc wctype.Wint_t) int32 {
|
---|
400 | return Bool32(unicode.IsLetter(rune(wc)) || unicode.IsNumber(rune(wc)))
|
---|
401 | }
|
---|
402 |
|
---|
403 | func __syscall1(t *TLS, trap, p1 long) long {
|
---|
404 | return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), 0, 0))
|
---|
405 | }
|
---|
406 |
|
---|
407 | func __syscall3(t *TLS, trap, p1, p2, p3 long) long {
|
---|
408 | return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3)))
|
---|
409 | }
|
---|
410 |
|
---|
411 | func __syscall4(t *TLS, trap, p1, p2, p3, p4 long) long {
|
---|
412 | return __syscall(unix.Syscall6(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3), uintptr(p4), 0, 0))
|
---|
413 | }
|
---|