From 3deda15e21ea3a8138d5b628f2649bbbfe7fa910 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 24 Aug 2021 13:43:41 -0700 Subject: [PATCH] std.os reorganization, avoiding `usingnamespace` The main purpose of this branch is to explore avoiding the `usingnamespace` feature of the zig language, specifically with regards to `std.os` and related functionality. If this experiment is successful, it will provide a data point on whether or not it would be practical to entirely remove `usingnamespace` from the language. In this commit, `usingnamespace` has been completely eliminated from the Linux x86_64 compilation path, aside from io_uring. The behavior tests pass, however that's as far as this branch goes. It is very breaking, and a lot more work is needed before it could be considered mergeable. I wanted to put a pull requset up early so that zig programmers have time to provide feedback. This is progress towards closing #6600 since it clarifies where the actual "owner" of each declaration is, and reduces the number of different ways to import the same declarations. One of the main organizational strategies used here is to do namespacing with real namespaces (e.g. structs) rather than by having declarations share a common prefix (the C strategy). It's no coincidence that `usingnamespace` has similar semantics to `#include` and becomes much less necessary when using proper namespaces. --- CMakeLists.txt | 10 +- lib/std/Thread/Mutex.zig | 4 +- lib/std/Thread/StaticResetEvent.zig | 4 +- lib/std/c.zig | 2 - lib/std/c/darwin.zig | 2 - lib/std/c/wasi.zig | 2 - lib/std/debug.zig | 34 +- lib/std/fs.zig | 76 +- lib/std/fs/file.zig | 16 +- lib/std/heap.zig | 4 +- lib/std/os.zig | 131 +- lib/std/os/bits.zig | 22 - lib/std/os/bits/darwin.zig | 2 - lib/std/os/bits/dragonfly.zig | 2 - lib/std/os/bits/freebsd.zig | 2 - lib/std/os/bits/haiku.zig | 2 - lib/std/os/bits/linux.zig | 2455 ------------ lib/std/os/bits/linux/i386.zig | 271 +- lib/std/os/bits/linux/netlink.zig | 498 --- lib/std/os/bits/linux/prctl.zig | 234 -- lib/std/os/bits/linux/securebits.zig | 35 - lib/std/os/bits/linux/x86_64.zig | 638 --- lib/std/os/bits/linux/xdp.zig | 77 - lib/std/os/bits/netbsd.zig | 2 - lib/std/os/bits/openbsd.zig | 2 - lib/std/os/bits/posix.zig | 28 - lib/std/os/bits/wasi.zig | 11 +- lib/std/os/bits/windows.zig | 5 - lib/std/os/darwin.zig | 3 - lib/std/os/dragonfly.zig | 3 - lib/std/os/freebsd.zig | 3 - lib/std/os/haiku.zig | 3 - lib/std/os/linux.zig | 3405 ++++++++++++++++- lib/std/os/linux/arm-eabi.zig | 2 - lib/std/os/linux/arm64.zig | 2 - lib/std/os/{bits => }/linux/errno/generic.zig | 0 lib/std/os/{bits => }/linux/errno/mips.zig | 0 lib/std/os/{bits => }/linux/errno/sparc.zig | 0 lib/std/os/linux/i386.zig | 2 - lib/std/os/linux/mips.zig | 2 - lib/std/os/linux/powerpc.zig | 2 - lib/std/os/linux/powerpc64.zig | 2 - lib/std/os/linux/riscv64.zig | 2 - lib/std/os/linux/sparc64.zig | 2 - lib/std/os/linux/tls.zig | 6 +- lib/std/os/linux/x86_64.zig | 692 +++- lib/std/os/netbsd.zig | 3 - lib/std/os/openbsd.zig | 3 - lib/std/os/wasi.zig | 2 - lib/std/time.zig | 6 +- 50 files changed, 4445 insertions(+), 4271 deletions(-) delete mode 100644 lib/std/os/bits.zig delete mode 100644 lib/std/os/bits/linux.zig delete mode 100644 lib/std/os/bits/linux/netlink.zig delete mode 100644 lib/std/os/bits/linux/prctl.zig delete mode 100644 lib/std/os/bits/linux/securebits.zig delete mode 100644 lib/std/os/bits/linux/x86_64.zig delete mode 100644 lib/std/os/bits/linux/xdp.zig delete mode 100644 lib/std/os/bits/posix.zig delete mode 100644 lib/std/os/darwin.zig delete mode 100644 lib/std/os/dragonfly.zig delete mode 100644 lib/std/os/freebsd.zig delete mode 100644 lib/std/os/haiku.zig rename lib/std/os/{bits => }/linux/errno/generic.zig (100%) rename lib/std/os/{bits => }/linux/errno/mips.zig (100%) rename lib/std/os/{bits => }/linux/errno/sparc.zig (100%) delete mode 100644 lib/std/os/netbsd.zig delete mode 100644 lib/std/os/openbsd.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cfe9e89e0..7227b04862 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,13 +424,9 @@ set(ZIG_STAGE2_SOURCES "${CMAKE_SOURCE_DIR}/lib/std/meta/trait.zig" "${CMAKE_SOURCE_DIR}/lib/std/multi_array_list.zig" "${CMAKE_SOURCE_DIR}/lib/std/os.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/errno/generic.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/netlink.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/prctl.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/securebits.zig" - "${CMAKE_SOURCE_DIR}/lib/std/os/bits/linux/x86_64.zig" + "${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig" + "${CMAKE_SOURCE_DIR}/lib/std/os/linux/errno/generic.zig" + "${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig" "${CMAKE_SOURCE_DIR}/lib/std/os/linux.zig" "${CMAKE_SOURCE_DIR}/lib/std/os/linux/io_uring.zig" "${CMAKE_SOURCE_DIR}/lib/std/os/linux/x86_64.zig" diff --git a/lib/std/Thread/Mutex.zig b/lib/std/Thread/Mutex.zig index ee54a1582b..a337809a18 100644 --- a/lib/std/Thread/Mutex.zig +++ b/lib/std/Thread/Mutex.zig @@ -133,7 +133,7 @@ pub const AtomicMutex = struct { .linux => { switch (linux.getErrno(linux.futex_wait( @ptrCast(*const i32, &m.state), - linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT, + linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT, @enumToInt(new_state), null, ))) { @@ -155,7 +155,7 @@ pub const AtomicMutex = struct { .linux => { switch (linux.getErrno(linux.futex_wake( @ptrCast(*const i32, &m.state), - linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE, + linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAKE, 1, ))) { .SUCCESS => {}, diff --git a/lib/std/Thread/StaticResetEvent.zig b/lib/std/Thread/StaticResetEvent.zig index d779a4de9e..29fdf3e489 100644 --- a/lib/std/Thread/StaticResetEvent.zig +++ b/lib/std/Thread/StaticResetEvent.zig @@ -194,7 +194,7 @@ pub const AtomicEvent = struct { _ = wake_count; const waiting = std.math.maxInt(i32); // wake_count const ptr = @ptrCast(*const i32, waiters); - const rc = linux.futex_wake(ptr, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, waiting); + const rc = linux.futex_wake(ptr, linux.FUTEX.WAKE | linux.FUTEX.PRIVATE_FLAG, waiting); assert(linux.getErrno(rc) == .SUCCESS); } @@ -213,7 +213,7 @@ pub const AtomicEvent = struct { return; const expected = @intCast(i32, waiting); const ptr = @ptrCast(*const i32, waiters); - const rc = linux.futex_wait(ptr, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, expected, ts_ptr); + const rc = linux.futex_wait(ptr, linux.FUTEX.WAIT | linux.FUTEX.PRIVATE_FLAG, expected, ts_ptr); switch (linux.getErrno(rc)) { .SUCCESS => continue, .TIMEDOUT => return error.TimedOut, diff --git a/lib/std/c.zig b/lib/std/c.zig index 0ad69f873f..f84e76cfb5 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -10,8 +10,6 @@ test { _ = tokenizer; } -pub usingnamespace @import("os/bits.zig"); - pub usingnamespace switch (std.Target.current.os.tag) { .linux => @import("c/linux.zig"), .windows => @import("c/windows.zig"), diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 7fa07719a8..fe074554f4 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -4,8 +4,6 @@ const builtin = @import("builtin"); const macho = std.macho; const native_arch = builtin.target.cpu.arch; -usingnamespace @import("../os/bits.zig"); - extern "c" fn __error() *c_int; pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32; pub extern "c" fn _NSGetExecutablePath(buf: [*:0]u8, bufsize: *u32) c_int; diff --git a/lib/std/c/wasi.zig b/lib/std/c/wasi.zig index 339bdcd127..350231da2e 100644 --- a/lib/std/c/wasi.zig +++ b/lib/std/c/wasi.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../os/bits.zig"); - extern threadlocal var errno: c_int; pub fn _errno() *c_int { diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 4b715f2b14..540586a38f 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -948,8 +948,8 @@ fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 { const mapped_mem = try os.mmap( null, file_len, - os.PROT_READ, - os.MAP_SHARED, + os.PROT.READ, + os.MAP.SHARED, file.handle, 0, ); @@ -1498,12 +1498,12 @@ pub fn attachSegfaultHandler() void { var act = os.Sigaction{ .handler = .{ .sigaction = handleSegfaultLinux }, .mask = os.empty_sigset, - .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND), + .flags = (os.SA.SIGINFO | os.SA.RESTART | os.SA.RESETHAND), }; - os.sigaction(os.SIGSEGV, &act, null); - os.sigaction(os.SIGILL, &act, null); - os.sigaction(os.SIGBUS, &act, null); + os.sigaction(os.SIG.SEGV, &act, null); + os.sigaction(os.SIG.ILL, &act, null); + os.sigaction(os.SIG.BUS, &act, null); } fn resetSegfaultHandler() void { @@ -1515,13 +1515,13 @@ fn resetSegfaultHandler() void { return; } var act = os.Sigaction{ - .handler = .{ .sigaction = os.SIG_DFL }, + .handler = .{ .sigaction = os.SIG.DFL }, .mask = os.empty_sigset, .flags = 0, }; - os.sigaction(os.SIGSEGV, &act, null); - os.sigaction(os.SIGILL, &act, null); - os.sigaction(os.SIGBUS, &act, null); + os.sigaction(os.SIG.SEGV, &act, null); + os.sigaction(os.SIG.ILL, &act, null); + os.sigaction(os.SIG.BUS, &act, null); } fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_void) callconv(.C) noreturn { @@ -1542,9 +1542,9 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v nosuspend { const stderr = io.getStdErr().writer(); _ = switch (sig) { - os.SIGSEGV => stderr.print("Segmentation fault at address 0x{x}\n", .{addr}), - os.SIGILL => stderr.print("Illegal instruction at address 0x{x}\n", .{addr}), - os.SIGBUS => stderr.print("Bus error at address 0x{x}\n", .{addr}), + os.SIG.SEGV => stderr.print("Segmentation fault at address 0x{x}\n", .{addr}), + os.SIG.ILL => stderr.print("Illegal instruction at address 0x{x}\n", .{addr}), + os.SIG.BUS => stderr.print("Bus error at address 0x{x}\n", .{addr}), else => unreachable, } catch os.abort(); } @@ -1552,20 +1552,20 @@ fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const c_v switch (native_arch) { .i386 => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); - const ip = @intCast(usize, ctx.mcontext.gregs[os.REG_EIP]); - const bp = @intCast(usize, ctx.mcontext.gregs[os.REG_EBP]); + const ip = @intCast(usize, ctx.mcontext.gregs[os.REG.EIP]); + const bp = @intCast(usize, ctx.mcontext.gregs[os.REG.EBP]); dumpStackTraceFromBase(bp, ip); }, .x86_64 => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); const ip = switch (native_os) { - .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG_RIP]), + .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.RIP]), .freebsd => @intCast(usize, ctx.mcontext.rip), .openbsd => @intCast(usize, ctx.sc_rip), else => unreachable, }; const bp = switch (native_os) { - .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG_RBP]), + .linux, .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.RBP]), .openbsd => @intCast(usize, ctx.sc_rbp), .freebsd => @intCast(usize, ctx.mcontext.rbp), else => unreachable, diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 1e02c0e805..3caf64b7af 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -907,35 +907,35 @@ pub const Dir = struct { return self.openFileW(path_w.span(), flags); } - var os_flags: u32 = os.O_CLOEXEC; + var os_flags: u32 = os.O.CLOEXEC; // Use the O_ locking flags if the os supports them to acquire the lock // atomically. - const has_flock_open_flags = @hasDecl(os, "O_EXLOCK"); + const has_flock_open_flags = @hasDecl(os.O, "EXLOCK"); if (has_flock_open_flags) { // Note that the O_NONBLOCK flag is removed after the openat() call // is successful. const nonblocking_lock_flag: u32 = if (flags.lock_nonblocking) - os.O_NONBLOCK + os.O.NONBLOCK else 0; os_flags |= switch (flags.lock) { .None => @as(u32, 0), - .Shared => os.O_SHLOCK | nonblocking_lock_flag, - .Exclusive => os.O_EXLOCK | nonblocking_lock_flag, + .Shared => os.O.SHLOCK | nonblocking_lock_flag, + .Exclusive => os.O.EXLOCK | nonblocking_lock_flag, }; } - if (@hasDecl(os, "O_LARGEFILE")) { - os_flags |= os.O_LARGEFILE; + if (@hasDecl(os.O, "LARGEFILE")) { + os_flags |= os.O.LARGEFILE; } if (!flags.allow_ctty) { - os_flags |= os.O_NOCTTY; + os_flags |= os.O.NOCTTY; } os_flags |= if (flags.write and flags.read) - @as(u32, os.O_RDWR) + @as(u32, os.O.RDWR) else if (flags.write) - @as(u32, os.O_WRONLY) + @as(u32, os.O.WRONLY) else - @as(u32, os.O_RDONLY); + @as(u32, os.O.RDONLY); const fd = if (flags.intended_io_mode != .blocking) try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0) else @@ -947,11 +947,11 @@ pub const Dir = struct { if (builtin.target.os.tag != .wasi) { if (!has_flock_open_flags and flags.lock != .None) { // TODO: integrate async I/O - const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0); + const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK.NB else @as(i32, 0); try os.flock(fd, switch (flags.lock) { .None => unreachable, - .Shared => os.LOCK_SH | lock_nonblocking, - .Exclusive => os.LOCK_EX | lock_nonblocking, + .Shared => os.LOCK.SH | lock_nonblocking, + .Exclusive => os.LOCK.EX | lock_nonblocking, }); } } @@ -963,7 +963,7 @@ pub const Dir = struct { error.PermissionDenied => unreachable, else => |e| return e, }; - fl_flags &= ~@as(usize, os.O_NONBLOCK); + fl_flags &= ~@as(usize, os.O.NONBLOCK); _ = os.fcntl(fd, os.F_SETFL, fl_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, @@ -1038,7 +1038,7 @@ pub const Dir = struct { /// Same as `createFile` but WASI only. pub fn createFileWasi(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File { const w = os.wasi; - var oflags = w.O_CREAT; + var oflags = w.O.CREAT; var base: w.rights_t = w.RIGHT_FD_WRITE | w.RIGHT_FD_DATASYNC | w.RIGHT_FD_SEEK | @@ -1054,10 +1054,10 @@ pub const Dir = struct { base |= w.RIGHT_FD_READ; } if (flags.truncate) { - oflags |= w.O_TRUNC; + oflags |= w.O.TRUNC; } if (flags.exclusive) { - oflags |= w.O_EXCL; + oflags |= w.O.EXCL; } const fd = try os.openatWasi(self.fd, sub_path, 0x0, oflags, 0x0, base, 0x0); return File{ .handle = fd }; @@ -1072,24 +1072,24 @@ pub const Dir = struct { // Use the O_ locking flags if the os supports them to acquire the lock // atomically. - const has_flock_open_flags = @hasDecl(os, "O_EXLOCK"); + const has_flock_open_flags = @hasDecl(os.O, "EXLOCK"); // Note that the O_NONBLOCK flag is removed after the openat() call // is successful. const nonblocking_lock_flag: u32 = if (has_flock_open_flags and flags.lock_nonblocking) - os.O_NONBLOCK + os.O.NONBLOCK else 0; const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) { .None => @as(u32, 0), - .Shared => os.O_SHLOCK | nonblocking_lock_flag, - .Exclusive => os.O_EXLOCK | nonblocking_lock_flag, + .Shared => os.O.SHLOCK | nonblocking_lock_flag, + .Exclusive => os.O.EXLOCK | nonblocking_lock_flag, } else 0; - const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0; - const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | os.O_CLOEXEC | - (if (flags.truncate) @as(u32, os.O_TRUNC) else 0) | - (if (flags.read) @as(u32, os.O_RDWR) else os.O_WRONLY) | - (if (flags.exclusive) @as(u32, os.O_EXCL) else 0); + const O_LARGEFILE = if (@hasDecl(os.O, "LARGEFILE")) os.O.LARGEFILE else 0; + const os_flags = lock_flag | O_LARGEFILE | os.O.CREAT | os.O.CLOEXEC | + (if (flags.truncate) @as(u32, os.O.TRUNC) else 0) | + (if (flags.read) @as(u32, os.O.RDWR) else os.O.WRONLY) | + (if (flags.exclusive) @as(u32, os.O.EXCL) else 0); const fd = if (flags.intended_io_mode != .blocking) try std.event.Loop.instance.?.openatZ(self.fd, sub_path_c, os_flags, flags.mode) else @@ -1101,11 +1101,11 @@ pub const Dir = struct { if (builtin.target.os.tag != .wasi) { if (!has_flock_open_flags and flags.lock != .None) { // TODO: integrate async I/O - const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0); + const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK.NB else @as(i32, 0); try os.flock(fd, switch (flags.lock) { .None => unreachable, - .Shared => os.LOCK_SH | lock_nonblocking, - .Exclusive => os.LOCK_EX | lock_nonblocking, + .Shared => os.LOCK.SH | lock_nonblocking, + .Exclusive => os.LOCK.EX | lock_nonblocking, }); } } @@ -1117,7 +1117,7 @@ pub const Dir = struct { error.PermissionDenied => unreachable, else => |e| return e, }; - fl_flags &= ~@as(usize, os.O_NONBLOCK); + fl_flags &= ~@as(usize, os.O.NONBLOCK); _ = os.fcntl(fd, os.F_SETFL, fl_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, @@ -1262,7 +1262,7 @@ pub const Dir = struct { return self.realpathW(pathname_w.span(), out_buffer); } - const flags = if (builtin.os.tag == .linux) os.O_PATH | os.O_NONBLOCK | os.O_CLOEXEC else os.O_NONBLOCK | os.O_CLOEXEC; + const flags = if (builtin.os.tag == .linux) os.O.PATH | os.O.NONBLOCK | os.O.CLOEXEC else os.O.NONBLOCK | os.O.CLOEXEC; const fd = os.openatZ(self.fd, pathname, flags, 0) catch |err| switch (err) { error.FileLocksNotSupported => unreachable, else => |e| return e, @@ -1423,7 +1423,7 @@ pub const Dir = struct { // TODO do we really need all the rights here? const inheriting: w.rights_t = w.RIGHT_ALL ^ w.RIGHT_SOCK_SHUTDOWN; - const result = os.openatWasi(self.fd, sub_path, symlink_flags, w.O_DIRECTORY, 0x0, base, inheriting); + const result = os.openatWasi(self.fd, sub_path, symlink_flags, w.O.DIRECTORY, 0x0, base, inheriting); const fd = result catch |err| switch (err) { error.FileTooBig => unreachable, // can't happen for directories error.IsDir => unreachable, // we're providing O_DIRECTORY @@ -1442,12 +1442,12 @@ pub const Dir = struct { const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c); return self.openDirW(sub_path_w.span().ptr, args); } - const symlink_flags: u32 = if (args.no_follow) os.O_NOFOLLOW else 0x0; + const symlink_flags: u32 = if (args.no_follow) os.O.NOFOLLOW else 0x0; if (!args.iterate) { - const O_PATH = if (@hasDecl(os, "O_PATH")) os.O_PATH else 0; - return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC | O_PATH | symlink_flags); + const O_PATH = if (@hasDecl(os.O, "PATH")) os.O.PATH else 0; + return self.openDirFlagsZ(sub_path_c, os.O.DIRECTORY | os.O.RDONLY | os.O.CLOEXEC | O_PATH | symlink_flags); } else { - return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC | symlink_flags); + return self.openDirFlagsZ(sub_path_c, os.O.DIRECTORY | os.O.RDONLY | os.O.CLOEXEC | symlink_flags); } } @@ -2132,7 +2132,7 @@ pub fn cwd() Dir { } else if (builtin.os.tag == .wasi and !builtin.link_libc) { @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead"); } else { - return Dir{ .fd = os.AT_FDCWD }; + return Dir{ .fd = os.AT.FDCWD }; } } diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 1dd5e8ba98..a0374161d7 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -329,14 +329,14 @@ pub const File = struct { os.FILETYPE_REGULAR_FILE => Kind.File, os.FILETYPE_SOCKET_STREAM, os.FILETYPE_SOCKET_DGRAM => Kind.UnixDomainSocket, else => Kind.Unknown, - } else switch (st.mode & os.S_IFMT) { - os.S_IFBLK => Kind.BlockDevice, - os.S_IFCHR => Kind.CharacterDevice, - os.S_IFDIR => Kind.Directory, - os.S_IFIFO => Kind.NamedPipe, - os.S_IFLNK => Kind.SymLink, - os.S_IFREG => Kind.File, - os.S_IFSOCK => Kind.UnixDomainSocket, + } else switch (st.mode & os.S.IFMT) { + os.S.IFBLK => Kind.BlockDevice, + os.S.IFCHR => Kind.CharacterDevice, + os.S.IFDIR => Kind.Directory, + os.S.IFIFO => Kind.NamedPipe, + os.S.IFLNK => Kind.SymLink, + os.S.IFREG => Kind.File, + os.S.IFSOCK => Kind.UnixDomainSocket, else => Kind.Unknown, }, .atime = @as(i128, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec, diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 9032225b39..55c7b4ee70 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -304,8 +304,8 @@ const PageAllocator = struct { const slice = os.mmap( hint, alloc_len, - os.PROT_READ | os.PROT_WRITE, - os.MAP_PRIVATE | os.MAP_ANONYMOUS, + os.PROT.READ | os.PROT.WRITE, + os.MAP.PRIVATE | os.MAP.ANONYMOUS, -1, 0, ) catch return error.OutOfMemory; diff --git a/lib/std/os.zig b/lib/std/os.zig index 294784c2bd..6b3daed049 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -16,7 +16,7 @@ const root = @import("root"); const std = @import("std.zig"); -const builtin = std.builtin; +const builtin = @import("builtin"); const assert = std.debug.assert; const math = std.math; const mem = std.mem; @@ -24,12 +24,12 @@ const elf = std.elf; const dl = @import("dynamic_library.zig"); const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; -pub const darwin = @import("os/darwin.zig"); -pub const dragonfly = @import("os/dragonfly.zig"); -pub const freebsd = @import("os/freebsd.zig"); -pub const haiku = @import("os/haiku.zig"); -pub const netbsd = @import("os/netbsd.zig"); -pub const openbsd = @import("os/openbsd.zig"); +pub const darwin = std.c; +pub const dragonfly = std.c; +pub const freebsd = std.c; +pub const haiku = std.c; +pub const netbsd = std.c; +pub const openbsd = std.c; pub const linux = @import("os/linux.zig"); pub const uefi = @import("os/uefi.zig"); pub const wasi = @import("os/wasi.zig"); @@ -73,7 +73,102 @@ else switch (builtin.os.tag) { else => struct {}, }; -pub usingnamespace @import("os/bits.zig"); +const bits = switch (builtin.os.tag) { + .macos, .ios, .tvos, .watchos => std.c, + .dragonfly => @import("os/bits/dragonfly.zig"), + .freebsd => @import("os/bits/freebsd.zig"), + .haiku => @import("os/bits/haiku.zig"), + .linux => linux, + .netbsd => @import("os/bits/netbsd.zig"), + .openbsd => @import("os/bits/openbsd.zig"), + .wasi => @import("os/bits/wasi.zig"), + .windows => @import("os/bits/windows.zig"), + else => struct {}, +}; +pub const E = bits.E; +pub const ARCH = bits.ARCH; +pub const Elf_Symndx = bits.Elf_Symndx; +pub const F = bits.F; +pub const Flock = bits.Flock; +pub const LOCK = bits.LOCK; +pub const MAP = bits.MAP; +pub const MMAP2_UNIT = bits.MMAP2_UNIT; +pub const O = bits.O; +pub const REG = bits.REG; +pub const SC = bits.SC; +pub const SYS = bits.SYS; +pub const VDSO = bits.VDSO; +pub const blkcnt_t = bits.blkcnt_t; +pub const blksize_t = bits.blksize_t; +pub const dev_t = bits.dev_t; +pub const ino_t = bits.ino_t; +pub const kernel_stat = bits.kernel_stat; +pub const libc_stat = bits.libc_stat; +pub const mcontext_t = bits.mcontext_t; +pub const mode_t = bits.mode_t; +pub const msghdr = bits.msghdr; +pub const msghdr_const = bits.msghdr_const; +pub const nlink_t = bits.nlink_t; +pub const off_t = bits.off_t; +pub const time_t = bits.time_t; +pub const timespec = bits.timespec; +pub const timeval = bits.timeval; +pub const timezone = bits.timezone; +pub const ucontext_t = bits.ucontext_t; +pub const user_desc = bits.user_desc; +pub const pid_t = bits.pid_t; +pub const fd_t = bits.fd_t; +pub const uid_t = bits.uid_t; +pub const gid_t = bits.gid_t; +pub const clock_t = bits.clock_t; +pub const NAME_MAX = bits.NAME_MAX; +pub const PATH_MAX = bits.PATH_MAX; +pub const IOV_MAX = bits.IOV_MAX; +pub const MAX_ADDR_LEN = bits.MAX_ADDR_LEN; +pub const STDIN_FILENO = bits.STDIN_FILENO; +pub const STDOUT_FILENO = bits.STDIN_FILENO; +pub const STDERR_FILENO = bits.STDIN_FILENO; +pub const AT = bits.AT; +pub const PROT = bits.PROT; +pub const CLOCK = bits.CLOCK; +pub const dl_phdr_info = bits.dl_phdr_info; +pub const Sigaction = bits.Sigaction; +pub const rlimit_resource = bits.rlimit_resource; +pub const SIG = bits.SIG; +pub const rlimit = bits.rlimit; +pub const empty_sigset = bits.empty_sigset; +pub const S = bits.S; +pub const siginfo_t = bits.siginfo_t; +pub const SA = bits.SA; + +pub const iovec = extern struct { + iov_base: [*]u8, + iov_len: usize, +}; + +pub const iovec_const = extern struct { + iov_base: [*]const u8, + iov_len: usize, +}; + +pub const LOG = struct { + /// system is unusable + pub const EMERG = 0; + /// action must be taken immediately + pub const ALERT = 1; + /// critical conditions + pub const CRIT = 2; + /// error conditions + pub const ERR = 3; + /// warning conditions + pub const WARNING = 4; + /// normal but significant condition + pub const NOTICE = 5; + /// informational + pub const INFO = 6; + /// debug-level messages + pub const DEBUG = 7; +}; pub const socket_t = if (builtin.os.tag == .windows) windows.ws2_32.SOCKET else fd_t; @@ -136,7 +231,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void { if (builtin.os.tag == .linux or builtin.os.tag == .freebsd) { var buf = buffer; const use_c = builtin.os.tag != .linux or - std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok; + std.c.versionCheck(std.builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok; while (buf.len != 0) { const res = if (use_c) blk: { @@ -210,11 +305,11 @@ pub fn abort() noreturn { windows.kernel32.ExitProcess(3); } if (!builtin.link_libc and builtin.os.tag == .linux) { - raise(SIGABRT) catch {}; + raise(SIG.ABRT) catch {}; // TODO the rest of the implementation of abort() from musl libc here - raise(SIGKILL) catch {}; + raise(SIG.KILL) catch {}; exit(127); } if (builtin.os.tag == .uefi) { @@ -239,15 +334,15 @@ pub fn raise(sig: u8) RaiseError!void { } if (builtin.os.tag == .linux) { - var set: linux.sigset_t = undefined; + var set: bits.sigset_t = undefined; // block application signals - _ = linux.sigprocmask(SIG_BLOCK, &linux.app_mask, &set); + _ = linux.sigprocmask(SIG.BLOCK, &linux.app_mask, &set); const tid = linux.gettid(); const rc = linux.tkill(tid, sig); // restore signal mask - _ = linux.sigprocmask(SIG_SETMASK, &set, null); + _ = linux.sigprocmask(SIG.SETMASK, &set, null); switch (errno(rc)) { .SUCCESS => return, @@ -2676,7 +2771,7 @@ pub fn isatty(handle: fd_t) bool { while (true) { var wsz: linux.winsize = undefined; const fd = @bitCast(usize, @as(isize, handle)); - const rc = linux.syscall3(.ioctl, fd, linux.TIOCGWINSZ, @ptrToInt(&wsz)); + const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @ptrToInt(&wsz)); switch (linux.getErrno(rc)) { .SUCCESS => return true, .INTR => continue, @@ -4679,7 +4774,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { return; } if (std.Target.current.os.tag == .windows) { - if (clk_id == CLOCK_REALTIME) { + if (clk_id == CLOCK.REALTIME) { var ft: windows.FILETIME = undefined; windows.kernel32.GetSystemTimeAsFileTime(&ft); // FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch. @@ -4691,7 +4786,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { }; return; } else { - // TODO POSIX implementation of CLOCK_MONOTONIC on Windows. + // TODO POSIX implementation of CLOCK.MONOTONIC on Windows. return error.UnsupportedClock; } } @@ -4929,7 +5024,7 @@ pub fn res_mkquery( // Make a reasonably unpredictable id var ts: timespec = undefined; - clock_gettime(CLOCK_REALTIME, &ts) catch {}; + clock_gettime(CLOCK.REALTIME, &ts) catch {}; const UInt = std.meta.Int(.unsigned, std.meta.bitCount(@TypeOf(ts.tv_nsec))); const unsec = @bitCast(UInt, ts.tv_nsec); const id = @truncate(u32, unsec + unsec / 65536); diff --git a/lib/std/os/bits.zig b/lib/std/os/bits.zig deleted file mode 100644 index 36eda7707f..0000000000 --- a/lib/std/os/bits.zig +++ /dev/null @@ -1,22 +0,0 @@ -//! Platform-dependent types and values that are used along with OS-specific APIs. -//! These are imported into `std.c`, `std.os`, and `std.os.linux`. -//! Root source files can define `os.bits` and these will additionally be added -//! to the namespace. - -const std = @import("std"); -const root = @import("root"); - -pub usingnamespace switch (std.Target.current.os.tag) { - .macos, .ios, .tvos, .watchos => @import("bits/darwin.zig"), - .dragonfly => @import("bits/dragonfly.zig"), - .freebsd => @import("bits/freebsd.zig"), - .haiku => @import("bits/haiku.zig"), - .linux => @import("bits/linux.zig"), - .netbsd => @import("bits/netbsd.zig"), - .openbsd => @import("bits/openbsd.zig"), - .wasi => @import("bits/wasi.zig"), - .windows => @import("bits/windows.zig"), - else => struct {}, -}; - -pub usingnamespace if (@hasDecl(root, "os") and @hasDecl(root.os, "bits")) root.os.bits else struct {}; diff --git a/lib/std/os/bits/darwin.zig b/lib/std/os/bits/darwin.zig index a0f5e5a400..e8556a4d89 100644 --- a/lib/std/os/bits/darwin.zig +++ b/lib/std/os/bits/darwin.zig @@ -2,8 +2,6 @@ const std = @import("../../std.zig"); const assert = std.debug.assert; const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - // See: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/sys/_types.h.auto.html // TODO: audit mode_t/pid_t, should likely be u16/i32 pub const fd_t = c_int; diff --git a/lib/std/os/bits/dragonfly.zig b/lib/std/os/bits/dragonfly.zig index 4fe52a6f0a..c7f6e88273 100644 --- a/lib/std/os/bits/dragonfly.zig +++ b/lib/std/os/bits/dragonfly.zig @@ -1,8 +1,6 @@ const std = @import("../../std.zig"); const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - pub fn S_ISCHR(m: u32) bool { return m & S_IFMT == S_IFCHR; } diff --git a/lib/std/os/bits/freebsd.zig b/lib/std/os/bits/freebsd.zig index b4ff21fbf0..2badc0a2ec 100644 --- a/lib/std/os/bits/freebsd.zig +++ b/lib/std/os/bits/freebsd.zig @@ -2,8 +2,6 @@ const std = @import("../../std.zig"); const builtin = @import("builtin"); const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - pub const blksize_t = i32; pub const blkcnt_t = i64; pub const clockid_t = i32; diff --git a/lib/std/os/bits/haiku.zig b/lib/std/os/bits/haiku.zig index 620bf23414..4a2cecf442 100644 --- a/lib/std/os/bits/haiku.zig +++ b/lib/std/os/bits/haiku.zig @@ -1,8 +1,6 @@ const std = @import("../../std.zig"); const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - pub const fd_t = c_int; pub const pid_t = c_int; pub const uid_t = u32; diff --git a/lib/std/os/bits/linux.zig b/lib/std/os/bits/linux.zig deleted file mode 100644 index dfc2b5065c..0000000000 --- a/lib/std/os/bits/linux.zig +++ /dev/null @@ -1,2455 +0,0 @@ -const std = @import("../../std.zig"); -const maxInt = std.math.maxInt; -const arch = @import("builtin").target.cpu.arch; -pub usingnamespace @import("posix.zig"); - -pub const E = switch (arch) { - .mips, .mipsel => @import("linux/errno/mips.zig").E, - .sparc, .sparcel, .sparcv9 => @import("linux/errno/sparc.zig").E, - else => @import("linux/errno/generic.zig").E, -}; - -pub usingnamespace switch (arch) { - .i386 => @import("linux/i386.zig"), - .x86_64 => @import("linux/x86_64.zig"), - .aarch64 => @import("linux/arm64.zig"), - .arm, .thumb => @import("linux/arm-eabi.zig"), - .riscv64 => @import("linux/riscv64.zig"), - .sparcv9 => @import("linux/sparc64.zig"), - .mips, .mipsel => @import("linux/mips.zig"), - .powerpc => @import("linux/powerpc.zig"), - .powerpc64, .powerpc64le => @import("linux/powerpc64.zig"), - else => struct {}, -}; - -pub usingnamespace @import("linux/netlink.zig"); -pub usingnamespace @import("linux/prctl.zig"); -pub usingnamespace @import("linux/securebits.zig"); -pub usingnamespace @import("linux/xdp.zig"); - -const is_mips = arch.isMIPS(); -const is_ppc = arch.isPPC(); -const is_ppc64 = arch.isPPC64(); -const is_sparc = arch.isSPARC(); - -pub const pid_t = i32; -pub const fd_t = i32; -pub const uid_t = u32; -pub const gid_t = u32; -pub const clock_t = isize; - -pub const NAME_MAX = 255; -pub const PATH_MAX = 4096; -pub const IOV_MAX = 1024; - -/// Largest hardware address length -/// e.g. a mac address is a type of hardware address -pub const MAX_ADDR_LEN = 32; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -/// Special value used to indicate openat should use the current working directory -pub const AT_FDCWD = -100; - -/// Do not follow symbolic links -pub const AT_SYMLINK_NOFOLLOW = 0x100; - -/// Remove directory instead of unlinking file -pub const AT_REMOVEDIR = 0x200; - -/// Follow symbolic links. -pub const AT_SYMLINK_FOLLOW = 0x400; - -/// Suppress terminal automount traversal -pub const AT_NO_AUTOMOUNT = 0x800; - -/// Allow empty relative pathname -pub const AT_EMPTY_PATH = 0x1000; - -/// Type of synchronisation required from statx() -pub const AT_STATX_SYNC_TYPE = 0x6000; - -/// - Do whatever stat() does -pub const AT_STATX_SYNC_AS_STAT = 0x0000; - -/// - Force the attributes to be sync'd with the server -pub const AT_STATX_FORCE_SYNC = 0x2000; - -/// - Don't sync attributes with the server -pub const AT_STATX_DONT_SYNC = 0x4000; - -/// Apply to the entire subtree -pub const AT_RECURSIVE = 0x8000; - -/// Default is extend size -pub const FALLOC_FL_KEEP_SIZE = 0x01; - -/// De-allocates range -pub const FALLOC_FL_PUNCH_HOLE = 0x02; - -/// Reserved codepoint -pub const FALLOC_FL_NO_HIDE_STALE = 0x04; - -/// Removes a range of a file without leaving a hole in the file -pub const FALLOC_FL_COLLAPSE_RANGE = 0x08; - -/// Converts a range of file to zeros preferably without issuing data IO -pub const FALLOC_FL_ZERO_RANGE = 0x10; - -/// Inserts space within the file size without overwriting any existing data -pub const FALLOC_FL_INSERT_RANGE = 0x20; - -/// Unshares shared blocks within the file size without overwriting any existing data -pub const FALLOC_FL_UNSHARE_RANGE = 0x40; - -pub const FUTEX_WAIT = 0; -pub const FUTEX_WAKE = 1; -pub const FUTEX_FD = 2; -pub const FUTEX_REQUEUE = 3; -pub const FUTEX_CMP_REQUEUE = 4; -pub const FUTEX_WAKE_OP = 5; -pub const FUTEX_LOCK_PI = 6; -pub const FUTEX_UNLOCK_PI = 7; -pub const FUTEX_TRYLOCK_PI = 8; -pub const FUTEX_WAIT_BITSET = 9; -pub const FUTEX_WAKE_BITSET = 10; -pub const FUTEX_WAIT_REQUEUE_PI = 11; -pub const FUTEX_CMP_REQUEUE_PI = 12; - -pub const FUTEX_PRIVATE_FLAG = 128; - -pub const FUTEX_CLOCK_REALTIME = 256; - -/// page can not be accessed -pub const PROT_NONE = 0x0; - -/// page can be read -pub const PROT_READ = 0x1; - -/// page can be written -pub const PROT_WRITE = 0x2; - -/// page can be executed -pub const PROT_EXEC = 0x4; - -/// page may be used for atomic ops -pub const PROT_SEM = switch (arch) { - // TODO: also xtensa - .mips, .mipsel, .mips64, .mips64el => 0x10, - else => 0x8, -}; - -/// mprotect flag: extend change to start of growsdown vma -pub const PROT_GROWSDOWN = 0x01000000; - -/// mprotect flag: extend change to end of growsup vma -pub const PROT_GROWSUP = 0x02000000; - -/// Share changes -pub const MAP_SHARED = 0x01; - -/// Changes are private -pub const MAP_PRIVATE = 0x02; - -/// share + validate extension flags -pub const MAP_SHARED_VALIDATE = 0x03; - -/// Mask for type of mapping -pub const MAP_TYPE = 0x0f; - -/// Interpret addr exactly -pub const MAP_FIXED = 0x10; - -/// don't use a file -pub const MAP_ANONYMOUS = if (is_mips) 0x800 else 0x20; - -// MAP_ 0x0100 - 0x4000 flags are per architecture - -/// populate (prefault) pagetables -pub const MAP_POPULATE = if (is_mips) 0x10000 else 0x8000; - -/// do not block on IO -pub const MAP_NONBLOCK = if (is_mips) 0x20000 else 0x10000; - -/// give out an address that is best suited for process/thread stacks -pub const MAP_STACK = if (is_mips) 0x40000 else 0x20000; - -/// create a huge page mapping -pub const MAP_HUGETLB = if (is_mips) 0x80000 else 0x40000; - -/// perform synchronous page faults for the mapping -pub const MAP_SYNC = 0x80000; - -/// MAP_FIXED which doesn't unmap underlying mapping -pub const MAP_FIXED_NOREPLACE = 0x100000; - -/// For anonymous mmap, memory could be uninitialized -pub const MAP_UNINITIALIZED = 0x4000000; - -pub const FD_CLOEXEC = 1; - -pub const F_OK = 0; -pub const X_OK = 1; -pub const W_OK = 2; -pub const R_OK = 4; - -pub const WNOHANG = 1; -pub const WUNTRACED = 2; -pub const WSTOPPED = 2; -pub const WEXITED = 4; -pub const WCONTINUED = 8; -pub const WNOWAIT = 0x1000000; - -// waitid id types -pub const P = enum(c_uint) { - ALL = 0, - PID = 1, - PGID = 2, - PIDFD = 3, - _, -}; - -pub usingnamespace if (is_mips) - struct { - pub const SA_NOCLDSTOP = 1; - pub const SA_NOCLDWAIT = 0x10000; - pub const SA_SIGINFO = 8; - pub const SA_RESTART = 0x10000000; - pub const SA_RESETHAND = 0x80000000; - pub const SA_ONSTACK = 0x08000000; - pub const SA_NODEFER = 0x40000000; - pub const SA_RESTORER = 0x04000000; - - pub const SIG_BLOCK = 1; - pub const SIG_UNBLOCK = 2; - pub const SIG_SETMASK = 3; - } -else if (is_sparc) - struct { - pub const SA_NOCLDSTOP = 0x8; - pub const SA_NOCLDWAIT = 0x100; - pub const SA_SIGINFO = 0x200; - pub const SA_RESTART = 0x2; - pub const SA_RESETHAND = 0x4; - pub const SA_ONSTACK = 0x1; - pub const SA_NODEFER = 0x20; - pub const SA_RESTORER = 0x04000000; - - pub const SIG_BLOCK = 1; - pub const SIG_UNBLOCK = 2; - pub const SIG_SETMASK = 4; - } -else - struct { - pub const SA_NOCLDSTOP = 1; - pub const SA_NOCLDWAIT = 2; - pub const SA_SIGINFO = 4; - pub const SA_RESTART = 0x10000000; - pub const SA_RESETHAND = 0x80000000; - pub const SA_ONSTACK = 0x08000000; - pub const SA_NODEFER = 0x40000000; - pub const SA_RESTORER = 0x04000000; - - pub const SIG_BLOCK = 0; - pub const SIG_UNBLOCK = 1; - pub const SIG_SETMASK = 2; - }; - -pub usingnamespace if (is_sparc) struct { - pub const SIGHUP = 1; - pub const SIGINT = 2; - pub const SIGQUIT = 3; - pub const SIGILL = 4; - pub const SIGTRAP = 5; - pub const SIGABRT = 6; - pub const SIGEMT = 7; - pub const SIGFPE = 8; - pub const SIGKILL = 9; - pub const SIGBUS = 10; - pub const SIGSEGV = 11; - pub const SIGSYS = 12; - pub const SIGPIPE = 13; - pub const SIGALRM = 14; - pub const SIGTERM = 15; - pub const SIGURG = 16; - pub const SIGSTOP = 17; - pub const SIGTSTP = 18; - pub const SIGCONT = 19; - pub const SIGCHLD = 20; - pub const SIGTTIN = 21; - pub const SIGTTOU = 22; - pub const SIGPOLL = 23; - pub const SIGXCPU = 24; - pub const SIGXFSZ = 25; - pub const SIGVTALRM = 26; - pub const SIGPROF = 27; - pub const SIGWINCH = 28; - pub const SIGLOST = 29; - pub const SIGUSR1 = 30; - pub const SIGUSR2 = 31; - pub const SIGIOT = SIGABRT; - pub const SIGCLD = SIGCHLD; - pub const SIGPWR = SIGLOST; - pub const SIGIO = SIGPOLL; -} else struct { - pub const SIGHUP = 1; - pub const SIGINT = 2; - pub const SIGQUIT = 3; - pub const SIGILL = 4; - pub const SIGTRAP = 5; - pub const SIGABRT = 6; - pub const SIGIOT = SIGABRT; - pub const SIGBUS = 7; - pub const SIGFPE = 8; - pub const SIGKILL = 9; - pub const SIGUSR1 = 10; - pub const SIGSEGV = 11; - pub const SIGUSR2 = 12; - pub const SIGPIPE = 13; - pub const SIGALRM = 14; - pub const SIGTERM = 15; - pub const SIGSTKFLT = 16; - pub const SIGCHLD = 17; - pub const SIGCONT = 18; - pub const SIGSTOP = 19; - pub const SIGTSTP = 20; - pub const SIGTTIN = 21; - pub const SIGTTOU = 22; - pub const SIGURG = 23; - pub const SIGXCPU = 24; - pub const SIGXFSZ = 25; - pub const SIGVTALRM = 26; - pub const SIGPROF = 27; - pub const SIGWINCH = 28; - pub const SIGIO = 29; - pub const SIGPOLL = 29; - pub const SIGPWR = 30; - pub const SIGSYS = 31; - pub const SIGUNUSED = SIGSYS; -}; - -pub const O_RDONLY = 0o0; -pub const O_WRONLY = 0o1; -pub const O_RDWR = 0o2; - -pub const kernel_rwf = u32; - -/// high priority request, poll if possible -pub const RWF_HIPRI: kernel_rwf = 0x00000001; - -/// per-IO O_DSYNC -pub const RWF_DSYNC: kernel_rwf = 0x00000002; - -/// per-IO O_SYNC -pub const RWF_SYNC: kernel_rwf = 0x00000004; - -/// per-IO, return -EAGAIN if operation would block -pub const RWF_NOWAIT: kernel_rwf = 0x00000008; - -/// per-IO O_APPEND -pub const RWF_APPEND: kernel_rwf = 0x00000010; - -pub const SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -pub const SHUT_RD = 0; -pub const SHUT_WR = 1; -pub const SHUT_RDWR = 2; - -pub const SOCK_STREAM = if (is_mips) 2 else 1; -pub const SOCK_DGRAM = if (is_mips) 1 else 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; -pub const SOCK_DCCP = 6; -pub const SOCK_PACKET = 10; -pub const SOCK_CLOEXEC = 0o2000000; -pub const SOCK_NONBLOCK = if (is_mips) 0o200 else 0o4000; - -pub const PF_UNSPEC = 0; -pub const PF_LOCAL = 1; -pub const PF_UNIX = PF_LOCAL; -pub const PF_FILE = PF_LOCAL; -pub const PF_INET = 2; -pub const PF_AX25 = 3; -pub const PF_IPX = 4; -pub const PF_APPLETALK = 5; -pub const PF_NETROM = 6; -pub const PF_BRIDGE = 7; -pub const PF_ATMPVC = 8; -pub const PF_X25 = 9; -pub const PF_INET6 = 10; -pub const PF_ROSE = 11; -pub const PF_DECnet = 12; -pub const PF_NETBEUI = 13; -pub const PF_SECURITY = 14; -pub const PF_KEY = 15; -pub const PF_NETLINK = 16; -pub const PF_ROUTE = PF_NETLINK; -pub const PF_PACKET = 17; -pub const PF_ASH = 18; -pub const PF_ECONET = 19; -pub const PF_ATMSVC = 20; -pub const PF_RDS = 21; -pub const PF_SNA = 22; -pub const PF_IRDA = 23; -pub const PF_PPPOX = 24; -pub const PF_WANPIPE = 25; -pub const PF_LLC = 26; -pub const PF_IB = 27; -pub const PF_MPLS = 28; -pub const PF_CAN = 29; -pub const PF_TIPC = 30; -pub const PF_BLUETOOTH = 31; -pub const PF_IUCV = 32; -pub const PF_RXRPC = 33; -pub const PF_ISDN = 34; -pub const PF_PHONET = 35; -pub const PF_IEEE802154 = 36; -pub const PF_CAIF = 37; -pub const PF_ALG = 38; -pub const PF_NFC = 39; -pub const PF_VSOCK = 40; -pub const PF_KCM = 41; -pub const PF_QIPCRTR = 42; -pub const PF_SMC = 43; -pub const PF_XDP = 44; -pub const PF_MAX = 45; - -pub const AF_UNSPEC = PF_UNSPEC; -pub const AF_LOCAL = PF_LOCAL; -pub const AF_UNIX = AF_LOCAL; -pub const AF_FILE = AF_LOCAL; -pub const AF_INET = PF_INET; -pub const AF_AX25 = PF_AX25; -pub const AF_IPX = PF_IPX; -pub const AF_APPLETALK = PF_APPLETALK; -pub const AF_NETROM = PF_NETROM; -pub const AF_BRIDGE = PF_BRIDGE; -pub const AF_ATMPVC = PF_ATMPVC; -pub const AF_X25 = PF_X25; -pub const AF_INET6 = PF_INET6; -pub const AF_ROSE = PF_ROSE; -pub const AF_DECnet = PF_DECnet; -pub const AF_NETBEUI = PF_NETBEUI; -pub const AF_SECURITY = PF_SECURITY; -pub const AF_KEY = PF_KEY; -pub const AF_NETLINK = PF_NETLINK; -pub const AF_ROUTE = PF_ROUTE; -pub const AF_PACKET = PF_PACKET; -pub const AF_ASH = PF_ASH; -pub const AF_ECONET = PF_ECONET; -pub const AF_ATMSVC = PF_ATMSVC; -pub const AF_RDS = PF_RDS; -pub const AF_SNA = PF_SNA; -pub const AF_IRDA = PF_IRDA; -pub const AF_PPPOX = PF_PPPOX; -pub const AF_WANPIPE = PF_WANPIPE; -pub const AF_LLC = PF_LLC; -pub const AF_IB = PF_IB; -pub const AF_MPLS = PF_MPLS; -pub const AF_CAN = PF_CAN; -pub const AF_TIPC = PF_TIPC; -pub const AF_BLUETOOTH = PF_BLUETOOTH; -pub const AF_IUCV = PF_IUCV; -pub const AF_RXRPC = PF_RXRPC; -pub const AF_ISDN = PF_ISDN; -pub const AF_PHONET = PF_PHONET; -pub const AF_IEEE802154 = PF_IEEE802154; -pub const AF_CAIF = PF_CAIF; -pub const AF_ALG = PF_ALG; -pub const AF_NFC = PF_NFC; -pub const AF_VSOCK = PF_VSOCK; -pub const AF_KCM = PF_KCM; -pub const AF_QIPCRTR = PF_QIPCRTR; -pub const AF_SMC = PF_SMC; -pub const AF_XDP = PF_XDP; -pub const AF_MAX = PF_MAX; - -pub usingnamespace if (is_mips) - struct {} -else if (is_ppc or is_ppc64) - struct { - pub const SO_DEBUG = 1; - pub const SO_REUSEADDR = 2; - pub const SO_TYPE = 3; - pub const SO_ERROR = 4; - pub const SO_DONTROUTE = 5; - pub const SO_BROADCAST = 6; - pub const SO_SNDBUF = 7; - pub const SO_RCVBUF = 8; - pub const SO_KEEPALIVE = 9; - pub const SO_OOBINLINE = 10; - pub const SO_NO_CHECK = 11; - pub const SO_PRIORITY = 12; - pub const SO_LINGER = 13; - pub const SO_BSDCOMPAT = 14; - pub const SO_REUSEPORT = 15; - pub const SO_RCVLOWAT = 16; - pub const SO_SNDLOWAT = 17; - pub const SO_RCVTIMEO = 18; - pub const SO_SNDTIMEO = 19; - pub const SO_PASSCRED = 20; - pub const SO_PEERCRED = 21; - pub const SO_ACCEPTCONN = 30; - pub const SO_PEERSEC = 31; - pub const SO_SNDBUFFORCE = 32; - pub const SO_RCVBUFFORCE = 33; - pub const SO_PROTOCOL = 38; - pub const SO_DOMAIN = 39; - } -else - struct { - pub const SO_DEBUG = 1; - pub const SO_REUSEADDR = 2; - pub const SO_TYPE = 3; - pub const SO_ERROR = 4; - pub const SO_DONTROUTE = 5; - pub const SO_BROADCAST = 6; - pub const SO_SNDBUF = 7; - pub const SO_RCVBUF = 8; - pub const SO_KEEPALIVE = 9; - pub const SO_OOBINLINE = 10; - pub const SO_NO_CHECK = 11; - pub const SO_PRIORITY = 12; - pub const SO_LINGER = 13; - pub const SO_BSDCOMPAT = 14; - pub const SO_REUSEPORT = 15; - pub const SO_PASSCRED = 16; - pub const SO_PEERCRED = 17; - pub const SO_RCVLOWAT = 18; - pub const SO_SNDLOWAT = 19; - pub const SO_RCVTIMEO = 20; - pub const SO_SNDTIMEO = 21; - pub const SO_ACCEPTCONN = 30; - pub const SO_PEERSEC = 31; - pub const SO_SNDBUFFORCE = 32; - pub const SO_RCVBUFFORCE = 33; - pub const SO_PROTOCOL = 38; - pub const SO_DOMAIN = 39; - }; - -pub const SO_SECURITY_AUTHENTICATION = 22; -pub const SO_SECURITY_ENCRYPTION_TRANSPORT = 23; -pub const SO_SECURITY_ENCRYPTION_NETWORK = 24; - -pub const SO_BINDTODEVICE = 25; - -pub const SO_ATTACH_FILTER = 26; -pub const SO_DETACH_FILTER = 27; -pub const SO_GET_FILTER = SO_ATTACH_FILTER; - -pub const SO_PEERNAME = 28; -pub const SO_TIMESTAMP_OLD = 29; -pub const SO_PASSSEC = 34; -pub const SO_TIMESTAMPNS_OLD = 35; -pub const SO_MARK = 36; -pub const SO_TIMESTAMPING_OLD = 37; - -pub const SO_RXQ_OVFL = 40; -pub const SO_WIFI_STATUS = 41; -pub const SCM_WIFI_STATUS = SO_WIFI_STATUS; -pub const SO_PEEK_OFF = 42; -pub const SO_NOFCS = 43; -pub const SO_LOCK_FILTER = 44; -pub const SO_SELECT_ERR_QUEUE = 45; -pub const SO_BUSY_POLL = 46; -pub const SO_MAX_PACING_RATE = 47; -pub const SO_BPF_EXTENSIONS = 48; -pub const SO_INCOMING_CPU = 49; -pub const SO_ATTACH_BPF = 50; -pub const SO_DETACH_BPF = SO_DETACH_FILTER; -pub const SO_ATTACH_REUSEPORT_CBPF = 51; -pub const SO_ATTACH_REUSEPORT_EBPF = 52; -pub const SO_CNX_ADVICE = 53; -pub const SCM_TIMESTAMPING_OPT_STATS = 54; -pub const SO_MEMINFO = 55; -pub const SO_INCOMING_NAPI_ID = 56; -pub const SO_COOKIE = 57; -pub const SCM_TIMESTAMPING_PKTINFO = 58; -pub const SO_PEERGROUPS = 59; -pub const SO_ZEROCOPY = 60; -pub const SO_TXTIME = 61; -pub const SCM_TXTIME = SO_TXTIME; -pub const SO_BINDTOIFINDEX = 62; -pub const SO_TIMESTAMP_NEW = 63; -pub const SO_TIMESTAMPNS_NEW = 64; -pub const SO_TIMESTAMPING_NEW = 65; -pub const SO_RCVTIMEO_NEW = 66; -pub const SO_SNDTIMEO_NEW = 67; -pub const SO_DETACH_REUSEPORT_BPF = 68; - -pub const SOL_SOCKET = if (is_mips) 65535 else 1; - -pub const SOL_IP = 0; -pub const SOL_IPV6 = 41; -pub const SOL_ICMPV6 = 58; - -pub const SOL_RAW = 255; -pub const SOL_DECNET = 261; -pub const SOL_X25 = 262; -pub const SOL_PACKET = 263; -pub const SOL_ATM = 264; -pub const SOL_AAL = 265; -pub const SOL_IRDA = 266; -pub const SOL_NETBEUI = 267; -pub const SOL_LLC = 268; -pub const SOL_DCCP = 269; -pub const SOL_NETLINK = 270; -pub const SOL_TIPC = 271; -pub const SOL_RXRPC = 272; -pub const SOL_PPPOL2TP = 273; -pub const SOL_BLUETOOTH = 274; -pub const SOL_PNPIPE = 275; -pub const SOL_RDS = 276; -pub const SOL_IUCV = 277; -pub const SOL_CAIF = 278; -pub const SOL_ALG = 279; -pub const SOL_NFC = 280; -pub const SOL_KCM = 281; -pub const SOL_TLS = 282; -pub const SOL_XDP = 283; - -pub const SOMAXCONN = 128; - -pub const IP_TOS = 1; -pub const IP_TTL = 2; -pub const IP_HDRINCL = 3; -pub const IP_OPTIONS = 4; -pub const IP_ROUTER_ALERT = 5; -pub const IP_RECVOPTS = 6; -pub const IP_RETOPTS = 7; -pub const IP_PKTINFO = 8; -pub const IP_PKTOPTIONS = 9; -pub const IP_PMTUDISC = 10; -pub const IP_MTU_DISCOVER = 10; -pub const IP_RECVERR = 11; -pub const IP_RECVTTL = 12; -pub const IP_RECVTOS = 13; -pub const IP_MTU = 14; -pub const IP_FREEBIND = 15; -pub const IP_IPSEC_POLICY = 16; -pub const IP_XFRM_POLICY = 17; -pub const IP_PASSSEC = 18; -pub const IP_TRANSPARENT = 19; -pub const IP_ORIGDSTADDR = 20; -pub const IP_RECVORIGDSTADDR = IP_ORIGDSTADDR; -pub const IP_MINTTL = 21; -pub const IP_NODEFRAG = 22; -pub const IP_CHECKSUM = 23; -pub const IP_BIND_ADDRESS_NO_PORT = 24; -pub const IP_RECVFRAGSIZE = 25; -pub const IP_MULTICAST_IF = 32; -pub const IP_MULTICAST_TTL = 33; -pub const IP_MULTICAST_LOOP = 34; -pub const IP_ADD_MEMBERSHIP = 35; -pub const IP_DROP_MEMBERSHIP = 36; -pub const IP_UNBLOCK_SOURCE = 37; -pub const IP_BLOCK_SOURCE = 38; -pub const IP_ADD_SOURCE_MEMBERSHIP = 39; -pub const IP_DROP_SOURCE_MEMBERSHIP = 40; -pub const IP_MSFILTER = 41; -pub const IP_MULTICAST_ALL = 49; -pub const IP_UNICAST_IF = 50; - -pub const IP_RECVRETOPTS = IP_RETOPTS; - -pub const IP_PMTUDISC_DONT = 0; -pub const IP_PMTUDISC_WANT = 1; -pub const IP_PMTUDISC_DO = 2; -pub const IP_PMTUDISC_PROBE = 3; -pub const IP_PMTUDISC_INTERFACE = 4; -pub const IP_PMTUDISC_OMIT = 5; - -pub const IP_DEFAULT_MULTICAST_TTL = 1; -pub const IP_DEFAULT_MULTICAST_LOOP = 1; -pub const IP_MAX_MEMBERSHIPS = 20; - -// IPv6 socket options - -pub const IPV6_ADDRFORM = 1; -pub const IPV6_2292PKTINFO = 2; -pub const IPV6_2292HOPOPTS = 3; -pub const IPV6_2292DSTOPTS = 4; -pub const IPV6_2292RTHDR = 5; -pub const IPV6_2292PKTOPTIONS = 6; -pub const IPV6_CHECKSUM = 7; -pub const IPV6_2292HOPLIMIT = 8; -pub const IPV6_NEXTHOP = 9; -pub const IPV6_AUTHHDR = 10; -pub const IPV6_FLOWINFO = 11; - -pub const IPV6_UNICAST_HOPS = 16; -pub const IPV6_MULTICAST_IF = 17; -pub const IPV6_MULTICAST_HOPS = 18; -pub const IPV6_MULTICAST_LOOP = 19; -pub const IPV6_ADD_MEMBERSHIP = 20; -pub const IPV6_DROP_MEMBERSHIP = 21; -pub const IPV6_ROUTER_ALERT = 22; -pub const IPV6_MTU_DISCOVER = 23; -pub const IPV6_MTU = 24; -pub const IPV6_RECVERR = 25; -pub const IPV6_V6ONLY = 26; -pub const IPV6_JOIN_ANYCAST = 27; -pub const IPV6_LEAVE_ANYCAST = 28; - -// IPV6_MTU_DISCOVER values -pub const IPV6_PMTUDISC_DONT = 0; -pub const IPV6_PMTUDISC_WANT = 1; -pub const IPV6_PMTUDISC_DO = 2; -pub const IPV6_PMTUDISC_PROBE = 3; -pub const IPV6_PMTUDISC_INTERFACE = 4; -pub const IPV6_PMTUDISC_OMIT = 5; - -// Flowlabel -pub const IPV6_FLOWLABEL_MGR = 32; -pub const IPV6_FLOWINFO_SEND = 33; -pub const IPV6_IPSEC_POLICY = 34; -pub const IPV6_XFRM_POLICY = 35; -pub const IPV6_HDRINCL = 36; - -// Advanced API (RFC3542) (1) -pub const IPV6_RECVPKTINFO = 49; -pub const IPV6_PKTINFO = 50; -pub const IPV6_RECVHOPLIMIT = 51; -pub const IPV6_HOPLIMIT = 52; -pub const IPV6_RECVHOPOPTS = 53; -pub const IPV6_HOPOPTS = 54; -pub const IPV6_RTHDRDSTOPTS = 55; -pub const IPV6_RECVRTHDR = 56; -pub const IPV6_RTHDR = 57; -pub const IPV6_RECVDSTOPTS = 58; -pub const IPV6_DSTOPTS = 59; -pub const IPV6_RECVPATHMTU = 60; -pub const IPV6_PATHMTU = 61; -pub const IPV6_DONTFRAG = 62; - -// Advanced API (RFC3542) (2) -pub const IPV6_RECVTCLASS = 66; -pub const IPV6_TCLASS = 67; - -pub const IPV6_AUTOFLOWLABEL = 70; - -// RFC5014: Source address selection -pub const IPV6_ADDR_PREFERENCES = 72; - -pub const IPV6_PREFER_SRC_TMP = 0x0001; -pub const IPV6_PREFER_SRC_PUBLIC = 0x0002; -pub const IPV6_PREFER_SRC_PUBTMP_DEFAULT = 0x0100; -pub const IPV6_PREFER_SRC_COA = 0x0004; -pub const IPV6_PREFER_SRC_HOME = 0x0400; -pub const IPV6_PREFER_SRC_CGA = 0x0008; -pub const IPV6_PREFER_SRC_NONCGA = 0x0800; - -// RFC5082: Generalized Ttl Security Mechanism -pub const IPV6_MINHOPCOUNT = 73; - -pub const IPV6_ORIGDSTADDR = 74; -pub const IPV6_RECVORIGDSTADDR = IPV6_ORIGDSTADDR; -pub const IPV6_TRANSPARENT = 75; -pub const IPV6_UNICAST_IF = 76; -pub const IPV6_RECVFRAGSIZE = 77; -pub const IPV6_FREEBIND = 78; - -pub const MSG_OOB = 0x0001; -pub const MSG_PEEK = 0x0002; -pub const MSG_DONTROUTE = 0x0004; -pub const MSG_CTRUNC = 0x0008; -pub const MSG_PROXY = 0x0010; -pub const MSG_TRUNC = 0x0020; -pub const MSG_DONTWAIT = 0x0040; -pub const MSG_EOR = 0x0080; -pub const MSG_WAITALL = 0x0100; -pub const MSG_FIN = 0x0200; -pub const MSG_SYN = 0x0400; -pub const MSG_CONFIRM = 0x0800; -pub const MSG_RST = 0x1000; -pub const MSG_ERRQUEUE = 0x2000; -pub const MSG_NOSIGNAL = 0x4000; -pub const MSG_MORE = 0x8000; -pub const MSG_WAITFORONE = 0x10000; -pub const MSG_BATCH = 0x40000; -pub const MSG_ZEROCOPY = 0x4000000; -pub const MSG_FASTOPEN = 0x20000000; -pub const MSG_CMSG_CLOEXEC = 0x40000000; - -pub const DT_UNKNOWN = 0; -pub const DT_FIFO = 1; -pub const DT_CHR = 2; -pub const DT_DIR = 4; -pub const DT_BLK = 6; -pub const DT_REG = 8; -pub const DT_LNK = 10; -pub const DT_SOCK = 12; -pub const DT_WHT = 14; - -pub const TCGETS = if (is_mips) 0x540D else 0x5401; -pub const TCSETS = 0x5402; -pub const TCSETSW = 0x5403; -pub const TCSETSF = 0x5404; -pub const TCGETA = 0x5405; -pub const TCSETA = 0x5406; -pub const TCSETAW = 0x5407; -pub const TCSETAF = 0x5408; -pub const TCSBRK = 0x5409; -pub const TCXONC = 0x540A; -pub const TCFLSH = 0x540B; -pub const TIOCEXCL = 0x540C; -pub const TIOCNXCL = 0x540D; -pub const TIOCSCTTY = 0x540E; -pub const TIOCGPGRP = 0x540F; -pub const TIOCSPGRP = 0x5410; -pub const TIOCOUTQ = if (is_mips) 0x7472 else 0x5411; -pub const TIOCSTI = 0x5412; -pub const TIOCGWINSZ = if (is_mips or is_ppc64) 0x40087468 else 0x5413; -pub const TIOCSWINSZ = if (is_mips or is_ppc64) 0x80087467 else 0x5414; -pub const TIOCMGET = 0x5415; -pub const TIOCMBIS = 0x5416; -pub const TIOCMBIC = 0x5417; -pub const TIOCMSET = 0x5418; -pub const TIOCGSOFTCAR = 0x5419; -pub const TIOCSSOFTCAR = 0x541A; -pub const FIONREAD = if (is_mips) 0x467F else 0x541B; -pub const TIOCINQ = FIONREAD; -pub const TIOCLINUX = 0x541C; -pub const TIOCCONS = 0x541D; -pub const TIOCGSERIAL = 0x541E; -pub const TIOCSSERIAL = 0x541F; -pub const TIOCPKT = 0x5420; -pub const FIONBIO = 0x5421; -pub const TIOCNOTTY = 0x5422; -pub const TIOCSETD = 0x5423; -pub const TIOCGETD = 0x5424; -pub const TCSBRKP = 0x5425; -pub const TIOCSBRK = 0x5427; -pub const TIOCCBRK = 0x5428; -pub const TIOCGSID = 0x5429; -pub const TIOCGRS485 = 0x542E; -pub const TIOCSRS485 = 0x542F; -pub const TIOCGPTN = 0x80045430; -pub const TIOCSPTLCK = 0x40045431; -pub const TIOCGDEV = 0x80045432; -pub const TCGETX = 0x5432; -pub const TCSETX = 0x5433; -pub const TCSETXF = 0x5434; -pub const TCSETXW = 0x5435; -pub const TIOCSIG = 0x40045436; -pub const TIOCVHANGUP = 0x5437; -pub const TIOCGPKT = 0x80045438; -pub const TIOCGPTLCK = 0x80045439; -pub const TIOCGEXCL = 0x80045440; - -pub const EPOLL_CLOEXEC = O_CLOEXEC; - -pub const EPOLL_CTL_ADD = 1; -pub const EPOLL_CTL_DEL = 2; -pub const EPOLL_CTL_MOD = 3; - -pub const EPOLLIN = 0x001; -pub const EPOLLPRI = 0x002; -pub const EPOLLOUT = 0x004; -pub const EPOLLRDNORM = 0x040; -pub const EPOLLRDBAND = 0x080; -pub const EPOLLWRNORM = if (is_mips) 0x004 else 0x100; -pub const EPOLLWRBAND = if (is_mips) 0x100 else 0x200; -pub const EPOLLMSG = 0x400; -pub const EPOLLERR = 0x008; -pub const EPOLLHUP = 0x010; -pub const EPOLLRDHUP = 0x2000; -pub const EPOLLEXCLUSIVE = (@as(u32, 1) << 28); -pub const EPOLLWAKEUP = (@as(u32, 1) << 29); -pub const EPOLLONESHOT = (@as(u32, 1) << 30); -pub const EPOLLET = (@as(u32, 1) << 31); - -pub const CLOCK_REALTIME = 0; -pub const CLOCK_MONOTONIC = 1; -pub const CLOCK_PROCESS_CPUTIME_ID = 2; -pub const CLOCK_THREAD_CPUTIME_ID = 3; -pub const CLOCK_MONOTONIC_RAW = 4; -pub const CLOCK_REALTIME_COARSE = 5; -pub const CLOCK_MONOTONIC_COARSE = 6; -pub const CLOCK_BOOTTIME = 7; -pub const CLOCK_REALTIME_ALARM = 8; -pub const CLOCK_BOOTTIME_ALARM = 9; -pub const CLOCK_SGI_CYCLE = 10; -pub const CLOCK_TAI = 11; - -pub const CSIGNAL = 0x000000ff; -pub const CLONE_VM = 0x00000100; -pub const CLONE_FS = 0x00000200; -pub const CLONE_FILES = 0x00000400; -pub const CLONE_SIGHAND = 0x00000800; -pub const CLONE_PIDFD = 0x00001000; -pub const CLONE_PTRACE = 0x00002000; -pub const CLONE_VFORK = 0x00004000; -pub const CLONE_PARENT = 0x00008000; -pub const CLONE_THREAD = 0x00010000; -pub const CLONE_NEWNS = 0x00020000; -pub const CLONE_SYSVSEM = 0x00040000; -pub const CLONE_SETTLS = 0x00080000; -pub const CLONE_PARENT_SETTID = 0x00100000; -pub const CLONE_CHILD_CLEARTID = 0x00200000; -pub const CLONE_DETACHED = 0x00400000; -pub const CLONE_UNTRACED = 0x00800000; -pub const CLONE_CHILD_SETTID = 0x01000000; -pub const CLONE_NEWCGROUP = 0x02000000; -pub const CLONE_NEWUTS = 0x04000000; -pub const CLONE_NEWIPC = 0x08000000; -pub const CLONE_NEWUSER = 0x10000000; -pub const CLONE_NEWPID = 0x20000000; -pub const CLONE_NEWNET = 0x40000000; -pub const CLONE_IO = 0x80000000; - -// Flags for the clone3() syscall. - -/// Clear any signal handler and reset to SIG_DFL. -pub const CLONE_CLEAR_SIGHAND = 0x100000000; -/// Clone into a specific cgroup given the right permissions. -pub const CLONE_INTO_CGROUP = 0x200000000; - -// cloning flags intersect with CSIGNAL so can be used with unshare and clone3 syscalls only. - -/// New time namespace -pub const CLONE_NEWTIME = 0x00000080; - -pub const EFD_SEMAPHORE = 1; -pub const EFD_CLOEXEC = O_CLOEXEC; -pub const EFD_NONBLOCK = O_NONBLOCK; - -pub const MS_RDONLY = 1; -pub const MS_NOSUID = 2; -pub const MS_NODEV = 4; -pub const MS_NOEXEC = 8; -pub const MS_SYNCHRONOUS = 16; -pub const MS_REMOUNT = 32; -pub const MS_MANDLOCK = 64; -pub const MS_DIRSYNC = 128; -pub const MS_NOATIME = 1024; -pub const MS_NODIRATIME = 2048; -pub const MS_BIND = 4096; -pub const MS_MOVE = 8192; -pub const MS_REC = 16384; -pub const MS_SILENT = 32768; -pub const MS_POSIXACL = (1 << 16); -pub const MS_UNBINDABLE = (1 << 17); -pub const MS_PRIVATE = (1 << 18); -pub const MS_SLAVE = (1 << 19); -pub const MS_SHARED = (1 << 20); -pub const MS_RELATIME = (1 << 21); -pub const MS_KERNMOUNT = (1 << 22); -pub const MS_I_VERSION = (1 << 23); -pub const MS_STRICTATIME = (1 << 24); -pub const MS_LAZYTIME = (1 << 25); -pub const MS_NOREMOTELOCK = (1 << 27); -pub const MS_NOSEC = (1 << 28); -pub const MS_BORN = (1 << 29); -pub const MS_ACTIVE = (1 << 30); -pub const MS_NOUSER = (1 << 31); - -pub const MS_RMT_MASK = (MS_RDONLY | MS_SYNCHRONOUS | MS_MANDLOCK | MS_I_VERSION | MS_LAZYTIME); - -pub const MS_MGC_VAL = 0xc0ed0000; -pub const MS_MGC_MSK = 0xffff0000; - -pub const MNT_FORCE = 1; -pub const MNT_DETACH = 2; -pub const MNT_EXPIRE = 4; -pub const UMOUNT_NOFOLLOW = 8; - -pub const IN_CLOEXEC = O_CLOEXEC; -pub const IN_NONBLOCK = O_NONBLOCK; - -pub const IN_ACCESS = 0x00000001; -pub const IN_MODIFY = 0x00000002; -pub const IN_ATTRIB = 0x00000004; -pub const IN_CLOSE_WRITE = 0x00000008; -pub const IN_CLOSE_NOWRITE = 0x00000010; -pub const IN_CLOSE = IN_CLOSE_WRITE | IN_CLOSE_NOWRITE; -pub const IN_OPEN = 0x00000020; -pub const IN_MOVED_FROM = 0x00000040; -pub const IN_MOVED_TO = 0x00000080; -pub const IN_MOVE = IN_MOVED_FROM | IN_MOVED_TO; -pub const IN_CREATE = 0x00000100; -pub const IN_DELETE = 0x00000200; -pub const IN_DELETE_SELF = 0x00000400; -pub const IN_MOVE_SELF = 0x00000800; -pub const IN_ALL_EVENTS = 0x00000fff; - -pub const IN_UNMOUNT = 0x00002000; -pub const IN_Q_OVERFLOW = 0x00004000; -pub const IN_IGNORED = 0x00008000; - -pub const IN_ONLYDIR = 0x01000000; -pub const IN_DONT_FOLLOW = 0x02000000; -pub const IN_EXCL_UNLINK = 0x04000000; -pub const IN_MASK_ADD = 0x20000000; - -pub const IN_ISDIR = 0x40000000; -pub const IN_ONESHOT = 0x80000000; - -pub const S_IFMT = 0o170000; - -pub const S_IFDIR = 0o040000; -pub const S_IFCHR = 0o020000; -pub const S_IFBLK = 0o060000; -pub const S_IFREG = 0o100000; -pub const S_IFIFO = 0o010000; -pub const S_IFLNK = 0o120000; -pub const S_IFSOCK = 0o140000; - -pub const S_ISUID = 0o4000; -pub const S_ISGID = 0o2000; -pub const S_ISVTX = 0o1000; -pub const S_IRUSR = 0o400; -pub const S_IWUSR = 0o200; -pub const S_IXUSR = 0o100; -pub const S_IRWXU = 0o700; -pub const S_IRGRP = 0o040; -pub const S_IWGRP = 0o020; -pub const S_IXGRP = 0o010; -pub const S_IRWXG = 0o070; -pub const S_IROTH = 0o004; -pub const S_IWOTH = 0o002; -pub const S_IXOTH = 0o001; -pub const S_IRWXO = 0o007; - -pub fn S_ISREG(m: u32) bool { - return m & S_IFMT == S_IFREG; -} - -pub fn S_ISDIR(m: u32) bool { - return m & S_IFMT == S_IFDIR; -} - -pub fn S_ISCHR(m: u32) bool { - return m & S_IFMT == S_IFCHR; -} - -pub fn S_ISBLK(m: u32) bool { - return m & S_IFMT == S_IFBLK; -} - -pub fn S_ISFIFO(m: u32) bool { - return m & S_IFMT == S_IFIFO; -} - -pub fn S_ISLNK(m: u32) bool { - return m & S_IFMT == S_IFLNK; -} - -pub fn S_ISSOCK(m: u32) bool { - return m & S_IFMT == S_IFSOCK; -} - -pub const UTIME_NOW = 0x3fffffff; -pub const UTIME_OMIT = 0x3ffffffe; - -pub const TFD_NONBLOCK = O_NONBLOCK; -pub const TFD_CLOEXEC = O_CLOEXEC; - -pub const TFD_TIMER_ABSTIME = 1; -pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1); - -pub fn WEXITSTATUS(s: u32) u8 { - return @intCast(u8, (s & 0xff00) >> 8); -} -pub fn WTERMSIG(s: u32) u32 { - return s & 0x7f; -} -pub fn WSTOPSIG(s: u32) u32 { - return WEXITSTATUS(s); -} -pub fn WIFEXITED(s: u32) bool { - return WTERMSIG(s) == 0; -} -pub fn WIFSTOPPED(s: u32) bool { - return @truncate(u16, ((s & 0xffff) *% 0x10001) >> 8) > 0x7f00; -} -pub fn WIFSIGNALED(s: u32) bool { - return (s & 0xffff) -% 1 < 0xff; -} - -pub const winsize = extern struct { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16, -}; - -/// NSIG is the total number of signals defined. -/// As signal numbers are sequential, NSIG is one greater than the largest defined signal number. -pub const NSIG = if (is_mips) 128 else 65; - -pub const sigset_t = [1024 / 32]u32; - -pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len; -pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30; - -pub const k_sigaction = switch (arch) { - .mips, .mipsel => extern struct { - flags: c_uint, - handler: ?fn (c_int) callconv(.C) void, - mask: [4]c_ulong, - restorer: fn () callconv(.C) void, - }, - .mips64, .mips64el => extern struct { - flags: c_uint, - handler: ?fn (c_int) callconv(.C) void, - mask: [2]c_ulong, - restorer: fn () callconv(.C) void, - }, - else => extern struct { - handler: ?fn (c_int) callconv(.C) void, - flags: c_ulong, - restorer: fn () callconv(.C) void, - mask: [2]c_uint, - }, -}; - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = extern struct { - pub const handler_fn = fn (c_int) callconv(.C) void; - pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; - - handler: extern union { - handler: ?handler_fn, - sigaction: ?sigaction_fn, - }, - mask: sigset_t, - flags: c_uint, - restorer: ?fn () callconv(.C) void = null, -}; - -pub const SIG_ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); -pub const SIG_DFL = @intToPtr(?Sigaction.sigaction_fn, 0); -pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - -pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).Array.len; - -pub const SFD_CLOEXEC = O_CLOEXEC; -pub const SFD_NONBLOCK = O_NONBLOCK; - -pub const signalfd_siginfo = extern struct { - signo: u32, - errno: i32, - code: i32, - pid: u32, - uid: uid_t, - fd: i32, - tid: u32, - band: u32, - overrun: u32, - trapno: u32, - status: i32, - int: i32, - ptr: u64, - utime: u64, - stime: u64, - addr: u64, - addr_lsb: u16, - __pad2: u16, - syscall: i32, - call_addr: u64, - arch: u32, - __pad: [28]u8, -}; - -pub const in_port_t = u16; -pub const sa_family_t = u16; -pub const socklen_t = u32; - -pub const sockaddr = extern struct { - family: sa_family_t, - data: [14]u8, -}; - -pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; - -/// IPv4 socket address -pub const sockaddr_in = extern struct { - family: sa_family_t = AF_INET, - port: in_port_t, - addr: u32, - zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, -}; - -/// IPv6 socket address -pub const sockaddr_in6 = extern struct { - family: sa_family_t = AF_INET6, - port: in_port_t, - flowinfo: u32, - addr: [16]u8, - scope_id: u32, -}; - -/// UNIX domain socket address -pub const sockaddr_un = extern struct { - family: sa_family_t = AF_UNIX, - path: [108]u8, -}; - -pub const mmsghdr = extern struct { - msg_hdr: msghdr, - msg_len: u32, -}; - -pub const mmsghdr_const = extern struct { - msg_hdr: msghdr_const, - msg_len: u32, -}; - -pub const epoll_data = extern union { - ptr: usize, - fd: i32, - @"u32": u32, - @"u64": u64, -}; - -// On x86_64 the structure is packed so that it matches the definition of its -// 32bit counterpart -pub const epoll_event = switch (arch) { - .x86_64 => packed struct { - events: u32, - data: epoll_data, - }, - else => extern struct { - events: u32, - data: epoll_data, - }, -}; - -pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330; -pub const _LINUX_CAPABILITY_U32S_1 = 1; - -pub const _LINUX_CAPABILITY_VERSION_2 = 0x20071026; -pub const _LINUX_CAPABILITY_U32S_2 = 2; - -pub const _LINUX_CAPABILITY_VERSION_3 = 0x20080522; -pub const _LINUX_CAPABILITY_U32S_3 = 2; - -pub const VFS_CAP_REVISION_MASK = 0xFF000000; -pub const VFS_CAP_REVISION_SHIFT = 24; -pub const VFS_CAP_FLAGS_MASK = ~VFS_CAP_REVISION_MASK; -pub const VFS_CAP_FLAGS_EFFECTIVE = 0x000001; - -pub const VFS_CAP_REVISION_1 = 0x01000000; -pub const VFS_CAP_U32_1 = 1; -pub const XATTR_CAPS_SZ_1 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_1); - -pub const VFS_CAP_REVISION_2 = 0x02000000; -pub const VFS_CAP_U32_2 = 2; -pub const XATTR_CAPS_SZ_2 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_2); - -pub const XATTR_CAPS_SZ = XATTR_CAPS_SZ_2; -pub const VFS_CAP_U32 = VFS_CAP_U32_2; -pub const VFS_CAP_REVISION = VFS_CAP_REVISION_2; - -pub const vfs_cap_data = extern struct { - //all of these are mandated as little endian - //when on disk. - const Data = struct { - permitted: u32, - inheritable: u32, - }; - - magic_etc: u32, - data: [VFS_CAP_U32]Data, -}; - -pub const CAP_CHOWN = 0; -pub const CAP_DAC_OVERRIDE = 1; -pub const CAP_DAC_READ_SEARCH = 2; -pub const CAP_FOWNER = 3; -pub const CAP_FSETID = 4; -pub const CAP_KILL = 5; -pub const CAP_SETGID = 6; -pub const CAP_SETUID = 7; -pub const CAP_SETPCAP = 8; -pub const CAP_LINUX_IMMUTABLE = 9; -pub const CAP_NET_BIND_SERVICE = 10; -pub const CAP_NET_BROADCAST = 11; -pub const CAP_NET_ADMIN = 12; -pub const CAP_NET_RAW = 13; -pub const CAP_IPC_LOCK = 14; -pub const CAP_IPC_OWNER = 15; -pub const CAP_SYS_MODULE = 16; -pub const CAP_SYS_RAWIO = 17; -pub const CAP_SYS_CHROOT = 18; -pub const CAP_SYS_PTRACE = 19; -pub const CAP_SYS_PACCT = 20; -pub const CAP_SYS_ADMIN = 21; -pub const CAP_SYS_BOOT = 22; -pub const CAP_SYS_NICE = 23; -pub const CAP_SYS_RESOURCE = 24; -pub const CAP_SYS_TIME = 25; -pub const CAP_SYS_TTY_CONFIG = 26; -pub const CAP_MKNOD = 27; -pub const CAP_LEASE = 28; -pub const CAP_AUDIT_WRITE = 29; -pub const CAP_AUDIT_CONTROL = 30; -pub const CAP_SETFCAP = 31; -pub const CAP_MAC_OVERRIDE = 32; -pub const CAP_MAC_ADMIN = 33; -pub const CAP_SYSLOG = 34; -pub const CAP_WAKE_ALARM = 35; -pub const CAP_BLOCK_SUSPEND = 36; -pub const CAP_AUDIT_READ = 37; -pub const CAP_LAST_CAP = CAP_AUDIT_READ; - -pub fn cap_valid(x: u8) bool { - return x >= 0 and x <= CAP_LAST_CAP; -} - -pub fn CAP_TO_MASK(cap: u8) u32 { - return @as(u32, 1) << @intCast(u5, cap & 31); -} - -pub fn CAP_TO_INDEX(cap: u8) u8 { - return cap >> 5; -} - -pub const cap_t = extern struct { - hdrp: *cap_user_header_t, - datap: *cap_user_data_t, -}; - -pub const cap_user_header_t = extern struct { - version: u32, - pid: usize, -}; - -pub const cap_user_data_t = extern struct { - effective: u32, - permitted: u32, - inheritable: u32, -}; - -pub const inotify_event = extern struct { - wd: i32, - mask: u32, - cookie: u32, - len: u32, - //name: [?]u8, -}; - -pub const dirent64 = extern struct { - d_ino: u64, - d_off: u64, - d_reclen: u16, - d_type: u8, - d_name: u8, // field address is the address of first byte of name https://github.com/ziglang/zig/issues/173 - - pub fn reclen(self: dirent64) u16 { - return self.d_reclen; - } -}; - -pub const dl_phdr_info = extern struct { - dlpi_addr: usize, - dlpi_name: ?[*:0]const u8, - dlpi_phdr: [*]std.elf.Phdr, - dlpi_phnum: u16, -}; - -pub const CPU_SETSIZE = 128; -pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize; -pub const cpu_count_t = std.meta.Int(.unsigned, std.math.log2(CPU_SETSIZE * 8)); - -pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t { - var sum: cpu_count_t = 0; - for (set) |x| { - sum += @popCount(usize, x); - } - return sum; -} - -// TODO port these over -//#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) -//#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) -//#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) -//#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) -//#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) -//#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) -//#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) -//#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) -//#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) - -pub const MINSIGSTKSZ = switch (arch) { - .i386, .x86_64, .arm, .mipsel => 2048, - .aarch64 => 5120, - else => @compileError("MINSIGSTKSZ not defined for this architecture"), -}; -pub const SIGSTKSZ = switch (arch) { - .i386, .x86_64, .arm, .mipsel => 8192, - .aarch64 => 16384, - else => @compileError("SIGSTKSZ not defined for this architecture"), -}; - -pub const SS_ONSTACK = 1; -pub const SS_DISABLE = 2; -pub const SS_AUTODISARM = 1 << 31; - -pub const stack_t = if (is_mips) - // IRIX compatible stack_t - extern struct { - ss_sp: [*]u8, - ss_size: usize, - ss_flags: i32, - } -else - extern struct { - ss_sp: [*]u8, - ss_flags: i32, - ss_size: usize, - }; - -pub const sigval = extern union { - int: i32, - ptr: *c_void, -}; - -const siginfo_fields_union = extern union { - pad: [128 - 2 * @sizeOf(c_int) - @sizeOf(c_long)]u8, - common: extern struct { - first: extern union { - piduid: extern struct { - pid: pid_t, - uid: uid_t, - }, - timer: extern struct { - timerid: i32, - overrun: i32, - }, - }, - second: extern union { - value: sigval, - sigchld: extern struct { - status: i32, - utime: clock_t, - stime: clock_t, - }, - }, - }, - sigfault: extern struct { - addr: *c_void, - addr_lsb: i16, - first: extern union { - addr_bnd: extern struct { - lower: *c_void, - upper: *c_void, - }, - pkey: u32, - }, - }, - sigpoll: extern struct { - band: isize, - fd: i32, - }, - sigsys: extern struct { - call_addr: *c_void, - syscall: i32, - arch: u32, - }, -}; - -pub const siginfo_t = if (is_mips) - extern struct { - signo: i32, - code: i32, - errno: i32, - fields: siginfo_fields_union, - } -else - extern struct { - signo: i32, - errno: i32, - code: i32, - fields: siginfo_fields_union, - }; - -pub const io_uring_params = extern struct { - sq_entries: u32, - cq_entries: u32, - flags: u32, - sq_thread_cpu: u32, - sq_thread_idle: u32, - features: u32, - wq_fd: u32, - resv: [3]u32, - sq_off: io_sqring_offsets, - cq_off: io_cqring_offsets, -}; - -// io_uring_params.features flags - -pub const IORING_FEAT_SINGLE_MMAP = 1 << 0; -pub const IORING_FEAT_NODROP = 1 << 1; -pub const IORING_FEAT_SUBMIT_STABLE = 1 << 2; -pub const IORING_FEAT_RW_CUR_POS = 1 << 3; -pub const IORING_FEAT_CUR_PERSONALITY = 1 << 4; -pub const IORING_FEAT_FAST_POLL = 1 << 5; -pub const IORING_FEAT_POLL_32BITS = 1 << 6; - -// io_uring_params.flags - -/// io_context is polled -pub const IORING_SETUP_IOPOLL = 1 << 0; - -/// SQ poll thread -pub const IORING_SETUP_SQPOLL = 1 << 1; - -/// sq_thread_cpu is valid -pub const IORING_SETUP_SQ_AFF = 1 << 2; - -/// app defines CQ size -pub const IORING_SETUP_CQSIZE = 1 << 3; - -/// clamp SQ/CQ ring sizes -pub const IORING_SETUP_CLAMP = 1 << 4; - -/// attach to existing wq -pub const IORING_SETUP_ATTACH_WQ = 1 << 5; - -/// start with ring disabled -pub const IORING_SETUP_R_DISABLED = 1 << 6; - -pub const io_sqring_offsets = extern struct { - /// offset of ring head - head: u32, - - /// offset of ring tail - tail: u32, - - /// ring mask value - ring_mask: u32, - - /// entries in ring - ring_entries: u32, - - /// ring flags - flags: u32, - - /// number of sqes not submitted - dropped: u32, - - /// sqe index array - array: u32, - - resv1: u32, - resv2: u64, -}; - -// io_sqring_offsets.flags - -/// needs io_uring_enter wakeup -pub const IORING_SQ_NEED_WAKEUP = 1 << 0; - -/// kernel has cqes waiting beyond the cq ring -pub const IORING_SQ_CQ_OVERFLOW = 1 << 1; - -pub const io_cqring_offsets = extern struct { - head: u32, - tail: u32, - ring_mask: u32, - ring_entries: u32, - overflow: u32, - cqes: u32, - resv: [2]u64, -}; - -pub const io_uring_sqe = extern struct { - opcode: IORING_OP, - flags: u8, - ioprio: u16, - fd: i32, - off: u64, - addr: u64, - len: u32, - rw_flags: u32, - user_data: u64, - buf_index: u16, - personality: u16, - splice_fd_in: i32, - __pad2: [2]u64, -}; - -pub const IOSQE_BIT = enum(u8) { - FIXED_FILE, - IO_DRAIN, - IO_LINK, - IO_HARDLINK, - ASYNC, - BUFFER_SELECT, - - _, -}; - -// io_uring_sqe.flags - -/// use fixed fileset -pub const IOSQE_FIXED_FILE = 1 << @enumToInt(IOSQE_BIT.FIXED_FILE); - -/// issue after inflight IO -pub const IOSQE_IO_DRAIN = 1 << @enumToInt(IOSQE_BIT.IO_DRAIN); - -/// links next sqe -pub const IOSQE_IO_LINK = 1 << @enumToInt(IOSQE_BIT.IO_LINK); - -/// like LINK, but stronger -pub const IOSQE_IO_HARDLINK = 1 << @enumToInt(IOSQE_BIT.IO_HARDLINK); - -/// always go async -pub const IOSQE_ASYNC = 1 << @enumToInt(IOSQE_BIT.ASYNC); - -/// select buffer from buf_group -pub const IOSQE_BUFFER_SELECT = 1 << @enumToInt(IOSQE_BIT.BUFFER_SELECT); - -pub const IORING_OP = enum(u8) { - NOP, - READV, - WRITEV, - FSYNC, - READ_FIXED, - WRITE_FIXED, - POLL_ADD, - POLL_REMOVE, - SYNC_FILE_RANGE, - SENDMSG, - RECVMSG, - TIMEOUT, - TIMEOUT_REMOVE, - ACCEPT, - ASYNC_CANCEL, - LINK_TIMEOUT, - CONNECT, - FALLOCATE, - OPENAT, - CLOSE, - FILES_UPDATE, - STATX, - READ, - WRITE, - FADVISE, - MADVISE, - SEND, - RECV, - OPENAT2, - EPOLL_CTL, - SPLICE, - PROVIDE_BUFFERS, - REMOVE_BUFFERS, - TEE, - - _, -}; - -// io_uring_sqe.fsync_flags -pub const IORING_FSYNC_DATASYNC = 1 << 0; - -// io_uring_sqe.timeout_flags -pub const IORING_TIMEOUT_ABS = 1 << 0; - -// IO completion data structure (Completion Queue Entry) -pub const io_uring_cqe = extern struct { - /// io_uring_sqe.data submission passed back - user_data: u64, - - /// result code for this event - res: i32, - flags: u32, - - pub fn err(self: io_uring_cqe) E { - if (self.res > -4096 and self.res < 0) { - return @intToEnum(E, -self.res); - } - return .SUCCESS; - } -}; - -// io_uring_cqe.flags - -/// If set, the upper 16 bits are the buffer ID -pub const IORING_CQE_F_BUFFER = 1 << 0; - -pub const IORING_OFF_SQ_RING = 0; -pub const IORING_OFF_CQ_RING = 0x8000000; -pub const IORING_OFF_SQES = 0x10000000; - -// io_uring_enter flags -pub const IORING_ENTER_GETEVENTS = 1 << 0; -pub const IORING_ENTER_SQ_WAKEUP = 1 << 1; - -// io_uring_register opcodes and arguments -pub const IORING_REGISTER = enum(u8) { - REGISTER_BUFFERS, - UNREGISTER_BUFFERS, - REGISTER_FILES, - UNREGISTER_FILES, - REGISTER_EVENTFD, - UNREGISTER_EVENTFD, - REGISTER_FILES_UPDATE, - REGISTER_EVENTFD_ASYNC, - REGISTER_PROBE, - REGISTER_PERSONALITY, - UNREGISTER_PERSONALITY, - REGISTER_RESTRICTIONS, - REGISTER_ENABLE_RINGS, - - _, -}; - -pub const io_uring_files_update = extern struct { - offset: u32, - resv: u32, - fds: u64, -}; - -pub const IO_URING_OP_SUPPORTED = 1 << 0; - -pub const io_uring_probe_op = extern struct { - op: IORING_OP, - - resv: u8, - - /// IO_URING_OP_* flags - flags: u16, - - resv2: u32, -}; - -pub const io_uring_probe = extern struct { - /// last opcode supported - last_op: IORING_OP, - - /// Number of io_uring_probe_op following - ops_len: u8, - - resv: u16, - resv2: u32[3], - - // Followed by up to `ops_len` io_uring_probe_op structures -}; - -pub const io_uring_restriction = extern struct { - opcode: u16, - arg: extern union { - /// IORING_RESTRICTION_REGISTER_OP - register_op: IORING_REGISTER, - - /// IORING_RESTRICTION_SQE_OP - sqe_op: IORING_OP, - - /// IORING_RESTRICTION_SQE_FLAGS_* - sqe_flags: u8, - }, - resv: u8, - resv2: u32[3], -}; - -/// io_uring_restriction->opcode values -pub const IORING_RESTRICTION = enum(u8) { - /// Allow an io_uring_register(2) opcode - REGISTER_OP = 0, - - /// Allow an sqe opcode - SQE_OP = 1, - - /// Allow sqe flags - SQE_FLAGS_ALLOWED = 2, - - /// Require sqe flags (these flags must be set on each submission) - SQE_FLAGS_REQUIRED = 3, - - _, -}; - -pub const utsname = extern struct { - sysname: [64:0]u8, - nodename: [64:0]u8, - release: [64:0]u8, - version: [64:0]u8, - machine: [64:0]u8, - domainname: [64:0]u8, -}; -pub const HOST_NAME_MAX = 64; - -pub const STATX_TYPE = 0x0001; -pub const STATX_MODE = 0x0002; -pub const STATX_NLINK = 0x0004; -pub const STATX_UID = 0x0008; -pub const STATX_GID = 0x0010; -pub const STATX_ATIME = 0x0020; -pub const STATX_MTIME = 0x0040; -pub const STATX_CTIME = 0x0080; -pub const STATX_INO = 0x0100; -pub const STATX_SIZE = 0x0200; -pub const STATX_BLOCKS = 0x0400; -pub const STATX_BASIC_STATS = 0x07ff; - -pub const STATX_BTIME = 0x0800; - -pub const STATX_ATTR_COMPRESSED = 0x0004; -pub const STATX_ATTR_IMMUTABLE = 0x0010; -pub const STATX_ATTR_APPEND = 0x0020; -pub const STATX_ATTR_NODUMP = 0x0040; -pub const STATX_ATTR_ENCRYPTED = 0x0800; -pub const STATX_ATTR_AUTOMOUNT = 0x1000; - -pub const statx_timestamp = extern struct { - tv_sec: i64, - tv_nsec: u32, - __pad1: u32, -}; - -/// Renamed to `Statx` to not conflict with the `statx` function. -pub const Statx = extern struct { - /// Mask of bits indicating filled fields - mask: u32, - - /// Block size for filesystem I/O - blksize: u32, - - /// Extra file attribute indicators - attributes: u64, - - /// Number of hard links - nlink: u32, - - /// User ID of owner - uid: uid_t, - - /// Group ID of owner - gid: gid_t, - - /// File type and mode - mode: u16, - __pad1: u16, - - /// Inode number - ino: u64, - - /// Total size in bytes - size: u64, - - /// Number of 512B blocks allocated - blocks: u64, - - /// Mask to show what's supported in `attributes`. - attributes_mask: u64, - - /// Last access file timestamp - atime: statx_timestamp, - - /// Creation file timestamp - btime: statx_timestamp, - - /// Last status change file timestamp - ctime: statx_timestamp, - - /// Last modification file timestamp - mtime: statx_timestamp, - - /// Major ID, if this file represents a device. - rdev_major: u32, - - /// Minor ID, if this file represents a device. - rdev_minor: u32, - - /// Major ID of the device containing the filesystem where this file resides. - dev_major: u32, - - /// Minor ID of the device containing the filesystem where this file resides. - dev_minor: u32, - - __pad2: [14]u64, -}; - -pub const addrinfo = extern struct { - flags: i32, - family: i32, - socktype: i32, - protocol: i32, - addrlen: socklen_t, - addr: ?*sockaddr, - canonname: ?[*:0]u8, - next: ?*addrinfo, -}; - -pub const IPPORT_RESERVED = 1024; - -pub const IPPROTO_IP = 0; -pub const IPPROTO_HOPOPTS = 0; -pub const IPPROTO_ICMP = 1; -pub const IPPROTO_IGMP = 2; -pub const IPPROTO_IPIP = 4; -pub const IPPROTO_TCP = 6; -pub const IPPROTO_EGP = 8; -pub const IPPROTO_PUP = 12; -pub const IPPROTO_UDP = 17; -pub const IPPROTO_IDP = 22; -pub const IPPROTO_TP = 29; -pub const IPPROTO_DCCP = 33; -pub const IPPROTO_IPV6 = 41; -pub const IPPROTO_ROUTING = 43; -pub const IPPROTO_FRAGMENT = 44; -pub const IPPROTO_RSVP = 46; -pub const IPPROTO_GRE = 47; -pub const IPPROTO_ESP = 50; -pub const IPPROTO_AH = 51; -pub const IPPROTO_ICMPV6 = 58; -pub const IPPROTO_NONE = 59; -pub const IPPROTO_DSTOPTS = 60; -pub const IPPROTO_MTP = 92; -pub const IPPROTO_BEETPH = 94; -pub const IPPROTO_ENCAP = 98; -pub const IPPROTO_PIM = 103; -pub const IPPROTO_COMP = 108; -pub const IPPROTO_SCTP = 132; -pub const IPPROTO_MH = 135; -pub const IPPROTO_UDPLITE = 136; -pub const IPPROTO_MPLS = 137; -pub const IPPROTO_RAW = 255; -pub const IPPROTO_MAX = 256; - -pub const RR_A = 1; -pub const RR_CNAME = 5; -pub const RR_AAAA = 28; - -/// Turn off Nagle's algorithm -pub const TCP_NODELAY = 1; -/// Limit MSS -pub const TCP_MAXSEG = 2; -/// Never send partially complete segments. -pub const TCP_CORK = 3; -/// Start keeplives after this period, in seconds -pub const TCP_KEEPIDLE = 4; -/// Interval between keepalives -pub const TCP_KEEPINTVL = 5; -/// Number of keepalives before death -pub const TCP_KEEPCNT = 6; -/// Number of SYN retransmits -pub const TCP_SYNCNT = 7; -/// Life time of orphaned FIN-WAIT-2 state -pub const TCP_LINGER2 = 8; -/// Wake up listener only when data arrive -pub const TCP_DEFER_ACCEPT = 9; -/// Bound advertised window -pub const TCP_WINDOW_CLAMP = 10; -/// Information about this connection. -pub const TCP_INFO = 11; -/// Block/reenable quick acks -pub const TCP_QUICKACK = 12; -/// Congestion control algorithm -pub const TCP_CONGESTION = 13; -/// TCP MD5 Signature (RFC2385) -pub const TCP_MD5SIG = 14; -/// Use linear timeouts for thin streams -pub const TCP_THIN_LINEAR_TIMEOUTS = 16; -/// Fast retrans. after 1 dupack -pub const TCP_THIN_DUPACK = 17; -/// How long for loss retry before timeout -pub const TCP_USER_TIMEOUT = 18; -/// TCP sock is under repair right now -pub const TCP_REPAIR = 19; -pub const TCP_REPAIR_QUEUE = 20; -pub const TCP_QUEUE_SEQ = 21; -pub const TCP_REPAIR_OPTIONS = 22; -/// Enable FastOpen on listeners -pub const TCP_FASTOPEN = 23; -pub const TCP_TIMESTAMP = 24; -/// limit number of unsent bytes in write queue -pub const TCP_NOTSENT_LOWAT = 25; -/// Get Congestion Control (optional) info -pub const TCP_CC_INFO = 26; -/// Record SYN headers for new connections -pub const TCP_SAVE_SYN = 27; -/// Get SYN headers recorded for connection -pub const TCP_SAVED_SYN = 28; -/// Get/set window parameters -pub const TCP_REPAIR_WINDOW = 29; -/// Attempt FastOpen with connect -pub const TCP_FASTOPEN_CONNECT = 30; -/// Attach a ULP to a TCP connection -pub const TCP_ULP = 31; -/// TCP MD5 Signature with extensions -pub const TCP_MD5SIG_EXT = 32; -/// Set the key for Fast Open (cookie) -pub const TCP_FASTOPEN_KEY = 33; -/// Enable TFO without a TFO cookie -pub const TCP_FASTOPEN_NO_COOKIE = 34; -pub const TCP_ZEROCOPY_RECEIVE = 35; -/// Notify bytes available to read as a cmsg on read -pub const TCP_INQ = 36; -pub const TCP_CM_INQ = TCP_INQ; -/// delay outgoing packets by XX usec -pub const TCP_TX_DELAY = 37; - -pub const TCP_REPAIR_ON = 1; -pub const TCP_REPAIR_OFF = 0; -/// Turn off without window probes -pub const TCP_REPAIR_OFF_NO_WP = -1; - -pub const tcp_repair_opt = extern struct { - opt_code: u32, - opt_val: u32, -}; - -pub const tcp_repair_window = extern struct { - snd_wl1: u32, - snd_wnd: u32, - max_window: u32, - rcv_wnd: u32, - rcv_wup: u32, -}; - -pub const TcpRepairOption = enum { - TCP_NO_QUEUE, - TCP_RECV_QUEUE, - TCP_SEND_QUEUE, - TCP_QUEUES_NR, -}; - -/// why fastopen failed from client perspective -pub const tcp_fastopen_client_fail = enum { - /// catch-all - TFO_STATUS_UNSPEC, - /// if not in TFO_CLIENT_NO_COOKIE mode - TFO_COOKIE_UNAVAILABLE, - /// SYN-ACK did not ack SYN data - TFO_DATA_NOT_ACKED, - /// SYN-ACK did not ack SYN data after timeout - TFO_SYN_RETRANSMITTED, -}; - -/// for TCP_INFO socket option -pub const TCPI_OPT_TIMESTAMPS = 1; -pub const TCPI_OPT_SACK = 2; -pub const TCPI_OPT_WSCALE = 4; -/// ECN was negociated at TCP session init -pub const TCPI_OPT_ECN = 8; -/// we received at least one packet with ECT -pub const TCPI_OPT_ECN_SEEN = 16; -/// SYN-ACK acked data in SYN sent or rcvd -pub const TCPI_OPT_SYN_DATA = 32; - -pub const nfds_t = usize; -pub const pollfd = extern struct { - fd: fd_t, - events: i16, - revents: i16, -}; - -pub const POLLIN = 0x001; -pub const POLLPRI = 0x002; -pub const POLLOUT = 0x004; -pub const POLLERR = 0x008; -pub const POLLHUP = 0x010; -pub const POLLNVAL = 0x020; -pub const POLLRDNORM = 0x040; -pub const POLLRDBAND = 0x080; - -pub const MFD_CLOEXEC = 0x0001; -pub const MFD_ALLOW_SEALING = 0x0002; -pub const MFD_HUGETLB = 0x0004; -pub const MFD_ALL_FLAGS = MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB; - -pub const HUGETLB_FLAG_ENCODE_SHIFT = 26; -pub const HUGETLB_FLAG_ENCODE_MASK = 0x3f; -pub const HUGETLB_FLAG_ENCODE_64KB = 16 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_512KB = 19 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_1MB = 20 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_2MB = 21 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_8MB = 23 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_16MB = 24 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_32MB = 25 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_256MB = 28 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_512MB = 29 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_1GB = 30 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_2GB = 31 << HUGETLB_FLAG_ENCODE_SHIFT; -pub const HUGETLB_FLAG_ENCODE_16GB = 34 << HUGETLB_FLAG_ENCODE_SHIFT; - -pub const MFD_HUGE_SHIFT = HUGETLB_FLAG_ENCODE_SHIFT; -pub const MFD_HUGE_MASK = HUGETLB_FLAG_ENCODE_MASK; -pub const MFD_HUGE_64KB = HUGETLB_FLAG_ENCODE_64KB; -pub const MFD_HUGE_512KB = HUGETLB_FLAG_ENCODE_512KB; -pub const MFD_HUGE_1MB = HUGETLB_FLAG_ENCODE_1MB; -pub const MFD_HUGE_2MB = HUGETLB_FLAG_ENCODE_2MB; -pub const MFD_HUGE_8MB = HUGETLB_FLAG_ENCODE_8MB; -pub const MFD_HUGE_16MB = HUGETLB_FLAG_ENCODE_16MB; -pub const MFD_HUGE_32MB = HUGETLB_FLAG_ENCODE_32MB; -pub const MFD_HUGE_256MB = HUGETLB_FLAG_ENCODE_256MB; -pub const MFD_HUGE_512MB = HUGETLB_FLAG_ENCODE_512MB; -pub const MFD_HUGE_1GB = HUGETLB_FLAG_ENCODE_1GB; -pub const MFD_HUGE_2GB = HUGETLB_FLAG_ENCODE_2GB; -pub const MFD_HUGE_16GB = HUGETLB_FLAG_ENCODE_16GB; - -pub const RUSAGE_SELF = 0; -pub const RUSAGE_CHILDREN = -1; -pub const RUSAGE_THREAD = 1; - -pub const rusage = extern struct { - utime: timeval, - stime: timeval, - maxrss: isize, - ixrss: isize, - idrss: isize, - isrss: isize, - minflt: isize, - majflt: isize, - nswap: isize, - inblock: isize, - oublock: isize, - msgsnd: isize, - msgrcv: isize, - nsignals: isize, - nvcsw: isize, - nivcsw: isize, - __reserved: [16]isize = [1]isize{0} ** 16, -}; - -pub const cc_t = u8; -pub const speed_t = u32; -pub const tcflag_t = u32; - -pub const NCCS = 32; - -pub const B0 = 0o0000000; -pub const B50 = 0o0000001; -pub const B75 = 0o0000002; -pub const B110 = 0o0000003; -pub const B134 = 0o0000004; -pub const B150 = 0o0000005; -pub const B200 = 0o0000006; -pub const B300 = 0o0000007; -pub const B600 = 0o0000010; -pub const B1200 = 0o0000011; -pub const B1800 = 0o0000012; -pub const B2400 = 0o0000013; -pub const B4800 = 0o0000014; -pub const B9600 = 0o0000015; -pub const B19200 = 0o0000016; -pub const B38400 = 0o0000017; -pub const BOTHER = 0o0010000; -pub const B57600 = 0o0010001; -pub const B115200 = 0o0010002; -pub const B230400 = 0o0010003; -pub const B460800 = 0o0010004; -pub const B500000 = 0o0010005; -pub const B576000 = 0o0010006; -pub const B921600 = 0o0010007; -pub const B1000000 = 0o0010010; -pub const B1152000 = 0o0010011; -pub const B1500000 = 0o0010012; -pub const B2000000 = 0o0010013; -pub const B2500000 = 0o0010014; -pub const B3000000 = 0o0010015; -pub const B3500000 = 0o0010016; -pub const B4000000 = 0o0010017; - -pub usingnamespace switch (arch) { - .powerpc, .powerpc64, .powerpc64le => struct { - pub const VINTR = 0; - pub const VQUIT = 1; - pub const VERASE = 2; - pub const VKILL = 3; - pub const VEOF = 4; - pub const VMIN = 5; - pub const VEOL = 6; - pub const VTIME = 7; - pub const VEOL2 = 8; - pub const VSWTC = 9; - pub const VWERASE = 10; - pub const VREPRINT = 11; - pub const VSUSP = 12; - pub const VSTART = 13; - pub const VSTOP = 14; - pub const VLNEXT = 15; - pub const VDISCARD = 16; - }, - .sparc, .sparcv9 => struct { - pub const VINTR = 0; - pub const VQUIT = 1; - pub const VERASE = 2; - pub const VKILL = 3; - pub const VEOF = 4; - pub const VEOL = 5; - pub const VEOL2 = 6; - pub const VSWTC = 7; - pub const VSTART = 8; - pub const VSTOP = 9; - pub const VSUSP = 10; - pub const VDSUSP = 11; - pub const VREPRINT = 12; - pub const VDISCARD = 13; - pub const VWERASE = 14; - pub const VLNEXT = 15; - pub const VMIN = VEOF; - pub const VTIME = VEOL; - }, - .mips, .mipsel, .mips64, .mips64el => struct { - pub const VINTR = 0; - pub const VQUIT = 1; - pub const VERASE = 2; - pub const VKILL = 3; - pub const VMIN = 4; - pub const VTIME = 5; - pub const VEOL2 = 6; - pub const VSWTC = 7; - pub const VSWTCH = 7; - pub const VSTART = 8; - pub const VSTOP = 9; - pub const VSUSP = 10; - pub const VREPRINT = 12; - pub const VDISCARD = 13; - pub const VWERASE = 14; - pub const VLNEXT = 15; - pub const VEOF = 16; - pub const VEOL = 17; - }, - else => struct { - pub const VINTR = 0; - pub const VQUIT = 1; - pub const VERASE = 2; - pub const VKILL = 3; - pub const VEOF = 4; - pub const VTIME = 5; - pub const VMIN = 6; - pub const VSWTC = 7; - pub const VSTART = 8; - pub const VSTOP = 9; - pub const VSUSP = 10; - pub const VEOL = 11; - pub const VREPRINT = 12; - pub const VDISCARD = 13; - pub const VWERASE = 14; - pub const VLNEXT = 15; - pub const VEOL2 = 16; - }, -}; - -pub const IGNBRK = 1; -pub const BRKINT = 2; -pub const IGNPAR = 4; -pub const PARMRK = 8; -pub const INPCK = 16; -pub const ISTRIP = 32; -pub const INLCR = 64; -pub const IGNCR = 128; -pub const ICRNL = 256; -pub const IUCLC = 512; -pub const IXON = 1024; -pub const IXANY = 2048; -pub const IXOFF = 4096; -pub const IMAXBEL = 8192; -pub const IUTF8 = 16384; - -pub const OPOST = 1; -pub const OLCUC = 2; -pub const ONLCR = 4; -pub const OCRNL = 8; -pub const ONOCR = 16; -pub const ONLRET = 32; -pub const OFILL = 64; -pub const OFDEL = 128; -pub const VTDLY = 16384; -pub const VT0 = 0; -pub const VT1 = 16384; - -pub const CSIZE = 48; -pub const CS5 = 0; -pub const CS6 = 16; -pub const CS7 = 32; -pub const CS8 = 48; -pub const CSTOPB = 64; -pub const CREAD = 128; -pub const PARENB = 256; -pub const PARODD = 512; -pub const HUPCL = 1024; -pub const CLOCAL = 2048; - -pub const ISIG = 1; -pub const ICANON = 2; -pub const ECHO = 8; -pub const ECHOE = 16; -pub const ECHOK = 32; -pub const ECHONL = 64; -pub const NOFLSH = 128; -pub const TOSTOP = 256; -pub const IEXTEN = 32768; - -pub const TCSA = enum(c_uint) { - NOW, - DRAIN, - FLUSH, - _, -}; - -pub const termios = extern struct { - iflag: tcflag_t, - oflag: tcflag_t, - cflag: tcflag_t, - lflag: tcflag_t, - line: cc_t, - cc: [NCCS]cc_t, - ispeed: speed_t, - ospeed: speed_t, -}; - -pub const SIOCGIFINDEX = 0x8933; -pub const IFNAMESIZE = 16; - -pub const ifmap = extern struct { - mem_start: u32, - mem_end: u32, - base_addr: u16, - irq: u8, - dma: u8, - port: u8, -}; - -pub const ifreq = extern struct { - ifrn: extern union { - name: [IFNAMESIZE]u8, - }, - ifru: extern union { - addr: sockaddr, - dstaddr: sockaddr, - broadaddr: sockaddr, - netmask: sockaddr, - hwaddr: sockaddr, - flags: i16, - ivalue: i32, - mtu: i32, - map: ifmap, - slave: [IFNAMESIZE - 1:0]u8, - newname: [IFNAMESIZE - 1:0]u8, - data: ?[*]u8, - }, -}; - -// doc comments copied from musl -pub const rlimit_resource = enum(c_int) { - /// Per-process CPU limit, in seconds. - CPU, - - /// Largest file that can be created, in bytes. - FSIZE, - - /// Maximum size of data segment, in bytes. - DATA, - - /// Maximum size of stack segment, in bytes. - STACK, - - /// Largest core file that can be created, in bytes. - CORE, - - /// Largest resident set size, in bytes. - /// This affects swapping; processes that are exceeding their - /// resident set size will be more likely to have physical memory - /// taken from them. - RSS, - - /// Number of processes. - NPROC, - - /// Number of open files. - NOFILE, - - /// Locked-in-memory address space. - MEMLOCK, - - /// Address space limit. - AS, - - /// Maximum number of file locks. - LOCKS, - - /// Maximum number of pending signals. - SIGPENDING, - - /// Maximum bytes in POSIX message queues. - MSGQUEUE, - - /// Maximum nice priority allowed to raise to. - /// Nice levels 19 .. -20 correspond to 0 .. 39 - /// values of this resource limit. - NICE, - - /// Maximum realtime priority allowed for non-priviledged - /// processes. - RTPRIO, - - /// Maximum CPU time in µs that a process scheduled under a real-time - /// scheduling policy may consume without making a blocking system - /// call before being forcibly descheduled. - RTTIME, - - _, -}; - -pub const rlim_t = u64; - -/// No limit -pub const RLIM_INFINITY = ~@as(rlim_t, 0); - -pub const RLIM_SAVED_MAX = RLIM_INFINITY; -pub const RLIM_SAVED_CUR = RLIM_INFINITY; - -pub const rlimit = extern struct { - /// Soft limit - cur: rlim_t, - /// Hard limit - max: rlim_t, -}; - -pub const MADV_NORMAL = 0; -pub const MADV_RANDOM = 1; -pub const MADV_SEQUENTIAL = 2; -pub const MADV_WILLNEED = 3; -pub const MADV_DONTNEED = 4; -pub const MADV_FREE = 8; -pub const MADV_REMOVE = 9; -pub const MADV_DONTFORK = 10; -pub const MADV_DOFORK = 11; -pub const MADV_MERGEABLE = 12; -pub const MADV_UNMERGEABLE = 13; -pub const MADV_HUGEPAGE = 14; -pub const MADV_NOHUGEPAGE = 15; -pub const MADV_DONTDUMP = 16; -pub const MADV_DODUMP = 17; -pub const MADV_WIPEONFORK = 18; -pub const MADV_KEEPONFORK = 19; -pub const MADV_COLD = 20; -pub const MADV_PAGEOUT = 21; -pub const MADV_HWPOISON = 100; -pub const MADV_SOFT_OFFLINE = 101; - -pub const POSIX_FADV_NORMAL = 0; -pub const POSIX_FADV_RANDOM = 1; -pub const POSIX_FADV_SEQUENTIAL = 2; -pub const POSIX_FADV_WILLNEED = 3; -pub usingnamespace switch (arch) { - .s390x => if (@typeInfo(usize).Int.bits == 64) - struct { - pub const POSIX_FADV_DONTNEED = 6; - pub const POSIX_FADV_NOREUSE = 7; - } - else - struct { - pub const POSIX_FADV_DONTNEED = 4; - pub const POSIX_FADV_NOREUSE = 5; - }, - else => struct { - pub const POSIX_FADV_DONTNEED = 4; - pub const POSIX_FADV_NOREUSE = 5; - }, -}; - -pub const __kernel_timespec = extern struct { - tv_sec: i64, - tv_nsec: i64, -}; diff --git a/lib/std/os/bits/linux/i386.zig b/lib/std/os/bits/linux/i386.zig index 12c8fa4713..723a249177 100644 --- a/lib/std/os/bits/linux/i386.zig +++ b/lib/std/os/bits/linux/i386.zig @@ -448,94 +448,145 @@ pub const SYS = enum(usize) { _, }; -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; +pub const O = struct { + pub const RDONLY = 0o0; + pub const WRONLY = 0o1; + pub const RDWR = 0o2; -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0o100000; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; + pub const CREAT = 0o100; + pub const EXCL = 0o200; + pub const NOCTTY = 0o400; + pub const TRUNC = 0o1000; + pub const APPEND = 0o2000; + pub const NONBLOCK = 0o4000; + pub const DSYNC = 0o10000; + pub const SYNC = 0o4010000; + pub const RSYNC = 0o4010000; + pub const DIRECTORY = 0o200000; + pub const NOFOLLOW = 0o400000; + pub const CLOEXEC = 0o2000000; -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; + pub const ASYNC = 0o20000; + pub const DIRECT = 0o40000; + pub const LARGEFILE = 0o100000; + pub const NOATIME = 0o1000000; + pub const PATH = 0o10000000; + pub const TMPFILE = 0o20200000; + pub const NDELAY = NONBLOCK; +}; -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + pub const SETOWN = 8; + pub const GETOWN = 9; + pub const SETSIG = 10; + pub const GETSIG = 11; + pub const GETLK = 12; + pub const SETLK = 13; + pub const SETLKW = 14; + pub const SETOWN_EX = 15; + pub const GETOWN_EX = 16; + pub const GETOWNER_UIDS = 17; -pub const F_GETLK = 12; -pub const F_SETLK = 13; -pub const F_SETLKW = 14; + pub const RDLCK = 0; + pub const WRLCK = 1; + pub const UNLCK = 2; +}; -pub const F_RDLCK = 0; -pub const F_WRLCK = 1; -pub const F_UNLCK = 2; +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const NB = 4; + pub const UN = 8; +}; -pub const LOCK_SH = 1; -pub const LOCK_EX = 2; -pub const LOCK_UN = 8; -pub const LOCK_NB = 4; +pub const MAP = struct { + /// Share changes + pub const SHARED = 0x01; -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; + /// Changes are private + pub const PRIVATE = 0x02; -pub const F_GETOWNER_UIDS = 17; + /// share + validate extension flags + pub const SHARED_VALIDATE = 0x03; -pub const MAP_NORESERVE = 0x4000; -pub const MAP_GROWSDOWN = 0x0100; -pub const MAP_DENYWRITE = 0x0800; -pub const MAP_EXECUTABLE = 0x1000; -pub const MAP_LOCKED = 0x2000; -pub const MAP_32BIT = 0x40; + /// Mask for type of mapping + pub const TYPE = 0x0f; + + /// Interpret addr exactly + pub const FIXED = 0x10; + + /// don't use a file + pub const ANONYMOUS = if (is_mips) 0x800 else 0x20; + + /// populate (prefault) pagetables + pub const POPULATE = if (is_mips) 0x10000 else 0x8000; + + /// do not block on IO + pub const NONBLOCK = if (is_mips) 0x20000 else 0x10000; + + /// give out an address that is best suited for process/thread stacks + pub const STACK = if (is_mips) 0x40000 else 0x20000; + + /// create a huge page mapping + pub const HUGETLB = if (is_mips) 0x80000 else 0x40000; + + /// perform synchronous page faults for the mapping + pub const SYNC = 0x80000; + + /// FIXED which doesn't unmap underlying mapping + pub const FIXED_NOREPLACE = 0x100000; + + /// For anonymous mmap, memory could be uninitialized + pub const UNINITIALIZED = 0x4000000; + + pub const NORESERVE = 0x4000; + pub const GROWSDOWN = 0x0100; + pub const DENYWRITE = 0x0800; + pub const EXECUTABLE = 0x1000; + pub const LOCKED = 0x2000; + pub const @"32BIT" = 0x40; +}; pub const MMAP2_UNIT = 4096; -pub const VDSO_CGT_SYM = "__vdso_clock_gettime"; -pub const VDSO_CGT_VER = "LINUX_2.6"; +pub const VDSO = struct { + pub const CGT_SYM = "__vdso_clock_gettime"; + pub const CGT_VER = "LINUX_2.6"; +}; + +pub const ARCH = struct {}; pub const Flock = extern struct { - l_type: i16, - l_whence: i16, - l_start: off_t, - l_len: off_t, - l_pid: pid_t, + type: i16, + whence: i16, + start: off_t, + len: off_t, + pid: pid_t, }; pub const msghdr = extern struct { - msg_name: ?*sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec, - msg_iovlen: i32, - msg_control: ?*c_void, - msg_controllen: socklen_t, - msg_flags: i32, + name: ?*sockaddr, + namelen: socklen_t, + iov: [*]iovec, + iovlen: i32, + control: ?*c_void, + controllen: socklen_t, + flags: i32, }; pub const msghdr_const = extern struct { - msg_name: ?*const sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec_const, - msg_iovlen: i32, - msg_control: ?*c_void, - msg_controllen: socklen_t, - msg_flags: i32, + name: ?*const sockaddr, + namelen: socklen_t, + iov: [*]iovec_const, + iovlen: i32, + control: ?*c_void, + controllen: socklen_t, + flags: i32, }; pub const blksize_t = i32; @@ -604,25 +655,27 @@ pub const mcontext_t = extern struct { cr2: usize, }; -pub const REG_GS = 0; -pub const REG_FS = 1; -pub const REG_ES = 2; -pub const REG_DS = 3; -pub const REG_EDI = 4; -pub const REG_ESI = 5; -pub const REG_EBP = 6; -pub const REG_ESP = 7; -pub const REG_EBX = 8; -pub const REG_EDX = 9; -pub const REG_ECX = 10; -pub const REG_EAX = 11; -pub const REG_TRAPNO = 12; -pub const REG_ERR = 13; -pub const REG_EIP = 14; -pub const REG_CS = 15; -pub const REG_EFL = 16; -pub const REG_UESP = 17; -pub const REG_SS = 18; +pub const REG = struct { + pub const GS = 0; + pub const FS = 1; + pub const ES = 2; + pub const DS = 3; + pub const EDI = 4; + pub const ESI = 5; + pub const EBP = 6; + pub const ESP = 7; + pub const EBX = 8; + pub const EDX = 9; + pub const ECX = 10; + pub const EAX = 11; + pub const TRAPNO = 12; + pub const ERR = 13; + pub const EIP = 14; + pub const CS = 15; + pub const EFL = 16; + pub const UESP = 17; + pub const SS = 18; +}; pub const ucontext_t = extern struct { flags: usize, @@ -647,24 +700,26 @@ pub const user_desc = packed struct { useable: u1, }; -// socketcall() call numbers -pub const SC_socket = 1; -pub const SC_bind = 2; -pub const SC_connect = 3; -pub const SC_listen = 4; -pub const SC_accept = 5; -pub const SC_getsockname = 6; -pub const SC_getpeername = 7; -pub const SC_socketpair = 8; -pub const SC_send = 9; -pub const SC_recv = 10; -pub const SC_sendto = 11; -pub const SC_recvfrom = 12; -pub const SC_shutdown = 13; -pub const SC_setsockopt = 14; -pub const SC_getsockopt = 15; -pub const SC_sendmsg = 16; -pub const SC_recvmsg = 17; -pub const SC_accept4 = 18; -pub const SC_recvmmsg = 19; -pub const SC_sendmmsg = 20; +/// socketcall() call numbers +pub const SC = struct { + pub const socket = 1; + pub const bind = 2; + pub const connect = 3; + pub const listen = 4; + pub const accept = 5; + pub const getsockname = 6; + pub const getpeername = 7; + pub const socketpair = 8; + pub const send = 9; + pub const recv = 10; + pub const sendto = 11; + pub const recvfrom = 12; + pub const shutdown = 13; + pub const setsockopt = 14; + pub const getsockopt = 15; + pub const sendmsg = 16; + pub const recvmsg = 17; + pub const accept4 = 18; + pub const recvmmsg = 19; + pub const sendmmsg = 20; +}; diff --git a/lib/std/os/bits/linux/netlink.zig b/lib/std/os/bits/linux/netlink.zig deleted file mode 100644 index 9de546fb20..0000000000 --- a/lib/std/os/bits/linux/netlink.zig +++ /dev/null @@ -1,498 +0,0 @@ -usingnamespace @import("../linux.zig"); - -/// Routing/device hook -pub const NETLINK_ROUTE = 0; - -/// Unused number -pub const NETLINK_UNUSED = 1; - -/// Reserved for user mode socket protocols -pub const NETLINK_USERSOCK = 2; - -/// Unused number, formerly ip_queue -pub const NETLINK_FIREWALL = 3; - -/// socket monitoring -pub const NETLINK_SOCK_DIAG = 4; - -/// netfilter/iptables ULOG -pub const NETLINK_NFLOG = 5; - -/// ipsec -pub const NETLINK_XFRM = 6; - -/// SELinux event notifications -pub const NETLINK_SELINUX = 7; - -/// Open-iSCSI -pub const NETLINK_ISCSI = 8; - -/// auditing -pub const NETLINK_AUDIT = 9; - -pub const NETLINK_FIB_LOOKUP = 10; - -pub const NETLINK_CONNECTOR = 11; - -/// netfilter subsystem -pub const NETLINK_NETFILTER = 12; - -pub const NETLINK_IP6_FW = 13; - -/// DECnet routing messages -pub const NETLINK_DNRTMSG = 14; - -/// Kernel messages to userspace -pub const NETLINK_KOBJECT_UEVENT = 15; - -pub const NETLINK_GENERIC = 16; - -// leave room for NETLINK_DM (DM Events) - -/// SCSI Transports -pub const NETLINK_SCSITRANSPORT = 18; - -pub const NETLINK_ECRYPTFS = 19; - -pub const NETLINK_RDMA = 20; - -/// Crypto layer -pub const NETLINK_CRYPTO = 21; - -/// SMC monitoring -pub const NETLINK_SMC = 22; - -// Flags values - -/// It is request message. -pub const NLM_F_REQUEST = 0x01; - -/// Multipart message, terminated by NLMSG_DONE -pub const NLM_F_MULTI = 0x02; - -/// Reply with ack, with zero or error code -pub const NLM_F_ACK = 0x04; - -/// Echo this request -pub const NLM_F_ECHO = 0x08; - -/// Dump was inconsistent due to sequence change -pub const NLM_F_DUMP_INTR = 0x10; - -/// Dump was filtered as requested -pub const NLM_F_DUMP_FILTERED = 0x20; - -// Modifiers to GET request - -/// specify tree root -pub const NLM_F_ROOT = 0x100; - -/// return all matching -pub const NLM_F_MATCH = 0x200; - -/// atomic GET -pub const NLM_F_ATOMIC = 0x400; -pub const NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH; - -// Modifiers to NEW request - -/// Override existing -pub const NLM_F_REPLACE = 0x100; - -/// Do not touch, if it exists -pub const NLM_F_EXCL = 0x200; - -/// Create, if it does not exist -pub const NLM_F_CREATE = 0x400; - -/// Add to end of list -pub const NLM_F_APPEND = 0x800; - -// Modifiers to DELETE request - -/// Do not delete recursively -pub const NLM_F_NONREC = 0x100; - -// Flags for ACK message - -/// request was capped -pub const NLM_F_CAPPED = 0x100; - -/// extended ACK TVLs were included -pub const NLM_F_ACK_TLVS = 0x200; - -pub const NetlinkMessageType = enum(u16) { - /// < 0x10: reserved control messages - pub const MIN_TYPE = 0x10; - - /// Nothing. - NOOP = 0x1, - - /// Error - ERROR = 0x2, - - /// End of a dump - DONE = 0x3, - - /// Data lost - OVERRUN = 0x4, - - // rtlink types - - RTM_NEWLINK = 16, - RTM_DELLINK, - RTM_GETLINK, - RTM_SETLINK, - - RTM_NEWADDR = 20, - RTM_DELADDR, - RTM_GETADDR, - - RTM_NEWROUTE = 24, - RTM_DELROUTE, - RTM_GETROUTE, - - RTM_NEWNEIGH = 28, - RTM_DELNEIGH, - RTM_GETNEIGH, - - RTM_NEWRULE = 32, - RTM_DELRULE, - RTM_GETRULE, - - RTM_NEWQDISC = 36, - RTM_DELQDISC, - RTM_GETQDISC, - - RTM_NEWTCLASS = 40, - RTM_DELTCLASS, - RTM_GETTCLASS, - - RTM_NEWTFILTER = 44, - RTM_DELTFILTER, - RTM_GETTFILTER, - - RTM_NEWACTION = 48, - RTM_DELACTION, - RTM_GETACTION, - - RTM_NEWPREFIX = 52, - - RTM_GETMULTICAST = 58, - - RTM_GETANYCAST = 62, - - RTM_NEWNEIGHTBL = 64, - RTM_GETNEIGHTBL = 66, - RTM_SETNEIGHTBL, - - RTM_NEWNDUSEROPT = 68, - - RTM_NEWADDRLABEL = 72, - RTM_DELADDRLABEL, - RTM_GETADDRLABEL, - - RTM_GETDCB = 78, - RTM_SETDCB, - - RTM_NEWNETCONF = 80, - RTM_DELNETCONF, - RTM_GETNETCONF = 82, - - RTM_NEWMDB = 84, - RTM_DELMDB = 85, - RTM_GETMDB = 86, - - RTM_NEWNSID = 88, - RTM_DELNSID = 89, - RTM_GETNSID = 90, - - RTM_NEWSTATS = 92, - RTM_GETSTATS = 94, - - RTM_NEWCACHEREPORT = 96, - - RTM_NEWCHAIN = 100, - RTM_DELCHAIN, - RTM_GETCHAIN, - - RTM_NEWNEXTHOP = 104, - RTM_DELNEXTHOP, - RTM_GETNEXTHOP, - - _, -}; - -/// Netlink socket address -pub const sockaddr_nl = extern struct { - family: sa_family_t = AF_NETLINK, - __pad1: c_ushort = 0, - - /// port ID - pid: u32, - - /// multicast groups mask - groups: u32, -}; - -/// Netlink message header -/// Specified in RFC 3549 Section 2.3.2 -pub const nlmsghdr = extern struct { - /// Length of message including header - len: u32, - - /// Message content - @"type": NetlinkMessageType, - - /// Additional flags - flags: u16, - - /// Sequence number - seq: u32, - - /// Sending process port ID - pid: u32, -}; - -pub const ifinfomsg = extern struct { - family: u8, - __pad1: u8 = 0, - - /// ARPHRD_* - @"type": c_ushort, - - /// Link index - index: c_int, - - /// IFF_* flags - flags: c_uint, - - /// IFF_* change mask - change: c_uint, -}; - -pub const rtattr = extern struct { - /// Length of option - len: c_ushort, - - /// Type of option - @"type": IFLA, - - pub const ALIGNTO = 4; -}; - -pub const IFLA = enum(c_ushort) { - UNSPEC, - ADDRESS, - BROADCAST, - IFNAME, - MTU, - LINK, - QDISC, - STATS, - COST, - PRIORITY, - MASTER, - - /// Wireless Extension event - WIRELESS, - - /// Protocol specific information for a link - PROTINFO, - - TXQLEN, - MAP, - WEIGHT, - OPERSTATE, - LINKMODE, - LINKINFO, - NET_NS_PID, - IFALIAS, - - /// Number of VFs if device is SR-IOV PF - NUM_VF, - - VFINFO_LIST, - STATS64, - VF_PORTS, - PORT_SELF, - AF_SPEC, - - /// Group the device belongs to - GROUP, - - NET_NS_FD, - - /// Extended info mask, VFs, etc - EXT_MASK, - - /// Promiscuity count: > 0 means acts PROMISC - PROMISCUITY, - - NUM_TX_QUEUES, - NUM_RX_QUEUES, - CARRIER, - PHYS_PORT_ID, - CARRIER_CHANGES, - PHYS_SWITCH_ID, - LINK_NETNSID, - PHYS_PORT_NAME, - PROTO_DOWN, - GSO_MAX_SEGS, - GSO_MAX_SIZE, - PAD, - XDP, - EVENT, - - NEW_NETNSID, - IF_NETNSID, - - CARRIER_UP_COUNT, - CARRIER_DOWN_COUNT, - NEW_IFINDEX, - MIN_MTU, - MAX_MTU, - - _, - - pub const TARGET_NETNSID: IFLA = .IF_NETNSID; -}; - -pub const rtnl_link_ifmap = extern struct { - mem_start: u64, - mem_end: u64, - base_addr: u64, - irq: u16, - dma: u8, - port: u8, -}; - -pub const rtnl_link_stats = extern struct { - /// total packets received - rx_packets: u32, - - /// total packets transmitted - tx_packets: u32, - - /// total bytes received - rx_bytes: u32, - - /// total bytes transmitted - tx_bytes: u32, - - /// bad packets received - rx_errors: u32, - - /// packet transmit problems - tx_errors: u32, - - /// no space in linux buffers - rx_dropped: u32, - - /// no space available in linux - tx_dropped: u32, - - /// multicast packets received - multicast: u32, - - collisions: u32, - - // detailed rx_errors - - rx_length_errors: u32, - - /// receiver ring buff overflow - rx_over_errors: u32, - - /// recved pkt with crc error - rx_crc_errors: u32, - - /// recv'd frame alignment error - rx_frame_errors: u32, - - /// recv'r fifo overrun - rx_fifo_errors: u32, - - /// receiver missed packet - rx_missed_errors: u32, - - // detailed tx_errors - tx_aborted_errors: u32, - tx_carrier_errors: u32, - tx_fifo_errors: u32, - tx_heartbeat_errors: u32, - tx_window_errors: u32, - - // for cslip etc - - rx_compressed: u32, - tx_compressed: u32, - - /// dropped, no handler found - rx_nohandler: u32, -}; - -pub const rtnl_link_stats64 = extern struct { - /// total packets received - rx_packets: u64, - - /// total packets transmitted - tx_packets: u64, - - /// total bytes received - rx_bytes: u64, - - /// total bytes transmitted - tx_bytes: u64, - - /// bad packets received - rx_errors: u64, - - /// packet transmit problems - tx_errors: u64, - - /// no space in linux buffers - rx_dropped: u64, - - /// no space available in linux - tx_dropped: u64, - - /// multicast packets received - multicast: u64, - - collisions: u64, - - // detailed rx_errors - - rx_length_errors: u64, - - /// receiver ring buff overflow - rx_over_errors: u64, - - /// recved pkt with crc error - rx_crc_errors: u64, - - /// recv'd frame alignment error - rx_frame_errors: u64, - - /// recv'r fifo overrun - rx_fifo_errors: u64, - - /// receiver missed packet - rx_missed_errors: u64, - - // detailed tx_errors - tx_aborted_errors: u64, - tx_carrier_errors: u64, - tx_fifo_errors: u64, - tx_heartbeat_errors: u64, - tx_window_errors: u64, - - // for cslip etc - - rx_compressed: u64, - tx_compressed: u64, - - /// dropped, no handler found - rx_nohandler: u64, -}; diff --git a/lib/std/os/bits/linux/prctl.zig b/lib/std/os/bits/linux/prctl.zig deleted file mode 100644 index d29b6021b6..0000000000 --- a/lib/std/os/bits/linux/prctl.zig +++ /dev/null @@ -1,234 +0,0 @@ -pub const PR = enum(i32) { - SET_PDEATHSIG = 1, - GET_PDEATHSIG = 2, - - GET_DUMPABLE = 3, - SET_DUMPABLE = 4, - - GET_UNALIGN = 5, - SET_UNALIGN = 6, - - GET_KEEPCAPS = 7, - SET_KEEPCAPS = 8, - - GET_FPEMU = 9, - SET_FPEMU = 10, - - GET_FPEXC = 11, - SET_FPEXC = 12, - - GET_TIMING = 13, - SET_TIMING = 14, - - SET_NAME = 15, - GET_NAME = 16, - - GET_ENDIAN = 19, - SET_ENDIAN = 20, - - GET_SECCOMP = 21, - SET_SECCOMP = 22, - - CAPBSET_READ = 23, - CAPBSET_DROP = 24, - - GET_TSC = 25, - SET_TSC = 26, - - GET_SECUREBITS = 27, - SET_SECUREBITS = 28, - - SET_TIMERSLACK = 29, - GET_TIMERSLACK = 30, - - TASK_PERF_EVENTS_DISABLE = 31, - TASK_PERF_EVENTS_ENABLE = 32, - - MCE_KILL = 33, - - MCE_KILL_GET = 34, - - SET_MM = 35, - - SET_PTRACER = 0x59616d61, - - SET_CHILD_SUBREAPER = 36, - GET_CHILD_SUBREAPER = 37, - - SET_NO_NEW_PRIVS = 38, - GET_NO_NEW_PRIVS = 39, - - GET_TID_ADDRESS = 40, - - SET_THP_DISABLE = 41, - GET_THP_DISABLE = 42, - - MPX_ENABLE_MANAGEMENT = 43, - MPX_DISABLE_MANAGEMENT = 44, - - SET_FP_MODE = 45, - GET_FP_MODE = 46, - - CAP_AMBIENT = 47, - - SVE_SET_VL = 50, - SVE_GET_VL = 51, - - GET_SPECULATION_CTRL = 52, - SET_SPECULATION_CTRL = 53, - - _, -}; - -pub const PR_SET_PDEATHSIG = @enumToInt(PR.SET_PDEATHSIG); -pub const PR_GET_PDEATHSIG = @enumToInt(PR.GET_PDEATHSIG); - -pub const PR_GET_DUMPABLE = @enumToInt(PR.GET_DUMPABLE); -pub const PR_SET_DUMPABLE = @enumToInt(PR.SET_DUMPABLE); - -pub const PR_GET_UNALIGN = @enumToInt(PR.GET_UNALIGN); -pub const PR_SET_UNALIGN = @enumToInt(PR.SET_UNALIGN); -pub const PR_UNALIGN_NOPRINT = 1; -pub const PR_UNALIGN_SIGBUS = 2; - -pub const PR_GET_KEEPCAPS = @enumToInt(PR.GET_KEEPCAPS); -pub const PR_SET_KEEPCAPS = @enumToInt(PR.SET_KEEPCAPS); - -pub const PR_GET_FPEMU = @enumToInt(PR.GET_FPEMU); -pub const PR_SET_FPEMU = @enumToInt(PR.SET_FPEMU); -pub const PR_FPEMU_NOPRINT = 1; -pub const PR_FPEMU_SIGFPE = 2; - -pub const PR_GET_FPEXC = @enumToInt(PR.GET_FPEXC); -pub const PR_SET_FPEXC = @enumToInt(PR.SET_FPEXC); -pub const PR_FP_EXC_SW_ENABLE = 0x80; -pub const PR_FP_EXC_DIV = 0x010000; -pub const PR_FP_EXC_OVF = 0x020000; -pub const PR_FP_EXC_UND = 0x040000; -pub const PR_FP_EXC_RES = 0x080000; -pub const PR_FP_EXC_INV = 0x100000; -pub const PR_FP_EXC_DISABLED = 0; -pub const PR_FP_EXC_NONRECOV = 1; -pub const PR_FP_EXC_ASYNC = 2; -pub const PR_FP_EXC_PRECISE = 3; - -pub const PR_GET_TIMING = @enumToInt(PR.GET_TIMING); -pub const PR_SET_TIMING = @enumToInt(PR.SET_TIMING); -pub const PR_TIMING_STATISTICAL = 0; -pub const PR_TIMING_TIMESTAMP = 1; - -pub const PR_SET_NAME = @enumToInt(PR.SET_NAME); -pub const PR_GET_NAME = @enumToInt(PR.GET_NAME); - -pub const PR_GET_ENDIAN = @enumToInt(PR.GET_ENDIAN); -pub const PR_SET_ENDIAN = @enumToInt(PR.SET_ENDIAN); -pub const PR_ENDIAN_BIG = 0; -pub const PR_ENDIAN_LITTLE = 1; -pub const PR_ENDIAN_PPC_LITTLE = 2; - -pub const PR_GET_SECCOMP = @enumToInt(PR.GET_SECCOMP); -pub const PR_SET_SECCOMP = @enumToInt(PR.SET_SECCOMP); - -pub const PR_CAPBSET_READ = @enumToInt(PR.CAPBSET_READ); -pub const PR_CAPBSET_DROP = @enumToInt(PR.CAPBSET_DROP); - -pub const PR_GET_TSC = @enumToInt(PR.GET_TSC); -pub const PR_SET_TSC = @enumToInt(PR.SET_TSC); -pub const PR_TSC_ENABLE = 1; -pub const PR_TSC_SIGSEGV = 2; - -pub const PR_GET_SECUREBITS = @enumToInt(PR.GET_SECUREBITS); -pub const PR_SET_SECUREBITS = @enumToInt(PR.SET_SECUREBITS); - -pub const PR_SET_TIMERSLACK = @enumToInt(PR.SET_TIMERSLACK); -pub const PR_GET_TIMERSLACK = @enumToInt(PR.GET_TIMERSLACK); - -pub const PR_TASK_PERF_EVENTS_DISABLE = @enumToInt(PR.TASK_PERF_EVENTS_DISABLE); -pub const PR_TASK_PERF_EVENTS_ENABLE = @enumToInt(PR.TASK_PERF_EVENTS_ENABLE); - -pub const PR_MCE_KILL = @enumToInt(PR.MCE_KILL); -pub const PR_MCE_KILL_CLEAR = 0; -pub const PR_MCE_KILL_SET = 1; - -pub const PR_MCE_KILL_LATE = 0; -pub const PR_MCE_KILL_EARLY = 1; -pub const PR_MCE_KILL_DEFAULT = 2; - -pub const PR_MCE_KILL_GET = @enumToInt(PR.MCE_KILL_GET); - -pub const PR_SET_MM = @enumToInt(PR.SET_MM); -pub const PR_SET_MM_START_CODE = 1; -pub const PR_SET_MM_END_CODE = 2; -pub const PR_SET_MM_START_DATA = 3; -pub const PR_SET_MM_END_DATA = 4; -pub const PR_SET_MM_START_STACK = 5; -pub const PR_SET_MM_START_BRK = 6; -pub const PR_SET_MM_BRK = 7; -pub const PR_SET_MM_ARG_START = 8; -pub const PR_SET_MM_ARG_END = 9; -pub const PR_SET_MM_ENV_START = 10; -pub const PR_SET_MM_ENV_END = 11; -pub const PR_SET_MM_AUXV = 12; -pub const PR_SET_MM_EXE_FILE = 13; -pub const PR_SET_MM_MAP = 14; -pub const PR_SET_MM_MAP_SIZE = 15; - -pub const prctl_mm_map = extern struct { - start_code: u64, - end_code: u64, - start_data: u64, - end_data: u64, - start_brk: u64, - brk: u64, - start_stack: u64, - arg_start: u64, - arg_end: u64, - env_start: u64, - env_end: u64, - auxv: *u64, - auxv_size: u32, - exe_fd: u32, -}; - -pub const PR_SET_PTRACER = @enumToInt(PR.SET_PTRACER); -pub const PR_SET_PTRACER_ANY = std.math.maxInt(c_ulong); - -pub const PR_SET_CHILD_SUBREAPER = @enumToInt(PR.SET_CHILD_SUBREAPER); -pub const PR_GET_CHILD_SUBREAPER = @enumToInt(PR.GET_CHILD_SUBREAPER); - -pub const PR_SET_NO_NEW_PRIVS = @enumToInt(PR.SET_NO_NEW_PRIVS); -pub const PR_GET_NO_NEW_PRIVS = @enumToInt(PR.GET_NO_NEW_PRIVS); - -pub const PR_GET_TID_ADDRESS = @enumToInt(PR.GET_TID_ADDRESS); - -pub const PR_SET_THP_DISABLE = @enumToInt(PR.SET_THP_DISABLE); -pub const PR_GET_THP_DISABLE = @enumToInt(PR.GET_THP_DISABLE); - -pub const PR_MPX_ENABLE_MANAGEMENT = @enumToInt(PR.MPX_ENABLE_MANAGEMENT); -pub const PR_MPX_DISABLE_MANAGEMENT = @enumToInt(PR.MPX_DISABLE_MANAGEMENT); - -pub const PR_SET_FP_MODE = @enumToInt(PR.SET_FP_MODE); -pub const PR_GET_FP_MODE = @enumToInt(PR.GET_FP_MODE); -pub const PR_FP_MODE_FR = 1 << 0; -pub const PR_FP_MODE_FRE = 1 << 1; - -pub const PR_CAP_AMBIENT = @enumToInt(PR.CAP_AMBIENT); -pub const PR_CAP_AMBIENT_IS_SET = 1; -pub const PR_CAP_AMBIENT_RAISE = 2; -pub const PR_CAP_AMBIENT_LOWER = 3; -pub const PR_CAP_AMBIENT_CLEAR_ALL = 4; - -pub const PR_SVE_SET_VL = @enumToInt(PR.SVE_SET_VL); -pub const PR_SVE_SET_VL_ONEXEC = 1 << 18; -pub const PR_SVE_GET_VL = @enumToInt(PR.SVE_GET_VL); -pub const PR_SVE_VL_LEN_MASK = 0xffff; -pub const PR_SVE_VL_INHERIT = 1 << 17; - -pub const PR_GET_SPECULATION_CTRL = @enumToInt(PR.GET_SPECULATION_CTRL); -pub const PR_SET_SPECULATION_CTRL = @enumToInt(PR.SET_SPECULATION_CTRL); -pub const PR_SPEC_STORE_BYPASS = 0; -pub const PR_SPEC_NOT_AFFECTED = 0; -pub const PR_SPEC_PRCTL = 1 << 0; -pub const PR_SPEC_ENABLE = 1 << 1; -pub const PR_SPEC_DISABLE = 1 << 2; -pub const PR_SPEC_FORCE_DISABLE = 1 << 3; diff --git a/lib/std/os/bits/linux/securebits.zig b/lib/std/os/bits/linux/securebits.zig deleted file mode 100644 index a23ced3cf2..0000000000 --- a/lib/std/os/bits/linux/securebits.zig +++ /dev/null @@ -1,35 +0,0 @@ -fn issecure_mask(comptime x: comptime_int) comptime_int { - return 1 << x; -} - -pub const SECUREBITS_DEFAULT = 0x00000000; - -pub const SECURE_NOROOT = 0; -pub const SECURE_NOROOT_LOCKED = 1; - -pub const SECBIT_NOROOT = issecure_mask(SECURE_NOROOT); -pub const SECBIT_NOROOT_LOCKED = issecure_mask(SECURE_NOROOT_LOCKED); - -pub const SECURE_NO_SETUID_FIXUP = 2; -pub const SECURE_NO_SETUID_FIXUP_LOCKED = 3; - -pub const SECBIT_NO_SETUID_FIXUP = issecure_mask(SECURE_NO_SETUID_FIXUP); -pub const SECBIT_NO_SETUID_FIXUP_LOCKED = issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED); - -pub const SECURE_KEEP_CAPS = 4; -pub const SECURE_KEEP_CAPS_LOCKED = 5; - -pub const SECBIT_KEEP_CAPS = issecure_mask(SECURE_KEEP_CAPS); -pub const SECBIT_KEEP_CAPS_LOCKED = issecure_mask(SECURE_KEEP_CAPS_LOCKED); - -pub const SECURE_NO_CAP_AMBIENT_RAISE = 6; -pub const SECURE_NO_CAP_AMBIENT_RAISE_LOCKED = 7; - -pub const SECBIT_NO_CAP_AMBIENT_RAISE = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); -pub const SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED); - -pub const SECURE_ALL_BITS = issecure_mask(SECURE_NOROOT) | - issecure_mask(SECURE_NO_SETUID_FIXUP) | - issecure_mask(SECURE_KEEP_CAPS) | - issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); -pub const SECURE_ALL_LOCKS = SECURE_ALL_BITS << 1; diff --git a/lib/std/os/bits/linux/x86_64.zig b/lib/std/os/bits/linux/x86_64.zig deleted file mode 100644 index 69beac9d87..0000000000 --- a/lib/std/os/bits/linux/x86_64.zig +++ /dev/null @@ -1,638 +0,0 @@ -// x86-64-specific declarations that are intended to be imported into the POSIX namespace. -const std = @import("../../../std.zig"); -const pid_t = linux.pid_t; -const uid_t = linux.uid_t; -const gid_t = linux.gid_t; -const clock_t = linux.clock_t; -const stack_t = linux.stack_t; -const sigset_t = linux.sigset_t; - -const linux = std.os.linux; -const sockaddr = linux.sockaddr; -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; -const iovec_const = linux.iovec_const; - -pub const mode_t = usize; -pub const time_t = isize; - -pub const SYS = enum(usize) { - read = 0, - write = 1, - open = 2, - close = 3, - stat = 4, - fstat = 5, - lstat = 6, - poll = 7, - lseek = 8, - mmap = 9, - mprotect = 10, - munmap = 11, - brk = 12, - rt_sigaction = 13, - rt_sigprocmask = 14, - rt_sigreturn = 15, - ioctl = 16, - pread = 17, - pwrite = 18, - readv = 19, - writev = 20, - access = 21, - pipe = 22, - select = 23, - sched_yield = 24, - mremap = 25, - msync = 26, - mincore = 27, - madvise = 28, - shmget = 29, - shmat = 30, - shmctl = 31, - dup = 32, - dup2 = 33, - pause = 34, - nanosleep = 35, - getitimer = 36, - alarm = 37, - setitimer = 38, - getpid = 39, - sendfile = 40, - socket = 41, - connect = 42, - accept = 43, - sendto = 44, - recvfrom = 45, - sendmsg = 46, - recvmsg = 47, - shutdown = 48, - bind = 49, - listen = 50, - getsockname = 51, - getpeername = 52, - socketpair = 53, - setsockopt = 54, - getsockopt = 55, - clone = 56, - fork = 57, - vfork = 58, - execve = 59, - exit = 60, - wait4 = 61, - kill = 62, - uname = 63, - semget = 64, - semop = 65, - semctl = 66, - shmdt = 67, - msgget = 68, - msgsnd = 69, - msgrcv = 70, - msgctl = 71, - fcntl = 72, - flock = 73, - fsync = 74, - fdatasync = 75, - truncate = 76, - ftruncate = 77, - getdents = 78, - getcwd = 79, - chdir = 80, - fchdir = 81, - rename = 82, - mkdir = 83, - rmdir = 84, - creat = 85, - link = 86, - unlink = 87, - symlink = 88, - readlink = 89, - chmod = 90, - fchmod = 91, - chown = 92, - fchown = 93, - lchown = 94, - umask = 95, - gettimeofday = 96, - getrlimit = 97, - getrusage = 98, - sysinfo = 99, - times = 100, - ptrace = 101, - getuid = 102, - syslog = 103, - getgid = 104, - setuid = 105, - setgid = 106, - geteuid = 107, - getegid = 108, - setpgid = 109, - getppid = 110, - getpgrp = 111, - setsid = 112, - setreuid = 113, - setregid = 114, - getgroups = 115, - setgroups = 116, - setresuid = 117, - getresuid = 118, - setresgid = 119, - getresgid = 120, - getpgid = 121, - setfsuid = 122, - setfsgid = 123, - getsid = 124, - capget = 125, - capset = 126, - rt_sigpending = 127, - rt_sigtimedwait = 128, - rt_sigqueueinfo = 129, - rt_sigsuspend = 130, - sigaltstack = 131, - utime = 132, - mknod = 133, - uselib = 134, - personality = 135, - ustat = 136, - statfs = 137, - fstatfs = 138, - sysfs = 139, - getpriority = 140, - setpriority = 141, - sched_setparam = 142, - sched_getparam = 143, - sched_setscheduler = 144, - sched_getscheduler = 145, - sched_get_priority_max = 146, - sched_get_priority_min = 147, - sched_rr_get_interval = 148, - mlock = 149, - munlock = 150, - mlockall = 151, - munlockall = 152, - vhangup = 153, - modify_ldt = 154, - pivot_root = 155, - _sysctl = 156, - prctl = 157, - arch_prctl = 158, - adjtimex = 159, - setrlimit = 160, - chroot = 161, - sync = 162, - acct = 163, - settimeofday = 164, - mount = 165, - umount2 = 166, - swapon = 167, - swapoff = 168, - reboot = 169, - sethostname = 170, - setdomainname = 171, - iopl = 172, - ioperm = 173, - create_module = 174, - init_module = 175, - delete_module = 176, - get_kernel_syms = 177, - query_module = 178, - quotactl = 179, - nfsservctl = 180, - getpmsg = 181, - putpmsg = 182, - afs_syscall = 183, - tuxcall = 184, - security = 185, - gettid = 186, - readahead = 187, - setxattr = 188, - lsetxattr = 189, - fsetxattr = 190, - getxattr = 191, - lgetxattr = 192, - fgetxattr = 193, - listxattr = 194, - llistxattr = 195, - flistxattr = 196, - removexattr = 197, - lremovexattr = 198, - fremovexattr = 199, - tkill = 200, - time = 201, - futex = 202, - sched_setaffinity = 203, - sched_getaffinity = 204, - set_thread_area = 205, - io_setup = 206, - io_destroy = 207, - io_getevents = 208, - io_submit = 209, - io_cancel = 210, - get_thread_area = 211, - lookup_dcookie = 212, - epoll_create = 213, - epoll_ctl_old = 214, - epoll_wait_old = 215, - remap_file_pages = 216, - getdents64 = 217, - set_tid_address = 218, - restart_syscall = 219, - semtimedop = 220, - fadvise64 = 221, - timer_create = 222, - timer_settime = 223, - timer_gettime = 224, - timer_getoverrun = 225, - timer_delete = 226, - clock_settime = 227, - clock_gettime = 228, - clock_getres = 229, - clock_nanosleep = 230, - exit_group = 231, - epoll_wait = 232, - epoll_ctl = 233, - tgkill = 234, - utimes = 235, - vserver = 236, - mbind = 237, - set_mempolicy = 238, - get_mempolicy = 239, - mq_open = 240, - mq_unlink = 241, - mq_timedsend = 242, - mq_timedreceive = 243, - mq_notify = 244, - mq_getsetattr = 245, - kexec_load = 246, - waitid = 247, - add_key = 248, - request_key = 249, - keyctl = 250, - ioprio_set = 251, - ioprio_get = 252, - inotify_init = 253, - inotify_add_watch = 254, - inotify_rm_watch = 255, - migrate_pages = 256, - openat = 257, - mkdirat = 258, - mknodat = 259, - fchownat = 260, - futimesat = 261, - fstatat = 262, - unlinkat = 263, - renameat = 264, - linkat = 265, - symlinkat = 266, - readlinkat = 267, - fchmodat = 268, - faccessat = 269, - pselect6 = 270, - ppoll = 271, - unshare = 272, - set_robust_list = 273, - get_robust_list = 274, - splice = 275, - tee = 276, - sync_file_range = 277, - vmsplice = 278, - move_pages = 279, - utimensat = 280, - epoll_pwait = 281, - signalfd = 282, - timerfd_create = 283, - eventfd = 284, - fallocate = 285, - timerfd_settime = 286, - timerfd_gettime = 287, - accept4 = 288, - signalfd4 = 289, - eventfd2 = 290, - epoll_create1 = 291, - dup3 = 292, - pipe2 = 293, - inotify_init1 = 294, - preadv = 295, - pwritev = 296, - rt_tgsigqueueinfo = 297, - perf_event_open = 298, - recvmmsg = 299, - fanotify_init = 300, - fanotify_mark = 301, - prlimit64 = 302, - name_to_handle_at = 303, - open_by_handle_at = 304, - clock_adjtime = 305, - syncfs = 306, - sendmmsg = 307, - setns = 308, - getcpu = 309, - process_vm_readv = 310, - process_vm_writev = 311, - kcmp = 312, - finit_module = 313, - sched_setattr = 314, - sched_getattr = 315, - renameat2 = 316, - seccomp = 317, - getrandom = 318, - memfd_create = 319, - kexec_file_load = 320, - bpf = 321, - execveat = 322, - userfaultfd = 323, - membarrier = 324, - mlock2 = 325, - copy_file_range = 326, - preadv2 = 327, - pwritev2 = 328, - pkey_mprotect = 329, - pkey_alloc = 330, - pkey_free = 331, - statx = 332, - io_pgetevents = 333, - rseq = 334, - pidfd_send_signal = 424, - io_uring_setup = 425, - io_uring_enter = 426, - io_uring_register = 427, - open_tree = 428, - move_mount = 429, - fsopen = 430, - fsconfig = 431, - fsmount = 432, - fspick = 433, - pidfd_open = 434, - clone3 = 435, - close_range = 436, - openat2 = 437, - pidfd_getfd = 438, - faccessat2 = 439, - process_madvise = 440, - epoll_pwait2 = 441, - - _, -}; - -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; - -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; - -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; - -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; - -pub const F_GETLK = 5; -pub const F_SETLK = 6; -pub const F_SETLKW = 7; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -/// only give out 32bit addresses -pub const MAP_32BIT = 0x40; - -/// stack-like segment -pub const MAP_GROWSDOWN = 0x0100; - -/// ETXTBSY -pub const MAP_DENYWRITE = 0x0800; - -/// mark it as an executable -pub const MAP_EXECUTABLE = 0x1000; - -/// pages are locked -pub const MAP_LOCKED = 0x2000; - -/// don't check for reservations -pub const MAP_NORESERVE = 0x4000; - -pub const VDSO_CGT_SYM = "__vdso_clock_gettime"; -pub const VDSO_CGT_VER = "LINUX_2.6"; -pub const VDSO_GETCPU_SYM = "__vdso_getcpu"; -pub const VDSO_GETCPU_VER = "LINUX_2.6"; - -pub const ARCH_SET_GS = 0x1001; -pub const ARCH_SET_FS = 0x1002; -pub const ARCH_GET_FS = 0x1003; -pub const ARCH_GET_GS = 0x1004; - -pub const REG_R8 = 0; -pub const REG_R9 = 1; -pub const REG_R10 = 2; -pub const REG_R11 = 3; -pub const REG_R12 = 4; -pub const REG_R13 = 5; -pub const REG_R14 = 6; -pub const REG_R15 = 7; -pub const REG_RDI = 8; -pub const REG_RSI = 9; -pub const REG_RBP = 10; -pub const REG_RBX = 11; -pub const REG_RDX = 12; -pub const REG_RAX = 13; -pub const REG_RCX = 14; -pub const REG_RSP = 15; -pub const REG_RIP = 16; -pub const REG_EFL = 17; -pub const REG_CSGSFS = 18; -pub const REG_ERR = 19; -pub const REG_TRAPNO = 20; -pub const REG_OLDMASK = 21; -pub const REG_CR2 = 22; - -pub const LOCK_SH = 1; -pub const LOCK_EX = 2; -pub const LOCK_UN = 8; -pub const LOCK_NB = 4; - -pub const F_RDLCK = 0; -pub const F_WRLCK = 1; -pub const F_UNLCK = 2; - -pub const Flock = extern struct { - l_type: i16, - l_whence: i16, - l_start: off_t, - l_len: off_t, - l_pid: pid_t, -}; - -pub const msghdr = extern struct { - msg_name: ?*sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec, - msg_iovlen: i32, - __pad1: i32 = 0, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t = 0, - msg_flags: i32, -}; - -pub const msghdr_const = extern struct { - msg_name: ?*const sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec_const, - msg_iovlen: i32, - __pad1: i32 = 0, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t = 0, - msg_flags: i32, -}; - -pub const off_t = i64; -pub const ino_t = u64; -pub const dev_t = u64; - -// The `stat` definition used by the Linux kernel. -pub const kernel_stat = extern struct { - dev: dev_t, - ino: ino_t, - nlink: usize, - - mode: u32, - uid: uid_t, - gid: gid_t, - __pad0: u32, - rdev: dev_t, - size: off_t, - blksize: isize, - blocks: i64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - __unused: [3]isize, - - pub fn atime(self: @This()) timespec { - return self.atim; - } - - pub fn mtime(self: @This()) timespec { - return self.mtim; - } - - pub fn ctime(self: @This()) timespec { - return self.ctim; - } -}; - -// The `stat64` definition used by the libc. -pub const libc_stat = kernel_stat; - -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; - -pub const timeval = extern struct { - tv_sec: isize, - tv_usec: isize, -}; - -pub const timezone = extern struct { - tz_minuteswest: i32, - tz_dsttime: i32, -}; - -pub const Elf_Symndx = u32; - -pub const greg_t = usize; -pub const gregset_t = [23]greg_t; -pub const fpstate = extern struct { - cwd: u16, - swd: u16, - ftw: u16, - fop: u16, - rip: usize, - rdp: usize, - mxcsr: u32, - mxcr_mask: u32, - st: [8]extern struct { - significand: [4]u16, - exponent: u16, - padding: [3]u16 = undefined, - }, - xmm: [16]extern struct { - element: [4]u32, - }, - padding: [24]u32 = undefined, -}; -pub const fpregset_t = *fpstate; -pub const sigcontext = extern struct { - r8: usize, - r9: usize, - r10: usize, - r11: usize, - r12: usize, - r13: usize, - r14: usize, - r15: usize, - - rdi: usize, - rsi: usize, - rbp: usize, - rbx: usize, - rdx: usize, - rax: usize, - rcx: usize, - rsp: usize, - rip: usize, - eflags: usize, - - cs: u16, - gs: u16, - fs: u16, - pad0: u16 = undefined, - - err: usize, - trapno: usize, - oldmask: usize, - cr2: usize, - - fpstate: *fpstate, - reserved1: [8]usize = undefined, -}; - -pub const mcontext_t = extern struct { - gregs: gregset_t, - fpregs: fpregset_t, - reserved1: [8]usize = undefined, -}; - -pub const ucontext_t = extern struct { - flags: usize, - link: *ucontext_t, - stack: stack_t, - mcontext: mcontext_t, - sigmask: sigset_t, - fpregs_mem: [64]usize, -}; diff --git a/lib/std/os/bits/linux/xdp.zig b/lib/std/os/bits/linux/xdp.zig deleted file mode 100644 index 88fa9918a3..0000000000 --- a/lib/std/os/bits/linux/xdp.zig +++ /dev/null @@ -1,77 +0,0 @@ -usingnamespace @import("../linux.zig"); - -pub const XDP_SHARED_UMEM = (1 << 0); -pub const XDP_COPY = (1 << 1); -pub const XDP_ZEROCOPY = (1 << 2); - -pub const XDP_UMEM_UNALIGNED_CHUNK_FLAG = (1 << 0); - -pub const sockaddr_xdp = extern struct { - family: u16 = AF_XDP, - flags: u16, - ifindex: u32, - queue_id: u32, - shared_umem_fd: u32, -}; - -pub const XDP_USE_NEED_WAKEUP = (1 << 3); - -pub const xdp_ring_offset = extern struct { - producer: u64, - consumer: u64, - desc: u64, - flags: u64, -}; - -pub const xdp_mmap_offsets = extern struct { - rx: xdp_ring_offset, - tx: xdp_ring_offset, - fr: xdp_ring_offset, - cr: xdp_ring_offset, -}; - -pub const XDP_MMAP_OFFSETS = 1; -pub const XDP_RX_RING = 2; -pub const XDP_TX_RING = 3; -pub const XDP_UMEM_REG = 4; -pub const XDP_UMEM_FILL_RING = 5; -pub const XDP_UMEM_COMPLETION_RING = 6; -pub const XDP_STATISTICS = 7; -pub const XDP_OPTIONS = 8; - -pub const xdp_umem_reg = extern struct { - addr: u64, - len: u64, - chunk_size: u32, - headroom: u32, - flags: u32, -}; - -pub const xdp_statistics = extern struct { - rx_dropped: u64, - rx_invalid_descs: u64, - tx_invalid_descs: u64, - rx_ring_full: u64, - rx_fill_ring_empty_descs: u64, - tx_ring_empty_descs: u64, -}; - -pub const xdp_options = extern struct { - flags: u32, -}; - -pub const XDP_OPTIONS_ZEROCOPY = (1 << 0); - -pub const XDP_PGOFF_RX_RING = 0; -pub const XDP_PGOFF_TX_RING = 0x80000000; -pub const XDP_UMEM_PGOFF_FILL_RING = 0x100000000; -pub const XDP_UMEM_PGOFF_COMPLETION_RING = 0x180000000; - -pub const XSK_UNALIGNED_BUF_OFFSET_SHIFT = 48; -pub const XSK_UNALIGNED_BUF_ADDR_MASK = (1 << XSK_UNALIGNED_BUF_OFFSET_SHIFT) - 1; - -pub const xdp_desc = extern struct { - addr: u64, - len: u32, - options: u32, -}; diff --git a/lib/std/os/bits/netbsd.zig b/lib/std/os/bits/netbsd.zig index be35889042..2ed4d89aa4 100644 --- a/lib/std/os/bits/netbsd.zig +++ b/lib/std/os/bits/netbsd.zig @@ -2,8 +2,6 @@ const std = @import("../../std.zig"); const builtin = std.builtin; const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - pub const blkcnt_t = i64; pub const blksize_t = i32; pub const clock_t = u32; diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index c7d1c0583b..d984c9063e 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -2,8 +2,6 @@ const std = @import("../../std.zig"); const builtin = std.builtin; const maxInt = std.math.maxInt; -pub usingnamespace @import("posix.zig"); - pub const blkcnt_t = i64; pub const blksize_t = i32; pub const clock_t = i64; diff --git a/lib/std/os/bits/posix.zig b/lib/std/os/bits/posix.zig deleted file mode 100644 index 6ffd2dcf6a..0000000000 --- a/lib/std/os/bits/posix.zig +++ /dev/null @@ -1,28 +0,0 @@ -pub const iovec = extern struct { - iov_base: [*]u8, - iov_len: usize, -}; - -pub const iovec_const = extern struct { - iov_base: [*]const u8, - iov_len: usize, -}; - -// syslog - -/// system is unusable -pub const LOG_EMERG = 0; -/// action must be taken immediately -pub const LOG_ALERT = 1; -/// critical conditions -pub const LOG_CRIT = 2; -/// error conditions -pub const LOG_ERR = 3; -/// warning conditions -pub const LOG_WARNING = 4; -/// normal but significant condition -pub const LOG_NOTICE = 5; -/// informational -pub const LOG_INFO = 6; -/// debug-level messages -pub const LOG_DEBUG = 7; diff --git a/lib/std/os/bits/wasi.zig b/lib/std/os/bits/wasi.zig index 567d74961e..91cf0653af 100644 --- a/lib/std/os/bits/wasi.zig +++ b/lib/std/os/bits/wasi.zig @@ -1,6 +1,5 @@ // Convenience types and consts used by std.os module const builtin = @import("builtin"); -const posix = @import("posix.zig"); pub const iovec = posix.iovec; pub const iovec_const = posix.iovec_const; @@ -87,10 +86,12 @@ pub const ADVICE_DONTNEED: advice_t = 4; pub const ADVICE_NOREUSE: advice_t = 5; pub const clockid_t = u32; -pub const CLOCK_REALTIME: clockid_t = 0; -pub const CLOCK_MONOTONIC: clockid_t = 1; -pub const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2; -pub const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3; +pub const CLOCK = struct { + pub const REALTIME: clockid_t = 0; + pub const MONOTONIC: clockid_t = 1; + pub const PROCESS_CPUTIME_ID: clockid_t = 2; + pub const THREAD_CPUTIME_ID: clockid_t = 3; +}; pub const device_t = u64; diff --git a/lib/std/os/bits/windows.zig b/lib/std/os/bits/windows.zig index bb57a13ffe..e38407579a 100644 --- a/lib/std/os/bits/windows.zig +++ b/lib/std/os/bits/windows.zig @@ -3,11 +3,6 @@ usingnamespace @import("../windows/bits.zig"); const ws2_32 = @import("../windows/ws2_32.zig"); -// TODO: Stop using os.iovec in std.fs et al on Windows in the future -const posix = @import("posix.zig"); -pub const iovec = posix.iovec; -pub const iovec_const = posix.iovec_const; - pub const fd_t = HANDLE; pub const ino_t = LARGE_INTEGER; pub const pid_t = HANDLE; diff --git a/lib/std/os/darwin.zig b/lib/std/os/darwin.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/darwin.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/dragonfly.zig b/lib/std/os/dragonfly.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/dragonfly.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/freebsd.zig b/lib/std/os/freebsd.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/freebsd.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/haiku.zig b/lib/std/os/haiku.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/haiku.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 369fae8f53..9aa18f786c 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -13,13 +13,22 @@ const vdso = @import("linux/vdso.zig"); const dl = @import("../dynamic_library.zig"); const native_arch = std.Target.current.cpu.arch; const native_endian = native_arch.endian(); +const is_mips = native_arch.isMIPS(); +const is_ppc = native_arch.isPPC(); +const is_ppc64 = native_arch.isPPC64(); +const is_sparc = native_arch.isSPARC(); -pub usingnamespace switch (native_arch) { +test { + if (std.Target.current.os.tag == .linux) { + _ = @import("linux/test.zig"); + } +} + +const arch_bits = switch (native_arch) { .i386 => @import("linux/i386.zig"), .x86_64 => @import("linux/x86_64.zig"), .aarch64 => @import("linux/arm64.zig"), - .arm => @import("linux/arm-eabi.zig"), - .thumb => @import("linux/thumb.zig"), + .arm, .thumb => @import("linux/arm-eabi.zig"), .riscv64 => @import("linux/riscv64.zig"), .sparcv9 => @import("linux/sparc64.zig"), .mips, .mipsel => @import("linux/mips.zig"), @@ -27,7 +36,48 @@ pub usingnamespace switch (native_arch) { .powerpc64, .powerpc64le => @import("linux/powerpc64.zig"), else => struct {}, }; -pub usingnamespace @import("bits.zig"); +pub const syscall0 = arch_bits.syscall0; +pub const syscall1 = arch_bits.syscall1; +pub const syscall2 = arch_bits.syscall2; +pub const syscall3 = arch_bits.syscall3; +pub const syscall4 = arch_bits.syscall4; +pub const syscall5 = arch_bits.syscall5; +pub const syscall6 = arch_bits.syscall6; +pub const clone = arch_bits.clone; +pub const restore = arch_bits.restore; +pub const restore_rt = arch_bits.restore_rt; + +pub const ARCH = arch_bits.ARCH; +pub const Elf_Symndx = arch_bits.Elf_Symndx; +pub const F = arch_bits.F; +pub const Flock = arch_bits.Flock; +pub const LOCK = arch_bits.LOCK; +pub const MAP = arch_bits.MAP; +pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT; +pub const O = arch_bits.O; +pub const REG = arch_bits.REG; +pub const SC = arch_bits.SC; +pub const SYS = arch_bits.SYS; +pub const VDSO = arch_bits.VDSO; +pub const blkcnt_t = arch_bits.blkcnt_t; +pub const blksize_t = arch_bits.blksize_t; +pub const dev_t = arch_bits.dev_t; +pub const ino_t = arch_bits.ino_t; +pub const kernel_stat = arch_bits.kernel_stat; +pub const libc_stat = arch_bits.libc_stat; +pub const mcontext_t = arch_bits.mcontext_t; +pub const mode_t = arch_bits.mode_t; +pub const msghdr = arch_bits.msghdr; +pub const msghdr_const = arch_bits.msghdr_const; +pub const nlink_t = arch_bits.nlink_t; +pub const off_t = arch_bits.off_t; +pub const time_t = arch_bits.time_t; +pub const timespec = arch_bits.timespec; +pub const timeval = arch_bits.timeval; +pub const timezone = arch_bits.timezone; +pub const ucontext_t = arch_bits.ucontext_t; +pub const user_desc = arch_bits.user_desc; + pub const tls = @import("linux/tls.zig"); pub const pie = @import("linux/start_pie.zig"); pub const BPF = @import("linux/bpf.zig"); @@ -931,18 +981,18 @@ pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?* pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) usize { assert(sig >= 1); - assert(sig != SIGKILL); - assert(sig != SIGSTOP); + assert(sig != SIG.KILL); + assert(sig != SIG.STOP); var ksa: k_sigaction = undefined; var oldksa: k_sigaction = undefined; const mask_size = @sizeOf(@TypeOf(ksa.mask)); if (act) |new| { - const restorer_fn = if ((new.flags & SA_SIGINFO) != 0) restore_rt else restore; + const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) restore_rt else restore; ksa = k_sigaction{ .handler = new.handler.handler, - .flags = new.flags | SA_RESTORER, + .flags = new.flags | SA.RESTORER, .mask = undefined, .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn), }; @@ -1531,8 +1581,3341 @@ pub fn fadvise(fd: fd_t, offset: i64, len: i64, advice: usize) usize { } } -test { - if (std.Target.current.os.tag == .linux) { - _ = @import("linux/test.zig"); +pub const E = switch (native_arch) { + .mips, .mipsel => @import("linux/errno/mips.zig").E, + .sparc, .sparcel, .sparcv9 => @import("linux/errno/sparc.zig").E, + else => @import("linux/errno/generic.zig").E, +}; + +pub const pid_t = i32; +pub const fd_t = i32; +pub const uid_t = u32; +pub const gid_t = u32; +pub const clock_t = isize; + +pub const NAME_MAX = 255; +pub const PATH_MAX = 4096; +pub const IOV_MAX = 1024; + +/// Largest hardware address length +/// e.g. a mac address is a type of hardware address +pub const MAX_ADDR_LEN = 32; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const AT = struct { + /// Special value used to indicate openat should use the current working directory + pub const FDCWD = -100; + + /// Do not follow symbolic links + pub const SYMLINK_NOFOLLOW = 0x100; + + /// Remove directory instead of unlinking file + pub const REMOVEDIR = 0x200; + + /// Follow symbolic links. + pub const SYMLINK_FOLLOW = 0x400; + + /// Suppress terminal automount traversal + pub const NO_AUTOMOUNT = 0x800; + + /// Allow empty relative pathname + pub const EMPTY_PATH = 0x1000; + + /// Type of synchronisation required from statx() + pub const STATX_SYNC_TYPE = 0x6000; + + /// - Do whatever stat() does + pub const STATX_SYNC_AS_STAT = 0x0000; + + /// - Force the attributes to be sync'd with the server + pub const STATX_FORCE_SYNC = 0x2000; + + /// - Don't sync attributes with the server + pub const STATX_DONT_SYNC = 0x4000; + + /// Apply to the entire subtree + pub const RECURSIVE = 0x8000; +}; + +pub const FALLOC = struct { + /// Default is extend size + pub const FL_KEEP_SIZE = 0x01; + + /// De-allocates range + pub const FL_PUNCH_HOLE = 0x02; + + /// Reserved codepoint + pub const FL_NO_HIDE_STALE = 0x04; + + /// Removes a range of a file without leaving a hole in the file + pub const FL_COLLAPSE_RANGE = 0x08; + + /// Converts a range of file to zeros preferably without issuing data IO + pub const FL_ZERO_RANGE = 0x10; + + /// Inserts space within the file size without overwriting any existing data + pub const FL_INSERT_RANGE = 0x20; + + /// Unshares shared blocks within the file size without overwriting any existing data + pub const FL_UNSHARE_RANGE = 0x40; +}; + +pub const FUTEX = struct { + pub const WAIT = 0; + pub const WAKE = 1; + pub const FD = 2; + pub const REQUEUE = 3; + pub const CMP_REQUEUE = 4; + pub const WAKE_OP = 5; + pub const LOCK_PI = 6; + pub const UNLOCK_PI = 7; + pub const TRYLOCK_PI = 8; + pub const WAIT_BITSET = 9; + pub const WAKE_BITSET = 10; + pub const WAIT_REQUEUE_PI = 11; + pub const CMP_REQUEUE_PI = 12; + + pub const PRIVATE_FLAG = 128; + + pub const CLOCK_REALTIME = 256; +}; + +pub const PROT = struct { + /// page can not be accessed + pub const NONE = 0x0; + + /// page can be read + pub const READ = 0x1; + + /// page can be written + pub const WRITE = 0x2; + + /// page can be executed + pub const EXEC = 0x4; + + /// page may be used for atomic ops + pub const SEM = switch (native_arch) { + // TODO: also xtensa + .mips, .mipsel, .mips64, .mips64el => 0x10, + else => 0x8, + }; + + /// mprotect flag: extend change to start of growsdown vma + pub const GROWSDOWN = 0x01000000; + + /// mprotect flag: extend change to end of growsup vma + pub const GROWSUP = 0x02000000; +}; + +pub const FD_CLOEXEC = 1; + +pub const F_OK = 0; +pub const X_OK = 1; +pub const W_OK = 2; +pub const R_OK = 4; + +pub const W = struct { + pub const NOHANG = 1; + pub const UNTRACED = 2; + pub const STOPPED = 2; + pub const EXITED = 4; + pub const CONTINUED = 8; + pub const NOWAIT = 0x1000000; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, (s & 0xff00) >> 8); } + pub fn TERMSIG(s: u32) u32 { + return s & 0x7f; + } + pub fn STOPSIG(s: u32) u32 { + return WEXITSTATUS(s); + } + pub fn IFEXITED(s: u32) bool { + return WTERMSIG(s) == 0; + } + pub fn IFSTOPPED(s: u32) bool { + return @intCast(u16, ((s & 0xffff) *% 0x10001) >> 8) > 0x7f00; + } + pub fn IFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; + } +}; + +// waitid id types +pub const P = enum(c_uint) { + ALL = 0, + PID = 1, + PGID = 2, + PIDFD = 3, + _, +}; + +pub const SA = if (is_mips) struct { + pub const NOCLDSTOP = 1; + pub const NOCLDWAIT = 0x10000; + pub const SIGINFO = 8; + pub const RESTART = 0x10000000; + pub const RESETHAND = 0x80000000; + pub const ONSTACK = 0x08000000; + pub const NODEFER = 0x40000000; + pub const RESTORER = 0x04000000; +} else if (is_sparc) struct { + pub const NOCLDSTOP = 0x8; + pub const NOCLDWAIT = 0x100; + pub const SIGINFO = 0x200; + pub const RESTART = 0x2; + pub const RESETHAND = 0x4; + pub const ONSTACK = 0x1; + pub const NODEFER = 0x20; + pub const RESTORER = 0x04000000; +} else struct { + pub const NOCLDSTOP = 1; + pub const NOCLDWAIT = 2; + pub const SIGINFO = 4; + pub const RESTART = 0x10000000; + pub const RESETHAND = 0x80000000; + pub const ONSTACK = 0x08000000; + pub const NODEFER = 0x40000000; + pub const RESTORER = 0x04000000; +}; + +pub const SIG = if (is_mips) struct { + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const BUS = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const USR1 = 10; + pub const SEGV = 11; + pub const USR2 = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const STKFLT = 16; + pub const CHLD = 17; + pub const CONT = 18; + pub const STOP = 19; + pub const TSTP = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const URG = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const IO = 29; + pub const POLL = 29; + pub const PWR = 30; + pub const SYS = 31; + pub const UNUSED = SIG.SYS; + + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); +} else if (is_sparc) struct { + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 4; + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const EMT = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const BUS = 10; + pub const SEGV = 11; + pub const SYS = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const URG = 16; + pub const STOP = 17; + pub const TSTP = 18; + pub const CONT = 19; + pub const CHLD = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const POLL = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const LOST = 29; + pub const USR1 = 30; + pub const USR2 = 31; + pub const IOT = ABRT; + pub const CLD = CHLD; + pub const PWR = LOST; + pub const IO = POLL; + + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); +} else struct { + pub const BLOCK = 0; + pub const UNBLOCK = 1; + pub const SETMASK = 2; + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const BUS = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const USR1 = 10; + pub const SEGV = 11; + pub const USR2 = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const STKFLT = 16; + pub const CHLD = 17; + pub const CONT = 18; + pub const STOP = 19; + pub const TSTP = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const URG = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const IO = 29; + pub const POLL = 29; + pub const PWR = 30; + pub const SYS = 31; + pub const UNUSED = SIG.SYS; + + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); +}; + +pub const kernel_rwf = u32; + +pub const RWF = struct { + /// high priority request, poll if possible + pub const HIPRI: kernel_rwf = 0x00000001; + + /// per-IO O_DSYNC + pub const DSYNC: kernel_rwf = 0x00000002; + + /// per-IO O_SYNC + pub const SYNC: kernel_rwf = 0x00000004; + + /// per-IO, return -EAGAIN if operation would block + pub const NOWAIT: kernel_rwf = 0x00000008; + + /// per-IO O_APPEND + pub const APPEND: kernel_rwf = 0x00000010; +}; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +pub const SOCK = struct { + pub const STREAM = if (is_mips) 2 else 1; + pub const DGRAM = if (is_mips) 1 else 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + pub const DCCP = 6; + pub const PACKET = 10; + pub const CLOEXEC = 0o2000000; + pub const NONBLOCK = if (is_mips) 0o200 else 0o4000; +}; + +pub const PF = struct { + pub const UNSPEC = 0; + pub const LOCAL = 1; + pub const UNIX = PF_LOCAL; + pub const FILE = PF_LOCAL; + pub const INET = 2; + pub const AX25 = 3; + pub const IPX = 4; + pub const APPLETALK = 5; + pub const NETROM = 6; + pub const BRIDGE = 7; + pub const ATMPVC = 8; + pub const X25 = 9; + pub const INET6 = 10; + pub const ROSE = 11; + pub const DECnet = 12; + pub const NETBEUI = 13; + pub const SECURITY = 14; + pub const KEY = 15; + pub const NETLINK = 16; + pub const ROUTE = PF_NETLINK; + pub const PACKET = 17; + pub const ASH = 18; + pub const ECONET = 19; + pub const ATMSVC = 20; + pub const RDS = 21; + pub const SNA = 22; + pub const IRDA = 23; + pub const PPPOX = 24; + pub const WANPIPE = 25; + pub const LLC = 26; + pub const IB = 27; + pub const MPLS = 28; + pub const CAN = 29; + pub const TIPC = 30; + pub const BLUETOOTH = 31; + pub const IUCV = 32; + pub const RXRPC = 33; + pub const ISDN = 34; + pub const PHONET = 35; + pub const IEEE802154 = 36; + pub const CAIF = 37; + pub const ALG = 38; + pub const NFC = 39; + pub const VSOCK = 40; + pub const KCM = 41; + pub const QIPCRTR = 42; + pub const SMC = 43; + pub const XDP = 44; + pub const MAX = 45; +}; + +pub const AF = struct { + pub const UNSPEC = PF.UNSPEC; + pub const LOCAL = PF.LOCAL; + pub const UNIX = AF.LOCAL; + pub const FILE = AF.LOCAL; + pub const INET = PF.INET; + pub const AX25 = PF.AX25; + pub const IPX = PF.IPX; + pub const APPLETALK = PF.APPLETALK; + pub const NETROM = PF.NETROM; + pub const BRIDGE = PF.BRIDGE; + pub const ATMPVC = PF.ATMPVC; + pub const X25 = PF.X25; + pub const INET6 = PF.INET6; + pub const ROSE = PF.ROSE; + pub const DECnet = PF.DECnet; + pub const NETBEUI = PF.NETBEUI; + pub const SECURITY = PF.SECURITY; + pub const KEY = PF.KEY; + pub const NETLINK = PF.NETLINK; + pub const ROUTE = PF.ROUTE; + pub const PACKET = PF.PACKET; + pub const ASH = PF.ASH; + pub const ECONET = PF.ECONET; + pub const ATMSVC = PF.ATMSVC; + pub const RDS = PF.RDS; + pub const SNA = PF.SNA; + pub const IRDA = PF.IRDA; + pub const PPPOX = PF.PPPOX; + pub const WANPIPE = PF.WANPIPE; + pub const LLC = PF.LLC; + pub const IB = PF.IB; + pub const MPLS = PF.MPLS; + pub const CAN = PF.CAN; + pub const TIPC = PF.TIPC; + pub const BLUETOOTH = PF.BLUETOOTH; + pub const IUCV = PF.IUCV; + pub const RXRPC = PF.RXRPC; + pub const ISDN = PF.ISDN; + pub const PHONET = PF.PHONET; + pub const IEEE802154 = PF.IEEE802154; + pub const CAIF = PF.CAIF; + pub const ALG = PF.ALG; + pub const NFC = PF.NFC; + pub const VSOCK = PF.VSOCK; + pub const KCM = PF.KCM; + pub const QIPCRTR = PF.QIPCRTR; + pub const SMC = PF.SMC; + pub const XDP = PF.XDP; + pub const MAX = PF.MAX; +}; + +pub const SO = if (is_mips) struct { + pub const SECURITY_AUTHENTICATION = 22; + pub const SECURITY_ENCRYPTION_TRANSPORT = 23; + pub const SECURITY_ENCRYPTION_NETWORK = 24; + + pub const BINDTODEVICE = 25; + + pub const ATTACH_FILTER = 26; + pub const DETACH_FILTER = 27; + pub const GET_FILTER = ATTACH_FILTER; + + pub const PEERNAME = 28; + pub const TIMESTAMP_OLD = 29; + pub const PASSSEC = 34; + pub const TIMESTAMPNS_OLD = 35; + pub const MARK = 36; + pub const TIMESTAMPING_OLD = 37; + + pub const RXQ_OVFL = 40; + pub const WIFI_STATUS = 41; + pub const PEEK_OFF = 42; + pub const NOFCS = 43; + pub const LOCK_FILTER = 44; + pub const SELECT_ERR_QUEUE = 45; + pub const BUSY_POLL = 46; + pub const MAX_PACING_RATE = 47; + pub const BPF_EXTENSIONS = 48; + pub const INCOMING_CPU = 49; + pub const ATTACH_BPF = 50; + pub const DETACH_BPF = DETACH_FILTER; + pub const ATTACH_REUSEPORT_CBPF = 51; + pub const ATTACH_REUSEPORT_EBPF = 52; + pub const CNX_ADVICE = 53; + pub const MEMINFO = 55; + pub const INCOMING_NAPI_ID = 56; + pub const COOKIE = 57; + pub const PEERGROUPS = 59; + pub const ZEROCOPY = 60; + pub const TXTIME = 61; + pub const BINDTOIFINDEX = 62; + pub const TIMESTAMP_NEW = 63; + pub const TIMESTAMPNS_NEW = 64; + pub const TIMESTAMPING_NEW = 65; + pub const RCVTIMEO_NEW = 66; + pub const SNDTIMEO_NEW = 67; + pub const DETACH_REUSEPORT_BPF = 68; +} else if (is_ppc or is_ppc64) struct { + pub const DEBUG = 1; + pub const REUSEADDR = 2; + pub const TYPE = 3; + pub const ERROR = 4; + pub const DONTROUTE = 5; + pub const BROADCAST = 6; + pub const SNDBUF = 7; + pub const RCVBUF = 8; + pub const KEEPALIVE = 9; + pub const OOBINLINE = 10; + pub const NO_CHECK = 11; + pub const PRIORITY = 12; + pub const LINGER = 13; + pub const BSDCOMPAT = 14; + pub const REUSEPORT = 15; + pub const RCVLOWAT = 16; + pub const SNDLOWAT = 17; + pub const RCVTIMEO = 18; + pub const SNDTIMEO = 19; + pub const PASSCRED = 20; + pub const PEERCRED = 21; + pub const ACCEPTCONN = 30; + pub const PEERSEC = 31; + pub const SNDBUFFORCE = 32; + pub const RCVBUFFORCE = 33; + pub const PROTOCOL = 38; + pub const DOMAIN = 39; + + pub const SECURITY_AUTHENTICATION = 22; + pub const SECURITY_ENCRYPTION_TRANSPORT = 23; + pub const SECURITY_ENCRYPTION_NETWORK = 24; + + pub const BINDTODEVICE = 25; + + pub const ATTACH_FILTER = 26; + pub const DETACH_FILTER = 27; + pub const GET_FILTER = ATTACH_FILTER; + + pub const PEERNAME = 28; + pub const TIMESTAMP_OLD = 29; + pub const PASSSEC = 34; + pub const TIMESTAMPNS_OLD = 35; + pub const MARK = 36; + pub const TIMESTAMPING_OLD = 37; + + pub const RXQ_OVFL = 40; + pub const WIFI_STATUS = 41; + pub const PEEK_OFF = 42; + pub const NOFCS = 43; + pub const LOCK_FILTER = 44; + pub const SELECT_ERR_QUEUE = 45; + pub const BUSY_POLL = 46; + pub const MAX_PACING_RATE = 47; + pub const BPF_EXTENSIONS = 48; + pub const INCOMING_CPU = 49; + pub const ATTACH_BPF = 50; + pub const DETACH_BPF = DETACH_FILTER; + pub const ATTACH_REUSEPORT_CBPF = 51; + pub const ATTACH_REUSEPORT_EBPF = 52; + pub const CNX_ADVICE = 53; + pub const MEMINFO = 55; + pub const INCOMING_NAPI_ID = 56; + pub const COOKIE = 57; + pub const PEERGROUPS = 59; + pub const ZEROCOPY = 60; + pub const TXTIME = 61; + pub const BINDTOIFINDEX = 62; + pub const TIMESTAMP_NEW = 63; + pub const TIMESTAMPNS_NEW = 64; + pub const TIMESTAMPING_NEW = 65; + pub const RCVTIMEO_NEW = 66; + pub const SNDTIMEO_NEW = 67; + pub const DETACH_REUSEPORT_BPF = 68; +} else struct { + pub const DEBUG = 1; + pub const REUSEADDR = 2; + pub const TYPE = 3; + pub const ERROR = 4; + pub const DONTROUTE = 5; + pub const BROADCAST = 6; + pub const SNDBUF = 7; + pub const RCVBUF = 8; + pub const KEEPALIVE = 9; + pub const OOBINLINE = 10; + pub const NO_CHECK = 11; + pub const PRIORITY = 12; + pub const LINGER = 13; + pub const BSDCOMPAT = 14; + pub const REUSEPORT = 15; + pub const PASSCRED = 16; + pub const PEERCRED = 17; + pub const RCVLOWAT = 18; + pub const SNDLOWAT = 19; + pub const RCVTIMEO = 20; + pub const SNDTIMEO = 21; + pub const ACCEPTCONN = 30; + pub const PEERSEC = 31; + pub const SNDBUFFORCE = 32; + pub const RCVBUFFORCE = 33; + pub const PROTOCOL = 38; + pub const DOMAIN = 39; + + pub const SECURITY_AUTHENTICATION = 22; + pub const SECURITY_ENCRYPTION_TRANSPORT = 23; + pub const SECURITY_ENCRYPTION_NETWORK = 24; + + pub const BINDTODEVICE = 25; + + pub const ATTACH_FILTER = 26; + pub const DETACH_FILTER = 27; + pub const GET_FILTER = ATTACH_FILTER; + + pub const PEERNAME = 28; + pub const TIMESTAMP_OLD = 29; + pub const PASSSEC = 34; + pub const TIMESTAMPNS_OLD = 35; + pub const MARK = 36; + pub const TIMESTAMPING_OLD = 37; + + pub const RXQ_OVFL = 40; + pub const WIFI_STATUS = 41; + pub const PEEK_OFF = 42; + pub const NOFCS = 43; + pub const LOCK_FILTER = 44; + pub const SELECT_ERR_QUEUE = 45; + pub const BUSY_POLL = 46; + pub const MAX_PACING_RATE = 47; + pub const BPF_EXTENSIONS = 48; + pub const INCOMING_CPU = 49; + pub const ATTACH_BPF = 50; + pub const DETACH_BPF = DETACH_FILTER; + pub const ATTACH_REUSEPORT_CBPF = 51; + pub const ATTACH_REUSEPORT_EBPF = 52; + pub const CNX_ADVICE = 53; + pub const MEMINFO = 55; + pub const INCOMING_NAPI_ID = 56; + pub const COOKIE = 57; + pub const PEERGROUPS = 59; + pub const ZEROCOPY = 60; + pub const TXTIME = 61; + pub const BINDTOIFINDEX = 62; + pub const TIMESTAMP_NEW = 63; + pub const TIMESTAMPNS_NEW = 64; + pub const TIMESTAMPING_NEW = 65; + pub const RCVTIMEO_NEW = 66; + pub const SNDTIMEO_NEW = 67; + pub const DETACH_REUSEPORT_BPF = 68; +}; + +pub const SCM = struct { + pub const WIFI_STATUS = SO.WIFI_STATUS; + pub const TIMESTAMPING_OPT_STATS = 54; + pub const TIMESTAMPING_PKTINFO = 58; + pub const TXTIME = SO.TXTIME; +}; + +pub const SOL = struct { + pub const SOCKET = if (is_mips) 65535 else 1; + + pub const IP = 0; + pub const IPV6 = 41; + pub const ICMPV6 = 58; + + pub const RAW = 255; + pub const DECNET = 261; + pub const X25 = 262; + pub const PACKET = 263; + pub const ATM = 264; + pub const AAL = 265; + pub const IRDA = 266; + pub const NETBEUI = 267; + pub const LLC = 268; + pub const DCCP = 269; + pub const NETLINK = 270; + pub const TIPC = 271; + pub const RXRPC = 272; + pub const PPPOL2TP = 273; + pub const BLUETOOTH = 274; + pub const PNPIPE = 275; + pub const RDS = 276; + pub const IUCV = 277; + pub const CAIF = 278; + pub const ALG = 279; + pub const NFC = 280; + pub const KCM = 281; + pub const TLS = 282; + pub const XDP = 283; +}; + +pub const SOMAXCONN = 128; + +pub const IP = struct { + pub const TOS = 1; + pub const TTL = 2; + pub const HDRINCL = 3; + pub const OPTIONS = 4; + pub const ROUTER_ALERT = 5; + pub const RECVOPTS = 6; + pub const RETOPTS = 7; + pub const PKTINFO = 8; + pub const PKTOPTIONS = 9; + pub const PMTUDISC = 10; + pub const MTU_DISCOVER = 10; + pub const RECVERR = 11; + pub const RECVTTL = 12; + pub const RECVTOS = 13; + pub const MTU = 14; + pub const FREEBIND = 15; + pub const IPSEC_POLICY = 16; + pub const XFRM_POLICY = 17; + pub const PASSSEC = 18; + pub const TRANSPARENT = 19; + pub const ORIGDSTADDR = 20; + pub const RECVORIGDSTADDR = IP_ORIGDSTADDR; + pub const MINTTL = 21; + pub const NODEFRAG = 22; + pub const CHECKSUM = 23; + pub const BIND_ADDRESS_NO_PORT = 24; + pub const RECVFRAGSIZE = 25; + pub const MULTICAST_IF = 32; + pub const MULTICAST_TTL = 33; + pub const MULTICAST_LOOP = 34; + pub const ADD_MEMBERSHIP = 35; + pub const DROP_MEMBERSHIP = 36; + pub const UNBLOCK_SOURCE = 37; + pub const BLOCK_SOURCE = 38; + pub const ADD_SOURCE_MEMBERSHIP = 39; + pub const DROP_SOURCE_MEMBERSHIP = 40; + pub const MSFILTER = 41; + pub const MULTICAST_ALL = 49; + pub const UNICAST_IF = 50; + + pub const RECVRETOPTS = IP_RETOPTS; + + pub const PMTUDISC_DONT = 0; + pub const PMTUDISC_WANT = 1; + pub const PMTUDISC_DO = 2; + pub const PMTUDISC_PROBE = 3; + pub const PMTUDISC_INTERFACE = 4; + pub const PMTUDISC_OMIT = 5; + + pub const DEFAULT_MULTICAST_TTL = 1; + pub const DEFAULT_MULTICAST_LOOP = 1; + pub const MAX_MEMBERSHIPS = 20; +}; + +/// IPv6 socket options +pub const IPV6 = struct { + pub const ADDRFORM = 1; + pub const @"2292PKTINFO" = 2; + pub const @"2292HOPOPTS" = 3; + pub const @"2292DSTOPTS" = 4; + pub const @"2292RTHDR" = 5; + pub const @"2292PKTOPTIONS" = 6; + pub const CHECKSUM = 7; + pub const @"2292HOPLIMIT" = 8; + pub const NEXTHOP = 9; + pub const AUTHHDR = 10; + pub const FLOWINFO = 11; + + pub const UNICAST_HOPS = 16; + pub const MULTICAST_IF = 17; + pub const MULTICAST_HOPS = 18; + pub const MULTICAST_LOOP = 19; + pub const ADD_MEMBERSHIP = 20; + pub const DROP_MEMBERSHIP = 21; + pub const ROUTER_ALERT = 22; + pub const MTU_DISCOVER = 23; + pub const MTU = 24; + pub const RECVERR = 25; + pub const V6ONLY = 26; + pub const JOIN_ANYCAST = 27; + pub const LEAVE_ANYCAST = 28; + + // IPV6_MTU_DISCOVER values + pub const PMTUDISC_DONT = 0; + pub const PMTUDISC_WANT = 1; + pub const PMTUDISC_DO = 2; + pub const PMTUDISC_PROBE = 3; + pub const PMTUDISC_INTERFACE = 4; + pub const PMTUDISC_OMIT = 5; + + // Flowlabel + pub const FLOWLABEL_MGR = 32; + pub const FLOWINFO_SEND = 33; + pub const IPSEC_POLICY = 34; + pub const XFRM_POLICY = 35; + pub const HDRINCL = 36; + + // Advanced API (RFC3542) (1) + pub const RECVPKTINFO = 49; + pub const PKTINFO = 50; + pub const RECVHOPLIMIT = 51; + pub const HOPLIMIT = 52; + pub const RECVHOPOPTS = 53; + pub const HOPOPTS = 54; + pub const RTHDRDSTOPTS = 55; + pub const RECVRTHDR = 56; + pub const RTHDR = 57; + pub const RECVDSTOPTS = 58; + pub const DSTOPTS = 59; + pub const RECVPATHMTU = 60; + pub const PATHMTU = 61; + pub const DONTFRAG = 62; + + // Advanced API (RFC3542) (2) + pub const RECVTCLASS = 66; + pub const TCLASS = 67; + + pub const AUTOFLOWLABEL = 70; + + // RFC5014: Source address selection + pub const ADDR_PREFERENCES = 72; + + pub const PREFER_SRC_TMP = 0x0001; + pub const PREFER_SRC_PUBLIC = 0x0002; + pub const PREFER_SRC_PUBTMP_DEFAULT = 0x0100; + pub const PREFER_SRC_COA = 0x0004; + pub const PREFER_SRC_HOME = 0x0400; + pub const PREFER_SRC_CGA = 0x0008; + pub const PREFER_SRC_NONCGA = 0x0800; + + // RFC5082: Generalized Ttl Security Mechanism + pub const MINHOPCOUNT = 73; + + pub const ORIGDSTADDR = 74; + pub const RECVORIGDSTADDR = IPV6_ORIGDSTADDR; + pub const TRANSPARENT = 75; + pub const UNICAST_IF = 76; + pub const RECVFRAGSIZE = 77; + pub const FREEBIND = 78; +}; + +pub const MSG = struct { + pub const OOB = 0x0001; + pub const PEEK = 0x0002; + pub const DONTROUTE = 0x0004; + pub const CTRUNC = 0x0008; + pub const PROXY = 0x0010; + pub const TRUNC = 0x0020; + pub const DONTWAIT = 0x0040; + pub const EOR = 0x0080; + pub const WAITALL = 0x0100; + pub const FIN = 0x0200; + pub const SYN = 0x0400; + pub const CONFIRM = 0x0800; + pub const RST = 0x1000; + pub const ERRQUEUE = 0x2000; + pub const NOSIGNAL = 0x4000; + pub const MORE = 0x8000; + pub const WAITFORONE = 0x10000; + pub const BATCH = 0x40000; + pub const ZEROCOPY = 0x4000000; + pub const FASTOPEN = 0x20000000; + pub const CMSG_CLOEXEC = 0x40000000; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; +}; + +pub const T = struct { + pub const CGETS = if (is_mips) 0x540D else 0x5401; + pub const CSETS = 0x5402; + pub const CSETSW = 0x5403; + pub const CSETSF = 0x5404; + pub const CGETA = 0x5405; + pub const CSETA = 0x5406; + pub const CSETAW = 0x5407; + pub const CSETAF = 0x5408; + pub const CSBRK = 0x5409; + pub const CXONC = 0x540A; + pub const CFLSH = 0x540B; + pub const IOCEXCL = 0x540C; + pub const IOCNXCL = 0x540D; + pub const IOCSCTTY = 0x540E; + pub const IOCGPGRP = 0x540F; + pub const IOCSPGRP = 0x5410; + pub const IOCOUTQ = if (is_mips) 0x7472 else 0x5411; + pub const IOCSTI = 0x5412; + pub const IOCGWINSZ = if (is_mips or is_ppc64) 0x40087468 else 0x5413; + pub const IOCSWINSZ = if (is_mips or is_ppc64) 0x80087467 else 0x5414; + pub const IOCMGET = 0x5415; + pub const IOCMBIS = 0x5416; + pub const IOCMBIC = 0x5417; + pub const IOCMSET = 0x5418; + pub const IOCGSOFTCAR = 0x5419; + pub const IOCSSOFTCAR = 0x541A; + pub const FIONREAD = if (is_mips) 0x467F else 0x541B; + pub const IOCINQ = FIONREAD; + pub const IOCLINUX = 0x541C; + pub const IOCCONS = 0x541D; + pub const IOCGSERIAL = 0x541E; + pub const IOCSSERIAL = 0x541F; + pub const IOCPKT = 0x5420; + pub const FIONBIO = 0x5421; + pub const IOCNOTTY = 0x5422; + pub const IOCSETD = 0x5423; + pub const IOCGETD = 0x5424; + pub const CSBRKP = 0x5425; + pub const IOCSBRK = 0x5427; + pub const IOCCBRK = 0x5428; + pub const IOCGSID = 0x5429; + pub const IOCGRS485 = 0x542E; + pub const IOCSRS485 = 0x542F; + pub const IOCGPTN = 0x80045430; + pub const IOCSPTLCK = 0x40045431; + pub const IOCGDEV = 0x80045432; + pub const CGETX = 0x5432; + pub const CSETX = 0x5433; + pub const CSETXF = 0x5434; + pub const CSETXW = 0x5435; + pub const IOCSIG = 0x40045436; + pub const IOCVHANGUP = 0x5437; + pub const IOCGPKT = 0x80045438; + pub const IOCGPTLCK = 0x80045439; + pub const IOCGEXCL = 0x80045440; +}; + +pub const EPOLL = struct { + pub const CLOEXEC = O.CLOEXEC; + + pub const CTL_ADD = 1; + pub const CTL_DEL = 2; + pub const CTL_MOD = 3; + + pub const IN = 0x001; + pub const PRI = 0x002; + pub const OUT = 0x004; + pub const RDNORM = 0x040; + pub const RDBAND = 0x080; + pub const WRNORM = if (is_mips) 0x004 else 0x100; + pub const WRBAND = if (is_mips) 0x100 else 0x200; + pub const MSG = 0x400; + pub const ERR = 0x008; + pub const HUP = 0x010; + pub const RDHUP = 0x2000; + pub const EXCLUSIVE = (@as(u32, 1) << 28); + pub const WAKEUP = (@as(u32, 1) << 29); + pub const ONESHOT = (@as(u32, 1) << 30); + pub const ET = (@as(u32, 1) << 31); +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const MONOTONIC = 1; + pub const PROCESS_CPUTIME_ID = 2; + pub const THREAD_CPUTIME_ID = 3; + pub const MONOTONIC_RAW = 4; + pub const REALTIME_COARSE = 5; + pub const MONOTONIC_COARSE = 6; + pub const BOOTTIME = 7; + pub const REALTIME_ALARM = 8; + pub const BOOTTIME_ALARM = 9; + pub const SGI_CYCLE = 10; + pub const TAI = 11; +}; + +pub const CSIGNAL = 0x000000ff; + +pub const CLONE = struct { + pub const VM = 0x00000100; + pub const FS = 0x00000200; + pub const FILES = 0x00000400; + pub const SIGHAND = 0x00000800; + pub const PIDFD = 0x00001000; + pub const PTRACE = 0x00002000; + pub const VFORK = 0x00004000; + pub const PARENT = 0x00008000; + pub const THREAD = 0x00010000; + pub const NEWNS = 0x00020000; + pub const SYSVSEM = 0x00040000; + pub const SETTLS = 0x00080000; + pub const PARENT_SETTID = 0x00100000; + pub const CHILD_CLEARTID = 0x00200000; + pub const DETACHED = 0x00400000; + pub const UNTRACED = 0x00800000; + pub const CHILD_SETTID = 0x01000000; + pub const NEWCGROUP = 0x02000000; + pub const NEWUTS = 0x04000000; + pub const NEWIPC = 0x08000000; + pub const NEWUSER = 0x10000000; + pub const NEWPID = 0x20000000; + pub const NEWNET = 0x40000000; + pub const IO = 0x80000000; + + // Flags for the clone3() syscall. + + /// Clear any signal handler and reset to SIG_DFL. + pub const CLEAR_SIGHAND = 0x100000000; + /// Clone into a specific cgroup given the right permissions. + pub const INTO_CGROUP = 0x200000000; + + // cloning flags intersect with CSIGNAL so can be used with unshare and clone3 syscalls only. + + /// New time namespace + pub const NEWTIME = 0x00000080; +}; + +pub const EFD = struct { + pub const SEMAPHORE = 1; + pub const CLOEXEC = O.CLOEXEC; + pub const NONBLOCK = O.NONBLOCK; +}; + +pub const MS = struct { + pub const RDONLY = 1; + pub const NOSUID = 2; + pub const NODEV = 4; + pub const NOEXEC = 8; + pub const SYNCHRONOUS = 16; + pub const REMOUNT = 32; + pub const MANDLOCK = 64; + pub const DIRSYNC = 128; + pub const NOATIME = 1024; + pub const NODIRATIME = 2048; + pub const BIND = 4096; + pub const MOVE = 8192; + pub const REC = 16384; + pub const SILENT = 32768; + pub const POSIXACL = (1 << 16); + pub const UNBINDABLE = (1 << 17); + pub const PRIVATE = (1 << 18); + pub const SLAVE = (1 << 19); + pub const SHARED = (1 << 20); + pub const RELATIME = (1 << 21); + pub const KERNMOUNT = (1 << 22); + pub const I_VERSION = (1 << 23); + pub const STRICTATIME = (1 << 24); + pub const LAZYTIME = (1 << 25); + pub const NOREMOTELOCK = (1 << 27); + pub const NOSEC = (1 << 28); + pub const BORN = (1 << 29); + pub const ACTIVE = (1 << 30); + pub const NOUSER = (1 << 31); + + pub const RMT_MASK = (RDONLY | SYNCHRONOUS | MANDLOCK | I_VERSION | LAZYTIME); + + pub const MGC_VAL = 0xc0ed0000; + pub const MGC_MSK = 0xffff0000; +}; + +pub const MNT = struct { + pub const FORCE = 1; + pub const DETACH = 2; + pub const EXPIRE = 4; +}; + +pub const UMOUNT_NOFOLLOW = 8; + +pub const IN = struct { + pub const CLOEXEC = O.CLOEXEC; + pub const NONBLOCK = O.NONBLOCK; + + pub const ACCESS = 0x00000001; + pub const MODIFY = 0x00000002; + pub const ATTRIB = 0x00000004; + pub const CLOSE_WRITE = 0x00000008; + pub const CLOSE_NOWRITE = 0x00000010; + pub const CLOSE = CLOSE_WRITE | CLOSE_NOWRITE; + pub const OPEN = 0x00000020; + pub const MOVED_FROM = 0x00000040; + pub const MOVED_TO = 0x00000080; + pub const MOVE = MOVED_FROM | MOVED_TO; + pub const CREATE = 0x00000100; + pub const DELETE = 0x00000200; + pub const DELETE_SELF = 0x00000400; + pub const MOVE_SELF = 0x00000800; + pub const ALL_EVENTS = 0x00000fff; + + pub const UNMOUNT = 0x00002000; + pub const Q_OVERFLOW = 0x00004000; + pub const IGNORED = 0x00008000; + + pub const ONLYDIR = 0x01000000; + pub const DONT_FOLLOW = 0x02000000; + pub const EXCL_UNLINK = 0x04000000; + pub const MASK_ADD = 0x20000000; + + pub const ISDIR = 0x40000000; + pub const ONESHOT = 0x80000000; +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFDIR = 0o040000; + pub const IFCHR = 0o020000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFIFO = 0o010000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXU = 0o700; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXG = 0o070; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + pub const IRWXO = 0o007; + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } +}; + +pub const UTIME = struct { + pub const NOW = 0x3fffffff; + pub const OMIT = 0x3ffffffe; +}; + +pub const TFD = struct { + pub const NONBLOCK = O.NONBLOCK; + pub const CLOEXEC = O.CLOEXEC; + + pub const TIMER_ABSTIME = 1; + pub const TIMER_CANCEL_ON_SET = (1 << 1); +}; + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +/// NSIG is the total number of signals defined. +/// As signal numbers are sequential, NSIG is one greater than the largest defined signal number. +pub const NSIG = if (is_mips) 128 else 65; + +pub const sigset_t = [1024 / 32]u32; + +pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len; +pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30; + +pub const k_sigaction = switch (native_arch) { + .mips, .mipsel => extern struct { + flags: c_uint, + handler: ?fn (c_int) callconv(.C) void, + mask: [4]c_ulong, + restorer: fn () callconv(.C) void, + }, + .mips64, .mips64el => extern struct { + flags: c_uint, + handler: ?fn (c_int) callconv(.C) void, + mask: [2]c_ulong, + restorer: fn () callconv(.C) void, + }, + else => extern struct { + handler: ?fn (c_int) callconv(.C) void, + flags: c_ulong, + restorer: fn () callconv(.C) void, + mask: [2]c_uint, + }, +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + mask: sigset_t, + flags: c_uint, + restorer: ?fn () callconv(.C) void = null, +}; + +pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).Array.len; + +pub const SFD = struct { + pub const CLOEXEC = O.CLOEXEC; + pub const NONBLOCK = O.NONBLOCK; +}; + +pub const signalfd_siginfo = extern struct { + signo: u32, + errno: i32, + code: i32, + pid: u32, + uid: uid_t, + fd: i32, + tid: u32, + band: u32, + overrun: u32, + trapno: u32, + status: i32, + int: i32, + ptr: u64, + utime: u64, + stime: u64, + addr: u64, + addr_lsb: u16, + __pad2: u16, + syscall: i32, + call_addr: u64, + native_arch: u32, + __pad: [28]u8, +}; + +pub const in_port_t = u16; +pub const sa_family_t = u16; +pub const socklen_t = u32; + +pub const sockaddr = extern struct { + family: sa_family_t, + data: [14]u8, +}; + +pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; + +/// IPv4 socket address +pub const sockaddr_in = extern struct { + family: sa_family_t = AF_INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, +}; + +/// IPv6 socket address +pub const sockaddr_in6 = extern struct { + family: sa_family_t = AF_INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, +}; + +/// UNIX domain socket address +pub const sockaddr_un = extern struct { + family: sa_family_t = AF_UNIX, + path: [108]u8, +}; + +pub const mmsghdr = extern struct { + msg_hdr: msghdr, + msg_len: u32, +}; + +pub const mmsghdr_const = extern struct { + msg_hdr: msghdr_const, + msg_len: u32, +}; + +pub const epoll_data = extern union { + ptr: usize, + fd: i32, + @"u32": u32, + @"u64": u64, +}; + +// On x86_64 the structure is packed so that it matches the definition of its +// 32bit counterpart +pub const epoll_event = switch (native_arch) { + .x86_64 => packed struct { + events: u32, + data: epoll_data, + }, + else => extern struct { + events: u32, + data: epoll_data, + }, +}; + +pub const VFS_CAP_REVISION_MASK = 0xFF000000; +pub const VFS_CAP_REVISION_SHIFT = 24; +pub const VFS_CAP_FLAGS_MASK = ~VFS_CAP_REVISION_MASK; +pub const VFS_CAP_FLAGS_EFFECTIVE = 0x000001; + +pub const VFS_CAP_REVISION_1 = 0x01000000; +pub const VFS_CAP_U32_1 = 1; +pub const XATTR_CAPS_SZ_1 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_1); + +pub const VFS_CAP_REVISION_2 = 0x02000000; +pub const VFS_CAP_U32_2 = 2; +pub const XATTR_CAPS_SZ_2 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_2); + +pub const XATTR_CAPS_SZ = XATTR_CAPS_SZ_2; +pub const VFS_CAP_U32 = VFS_CAP_U32_2; +pub const VFS_CAP_REVISION = VFS_CAP_REVISION_2; + +pub const vfs_cap_data = extern struct { + //all of these are mandated as little endian + //when on disk. + const Data = struct { + permitted: u32, + inheritable: u32, + }; + + magic_etc: u32, + data: [VFS_CAP_U32]Data, +}; + +pub const CAP = struct { + pub const CHOWN = 0; + pub const DAC_OVERRIDE = 1; + pub const DAC_READ_SEARCH = 2; + pub const FOWNER = 3; + pub const FSETID = 4; + pub const KILL = 5; + pub const SETGID = 6; + pub const SETUID = 7; + pub const SETPCAP = 8; + pub const LINUX_IMMUTABLE = 9; + pub const NET_BIND_SERVICE = 10; + pub const NET_BROADCAST = 11; + pub const NET_ADMIN = 12; + pub const NET_RAW = 13; + pub const IPC_LOCK = 14; + pub const IPC_OWNER = 15; + pub const SYS_MODULE = 16; + pub const SYS_RAWIO = 17; + pub const SYS_CHROOT = 18; + pub const SYS_PTRACE = 19; + pub const SYS_PACCT = 20; + pub const SYS_ADMIN = 21; + pub const SYS_BOOT = 22; + pub const SYS_NICE = 23; + pub const SYS_RESOURCE = 24; + pub const SYS_TIME = 25; + pub const SYS_TTY_CONFIG = 26; + pub const MKNOD = 27; + pub const LEASE = 28; + pub const AUDIT_WRITE = 29; + pub const AUDIT_CONTROL = 30; + pub const SETFCAP = 31; + pub const MAC_OVERRIDE = 32; + pub const MAC_ADMIN = 33; + pub const SYSLOG = 34; + pub const WAKE_ALARM = 35; + pub const BLOCK_SUSPEND = 36; + pub const AUDIT_READ = 37; + pub const LAST_CAP = AUDIT_READ; + + pub fn valid(x: u8) bool { + return x >= 0 and x <= LAST_CAP; + } + + pub fn TO_MASK(cap: u8) u32 { + return @as(u32, 1) << @intCast(u5, cap & 31); + } + + pub fn TO_INDEX(cap: u8) u8 { + return cap >> 5; + } +}; + +pub const cap_t = extern struct { + hdrp: *cap_user_header_t, + datap: *cap_user_data_t, +}; + +pub const cap_user_header_t = extern struct { + version: u32, + pid: usize, +}; + +pub const cap_user_data_t = extern struct { + effective: u32, + permitted: u32, + inheritable: u32, +}; + +pub const inotify_event = extern struct { + wd: i32, + mask: u32, + cookie: u32, + len: u32, + //name: [?]u8, +}; + +pub const dirent64 = extern struct { + d_ino: u64, + d_off: u64, + d_reclen: u16, + d_type: u8, + d_name: u8, // field address is the address of first byte of name https://github.com/ziglang/zig/issues/173 + + pub fn reclen(self: dirent64) u16 { + return self.d_reclen; + } +}; + +pub const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: u16, +}; + +pub const CPU_SETSIZE = 128; +pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize; +pub const cpu_count_t = std.meta.Int(.unsigned, std.math.log2(CPU_SETSIZE * 8)); + +pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t { + var sum: cpu_count_t = 0; + for (set) |x| { + sum += @popCount(usize, x); + } + return sum; } + +pub const MINSIGSTKSZ = switch (native_arch) { + .i386, .x86_64, .arm, .mipsel => 2048, + .aarch64 => 5120, + else => @compileError("MINSIGSTKSZ not defined for this architecture"), +}; +pub const SIGSTKSZ = switch (native_arch) { + .i386, .x86_64, .arm, .mipsel => 8192, + .aarch64 => 16384, + else => @compileError("SIGSTKSZ not defined for this architecture"), +}; + +pub const SS_ONSTACK = 1; +pub const SS_DISABLE = 2; +pub const SS_AUTODISARM = 1 << 31; + +pub const stack_t = if (is_mips) + // IRIX compatible stack_t + extern struct { + ss_sp: [*]u8, + ss_size: usize, + ss_flags: i32, + } +else + extern struct { + ss_sp: [*]u8, + ss_flags: i32, + ss_size: usize, + }; + +pub const sigval = extern union { + int: i32, + ptr: *c_void, +}; + +const siginfo_fields_union = extern union { + pad: [128 - 2 * @sizeOf(c_int) - @sizeOf(c_long)]u8, + common: extern struct { + first: extern union { + piduid: extern struct { + pid: pid_t, + uid: uid_t, + }, + timer: extern struct { + timerid: i32, + overrun: i32, + }, + }, + second: extern union { + value: sigval, + sigchld: extern struct { + status: i32, + utime: clock_t, + stime: clock_t, + }, + }, + }, + sigfault: extern struct { + addr: *c_void, + addr_lsb: i16, + first: extern union { + addr_bnd: extern struct { + lower: *c_void, + upper: *c_void, + }, + pkey: u32, + }, + }, + sigpoll: extern struct { + band: isize, + fd: i32, + }, + sigsys: extern struct { + call_addr: *c_void, + syscall: i32, + native_arch: u32, + }, +}; + +pub const siginfo_t = if (is_mips) + extern struct { + signo: i32, + code: i32, + errno: i32, + fields: siginfo_fields_union, + } +else + extern struct { + signo: i32, + errno: i32, + code: i32, + fields: siginfo_fields_union, + }; + +pub const io_uring_params = extern struct { + sq_entries: u32, + cq_entries: u32, + flags: u32, + sq_thread_cpu: u32, + sq_thread_idle: u32, + features: u32, + wq_fd: u32, + resv: [3]u32, + sq_off: io_sqring_offsets, + cq_off: io_cqring_offsets, +}; + +// io_uring_params.features flags + +pub const IORING_FEAT_SINGLE_MMAP = 1 << 0; +pub const IORING_FEAT_NODROP = 1 << 1; +pub const IORING_FEAT_SUBMIT_STABLE = 1 << 2; +pub const IORING_FEAT_RW_CUR_POS = 1 << 3; +pub const IORING_FEAT_CUR_PERSONALITY = 1 << 4; +pub const IORING_FEAT_FAST_POLL = 1 << 5; +pub const IORING_FEAT_POLL_32BITS = 1 << 6; + +// io_uring_params.flags + +/// io_context is polled +pub const IORING_SETUP_IOPOLL = 1 << 0; + +/// SQ poll thread +pub const IORING_SETUP_SQPOLL = 1 << 1; + +/// sq_thread_cpu is valid +pub const IORING_SETUP_SQ_AFF = 1 << 2; + +/// app defines CQ size +pub const IORING_SETUP_CQSIZE = 1 << 3; + +/// clamp SQ/CQ ring sizes +pub const IORING_SETUP_CLAMP = 1 << 4; + +/// attach to existing wq +pub const IORING_SETUP_ATTACH_WQ = 1 << 5; + +/// start with ring disabled +pub const IORING_SETUP_R_DISABLED = 1 << 6; + +pub const io_sqring_offsets = extern struct { + /// offset of ring head + head: u32, + + /// offset of ring tail + tail: u32, + + /// ring mask value + ring_mask: u32, + + /// entries in ring + ring_entries: u32, + + /// ring flags + flags: u32, + + /// number of sqes not submitted + dropped: u32, + + /// sqe index array + array: u32, + + resv1: u32, + resv2: u64, +}; + +// io_sqring_offsets.flags + +/// needs io_uring_enter wakeup +pub const IORING_SQ_NEED_WAKEUP = 1 << 0; + +/// kernel has cqes waiting beyond the cq ring +pub const IORING_SQ_CQ_OVERFLOW = 1 << 1; + +pub const io_cqring_offsets = extern struct { + head: u32, + tail: u32, + ring_mask: u32, + ring_entries: u32, + overflow: u32, + cqes: u32, + resv: [2]u64, +}; + +pub const io_uring_sqe = extern struct { + opcode: IORING_OP, + flags: u8, + ioprio: u16, + fd: i32, + off: u64, + addr: u64, + len: u32, + rw_flags: u32, + user_data: u64, + buf_index: u16, + personality: u16, + splice_fd_in: i32, + __pad2: [2]u64, +}; + +pub const IOSQE_BIT = enum(u8) { + FIXED_FILE, + IO_DRAIN, + IO_LINK, + IO_HARDLINK, + ASYNC, + BUFFER_SELECT, + + _, +}; + +// io_uring_sqe.flags + +/// use fixed fileset +pub const IOSQE_FIXED_FILE = 1 << @enumToInt(IOSQE_BIT.FIXED_FILE); + +/// issue after inflight IO +pub const IOSQE_IO_DRAIN = 1 << @enumToInt(IOSQE_BIT.IO_DRAIN); + +/// links next sqe +pub const IOSQE_IO_LINK = 1 << @enumToInt(IOSQE_BIT.IO_LINK); + +/// like LINK, but stronger +pub const IOSQE_IO_HARDLINK = 1 << @enumToInt(IOSQE_BIT.IO_HARDLINK); + +/// always go async +pub const IOSQE_ASYNC = 1 << @enumToInt(IOSQE_BIT.ASYNC); + +/// select buffer from buf_group +pub const IOSQE_BUFFER_SELECT = 1 << @enumToInt(IOSQE_BIT.BUFFER_SELECT); + +pub const IORING_OP = enum(u8) { + NOP, + READV, + WRITEV, + FSYNC, + READ_FIXED, + WRITE_FIXED, + POLL_ADD, + POLL_REMOVE, + SYNC_FILE_RANGE, + SENDMSG, + RECVMSG, + TIMEOUT, + TIMEOUT_REMOVE, + ACCEPT, + ASYNC_CANCEL, + LINK_TIMEOUT, + CONNECT, + FALLOCATE, + OPENAT, + CLOSE, + FILES_UPDATE, + STATX, + READ, + WRITE, + FADVISE, + MADVISE, + SEND, + RECV, + OPENAT2, + EPOLL_CTL, + SPLICE, + PROVIDE_BUFFERS, + REMOVE_BUFFERS, + TEE, + + _, +}; + +// io_uring_sqe.fsync_flags +pub const IORING_FSYNC_DATASYNC = 1 << 0; + +// io_uring_sqe.timeout_flags +pub const IORING_TIMEOUT_ABS = 1 << 0; + +// IO completion data structure (Completion Queue Entry) +pub const io_uring_cqe = extern struct { + /// io_uring_sqe.data submission passed back + user_data: u64, + + /// result code for this event + res: i32, + flags: u32, + + pub fn err(self: io_uring_cqe) E { + if (self.res > -4096 and self.res < 0) { + return @intToEnum(E, -self.res); + } + return .SUCCESS; + } +}; + +// io_uring_cqe.flags + +/// If set, the upper 16 bits are the buffer ID +pub const IORING_CQE_F_BUFFER = 1 << 0; + +pub const IORING_OFF_SQ_RING = 0; +pub const IORING_OFF_CQ_RING = 0x8000000; +pub const IORING_OFF_SQES = 0x10000000; + +// io_uring_enter flags +pub const IORING_ENTER_GETEVENTS = 1 << 0; +pub const IORING_ENTER_SQ_WAKEUP = 1 << 1; + +// io_uring_register opcodes and arguments +pub const IORING_REGISTER = enum(u8) { + REGISTER_BUFFERS, + UNREGISTER_BUFFERS, + REGISTER_FILES, + UNREGISTER_FILES, + REGISTER_EVENTFD, + UNREGISTER_EVENTFD, + REGISTER_FILES_UPDATE, + REGISTER_EVENTFD_ASYNC, + REGISTER_PROBE, + REGISTER_PERSONALITY, + UNREGISTER_PERSONALITY, + REGISTER_RESTRICTIONS, + REGISTER_ENABLE_RINGS, + + _, +}; + +pub const io_uring_files_update = extern struct { + offset: u32, + resv: u32, + fds: u64, +}; + +pub const IO_URING_OP_SUPPORTED = 1 << 0; + +pub const io_uring_probe_op = extern struct { + op: IORING_OP, + + resv: u8, + + /// IO_URING_OP_* flags + flags: u16, + + resv2: u32, +}; + +pub const io_uring_probe = extern struct { + /// last opcode supported + last_op: IORING_OP, + + /// Number of io_uring_probe_op following + ops_len: u8, + + resv: u16, + resv2: u32[3], + + // Followed by up to `ops_len` io_uring_probe_op structures +}; + +pub const io_uring_restriction = extern struct { + opcode: u16, + arg: extern union { + /// IORING_RESTRICTION_REGISTER_OP + register_op: IORING_REGISTER, + + /// IORING_RESTRICTION_SQE_OP + sqe_op: IORING_OP, + + /// IORING_RESTRICTION_SQE_FLAGS_* + sqe_flags: u8, + }, + resv: u8, + resv2: u32[3], +}; + +/// io_uring_restriction->opcode values +pub const IORING_RESTRICTION = enum(u8) { + /// Allow an io_uring_register(2) opcode + REGISTER_OP = 0, + + /// Allow an sqe opcode + SQE_OP = 1, + + /// Allow sqe flags + SQE_FLAGS_ALLOWED = 2, + + /// Require sqe flags (these flags must be set on each submission) + SQE_FLAGS_REQUIRED = 3, + + _, +}; + +pub const utsname = extern struct { + sysname: [64:0]u8, + nodename: [64:0]u8, + release: [64:0]u8, + version: [64:0]u8, + machine: [64:0]u8, + domainname: [64:0]u8, +}; +pub const HOST_NAME_MAX = 64; + +pub const STATX_TYPE = 0x0001; +pub const STATX_MODE = 0x0002; +pub const STATX_NLINK = 0x0004; +pub const STATX_UID = 0x0008; +pub const STATX_GID = 0x0010; +pub const STATX_ATIME = 0x0020; +pub const STATX_MTIME = 0x0040; +pub const STATX_CTIME = 0x0080; +pub const STATX_INO = 0x0100; +pub const STATX_SIZE = 0x0200; +pub const STATX_BLOCKS = 0x0400; +pub const STATX_BASIC_STATS = 0x07ff; + +pub const STATX_BTIME = 0x0800; + +pub const STATX_ATTR_COMPRESSED = 0x0004; +pub const STATX_ATTR_IMMUTABLE = 0x0010; +pub const STATX_ATTR_APPEND = 0x0020; +pub const STATX_ATTR_NODUMP = 0x0040; +pub const STATX_ATTR_ENCRYPTED = 0x0800; +pub const STATX_ATTR_AUTOMOUNT = 0x1000; + +pub const statx_timestamp = extern struct { + tv_sec: i64, + tv_nsec: u32, + __pad1: u32, +}; + +/// Renamed to `Statx` to not conflict with the `statx` function. +pub const Statx = extern struct { + /// Mask of bits indicating filled fields + mask: u32, + + /// Block size for filesystem I/O + blksize: u32, + + /// Extra file attribute indicators + attributes: u64, + + /// Number of hard links + nlink: u32, + + /// User ID of owner + uid: uid_t, + + /// Group ID of owner + gid: gid_t, + + /// File type and mode + mode: u16, + __pad1: u16, + + /// Inode number + ino: u64, + + /// Total size in bytes + size: u64, + + /// Number of 512B blocks allocated + blocks: u64, + + /// Mask to show what's supported in `attributes`. + attributes_mask: u64, + + /// Last access file timestamp + atime: statx_timestamp, + + /// Creation file timestamp + btime: statx_timestamp, + + /// Last status change file timestamp + ctime: statx_timestamp, + + /// Last modification file timestamp + mtime: statx_timestamp, + + /// Major ID, if this file represents a device. + rdev_major: u32, + + /// Minor ID, if this file represents a device. + rdev_minor: u32, + + /// Major ID of the device containing the filesystem where this file resides. + dev_major: u32, + + /// Minor ID of the device containing the filesystem where this file resides. + dev_minor: u32, + + __pad2: [14]u64, +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + addr: ?*sockaddr, + canonname: ?[*:0]u8, + next: ?*addrinfo, +}; + +pub const IPPORT_RESERVED = 1024; + +pub const IPPROTO_IP = 0; +pub const IPPROTO_HOPOPTS = 0; +pub const IPPROTO_ICMP = 1; +pub const IPPROTO_IGMP = 2; +pub const IPPROTO_IPIP = 4; +pub const IPPROTO_TCP = 6; +pub const IPPROTO_EGP = 8; +pub const IPPROTO_PUP = 12; +pub const IPPROTO_UDP = 17; +pub const IPPROTO_IDP = 22; +pub const IPPROTO_TP = 29; +pub const IPPROTO_DCCP = 33; +pub const IPPROTO_IPV6 = 41; +pub const IPPROTO_ROUTING = 43; +pub const IPPROTO_FRAGMENT = 44; +pub const IPPROTO_RSVP = 46; +pub const IPPROTO_GRE = 47; +pub const IPPROTO_ESP = 50; +pub const IPPROTO_AH = 51; +pub const IPPROTO_ICMPV6 = 58; +pub const IPPROTO_NONE = 59; +pub const IPPROTO_DSTOPTS = 60; +pub const IPPROTO_MTP = 92; +pub const IPPROTO_BEETPH = 94; +pub const IPPROTO_ENCAP = 98; +pub const IPPROTO_PIM = 103; +pub const IPPROTO_COMP = 108; +pub const IPPROTO_SCTP = 132; +pub const IPPROTO_MH = 135; +pub const IPPROTO_UDPLITE = 136; +pub const IPPROTO_MPLS = 137; +pub const IPPROTO_RAW = 255; +pub const IPPROTO_MAX = 256; + +pub const RR_A = 1; +pub const RR_CNAME = 5; +pub const RR_AAAA = 28; + +/// Turn off Nagle's algorithm +pub const TCP_NODELAY = 1; +/// Limit MSS +pub const TCP_MAXSEG = 2; +/// Never send partially complete segments. +pub const TCP_CORK = 3; +/// Start keeplives after this period, in seconds +pub const TCP_KEEPIDLE = 4; +/// Interval between keepalives +pub const TCP_KEEPINTVL = 5; +/// Number of keepalives before death +pub const TCP_KEEPCNT = 6; +/// Number of SYN retransmits +pub const TCP_SYNCNT = 7; +/// Life time of orphaned FIN-WAIT-2 state +pub const TCP_LINGER2 = 8; +/// Wake up listener only when data arrive +pub const TCP_DEFER_ACCEPT = 9; +/// Bound advertised window +pub const TCP_WINDOW_CLAMP = 10; +/// Information about this connection. +pub const TCP_INFO = 11; +/// Block/reenable quick acks +pub const TCP_QUICKACK = 12; +/// Congestion control algorithm +pub const TCP_CONGESTION = 13; +/// TCP MD5 Signature (RFC2385) +pub const TCP_MD5SIG = 14; +/// Use linear timeouts for thin streams +pub const TCP_THIN_LINEAR_TIMEOUTS = 16; +/// Fast retrans. after 1 dupack +pub const TCP_THIN_DUPACK = 17; +/// How long for loss retry before timeout +pub const TCP_USER_TIMEOUT = 18; +/// TCP sock is under repair right now +pub const TCP_REPAIR = 19; +pub const TCP_REPAIR_QUEUE = 20; +pub const TCP_QUEUE_SEQ = 21; +pub const TCP_REPAIR_OPTIONS = 22; +/// Enable FastOpen on listeners +pub const TCP_FASTOPEN = 23; +pub const TCP_TIMESTAMP = 24; +/// limit number of unsent bytes in write queue +pub const TCP_NOTSENT_LOWAT = 25; +/// Get Congestion Control (optional) info +pub const TCP_CC_INFO = 26; +/// Record SYN headers for new connections +pub const TCP_SAVE_SYN = 27; +/// Get SYN headers recorded for connection +pub const TCP_SAVED_SYN = 28; +/// Get/set window parameters +pub const TCP_REPAIR_WINDOW = 29; +/// Attempt FastOpen with connect +pub const TCP_FASTOPEN_CONNECT = 30; +/// Attach a ULP to a TCP connection +pub const TCP_ULP = 31; +/// TCP MD5 Signature with extensions +pub const TCP_MD5SIG_EXT = 32; +/// Set the key for Fast Open (cookie) +pub const TCP_FASTOPEN_KEY = 33; +/// Enable TFO without a TFO cookie +pub const TCP_FASTOPEN_NO_COOKIE = 34; +pub const TCP_ZEROCOPY_RECEIVE = 35; +/// Notify bytes available to read as a cmsg on read +pub const TCP_INQ = 36; +pub const TCP_CM_INQ = TCP_INQ; +/// delay outgoing packets by XX usec +pub const TCP_TX_DELAY = 37; + +pub const TCP_REPAIR_ON = 1; +pub const TCP_REPAIR_OFF = 0; +/// Turn off without window probes +pub const TCP_REPAIR_OFF_NO_WP = -1; + +pub const tcp_repair_opt = extern struct { + opt_code: u32, + opt_val: u32, +}; + +pub const tcp_repair_window = extern struct { + snd_wl1: u32, + snd_wnd: u32, + max_window: u32, + rcv_wnd: u32, + rcv_wup: u32, +}; + +pub const TcpRepairOption = enum { + TCP_NO_QUEUE, + TCP_RECV_QUEUE, + TCP_SEND_QUEUE, + TCP_QUEUES_NR, +}; + +/// why fastopen failed from client perspective +pub const tcp_fastopen_client_fail = enum { + /// catch-all + TFO_STATUS_UNSPEC, + /// if not in TFO_CLIENT_NO_COOKIE mode + TFO_COOKIE_UNAVAILABLE, + /// SYN-ACK did not ack SYN data + TFO_DATA_NOT_ACKED, + /// SYN-ACK did not ack SYN data after timeout + TFO_SYN_RETRANSMITTED, +}; + +/// for TCP_INFO socket option +pub const TCPI_OPT_TIMESTAMPS = 1; +pub const TCPI_OPT_SACK = 2; +pub const TCPI_OPT_WSCALE = 4; +/// ECN was negociated at TCP session init +pub const TCPI_OPT_ECN = 8; +/// we received at least one packet with ECT +pub const TCPI_OPT_ECN_SEEN = 16; +/// SYN-ACK acked data in SYN sent or rcvd +pub const TCPI_OPT_SYN_DATA = 32; + +pub const nfds_t = usize; +pub const pollfd = extern struct { + fd: fd_t, + events: i16, + revents: i16, +}; + +pub const POLLIN = 0x001; +pub const POLLPRI = 0x002; +pub const POLLOUT = 0x004; +pub const POLLERR = 0x008; +pub const POLLHUP = 0x010; +pub const POLLNVAL = 0x020; +pub const POLLRDNORM = 0x040; +pub const POLLRDBAND = 0x080; + +pub const MFD_CLOEXEC = 0x0001; +pub const MFD_ALLOW_SEALING = 0x0002; +pub const MFD_HUGETLB = 0x0004; +pub const MFD_ALL_FLAGS = MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB; + +pub const HUGETLB_FLAG_ENCODE_SHIFT = 26; +pub const HUGETLB_FLAG_ENCODE_MASK = 0x3f; +pub const HUGETLB_FLAG_ENCODE_64KB = 16 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_512KB = 19 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_1MB = 20 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_2MB = 21 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_8MB = 23 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_16MB = 24 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_32MB = 25 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_256MB = 28 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_512MB = 29 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_1GB = 30 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_2GB = 31 << HUGETLB_FLAG_ENCODE_SHIFT; +pub const HUGETLB_FLAG_ENCODE_16GB = 34 << HUGETLB_FLAG_ENCODE_SHIFT; + +pub const MFD_HUGE_SHIFT = HUGETLB_FLAG_ENCODE_SHIFT; +pub const MFD_HUGE_MASK = HUGETLB_FLAG_ENCODE_MASK; +pub const MFD_HUGE_64KB = HUGETLB_FLAG_ENCODE_64KB; +pub const MFD_HUGE_512KB = HUGETLB_FLAG_ENCODE_512KB; +pub const MFD_HUGE_1MB = HUGETLB_FLAG_ENCODE_1MB; +pub const MFD_HUGE_2MB = HUGETLB_FLAG_ENCODE_2MB; +pub const MFD_HUGE_8MB = HUGETLB_FLAG_ENCODE_8MB; +pub const MFD_HUGE_16MB = HUGETLB_FLAG_ENCODE_16MB; +pub const MFD_HUGE_32MB = HUGETLB_FLAG_ENCODE_32MB; +pub const MFD_HUGE_256MB = HUGETLB_FLAG_ENCODE_256MB; +pub const MFD_HUGE_512MB = HUGETLB_FLAG_ENCODE_512MB; +pub const MFD_HUGE_1GB = HUGETLB_FLAG_ENCODE_1GB; +pub const MFD_HUGE_2GB = HUGETLB_FLAG_ENCODE_2GB; +pub const MFD_HUGE_16GB = HUGETLB_FLAG_ENCODE_16GB; + +pub const RUSAGE_SELF = 0; +pub const RUSAGE_CHILDREN = -1; +pub const RUSAGE_THREAD = 1; + +pub const rusage = extern struct { + utime: timeval, + stime: timeval, + maxrss: isize, + ixrss: isize, + idrss: isize, + isrss: isize, + minflt: isize, + majflt: isize, + nswap: isize, + inblock: isize, + oublock: isize, + msgsnd: isize, + msgrcv: isize, + nsignals: isize, + nvcsw: isize, + nivcsw: isize, + __reserved: [16]isize = [1]isize{0} ** 16, +}; + +pub const cc_t = u8; +pub const speed_t = u32; +pub const tcflag_t = u32; + +pub const NCCS = 32; + +pub const B0 = 0o0000000; +pub const B50 = 0o0000001; +pub const B75 = 0o0000002; +pub const B110 = 0o0000003; +pub const B134 = 0o0000004; +pub const B150 = 0o0000005; +pub const B200 = 0o0000006; +pub const B300 = 0o0000007; +pub const B600 = 0o0000010; +pub const B1200 = 0o0000011; +pub const B1800 = 0o0000012; +pub const B2400 = 0o0000013; +pub const B4800 = 0o0000014; +pub const B9600 = 0o0000015; +pub const B19200 = 0o0000016; +pub const B38400 = 0o0000017; +pub const BOTHER = 0o0010000; +pub const B57600 = 0o0010001; +pub const B115200 = 0o0010002; +pub const B230400 = 0o0010003; +pub const B460800 = 0o0010004; +pub const B500000 = 0o0010005; +pub const B576000 = 0o0010006; +pub const B921600 = 0o0010007; +pub const B1000000 = 0o0010010; +pub const B1152000 = 0o0010011; +pub const B1500000 = 0o0010012; +pub const B2000000 = 0o0010013; +pub const B2500000 = 0o0010014; +pub const B3000000 = 0o0010015; +pub const B3500000 = 0o0010016; +pub const B4000000 = 0o0010017; + +pub const V = switch (native_arch) { + .powerpc, .powerpc64, .powerpc64le => struct { + pub const INTR = 0; + pub const QUIT = 1; + pub const ERASE = 2; + pub const KILL = 3; + pub const EOF = 4; + pub const MIN = 5; + pub const EOL = 6; + pub const TIME = 7; + pub const EOL2 = 8; + pub const SWTC = 9; + pub const WERASE = 10; + pub const REPRINT = 11; + pub const SUSP = 12; + pub const START = 13; + pub const STOP = 14; + pub const LNEXT = 15; + pub const DISCARD = 16; + }, + .sparc, .sparcv9 => struct { + pub const INTR = 0; + pub const QUIT = 1; + pub const ERASE = 2; + pub const KILL = 3; + pub const EOF = 4; + pub const EOL = 5; + pub const EOL2 = 6; + pub const SWTC = 7; + pub const START = 8; + pub const STOP = 9; + pub const SUSP = 10; + pub const DSUSP = 11; + pub const REPRINT = 12; + pub const DISCARD = 13; + pub const WERASE = 14; + pub const LNEXT = 15; + pub const MIN = VEOF; + pub const TIME = VEOL; + }, + .mips, .mipsel, .mips64, .mips64el => struct { + pub const INTR = 0; + pub const QUIT = 1; + pub const ERASE = 2; + pub const KILL = 3; + pub const MIN = 4; + pub const TIME = 5; + pub const EOL2 = 6; + pub const SWTC = 7; + pub const SWTCH = 7; + pub const START = 8; + pub const STOP = 9; + pub const SUSP = 10; + pub const REPRINT = 12; + pub const DISCARD = 13; + pub const WERASE = 14; + pub const LNEXT = 15; + pub const EOF = 16; + pub const EOL = 17; + }, + else => struct { + pub const INTR = 0; + pub const QUIT = 1; + pub const ERASE = 2; + pub const KILL = 3; + pub const EOF = 4; + pub const TIME = 5; + pub const MIN = 6; + pub const SWTC = 7; + pub const START = 8; + pub const STOP = 9; + pub const SUSP = 10; + pub const EOL = 11; + pub const REPRINT = 12; + pub const DISCARD = 13; + pub const WERASE = 14; + pub const LNEXT = 15; + pub const EOL2 = 16; + }, +}; + +pub const IGNBRK = 1; +pub const BRKINT = 2; +pub const IGNPAR = 4; +pub const PARMRK = 8; +pub const INPCK = 16; +pub const ISTRIP = 32; +pub const INLCR = 64; +pub const IGNCR = 128; +pub const ICRNL = 256; +pub const IUCLC = 512; +pub const IXON = 1024; +pub const IXANY = 2048; +pub const IXOFF = 4096; +pub const IMAXBEL = 8192; +pub const IUTF8 = 16384; + +pub const OPOST = 1; +pub const OLCUC = 2; +pub const ONLCR = 4; +pub const OCRNL = 8; +pub const ONOCR = 16; +pub const ONLRET = 32; +pub const OFILL = 64; +pub const OFDEL = 128; +pub const VTDLY = 16384; +pub const VT0 = 0; +pub const VT1 = 16384; + +pub const CSIZE = 48; +pub const CS5 = 0; +pub const CS6 = 16; +pub const CS7 = 32; +pub const CS8 = 48; +pub const CSTOPB = 64; +pub const CREAD = 128; +pub const PARENB = 256; +pub const PARODD = 512; +pub const HUPCL = 1024; +pub const CLOCAL = 2048; + +pub const ISIG = 1; +pub const ICANON = 2; +pub const ECHO = 8; +pub const ECHOE = 16; +pub const ECHOK = 32; +pub const ECHONL = 64; +pub const NOFLSH = 128; +pub const TOSTOP = 256; +pub const IEXTEN = 32768; + +pub const TCSA = enum(c_uint) { + NOW, + DRAIN, + FLUSH, + _, +}; + +pub const termios = extern struct { + iflag: tcflag_t, + oflag: tcflag_t, + cflag: tcflag_t, + lflag: tcflag_t, + line: cc_t, + cc: [NCCS]cc_t, + ispeed: speed_t, + ospeed: speed_t, +}; + +pub const SIOCGIFINDEX = 0x8933; +pub const IFNAMESIZE = 16; + +pub const ifmap = extern struct { + mem_start: u32, + mem_end: u32, + base_addr: u16, + irq: u8, + dma: u8, + port: u8, +}; + +pub const ifreq = extern struct { + ifrn: extern union { + name: [IFNAMESIZE]u8, + }, + ifru: extern union { + addr: sockaddr, + dstaddr: sockaddr, + broadaddr: sockaddr, + netmask: sockaddr, + hwaddr: sockaddr, + flags: i16, + ivalue: i32, + mtu: i32, + map: ifmap, + slave: [IFNAMESIZE - 1:0]u8, + newname: [IFNAMESIZE - 1:0]u8, + data: ?[*]u8, + }, +}; + +// doc comments copied from musl +pub const rlimit_resource = enum(c_int) { + /// Per-process CPU limit, in seconds. + CPU, + + /// Largest file that can be created, in bytes. + FSIZE, + + /// Maximum size of data segment, in bytes. + DATA, + + /// Maximum size of stack segment, in bytes. + STACK, + + /// Largest core file that can be created, in bytes. + CORE, + + /// Largest resident set size, in bytes. + /// This affects swapping; processes that are exceeding their + /// resident set size will be more likely to have physical memory + /// taken from them. + RSS, + + /// Number of processes. + NPROC, + + /// Number of open files. + NOFILE, + + /// Locked-in-memory address space. + MEMLOCK, + + /// Address space limit. + AS, + + /// Maximum number of file locks. + LOCKS, + + /// Maximum number of pending signals. + SIGPENDING, + + /// Maximum bytes in POSIX message queues. + MSGQUEUE, + + /// Maximum nice priority allowed to raise to. + /// Nice levels 19 .. -20 correspond to 0 .. 39 + /// values of this resource limit. + NICE, + + /// Maximum realtime priority allowed for non-priviledged + /// processes. + RTPRIO, + + /// Maximum CPU time in µs that a process scheduled under a real-time + /// scheduling policy may consume without making a blocking system + /// call before being forcibly descheduled. + RTTIME, + + _, +}; + +pub const rlim_t = u64; + +/// No limit +pub const RLIM_INFINITY = ~@as(rlim_t, 0); + +pub const RLIM_SAVED_MAX = RLIM_INFINITY; +pub const RLIM_SAVED_CUR = RLIM_INFINITY; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const MADV = struct { + pub const NORMAL = 0; + pub const RANDOM = 1; + pub const SEQUENTIAL = 2; + pub const WILLNEED = 3; + pub const DONTNEED = 4; + pub const FREE = 8; + pub const REMOVE = 9; + pub const DONTFORK = 10; + pub const DOFORK = 11; + pub const MERGEABLE = 12; + pub const UNMERGEABLE = 13; + pub const HUGEPAGE = 14; + pub const NOHUGEPAGE = 15; + pub const DONTDUMP = 16; + pub const DODUMP = 17; + pub const WIPEONFORK = 18; + pub const KEEPONFORK = 19; + pub const COLD = 20; + pub const PAGEOUT = 21; + pub const HWPOISON = 100; + pub const SOFT_OFFLINE = 101; +}; + +pub const POSIX_FADV = switch (native_arch) { + .s390x => if (@typeInfo(usize).Int.bits == 64) struct { + pub const NORMAL = 0; + pub const RANDOM = 1; + pub const SEQUENTIAL = 2; + pub const WILLNEED = 3; + pub const DONTNEED = 6; + pub const NOREUSE = 7; + } else struct { + pub const NORMAL = 0; + pub const RANDOM = 1; + pub const SEQUENTIAL = 2; + pub const WILLNEED = 3; + pub const DONTNEED = 4; + pub const NOREUSE = 5; + }, + else => struct { + pub const NORMAL = 0; + pub const RANDOM = 1; + pub const SEQUENTIAL = 2; + pub const WILLNEED = 3; + pub const DONTNEED = 4; + pub const NOREUSE = 5; + }, +}; + +pub const __kernel_timespec = extern struct { + tv_sec: i64, + tv_nsec: i64, +}; + +pub const XDP = struct { + pub const SHARED_UMEM = (1 << 0); + pub const COPY = (1 << 1); + pub const ZEROCOPY = (1 << 2); + pub const UMEM_UNALIGNED_CHUNK_FLAG = (1 << 0); + pub const USE_NEED_WAKEUP = (1 << 3); + + pub const MMAP_OFFSETS = 1; + pub const RX_RING = 2; + pub const TX_RING = 3; + pub const UMEM_REG = 4; + pub const UMEM_FILL_RING = 5; + pub const UMEM_COMPLETION_RING = 6; + pub const STATISTICS = 7; + pub const OPTIONS = 8; + + pub const OPTIONS_ZEROCOPY = (1 << 0); + + pub const PGOFF_RX_RING = 0; + pub const PGOFF_TX_RING = 0x80000000; + pub const UMEM_PGOFF_FILL_RING = 0x100000000; + pub const UMEM_PGOFF_COMPLETION_RING = 0x180000000; +}; + +pub const sockaddr_xdp = extern struct { + family: u16 = AF_XDP, + flags: u16, + ifindex: u32, + queue_id: u32, + shared_umem_fd: u32, +}; + +pub const xdp_ring_offset = extern struct { + producer: u64, + consumer: u64, + desc: u64, + flags: u64, +}; + +pub const xdp_mmap_offsets = extern struct { + rx: xdp_ring_offset, + tx: xdp_ring_offset, + fr: xdp_ring_offset, + cr: xdp_ring_offset, +}; + +pub const xdp_umem_reg = extern struct { + addr: u64, + len: u64, + chunk_size: u32, + headroom: u32, + flags: u32, +}; + +pub const xdp_statistics = extern struct { + rx_dropped: u64, + rx_invalid_descs: u64, + tx_invalid_descs: u64, + rx_ring_full: u64, + rx_fill_ring_empty_descs: u64, + tx_ring_empty_descs: u64, +}; + +pub const xdp_options = extern struct { + flags: u32, +}; + +pub const XSK_UNALIGNED_BUF_OFFSET_SHIFT = 48; +pub const XSK_UNALIGNED_BUF_ADDR_MASK = (1 << XSK_UNALIGNED_BUF_OFFSET_SHIFT) - 1; + +pub const xdp_desc = extern struct { + addr: u64, + len: u32, + options: u32, +}; + +fn issecure_mask(comptime x: comptime_int) comptime_int { + return 1 << x; +} + +pub const SECUREBITS_DEFAULT = 0x00000000; + +pub const SECURE_NOROOT = 0; +pub const SECURE_NOROOT_LOCKED = 1; + +pub const SECBIT_NOROOT = issecure_mask(SECURE_NOROOT); +pub const SECBIT_NOROOT_LOCKED = issecure_mask(SECURE_NOROOT_LOCKED); + +pub const SECURE_NO_SETUID_FIXUP = 2; +pub const SECURE_NO_SETUID_FIXUP_LOCKED = 3; + +pub const SECBIT_NO_SETUID_FIXUP = issecure_mask(SECURE_NO_SETUID_FIXUP); +pub const SECBIT_NO_SETUID_FIXUP_LOCKED = issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED); + +pub const SECURE_KEEP_CAPS = 4; +pub const SECURE_KEEP_CAPS_LOCKED = 5; + +pub const SECBIT_KEEP_CAPS = issecure_mask(SECURE_KEEP_CAPS); +pub const SECBIT_KEEP_CAPS_LOCKED = issecure_mask(SECURE_KEEP_CAPS_LOCKED); + +pub const SECURE_NO_CAP_AMBIENT_RAISE = 6; +pub const SECURE_NO_CAP_AMBIENT_RAISE_LOCKED = 7; + +pub const SECBIT_NO_CAP_AMBIENT_RAISE = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); +pub const SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED); + +pub const SECURE_ALL_BITS = issecure_mask(SECURE_NOROOT) | + issecure_mask(SECURE_NO_SETUID_FIXUP) | + issecure_mask(SECURE_KEEP_CAPS) | + issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); +pub const SECURE_ALL_LOCKS = SECURE_ALL_BITS << 1; + +pub const PR = enum(i32) { + SET_PDEATHSIG = 1, + GET_PDEATHSIG = 2, + + GET_DUMPABLE = 3, + SET_DUMPABLE = 4, + + GET_UNALIGN = 5, + SET_UNALIGN = 6, + + GET_KEEPCAPS = 7, + SET_KEEPCAPS = 8, + + GET_FPEMU = 9, + SET_FPEMU = 10, + + GET_FPEXC = 11, + SET_FPEXC = 12, + + GET_TIMING = 13, + SET_TIMING = 14, + + SET_NAME = 15, + GET_NAME = 16, + + GET_ENDIAN = 19, + SET_ENDIAN = 20, + + GET_SECCOMP = 21, + SET_SECCOMP = 22, + + CAPBSET_READ = 23, + CAPBSET_DROP = 24, + + GET_TSC = 25, + SET_TSC = 26, + + GET_SECUREBITS = 27, + SET_SECUREBITS = 28, + + SET_TIMERSLACK = 29, + GET_TIMERSLACK = 30, + + TASK_PERF_EVENTS_DISABLE = 31, + TASK_PERF_EVENTS_ENABLE = 32, + + MCE_KILL = 33, + + MCE_KILL_GET = 34, + + SET_MM = 35, + + SET_PTRACER = 0x59616d61, + + SET_CHILD_SUBREAPER = 36, + GET_CHILD_SUBREAPER = 37, + + SET_NO_NEW_PRIVS = 38, + GET_NO_NEW_PRIVS = 39, + + GET_TID_ADDRESS = 40, + + SET_THP_DISABLE = 41, + GET_THP_DISABLE = 42, + + MPX_ENABLE_MANAGEMENT = 43, + MPX_DISABLE_MANAGEMENT = 44, + + SET_FP_MODE = 45, + GET_FP_MODE = 46, + + CAP_AMBIENT = 47, + + SVE_SET_VL = 50, + SVE_GET_VL = 51, + + GET_SPECULATION_CTRL = 52, + SET_SPECULATION_CTRL = 53, + + _, + + pub const UNALIGN_NOPRINT = 1; + pub const UNALIGN_SIGBUS = 2; + + pub const FPEMU_NOPRINT = 1; + pub const FPEMU_SIGFPE = 2; + + pub const FP_EXC_SW_ENABLE = 0x80; + pub const FP_EXC_DIV = 0x010000; + pub const FP_EXC_OVF = 0x020000; + pub const FP_EXC_UND = 0x040000; + pub const FP_EXC_RES = 0x080000; + pub const FP_EXC_INV = 0x100000; + pub const FP_EXC_DISABLED = 0; + pub const FP_EXC_NONRECOV = 1; + pub const FP_EXC_ASYNC = 2; + pub const FP_EXC_PRECISE = 3; + + pub const TIMING_STATISTICAL = 0; + pub const TIMING_TIMESTAMP = 1; + + pub const ENDIAN_BIG = 0; + pub const ENDIAN_LITTLE = 1; + pub const ENDIAN_PPC_LITTLE = 2; + + pub const TSC_ENABLE = 1; + pub const TSC_SIGSEGV = 2; + + pub const MCE_KILL_CLEAR = 0; + pub const MCE_KILL_SET = 1; + + pub const MCE_KILL_LATE = 0; + pub const MCE_KILL_EARLY = 1; + pub const MCE_KILL_DEFAULT = 2; + + pub const SET_MM_START_CODE = 1; + pub const SET_MM_END_CODE = 2; + pub const SET_MM_START_DATA = 3; + pub const SET_MM_END_DATA = 4; + pub const SET_MM_START_STACK = 5; + pub const SET_MM_START_BRK = 6; + pub const SET_MM_BRK = 7; + pub const SET_MM_ARG_START = 8; + pub const SET_MM_ARG_END = 9; + pub const SET_MM_ENV_START = 10; + pub const SET_MM_ENV_END = 11; + pub const SET_MM_AUXV = 12; + pub const SET_MM_EXE_FILE = 13; + pub const SET_MM_MAP = 14; + pub const SET_MM_MAP_SIZE = 15; + + pub const SET_PTRACER_ANY = std.math.maxInt(c_ulong); + + pub const FP_MODE_FR = 1 << 0; + pub const FP_MODE_FRE = 1 << 1; + + pub const CAP_AMBIENT_IS_SET = 1; + pub const CAP_AMBIENT_RAISE = 2; + pub const CAP_AMBIENT_LOWER = 3; + pub const CAP_AMBIENT_CLEAR_ALL = 4; + + pub const SVE_SET_VL_ONEXEC = 1 << 18; + pub const SVE_VL_LEN_MASK = 0xffff; + pub const SVE_VL_INHERIT = 1 << 17; + + pub const SPEC_STORE_BYPASS = 0; + pub const SPEC_NOT_AFFECTED = 0; + pub const SPEC_PRCTL = 1 << 0; + pub const SPEC_ENABLE = 1 << 1; + pub const SPEC_DISABLE = 1 << 2; + pub const SPEC_FORCE_DISABLE = 1 << 3; +}; + +pub const prctl_mm_map = extern struct { + start_code: u64, + end_code: u64, + start_data: u64, + end_data: u64, + start_brk: u64, + brk: u64, + start_stack: u64, + arg_start: u64, + arg_end: u64, + env_start: u64, + env_end: u64, + auxv: *u64, + auxv_size: u32, + exe_fd: u32, +}; + +pub const NETLINK = struct { + + /// Routing/device hook + pub const ROUTE = 0; + + /// Unused number + pub const UNUSED = 1; + + /// Reserved for user mode socket protocols + pub const USERSOCK = 2; + + /// Unused number, formerly ip_queue + pub const FIREWALL = 3; + + /// socket monitoring + pub const SOCK_DIAG = 4; + + /// netfilter/iptables ULOG + pub const NFLOG = 5; + + /// ipsec + pub const XFRM = 6; + + /// SELinux event notifications + pub const SELINUX = 7; + + /// Open-iSCSI + pub const ISCSI = 8; + + /// auditing + pub const AUDIT = 9; + + pub const FIB_LOOKUP = 10; + + pub const CONNECTOR = 11; + + /// netfilter subsystem + pub const NETFILTER = 12; + + pub const IP6_FW = 13; + + /// DECnet routing messages + pub const DNRTMSG = 14; + + /// Kernel messages to userspace + pub const KOBJECT_UEVENT = 15; + + pub const GENERIC = 16; + + // leave room for NETLINK_DM (DM Events) + + /// SCSI Transports + pub const SCSITRANSPORT = 18; + + pub const ECRYPTFS = 19; + + pub const RDMA = 20; + + /// Crypto layer + pub const CRYPTO = 21; + + /// SMC monitoring + pub const SMC = 22; +}; + +// Flags values + +/// It is request message. +pub const NLM_F_REQUEST = 0x01; + +/// Multipart message, terminated by NLMSG_DONE +pub const NLM_F_MULTI = 0x02; + +/// Reply with ack, with zero or error code +pub const NLM_F_ACK = 0x04; + +/// Echo this request +pub const NLM_F_ECHO = 0x08; + +/// Dump was inconsistent due to sequence change +pub const NLM_F_DUMP_INTR = 0x10; + +/// Dump was filtered as requested +pub const NLM_F_DUMP_FILTERED = 0x20; + +// Modifiers to GET request + +/// specify tree root +pub const NLM_F_ROOT = 0x100; + +/// return all matching +pub const NLM_F_MATCH = 0x200; + +/// atomic GET +pub const NLM_F_ATOMIC = 0x400; +pub const NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH; + +// Modifiers to NEW request + +/// Override existing +pub const NLM_F_REPLACE = 0x100; + +/// Do not touch, if it exists +pub const NLM_F_EXCL = 0x200; + +/// Create, if it does not exist +pub const NLM_F_CREATE = 0x400; + +/// Add to end of list +pub const NLM_F_APPEND = 0x800; + +// Modifiers to DELETE request + +/// Do not delete recursively +pub const NLM_F_NONREC = 0x100; + +// Flags for ACK message + +/// request was capped +pub const NLM_F_CAPPED = 0x100; + +/// extended ACK TVLs were included +pub const NLM_F_ACK_TLVS = 0x200; + +pub const NetlinkMessageType = enum(u16) { + /// < 0x10: reserved control messages + pub const MIN_TYPE = 0x10; + + /// Nothing. + NOOP = 0x1, + + /// Error + ERROR = 0x2, + + /// End of a dump + DONE = 0x3, + + /// Data lost + OVERRUN = 0x4, + + // rtlink types + + RTM_NEWLINK = 16, + RTM_DELLINK, + RTM_GETLINK, + RTM_SETLINK, + + RTM_NEWADDR = 20, + RTM_DELADDR, + RTM_GETADDR, + + RTM_NEWROUTE = 24, + RTM_DELROUTE, + RTM_GETROUTE, + + RTM_NEWNEIGH = 28, + RTM_DELNEIGH, + RTM_GETNEIGH, + + RTM_NEWRULE = 32, + RTM_DELRULE, + RTM_GETRULE, + + RTM_NEWQDISC = 36, + RTM_DELQDISC, + RTM_GETQDISC, + + RTM_NEWTCLASS = 40, + RTM_DELTCLASS, + RTM_GETTCLASS, + + RTM_NEWTFILTER = 44, + RTM_DELTFILTER, + RTM_GETTFILTER, + + RTM_NEWACTION = 48, + RTM_DELACTION, + RTM_GETACTION, + + RTM_NEWPREFIX = 52, + + RTM_GETMULTICAST = 58, + + RTM_GETANYCAST = 62, + + RTM_NEWNEIGHTBL = 64, + RTM_GETNEIGHTBL = 66, + RTM_SETNEIGHTBL, + + RTM_NEWNDUSEROPT = 68, + + RTM_NEWADDRLABEL = 72, + RTM_DELADDRLABEL, + RTM_GETADDRLABEL, + + RTM_GETDCB = 78, + RTM_SETDCB, + + RTM_NEWNETCONF = 80, + RTM_DELNETCONF, + RTM_GETNETCONF = 82, + + RTM_NEWMDB = 84, + RTM_DELMDB = 85, + RTM_GETMDB = 86, + + RTM_NEWNSID = 88, + RTM_DELNSID = 89, + RTM_GETNSID = 90, + + RTM_NEWSTATS = 92, + RTM_GETSTATS = 94, + + RTM_NEWCACHEREPORT = 96, + + RTM_NEWCHAIN = 100, + RTM_DELCHAIN, + RTM_GETCHAIN, + + RTM_NEWNEXTHOP = 104, + RTM_DELNEXTHOP, + RTM_GETNEXTHOP, + + _, +}; + +/// Netlink socket address +pub const sockaddr_nl = extern struct { + family: sa_family_t = AF_NETLINK, + __pad1: c_ushort = 0, + + /// port ID + pid: u32, + + /// multicast groups mask + groups: u32, +}; + +/// Netlink message header +/// Specified in RFC 3549 Section 2.3.2 +pub const nlmsghdr = extern struct { + /// Length of message including header + len: u32, + + /// Message content + @"type": NetlinkMessageType, + + /// Additional flags + flags: u16, + + /// Sequence number + seq: u32, + + /// Sending process port ID + pid: u32, +}; + +pub const ifinfomsg = extern struct { + family: u8, + __pad1: u8 = 0, + + /// ARPHRD_* + @"type": c_ushort, + + /// Link index + index: c_int, + + /// IFF_* flags + flags: c_uint, + + /// IFF_* change mask + change: c_uint, +}; + +pub const rtattr = extern struct { + /// Length of option + len: c_ushort, + + /// Type of option + @"type": IFLA, + + pub const ALIGNTO = 4; +}; + +pub const IFLA = enum(c_ushort) { + UNSPEC, + ADDRESS, + BROADCAST, + IFNAME, + MTU, + LINK, + QDISC, + STATS, + COST, + PRIORITY, + MASTER, + + /// Wireless Extension event + WIRELESS, + + /// Protocol specific information for a link + PROTINFO, + + TXQLEN, + MAP, + WEIGHT, + OPERSTATE, + LINKMODE, + LINKINFO, + NET_NS_PID, + IFALIAS, + + /// Number of VFs if device is SR-IOV PF + NUM_VF, + + VFINFO_LIST, + STATS64, + VF_PORTS, + PORT_SELF, + AF_SPEC, + + /// Group the device belongs to + GROUP, + + NET_NS_FD, + + /// Extended info mask, VFs, etc + EXT_MASK, + + /// Promiscuity count: > 0 means acts PROMISC + PROMISCUITY, + + NUM_TX_QUEUES, + NUM_RX_QUEUES, + CARRIER, + PHYS_PORT_ID, + CARRIER_CHANGES, + PHYS_SWITCH_ID, + LINK_NETNSID, + PHYS_PORT_NAME, + PROTO_DOWN, + GSO_MAX_SEGS, + GSO_MAX_SIZE, + PAD, + XDP, + EVENT, + + NEW_NETNSID, + IF_NETNSID, + + CARRIER_UP_COUNT, + CARRIER_DOWN_COUNT, + NEW_IFINDEX, + MIN_MTU, + MAX_MTU, + + _, + + pub const TARGET_NETNSID: IFLA = .IF_NETNSID; +}; + +pub const rtnl_link_ifmap = extern struct { + mem_start: u64, + mem_end: u64, + base_addr: u64, + irq: u16, + dma: u8, + port: u8, +}; + +pub const rtnl_link_stats = extern struct { + /// total packets received + rx_packets: u32, + + /// total packets transmitted + tx_packets: u32, + + /// total bytes received + rx_bytes: u32, + + /// total bytes transmitted + tx_bytes: u32, + + /// bad packets received + rx_errors: u32, + + /// packet transmit problems + tx_errors: u32, + + /// no space in linux buffers + rx_dropped: u32, + + /// no space available in linux + tx_dropped: u32, + + /// multicast packets received + multicast: u32, + + collisions: u32, + + // detailed rx_errors + + rx_length_errors: u32, + + /// receiver ring buff overflow + rx_over_errors: u32, + + /// recved pkt with crc error + rx_crc_errors: u32, + + /// recv'd frame alignment error + rx_frame_errors: u32, + + /// recv'r fifo overrun + rx_fifo_errors: u32, + + /// receiver missed packet + rx_missed_errors: u32, + + // detailed tx_errors + tx_aborted_errors: u32, + tx_carrier_errors: u32, + tx_fifo_errors: u32, + tx_heartbeat_errors: u32, + tx_window_errors: u32, + + // for cslip etc + + rx_compressed: u32, + tx_compressed: u32, + + /// dropped, no handler found + rx_nohandler: u32, +}; + +pub const rtnl_link_stats64 = extern struct { + /// total packets received + rx_packets: u64, + + /// total packets transmitted + tx_packets: u64, + + /// total bytes received + rx_bytes: u64, + + /// total bytes transmitted + tx_bytes: u64, + + /// bad packets received + rx_errors: u64, + + /// packet transmit problems + tx_errors: u64, + + /// no space in linux buffers + rx_dropped: u64, + + /// no space available in linux + tx_dropped: u64, + + /// multicast packets received + multicast: u64, + + collisions: u64, + + // detailed rx_errors + + rx_length_errors: u64, + + /// receiver ring buff overflow + rx_over_errors: u64, + + /// recved pkt with crc error + rx_crc_errors: u64, + + /// recv'd frame alignment error + rx_frame_errors: u64, + + /// recv'r fifo overrun + rx_fifo_errors: u64, + + /// receiver missed packet + rx_missed_errors: u64, + + // detailed tx_errors + tx_aborted_errors: u64, + tx_carrier_errors: u64, + tx_fifo_errors: u64, + tx_heartbeat_errors: u64, + tx_window_errors: u64, + + // for cslip etc + + rx_compressed: u64, + tx_compressed: u64, + + /// dropped, no handler found + rx_nohandler: u64, +}; diff --git a/lib/std/os/linux/arm-eabi.zig b/lib/std/os/linux/arm-eabi.zig index 415cfd42f2..9c50f8494c 100644 --- a/lib/std/os/linux/arm-eabi.zig +++ b/lib/std/os/linux/arm-eabi.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ("svc #0" : [ret] "={r0}" (-> usize), diff --git a/lib/std/os/linux/arm64.zig b/lib/std/os/linux/arm64.zig index 3a619c5f45..c9d95ecc18 100644 --- a/lib/std/os/linux/arm64.zig +++ b/lib/std/os/linux/arm64.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ("svc #0" : [ret] "={x0}" (-> usize), diff --git a/lib/std/os/bits/linux/errno/generic.zig b/lib/std/os/linux/errno/generic.zig similarity index 100% rename from lib/std/os/bits/linux/errno/generic.zig rename to lib/std/os/linux/errno/generic.zig diff --git a/lib/std/os/bits/linux/errno/mips.zig b/lib/std/os/linux/errno/mips.zig similarity index 100% rename from lib/std/os/bits/linux/errno/mips.zig rename to lib/std/os/linux/errno/mips.zig diff --git a/lib/std/os/bits/linux/errno/sparc.zig b/lib/std/os/linux/errno/sparc.zig similarity index 100% rename from lib/std/os/bits/linux/errno/sparc.zig rename to lib/std/os/linux/errno/sparc.zig diff --git a/lib/std/os/linux/i386.zig b/lib/std/os/linux/i386.zig index 04a67da762..c40a438563 100644 --- a/lib/std/os/linux/i386.zig +++ b/lib/std/os/linux/i386.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ("int $0x80" : [ret] "={eax}" (-> usize), diff --git a/lib/std/os/linux/mips.zig b/lib/std/os/linux/mips.zig index f59f04b130..bc1ebede1a 100644 --- a/lib/std/os/linux/mips.zig +++ b/lib/std/os/linux/mips.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ( \\ syscall diff --git a/lib/std/os/linux/powerpc.zig b/lib/std/os/linux/powerpc.zig index 760655b5fa..0bcc311f58 100644 --- a/lib/std/os/linux/powerpc.zig +++ b/lib/std/os/linux/powerpc.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ( \\ sc diff --git a/lib/std/os/linux/powerpc64.zig b/lib/std/os/linux/powerpc64.zig index 760655b5fa..0bcc311f58 100644 --- a/lib/std/os/linux/powerpc64.zig +++ b/lib/std/os/linux/powerpc64.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ( \\ sc diff --git a/lib/std/os/linux/riscv64.zig b/lib/std/os/linux/riscv64.zig index 1448bfe3d3..e0aaa2a2ed 100644 --- a/lib/std/os/linux/riscv64.zig +++ b/lib/std/os/linux/riscv64.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall0(number: SYS) usize { return asm volatile ("ecall" : [ret] "={x10}" (-> usize), diff --git a/lib/std/os/linux/sparc64.zig b/lib/std/os/linux/sparc64.zig index ce41af40d6..d5fdcb6993 100644 --- a/lib/std/os/linux/sparc64.zig +++ b/lib/std/os/linux/sparc64.zig @@ -1,5 +1,3 @@ -usingnamespace @import("../bits/linux.zig"); - pub fn syscall_pipe(fd: *[2]i32) usize { return asm volatile ( \\ mov %[arg], %%g3 diff --git a/lib/std/os/linux/tls.zig b/lib/std/os/linux/tls.zig index d04bf24b5f..de9c475083 100644 --- a/lib/std/os/linux/tls.zig +++ b/lib/std/os/linux/tls.zig @@ -135,7 +135,7 @@ pub fn setThreadPointer(addr: usize) void { ); }, .x86_64 => { - const rc = std.os.linux.syscall2(.arch_prctl, std.os.linux.ARCH_SET_FS, addr); + const rc = std.os.linux.syscall2(.arch_prctl, std.os.linux.ARCH.SET_FS, addr); assert(rc == 0); }, .aarch64 => { @@ -319,8 +319,8 @@ pub fn initStaticTLS(phdrs: []elf.Phdr) void { const alloc_tls_area = os.mmap( null, tls_image.alloc_size + tls_image.alloc_align - 1, - os.PROT_READ | os.PROT_WRITE, - os.MAP_PRIVATE | os.MAP_ANONYMOUS, + os.PROT.READ | os.PROT.WRITE, + os.MAP.PRIVATE | os.MAP.ANONYMOUS, -1, 0, ) catch os.abort(); diff --git a/lib/std/os/linux/x86_64.zig b/lib/std/os/linux/x86_64.zig index 7398c3afb6..ee1931e4ea 100644 --- a/lib/std/os/linux/x86_64.zig +++ b/lib/std/os/linux/x86_64.zig @@ -1,4 +1,16 @@ -usingnamespace @import("../bits/linux.zig"); +const std = @import("../../std.zig"); +const linux = std.os.linux; + +const pid_t = linux.pid_t; +const uid_t = linux.uid_t; +const gid_t = linux.gid_t; +const clock_t = linux.clock_t; +const stack_t = linux.stack_t; +const sigset_t = linux.sigset_t; +const sockaddr = linux.sockaddr; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; +const iovec_const = linux.iovec_const; pub fn syscall0(number: SYS) usize { return asm volatile ("syscall" @@ -97,3 +109,681 @@ pub fn restore_rt() callconv(.Naked) void { : "rcx", "r11", "memory" ); } + +pub const mode_t = usize; +pub const time_t = isize; + +pub const SYS = enum(usize) { + read = 0, + write = 1, + open = 2, + close = 3, + stat = 4, + fstat = 5, + lstat = 6, + poll = 7, + lseek = 8, + mmap = 9, + mprotect = 10, + munmap = 11, + brk = 12, + rt_sigaction = 13, + rt_sigprocmask = 14, + rt_sigreturn = 15, + ioctl = 16, + pread = 17, + pwrite = 18, + readv = 19, + writev = 20, + access = 21, + pipe = 22, + select = 23, + sched_yield = 24, + mremap = 25, + msync = 26, + mincore = 27, + madvise = 28, + shmget = 29, + shmat = 30, + shmctl = 31, + dup = 32, + dup2 = 33, + pause = 34, + nanosleep = 35, + getitimer = 36, + alarm = 37, + setitimer = 38, + getpid = 39, + sendfile = 40, + socket = 41, + connect = 42, + accept = 43, + sendto = 44, + recvfrom = 45, + sendmsg = 46, + recvmsg = 47, + shutdown = 48, + bind = 49, + listen = 50, + getsockname = 51, + getpeername = 52, + socketpair = 53, + setsockopt = 54, + getsockopt = 55, + clone = 56, + fork = 57, + vfork = 58, + execve = 59, + exit = 60, + wait4 = 61, + kill = 62, + uname = 63, + semget = 64, + semop = 65, + semctl = 66, + shmdt = 67, + msgget = 68, + msgsnd = 69, + msgrcv = 70, + msgctl = 71, + fcntl = 72, + flock = 73, + fsync = 74, + fdatasync = 75, + truncate = 76, + ftruncate = 77, + getdents = 78, + getcwd = 79, + chdir = 80, + fchdir = 81, + rename = 82, + mkdir = 83, + rmdir = 84, + creat = 85, + link = 86, + unlink = 87, + symlink = 88, + readlink = 89, + chmod = 90, + fchmod = 91, + chown = 92, + fchown = 93, + lchown = 94, + umask = 95, + gettimeofday = 96, + getrlimit = 97, + getrusage = 98, + sysinfo = 99, + times = 100, + ptrace = 101, + getuid = 102, + syslog = 103, + getgid = 104, + setuid = 105, + setgid = 106, + geteuid = 107, + getegid = 108, + setpgid = 109, + getppid = 110, + getpgrp = 111, + setsid = 112, + setreuid = 113, + setregid = 114, + getgroups = 115, + setgroups = 116, + setresuid = 117, + getresuid = 118, + setresgid = 119, + getresgid = 120, + getpgid = 121, + setfsuid = 122, + setfsgid = 123, + getsid = 124, + capget = 125, + capset = 126, + rt_sigpending = 127, + rt_sigtimedwait = 128, + rt_sigqueueinfo = 129, + rt_sigsuspend = 130, + sigaltstack = 131, + utime = 132, + mknod = 133, + uselib = 134, + personality = 135, + ustat = 136, + statfs = 137, + fstatfs = 138, + sysfs = 139, + getpriority = 140, + setpriority = 141, + sched_setparam = 142, + sched_getparam = 143, + sched_setscheduler = 144, + sched_getscheduler = 145, + sched_get_priority_max = 146, + sched_get_priority_min = 147, + sched_rr_get_interval = 148, + mlock = 149, + munlock = 150, + mlockall = 151, + munlockall = 152, + vhangup = 153, + modify_ldt = 154, + pivot_root = 155, + _sysctl = 156, + prctl = 157, + arch_prctl = 158, + adjtimex = 159, + setrlimit = 160, + chroot = 161, + sync = 162, + acct = 163, + settimeofday = 164, + mount = 165, + umount2 = 166, + swapon = 167, + swapoff = 168, + reboot = 169, + sethostname = 170, + setdomainname = 171, + iopl = 172, + ioperm = 173, + create_module = 174, + init_module = 175, + delete_module = 176, + get_kernel_syms = 177, + query_module = 178, + quotactl = 179, + nfsservctl = 180, + getpmsg = 181, + putpmsg = 182, + afs_syscall = 183, + tuxcall = 184, + security = 185, + gettid = 186, + readahead = 187, + setxattr = 188, + lsetxattr = 189, + fsetxattr = 190, + getxattr = 191, + lgetxattr = 192, + fgetxattr = 193, + listxattr = 194, + llistxattr = 195, + flistxattr = 196, + removexattr = 197, + lremovexattr = 198, + fremovexattr = 199, + tkill = 200, + time = 201, + futex = 202, + sched_setaffinity = 203, + sched_getaffinity = 204, + set_thread_area = 205, + io_setup = 206, + io_destroy = 207, + io_getevents = 208, + io_submit = 209, + io_cancel = 210, + get_thread_area = 211, + lookup_dcookie = 212, + epoll_create = 213, + epoll_ctl_old = 214, + epoll_wait_old = 215, + remap_file_pages = 216, + getdents64 = 217, + set_tid_address = 218, + restart_syscall = 219, + semtimedop = 220, + fadvise64 = 221, + timer_create = 222, + timer_settime = 223, + timer_gettime = 224, + timer_getoverrun = 225, + timer_delete = 226, + clock_settime = 227, + clock_gettime = 228, + clock_getres = 229, + clock_nanosleep = 230, + exit_group = 231, + epoll_wait = 232, + epoll_ctl = 233, + tgkill = 234, + utimes = 235, + vserver = 236, + mbind = 237, + set_mempolicy = 238, + get_mempolicy = 239, + mq_open = 240, + mq_unlink = 241, + mq_timedsend = 242, + mq_timedreceive = 243, + mq_notify = 244, + mq_getsetattr = 245, + kexec_load = 246, + waitid = 247, + add_key = 248, + request_key = 249, + keyctl = 250, + ioprio_set = 251, + ioprio_get = 252, + inotify_init = 253, + inotify_add_watch = 254, + inotify_rm_watch = 255, + migrate_pages = 256, + openat = 257, + mkdirat = 258, + mknodat = 259, + fchownat = 260, + futimesat = 261, + fstatat = 262, + unlinkat = 263, + renameat = 264, + linkat = 265, + symlinkat = 266, + readlinkat = 267, + fchmodat = 268, + faccessat = 269, + pselect6 = 270, + ppoll = 271, + unshare = 272, + set_robust_list = 273, + get_robust_list = 274, + splice = 275, + tee = 276, + sync_file_range = 277, + vmsplice = 278, + move_pages = 279, + utimensat = 280, + epoll_pwait = 281, + signalfd = 282, + timerfd_create = 283, + eventfd = 284, + fallocate = 285, + timerfd_settime = 286, + timerfd_gettime = 287, + accept4 = 288, + signalfd4 = 289, + eventfd2 = 290, + epoll_create1 = 291, + dup3 = 292, + pipe2 = 293, + inotify_init1 = 294, + preadv = 295, + pwritev = 296, + rt_tgsigqueueinfo = 297, + perf_event_open = 298, + recvmmsg = 299, + fanotify_init = 300, + fanotify_mark = 301, + prlimit64 = 302, + name_to_handle_at = 303, + open_by_handle_at = 304, + clock_adjtime = 305, + syncfs = 306, + sendmmsg = 307, + setns = 308, + getcpu = 309, + process_vm_readv = 310, + process_vm_writev = 311, + kcmp = 312, + finit_module = 313, + sched_setattr = 314, + sched_getattr = 315, + renameat2 = 316, + seccomp = 317, + getrandom = 318, + memfd_create = 319, + kexec_file_load = 320, + bpf = 321, + execveat = 322, + userfaultfd = 323, + membarrier = 324, + mlock2 = 325, + copy_file_range = 326, + preadv2 = 327, + pwritev2 = 328, + pkey_mprotect = 329, + pkey_alloc = 330, + pkey_free = 331, + statx = 332, + io_pgetevents = 333, + rseq = 334, + pidfd_send_signal = 424, + io_uring_setup = 425, + io_uring_enter = 426, + io_uring_register = 427, + open_tree = 428, + move_mount = 429, + fsopen = 430, + fsconfig = 431, + fsmount = 432, + fspick = 433, + pidfd_open = 434, + clone3 = 435, + close_range = 436, + openat2 = 437, + pidfd_getfd = 438, + faccessat2 = 439, + process_madvise = 440, + epoll_pwait2 = 441, + + _, +}; + +pub const O = struct { + pub const RDONLY = 0o0; + pub const WRONLY = 0o1; + pub const RDWR = 0o2; + + pub const CREAT = 0o100; + pub const EXCL = 0o200; + pub const NOCTTY = 0o400; + pub const TRUNC = 0o1000; + pub const APPEND = 0o2000; + pub const NONBLOCK = 0o4000; + pub const DSYNC = 0o10000; + pub const SYNC = 0o4010000; + pub const RSYNC = 0o4010000; + pub const DIRECTORY = 0o200000; + pub const NOFOLLOW = 0o400000; + pub const CLOEXEC = 0o2000000; + + pub const ASYNC = 0o20000; + pub const DIRECT = 0o40000; + pub const LARGEFILE = 0; + pub const NOATIME = 0o1000000; + pub const PATH = 0o10000000; + pub const TMPFILE = 0o20200000; + pub const NDELAY = NONBLOCK; +}; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + pub const GETLK = 5; + pub const SETLK = 6; + pub const SETLKW = 7; + pub const SETOWN = 8; + pub const GETOWN = 9; + pub const SETSIG = 10; + pub const GETSIG = 11; + + pub const SETOWN_EX = 15; + pub const GETOWN_EX = 16; + pub const GETOWNER_UIDS = 17; + + pub const RDLCK = 0; + pub const WRLCK = 1; + pub const UNLCK = 2; +}; + +pub const MAP = struct { + /// Share changes + pub const SHARED = 0x01; + + /// Changes are private + pub const PRIVATE = 0x02; + + /// share + validate extension flags + pub const SHARED_VALIDATE = 0x03; + + /// Mask for type of mapping + pub const TYPE = 0x0f; + + /// Interpret addr exactly + pub const FIXED = 0x10; + + /// don't use a file + pub const ANONYMOUS = 0x20; + + /// populate (prefault) pagetables + pub const POPULATE = 0x8000; + + /// do not block on IO + pub const NONBLOCK = 0x10000; + + /// give out an address that is best suited for process/thread stacks + pub const STACK = 0x20000; + + /// create a huge page mapping + pub const HUGETLB = 0x40000; + + /// perform synchronous page faults for the mapping + pub const SYNC = 0x80000; + + /// FIXED which doesn't unmap underlying mapping + pub const FIXED_NOREPLACE = 0x100000; + + /// For anonymous mmap, memory could be uninitialized + pub const UNINITIALIZED = 0x4000000; + /// only give out 32bit addresses + pub const @"32BIT" = 0x40; + + /// stack-like segment + pub const GROWSDOWN = 0x0100; + + /// ETXTBSY + pub const DENYWRITE = 0x0800; + + /// mark it as an executable + pub const EXECUTABLE = 0x1000; + + /// pages are locked + pub const LOCKED = 0x2000; + + /// don't check for reservations + pub const NORESERVE = 0x4000; +}; + +pub const VDSO = struct { + pub const CGT_SYM = "__vdso_clock_gettime"; + pub const CGT_VER = "LINUX_2.6"; + + pub const GETCPU_SYM = "__vdso_getcpu"; + pub const GETCPU_VER = "LINUX_2.6"; +}; + +pub const ARCH = struct { + pub const SET_GS = 0x1001; + pub const SET_FS = 0x1002; + pub const GET_FS = 0x1003; + pub const GET_GS = 0x1004; +}; + +pub const REG = struct { + pub const R8 = 0; + pub const R9 = 1; + pub const R10 = 2; + pub const R11 = 3; + pub const R12 = 4; + pub const R13 = 5; + pub const R14 = 6; + pub const R15 = 7; + pub const RDI = 8; + pub const RSI = 9; + pub const RBP = 10; + pub const RBX = 11; + pub const RDX = 12; + pub const RAX = 13; + pub const RCX = 14; + pub const RSP = 15; + pub const RIP = 16; + pub const EFL = 17; + pub const CSGSFS = 18; + pub const ERR = 19; + pub const TRAPNO = 20; + pub const OLDMASK = 21; + pub const CR2 = 22; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const NB = 4; + pub const UN = 8; +}; + +pub const Flock = extern struct { + type: i16, + whence: i16, + start: off_t, + len: off_t, + pid: pid_t, +}; + +pub const msghdr = extern struct { + name: ?*sockaddr, + namelen: socklen_t, + iov: [*]iovec, + iovlen: i32, + __pad1: i32 = 0, + control: ?*c_void, + controllen: socklen_t, + __pad2: socklen_t = 0, + flags: i32, +}; + +pub const msghdr_const = extern struct { + name: ?*const sockaddr, + namelen: socklen_t, + iov: [*]iovec_const, + iovlen: i32, + __pad1: i32 = 0, + control: ?*c_void, + controllen: socklen_t, + __pad2: socklen_t = 0, + flags: i32, +}; + +pub const off_t = i64; +pub const ino_t = u64; +pub const dev_t = u64; + +// The `stat` definition used by the Linux kernel. +pub const kernel_stat = extern struct { + dev: dev_t, + ino: ino_t, + nlink: usize, + + mode: u32, + uid: uid_t, + gid: gid_t, + __pad0: u32, + rdev: dev_t, + size: off_t, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [3]isize, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +// The `stat64` definition used by the libc. +pub const libc_stat = kernel_stat; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const timeval = extern struct { + tv_sec: isize, + tv_usec: isize, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; + +pub const Elf_Symndx = u32; + +pub const greg_t = usize; +pub const gregset_t = [23]greg_t; +pub const fpstate = extern struct { + cwd: u16, + swd: u16, + ftw: u16, + fop: u16, + rip: usize, + rdp: usize, + mxcsr: u32, + mxcr_mask: u32, + st: [8]extern struct { + significand: [4]u16, + exponent: u16, + padding: [3]u16 = undefined, + }, + xmm: [16]extern struct { + element: [4]u32, + }, + padding: [24]u32 = undefined, +}; +pub const fpregset_t = *fpstate; +pub const sigcontext = extern struct { + r8: usize, + r9: usize, + r10: usize, + r11: usize, + r12: usize, + r13: usize, + r14: usize, + r15: usize, + + rdi: usize, + rsi: usize, + rbp: usize, + rbx: usize, + rdx: usize, + rax: usize, + rcx: usize, + rsp: usize, + rip: usize, + eflags: usize, + + cs: u16, + gs: u16, + fs: u16, + pad0: u16 = undefined, + + err: usize, + trapno: usize, + oldmask: usize, + cr2: usize, + + fpstate: *fpstate, + reserved1: [8]usize = undefined, +}; + +pub const mcontext_t = extern struct { + gregs: gregset_t, + fpregs: fpregset_t, + reserved1: [8]usize = undefined, +}; + +pub const ucontext_t = extern struct { + flags: usize, + link: *ucontext_t, + stack: stack_t, + mcontext: mcontext_t, + sigmask: sigset_t, + fpregs_mem: [64]usize, +}; diff --git a/lib/std/os/netbsd.zig b/lib/std/os/netbsd.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/netbsd.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/openbsd.zig b/lib/std/os/openbsd.zig deleted file mode 100644 index 5f9684b96d..0000000000 --- a/lib/std/os/openbsd.zig +++ /dev/null @@ -1,3 +0,0 @@ -const std = @import("../std.zig"); -pub usingnamespace std.c; -pub usingnamespace @import("bits.zig"); diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig index 708f445ee4..002f7ed684 100644 --- a/lib/std/os/wasi.zig +++ b/lib/std/os/wasi.zig @@ -4,8 +4,6 @@ const std = @import("std"); const assert = std.debug.assert; -pub usingnamespace @import("bits.zig"); - comptime { assert(@alignOf(i8) == 1); assert(@alignOf(u8) == 1); diff --git a/lib/std/time.zig b/lib/std/time.zig index 2306530753..9e070ef9a5 100644 --- a/lib/std/time.zig +++ b/lib/std/time.zig @@ -24,7 +24,7 @@ pub fn sleep(nanoseconds: u64) void { const w = std.os.wasi; const userdata: w.userdata_t = 0x0123_45678; const clock = w.subscription_clock_t{ - .id = w.CLOCK_MONOTONIC, + .id = w.CLOCK.MONOTONIC, .timeout = nanoseconds, .precision = 0, .flags = 0, @@ -152,7 +152,7 @@ pub const Timer = struct { /// At some point we may change our minds on RAW, but for now we're /// sticking with posix standard MONOTONIC. For more information, see: /// https://github.com/ziglang/zig/pull/933 - const monotonic_clock_id = os.CLOCK_MONOTONIC; + const monotonic_clock_id = os.CLOCK.MONOTONIC; /// Initialize the timer structure. /// Can only fail when running in a hostile environment that intentionally injects @@ -161,7 +161,7 @@ pub const Timer = struct { pub fn start() Error!Timer { // This gives us an opportunity to grab the counter frequency in windows. // On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000. - // On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not + // On Posix: CLOCK.MONOTONIC will only fail if the monotonic counter is not // supported, or if the timespec pointer is out of bounds, which should be // impossible here barring cosmic rays or other such occurrences of // incredibly bad luck.