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