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