| 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 PATH_MAX: size = 4096z; |
| 26 |
export type path = (str | []u8 | *const u8); |
| 27 |
let pathbuf: [PATH_MAX]u8 = [0...]; |
| 28 |
|
| 29 |
fn copy_kpath(path: path, buf: []u8) (*const u8 | errno) = { |
| 30 |
let path = match (path) { |
| 31 |
case let c: *const u8 => |
| 32 |
return c; |
| 33 |
case let s: str => |
| 34 |
let ptr = &s: *struct { |
| 35 |
buf: *[*]u8, |
| 36 |
length: size, |
| 37 |
capacity: size, |
| 38 |
}; |
| 39 |
yield ptr.buf[..ptr.length]; |
| 40 |
case let b: []u8 => |
| 41 |
yield b; |
| 42 |
}; |
| 43 |
if (len(path) + 1 >= len(pathbuf)) { |
| 44 |
return ENAMETOOLONG; |
| 45 |
}; |
| 46 |
memcpy(buf: *[*]u8, path: *[*]u8, len(path)); |
| 47 |
buf[len(path)] = 0; |
| 48 |
return buf: *[*]u8: *const u8; |
| 49 |
}; |
| 50 |
|
| 51 |
// NUL terminates a string and stores it in a static buffer of PATH_MAX bytes in |
| 52 |
// length. |
| 53 |
fn kpath(path: path) (*const u8 | errno) = { |
| 54 |
return copy_kpath(path, pathbuf); |
| 55 |
}; |
| 56 |
|
| 57 |
export fn read(fd: int, buf: *opaque, count: size) (size | errno) = { |
| 58 |
return wrap_return(syscall3(SYS_read, |
| 59 |
fd: u64, buf: uintptr: u64, count: u64))?: size; |
| 60 |
}; |
| 61 |
|
| 62 |
export fn write(fd: int, buf: *const opaque, count: size) (size | errno) = { |
| 63 |
return wrap_return(syscall3(SYS_write, |
| 64 |
fd: u64, buf: uintptr: u64, count: u64))?: size; |
| 65 |
}; |
| 66 |
|
| 67 |
export fn open(path: path, flags: int, mode: uint) (int | errno) = { |
| 68 |
let path = kpath(path)?; |
| 69 |
return wrap_return(syscall4(SYS_openat, AT_FDCWD: u64, |
| 70 |
path: uintptr: u64, flags: u64, mode: u64))?: int; |
| 71 |
}; |
| 72 |
|
| 73 |
fn openat( |
| 74 |
dirfd: int, |
| 75 |
path: path, |
| 76 |
flags: int, |
| 77 |
mode: uint, |
| 78 |
) (int | errno) = { |
| 79 |
let path = kpath(path)?; |
| 80 |
return wrap_return(syscall4(SYS_openat, dirfd: u64, |
| 81 |
path: uintptr: u64, flags: u64, mode: u64))?: int; |
| 82 |
}; |
| 83 |
|
| 84 |
export fn openat2( |
| 85 |
dirfd: int, |
| 86 |
path: path, |
| 87 |
how: *open_how, |
| 88 |
how_sz: size, |
| 89 |
) (int | errno) = { |
| 90 |
let path = kpath(path)?; |
| 91 |
match (wrap_return(syscall4(SYS_openat2, |
| 92 |
dirfd: u64, |
| 93 |
path: uintptr: u64, |
| 94 |
how: uintptr: u64, |
| 95 |
how_sz: u64))) { |
| 96 |
case let i: u64 => |
| 97 |
return i: int; |
| 98 |
case let err: errno => |
| 99 |
if (err != ENOSYS) { |
| 100 |
return err; |
| 101 |
}; |
| 102 |
}; |
| 103 |
|
| 104 |
// Fallback if the requested behavior is compatible with openat(2) |
| 105 |
if (how.resolve != 0) { |
| 106 |
return ENOSYS; |
| 107 |
}; |
| 108 |
return openat(dirfd, path, how.flags: int, how.mode: uint); |
| 109 |
}; |
| 110 |
|
| 111 |
export fn readlinkat( |
| 112 |
dirfd: int, |
| 113 |
path: path, |
| 114 |
buf: []u8, |
| 115 |
) (size | errno) = { |
| 116 |
let path = kpath(path)?; |
| 117 |
return wrap_return(syscall4(SYS_readlinkat, |
| 118 |
dirfd: u64, path: uintptr: u64, |
| 119 |
buf: *[*]u8: uintptr: u64, |
| 120 |
len(buf): u64))?: size; |
| 121 |
}; |
| 122 |
|
| 123 |
export fn unlink(path: path) (void | errno) = { |
| 124 |
let path = kpath(path)?; |
| 125 |
wrap_return(syscall3(SYS_unlinkat, |
| 126 |
AT_FDCWD: u64, path: uintptr: u64, 0u64))?; |
| 127 |
}; |
| 128 |
|
| 129 |
export fn unlinkat(dirfd: int, path: path, flags: int) (void | errno) = { |
| 130 |
let path = kpath(path)?; |
| 131 |
wrap_return(syscall3(SYS_unlinkat, |
| 132 |
dirfd: u64, path: uintptr: u64, flags: u64))?; |
| 133 |
}; |
| 134 |
|
| 135 |
export fn linkat( |
| 136 |
olddirfd: int, |
| 137 |
oldpath: path, |
| 138 |
newdirfd: int, |
| 139 |
newpath: path, |
| 140 |
flags: int, |
| 141 |
) (void | errno) = { |
| 142 |
let oldpath = kpath(oldpath)?; |
| 143 |
static let newpathbuf: [PATH_MAX]u8 = [0...]; |
| 144 |
let newpath = copy_kpath(newpath, newpathbuf)?; |
| 145 |
wrap_return(syscall5(SYS_linkat, |
| 146 |
olddirfd: u64, oldpath: uintptr: u64, |
| 147 |
newdirfd: u64, newpath: uintptr: u64, flags: u64))?; |
| 148 |
}; |
| 149 |
|
| 150 |
export fn symlinkat( |
| 151 |
target: path, |
| 152 |
newdirfd: int, |
| 153 |
linkpath: path, |
| 154 |
) (void | errno) = { |
| 155 |
let target = kpath(target)?; |
| 156 |
static let linkpathbuf: [PATH_MAX]u8 = [0...]; |
| 157 |
let linkpath = copy_kpath(linkpath, linkpathbuf)?; |
| 158 |
wrap_return(syscall3(SYS_symlinkat, target: uintptr: u64, |
| 159 |
newdirfd: u64, linkpath: uintptr: u64))?; |
| 160 |
}; |
| 161 |
|
| 162 |
export fn mknodat( |
| 163 |
dirfd: int, |
| 164 |
path: path, |
| 165 |
mode: mode_t, |
| 166 |
dev: dev_t, |
| 167 |
) (void | errno) = { |
| 168 |
let path = kpath(path)?; |
| 169 |
wrap_return(syscall4(SYS_mknodat, |
| 170 |
dirfd: u64, path: uintptr: u64, mode: u64, dev: u64))?; |
| 171 |
}; |
| 172 |
|
| 173 |
export fn chmod(path: path, mode: uint) (void | errno) = { |
| 174 |
let path = kpath(path)?; |
| 175 |
wrap_return(syscall4(SYS_fchmodat, |
| 176 |
AT_FDCWD: u64, path: uintptr: u64, mode: u64, 0))?; |
| 177 |
}; |
| 178 |
|
| 179 |
export fn fchmod(fd: int, mode: uint) (void | errno) = { |
| 180 |
wrap_return(syscall2(SYS_fchmod, |
| 181 |
fd: u64, mode: u64))?; |
| 182 |
}; |
| 183 |
|
| 184 |
export fn fchmodat(dirfd: int, path: path, mode: uint, flags: int) (void | errno) = { |
| 185 |
let path = kpath(path)?; |
| 186 |
wrap_return(syscall4(SYS_fchmodat, |
| 187 |
dirfd: u64, path: uintptr: u64, mode: u64, flags: u64))?; |
| 188 |
}; |
| 189 |
|
| 190 |
export fn chown(path: path, uid: uint, gid: uint) (void | errno) = { |
| 191 |
let path = kpath(path)?; |
| 192 |
wrap_return(syscall5(SYS_fchownat, |
| 193 |
AT_FDCWD: u64, path: uintptr: u64, uid: u32, gid: u32, 0))?; |
| 194 |
}; |
| 195 |
|
| 196 |
export fn fchown(fd: int, uid: uint, gid: uint) (void | errno) = { |
| 197 |
wrap_return(syscall3(SYS_fchown, |
| 198 |
fd: u64, uid: u32, gid: u32))?; |
| 199 |
}; |
| 200 |
|
| 201 |
export fn fchownat(dirfd: int, path: path, uid: uint, gid: uint, flags: int) (void | errno) = { |
| 202 |
let path = kpath(path)?; |
| 203 |
wrap_return(syscall5(SYS_fchownat, |
| 204 |
dirfd: u64, path: uintptr: u64, uid: u32, gid: u32, flags: u64))?; |
| 205 |
}; |
| 206 |
|
| 207 |
export fn utimensat(dirfd: int, path: str, ts: *[2]timespec, flags: int) (void | errno) = { |
| 208 |
let path = kpath(path)?; |
| 209 |
wrap_return(syscall4(SYS_utimensat, |
| 210 |
dirfd: u64, path: uintptr: u64, ts: uintptr: u64, flags: u64))?; |
| 211 |
}; |
| 212 |
|
| 213 |
export fn futimens(fd: int, ts: *[2]timespec) (void | errno) = { |
| 214 |
wrap_return(syscall4(SYS_utimensat, |
| 215 |
fd: u64, 0, ts: uintptr: u64, 0))?; |
| 216 |
}; |
| 217 |
|
| 218 |
export fn renameat( |
| 219 |
olddirfd: int, |
| 220 |
oldpath: path, |
| 221 |
newdirfd: int, |
| 222 |
newpath: path, |
| 223 |
flags: uint, |
| 224 |
) (void | errno) = { |
| 225 |
let oldpath = kpath(oldpath)?; |
| 226 |
static let newpathbuf: [PATH_MAX]u8 = [0...]; |
| 227 |
let newpath = copy_kpath(newpath, newpathbuf)?; |
| 228 |
wrap_return(syscall5(SYS_renameat2, |
| 229 |
olddirfd: u64, oldpath: uintptr: u64, |
| 230 |
newdirfd: u64, newpath: uintptr: u64, |
| 231 |
flags: u64))?; |
| 232 |
}; |
| 233 |
|
| 234 |
export fn dup(fd: int) (int | errno) = { |
| 235 |
return wrap_return(syscall1(SYS_dup, fd: u64))?: int; |
| 236 |
}; |
| 237 |
|
| 238 |
export fn dup2(oldfd: int, newfd: int) (int | errno) = { |
| 239 |
return dup3(oldfd, newfd, 0); |
| 240 |
}; |
| 241 |
|
| 242 |
export fn dup3(oldfd: int, newfd: int, flags: int) (int | errno) = { |
| 243 |
return wrap_return(syscall3(SYS_dup3, |
| 244 |
oldfd: u64, newfd: u64, flags: u64))?: int; |
| 245 |
}; |
| 246 |
|
| 247 |
export fn close(fd: int) (void | errno) = { |
| 248 |
wrap_return(syscall1(SYS_close, fd: u64))?; |
| 249 |
}; |
| 250 |
|
| 251 |
export fn chdir(path: path) (void | errno) = { |
| 252 |
let path = kpath(path)?; |
| 253 |
wrap_return(syscall1(SYS_chdir, path: uintptr: u64))?; |
| 254 |
}; |
| 255 |
|
| 256 |
export fn fchdir(fd: int) (void | errno) = { |
| 257 |
wrap_return(syscall1(SYS_fchdir, fd: u64))?; |
| 258 |
}; |
| 259 |
|
| 260 |
export fn chroot(path: path) (void | errno) = { |
| 261 |
let path = kpath(path)?; |
| 262 |
wrap_return(syscall1(SYS_chroot, path: uintptr: u64))?; |
| 263 |
}; |
| 264 |
|
| 265 |
export fn mkdir(path: path, mode: uint) (void | errno) = { |
| 266 |
let path = kpath(path)?; |
| 267 |
wrap_return(syscall3(SYS_mkdirat, AT_FDCWD: u64, |
| 268 |
path: uintptr: u64, mode: u64))?; |
| 269 |
}; |
| 270 |
|
| 271 |
export fn mkdirat(dirfd: int, path: path, mode: uint) (void | errno) = { |
| 272 |
let path = kpath(path)?; |
| 273 |
wrap_return(syscall3(SYS_mkdirat, |
| 274 |
dirfd: u64, path: uintptr: u64, mode: u64))?; |
| 275 |
}; |
| 276 |
|
| 277 |
export fn execveat(dirfd: int, path: path, argv: *[*]nullable *const u8, |
| 278 |
envp: *[*]nullable *const u8, flags: int) errno = { |
| 279 |
let path = kpath(path)?; |
| 280 |
return match (wrap_return(syscall5(SYS_execveat, dirfd: u64, |
| 281 |
path: uintptr: u64, argv: uintptr: u64, |
| 282 |
envp: uintptr: u64, flags: u64))) { |
| 283 |
case let err: errno => |
| 284 |
yield err; |
| 285 |
case u64 => |
| 286 |
abort("unreachable"); |
| 287 |
}; |
| 288 |
}; |
| 289 |
|
| 290 |
export fn execve(path: path, argv: *[*]nullable *const u8, |
| 291 |
envp: *[*]nullable *const u8) errno = { |
| 292 |
let path = kpath(path)?; |
| 293 |
return match (wrap_return(syscall3(SYS_execve, path: uintptr: u64, |
| 294 |
argv: uintptr, envp: uintptr))) { |
| 295 |
case let err: errno => |
| 296 |
yield err; |
| 297 |
case u64 => |
| 298 |
abort("unreachable"); |
| 299 |
}; |
| 300 |
}; |
| 301 |
|
| 302 |
// Returns the new PID to the parent, void to the child, or errno if something |
| 303 |
// goes wrong. |
| 304 |
export fn fork() (pid_t | void | errno) = { |
| 305 |
match (clone(null, SIGCHLD, null, null, 0)?) { |
| 306 |
case let id: int => |
| 307 |
return id: pid_t; |
| 308 |
case void => |
| 309 |
return void; |
| 310 |
}; |
| 311 |
}; |
| 312 |
|
| 313 |
export fn getpid() pid_t = syscall0(SYS_getpid): pid_t; |
| 314 |
|
| 315 |
export fn getppid() pid_t = syscall0(SYS_getppid): pid_t; |
| 316 |
|
| 317 |
export fn getpgrp() pid_t = getpgid(0)!; |
| 318 |
|
| 319 |
export fn getpgid(pid: pid_t) (pid_t | errno) = { |
| 320 |
return wrap_return(syscall1(SYS_getpgid, pid: u64))?: pid_t; |
| 321 |
}; |
| 322 |
|
| 323 |
export fn setpgid(pid: pid_t, pgid: pid_t) (void | errno) = { |
| 324 |
wrap_return(syscall2(SYS_setpgid, pid: u64, pgid: u64))?; |
| 325 |
}; |
| 326 |
|
| 327 |
export fn wait4( |
| 328 |
pid: pid_t, |
| 329 |
wstatus: nullable *int, |
| 330 |
options: int, |
| 331 |
rusage: nullable *rusage, |
| 332 |
) (pid_t | errno) = { |
| 333 |
return wrap_return(syscall4(SYS_wait4, |
| 334 |
pid: u64, wstatus: uintptr: u64, |
| 335 |
options: u64, rusage: uintptr: u64))?: pid_t; |
| 336 |
}; |
| 337 |
|
| 338 |
export fn waitid( |
| 339 |
idtype: int, |
| 340 |
id: id_t, |
| 341 |
info: *siginfo, |
| 342 |
options: int, |
| 343 |
rusage: nullable *rusage, |
| 344 |
) (void | errno) = { |
| 345 |
wrap_return(syscall5(SYS_waitid, |
| 346 |
idtype: u64, id: u64, info: uintptr: u64, |
| 347 |
options: u64, rusage: uintptr: u64))?; |
| 348 |
}; |
| 349 |
|
| 350 |
export fn sendfile( |
| 351 |
out: int, |
| 352 |
in: int, |
| 353 |
offs: nullable *size, |
| 354 |
count: size, |
| 355 |
) (size | errno) = wrap_return(syscall4(SYS_sendfile, |
| 356 |
out: u64, in: u64, offs: uintptr: u64, count: u64))?: size; |
| 357 |
|
| 358 |
export fn exit(status: int) never = { |
| 359 |
syscall1(SYS_exit_group, status: u64); |
| 360 |
abort(); |
| 361 |
}; |
| 362 |
|
| 363 |
export fn kill(pid: pid_t, signal: int) (void | errno) = { |
| 364 |
wrap_return(syscall2(SYS_kill, pid: u64, signal: u64))?; |
| 365 |
}; |
| 366 |
|
| 367 |
export fn pipe2(pipefd: *[2]int, flags: int) (void | errno) = { |
| 368 |
wrap_return(syscall2(SYS_pipe2, pipefd: uintptr: u64, flags: u64))?; |
| 369 |
}; |
| 370 |
|
| 371 |
export fn mmap( |
| 372 |
addr: nullable *opaque, |
| 373 |
length: size, |
| 374 |
prot: uint, |
| 375 |
flags: uint, |
| 376 |
fd: int, |
| 377 |
offs: size |
| 378 |
) (*opaque | errno) = { |
| 379 |
let r = syscall6(SYS_mmap, addr: uintptr: u64, |
| 380 |
length: u64, prot: u64, flags: u64, fd: u64, offs: u64); |
| 381 |
match (wrap_return(r)) { |
| 382 |
case let err: errno => |
| 383 |
if (err == -EPERM && addr == null && (flags & MAP_ANON) > 0 |
| 384 |
&& (flags & MAP_FIXED) == 0) { |
| 385 |
// Fix up incorrect EPERM from kernel: |
| 386 |
return ENOMEM; |
| 387 |
}; |
| 388 |
return err; |
| 389 |
case let n: u64 => |
| 390 |
return n: uintptr: *opaque; |
| 391 |
}; |
| 392 |
}; |
| 393 |
|
| 394 |
export fn mremap( |
| 395 |
old_addr: *opaque, |
| 396 |
old_len: size, |
| 397 |
new_len: size, |
| 398 |
flags: uint, |
| 399 |
new_addr: nullable *opaque, |
| 400 |
) (*opaque | errno) = { |
| 401 |
let r = syscall5(SYS_mremap, old_addr: uintptr: u64, old_len: u64, |
| 402 |
new_len: u64, flags: u64, new_addr: uintptr: u64); |
| 403 |
return wrap_return(r)?: uintptr: *opaque; |
| 404 |
}; |
| 405 |
|
| 406 |
export fn munmap(addr: *opaque, length: size) (void | errno) = { |
| 407 |
wrap_return(syscall2(SYS_munmap, |
| 408 |
addr: uintptr: u64, length: u64))?; |
| 409 |
}; |
| 410 |
|
| 411 |
export fn mprotect(addr: *opaque, length: size, prot: uint) (void | errno) = { |
| 412 |
wrap_return(syscall3(SYS_mprotect, |
| 413 |
addr: uintptr: u64, length: u64, prot: u64))?; |
| 414 |
}; |
| 415 |
|
| 416 |
export fn lseek(fd: int, off: i64, whence: int) (i64 | errno) = { |
| 417 |
return wrap_return(syscall3(SYS_lseek, |
| 418 |
fd: u64, off: u64, whence: u64))?: i64; |
| 419 |
}; |
| 420 |
|
| 421 |
export fn ftruncate(fd: int, ln: off_t) (void | errno) = { |
| 422 |
wrap_return(syscall2(SYS_ftruncate, fd: u64, ln: u32))?; |
| 423 |
}; |
| 424 |
|
| 425 |
fn faccessat1(dirfd: int, path: *const u8, mode: int) (bool | errno) = { |
| 426 |
return match (wrap_return(syscall3(SYS_faccessat, dirfd: u64, |
| 427 |
path: uintptr: u64, mode: u64))) { |
| 428 |
case let err: errno => |
| 429 |
yield switch (err) { |
| 430 |
case EACCES => |
| 431 |
yield false; |
| 432 |
case => |
| 433 |
yield err; |
| 434 |
}; |
| 435 |
case let n: u64 => |
| 436 |
assert(n == 0); |
| 437 |
yield true; |
| 438 |
}; |
| 439 |
}; |
| 440 |
|
| 441 |
// The use of this function is discouraged, as it can create race conditions. |
| 442 |
// TOCTOU is preferred: attempt to simply use the resource you need and handle |
| 443 |
// any access errors which occur. |
| 444 |
export fn faccessat( |
| 445 |
dirfd: int, |
| 446 |
path: path, |
| 447 |
mode: int, |
| 448 |
flags: int, |
| 449 |
) (bool | errno) = { |
| 450 |
let path = kpath(path)?; |
| 451 |
match (wrap_return(syscall4(SYS_faccessat2, dirfd: u64, |
| 452 |
path: uintptr: u64, mode: u64, flags: u64))) { |
| 453 |
case let err: errno => |
| 454 |
switch (err) { |
| 455 |
case EACCES => |
| 456 |
return false; |
| 457 |
case ENOSYS => |
| 458 |
if (flags == 0) { |
| 459 |
return faccessat1(dirfd, path, mode); |
| 460 |
} else { |
| 461 |
return err; |
| 462 |
}; |
| 463 |
case => |
| 464 |
return err; |
| 465 |
}; |
| 466 |
case let n: u64 => |
| 467 |
assert(n == 0); |
| 468 |
return true; |
| 469 |
}; |
| 470 |
}; |
| 471 |
|
| 472 |
export fn getdents64(dirfd: int, dirp: *opaque, count: size) (size | errno) = { |
| 473 |
return wrap_return(syscall3(SYS_getdents64, dirfd: u64, |
| 474 |
dirp: uintptr: u64, count: u64))?: size; |
| 475 |
}; |
| 476 |
|
| 477 |
// The use of this function is discouraged, as it can create race conditions. |
| 478 |
// TOCTOU is preferred: attempt to simply use the resource you need and handle |
| 479 |
// any access errors which occur. |
| 480 |
export fn access(path: path, mode: int) (bool | errno) = |
| 481 |
faccessat(AT_FDCWD, path, mode, 0); |
| 482 |
|
| 483 |
export type fcntl_arg = (void | int | *st_flock | *f_owner_ex | *u64); |
| 484 |
|
| 485 |
export fn fcntl(fd: int, cmd: int, arg: fcntl_arg) (int | errno) = { |
| 486 |
let _fd = fd: u64, _cmd = cmd: u64; |
| 487 |
return wrap_return(match (arg) { |
| 488 |
case void => |
| 489 |
yield syscall2(SYS_fcntl, _fd, _cmd); |
| 490 |
case let i: int => |
| 491 |
yield syscall3(SYS_fcntl, _fd, _cmd, i: u64); |
| 492 |
case let l: *st_flock => |
| 493 |
yield syscall3(SYS_fcntl, _fd, _cmd, l: uintptr: u64); |
| 494 |
case let o: *f_owner_ex => |
| 495 |
yield syscall3(SYS_fcntl, _fd, _cmd, o: uintptr: u64); |
| 496 |
case let u: *u64 => |
| 497 |
yield syscall3(SYS_fcntl, _fd, _cmd, u: uintptr: u64); |
| 498 |
})?: int; |
| 499 |
}; |
| 500 |
|
| 501 |
export fn getrandom(buf: *opaque, bufln: size, flags: uint) (size | errno) = { |
| 502 |
return wrap_return(syscall3(SYS_getrandom, |
| 503 |
buf: uintptr: u64, bufln: u64, flags: u64))?: size; |
| 504 |
}; |
| 505 |
|
| 506 |
export fn clock_gettime(clock_id: int, tp: *timespec) (void | errno) = { |
| 507 |
wrap_return(syscall2(SYS_clock_gettime, |
| 508 |
clock_id: u64, tp: uintptr: u64))?; |
| 509 |
}; |
| 510 |
|
| 511 |
export fn clock_settime(clock_id: int, tp: *const timespec) (void | errno) = { |
| 512 |
wrap_return(syscall2(SYS_clock_settime, |
| 513 |
clock_id: u64, tp: uintptr: u64))?; |
| 514 |
}; |
| 515 |
|
| 516 |
export fn nanosleep( |
| 517 |
req: *const timespec, |
| 518 |
rem: nullable *timespec, |
| 519 |
) (void | errno) = { |
| 520 |
wrap_return(syscall2(SYS_nanosleep, |
| 521 |
req: uintptr: u64, rem: uintptr: u64))?; |
| 522 |
}; |
| 523 |
|
| 524 |
export fn clock_nanosleep( |
| 525 |
clock_id: int, |
| 526 |
flags: int, |
| 527 |
requested: *timespec, |
| 528 |
remaining: nullable *timespec, |
| 529 |
) (void | errno) = { |
| 530 |
wrap_return(syscall4(SYS_clock_nanosleep, |
| 531 |
clock_id: u64, |
| 532 |
flags: u64, |
| 533 |
requested: uintptr: u64, |
| 534 |
remaining: uintptr: u64 |
| 535 |
))?; |
| 536 |
}; |
| 537 |
|
| 538 |
export fn uname(uts: *utsname) (void | errno) = { |
| 539 |
wrap_return(syscall1(SYS_uname, uts: uintptr: u64))?; |
| 540 |
}; |
| 541 |
|
| 542 |
// The return value is statically allocated and must be duplicated before |
| 543 |
// calling getcwd again. |
| 544 |
export fn getcwd() (*const u8 | errno) = { |
| 545 |
static let pathbuf: [PATH_MAX]u8 = [0...]; |
| 546 |
wrap_return(syscall2(SYS_getcwd, |
| 547 |
&pathbuf: *[*]u8: uintptr: u64, |
| 548 |
PATH_MAX))?; |
| 549 |
return &pathbuf: *const u8; |
| 550 |
}; |
| 551 |
|
| 552 |
export fn ppoll( |
| 553 |
fds: *[*]pollfd, |
| 554 |
nfds: nfds_t, |
| 555 |
timeout: const nullable *timespec, |
| 556 |
sigmask: const nullable *sigset, |
| 557 |
) (int | errno) = { |
| 558 |
return wrap_return(syscall4(SYS_ppoll, fds: uintptr: u64, nfds: u64, |
| 559 |
timeout: uintptr: u64, sigmask: uintptr: u64))?: int; |
| 560 |
}; |
| 561 |
|
| 562 |
export fn poll(fds: *[*]pollfd, nfds: nfds_t, timeout: int) (int | errno) = { |
| 563 |
const ts = timespec { |
| 564 |
tv_sec = timeout % 1000, |
| 565 |
tv_nsec = timeout * 1000000, |
| 566 |
}; |
| 567 |
return ppoll(fds, nfds, (if (timeout != -1) &ts else null), null); |
| 568 |
}; |
| 569 |
|
| 570 |
export fn epoll_create1(flags: int) (int | errno) = { |
| 571 |
return wrap_return(syscall1(SYS_epoll_create1, flags: u64))?: int; |
| 572 |
}; |
| 573 |
|
| 574 |
export fn epoll_create(size_: int) (int | errno) = { |
| 575 |
return epoll_create1(0); |
| 576 |
}; |
| 577 |
|
| 578 |
export fn epoll_ctl( |
| 579 |
epfd: int, |
| 580 |
op: int, |
| 581 |
fd: int, |
| 582 |
event: nullable *epoll_event |
| 583 |
) (void | errno) = { |
| 584 |
wrap_return(syscall4(SYS_epoll_ctl, |
| 585 |
epfd: u64, op: u64, fd: u64, event: uintptr: u64))?; |
| 586 |
}; |
| 587 |
|
| 588 |
export fn epoll_pwait( |
| 589 |
epfd: int, |
| 590 |
events: *epoll_event, |
| 591 |
maxevents: int, |
| 592 |
timeout: int, |
| 593 |
sigmask: nullable *sigset |
| 594 |
) (int | errno) = { |
| 595 |
return wrap_return(syscall6(SYS_epoll_pwait, |
| 596 |
epfd: u64, events: uintptr: u64, |
| 597 |
maxevents: u64, timeout: u64, |
| 598 |
sigmask: uintptr: u64, size(sigset): u64))?: int; |
| 599 |
}; |
| 600 |
|
| 601 |
export fn epoll_wait( |
| 602 |
epfd: int, |
| 603 |
events: *epoll_event, |
| 604 |
maxevents: int, |
| 605 |
timeout: int, |
| 606 |
) (int | errno) = { |
| 607 |
return epoll_pwait(epfd, events, maxevents, timeout, null); |
| 608 |
}; |
| 609 |
|
| 610 |
export fn timerfd_create(clock_id: int, flags: int) (int | errno) = { |
| 611 |
return wrap_return(syscall2(SYS_timerfd_create, |
| 612 |
clock_id: u64, flags: u64))?: int; |
| 613 |
}; |
| 614 |
|
| 615 |
export fn eventfd(initval: uint, flags: int) (int | errno) = { |
| 616 |
return wrap_return(syscall2(SYS_eventfd2, |
| 617 |
initval: u64, flags: u64))?: int; |
| 618 |
}; |
| 619 |
|
| 620 |
export fn timerfd_settime( |
| 621 |
fd: int, |
| 622 |
flags: int, |
| 623 |
new_value: *const itimerspec, |
| 624 |
old_value: nullable *itimerspec |
| 625 |
) (int | errno) = { |
| 626 |
return wrap_return(syscall4(SYS_timerfd_settime, |
| 627 |
fd: u64, flags: u64, |
| 628 |
new_value: uintptr: u64, old_value: uintptr: u64))?: int; |
| 629 |
}; |
| 630 |
|
| 631 |
export fn timerfd_gettime(fd: int, curr_value: *itimerspec) (int | errno) = { |
| 632 |
return wrap_return(syscall2(SYS_timerfd_gettime, |
| 633 |
fd: u64, curr_value: uintptr: u64))?: int; |
| 634 |
}; |
| 635 |
|
| 636 |
export fn signalfd(fd: int, mask: *const sigset, flags: int) (int | errno) = { |
| 637 |
return wrap_return(syscall4(SYS_signalfd4, |
| 638 |
fd: u64, mask: uintptr: u64, size(sigset): u64, |
| 639 |
flags: u64))?: int; |
| 640 |
}; |
| 641 |
|
| 642 |
export fn sigprocmask( |
| 643 |
how: int, |
| 644 |
set: nullable *const sigset, |
| 645 |
old: nullable *sigset |
| 646 |
) (int | errno) = { |
| 647 |
return wrap_return(syscall4(SYS_rt_sigprocmask, |
| 648 |
how: u64, set: uintptr: u64, old: uintptr: u64, |
| 649 |
size(sigset): u64))?: int; |
| 650 |
}; |
| 651 |
|
| 652 |
fn restore() void; |
| 653 |
fn restore_si() void; |
| 654 |
|
| 655 |
export fn sigaction( |
| 656 |
signum: int, |
| 657 |
act: *const sigact, |
| 658 |
old: nullable *sigact |
| 659 |
) (int | errno) = { |
| 660 |
let real_act = *act; |
| 661 |
real_act.sa_flags |= SA_RESTORER; |
| 662 |
let restore_fn = if ((act.sa_flags & SA_SIGINFO) != 0) &restore_si else &restore; |
| 663 |
real_act.sa_restorer = &restore; |
| 664 |
return wrap_return(syscall4(SYS_rt_sigaction, |
| 665 |
signum: u64, &real_act: uintptr: u64, old: uintptr: u64, |
| 666 |
size(sigset): u64))?: int; |
| 667 |
}; |
| 668 |
|
| 669 |
export fn sigaltstack( |
| 670 |
ss: nullable *stack_t, |
| 671 |
old_ss: nullable *stack_t, |
| 672 |
) (void | errno) = { |
| 673 |
wrap_return(syscall2(SYS_sigaltstack, |
| 674 |
ss: uintptr: u64, old_ss: uintptr: u64))?; |
| 675 |
}; |
| 676 |
|
| 677 |
export fn socket(domain: int, type_: int, protocol: int) (int | errno) = { |
| 678 |
return wrap_return(syscall3(SYS_socket, |
| 679 |
domain: u64, type_: u64, protocol: u64))?: int; |
| 680 |
}; |
| 681 |
|
| 682 |
export fn socketpair( |
| 683 |
domain: int, |
| 684 |
type_: int, |
| 685 |
protocol: int, |
| 686 |
sv: *[2]int |
| 687 |
) (int | errno) = { |
| 688 |
return wrap_return(syscall4(SYS_socketpair, domain: u64, |
| 689 |
type_: u64, protocol: u64, sv: uintptr: u64))?: int; |
| 690 |
}; |
| 691 |
|
| 692 |
export fn connect(sockfd: int, addr: *const sockaddr, addrlen: u32) (void | errno) = { |
| 693 |
wrap_return(syscall3(SYS_connect, sockfd: u64, |
| 694 |
addr: uintptr: u64, addrlen: u64))?; |
| 695 |
}; |
| 696 |
|
| 697 |
export fn bind(sockfd: int, addr: *const sockaddr, addrlen: u32) (void | errno) = { |
| 698 |
wrap_return(syscall3(SYS_bind, sockfd: u64, |
| 699 |
addr: uintptr: u64, addrlen: u64))?; |
| 700 |
}; |
| 701 |
|
| 702 |
export fn listen(sockfd: int, backlog: u32) (int | errno) = { |
| 703 |
return wrap_return(syscall2(SYS_listen, |
| 704 |
sockfd: u64, backlog: u64))?: int; |
| 705 |
}; |
| 706 |
|
| 707 |
export fn accept(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = { |
| 708 |
return wrap_return(syscall3(SYS_accept, |
| 709 |
sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int; |
| 710 |
}; |
| 711 |
|
| 712 |
export fn accept4(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32, flags: int) (int | errno) = { |
| 713 |
return wrap_return(syscall4(SYS_accept4, |
| 714 |
sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64, flags: u64))?: int; |
| 715 |
}; |
| 716 |
|
| 717 |
export fn recvfrom(sockfd: int, buf: *opaque, len_: size, flags: int, |
| 718 |
src_addr: nullable *sockaddr, addrlen: nullable *u32 |
| 719 |
) (size | errno) = { |
| 720 |
return wrap_return(syscall6(SYS_recvfrom, |
| 721 |
sockfd: u64, buf: uintptr: u64, len_: u64, flags: u64, |
| 722 |
src_addr: uintptr: u64, addrlen: uintptr: u64))?: size; |
| 723 |
}; |
| 724 |
|
| 725 |
export fn sendto(sockfd: int, buf: *opaque, len_: size, flags: int, |
| 726 |
dest_addr: nullable *sockaddr, addrlen: u32 |
| 727 |
) (size | errno) = { |
| 728 |
return wrap_return(syscall6(SYS_sendto, |
| 729 |
sockfd: u64, buf: uintptr: u64, len_: u64, flags: u64, |
| 730 |
dest_addr: uintptr: u64, addrlen: u64))?: size; |
| 731 |
}; |
| 732 |
|
| 733 |
export fn recv(sockfd: int, buf: *opaque, len_: size, flags: int) (size | errno) = { |
| 734 |
return recvfrom(sockfd, buf, len_, flags, null, null); |
| 735 |
}; |
| 736 |
|
| 737 |
export fn send(sockfd: int, buf: *opaque, len_: size, flags: int) (size | errno) = { |
| 738 |
return sendto(sockfd, buf, len_, flags, null, 0); |
| 739 |
}; |
| 740 |
|
| 741 |
export fn getsockopt(sockfd: int, level: int, optname: int, optval: nullable *opaque, optlen: nullable *u32) (int | errno) = { |
| 742 |
return wrap_return(syscall5(SYS_getsockopt, |
| 743 |
sockfd: u64, level: u64, optname: u64, |
| 744 |
optval: uintptr: u64, optlen: uintptr: u64))?: int; |
| 745 |
}; |
| 746 |
|
| 747 |
export fn setsockopt(sockfd: int, level: int, optname: int, optval: *opaque, optlen: u32) (int | errno) = { |
| 748 |
return wrap_return(syscall5(SYS_setsockopt, |
| 749 |
sockfd: u64, level: u64, optname: u64, |
| 750 |
optval: uintptr: u64, optlen: u64))?: int; |
| 751 |
}; |
| 752 |
|
| 753 |
export type ioctl_arg = (nullable *opaque | u64); |
| 754 |
|
| 755 |
export fn ioctl(fd: int, req: u64, arg: ioctl_arg) (int | errno) = { |
| 756 |
let fd = fd: u64, req = req: u64; |
| 757 |
return wrap_return(match (arg) { |
| 758 |
case let u: u64 => |
| 759 |
yield syscall3(SYS_ioctl, fd, req, u); |
| 760 |
case let v: nullable *opaque => |
| 761 |
yield syscall3(SYS_ioctl, fd, req, v: uintptr: u64); |
| 762 |
})?: int; |
| 763 |
}; |
| 764 |
|
| 765 |
export fn getsockname(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = { |
| 766 |
return wrap_return(syscall3(SYS_getsockname, |
| 767 |
sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int; |
| 768 |
}; |
| 769 |
|
| 770 |
export fn getpeername(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = { |
| 771 |
return wrap_return(syscall3(SYS_getpeername, |
| 772 |
sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int; |
| 773 |
}; |
| 774 |
|
| 775 |
export fn readv(fd: int, iov: const *[*]iovec, iovcnt: int) (size | errno) = { |
| 776 |
return wrap_return(syscall3(SYS_readv, |
| 777 |
fd: u64, iov: uintptr: u64, iovcnt: u64))?: size; |
| 778 |
}; |
| 779 |
|
| 780 |
export fn writev(fd: int, iov: const *[*]iovec, iovcnt: int) (size | errno) = { |
| 781 |
return wrap_return(syscall3(SYS_writev, |
| 782 |
fd: u64, iov: uintptr: u64, iovcnt: u64))?: size; |
| 783 |
}; |
| 784 |
|
| 785 |
export fn sendmsg(fd: int, msg: *const msghdr, flags: int) (int | errno) = { |
| 786 |
return wrap_return(syscall3(SYS_sendmsg, |
| 787 |
fd: u64, msg: uintptr: u64, flags: u64))?: int; |
| 788 |
}; |
| 789 |
|
| 790 |
export fn recvmsg(fd: int, msg: *const msghdr, flags: int) (int | errno) = { |
| 791 |
return wrap_return(syscall3(SYS_recvmsg, |
| 792 |
fd: u64, msg: uintptr: u64, flags: u64))?: int; |
| 793 |
}; |
| 794 |
|
| 795 |
export fn umask(mode: mode_t) (mode_t | errno) = { |
| 796 |
return wrap_return(syscall1(SYS_umask, mode: u64))?: mode_t; |
| 797 |
}; |
| 798 |
|
| 799 |
export fn setresuid(uid: uid_t, euid: uid_t, suid: uid_t) (void | errno) = { |
| 800 |
wrap_return(syscall3(SYS_setresuid, uid: u64, euid: u64, suid: u64))?; |
| 801 |
}; |
| 802 |
|
| 803 |
export fn setresgid(gid: gid_t, egid: gid_t, sgid: gid_t) (void | errno) = { |
| 804 |
wrap_return(syscall3(SYS_setresgid, gid: u64, egid: u64, sgid: u64))?; |
| 805 |
}; |
| 806 |
|
| 807 |
export fn getresuid(uid: *uid_t, euid: *uid_t, suid: *uid_t) (void | errno) = { |
| 808 |
wrap_return(syscall3(SYS_getresuid, |
| 809 |
uid: uintptr: u64, |
| 810 |
euid: uintptr: u64, |
| 811 |
suid: uintptr: u64))?; |
| 812 |
}; |
| 813 |
|
| 814 |
export fn getresgid(gid: *gid_t, egid: *gid_t, sgid: *gid_t) (void | errno) = { |
| 815 |
wrap_return(syscall3(SYS_getresgid, |
| 816 |
gid: uintptr: u64, |
| 817 |
egid: uintptr: u64, |
| 818 |
sgid: uintptr: u64))?; |
| 819 |
}; |
| 820 |
|
| 821 |
export fn getgroups(gids: []gid_t) (uint | errno) = { |
| 822 |
return wrap_return(syscall2(SYS_getgroups, |
| 823 |
len(gids): u64, gids: *[*]gid_t: uintptr: u64))?: uint; |
| 824 |
}; |
| 825 |
|
| 826 |
export fn setgroups(gids: []gid_t) (void | errno) = { |
| 827 |
wrap_return(syscall2(SYS_setgroups, |
| 828 |
len(gids): u64, gids: *[*]gid_t: uintptr: u64))?; |
| 829 |
}; |
| 830 |
|
| 831 |
export fn getpriority(which: int, who: id_t) (int | errno) = { |
| 832 |
return wrap_return(syscall2(SYS_setpriority, |
| 833 |
which: u64, who: u64))?: int; |
| 834 |
}; |
| 835 |
|
| 836 |
export fn setpriority(which: int, who: id_t, prio: int) (void | errno) = { |
| 837 |
wrap_return(syscall3(SYS_setpriority, which: u64, who: u64, prio: u64))?; |
| 838 |
}; |
| 839 |
|
| 840 |
export fn io_uring_setup(entries: u32, params: *io_uring_params) (int | errno) = { |
| 841 |
return wrap_return(syscall2(SYS_io_uring_setup, |
| 842 |
entries: u64, params: uintptr: u64))?: int; |
| 843 |
}; |
| 844 |
|
| 845 |
export fn io_uring_register( |
| 846 |
fd: int, |
| 847 |
opcode: uint, |
| 848 |
arg: nullable *opaque, |
| 849 |
nr_args: uint, |
| 850 |
) (int | errno) = wrap_return(syscall4(SYS_io_uring_register, |
| 851 |
fd: u64, opcode: u64, arg: uintptr: u64, nr_args: u64))?: int; |
| 852 |
|
| 853 |
export fn io_uring_enter( |
| 854 |
fd: int, |
| 855 |
to_submit: uint, |
| 856 |
min_complete: uint, |
| 857 |
flags: uint, |
| 858 |
sig: const nullable *sigset, |
| 859 |
) (uint | errno) = { |
| 860 |
return wrap_return(syscall5(SYS_io_uring_enter, |
| 861 |
fd: u64, to_submit: u64, min_complete: u64, |
| 862 |
flags: u64, sig: uintptr: u64))?: uint; |
| 863 |
}; |
| 864 |
|
| 865 |
export fn io_uring_enter2( |
| 866 |
fd: int, |
| 867 |
to_submit: uint, |
| 868 |
min_complete: uint, |
| 869 |
flags: uint, |
| 870 |
arg: const nullable *opaque, |
| 871 |
argsz: size, |
| 872 |
) (uint | errno) = { |
| 873 |
return wrap_return(syscall6(SYS_io_uring_enter, |
| 874 |
fd: u64, to_submit: u64, min_complete: u64, |
| 875 |
flags: u64, arg: uintptr: u64, argsz: u64))?: uint; |
| 876 |
}; |
| 877 |
|
| 878 |
export fn mlock2(addr: *opaque, length: size, flags: uint) (void | errno) = { |
| 879 |
wrap_return(syscall3(SYS_mlock2, addr: uintptr: u64, |
| 880 |
length: u64, flags: u64))?; |
| 881 |
}; |
| 882 |
|
| 883 |
export fn munlock(addr: *opaque, length: size) (void | errno) = { |
| 884 |
wrap_return(syscall2(SYS_munlock, addr: uintptr: u64, |
| 885 |
length: u64))?; |
| 886 |
}; |
| 887 |
|
| 888 |
export fn mlockall(flags: uint) (void | errno) = { |
| 889 |
wrap_return(syscall1(SYS_mlockall, flags: u64))?; |
| 890 |
}; |
| 891 |
|
| 892 |
export fn munlockall() (void | errno) = { |
| 893 |
wrap_return(syscall0(SYS_munlockall))?; |
| 894 |
}; |
| 895 |
|
| 896 |
export fn prctl( |
| 897 |
option: int, |
| 898 |
arg2: u64, |
| 899 |
arg3: u64, |
| 900 |
arg4: u64, |
| 901 |
arg5: u64, |
| 902 |
) (int | errno) = { |
| 903 |
return wrap_return(syscall5(SYS_prctl, option: u64, arg2, arg3, arg4, |
| 904 |
arg5))?: int; |
| 905 |
}; |
| 906 |
|
| 907 |
export fn add_key( |
| 908 |
keytype: *const u8, |
| 909 |
name: *const u8, |
| 910 |
payload: *opaque, |
| 911 |
plen: size, |
| 912 |
keyring: int, |
| 913 |
) (int | errno) = { |
| 914 |
return wrap_return(syscall5(SYS_add_key, |
| 915 |
keytype: uintptr: u64, name: uintptr: u64, |
| 916 |
payload: uintptr: u64, plen: u64, |
| 917 |
keyring: u64))?: int; |
| 918 |
}; |
| 919 |
|
| 920 |
export fn keyctl( |
| 921 |
operation: int, |
| 922 |
arg2: u64, |
| 923 |
arg3: u64, |
| 924 |
arg4: u64, |
| 925 |
arg5: u64, |
| 926 |
) (int | errno) = { |
| 927 |
return wrap_return(syscall5(SYS_keyctl, operation: u64, |
| 928 |
arg2, arg3, arg4, arg5))?: int; |
| 929 |
}; |
| 930 |
|
| 931 |
export fn getsid(pid: pid_t) (pid_t | errno) = { |
| 932 |
return wrap_return(syscall1(SYS_getsid, pid: u64))?: pid_t; |
| 933 |
}; |
| 934 |
|
| 935 |
export fn setsid() (void | errno) = { |
| 936 |
wrap_return(syscall0(SYS_setsid))?; |
| 937 |
}; |
| 938 |
|
| 939 |
export fn mount( |
| 940 |
source: path, |
| 941 |
target: path, |
| 942 |
filesystemtype: *const u8, |
| 943 |
mountflags: u64, |
| 944 |
data: nullable *opaque |
| 945 |
) (void | errno) = { |
| 946 |
let source = kpath(source)?; |
| 947 |
let target = kpath(target)?; |
| 948 |
wrap_return(syscall5(SYS_mount, source: uintptr, target: uintptr, |
| 949 |
filesystemtype: uintptr, mountflags: u64, data: uintptr))?; |
| 950 |
}; |
| 951 |
|
| 952 |
export fn umount2(target: path, flags: int) (void | errno) = { |
| 953 |
let target = kpath(target)?; |
| 954 |
wrap_return(syscall2(SYS_umount2, target: uintptr, flags: u64))?; |
| 955 |
}; |
| 956 |
|
| 957 |
export fn ptrace( |
| 958 |
request: int, |
| 959 |
pid: pid_t, |
| 960 |
addr: uintptr, |
| 961 |
data: uintptr, |
| 962 |
) (u64 | errno) = { |
| 963 |
// PTRACE_PEEK* requests write into *data instead of just returning |
| 964 |
// the word that they read |
| 965 |
let result = 0u64; |
| 966 |
const wrdata = request >= PTRACE_PEEKTEXT && request <= PTRACE_PEEKUSER; |
| 967 |
if (wrdata) { |
| 968 |
data = &result: uintptr; |
| 969 |
}; |
| 970 |
const ret = wrap_return(syscall4(SYS_ptrace, request: u64, pid: u64, |
| 971 |
addr, data))?: u64; |
| 972 |
if (wrdata) { |
| 973 |
return result; |
| 974 |
} else { |
| 975 |
return ret; |
| 976 |
}; |
| 977 |
}; |
| 978 |
|
| 979 |
|
| 980 |
export fn sync() void = { |
| 981 |
wrap_return(syscall0(SYS_sync))!; |
| 982 |
}; |
| 983 |
|
| 984 |
export fn memfd_create(name: path, flags: uint) (int | errno) = { |
| 985 |
let path = kpath(name)?; |
| 986 |
return wrap_return(syscall2(SYS_memfd_create, |
| 987 |
path: uintptr: u64, flags: u64))?: int; |
| 988 |
}; |
| 989 |
|
| 990 |
export fn splice( |
| 991 |
fd_in: int, |
| 992 |
off_in: nullable *u64, |
| 993 |
fd_out: int, |
| 994 |
off_out: nullable *u64, |
| 995 |
ln: size, |
| 996 |
flags: uint, |
| 997 |
) (size | errno) = { |
| 998 |
return wrap_return(syscall6(SYS_splice, |
| 999 |
fd_in: u64, off_in: uintptr: u64, |
| 1000 |
fd_out: u64, off_out: uintptr: u64, |
| 1001 |
ln: u64, flags: u64))?: size; |
| 1002 |
}; |
| 1003 |
|
| 1004 |
export fn tee(fd_in: int, fd_out: int, ln: size, flags: uint) (size | errno) = { |
| 1005 |
return wrap_return(syscall4(SYS_tee, |
| 1006 |
fd_in: u64, fd_out: u64, |
| 1007 |
ln: u64, flags: u64))?: size; |
| 1008 |
}; |
| 1009 |
|
| 1010 |
export fn fallocate(fd: int, mode: int, off: i64, ln: i64) (void | errno) = { |
| 1011 |
wrap_return(syscall4(SYS_fallocate, |
| 1012 |
fd: u64, mode: u64, off: u64, ln: u64))?; |
| 1013 |
}; |
| 1014 |
|
| 1015 |
export fn posix_fallocate(fd: int, off: i64, ln: i64) (void | errno) = { |
| 1016 |
fallocate(fd, 0, off, ln)?; |
| 1017 |
}; |
| 1018 |
|
| 1019 |
export fn flock(fd: int, op: int) (void | errno) = { |
| 1020 |
wrap_return(syscall2(SYS_flock, |
| 1021 |
fd: u64, op: u64))?; |
| 1022 |
}; |
| 1023 |
|
| 1024 |
export def NAME_MAX: size = 255z; |
| 1025 |
export def INOTIFY_EVENT_MAX_SIZE: size = size(int) + size(u32)*3 + NAME_MAX + 1z; |
| 1026 |
|
| 1027 |
export fn inotify_init() (int | errno) = { |
| 1028 |
return wrap_return(syscall1(SYS_inotify_init1, 0))?: int; |
| 1029 |
}; |
| 1030 |
|
| 1031 |
export fn inotify_init1(flags: int) (int | errno) = { |
| 1032 |
return wrap_return(syscall1(SYS_inotify_init1, flags: u64))?: int; |
| 1033 |
}; |
| 1034 |
|
| 1035 |
export fn inotify_add_watch(fd: int, path: path, mask: u32) (int | errno) = { |
| 1036 |
let path = kpath(path)?; |
| 1037 |
return wrap_return(syscall3(SYS_inotify_add_watch, |
| 1038 |
fd: u64, path: uintptr: u64, mask))?: int; |
| 1039 |
}; |
| 1040 |
|
| 1041 |
export fn inotify_rm_watch(fd: int, wd: int) (int | errno) = { |
| 1042 |
return wrap_return(syscall2(SYS_inotify_rm_watch, |
| 1043 |
fd: u64, wd: u64))?: int; |
| 1044 |
}; |
| 1045 |
|
| 1046 |
export type inotify_event = struct { |
| 1047 |
wd: int, |
| 1048 |
mask: u32, |
| 1049 |
cookie: u32, |
| 1050 |
length: u32, |
| 1051 |
name: [*]u8, |
| 1052 |
}; |
| 1053 |
|
| 1054 |
export fn shmat(id: int, addr: *const opaque, flag: int) *opaque = { |
| 1055 |
return syscall3(SYS_shmat, id: u64, addr: uintptr: u64, |
| 1056 |
flag: u64): uintptr: *opaque; |
| 1057 |
}; |
| 1058 |
|
| 1059 |
export fn sched_getaffinity( |
| 1060 |
pid: pid_t, |
| 1061 |
cpusetsize: size, |
| 1062 |
mask: *cpu_set, |
| 1063 |
) (void | errno) = { |
| 1064 |
wrap_return(syscall3(SYS_sched_getaffinity, |
| 1065 |
pid: u64, cpusetsize: u64, mask: uintptr: u64))?; |
| 1066 |
}; |
| 1067 |
|
| 1068 |
export fn sched_setaffinity( |
| 1069 |
pid: pid_t, |
| 1070 |
cpusetsize: size, |
| 1071 |
mask: *const cpu_set, |
| 1072 |
) (void | errno) = { |
| 1073 |
wrap_return(syscall3(SYS_sched_setaffinity, |
| 1074 |
pid: u64, cpusetsize: u64, mask: uintptr: u64))?; |
| 1075 |
}; |
| 1076 |
|
| 1077 |
export fn getrlimit(resource: int, rlim: *rlimit) (void | errno) = { |
| 1078 |
wrap_return(syscall2(SYS_getrlimit, |
| 1079 |
resource: u64, rlim: uintptr: u64))?; |
| 1080 |
}; |
| 1081 |
|
| 1082 |
export fn setrlimit(resource: int, rlim: *const rlimit) (void | errno) = { |
| 1083 |
wrap_return(syscall2(SYS_setrlimit, |
| 1084 |
resource: u64, rlim: uintptr: u64))?; |
| 1085 |
}; |
| 1086 |
|
| 1087 |
export fn shutdown(sockfd: int, how: int) (void | errno) = { |
| 1088 |
wrap_return(syscall2(SYS_shutdown, |
| 1089 |
sockfd: u64, how: u64))?; |
| 1090 |
}; |
| 1091 |
|
| 1092 |
// Sets an extended file attribute. |
| 1093 |
export fn setxattr( |
| 1094 |
path: path, |
| 1095 |
name: str, |
| 1096 |
value: []u8, |
| 1097 |
flags: int = 0 |
| 1098 |
) (void | errno) = { |
| 1099 |
let path = kpath(path)?; |
| 1100 |
static let namebuf: [PATH_MAX]u8 = [0...]; |
| 1101 |
let name = copy_kpath(name, namebuf)?; |
| 1102 |
|
| 1103 |
wrap_return(syscall5(SYS_setxattr, |
| 1104 |
path: uintptr: u64, |
| 1105 |
name: uintptr: u64, |
| 1106 |
value: *[*]u8: uintptr: u64, |
| 1107 |
len(value): u64, |
| 1108 |
flags: u64 |
| 1109 |
))?; |
| 1110 |
}; |
| 1111 |
|
| 1112 |
// Gets an extended file attribute. |
| 1113 |
export fn getxattr(path: path, name: str, value: []u8) (u64 | errno) = { |
| 1114 |
let path = kpath(path)?; |
| 1115 |
static let namebuf: [PATH_MAX]u8 = [0...]; |
| 1116 |
let name = copy_kpath(name, namebuf)?; |
| 1117 |
|
| 1118 |
return wrap_return(syscall4(SYS_getxattr, |
| 1119 |
path: uintptr, |
| 1120 |
name: uintptr, |
| 1121 |
value: *[*]u8: uintptr: u64, |
| 1122 |
len(value): u64, |
| 1123 |
)); |
| 1124 |
}; |
| 1125 |
|
| 1126 |
// Removes an extended file attribute. |
| 1127 |
export fn removexattr(path: path, name: str) (void | errno) = { |
| 1128 |
let path = kpath(path)?; |
| 1129 |
static let namebuf: [PATH_MAX]u8 = [0...]; |
| 1130 |
let name = copy_kpath(name, namebuf)?; |
| 1131 |
|
| 1132 |
wrap_return(syscall2(SYS_removexattr, path: uintptr, name: uintptr))?; |
| 1133 |
}; |
| 1134 |
|
| 1135 |
export fn fsync(fd: int) (void | errno) = { |
| 1136 |
wrap_return(syscall1(SYS_fsync, fd: u64))?; |
| 1137 |
}; |
| 1138 |
|
| 1139 |
export fn fdatasync(fd: int) (void | errno) = { |
| 1140 |
wrap_return(syscall1(SYS_fdatasync, fd: u64))?; |
| 1141 |
}; |
| 1142 |
|
| 1143 |
export fn sigqueueinfo(tgid: pid_t, sig: int, info: *siginfo) (void | errno) = { |
| 1144 |
wrap_return(syscall3(SYS_rt_sigqueueinfo, tgid: u64, |
| 1145 |
sig: u64, info: uintptr: u64))?; |
| 1146 |
}; |
| 1147 |
|
| 1148 |
export fn pidfd_open(pid: pid_t, flags: uint) (int | errno) = { |
| 1149 |
return wrap_return(syscall2(SYS_pidfd_open, |
| 1150 |
pid: u64, flags: u64))?: int; |
| 1151 |
}; |
| 1152 |
|
| 1153 |
export fn pidfd_getfd(pidfd: int, targetfd: int, flags: uint) (int | errno) = { |
| 1154 |
return wrap_return(syscall3(SYS_pidfd_getfd, |
| 1155 |
pidfd: u64, targetfd: u64, flags: u64))?: int; |
| 1156 |
}; |
| 1157 |
|
| 1158 |
export fn pidfd_send_signal( |
| 1159 |
pidfd: int, |
| 1160 |
sig: int, |
| 1161 |
info: nullable *siginfo, |
| 1162 |
flags: uint, |
| 1163 |
) (void | errno) = { |
| 1164 |
wrap_return(syscall4( |
| 1165 |
SYS_pidfd_send_signal, |
| 1166 |
pidfd: u64, |
| 1167 |
sig: u64, |
| 1168 |
info: uintptr: u64, |
| 1169 |
flags: u64, |
| 1170 |
))?; |
| 1171 |
}; |
| 1172 |
|
| 1173 |
export fn clone3(cl_args: *clone_args) (pid_t | errno) = { |
| 1174 |
return wrap_return(syscall2( |
| 1175 |
SYS_clone3, |
| 1176 |
cl_args: uintptr: u64, |
| 1177 |
size(clone_args)))?: pid_t; |
| 1178 |
}; |
| 1179 |
|
| 1180 |
// Scheduling |
| 1181 |
export fn sched_getscheduler(pid: pid_t) (int | errno) = { |
| 1182 |
return wrap_return(syscall1(SYS_sched_getscheduler, |
| 1183 |
pid: u64))?: int; |
| 1184 |
}; |
| 1185 |
|
| 1186 |
export fn sched_setscheduler(pid: pid_t, policy: int, params: *sched_param) (int | errno) = { |
| 1187 |
return wrap_return(syscall3(SYS_sched_setscheduler, |
| 1188 |
pid: u64, policy: u64, params: uintptr: u64))?: int; |
| 1189 |
}; |
| 1190 |
|
| 1191 |
export fn sched_getattr(pid: pid_t, attr: *sched_attr, size_: uint, flags: uint) (void | errno) = { |
| 1192 |
wrap_return(syscall4(SYS_sched_getattr, |
| 1193 |
pid: u64, attr: uintptr: u64, size_: u64, flags: u64))?; |
| 1194 |
}; |
| 1195 |
|
| 1196 |
export fn sched_setattr(pid: pid_t, attr: *sched_attr, flags: uint) (void | errno) = { |
| 1197 |
wrap_return(syscall3(SYS_sched_setattr, |
| 1198 |
pid: u64, attr: uintptr: u64, flags: u64))?; |
| 1199 |
}; |
| 1200 |
|
| 1201 |
export fn sched_yield() void = { |
| 1202 |
syscall0(SYS_sched_yield); |
| 1203 |
}; |