blob b6030fe2 (41081B) - Raw
1 // This file provides the system interface functions for Linux matching those 2 // that are provided by libc, whether or not libc is linked. The following 3 // abstractions are made: 4 // * Work around kernel bugs and limitations. For example, see sendmmsg. 5 // * Implement all the syscalls in the same way that libc functions will 6 // provide `rename` when only the `renameat` syscall exists. 7 // * Does not support POSIX thread cancellation. 8 const std = @import("../std.zig"); 9 const builtin = @import("builtin"); 10 const assert = std.debug.assert; 11 const maxInt = std.math.maxInt; 12 const elf = std.elf; 13 const vdso = @import("linux/vdso.zig"); 14 const dl = @import("../dynamic_library.zig"); 15 16 pub usingnamespace switch (builtin.arch) { 17 .i386 => @import("linux/i386.zig"), 18 .x86_64 => @import("linux/x86_64.zig"), 19 .aarch64 => @import("linux/arm64.zig"), 20 .arm => @import("linux/arm-eabi.zig"), 21 .riscv64 => @import("linux/riscv64.zig"), 22 .mipsel => @import("linux/mipsel.zig"), 23 else => struct {}, 24 }; 25 pub usingnamespace @import("bits.zig"); 26 pub const tls = @import("linux/tls.zig"); 27 28 /// Set by startup code, used by `getauxval`. 29 pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; 30 31 /// See `std.elf` for the constants. 32 pub fn getauxval(index: usize) usize { 33 const auxv = elf_aux_maybe orelse return 0; 34 var i: usize = 0; 35 while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { 36 if (auxv[i].a_type == index) 37 return auxv[i].a_un.a_val; 38 } 39 return 0; 40 } 41 42 /// Get the errno from a syscall return value, or 0 for no error. 43 pub fn getErrno(r: usize) u12 { 44 const signed_r = @bitCast(isize, r); 45 return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; 46 } 47 48 pub fn dup2(old: i32, new: i32) usize { 49 if (@hasDecl(@This(), "SYS_dup2")) { 50 return syscall2(SYS_dup2, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new))); 51 } else { 52 if (old == new) { 53 if (std.debug.runtime_safety) { 54 const rc = syscall2(SYS_fcntl, @bitCast(usize, @as(isize, old)), F_GETFD); 55 if (@bitCast(isize, rc) < 0) return rc; 56 } 57 return @intCast(usize, old); 58 } else { 59 return syscall3(SYS_dup3, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new)), 0); 60 } 61 } 62 } 63 64 pub fn dup3(old: i32, new: i32, flags: u32) usize { 65 return syscall3(SYS_dup3, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new)), flags); 66 } 67 68 pub fn chdir(path: [*:0]const u8) usize { 69 return syscall1(SYS_chdir, @ptrToInt(path)); 70 } 71 72 pub fn chroot(path: [*:0]const u8) usize { 73 return syscall1(SYS_chroot, @ptrToInt(path)); 74 } 75 76 pub fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) usize { 77 return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); 78 } 79 80 pub fn fork() usize { 81 if (@hasDecl(@This(), "SYS_fork")) { 82 return syscall0(SYS_fork); 83 } else { 84 return syscall2(SYS_clone, SIGCHLD, 0); 85 } 86 } 87 88 /// This must be inline, and inline call the syscall function, because if the 89 /// child does a return it will clobber the parent's stack. 90 /// It is advised to avoid this function and use clone instead, because 91 /// the compiler is not aware of how vfork affects control flow and you may 92 /// see different results in optimized builds. 93 pub inline fn vfork() usize { 94 return @call(.{ .modifier = .always_inline }, syscall0, .{SYS_vfork}); 95 } 96 97 pub fn futimens(fd: i32, times: *const [2]timespec) usize { 98 return utimensat(fd, null, times, 0); 99 } 100 101 pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, flags: u32) usize { 102 return syscall4(SYS_utimensat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(times), flags); 103 } 104 105 pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { 106 return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); 107 } 108 109 pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { 110 return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); 111 } 112 113 pub fn getcwd(buf: [*]u8, size: usize) usize { 114 return syscall2(SYS_getcwd, @ptrToInt(buf), size); 115 } 116 117 pub fn getdents(fd: i32, dirp: [*]u8, len: usize) usize { 118 return syscall3( 119 SYS_getdents, 120 @bitCast(usize, @as(isize, fd)), 121 @ptrToInt(dirp), 122 std.math.min(len, maxInt(c_int)), 123 ); 124 } 125 126 pub fn getdents64(fd: i32, dirp: [*]u8, len: usize) usize { 127 return syscall3( 128 SYS_getdents64, 129 @bitCast(usize, @as(isize, fd)), 130 @ptrToInt(dirp), 131 std.math.min(len, maxInt(c_int)), 132 ); 133 } 134 135 pub fn inotify_init1(flags: u32) usize { 136 return syscall1(SYS_inotify_init1, flags); 137 } 138 139 pub fn inotify_add_watch(fd: i32, pathname: [*:0]const u8, mask: u32) usize { 140 return syscall3(SYS_inotify_add_watch, @bitCast(usize, @as(isize, fd)), @ptrToInt(pathname), mask); 141 } 142 143 pub fn inotify_rm_watch(fd: i32, wd: i32) usize { 144 return syscall2(SYS_inotify_rm_watch, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, wd))); 145 } 146 147 pub fn readlink(noalias path: [*:0]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { 148 if (@hasDecl(@This(), "SYS_readlink")) { 149 return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); 150 } else { 151 return syscall4(SYS_readlinkat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); 152 } 153 } 154 155 pub fn readlinkat(dirfd: i32, noalias path: [*:0]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { 156 return syscall4(SYS_readlinkat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); 157 } 158 159 pub fn mkdir(path: [*:0]const u8, mode: u32) usize { 160 if (@hasDecl(@This(), "SYS_mkdir")) { 161 return syscall2(SYS_mkdir, @ptrToInt(path), mode); 162 } else { 163 return syscall3(SYS_mkdirat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(path), mode); 164 } 165 } 166 167 pub fn mkdirat(dirfd: i32, path: [*:0]const u8, mode: u32) usize { 168 return syscall3(SYS_mkdirat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), mode); 169 } 170 171 pub fn mount(special: [*:0]const u8, dir: [*:0]const u8, fstype: [*:0]const u8, flags: u32, data: usize) usize { 172 return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); 173 } 174 175 pub fn umount(special: [*:0]const u8) usize { 176 return syscall2(SYS_umount2, @ptrToInt(special), 0); 177 } 178 179 pub fn umount2(special: [*:0]const u8, flags: u32) usize { 180 return syscall2(SYS_umount2, @ptrToInt(special), flags); 181 } 182 183 pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: u64) usize { 184 if (@hasDecl(@This(), "SYS_mmap2")) { 185 // Make sure the offset is also specified in multiples of page size 186 if ((offset & (MMAP2_UNIT - 1)) != 0) 187 return @bitCast(usize, @as(isize, -EINVAL)); 188 189 return syscall6( 190 SYS_mmap2, 191 @ptrToInt(address), 192 length, 193 prot, 194 flags, 195 @bitCast(usize, @as(isize, fd)), 196 @truncate(usize, offset / MMAP2_UNIT), 197 ); 198 } else { 199 return syscall6( 200 SYS_mmap, 201 @ptrToInt(address), 202 length, 203 prot, 204 flags, 205 @bitCast(usize, @as(isize, fd)), 206 offset, 207 ); 208 } 209 } 210 211 pub fn mprotect(address: [*]const u8, length: usize, protection: usize) usize { 212 return syscall3(SYS_mprotect, @ptrToInt(address), length, protection); 213 } 214 215 pub fn munmap(address: [*]const u8, length: usize) usize { 216 return syscall2(SYS_munmap, @ptrToInt(address), length); 217 } 218 219 pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize { 220 if (@hasDecl(@This(), "SYS_poll")) { 221 return syscall3(SYS_poll, @ptrToInt(fds), n, @bitCast(u32, timeout)); 222 } else { 223 return syscall6( 224 SYS_ppoll, 225 @ptrToInt(fds), 226 n, 227 @ptrToInt(if (timeout >= 0) 228 ×pec{ 229 .tv_sec = @divTrunc(timeout, 1000), 230 .tv_nsec = @rem(timeout, 1000) * 1000000, 231 } 232 else 233 null), 234 0, 235 0, 236 NSIG / 8, 237 ); 238 } 239 } 240 241 pub fn read(fd: i32, buf: [*]u8, count: usize) usize { 242 return syscall3(SYS_read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count); 243 } 244 245 pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { 246 return syscall5( 247 SYS_preadv, 248 @bitCast(usize, @as(isize, fd)), 249 @ptrToInt(iov), 250 count, 251 @truncate(usize, offset), 252 @truncate(usize, offset >> 32), 253 ); 254 } 255 256 pub fn preadv2(fd: i32, iov: [*]const iovec, count: usize, offset: u64, flags: kernel_rwf) usize { 257 return syscall6( 258 SYS_preadv2, 259 @bitCast(usize, @as(isize, fd)), 260 @ptrToInt(iov), 261 count, 262 @truncate(usize, offset), 263 @truncate(usize, offset >> 32), 264 flags, 265 ); 266 } 267 268 pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { 269 return syscall3(SYS_readv, @bitCast(usize, @as(isize, fd)), @ptrToInt(iov), count); 270 } 271 272 pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { 273 return syscall3(SYS_writev, @bitCast(usize, @as(isize, fd)), @ptrToInt(iov), count); 274 } 275 276 pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { 277 return syscall5( 278 SYS_pwritev, 279 @bitCast(usize, @as(isize, fd)), 280 @ptrToInt(iov), 281 count, 282 @truncate(usize, offset), 283 @truncate(usize, offset >> 32), 284 ); 285 } 286 287 pub fn pwritev2(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64, flags: kernel_rwf) usize { 288 return syscall6( 289 SYS_pwritev2, 290 @bitCast(usize, @as(isize, fd)), 291 @ptrToInt(iov), 292 count, 293 @truncate(usize, offset), 294 @truncate(usize, offset >> 32), 295 flags, 296 ); 297 } 298 299 pub fn rmdir(path: [*:0]const u8) usize { 300 if (@hasDecl(@This(), "SYS_rmdir")) { 301 return syscall1(SYS_rmdir, @ptrToInt(path)); 302 } else { 303 return syscall3(SYS_unlinkat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(path), AT_REMOVEDIR); 304 } 305 } 306 307 pub fn symlink(existing: [*:0]const u8, new: [*:0]const u8) usize { 308 if (@hasDecl(@This(), "SYS_symlink")) { 309 return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); 310 } else { 311 return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(new)); 312 } 313 } 314 315 pub fn symlinkat(existing: [*:0]const u8, newfd: i32, newpath: [*:0]const u8) usize { 316 return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, @as(isize, newfd)), @ptrToInt(newpath)); 317 } 318 319 pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { 320 return syscall4(SYS_pread, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count, offset); 321 } 322 323 pub fn access(path: [*:0]const u8, mode: u32) usize { 324 if (@hasDecl(@This(), "SYS_access")) { 325 return syscall2(SYS_access, @ptrToInt(path), mode); 326 } else { 327 return syscall4(SYS_faccessat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(path), mode, 0); 328 } 329 } 330 331 pub fn faccessat(dirfd: i32, path: [*:0]const u8, mode: u32, flags: u32) usize { 332 return syscall4(SYS_faccessat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), mode, flags); 333 } 334 335 pub fn pipe(fd: *[2]i32) usize { 336 if (builtin.arch == .mipsel) { 337 return syscall_pipe(fd); 338 } else if (@hasDecl(@This(), "SYS_pipe")) { 339 return syscall1(SYS_pipe, @ptrToInt(fd)); 340 } else { 341 return syscall2(SYS_pipe2, @ptrToInt(fd), 0); 342 } 343 } 344 345 pub fn pipe2(fd: *[2]i32, flags: u32) usize { 346 return syscall2(SYS_pipe2, @ptrToInt(fd), flags); 347 } 348 349 pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { 350 return syscall3(SYS_write, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count); 351 } 352 353 pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { 354 return syscall4(SYS_pwrite, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count, offset); 355 } 356 357 pub fn rename(old: [*:0]const u8, new: [*:0]const u8) usize { 358 if (@hasDecl(@This(), "SYS_rename")) { 359 return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); 360 } else if (@hasDecl(@This(), "SYS_renameat")) { 361 return syscall4(SYS_renameat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(old), @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(new)); 362 } else { 363 return syscall5(SYS_renameat2, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(old), @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(new), 0); 364 } 365 } 366 367 pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { 368 if (@hasDecl(@This(), "SYS_renameat")) { 369 return syscall4( 370 SYS_renameat, 371 @bitCast(usize, @as(isize, oldfd)), 372 @ptrToInt(old), 373 @bitCast(usize, @as(isize, newfd)), 374 @ptrToInt(new), 375 ); 376 } else { 377 return syscall5( 378 SYS_renameat2, 379 @bitCast(usize, @as(isize, oldfd)), 380 @ptrToInt(old), 381 @bitCast(usize, @as(isize, newfd)), 382 @ptrToInt(new), 383 0, 384 ); 385 } 386 } 387 388 pub fn renameat2(oldfd: i32, oldpath: [*:0]const u8, newfd: i32, newpath: [*:0]const u8, flags: u32) usize { 389 return syscall5( 390 SYS_renameat2, 391 @bitCast(usize, @as(isize, oldfd)), 392 @ptrToInt(oldpath), 393 @bitCast(usize, @as(isize, newfd)), 394 @ptrToInt(newpath), 395 flags, 396 ); 397 } 398 399 pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize { 400 if (@hasDecl(@This(), "SYS_open")) { 401 return syscall3(SYS_open, @ptrToInt(path), flags, perm); 402 } else { 403 return syscall4( 404 SYS_openat, 405 @bitCast(usize, @as(isize, AT_FDCWD)), 406 @ptrToInt(path), 407 flags, 408 perm, 409 ); 410 } 411 } 412 413 pub fn create(path: [*:0]const u8, perm: usize) usize { 414 return syscall2(SYS_creat, @ptrToInt(path), perm); 415 } 416 417 pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: usize) usize { 418 // dirfd could be negative, for example AT_FDCWD is -100 419 return syscall4(SYS_openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode); 420 } 421 422 /// See also `clone` (from the arch-specific include) 423 pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { 424 return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); 425 } 426 427 /// See also `clone` (from the arch-specific include) 428 pub fn clone2(flags: u32, child_stack_ptr: usize) usize { 429 return syscall2(SYS_clone, flags, child_stack_ptr); 430 } 431 432 pub fn close(fd: i32) usize { 433 return syscall1(SYS_close, @bitCast(usize, @as(isize, fd))); 434 } 435 436 /// Can only be called on 32 bit systems. For 64 bit see `lseek`. 437 pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { 438 return syscall5( 439 SYS__llseek, 440 @bitCast(usize, @as(isize, fd)), 441 @truncate(usize, offset >> 32), 442 @truncate(usize, offset), 443 @ptrToInt(result), 444 whence, 445 ); 446 } 447 448 /// Can only be called on 64 bit systems. For 32 bit see `llseek`. 449 pub fn lseek(fd: i32, offset: i64, whence: usize) usize { 450 return syscall3(SYS_lseek, @bitCast(usize, @as(isize, fd)), @bitCast(usize, offset), whence); 451 } 452 453 pub fn exit(status: i32) noreturn { 454 _ = syscall1(SYS_exit, @bitCast(usize, @as(isize, status))); 455 unreachable; 456 } 457 458 pub fn exit_group(status: i32) noreturn { 459 _ = syscall1(SYS_exit_group, @bitCast(usize, @as(isize, status))); 460 unreachable; 461 } 462 463 pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { 464 return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); 465 } 466 467 pub fn kill(pid: pid_t, sig: i32) usize { 468 return syscall2(SYS_kill, @bitCast(usize, @as(isize, pid)), @bitCast(usize, @as(isize, sig))); 469 } 470 471 pub fn tkill(tid: pid_t, sig: i32) usize { 472 return syscall2(SYS_tkill, @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig))); 473 } 474 475 pub fn tgkill(tgid: pid_t, tid: pid_t, sig: i32) usize { 476 return syscall2(SYS_tgkill, @bitCast(usize, @as(isize, tgid)), @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig))); 477 } 478 479 pub fn unlink(path: [*:0]const u8) usize { 480 if (@hasDecl(@This(), "SYS_unlink")) { 481 return syscall1(SYS_unlink, @ptrToInt(path)); 482 } else { 483 return syscall3(SYS_unlinkat, @bitCast(usize, @as(isize, AT_FDCWD)), @ptrToInt(path), 0); 484 } 485 } 486 487 pub fn unlinkat(dirfd: i32, path: [*:0]const u8, flags: u32) usize { 488 return syscall3(SYS_unlinkat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags); 489 } 490 491 pub fn waitpid(pid: pid_t, status: *u32, flags: u32) usize { 492 return syscall4(SYS_wait4, @bitCast(usize, @as(isize, pid)), @ptrToInt(status), flags, 0); 493 } 494 495 var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); 496 497 // We must follow the C calling convention when we call into the VDSO 498 const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; 499 500 pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { 501 if (@hasDecl(@This(), "VDSO_CGT_SYM")) { 502 const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); 503 if (ptr) |fn_ptr| { 504 const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); 505 const rc = f(clk_id, tp); 506 switch (rc) { 507 0, @bitCast(usize, @as(isize, -EINVAL)) => return rc, 508 else => {}, 509 } 510 } 511 } 512 return syscall2(SYS_clock_gettime, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp)); 513 } 514 515 extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { 516 const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); 517 // Note that we may not have a VDSO at all, update the stub address anyway 518 // so that clock_gettime will fall back on the good old (and slow) syscall 519 @atomicStore(?*const c_void, &vdso_clock_gettime, ptr, .Monotonic); 520 // Call into the VDSO if available 521 if (ptr) |fn_ptr| { 522 const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); 523 return f(clk, ts); 524 } 525 return @bitCast(usize, @as(isize, -ENOSYS)); 526 } 527 528 pub fn clock_getres(clk_id: i32, tp: *timespec) usize { 529 return syscall2(SYS_clock_getres, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp)); 530 } 531 532 pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { 533 return syscall2(SYS_clock_settime, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp)); 534 } 535 536 pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { 537 return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); 538 } 539 540 pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { 541 return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); 542 } 543 544 pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { 545 return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); 546 } 547 548 pub fn setuid(uid: u32) usize { 549 if (@hasDecl(@This(), "SYS_setuid32")) { 550 return syscall1(SYS_setuid32, uid); 551 } else { 552 return syscall1(SYS_setuid, uid); 553 } 554 } 555 556 pub fn setgid(gid: u32) usize { 557 if (@hasDecl(@This(), "SYS_setgid32")) { 558 return syscall1(SYS_setgid32, gid); 559 } else { 560 return syscall1(SYS_setgid, gid); 561 } 562 } 563 564 pub fn setreuid(ruid: u32, euid: u32) usize { 565 if (@hasDecl(@This(), "SYS_setreuid32")) { 566 return syscall2(SYS_setreuid32, ruid, euid); 567 } else { 568 return syscall2(SYS_setreuid, ruid, euid); 569 } 570 } 571 572 pub fn setregid(rgid: u32, egid: u32) usize { 573 if (@hasDecl(@This(), "SYS_setregid32")) { 574 return syscall2(SYS_setregid32, rgid, egid); 575 } else { 576 return syscall2(SYS_setregid, rgid, egid); 577 } 578 } 579 580 pub fn getuid() u32 { 581 if (@hasDecl(@This(), "SYS_getuid32")) { 582 return @as(u32, syscall0(SYS_getuid32)); 583 } else { 584 return @as(u32, syscall0(SYS_getuid)); 585 } 586 } 587 588 pub fn getgid() u32 { 589 if (@hasDecl(@This(), "SYS_getgid32")) { 590 return @as(u32, syscall0(SYS_getgid32)); 591 } else { 592 return @as(u32, syscall0(SYS_getgid)); 593 } 594 } 595 596 pub fn geteuid() u32 { 597 if (@hasDecl(@This(), "SYS_geteuid32")) { 598 return @as(u32, syscall0(SYS_geteuid32)); 599 } else { 600 return @as(u32, syscall0(SYS_geteuid)); 601 } 602 } 603 604 pub fn getegid() u32 { 605 if (@hasDecl(@This(), "SYS_getegid32")) { 606 return @as(u32, syscall0(SYS_getegid32)); 607 } else { 608 return @as(u32, syscall0(SYS_getegid)); 609 } 610 } 611 612 pub fn seteuid(euid: u32) usize { 613 return setreuid(std.math.maxInt(u32), euid); 614 } 615 616 pub fn setegid(egid: u32) usize { 617 return setregid(std.math.maxInt(u32), egid); 618 } 619 620 pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { 621 if (@hasDecl(@This(), "SYS_getresuid32")) { 622 return syscall3(SYS_getresuid32, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); 623 } else { 624 return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); 625 } 626 } 627 628 pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { 629 if (@hasDecl(@This(), "SYS_getresgid32")) { 630 return syscall3(SYS_getresgid32, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); 631 } else { 632 return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); 633 } 634 } 635 636 pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { 637 if (@hasDecl(@This(), "SYS_setresuid32")) { 638 return syscall3(SYS_setresuid32, ruid, euid, suid); 639 } else { 640 return syscall3(SYS_setresuid, ruid, euid, suid); 641 } 642 } 643 644 pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { 645 if (@hasDecl(@This(), "SYS_setresgid32")) { 646 return syscall3(SYS_setresgid32, rgid, egid, sgid); 647 } else { 648 return syscall3(SYS_setresgid, rgid, egid, sgid); 649 } 650 } 651 652 pub fn getgroups(size: usize, list: *u32) usize { 653 if (@hasDecl(@This(), "SYS_getgroups32")) { 654 return syscall2(SYS_getgroups32, size, @ptrToInt(list)); 655 } else { 656 return syscall2(SYS_getgroups, size, @ptrToInt(list)); 657 } 658 } 659 660 pub fn setgroups(size: usize, list: *const u32) usize { 661 if (@hasDecl(@This(), "SYS_setgroups32")) { 662 return syscall2(SYS_setgroups32, size, @ptrToInt(list)); 663 } else { 664 return syscall2(SYS_setgroups, size, @ptrToInt(list)); 665 } 666 } 667 668 pub fn getpid() pid_t { 669 return @bitCast(pid_t, @truncate(u32, syscall0(SYS_getpid))); 670 } 671 672 pub fn gettid() pid_t { 673 return @bitCast(pid_t, @truncate(u32, syscall0(SYS_gettid))); 674 } 675 676 pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) usize { 677 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8); 678 } 679 680 pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { 681 assert(sig >= 1); 682 assert(sig != SIGKILL); 683 assert(sig != SIGSTOP); 684 685 const restorer_fn = if ((act.flags & SA_SIGINFO) != 0) restore_rt else restore; 686 var ksa = k_sigaction{ 687 .sigaction = act.sigaction, 688 .flags = act.flags | SA_RESTORER, 689 .mask = undefined, 690 .restorer = @ptrCast(extern fn () void, restorer_fn), 691 }; 692 var ksa_old: k_sigaction = undefined; 693 const ksa_mask_size = @sizeOf(@TypeOf(ksa_old.mask)); 694 @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), ksa_mask_size); 695 const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), ksa_mask_size); 696 const err = getErrno(result); 697 if (err != 0) { 698 return result; 699 } 700 if (oact) |old| { 701 old.sigaction = ksa_old.sigaction; 702 old.flags = @truncate(u32, ksa_old.flags); 703 @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), ksa_mask_size); 704 } 705 return 0; 706 } 707 708 pub fn sigaddset(set: *sigset_t, sig: u6) void { 709 const s = sig - 1; 710 (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); 711 } 712 713 pub fn sigismember(set: *const sigset_t, sig: u6) bool { 714 const s = sig - 1; 715 return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; 716 } 717 718 pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { 719 if (builtin.arch == .i386) { 720 return socketcall(SC_getsockname, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len) }); 721 } 722 return syscall3(SYS_getsockname, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len)); 723 } 724 725 pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { 726 if (builtin.arch == .i386) { 727 return socketcall(SC_getpeername, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len) }); 728 } 729 return syscall3(SYS_getpeername, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len)); 730 } 731 732 pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { 733 if (builtin.arch == .i386) { 734 return socketcall(SC_socket, &[3]usize{ domain, socket_type, protocol }); 735 } 736 return syscall3(SYS_socket, domain, socket_type, protocol); 737 } 738 739 pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { 740 if (builtin.arch == .i386) { 741 return socketcall(SC_setsockopt, &[5]usize{ @bitCast(usize, @as(isize, fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen) }); 742 } 743 return syscall5(SYS_setsockopt, @bitCast(usize, @as(isize, fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); 744 } 745 746 pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { 747 if (builtin.arch == .i386) { 748 return socketcall(SC_getsockopt, &[5]usize{ @bitCast(usize, @as(isize, fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen) }); 749 } 750 return syscall5(SYS_getsockopt, @bitCast(usize, @as(isize, fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); 751 } 752 753 pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { 754 if (builtin.arch == .i386) { 755 return socketcall(SC_sendmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags }); 756 } 757 return syscall3(SYS_sendmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags); 758 } 759 760 pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { 761 if (@typeInfo(usize).Int.bits > @typeInfo(@TypeOf(mmsghdr(undefined).msg_len)).Int.bits) { 762 // workaround kernel brokenness: 763 // if adding up all iov_len overflows a i32 then split into multiple calls 764 // see https://www.openwall.com/lists/musl/2014/06/07/5 765 const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel 766 var next_unsent: usize = 0; 767 for (msgvec[0..kvlen]) |*msg, i| { 768 var size: i32 = 0; 769 const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned 770 for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { 771 if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { 772 // batch-send all messages up to the current message 773 if (next_unsent < i) { 774 const batch_size = i - next_unsent; 775 const r = syscall4(SYS_sendmmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); 776 if (getErrno(r) != 0) return next_unsent; 777 if (r < batch_size) return next_unsent + r; 778 } 779 // send current message as own packet 780 const r = sendmsg(fd, &msg.msg_hdr, flags); 781 if (getErrno(r) != 0) return r; 782 // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. 783 msg.msg_len = @intCast(u32, r); 784 next_unsent = i + 1; 785 break; 786 } 787 } 788 } 789 if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) 790 const batch_size = kvlen - next_unsent; 791 const r = syscall4(SYS_sendmmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); 792 if (getErrno(r) != 0) return r; 793 return next_unsent + r; 794 } 795 return kvlen; 796 } 797 return syscall4(SYS_sendmmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msgvec), vlen, flags); 798 } 799 800 pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { 801 if (builtin.arch == .i386) { 802 return socketcall(SC_connect, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), len }); 803 } 804 return syscall3(SYS_connect, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), len); 805 } 806 807 pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { 808 if (builtin.arch == .i386) { 809 return socketcall(SC_recvmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags }); 810 } 811 return syscall3(SYS_recvmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags); 812 } 813 814 pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { 815 if (builtin.arch == .i386) { 816 return socketcall(SC_recvfrom, &[6]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen) }); 817 } 818 return syscall6(SYS_recvfrom, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); 819 } 820 821 pub fn shutdown(fd: i32, how: i32) usize { 822 if (builtin.arch == .i386) { 823 return socketcall(SC_shutdown, &[2]usize{ @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, how)) }); 824 } 825 return syscall2(SYS_shutdown, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, how))); 826 } 827 828 pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { 829 if (builtin.arch == .i386) { 830 return socketcall(SC_bind, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @intCast(usize, len) }); 831 } 832 return syscall3(SYS_bind, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @intCast(usize, len)); 833 } 834 835 pub fn listen(fd: i32, backlog: u32) usize { 836 if (builtin.arch == .i386) { 837 return socketcall(SC_listen, &[2]usize{ @bitCast(usize, @as(isize, fd)), backlog }); 838 } 839 return syscall2(SYS_listen, @bitCast(usize, @as(isize, fd)), backlog); 840 } 841 842 pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { 843 if (builtin.arch == .i386) { 844 return socketcall(SC_sendto, &[6]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen) }); 845 } 846 return syscall6(SYS_sendto, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); 847 } 848 849 pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { 850 if (builtin.arch == .i386) { 851 return socketcall(SC_socketpair, &[4]usize{ @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0]) }); 852 } 853 return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); 854 } 855 856 pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { 857 if (builtin.arch == .i386) { 858 return socketcall(SC_accept, &[4]usize{ fd, addr, len, 0 }); 859 } 860 return accept4(fd, addr, len, 0); 861 } 862 863 pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { 864 if (builtin.arch == .i386) { 865 return socketcall(SC_accept4, &[4]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len), flags }); 866 } 867 return syscall4(SYS_accept4, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), @ptrToInt(len), flags); 868 } 869 870 pub fn fstat(fd: i32, stat_buf: *Stat) usize { 871 if (@hasDecl(@This(), "SYS_fstat64")) { 872 return syscall2(SYS_fstat64, @bitCast(usize, @as(isize, fd)), @ptrToInt(stat_buf)); 873 } else { 874 return syscall2(SYS_fstat, @bitCast(usize, @as(isize, fd)), @ptrToInt(stat_buf)); 875 } 876 } 877 878 pub fn stat(pathname: [*:0]const u8, statbuf: *Stat) usize { 879 if (@hasDecl(@This(), "SYS_stat64")) { 880 return syscall2(SYS_stat64, @ptrToInt(pathname), @ptrToInt(statbuf)); 881 } else { 882 return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); 883 } 884 } 885 886 pub fn lstat(pathname: [*:0]const u8, statbuf: *Stat) usize { 887 if (@hasDecl(@This(), "SYS_lstat64")) { 888 return syscall2(SYS_lstat64, @ptrToInt(pathname), @ptrToInt(statbuf)); 889 } else { 890 return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); 891 } 892 } 893 894 pub fn fstatat(dirfd: i32, path: [*:0]const u8, stat_buf: *Stat, flags: u32) usize { 895 if (@hasDecl(@This(), "SYS_fstatat64")) { 896 return syscall4(SYS_fstatat64, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); 897 } else { 898 return syscall4(SYS_fstatat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); 899 } 900 } 901 902 pub fn statx(dirfd: i32, path: [*]const u8, flags: u32, mask: u32, statx_buf: *Statx) usize { 903 if (@hasDecl(@This(), "SYS_statx")) { 904 return syscall5( 905 SYS_statx, 906 @bitCast(usize, @as(isize, dirfd)), 907 @ptrToInt(path), 908 flags, 909 mask, 910 @ptrToInt(statx_buf), 911 ); 912 } 913 return @bitCast(usize, @as(isize, -ENOSYS)); 914 } 915 916 pub fn listxattr(path: [*:0]const u8, list: [*]u8, size: usize) usize { 917 return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); 918 } 919 920 pub fn llistxattr(path: [*:0]const u8, list: [*]u8, size: usize) usize { 921 return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); 922 } 923 924 pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { 925 return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); 926 } 927 928 pub fn getxattr(path: [*:0]const u8, name: [*:0]const u8, value: [*]u8, size: usize) usize { 929 return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); 930 } 931 932 pub fn lgetxattr(path: [*:0]const u8, name: [*:0]const u8, value: [*]u8, size: usize) usize { 933 return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); 934 } 935 936 pub fn fgetxattr(fd: usize, name: [*:0]const u8, value: [*]u8, size: usize) usize { 937 return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); 938 } 939 940 pub fn setxattr(path: [*:0]const u8, name: [*:0]const u8, value: *const void, size: usize, flags: usize) usize { 941 return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); 942 } 943 944 pub fn lsetxattr(path: [*:0]const u8, name: [*:0]const u8, value: *const void, size: usize, flags: usize) usize { 945 return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); 946 } 947 948 pub fn fsetxattr(fd: usize, name: [*:0]const u8, value: *const void, size: usize, flags: usize) usize { 949 return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); 950 } 951 952 pub fn removexattr(path: [*:0]const u8, name: [*:0]const u8) usize { 953 return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); 954 } 955 956 pub fn lremovexattr(path: [*:0]const u8, name: [*:0]const u8) usize { 957 return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); 958 } 959 960 pub fn fremovexattr(fd: usize, name: [*:0]const u8) usize { 961 return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); 962 } 963 964 pub fn sched_yield() usize { 965 return syscall0(SYS_sched_yield); 966 } 967 968 pub fn sched_getaffinity(pid: pid_t, size: usize, set: *cpu_set_t) usize { 969 const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, @as(isize, pid)), size, @ptrToInt(set)); 970 if (@bitCast(isize, rc) < 0) return rc; 971 if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc); 972 return 0; 973 } 974 975 pub fn epoll_create() usize { 976 return epoll_create1(0); 977 } 978 979 pub fn epoll_create1(flags: usize) usize { 980 return syscall1(SYS_epoll_create1, flags); 981 } 982 983 pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: ?*epoll_event) usize { 984 return syscall4(SYS_epoll_ctl, @bitCast(usize, @as(isize, epoll_fd)), @intCast(usize, op), @bitCast(usize, @as(isize, fd)), @ptrToInt(ev)); 985 } 986 987 pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { 988 return epoll_pwait(epoll_fd, events, maxevents, timeout, null); 989 } 990 991 pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { 992 return syscall6( 993 SYS_epoll_pwait, 994 @bitCast(usize, @as(isize, epoll_fd)), 995 @ptrToInt(events), 996 @intCast(usize, maxevents), 997 @bitCast(usize, @as(isize, timeout)), 998 @ptrToInt(sigmask), 999 @sizeOf(sigset_t), 1000 ); 1001 } 1002 1003 pub fn eventfd(count: u32, flags: u32) usize { 1004 return syscall2(SYS_eventfd2, count, flags); 1005 } 1006 1007 pub fn timerfd_create(clockid: i32, flags: u32) usize { 1008 return syscall2(SYS_timerfd_create, @bitCast(usize, @as(isize, clockid)), flags); 1009 } 1010 1011 pub const itimerspec = extern struct { 1012 it_interval: timespec, 1013 it_value: timespec, 1014 }; 1015 1016 pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { 1017 return syscall2(SYS_timerfd_gettime, @bitCast(usize, @as(isize, fd)), @ptrToInt(curr_value)); 1018 } 1019 1020 pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { 1021 return syscall4(SYS_timerfd_settime, @bitCast(usize, @as(isize, fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); 1022 } 1023 1024 pub fn unshare(flags: usize) usize { 1025 return syscall1(SYS_unshare, flags); 1026 } 1027 1028 pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { 1029 return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); 1030 } 1031 1032 pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { 1033 return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); 1034 } 1035 1036 pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize { 1037 return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss)); 1038 } 1039 1040 pub fn uname(uts: *utsname) usize { 1041 return syscall1(SYS_uname, @ptrToInt(uts)); 1042 } 1043 1044 // XXX: This should be weak 1045 extern const __ehdr_start: elf.Ehdr = undefined; 1046 1047 pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { 1048 if (builtin.link_libc) { 1049 return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); 1050 } 1051 1052 const elf_base = @ptrToInt(&__ehdr_start); 1053 const n_phdr = __ehdr_start.e_phnum; 1054 const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; 1055 1056 var it = dl.linkmap_iterator(phdrs) catch return 0; 1057 1058 // The executable has no dynamic link segment, create a single entry for 1059 // the whole ELF image 1060 if (it.end()) { 1061 var info = dl_phdr_info{ 1062 .dlpi_addr = elf_base, 1063 .dlpi_name = "/proc/self/exe", 1064 .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), 1065 .dlpi_phnum = __ehdr_start.e_phnum, 1066 }; 1067 1068 return callback(&info, @sizeOf(dl_phdr_info), data); 1069 } 1070 1071 // Last return value from the callback function 1072 var last_r: isize = 0; 1073 while (it.next()) |entry| { 1074 var dlpi_phdr: usize = undefined; 1075 var dlpi_phnum: u16 = undefined; 1076 1077 if (entry.l_addr != 0) { 1078 const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); 1079 dlpi_phdr = entry.l_addr + elf_header.e_phoff; 1080 dlpi_phnum = elf_header.e_phnum; 1081 } else { 1082 // This is the running ELF image 1083 dlpi_phdr = elf_base + __ehdr_start.e_phoff; 1084 dlpi_phnum = __ehdr_start.e_phnum; 1085 } 1086 1087 var info = dl_phdr_info{ 1088 .dlpi_addr = entry.l_addr, 1089 .dlpi_name = entry.l_name, 1090 .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), 1091 .dlpi_phnum = dlpi_phnum, 1092 }; 1093 1094 last_r = callback(&info, @sizeOf(dl_phdr_info), data); 1095 if (last_r != 0) break; 1096 } 1097 1098 return last_r; 1099 } 1100 1101 pub fn io_uring_setup(entries: u32, p: *io_uring_params) usize { 1102 return syscall2(SYS_io_uring_setup, entries, @ptrToInt(p)); 1103 } 1104 1105 pub fn io_uring_enter(fd: i32, to_submit: u32, min_complete: u32, flags: u32, sig: ?*sigset_t) usize { 1106 return syscall6(SYS_io_uring_enter, @bitCast(usize, @as(isize, fd)), to_submit, min_complete, flags, @ptrToInt(sig), NSIG / 8); 1107 } 1108 1109 pub fn io_uring_register(fd: i32, opcode: u32, arg: ?*const c_void, nr_args: u32) usize { 1110 return syscall4(SYS_io_uring_register, @bitCast(usize, @as(isize, fd)), opcode, @ptrToInt(arg), nr_args); 1111 } 1112 1113 pub fn memfd_create(name: [*:0]const u8, flags: u32) usize { 1114 return syscall2(SYS_memfd_create, @ptrToInt(name), flags); 1115 } 1116 1117 test "" { 1118 if (builtin.os == .linux) { 1119 _ = @import("linux/test.zig"); 1120 } 1121 }