Debian Codegraph @

rt/+dragonfly/syscalls.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 export fn syscall(num: u64, args: u64...) u64 = {
5 switch (len(args)) {
6 case 0 => return syscall0(num);
7 case 1 => return syscall1(num, args[0]);
8 case 2 => return syscall2(num, args[0], args[1]);
9 case 3 => return syscall3(num, args[0], args[1], args[2]);
10 case 4 => return syscall4(num, args[0], args[1], args[2], args[3]);
11 case 5 => return syscall5(num, args[0], args[1], args[2], args[3], args[4]);
12 case 6 => return syscall6(num, args[0], args[1], args[2], args[3], args[4], args[5]);
13 case => abort("syscalls can't have more than 6 arguments");
14 };
15 };
16
17 fn syscall0(_: u64) u64;
18 fn syscall1(_: u64, _: u64) u64;
19 fn syscall2(_: u64, _: u64, _: u64) u64;
20 fn syscall3(_: u64, _: u64, _: u64, _: u64) u64;
21 fn syscall4(_: u64, _: u64, _: u64, _: u64, _: u64) u64;
22 fn syscall5(_: u64, _: u64, _: u64, _: u64, _: u64, _: u64) u64;
23 fn syscall6(_: u64, _: u64, _: u64, _: u64, _: u64, _: u64, _: u64) u64;
24 fn _mmap(_: u64, _: u64, _: u64, _: u64, _: u64, _: u64) u64;
25 fn _pipe2(_: u64, _: u64) u64;
26
27 export def NAME_MAX: size = 255z; // max bytes in a file name
28 export def PATH_MAX: size = 1024z; // max bytes in pathname
29 export type path = (str | []u8 | *const u8);
30 let pathbuf: [PATH_MAX]u8 = [0...];
31
32 fn copy_kpath(path: path, buf: []u8) (*const u8 | errno) = {
33 let path = match (path) {
34 case let c: *const u8 =>
35 return c;
36 case let s: str =>
37 let ptr = &s: *struct {
38 buf: *[*]u8,
39 length: size,
40 capacity: size,
41 };
42 yield ptr.buf[..ptr.length];
43 case let b: []u8 =>
44 yield b;
45 };
46 if (len(path) + 1 >= len(pathbuf)) {
47 return ENAMETOOLONG;
48 };
49 memcpy(buf: *[*]u8, path: *[*]u8, len(path));
50 buf[len(path)] = 0;
51 return buf: *[*]u8: *const u8;
52 };
53
54 // NUL terminates a string and stores it in a static buffer of PATH_MAX bytes in
55 // length.
56 fn kpath(path: path) (*const u8 | errno) = {
57 return copy_kpath(path, pathbuf);
58 };
59
60 export fn read(fd: int, buf: *opaque, count: size) (size | errno) = {
61 return wrap_return(syscall3(SYS_read,
62 fd: u64, buf: uintptr: u64, count: u64))?: size;
63 };
64
65 export fn write(fd: int, buf: *const opaque, count: size) (size | errno) = {
66 return wrap_return(syscall3(SYS_write,
67 fd: u64, buf: uintptr: u64, count: u64))?: size;
68 };
69
70 export fn readv(fd: int, iov: const *[*]iovec, iovcnt: int) (size | errno) = {
71 return wrap_return(syscall3(SYS_readv,
72 fd: u64, iov: uintptr: u64, iovcnt: u64))?: size;
73 };
74
75 export fn writev(fd: int, iov: const *[*]iovec, iovcnt: int) (size | errno) = {
76 return wrap_return(syscall3(SYS_writev,
77 fd: u64, iov: uintptr: u64, iovcnt: u64))?: size;
78 };
79
80 export fn close(fd: int) (void | errno) = {
81 wrap_return(syscall1(SYS_close, fd: u64))?;
82 };
83
84 export fn lseek(fd: int, off: i64, whence: int) (i64 | errno) = {
85 return wrap_return(syscall4(SYS_lseek,
86 fd: u64, 0: u64, off: u64, whence: u64))?: i64;
87 };
88
89 export fn ftruncate(fd: int, ln: off_t) (void | errno) = {
90 wrap_return(syscall3(SYS_ftruncate, fd: u64, 0: u64, ln: u64))?;
91 };
92
93 export fn truncate(path: path, ln: off_t) (void | errno) = {
94 let path = kpath(path)?;
95 wrap_return(syscall3(SYS_truncate, path: uintptr: u64,
96 0: u64, ln: u64))?;
97 };
98
99
100 export fn pipe2(pipefd: *[2]int, flags: int) (void | errno) = {
101 wrap_return(_pipe2(pipefd: uintptr: u64, flags: u64))?;
102 };
103
104 export type ioctl_arg = (nullable *opaque | u64);
105
106 export fn ioctl(fd: int, req: u64, arg: ioctl_arg) (int | errno) = {
107 let fd = fd: u64, req = req: u64;
108 return wrap_return(match (arg) {
109 case let u: u64 =>
110 yield syscall3(SYS_ioctl, fd, req, u);
111 case let v: nullable *opaque =>
112 yield syscall3(SYS_ioctl, fd, req, v: uintptr: u64);
113 })?: int;
114 };
115
116 export fn openat(
117 dirfd: int,
118 path: path,
119 flags: int,
120 mode: uint,
121 ) (int | errno) = {
122 let path = kpath(path)?;
123 return wrap_return(syscall4(SYS_openat, dirfd: u64,
124 path: uintptr: u64, flags: u64, mode: u64))?: int;
125 };
126
127 export fn open(path: str, flags: int, mode: uint) (int | errno) = {
128 return openat(AT_FDCWD, path, flags, mode);
129 };
130
131 export fn unlink(path: path) (void | errno) = {
132 let path = kpath(path)?;
133 wrap_return(syscall3(SYS_unlinkat,
134 AT_FDCWD: u64, path: uintptr: u64, 0u64))?;
135 };
136
137 export fn renameat(
138 olddirfd: int,
139 oldpath: str,
140 newdirfd: int,
141 newpath: str,
142 ) (void | errno) = {
143 let oldpath = kpath(oldpath)?;
144 static let newpathbuf: [PATH_MAX]u8 = [0...];
145 let newpath = copy_kpath(newpath, newpathbuf)?;
146 wrap_return(syscall4(SYS_renameat,
147 olddirfd: u64, oldpath: uintptr: u64,
148 newdirfd: u64, newpath: uintptr: u64))?;
149 };
150
151 export fn unlinkat(dirfd: int, path: path, flags: int) (void | errno) = {
152 let path = kpath(path)?;
153 wrap_return(syscall3(SYS_unlinkat,
154 dirfd: u64, path: uintptr: u64, flags: u64))?;
155 };
156
157 fn convert_stat(dfstat: *dragonfly_stat, stat: *st) void = {
158 stat.dev = dfstat.st_dev;
159 stat.ino = dfstat.st_ino;
160 stat.mode = dfstat.st_mode;
161 stat.nlink = dfstat.st_nlink;
162 stat.uid = dfstat.st_uid;
163 stat.gid = dfstat.st_gid;
164 stat.rdev = dfstat.st_rdev;
165 stat.atime.tv_sec = dfstat.st_atim.tv_sec;
166 stat.atime.tv_nsec = dfstat.st_atim.tv_nsec;
167 stat.mtime.tv_sec = dfstat.st_mtim.tv_sec;
168 stat.mtime.tv_nsec = dfstat.st_mtim.tv_nsec;
169 stat.ctime.tv_sec = dfstat.st_ctim.tv_sec;
170 stat.ctime.tv_nsec = dfstat.st_ctim.tv_nsec;
171 stat.sz = dfstat.st_size;
172 stat.blocks = dfstat.st_blocks;
173 stat.blksz = dfstat.st_blksize;
174 stat.flags = dfstat.st_flags;
175 };
176
177 export fn fstatat(fd: int, path: path, stat: *st, flag: int) (void | errno) = {
178 let path = kpath(path)?;
179 let dfstat = dragonfly_stat { ... };
180 wrap_return(syscall4(SYS_fstatat, fd: u64,
181 path: uintptr: u64, &dfstat: uintptr: u64, flag: u64))?;
182 convert_stat(&dfstat, stat);
183 };
184
185 export fn fstat(fd: int, stat: *st) (errno | void) = {
186 let dfstat = dragonfly_stat { ... };
187 wrap_return(syscall2(SYS_fstat, fd: u64,
188 &dfstat: uintptr: u64))?;
189 convert_stat(&dfstat, stat);
190 };
191
192 export fn readlinkat(
193 dirfd: int,
194 path: path,
195 buf: []u8,
196 ) (size | errno) = {
197 let path = kpath(path)?;
198 return wrap_return(syscall4(SYS_readlinkat,
199 dirfd: u64, path: uintptr: u64,
200 buf: *[*]u8: uintptr: u64,
201 len(buf): u64))?: size;
202 };
203
204 export fn mkdirat(dirfd: int, path: path, mode: uint) (void | errno) = {
205 let path = kpath(path)?;
206 wrap_return(syscall3(SYS_mkdirat,
207 dirfd: u64, path: uintptr: u64, mode: u64))?;
208 };
209
210
211 export fn fchmod(fd: int, mode: uint) (void | errno) = {
212 wrap_return(syscall2(SYS_fchmod,
213 fd: u64, mode: u64))?;
214 };
215
216 export fn fchmodat(dirfd: int, path: path, mode: uint, flags: int) (void | errno) = {
217 let path = kpath(path)?;
218 wrap_return(syscall4(SYS_fchmodat,
219 dirfd: u64, path: uintptr: u64, mode: u64, flags: u64))?;
220 };
221
222 export fn fchown(fd: int, uid: uint, gid: uint) (void | errno) = {
223 wrap_return(syscall3(SYS_fchown,
224 fd: u64, uid: u32, gid: u32))?;
225 };
226
227 export fn fchownat(dirfd: int, path: path, uid: uint, gid: uint, flags: int) (void | errno) = {
228 let path = kpath(path)?;
229 wrap_return(syscall5(SYS_fchownat,
230 dirfd: u64, path: uintptr: u64, uid: u32, gid: u32, flags: u64))?;
231 };
232
233 export fn utimensat(dirfd: int, path: str, ts: *[2]timespec, flags: int) (void | errno) = {
234 let path = kpath(path)?;
235 wrap_return(syscall4(SYS_utimensat,
236 dirfd: u64, path: uintptr: u64, ts: uintptr: u64, flags: u64))?;
237 };
238
239 export fn futimens(fd: int, ts: *[2]timespec) (void | errno) = {
240 wrap_return(syscall2(SYS_futimens,
241 fd: u64, ts: uintptr: u64))?;
242 };
243
244 export fn faccessat(
245 dirfd: int,
246 path: path,
247 mode: int,
248 flags: int,
249 ) (bool | errno) = {
250 let path = kpath(path)?;
251 match (wrap_return(syscall4(SYS_faccessat, dirfd: u64,
252 path: uintptr: u64, mode: u64, flags: u64))) {
253 case let err: errno =>
254 switch (err) {
255 case EACCES =>
256 return false;
257 case =>
258 return err;
259 };
260 case let n: u64 =>
261 assert(n == 0);
262 return true;
263 };
264 };
265
266 // The use of this function is discouraged, as it can create race conditions.
267 // TOCTOU is preferred: attempt to simply use the resource you need and handle
268 // any access errors which occur.
269 export fn access(path: path, mode: int) (bool | errno) =
270 faccessat(AT_FDCWD, path, mode, 0);
271
272 export fn getdents(dirfd: int, buf: *opaque, nbytes: size) (size | errno) = {
273 return wrap_return(syscall3(SYS_getdents, dirfd: u64,
274 buf: uintptr: u64, nbytes: u64))?: size;
275 };
276
277 // The return value is statically allocated and must be duplicated before
278 // calling getcwd again.
279 export fn getcwd() (*const u8 | errno) = {
280 static let pathbuf: [PATH_MAX]u8 = [0...];
281 wrap_return(syscall2(SYS___getcwd,
282 &pathbuf: *[*]u8: uintptr: u64,
283 PATH_MAX))?;
284 return &pathbuf: *const u8;
285 };
286
287 export fn fchdir(fd: int) (void | errno) = {
288 wrap_return(syscall1(SYS_fchdir, fd: u64))?;
289 };
290
291 export fn chdir(path: path) (void | errno) = {
292 let path = kpath(path)?;
293 wrap_return(syscall1(SYS_chdir, path: uintptr: u64))?;
294 };
295
296 export fn chroot(path: path) (void | errno) = {
297 let path = kpath(path)?;
298 wrap_return(syscall1(SYS_chroot, path: uintptr: u64))?;
299 };
300
301 export fn mmap(
302 addr: nullable *opaque,
303 length: size,
304 prot: uint,
305 flags: uint,
306 fd: int,
307 offs: size
308 ) (errno | *opaque) = {
309 return wrap_return(_mmap(addr: uintptr: u64, length: u64, prot: u64,
310 flags: u64, fd: u64, offs: u64))?: uintptr: *opaque;
311 };
312
313 export fn munmap(addr: *opaque, length: size) (void | errno) = {
314 wrap_return(syscall2(SYS_munmap, addr: uintptr: u64, length: u64))?;
315 };
316
317 export fn exit(status: int) never = {
318 syscall1(SYS_exit, status: u64);
319 abort();
320 };
321
322 export fn kill(pid: pid_t, signal: int) (void | errno) = {
323 wrap_return(syscall2(SYS_kill, pid: u64, signal: u64))?;
324 };
325
326 export fn fork() (pid_t | void | errno) = {
327 let n = wrap_return(syscall0(SYS_fork))?: pid_t;
328 switch (n) {
329 case 0 =>
330 return;
331 case =>
332 return n;
333 };
334 };
335
336 export fn fexecve(fd: int, argv: *[*]nullable *const u8,
337 envp: *[*]nullable *const u8) errno = {
338 return match (wrap_return(syscall3(SYS_fexecve, fd: u64,
339 argv: uintptr: u64, envp: uintptr: u64))) {
340 case let err: errno =>
341 yield err;
342 case u64 =>
343 abort("unreachable");
344 };
345 };
346
347 export fn wait4(
348 pid: pid_t,
349 wstatus: nullable *int,
350 options: int,
351 rusage: nullable *rusage,
352 ) (int | errno) = {
353 return wrap_return(syscall4(SYS_wait4,
354 pid: u64, wstatus: uintptr: u64,
355 options: u64, rusage: uintptr: u64))?: int;
356 };
357
358 export fn wifexited(status: int) bool = wtermsig(status) == 0;
359 export fn wexitstatus(status: int) int = (status & 0xff00) >> 8;
360
361 export fn wtermsig(status: int) int = status & 0o177;
362 export fn wifsignaled(status: int) bool =
363 wtermsig(status) != 0o177 && wtermsig(status) != 0 && status != 0x13;
364
365 export fn getpid() pid_t = syscall0(SYS_getpid): pid_t;
366
367 export fn getppid() pid_t = syscall0(SYS_getppid): pid_t;
368
369 export fn getpgrp() pid_t = syscall0(SYS_getpgrp): pid_t;
370
371 export fn getpgid(pid: pid_t) (pid_t | errno) = {
372 return wrap_return(syscall1(SYS_getpgid, pid: u64))?: pid_t;
373 };
374
375 export fn getsid(pid: pid_t) (pid_t | errno) = {
376 return wrap_return(syscall1(SYS_getsid, pid: u64))?: pid_t;
377 };
378
379 export fn getpriority(which: int, who: id_t) (int | errno) = {
380 return wrap_return(syscall2(SYS_getpriority,
381 which: u64, who: u64))?: int;
382 };
383
384 export fn setpriority(which: int, who: id_t, prio: int) (void | errno) = {
385 wrap_return(syscall3(SYS_setpriority, which: u64, who: u64, prio: u64))?;
386 };
387
388 export fn umask(mode: mode_t) (mode_t | errno) = {
389 return wrap_return(syscall1(SYS_umask, mode: u64))?: mode_t;
390 };
391
392 export fn setresuid(uid: uid_t, euid: uid_t, suid: uid_t) (void | errno) = {
393 wrap_return(syscall3(SYS_setresuid, uid: u64, euid: u64, suid: u64))?;
394 };
395
396 export fn setresgid(gid: gid_t, egid: gid_t, sgid: gid_t) (void | errno) = {
397 wrap_return(syscall3(SYS_setresgid, gid: u64, egid: u64, sgid: u64))?;
398 };
399
400 export fn getgroups(gids: []gid_t) (uint | errno) = {
401 return wrap_return(syscall2(SYS_getgroups,
402 len(gids): u64, gids: *[*]gid_t: uintptr: u64))?: uint;
403 };
404
405 export fn setgroups(gids: []gid_t) (void | errno) = {
406 wrap_return(syscall2(SYS_setgroups,
407 len(gids): u64, gids: *[*]gid_t: uintptr: u64))?;
408 };
409
410 export fn getresuid(uid: *uid_t, euid: *uid_t, suid: *uid_t) (void | errno) = {
411 wrap_return(syscall3(SYS_getresuid,
412 uid: uintptr: u64,
413 euid: uintptr: u64,
414 suid: uintptr: u64))?;
415 };
416
417 export fn getresgid(gid: *gid_t, egid: *gid_t, sgid: *gid_t) (void | errno) = {
418 wrap_return(syscall3(SYS_getresgid,
419 gid: uintptr: u64,
420 egid: uintptr: u64,
421 sgid: uintptr: u64))?;
422 };
423
424 export fn clock_gettime(clock_id: int, tp: *timespec) (void | errno) = {
425 wrap_return(syscall2(SYS_clock_gettime,
426 clock_id: u64, tp: uintptr: u64))?;
427 };
428
429 export fn clock_settime(clock_id: int, tp: *const timespec) (void | errno) = {
430 wrap_return(syscall2(SYS_clock_settime,
431 clock_id: u64, tp: uintptr: u64))?;
432 };
433
434 export fn nanosleep(req: *const timespec, rem: *timespec) (void | errno) = {
435 wrap_return(syscall2(SYS_nanosleep,
436 req: uintptr: u64, rem: uintptr: u64))?;
437 };
438
439 export fn clock_nanosleep(
440 clock_id: int,
441 flags: int,
442 requested: *timespec,
443 remaining: *timespec
444 ) (void | errno) = {
445 wrap_return(syscall4(SYS_clock_nanosleep,
446 clock_id: u64,
447 flags: u64,
448 requested: uintptr: u64,
449 remaining: uintptr: u64
450 ))?;
451 };
452
453 export fn getrandom(buf: *opaque, bufln: size, flags: uint) (size | errno) = {
454 return wrap_return(syscall3(SYS_getrandom,
455 buf: uintptr: u64, bufln: u64, flags: u64))?: size;
456 };
457
458 export type fcntl_arg = (void | int | *st_flock | *u64);
459
460 export fn fcntl(fd: int, cmd: int, arg: fcntl_arg) (int | errno) = {
461 let _fd = fd: u64, _cmd = cmd: u64;
462 return wrap_return(match (arg) {
463 case void =>
464 yield syscall2(SYS_fcntl, _fd, _cmd);
465 case let i: int =>
466 yield syscall3(SYS_fcntl, _fd, _cmd, i: u64);
467 case let l: *st_flock =>
468 yield syscall3(SYS_fcntl, _fd, _cmd, l: uintptr: u64);
469 case let u: *u64 =>
470 yield syscall3(SYS_fcntl, _fd, _cmd, u: uintptr: u64);
471 })?: int;
472 };
473
474 export fn ppoll(
475 fds: *[*]pollfd,
476 nfds: nfds_t,
477 timeout: const nullable *timespec,
478 sigmask: const nullable *sigset,
479 ) (int | errno) = {
480 return wrap_return(syscall4(SYS_ppoll, fds: uintptr: u64, nfds: u64,
481 timeout: uintptr: u64, sigmask: uintptr: u64))?: int;
482 };
483
484 export fn poll(fds: *[*]pollfd, nfds: nfds_t, timeout: int) (int | errno) = {
485 const ts = timespec {
486 tv_sec = timeout % 1000,
487 tv_nsec = timeout * 1000000,
488 };
489 return ppoll(fds, nfds, (if (timeout != -1) &ts else null), null);
490 };
491
492 export fn socket(domain: int, type_: int, protocol: int) (int | errno) = {
493 return wrap_return(syscall3(SYS_socket,
494 domain: u64, type_: u64, protocol: u64))?: int;
495 };
496
497 export fn socketpair(
498 domain: int,
499 type_: int,
500 protocol: int,
501 sv: *[*]int,
502 ) (int | errno) = {
503 return wrap_return(syscall4(SYS_socketpair, domain: u64,
504 type_: u64, protocol: u64, sv: uintptr: u64))?: int;
505 };
506
507 export fn connect(sockfd: int, addr: *const sockaddr, addrlen: u32) (void | errno) = {
508 wrap_return(syscall3(SYS_connect, sockfd: u64,
509 addr: uintptr: u64, addrlen: u64))?;
510 };
511
512 export fn bind(sockfd: int, addr: *const sockaddr, addrlen: u32) (void | errno) = {
513 wrap_return(syscall3(SYS_bind, sockfd: u64,
514 addr: uintptr: u64, addrlen: u64))?;
515 };
516
517 export fn listen(sockfd: int, backlog: u32) (int | errno) = {
518 return wrap_return(syscall2(SYS_listen,
519 sockfd: u64, backlog: u64))?: int;
520 };
521
522 export fn accept(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = {
523 return wrap_return(syscall3(SYS_accept,
524 sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int;
525 };
526
527 export fn accept4(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32, flags: int) (int | errno) = {
528 return wrap_return(syscall4(SYS_accept4,
529 sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64, flags: u64))?: int;
530 };
531
532 export fn recvfrom(sockfd: int, buf: *opaque, len_: size, flags: int,
533 src_addr: nullable *sockaddr, addrlen: nullable *u32
534 ) (size | errno) = {
535 return wrap_return(syscall6(SYS_recvfrom,
536 sockfd: u64, buf: uintptr: u64, len_: u64, flags: u64,
537 src_addr: uintptr: u64, addrlen: uintptr: u64))?: size;
538 };
539
540 export fn sendto(sockfd: int, buf: *opaque, len_: size, flags: int,
541 dest_addr: nullable *sockaddr, addrlen: u32
542 ) (size | errno) = {
543 return wrap_return(syscall6(SYS_sendto,
544 sockfd: u64, buf: uintptr: u64, len_: u64, flags: u64,
545 dest_addr: uintptr: u64, addrlen: u64))?: size;
546 };
547
548 export fn recv(sockfd: int, buf: *opaque, len_: size, flags: int) (size | errno) = {
549 return recvfrom(sockfd, buf, len_, flags, null, null);
550 };
551
552 export fn send(sockfd: int, buf: *opaque, len_: size, flags: int) (size | errno) = {
553 return sendto(sockfd, buf, len_, flags, null, 0);
554 };
555
556 export fn sendmsg(fd: int, msg: *const msghdr, flags: int) (int | errno) = {
557 return wrap_return(syscall3(SYS_sendmsg,
558 fd: u64, msg: uintptr: u64, flags: u64))?: int;
559 };
560
561 export fn recvmsg(fd: int, msg: *const msghdr, flags: int) (int | errno) = {
562 return wrap_return(syscall3(SYS_recvmsg,
563 fd: u64, msg: uintptr: u64, flags: u64))?: int;
564 };
565
566 export fn getsockopt(sockfd: int, level: int, optname: int, optval: nullable *opaque, optlen: nullable *u32) (int | errno) = {
567 return wrap_return(syscall5(SYS_getsockopt,
568 sockfd: u64, level: u64, optname: u64,
569 optval: uintptr: u64, optlen: uintptr: u64))?: int;
570 };
571
572 export fn setsockopt(sockfd: int, level: int, optname: int, optval: *opaque, optlen: u32) (int | errno) = {
573 return wrap_return(syscall5(SYS_setsockopt,
574 sockfd: u64, level: u64, optname: u64,
575 optval: uintptr: u64, optlen: u64))?: int;
576 };
577
578 export fn getsockname(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = {
579 return wrap_return(syscall3(SYS_getsockname,
580 sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int;
581 };
582
583 export fn getpeername(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = {
584 return wrap_return(syscall3(SYS_getpeername,
585 sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int;
586 };
587
588 export fn sysctl(name: []const int, oldp: nullable *opaque,
589 oldlenp: nullable *size,
590 newp: nullable *const opaque,
591 newlen: size) (void | errno) = {
592 wrap_return(syscall6(SYS___sysctl,
593 &name[0]: uintptr: u64, len(name): u64,
594 oldp: uintptr: u64, oldlenp: uintptr: u64,
595 newp: uintptr: u64, newlen: u64))?;
596 };
597
598 export fn sysctlbyname(name: str, oldp: nullable *opaque,
599 oldlenp: nullable *size,
600 newp: nullable *const opaque,
601 newlen: size) (void | errno) = {
602 let real_oid: [CTL_MAXNAME+2]int = [0...];
603 let oidlen: size = len(real_oid);
604 sysctlnametomib(name, &real_oid[0], &oidlen)?;
605
606 sysctl(real_oid[0..oidlen], oldp, oldlenp, newp, newlen)?;
607 };
608
609 // [[sizep]] contains the number of elements in [[mibp]]. We have to convert
610 // it to number of bytes.
611 fn sysctlnametomib(name: str, mibp: *int, sizep: *size) (void | errno) = {
612 const oid: [2]int = [CTL_SYSCTL, CTL_SYSCTL_NAME2OID];
613 let kname = kpath(name)?;
614 *sizep *= size(int);
615 sysctl(oid[0..], mibp: *opaque, sizep, kname, len(name))?;
616 *sizep /= size(int);
617 };
618
619 export fn dup2(oldfd: int, newfd: int) (int | errno) = {
620 return wrap_return(syscall2(SYS_dup2, oldfd: u64, newfd: u64))?: int;
621 };
622
623 export fn posix_fallocate(fd: int, off: i64, ln: i64) (void | errno) = {
624 wrap_return(syscall3(SYS_posix_fallocate,
625 fd: u64, off: u64, ln: u64))?;
626 };
627
628 export fn flock(fd: int, op: int) (void | errno) = {
629 wrap_return(syscall2(SYS_flock,
630 fd: u64, op: u64))?;
631 };
632
633 export fn shmat(id: int, addr: *const opaque, flag: int) *opaque = {
634 return syscall3(SYS_shmat, id: u64, addr: uintptr: u64,
635 flag: u64): uintptr: *opaque;
636 };
637
638 export fn getrlimit(resource: int, rlim: *rlimit) (void | errno) = {
639 wrap_return(syscall2(SYS_getrlimit,
640 resource: u64, rlim: uintptr: u64))?;
641 };
642
643 export fn setrlimit(resource: int, rlim: *const rlimit) (void | errno) = {
644 wrap_return(syscall2(SYS_setrlimit,
645 resource: u64, rlim: uintptr: u64))?;
646 };
647
648 export fn sigprocmask(
649 how: int,
650 set: nullable *const sigset,
651 old: nullable *sigset,
652 ) (int | errno) = {
653 return wrap_return(syscall3(SYS_sigprocmask,
654 how: u64, set: uintptr: u64, old: uintptr: u64))?: int;
655 };
656
657 export fn sigaction(
658 signum: int,
659 act: *const sigact,
660 old: nullable *sigact,
661 ) (int | errno) = {
662 return wrap_return(syscall3(SYS_sigaction,
663 signum: u64, act: uintptr: u64, old: uintptr: u64))?: int;
664 };
665
666 export fn sigaltstack(
667 ss: nullable *stack_t,
668 old_ss: nullable *stack_t,
669 ) (void | errno) = {
670 wrap_return(syscall2(SYS_sigaltstack,
671 ss: uintptr: u64, old_ss: uintptr: u64))?;
672 };
673
674 export fn shutdown(sockfd: int, how: int) (void | errno) = {
675 wrap_return(syscall2(SYS_shutdown,
676 sockfd: u64, how: u64))?;
677 };
678
679 export fn fsync(fd: int) (void | errno) = {
680 wrap_return(syscall1(SYS_fsync, fd: u64))?;
681 };
682
683 export fn fdatasync(fd: int) (void | errno) = {
684 wrap_return(syscall1(SYS_fdatasync, fd: u64))?;
685 };
686
687 // Scheduling
688 export fn sched_getscheduler(pid: pid_t) (int | errno) = {
689 return wrap_return(syscall1(SYS_sched_getscheduler,
690 pid: u64))?: int;
691 };
692
693 export fn sched_setscheduler(pid: pid_t, policy: int, params: *sched_param) (int | errno) = {
694 return wrap_return(syscall3(SYS_sched_setscheduler,
695 pid: u64, policy: u64, params: uintptr: u64))?: int;
696 };
697
698 export fn sched_yield() void = {
699 syscall0(SYS_sched_yield);
700 };
701
702 export fn kqueue() (int | errno) = {
703 return wrap_return(syscall0(SYS_kqueue))?: int;
704 };
705
706 export fn kevent(kq: int, changelist: const *kevent_t, nchanges: int, eventlist: *kevent_t, nevents: int, timeout: const nullable *timespec) (int | errno) = {
707 return wrap_return(syscall6(SYS_kevent, kq: u64, changelist: uintptr, nchanges: u64, eventlist: uintptr, nevents: u64, timeout: uintptr))?: int;
708 };