zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit daae7e1f5aa5aac02a342c9a60d703c2a0cba579 (tree)
parent 67726e36b02d26343398ed8ede460622d706c539
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Mon, 20 May 2019 01:15:23 -0400

more progress on posix API layer

see #2380

Diffstat:
MCMakeLists.txt | 9+++++++++
Mdoc/langref.html.in | 1+
Mstd/c.zig | 70+++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Mstd/c/darwin.zig | 193+++++++------------------------------------------------------------------------
Mstd/c/freebsd.zig | 150-------------------------------------------------------------------------------
Mstd/c/netbsd.zig | 146-------------------------------------------------------------------------------
Astd/c/posix.zig | 26++++++++++++++++++++++++++
Astd/c/posix/darwin.zig | 1099+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/c/posix/freebsd.zig | 839+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/c/posix/netbsd.zig | 728+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/c/posix/windows.zig | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstd/event/fs.zig | 49+++++++++++++++++++++++++------------------------
Mstd/event/net.zig | 18++++++++++--------
Mstd/os.zig | 64++++++++++++++++++++--------------------------------------------
Mstd/os/child_process.zig | 9+--------
Mstd/os/darwin.zig | 889+------------------------------------------------------------------------------
Dstd/os/darwin/errno.zig | 328-------------------------------------------------------------------------------
Mstd/os/file.zig | 83++++++++++++-------------------------------------------------------------------
Mstd/os/freebsd.zig | 847+------------------------------------------------------------------------------
Dstd/os/freebsd/errno.zig | 121-------------------------------------------------------------------------------
Mstd/os/linux.zig | 953+++----------------------------------------------------------------------------
Mstd/os/linux/arm64.zig | 403-------------------------------------------------------------------------------
Astd/os/linux/posix.zig | 917+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/os/linux/posix/arm64.zig | 405+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/os/linux/posix/x86_64.zig | 470+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstd/os/linux/x86_64.zig | 470+------------------------------------------------------------------------------
Mstd/os/netbsd.zig | 724+------------------------------------------------------------------------------
Dstd/os/netbsd/errno.zig | 134-------------------------------------------------------------------------------
Mstd/os/path.zig | 2--
Mstd/os/posix.zig | 475+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mstd/os/wasi.zig | 320+------------------------------------------------------------------------------
Astd/os/wasi/posix.zig | 320+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstd/os/windows.zig | 12++----------
Dstd/os/windows/errno.zig | 1-
Astd/os/windows/posix.zig | 15+++++++++++++++
Mstd/os/windows/util.zig | 2--
36 files changed, 5430 insertions(+), 5948 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -474,6 +474,10 @@ set(ZIG_STD_FILES "c/linux.zig" "c/netbsd.zig" "c/windows.zig" + "c/posix.zig" + "c/posix/darwin.zig" + "c/posix/freebsd.zig" + "c/posix/windows.zig" "coff.zig" "crypto.zig" "crypto/blake2.zig" @@ -612,15 +616,20 @@ set(ZIG_STD_FILES "os/linux.zig" "os/linux/arm64.zig" "os/linux/errno.zig" + "os/linux/posix.zig" + "os/linux/posix/arm64.zig" + "os/linux/posix/x86_64.zig" "os/linux/tls.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" "os/netbsd.zig" "os/netbsd/errno.zig" "os/path.zig" + "os/posix.zig" "os/time.zig" "os/uefi.zig" "os/wasi.zig" + "os/wasi/posix.zig" "os/windows.zig" "os/windows/advapi32.zig" "os/windows/errno.zig" diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -9923,6 +9923,7 @@ keyword &lt;- KEYWORD_align / KEYWORD_and / KEYWORD_allowzero / KEYWORD_anyerror <li>Avoid local maximums.</li> <li>Reduce the amount one must remember.</li> <li>Minimize energy spent on coding style.</li> + <li>Resource deallocation must succeed.</li> <li>Together we serve end users.</li> </ul> {#header_close#} diff --git a/std/c.zig b/std/c.zig @@ -1,9 +1,10 @@ const builtin = @import("builtin"); -pub const is_the_target = builtin.link_libc; +pub const posix = @import("c/posix.zig"); +pub use posix; pub use switch (builtin.os) { - .linux => @import("c/linux.zig"), + .linux => @import("os/linux/posix.zig"), .windows => @import("c/windows.zig"), .macosx, .ios, .tvos, .watchos => @import("c/darwin.zig"), .freebsd => @import("c/freebsd.zig"), @@ -21,7 +22,6 @@ pub fn getErrno(rc: var) u12 { // TODO https://github.com/ziglang/zig/issues/265 on this whole file -pub const FILE = @OpaqueType(); pub extern "c" fn fopen(filename: [*]const u8, modes: [*]const u8) ?*FILE; pub extern "c" fn fclose(stream: *FILE) c_int; pub extern "c" fn fwrite(ptr: [*]const u8, size_of_type: usize, item_count: usize, stream: *FILE) usize; @@ -29,33 +29,36 @@ pub extern "c" fn fread(ptr: [*]u8, size_of_type: usize, item_count: usize, stre pub extern "c" fn abort() noreturn; pub extern "c" fn exit(code: c_int) noreturn; -pub extern "c" fn isatty(fd: c_int) c_int; -pub extern "c" fn close(fd: c_int) c_int; -pub extern "c" fn fstat(fd: c_int, buf: *Stat) c_int; -pub extern "c" fn @"fstat$INODE64"(fd: c_int, buf: *Stat) c_int; -pub extern "c" fn lseek(fd: c_int, offset: isize, whence: c_int) isize; +pub extern "c" fn isatty(fd: fd_t) c_int; +pub extern "c" fn close(fd: fd_t) c_int; +pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int; +pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int; +pub extern "c" fn lseek(fd: fd_t, offset: isize, whence: c_int) isize; pub extern "c" fn open(path: [*]const u8, oflag: c_int, ...) c_int; pub extern "c" fn raise(sig: c_int) c_int; -pub extern "c" fn read(fd: c_int, buf: *c_void, nbyte: usize) isize; -pub extern "c" fn pread(fd: c_int, buf: *c_void, nbyte: usize, offset: u64) isize; +pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize; +pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; +pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; +pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int; -pub extern "c" fn write(fd: c_int, buf: *const c_void, nbyte: usize) isize; -pub extern "c" fn pwrite(fd: c_int, buf: *const c_void, nbyte: usize, offset: u64) isize; -pub extern "c" fn mmap(addr: ?*c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?*c_void; +pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize; +pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; +pub extern "c" fn mmap(addr: ?*c_void, len: usize, prot: c_int, flags: c_int, fd: fd_t, offset: isize) usize; pub extern "c" fn munmap(addr: ?*c_void, len: usize) c_int; pub extern "c" fn unlink(path: [*]const u8) c_int; pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8; pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int; pub extern "c" fn fork() c_int; pub extern "c" fn access(path: [*]const u8, mode: c_uint) c_int; -pub extern "c" fn pipe(fds: *[2]c_int) c_int; +pub extern "c" fn pipe(fds: *[2]fd_t) c_int; +pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int; pub extern "c" fn mkdir(path: [*]const u8, mode: c_uint) c_int; pub extern "c" fn symlink(existing: [*]const u8, new: [*]const u8) c_int; pub extern "c" fn rename(old: [*]const u8, new: [*]const u8) c_int; pub extern "c" fn chdir(path: [*]const u8) c_int; pub extern "c" fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) c_int; -pub extern "c" fn dup(fd: c_int) c_int; -pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int; +pub extern "c" fn dup(fd: fd_t) c_int; +pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int; pub extern "c" fn readlink(noalias path: [*]const u8, noalias buf: [*]u8, bufsize: usize) isize; pub extern "c" fn realpath(noalias file_name: [*]const u8, noalias resolved_name: [*]u8) ?[*]u8; pub extern "c" fn sigprocmask(how: c_int, noalias set: *const sigset_t, noalias oset: ?*sigset_t) c_int; @@ -66,6 +69,19 @@ pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int; pub extern "c" fn rmdir(path: [*]const u8) c_int; pub extern "c" fn getenv(name: [*]const u8) ?[*]u8; +pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; +pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; +pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; + +pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int; +pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int; +pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int; +pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize; +pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int; +pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int; +pub extern "c" fn setuid(uid: c_uint) c_int; +pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void; pub extern "c" fn malloc(usize) ?*c_void; @@ -73,11 +89,19 @@ pub extern "c" fn realloc(?*c_void, usize) ?*c_void; pub extern "c" fn free(*c_void) void; pub extern "c" fn posix_memalign(memptr: **c_void, alignment: usize, size: usize) c_int; -pub extern "pthread" fn pthread_create(noalias newthread: *pthread_t, noalias attr: ?*const pthread_attr_t, start_routine: extern fn (?*c_void) ?*c_void, noalias arg: ?*c_void) c_int; -pub extern "pthread" fn pthread_attr_init(attr: *pthread_attr_t) c_int; -pub extern "pthread" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: *c_void, stacksize: usize) c_int; -pub extern "pthread" fn pthread_attr_destroy(attr: *pthread_attr_t) c_int; -pub extern "pthread" fn pthread_self() pthread_t; -pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?*?*c_void) c_int; +pub extern "c" fn pthread_create(noalias newthread: *pthread_t, noalias attr: ?*const pthread_attr_t, start_routine: extern fn (?*c_void) ?*c_void, noalias arg: ?*c_void) c_int; +pub extern "c" fn pthread_attr_init(attr: *pthread_attr_t) c_int; +pub extern "c" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: *c_void, stacksize: usize) c_int; +pub extern "c" fn pthread_attr_destroy(attr: *pthread_attr_t) c_int; +pub extern "c" fn pthread_self() pthread_t; +pub extern "c" fn pthread_join(thread: pthread_t, arg_return: ?*?*c_void) c_int; -pub const pthread_t = *@OpaqueType(); +pub extern "c" fn kqueue() c_int; +pub extern "c" fn kevent( + kq: c_int, + changelist: [*]const Kevent, + nchanges: c_int, + eventlist: [*]Kevent, + nevents: c_int, + timeout: ?*const timespec, +) c_int; diff --git a/std/c/darwin.zig b/std/c/darwin.zig @@ -1,24 +1,19 @@ -const macho = @import("../macho.zig"); +const std = @import("../std.zig"); +const assert = std.debug.assert; +const builtin = @import("builtin"); +const macho = std.macho; + +use @import("posix/darwin.zig"); extern "c" fn __error() *c_int; pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int; pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header; -pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize; +pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize; pub extern "c" fn mach_absolute_time() u64; pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void; -pub extern "c" fn kqueue() c_int; -pub extern "c" fn kevent( - kq: c_int, - changelist: [*]const Kevent, - nchanges: c_int, - eventlist: [*]Kevent, - nevents: c_int, - timeout: ?*const timespec, -) c_int; - pub extern "c" fn kevent64( kq: c_int, changelist: [*]const kevent64_s, @@ -29,13 +24,6 @@ pub extern "c" fn kevent64( timeout: ?*const timespec, ) c_int; -pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; - -pub extern "c" fn bind(socket: c_int, address: ?*const sockaddr, address_len: socklen_t) c_int; -pub extern "c" fn socket(domain: c_int, type: c_int, protocol: c_int) c_int; - const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header; /// The value of the link editor defined symbol _MH_EXECUTE_SYM is the address @@ -48,170 +36,21 @@ const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header; /// export a weak symbol here, to be overridden by the real one. pub extern "c" var _mh_execute_header: mach_hdr = undefined; comptime { - @export("__mh_execute_header", _mh_execute_header, @import("builtin").GlobalLinkage.Weak); + if (std.os.darwin.is_the_target) { + @export("__mh_execute_header", _mh_execute_header, .Weak); + } } pub const mach_header_64 = macho.mach_header_64; pub const mach_header = macho.mach_header; -pub use @import("../os/darwin/errno.zig"); - pub const _errno = __error; -pub const in_port_t = u16; -pub const sa_family_t = u8; -pub const socklen_t = u32; -pub const sockaddr = extern union { - in: sockaddr_in, - in6: sockaddr_in6, -}; -pub const sockaddr_in = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - addr: u32, - zero: [8]u8, -}; -pub const sockaddr_in6 = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - flowinfo: u32, - addr: [16]u8, - scope_id: u32, -}; - -pub const timeval = extern struct { - tv_sec: c_long, - tv_usec: i32, -}; - -pub const timezone = extern struct { - tz_minuteswest: i32, - tz_dsttime: i32, -}; - -pub const mach_timebase_info_data = extern struct { - numer: u32, - denom: u32, -}; - -/// Renamed to Stat to not conflict with the stat function. -pub const Stat = extern struct { - dev: i32, - mode: u16, - nlink: u16, - ino: u64, - uid: u32, - gid: u32, - rdev: i32, - atime: usize, - atimensec: usize, - mtime: usize, - mtimensec: usize, - ctime: usize, - ctimensec: usize, - birthtime: usize, - birthtimensec: usize, - size: i64, - blocks: i64, - blksize: i32, - flags: u32, - gen: u32, - lspare: i32, - qspare: [2]i64, -}; +pub extern "c" fn mach_host_self() mach_port_t; +pub extern "c" fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t; +pub extern "c" fn host_get_clock_service(host: host_t, clock_id: clock_id_t, clock_serv: ?[*]clock_serv_t) kern_return_t; +pub extern "c" fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t; -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; - -pub const sigset_t = u32; - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name. -pub const Sigaction = extern struct { - handler: extern fn (c_int) void, - sa_mask: sigset_t, - sa_flags: c_int, -}; - -pub const dirent = extern struct { - d_ino: usize, - d_seekoff: usize, - d_reclen: u16, - d_namlen: u16, - d_type: u8, - d_name: u8, // field address is address of first byte of name -}; - -pub const pthread_attr_t = extern struct { - __sig: c_long, - __opaque: [56]u8, -}; - -/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. -pub const Kevent = extern struct { - ident: usize, - filter: i16, - flags: u16, - fflags: u32, - data: isize, - udata: usize, -}; - -// sys/types.h on macos uses #pragma pack(4) so these checks are -// to make sure the struct is laid out the same. These values were -// produced from C code using the offsetof macro. -const std = @import("../std.zig"); -const assert = std.debug.assert; - -comptime { - assert(@byteOffsetOf(Kevent, "ident") == 0); - assert(@byteOffsetOf(Kevent, "filter") == 8); - assert(@byteOffsetOf(Kevent, "flags") == 10); - assert(@byteOffsetOf(Kevent, "fflags") == 12); - assert(@byteOffsetOf(Kevent, "data") == 16); - assert(@byteOffsetOf(Kevent, "udata") == 24); -} - -pub const kevent64_s = extern struct { - ident: u64, - filter: i16, - flags: u16, - fflags: u32, - data: i64, - udata: u64, - ext: [2]u64, -}; - -pub const mach_port_t = c_uint; -pub const clock_serv_t = mach_port_t; -pub const clock_res_t = c_int; -pub const mach_port_name_t = natural_t; -pub const natural_t = c_uint; -pub const mach_timespec_t = extern struct { - tv_sec: c_uint, - tv_nsec: clock_res_t, -}; -pub const kern_return_t = c_int; -pub const host_t = mach_port_t; -pub const CALENDAR_CLOCK = 1; - -pub extern fn mach_host_self() mach_port_t; -pub extern fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t; -pub extern fn host_get_clock_service(host: host_t, clock_id: clock_id_t, clock_serv: ?[*]clock_serv_t) kern_return_t; -pub extern fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t; - -// sys/types.h on macos uses #pragma pack() so these checks are -// to make sure the struct is laid out the same. These values were -// produced from C code using the offsetof macro. -comptime { - assert(@byteOffsetOf(kevent64_s, "ident") == 0); - assert(@byteOffsetOf(kevent64_s, "filter") == 8); - assert(@byteOffsetOf(kevent64_s, "flags") == 10); - assert(@byteOffsetOf(kevent64_s, "fflags") == 12); - assert(@byteOffsetOf(kevent64_s, "data") == 16); - assert(@byteOffsetOf(kevent64_s, "udata") == 24); - assert(@byteOffsetOf(kevent64_s, "ext") == 32); +pub fn sigaddset(set: *sigset_t, signo: u5) void { + set.* |= u32(1) << (signo - 1); } diff --git a/std/c/freebsd.zig b/std/c/freebsd.zig @@ -1,154 +1,4 @@ extern "c" fn __error() *c_int; pub const _errno = __error; -pub extern "c" fn kqueue() c_int; -pub extern "c" fn kevent( - kq: c_int, - changelist: [*]const Kevent, - nchanges: c_int, - eventlist: [*]Kevent, - nevents: c_int, - timeout: ?*const timespec, -) c_int; -pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; -pub extern "c" fn getdirentries(fd: c_int, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize; pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; -pub extern "c" fn pipe2(arg0: *[2]c_int, arg1: u32) c_int; -pub extern "c" fn preadv(fd: c_int, iov: *const c_void, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn pwritev(fd: c_int, iov: *const c_void, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn openat(fd: c_int, path: ?[*]const u8, flags: c_int) c_int; -pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int; -pub extern "c" fn setuid(uid: c_uint) c_int; -pub extern "c" fn kill(pid: c_int, sig: c_int) c_int; -pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; -pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; - -/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. -pub const Kevent = extern struct { - ident: usize, - filter: i16, - flags: u16, - fflags: u32, - data: i64, - udata: usize, - // TODO ext -}; - -pub const pthread_attr_t = extern struct { - __size: [56]u8, - __align: c_long, -}; - -pub const msghdr = extern struct { - /// optional address - msg_name: ?*sockaddr, - - /// size of address - msg_namelen: socklen_t, - - /// scatter/gather array - msg_iov: [*]iovec, - - /// # elements in msg_iov - msg_iovlen: i32, - - /// ancillary data - msg_control: ?*c_void, - - /// ancillary data buffer len - msg_controllen: socklen_t, - - /// flags on received message - msg_flags: i32, -}; - -pub const msghdr_const = extern struct { - /// optional address - msg_name: ?*const sockaddr, - - /// size of address - msg_namelen: socklen_t, - - /// scatter/gather array - msg_iov: [*]iovec_const, - - /// # elements in msg_iov - msg_iovlen: i32, - - /// ancillary data - msg_control: ?*c_void, - - /// ancillary data buffer len - msg_controllen: socklen_t, - - /// flags on received message - msg_flags: i32, -}; - -pub const Stat = extern struct { - dev: u64, - ino: u64, - nlink: usize, - - mode: u16, - __pad0: u16, - uid: u32, - gid: u32, - __pad1: u32, - rdev: u64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - birthtim: timespec, - - size: i64, - blocks: i64, - blksize: isize, - flags: u32, - gen: u64, - __spare: [10]u64, -}; - -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; - -pub const dirent = extern struct { - d_fileno: usize, - d_off: i64, - d_reclen: u16, - d_type: u8, - d_pad0: u8, - d_namlen: u16, - d_pad1: u16, - d_name: [256]u8, -}; - -pub const in_port_t = u16; -pub const sa_family_t = u16; - -pub const sockaddr = extern union { - in: sockaddr_in, - in6: sockaddr_in6, -}; - -pub const sockaddr_in = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - addr: [16]u8, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - flowinfo: u32, - addr: [16]u8, - scope_id: u32, -}; diff --git a/std/c/netbsd.zig b/std/c/netbsd.zig @@ -1,150 +1,4 @@ extern "c" fn __errno() *c_int; pub const _errno = __errno; -pub extern "c" fn kqueue() c_int; -pub extern "c" fn kevent( - kq: c_int, - changelist: [*]const Kevent, - nchanges: c_int, - eventlist: [*]Kevent, - nevents: c_int, - timeout: ?*const timespec, -) c_int; -pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; -pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; -pub extern "c" fn getdirentries(fd: c_int, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize; pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize; -pub extern "c" fn pipe2(arg0: *[2]c_int, arg1: u32) c_int; -pub extern "c" fn preadv(fd: c_int, iov: *const c_void, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn pwritev(fd: c_int, iov: *const c_void, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn openat(fd: c_int, path: ?[*]const u8, flags: c_int) c_int; -pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int; -pub extern "c" fn setuid(uid: c_uint) c_int; -pub extern "c" fn kill(pid: c_int, sig: c_int) c_int; -pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; -pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; - -/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. -pub const Kevent = extern struct { - ident: usize, - filter: i32, - flags: u32, - fflags: u32, - data: i64, - udata: usize, -}; - -pub const pthread_attr_t = extern struct { - pta_magic: u32, - pta_flags: c_int, - pta_private: *c_void, -}; - -pub const msghdr = extern struct { - /// optional address - msg_name: ?*sockaddr, - - /// size of address - msg_namelen: socklen_t, - - /// scatter/gather array - msg_iov: [*]iovec, - - /// # elements in msg_iov - msg_iovlen: i32, - - /// ancillary data - msg_control: ?*c_void, - - /// ancillary data buffer len - msg_controllen: socklen_t, - - /// flags on received message - msg_flags: i32, -}; - -pub const msghdr_const = extern struct { - /// optional address - msg_name: ?*const sockaddr, - - /// size of address - msg_namelen: socklen_t, - - /// scatter/gather array - msg_iov: [*]iovec_const, - - /// # elements in msg_iov - msg_iovlen: i32, - - /// ancillary data - msg_control: ?*c_void, - - /// ancillary data buffer len - msg_controllen: socklen_t, - - /// flags on received message - msg_flags: i32, -}; - -pub const Stat = extern struct { - dev: u64, - mode: u32, - ino: u64, - nlink: usize, - - uid: u32, - gid: u32, - rdev: u64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - birthtim: timespec, - - size: i64, - blocks: i64, - blksize: isize, - flags: u32, - gen: u32, - __spare: [2]u32, -}; - -pub const timespec = extern struct { - tv_sec: i64, - tv_nsec: isize, -}; - -pub const dirent = extern struct { - d_fileno: u64, - d_reclen: u16, - d_namlen: u16, - d_type: u8, - d_off: i64, - d_name: [512]u8, -}; - -pub const in_port_t = u16; -pub const sa_family_t = u8; - -pub const sockaddr = extern union { - in: sockaddr_in, - in6: sockaddr_in6, -}; - -pub const sockaddr_in = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - addr: u32, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - len: u8, - family: sa_family_t, - port: in_port_t, - flowinfo: u32, - addr: [16]u8, - scope_id: u32, -}; diff --git a/std/c/posix.zig b/std/c/posix.zig @@ -0,0 +1,26 @@ +// Declarations that are intended to be imported into the POSIX namespace. + +const builtin = @import("builtin"); + +pub use switch (builtin.os) { + .windows => @import("posix/windows.zig"), + .macosx, .ios, .tvos, .watchos => @import("posix/darwin.zig"), + .freebsd => @import("posix/freebsd.zig"), + .netbsd => @import("posix/netbsd.zig"), + else => struct {}, +}; + +pub const fd_t = c_int; +pub const pid_t = c_int; +pub const pthread_t = *@OpaqueType(); +pub const FILE = @OpaqueType(); + +pub const iovec = extern struct { + iov_base: [*]u8, + iov_len: usize, +}; + +pub const iovec_const = extern struct { + iov_base: [*]const u8, + iov_len: usize, +}; diff --git a/std/c/posix/darwin.zig b/std/c/posix/darwin.zig @@ -0,0 +1,1099 @@ +const std = @import("../std.zig"); +const assert = std.debug.assert; + +pub fn sigaction(sig: u5, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { + assert(sig != SIGKILL); + assert(sig != SIGSTOP); + var cact = c.Sigaction{ + .handler = @ptrCast(extern fn (c_int) void, act.handler), + .sa_flags = @bitCast(c_int, act.flags), + .sa_mask = act.mask, + }; + var coact: c.Sigaction = undefined; + const result = errnoWrap(c.sigaction(sig, &cact, &coact)); + if (result != 0) { + return result; + } + if (oact) |old| { + old.* = Sigaction{ + .handler = @ptrCast(extern fn (i32) void, coact.handler), + .flags = @bitCast(u32, coact.sa_flags), + .mask = coact.sa_mask, + }; + } + return result; +} + +pub const in_port_t = u16; +pub const sa_family_t = u8; +pub const socklen_t = u32; +pub const sockaddr = extern union { + in: sockaddr_in, + in6: sockaddr_in6, +}; +pub const sockaddr_in = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + addr: u32, + zero: [8]u8, +}; +pub const sockaddr_in6 = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, +}; + +pub const timeval = extern struct { + tv_sec: c_long, + tv_usec: i32, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; + +pub const mach_timebase_info_data = extern struct { + numer: u32, + denom: u32, +}; + +/// Renamed to Stat to not conflict with the stat function. +pub const Stat = extern struct { + dev: i32, + mode: u16, + nlink: u16, + ino: u64, + uid: u32, + gid: u32, + rdev: i32, + atime: usize, + atimensec: usize, + mtime: usize, + mtimensec: usize, + ctime: usize, + ctimensec: usize, + birthtime: usize, + birthtimensec: usize, + size: i64, + blocks: i64, + blksize: i32, + flags: u32, + gen: u32, + lspare: i32, + qspare: [2]i64, +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const sigset_t = u32; +pub const empty_sigset = sigset_t(0); + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name. +pub const Sigaction = extern struct { + handler: extern fn (c_int) void, + sa_mask: sigset_t, + sa_flags: c_int, +}; + +pub const dirent = extern struct { + d_ino: usize, + d_seekoff: usize, + d_reclen: u16, + d_namlen: u16, + d_type: u8, + d_name: u8, // field address is address of first byte of name +}; + +pub const pthread_attr_t = extern struct { + __sig: c_long, + __opaque: [56]u8, +}; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i16, + flags: u16, + fflags: u32, + data: isize, + udata: usize, +}; + +// sys/types.h on macos uses #pragma pack(4) so these checks are +// to make sure the struct is laid out the same. These values were +// produced from C code using the offsetof macro. +comptime { + assert(@byteOffsetOf(Kevent, "ident") == 0); + assert(@byteOffsetOf(Kevent, "filter") == 8); + assert(@byteOffsetOf(Kevent, "flags") == 10); + assert(@byteOffsetOf(Kevent, "fflags") == 12); + assert(@byteOffsetOf(Kevent, "data") == 16); + assert(@byteOffsetOf(Kevent, "udata") == 24); +} + +pub const kevent64_s = extern struct { + ident: u64, + filter: i16, + flags: u16, + fflags: u32, + data: i64, + udata: u64, + ext: [2]u64, +}; + +// sys/types.h on macos uses #pragma pack() so these checks are +// to make sure the struct is laid out the same. These values were +// produced from C code using the offsetof macro. +comptime { + assert(@byteOffsetOf(kevent64_s, "ident") == 0); + assert(@byteOffsetOf(kevent64_s, "filter") == 8); + assert(@byteOffsetOf(kevent64_s, "flags") == 10); + assert(@byteOffsetOf(kevent64_s, "fflags") == 12); + assert(@byteOffsetOf(kevent64_s, "data") == 16); + assert(@byteOffsetOf(kevent64_s, "udata") == 24); + assert(@byteOffsetOf(kevent64_s, "ext") == 32); +} + +pub const mach_port_t = c_uint; +pub const clock_serv_t = mach_port_t; +pub const clock_res_t = c_int; +pub const mach_port_name_t = natural_t; +pub const natural_t = c_uint; +pub const mach_timespec_t = extern struct { + tv_sec: c_uint, + tv_nsec: clock_res_t, +}; +pub const kern_return_t = c_int; +pub const host_t = mach_port_t; +pub const CALENDAR_CLOCK = 1; + +pub const PATH_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +/// [MC2] no permissions +pub const PROT_NONE = 0x00; + +/// [MC2] pages can be read +pub const PROT_READ = 0x01; + +/// [MC2] pages can be written +pub const PROT_WRITE = 0x02; + +/// [MC2] pages can be executed +pub const PROT_EXEC = 0x04; + +/// allocated from memory, swap space +pub const MAP_ANONYMOUS = 0x1000; + +/// map from file (default) +pub const MAP_FILE = 0x0000; + +/// interpret addr exactly +pub const MAP_FIXED = 0x0010; + +/// region may contain semaphores +pub const MAP_HASSEMAPHORE = 0x0200; + +/// changes are private +pub const MAP_PRIVATE = 0x0002; + +/// share changes +pub const MAP_SHARED = 0x0001; + +/// don't cache pages for this mapping +pub const MAP_NOCACHE = 0x0400; + +/// don't reserve needed swap area +pub const MAP_NORESERVE = 0x0040; +pub const MAP_FAILED = maxInt(usize); + +/// [XSI] no hang in wait/no child to reap +pub const WNOHANG = 0x00000001; + +/// [XSI] notify on stop, untraced child +pub const WUNTRACED = 0x00000002; + +/// take signal on signal stack +pub const SA_ONSTACK = 0x0001; + +/// restart system on signal return +pub const SA_RESTART = 0x0002; + +/// reset to SIG_DFL when taking signal +pub const SA_RESETHAND = 0x0004; + +/// do not generate SIGCHLD on child stop +pub const SA_NOCLDSTOP = 0x0008; + +/// don't mask the signal we're delivering +pub const SA_NODEFER = 0x0010; + +/// don't keep zombies around +pub const SA_NOCLDWAIT = 0x0020; + +/// signal handler with SA_SIGINFO args +pub const SA_SIGINFO = 0x0040; + +/// do not bounce off kernel's sigtramp +pub const SA_USERTRAMP = 0x0100; + +/// signal handler with SA_SIGINFO args with 64bit regs information +pub const SA_64REGSET = 0x0200; + +pub const O_LARGEFILE = 0x0000; +pub const O_PATH = 0x0000; + +pub const F_OK = 0; +pub const X_OK = 1; +pub const W_OK = 2; +pub const R_OK = 4; + +/// open for reading only +pub const O_RDONLY = 0x0000; + +/// open for writing only +pub const O_WRONLY = 0x0001; + +/// open for reading and writing +pub const O_RDWR = 0x0002; + +/// do not block on open or for data to become available +pub const O_NONBLOCK = 0x0004; + +/// append on each write +pub const O_APPEND = 0x0008; + +/// create file if it does not exist +pub const O_CREAT = 0x0200; + +/// truncate size to 0 +pub const O_TRUNC = 0x0400; + +/// error if O_CREAT and the file exists +pub const O_EXCL = 0x0800; + +/// atomically obtain a shared lock +pub const O_SHLOCK = 0x0010; + +/// atomically obtain an exclusive lock +pub const O_EXLOCK = 0x0020; + +/// do not follow symlinks +pub const O_NOFOLLOW = 0x0100; + +/// allow open of symlinks +pub const O_SYMLINK = 0x200000; + +/// descriptor requested for event notifications only +pub const O_EVTONLY = 0x8000; + +/// mark as close-on-exec +pub const O_CLOEXEC = 0x1000000; + +pub const O_ACCMODE = 3; +pub const O_ALERT = 536870912; +pub const O_ASYNC = 64; +pub const O_DIRECTORY = 1048576; +pub const O_DP_GETRAWENCRYPTED = 1; +pub const O_DP_GETRAWUNENCRYPTED = 2; +pub const O_DSYNC = 4194304; +pub const O_FSYNC = O_SYNC; +pub const O_NOCTTY = 131072; +pub const O_POPUP = 2147483648; +pub const O_SYNC = 128; + +pub const SEEK_SET = 0x0; +pub const SEEK_CUR = 0x1; +pub const SEEK_END = 0x2; + +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; + +/// block specified signal set +pub const SIG_BLOCK = 1; + +/// unblock specified signal set +pub const SIG_UNBLOCK = 2; + +/// set specified signal set +pub const SIG_SETMASK = 3; + +/// hangup +pub const SIGHUP = 1; + +/// interrupt +pub const SIGINT = 2; + +/// quit +pub const SIGQUIT = 3; + +/// illegal instruction (not reset when caught) +pub const SIGILL = 4; + +/// trace trap (not reset when caught) +pub const SIGTRAP = 5; + +/// abort() +pub const SIGABRT = 6; + +/// pollable event ([XSR] generated, not supported) +pub const SIGPOLL = 7; + +/// compatibility +pub const SIGIOT = SIGABRT; + +/// EMT instruction +pub const SIGEMT = 7; + +/// floating point exception +pub const SIGFPE = 8; + +/// kill (cannot be caught or ignored) +pub const SIGKILL = 9; + +/// bus error +pub const SIGBUS = 10; + +/// segmentation violation +pub const SIGSEGV = 11; + +/// bad argument to system call +pub const SIGSYS = 12; + +/// write on a pipe with no one to read it +pub const SIGPIPE = 13; + +/// alarm clock +pub const SIGALRM = 14; + +/// software termination signal from kill +pub const SIGTERM = 15; + +/// urgent condition on IO channel +pub const SIGURG = 16; + +/// sendable stop signal not from tty +pub const SIGSTOP = 17; + +/// stop signal from tty +pub const SIGTSTP = 18; + +/// continue a stopped process +pub const SIGCONT = 19; + +/// to parent on child stop or exit +pub const SIGCHLD = 20; + +/// to readers pgrp upon background tty read +pub const SIGTTIN = 21; + +/// like TTIN for output if (tp->t_local&LTOSTOP) +pub const SIGTTOU = 22; + +/// input/output possible signal +pub const SIGIO = 23; + +/// exceeded CPU time limit +pub const SIGXCPU = 24; + +/// exceeded file size limit +pub const SIGXFSZ = 25; + +/// virtual time alarm +pub const SIGVTALRM = 26; + +/// profiling time alarm +pub const SIGPROF = 27; + +/// window size changes +pub const SIGWINCH = 28; + +/// information request +pub const SIGINFO = 29; + +/// user defined signal 1 +pub const SIGUSR1 = 30; + +/// user defined signal 2 +pub const SIGUSR2 = 31; + +/// no flag value +pub const KEVENT_FLAG_NONE = 0x000; + +/// immediate timeout +pub const KEVENT_FLAG_IMMEDIATE = 0x001; + +/// output events only include change +pub const KEVENT_FLAG_ERROR_EVENTS = 0x002; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +/// unique kevent per udata value +pub const EV_UDATA_SPECIFIC = 0x0100; + +/// ... in combination with EV_DELETE +/// will defer delete until udata-specific +/// event enabled. EINPROGRESS will be +/// returned to indicate the deferral +pub const EV_DISPATCH2 = EV_DISPATCH | EV_UDATA_SPECIFIC; + +/// report that source has vanished +/// ... only valid with EV_DISPATCH2 +pub const EV_VANISHED = 0x0200; + +/// reserved by system +pub const EV_SYSFLAGS = 0xF000; + +/// filter-specific flag +pub const EV_FLAG0 = 0x1000; + +/// filter-specific flag +pub const EV_FLAG1 = 0x2000; + +/// EOF detected +pub const EV_EOF = 0x8000; + +/// error, data contains errno +pub const EV_ERROR = 0x4000; + +pub const EV_POLL = EV_FLAG0; +pub const EV_OOBAND = EV_FLAG1; + +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; + +/// attached to aio requests +pub const EVFILT_AIO = -3; + +/// attached to vnodes +pub const EVFILT_VNODE = -4; + +/// attached to struct proc +pub const EVFILT_PROC = -5; + +/// attached to struct proc +pub const EVFILT_SIGNAL = -6; + +/// timers +pub const EVFILT_TIMER = -7; + +/// Mach portsets +pub const EVFILT_MACHPORT = -8; + +/// Filesystem events +pub const EVFILT_FS = -9; + +/// User events +pub const EVFILT_USER = -10; + +/// Virtual memory events +pub const EVFILT_VM = -12; + +/// Exception events +pub const EVFILT_EXCEPT = -15; + +pub const EVFILT_SYSCOUNT = 17; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x01000000; + +/// ignore input fflags +pub const NOTE_FFNOP = 0x00000000; + +/// and fflags +pub const NOTE_FFAND = 0x40000000; + +/// or fflags +pub const NOTE_FFOR = 0x80000000; + +/// copy fflags +pub const NOTE_FFCOPY = 0xc0000000; + +/// mask for operations +pub const NOTE_FFCTRLMASK = 0xc0000000; +pub const NOTE_FFLAGSMASK = 0x00ffffff; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// OOB data +pub const NOTE_OOB = 0x00000002; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// No specific vnode event: to test for EVFILT_READ activation +pub const NOTE_NONE = 0x00000080; + +/// vnode was unlocked by flock(2) +pub const NOTE_FUNLOCK = 0x00000100; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// shared with EVFILT_SIGNAL +pub const NOTE_SIGNAL = 0x08000000; + +/// exit status to be returned, valid for child process only +pub const NOTE_EXITSTATUS = 0x04000000; + +/// provide details on reasons for exit +pub const NOTE_EXIT_DETAIL = 0x02000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); + +pub const NOTE_EXIT_DETAIL_MASK = 0x00070000; +pub const NOTE_EXIT_DECRYPTFAIL = 0x00010000; +pub const NOTE_EXIT_MEMORY = 0x00020000; +pub const NOTE_EXIT_CSERROR = 0x00040000; + +/// will react on memory pressure +pub const NOTE_VM_PRESSURE = 0x80000000; + +/// will quit on memory pressure, possibly after cleaning up dirty state +pub const NOTE_VM_PRESSURE_TERMINATE = 0x40000000; + +/// will quit immediately on memory pressure +pub const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = 0x20000000; + +/// there was an error +pub const NOTE_VM_ERROR = 0x10000000; + +/// data is seconds +pub const NOTE_SECONDS = 0x00000001; + +/// data is microseconds +pub const NOTE_USECONDS = 0x00000002; + +/// data is nanoseconds +pub const NOTE_NSECONDS = 0x00000004; + +/// absolute timeout +pub const NOTE_ABSOLUTE = 0x00000008; + +/// ext[1] holds leeway for power aware timers +pub const NOTE_LEEWAY = 0x00000010; + +/// system does minimal timer coalescing +pub const NOTE_CRITICAL = 0x00000020; + +/// system does maximum timer coalescing +pub const NOTE_BACKGROUND = 0x00000040; +pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080; + +/// data is mach absolute time units +pub const NOTE_MACHTIME = 0x00000100; + +pub const AF_UNSPEC = 0; +pub const AF_LOCAL = 1; +pub const AF_UNIX = AF_LOCAL; +pub const AF_INET = 2; +pub const AF_SYS_CONTROL = 2; +pub const AF_IMPLINK = 3; +pub const AF_PUP = 4; +pub const AF_CHAOS = 5; +pub const AF_NS = 6; +pub const AF_ISO = 7; +pub const AF_OSI = AF_ISO; +pub const AF_ECMA = 8; +pub const AF_DATAKIT = 9; +pub const AF_CCITT = 10; +pub const AF_SNA = 11; +pub const AF_DECnet = 12; +pub const AF_DLI = 13; +pub const AF_LAT = 14; +pub const AF_HYLINK = 15; +pub const AF_APPLETALK = 16; +pub const AF_ROUTE = 17; +pub const AF_LINK = 18; +pub const AF_XTP = 19; +pub const AF_COIP = 20; +pub const AF_CNT = 21; +pub const AF_RTIP = 22; +pub const AF_IPX = 23; +pub const AF_SIP = 24; +pub const AF_PIP = 25; +pub const AF_ISDN = 28; +pub const AF_E164 = AF_ISDN; +pub const AF_KEY = 29; +pub const AF_INET6 = 30; +pub const AF_NATM = 31; +pub const AF_SYSTEM = 32; +pub const AF_NETBIOS = 33; +pub const AF_PPP = 34; +pub const AF_MAX = 40; + +pub const PF_UNSPEC = AF_UNSPEC; +pub const PF_LOCAL = AF_LOCAL; +pub const PF_UNIX = PF_LOCAL; +pub const PF_INET = AF_INET; +pub const PF_IMPLINK = AF_IMPLINK; +pub const PF_PUP = AF_PUP; +pub const PF_CHAOS = AF_CHAOS; +pub const PF_NS = AF_NS; +pub const PF_ISO = AF_ISO; +pub const PF_OSI = AF_ISO; +pub const PF_ECMA = AF_ECMA; +pub const PF_DATAKIT = AF_DATAKIT; +pub const PF_CCITT = AF_CCITT; +pub const PF_SNA = AF_SNA; +pub const PF_DECnet = AF_DECnet; +pub const PF_DLI = AF_DLI; +pub const PF_LAT = AF_LAT; +pub const PF_HYLINK = AF_HYLINK; +pub const PF_APPLETALK = AF_APPLETALK; +pub const PF_ROUTE = AF_ROUTE; +pub const PF_LINK = AF_LINK; +pub const PF_XTP = AF_XTP; +pub const PF_COIP = AF_COIP; +pub const PF_CNT = AF_CNT; +pub const PF_SIP = AF_SIP; +pub const PF_IPX = AF_IPX; +pub const PF_RTIP = AF_RTIP; +pub const PF_PIP = AF_PIP; +pub const PF_ISDN = AF_ISDN; +pub const PF_KEY = AF_KEY; +pub const PF_INET6 = AF_INET6; +pub const PF_NATM = AF_NATM; +pub const PF_SYSTEM = AF_SYSTEM; +pub const PF_NETBIOS = AF_NETBIOS; +pub const PF_PPP = AF_PPP; +pub const PF_MAX = AF_MAX; + +pub const SYSPROTO_EVENT = 1; +pub const SYSPROTO_CONTROL = 2; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 2; +pub const SOCK_RAW = 3; +pub const SOCK_RDM = 4; +pub const SOCK_SEQPACKET = 5; +pub const SOCK_MAXADDRLEN = 255; + +pub const IPPROTO_ICMP = 1; +pub const IPPROTO_ICMPV6 = 58; +pub const IPPROTO_TCP = 6; +pub const IPPROTO_UDP = 17; +pub const IPPROTO_IP = 0; +pub const IPPROTO_IPV6 = 41; + +fn wstatus(x: i32) i32 { + return x & 0o177; +} +const wstopped = 0o177; +pub fn WEXITSTATUS(x: i32) i32 { + return x >> 8; +} +pub fn WTERMSIG(x: i32) i32 { + return wstatus(x); +} +pub fn WSTOPSIG(x: i32) i32 { + return x >> 8; +} +pub fn WIFEXITED(x: i32) bool { + return wstatus(x) == 0; +} +pub fn WIFSTOPPED(x: i32) bool { + return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; +} +pub fn WIFSIGNALED(x: i32) bool { + return wstatus(x) != wstopped and wstatus(x) != 0; +} + +/// Operation not permitted +pub const EPERM = 1; + +/// No such file or directory +pub const ENOENT = 2; + +/// No such process +pub const ESRCH = 3; + +/// Interrupted system call +pub const EINTR = 4; + +/// Input/output error +pub const EIO = 5; + +/// Device not configured +pub const ENXIO = 6; + +/// Argument list too long +pub const E2BIG = 7; + +/// Exec format error +pub const ENOEXEC = 8; + +/// Bad file descriptor +pub const EBADF = 9; + +/// No child processes +pub const ECHILD = 10; + +/// Resource deadlock avoided +pub const EDEADLK = 11; + +/// Cannot allocate memory +pub const ENOMEM = 12; + +/// Permission denied +pub const EACCES = 13; + +/// Bad address +pub const EFAULT = 14; + +/// Block device required +pub const ENOTBLK = 15; + +/// Device / Resource busy +pub const EBUSY = 16; + +/// File exists +pub const EEXIST = 17; + +/// Cross-device link +pub const EXDEV = 18; + +/// Operation not supported by device +pub const ENODEV = 19; + +/// Not a directory +pub const ENOTDIR = 20; + +/// Is a directory +pub const EISDIR = 21; + +/// Invalid argument +pub const EINVAL = 22; + +/// Too many open files in system +pub const ENFILE = 23; + +/// Too many open files +pub const EMFILE = 24; + +/// Inappropriate ioctl for device +pub const ENOTTY = 25; + +/// Text file busy +pub const ETXTBSY = 26; + +/// File too large +pub const EFBIG = 27; + +/// No space left on device +pub const ENOSPC = 28; + +/// Illegal seek +pub const ESPIPE = 29; + +/// Read-only file system +pub const EROFS = 30; + +/// Too many links +pub const EMLINK = 31; +/// Broken pipe + +// math software +pub const EPIPE = 32; + +/// Numerical argument out of domain +pub const EDOM = 33; +/// Result too large + +// non-blocking and interrupt i/o +pub const ERANGE = 34; + +/// Resource temporarily unavailable +pub const EAGAIN = 35; + +/// Operation would block +pub const EWOULDBLOCK = EAGAIN; + +/// Operation now in progress +pub const EINPROGRESS = 36; +/// Operation already in progress + +// ipc/network software -- argument errors +pub const EALREADY = 37; + +/// Socket operation on non-socket +pub const ENOTSOCK = 38; + +/// Destination address required +pub const EDESTADDRREQ = 39; + +/// Message too long +pub const EMSGSIZE = 40; + +/// Protocol wrong type for socket +pub const EPROTOTYPE = 41; + +/// Protocol not available +pub const ENOPROTOOPT = 42; + +/// Protocol not supported +pub const EPROTONOSUPPORT = 43; + +/// Socket type not supported +pub const ESOCKTNOSUPPORT = 44; + +/// Operation not supported +pub const ENOTSUP = 45; + +/// Protocol family not supported +pub const EPFNOSUPPORT = 46; + +/// Address family not supported by protocol family +pub const EAFNOSUPPORT = 47; + +/// Address already in use +pub const EADDRINUSE = 48; +/// Can't assign requested address + +// ipc/network software -- operational errors +pub const EADDRNOTAVAIL = 49; + +/// Network is down +pub const ENETDOWN = 50; + +/// Network is unreachable +pub const ENETUNREACH = 51; + +/// Network dropped connection on reset +pub const ENETRESET = 52; + +/// Software caused connection abort +pub const ECONNABORTED = 53; + +/// Connection reset by peer +pub const ECONNRESET = 54; + +/// No buffer space available +pub const ENOBUFS = 55; + +/// Socket is already connected +pub const EISCONN = 56; + +/// Socket is not connected +pub const ENOTCONN = 57; + +/// Can't send after socket shutdown +pub const ESHUTDOWN = 58; + +/// Too many references: can't splice +pub const ETOOMANYREFS = 59; + +/// Operation timed out +pub const ETIMEDOUT = 60; + +/// Connection refused +pub const ECONNREFUSED = 61; + +/// Too many levels of symbolic links +pub const ELOOP = 62; + +/// File name too long +pub const ENAMETOOLONG = 63; + +/// Host is down +pub const EHOSTDOWN = 64; + +/// No route to host +pub const EHOSTUNREACH = 65; +/// Directory not empty + +// quotas & mush +pub const ENOTEMPTY = 66; + +/// Too many processes +pub const EPROCLIM = 67; + +/// Too many users +pub const EUSERS = 68; +/// Disc quota exceeded + +// Network File System +pub const EDQUOT = 69; + +/// Stale NFS file handle +pub const ESTALE = 70; + +/// Too many levels of remote in path +pub const EREMOTE = 71; + +/// RPC struct is bad +pub const EBADRPC = 72; + +/// RPC version wrong +pub const ERPCMISMATCH = 73; + +/// RPC prog. not avail +pub const EPROGUNAVAIL = 74; + +/// Program version wrong +pub const EPROGMISMATCH = 75; + +/// Bad procedure for program +pub const EPROCUNAVAIL = 76; + +/// No locks available +pub const ENOLCK = 77; + +/// Function not implemented +pub const ENOSYS = 78; + +/// Inappropriate file type or format +pub const EFTYPE = 79; + +/// Authentication error +pub const EAUTH = 80; +/// Need authenticator + +// Intelligent device errors +pub const ENEEDAUTH = 81; + +/// Device power is off +pub const EPWROFF = 82; + +/// Device error, e.g. paper out +pub const EDEVERR = 83; +/// Value too large to be stored in data type + +// Program loading errors +pub const EOVERFLOW = 84; + +/// Bad executable +pub const EBADEXEC = 85; + +/// Bad CPU type in executable +pub const EBADARCH = 86; + +/// Shared library version mismatch +pub const ESHLIBVERS = 87; + +/// Malformed Macho file +pub const EBADMACHO = 88; + +/// Operation canceled +pub const ECANCELED = 89; + +/// Identifier removed +pub const EIDRM = 90; + +/// No message of desired type +pub const ENOMSG = 91; + +/// Illegal byte sequence +pub const EILSEQ = 92; + +/// Attribute not found +pub const ENOATTR = 93; + +/// Bad message +pub const EBADMSG = 94; + +/// Reserved +pub const EMULTIHOP = 95; + +/// No message available on STREAM +pub const ENODATA = 96; + +/// Reserved +pub const ENOLINK = 97; + +/// No STREAM resources +pub const ENOSR = 98; + +/// Not a STREAM +pub const ENOSTR = 99; + +/// Protocol error +pub const EPROTO = 100; + +/// STREAM ioctl timeout +pub const ETIME = 101; + +/// No such policy registered +pub const ENOPOLICY = 103; + +/// State not recoverable +pub const ENOTRECOVERABLE = 104; + +/// Previous owner died +pub const EOWNERDEAD = 105; + +/// Interface output queue is full +pub const EQFULL = 106; + +/// Must be equal largest errno +pub const ELAST = 106; diff --git a/std/c/posix/freebsd.zig b/std/c/posix/freebsd.zig @@ -0,0 +1,839 @@ +const std = @import("../std.zig"); + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i16, + flags: u16, + fflags: u32, + data: i64, + udata: usize, + // TODO ext +}; + +pub const pthread_attr_t = extern struct { + __size: [56]u8, + __align: c_long, +}; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const Stat = extern struct { + dev: u64, + ino: u64, + nlink: usize, + + mode: u16, + __pad0: u16, + uid: u32, + gid: u32, + __pad1: u32, + rdev: u64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + birthtim: timespec, + + size: i64, + blocks: i64, + blksize: isize, + flags: u32, + gen: u64, + __spare: [10]u64, +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const dirent = extern struct { + d_fileno: usize, + d_off: i64, + d_reclen: u16, + d_type: u8, + d_pad0: u8, + d_namlen: u16, + d_pad1: u16, + d_name: [256]u8, +}; + +pub const in_port_t = u16; +pub const sa_family_t = u16; + +pub const sockaddr = extern union { + in: sockaddr_in, + in6: sockaddr_in6, +}; + +pub const sockaddr_in = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + addr: [16]u8, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, +}; + +pub const CTL_KERN = 1; +pub const CTL_DEBUG = 5; + +pub const KERN_PROC = 14; // struct: process entries +pub const KERN_PROC_PATHNAME = 12; // path to executable + +pub const PATH_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT_NONE = 0; +pub const PROT_READ = 1; +pub const PROT_WRITE = 2; +pub const PROT_EXEC = 4; + +pub const CLOCK_REALTIME = 0; +pub const CLOCK_VIRTUAL = 1; +pub const CLOCK_PROF = 2; +pub const CLOCK_MONOTONIC = 4; +pub const CLOCK_UPTIME = 5; +pub const CLOCK_UPTIME_PRECISE = 7; +pub const CLOCK_UPTIME_FAST = 8; +pub const CLOCK_REALTIME_PRECISE = 9; +pub const CLOCK_REALTIME_FAST = 10; +pub const CLOCK_MONOTONIC_PRECISE = 11; +pub const CLOCK_MONOTONIC_FAST = 12; +pub const CLOCK_SECOND = 13; +pub const CLOCK_THREAD_CPUTIME_ID = 14; +pub const CLOCK_PROCESS_CPUTIME_ID = 15; + +pub const MAP_FAILED = maxInt(usize); +pub const MAP_SHARED = 0x0001; +pub const MAP_PRIVATE = 0x0002; +pub const MAP_FIXED = 0x0010; +pub const MAP_STACK = 0x0400; +pub const MAP_NOSYNC = 0x0800; +pub const MAP_ANON = 0x1000; +pub const MAP_ANONYMOUS = MAP_ANON; +pub const MAP_FILE = 0; +pub const MAP_NORESERVE = 0; + +pub const MAP_GUARD = 0x00002000; +pub const MAP_EXCL = 0x00004000; +pub const MAP_NOCORE = 0x00020000; +pub const MAP_PREFAULT_READ = 0x00040000; +pub const MAP_32BIT = 0x00080000; + +pub const WNOHANG = 1; +pub const WUNTRACED = 2; +pub const WSTOPPED = WUNTRACED; +pub const WCONTINUED = 4; +pub const WNOWAIT = 8; +pub const WEXITED = 16; +pub const WTRAPPED = 32; + +pub const SA_ONSTACK = 0x0001; +pub const SA_RESTART = 0x0002; +pub const SA_RESETHAND = 0x0004; +pub const SA_NOCLDSTOP = 0x0008; +pub const SA_NODEFER = 0x0010; +pub const SA_NOCLDWAIT = 0x0020; +pub const SA_SIGINFO = 0x0040; + +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 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 SIGIO = 23; +pub const SIGXCPU = 24; +pub const SIGXFSZ = 25; +pub const SIGVTALRM = 26; +pub const SIGPROF = 27; +pub const SIGWINCH = 28; +pub const SIGINFO = 29; +pub const SIGUSR1 = 30; +pub const SIGUSR2 = 31; +pub const SIGTHR = 32; +pub const SIGLWP = SIGTHR; +pub const SIGLIBRT = 33; + +pub const SIGRTMIN = 65; +pub const SIGRTMAX = 126; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O_RDONLY = 0x0000; +pub const O_WRONLY = 0x0001; +pub const O_RDWR = 0x0002; +pub const O_ACCMODE = 0x0003; + +pub const O_CREAT = 0x0200; +pub const O_EXCL = 0x0800; +pub const O_NOCTTY = 0x8000; +pub const O_TRUNC = 0x0400; +pub const O_APPEND = 0x0008; +pub const O_NONBLOCK = 0x0004; +pub const O_DSYNC = 0o10000; +pub const O_SYNC = 0x0080; +pub const O_RSYNC = 0o4010000; +pub const O_DIRECTORY = 0o200000; +pub const O_NOFOLLOW = 0x0100; +pub const O_CLOEXEC = 0x00100000; + +pub const O_ASYNC = 0x0040; +pub const O_DIRECT = 0x00010000; +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; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + +pub const SIG_BLOCK = 1; +pub const SIG_UNBLOCK = 2; +pub const SIG_SETMASK = 3; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 2; +pub const SOCK_RAW = 3; +pub const SOCK_RDM = 4; +pub const SOCK_SEQPACKET = 5; + +pub const SOCK_CLOEXEC = 0x10000000; +pub const SOCK_NONBLOCK = 0x20000000; + +pub const PROTO_ip = 0o000; +pub const PROTO_icmp = 0o001; +pub const PROTO_igmp = 0o002; +pub const PROTO_ggp = 0o003; +pub const PROTO_ipencap = 0o004; +pub const PROTO_st = 0o005; +pub const PROTO_tcp = 0o006; +pub const PROTO_egp = 0o010; +pub const PROTO_pup = 0o014; +pub const PROTO_udp = 0o021; +pub const PROTO_hmp = 0o024; +pub const PROTO_xns_idp = 0o026; +pub const PROTO_rdp = 0o033; +pub const PROTO_iso_tp4 = 0o035; +pub const PROTO_xtp = 0o044; +pub const PROTO_ddp = 0o045; +pub const PROTO_idpr_cmtp = 0o046; +pub const PROTO_ipv6 = 0o051; +pub const PROTO_ipv6_route = 0o053; +pub const PROTO_ipv6_frag = 0o054; +pub const PROTO_idrp = 0o055; +pub const PROTO_rsvp = 0o056; +pub const PROTO_gre = 0o057; +pub const PROTO_esp = 0o062; +pub const PROTO_ah = 0o063; +pub const PROTO_skip = 0o071; +pub const PROTO_ipv6_icmp = 0o072; +pub const PROTO_ipv6_nonxt = 0o073; +pub const PROTO_ipv6_opts = 0o074; +pub const PROTO_rspf = 0o111; +pub const PROTO_vmtp = 0o121; +pub const PROTO_ospf = 0o131; +pub const PROTO_ipip = 0o136; +pub const PROTO_encap = 0o142; +pub const PROTO_pim = 0o147; +pub const PROTO_raw = 0o377; + +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_MAX = 41; + +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_MAX = PF_MAX; + +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; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; + +/// attached to aio requests +pub const EVFILT_AIO = -3; + +/// attached to vnodes +pub const EVFILT_VNODE = -4; + +/// attached to struct proc +pub const EVFILT_PROC = -5; + +/// attached to struct proc +pub const EVFILT_SIGNAL = -6; + +/// timers +pub const EVFILT_TIMER = -7; + +/// Process descriptors +pub const EVFILT_PROCDESC = -8; + +/// Filesystem events +pub const EVFILT_FS = -9; + +pub const EVFILT_LIO = -10; + +/// User events +pub const EVFILT_USER = -11; + +/// Sendfile events +pub const EVFILT_SENDFILE = -12; + +pub const EVFILT_EMPTY = -13; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x01000000; + +/// ignore input fflags +pub const NOTE_FFNOP = 0x00000000; + +/// and fflags +pub const NOTE_FFAND = 0x40000000; + +/// or fflags +pub const NOTE_FFOR = 0x80000000; + +/// copy fflags +pub const NOTE_FFCOPY = 0xc0000000; + +/// mask for operations +pub const NOTE_FFCTRLMASK = 0xc0000000; +pub const NOTE_FFLAGSMASK = 0x00ffffff; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// behave like poll() +pub const NOTE_FILE_POLL = 0x00000002; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// vnode was opened +pub const NOTE_OPEN = 0x00000080; + +/// file closed, fd did not allow write +pub const NOTE_CLOSE = 0x00000100; + +/// file closed, fd did allow write +pub const NOTE_CLOSE_WRITE = 0x00000200; + +/// file was read +pub const NOTE_READ = 0x00000400; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); + +/// data is seconds +pub const NOTE_SECONDS = 0x00000001; + +/// data is milliseconds +pub const NOTE_MSECONDS = 0x00000002; + +/// data is microseconds +pub const NOTE_USECONDS = 0x00000004; + +/// data is nanoseconds +pub const NOTE_NSECONDS = 0x00000008; + +/// timeout is absolute +pub const NOTE_ABSTIME = 0x00000010; + +pub const TCGETS = 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 = 0x5411; +pub const TIOCSTI = 0x5412; +pub const TIOCGWINSZ = 0x5413; +pub const TIOCSWINSZ = 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 = 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; + +fn unsigned(s: i32) u32 { + return @bitCast(u32, s); +} +fn signed(s: u32) i32 { + return @bitCast(i32, s); +} +pub fn WEXITSTATUS(s: i32) i32 { + return signed((unsigned(s) & 0xff00) >> 8); +} +pub fn WTERMSIG(s: i32) i32 { + return signed(unsigned(s) & 0x7f); +} +pub fn WSTOPSIG(s: i32) i32 { + return WEXITSTATUS(s); +} +pub fn WIFEXITED(s: i32) bool { + return WTERMSIG(s) == 0; +} +pub fn WIFSTOPPED(s: i32) bool { + return @intCast(u16, (((unsigned(s) & 0xffff) *% 0x10001) >> 8)) > 0x7f00; +} +pub fn WIFSIGNALED(s: i32) bool { + return (unsigned(s) & 0xffff) -% 1 < 0xff; +} + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 32; + +pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); +pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); +pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + /// signal handler + __sigaction_u: extern union { + __sa_handler: extern fn (i32) void, + __sa_sigaction: extern fn (i32, *__siginfo, usize) void, + }, + + /// see signal options + sa_flags: u32, + + /// signal mask to apply + sa_mask: sigset_t, +}; + +pub const _SIG_WORDS = 4; +pub const _SIG_MAXSIG = 128; + +pub inline fn _SIG_IDX(sig: usize) usize { + return sig - 1; +} +pub inline fn _SIG_WORD(sig: usize) usize { + return_SIG_IDX(sig) >> 5; +} +pub inline fn _SIG_BIT(sig: usize) usize { + return 1 << (_SIG_IDX(sig) & 31); +} +pub inline fn _SIG_VALID(sig: usize) usize { + return sig <= _SIG_MAXSIG and sig > 0; +} + +pub const sigset_t = extern struct { + __bits: [_SIG_WORDS]u32, +}; + +pub const EPERM = 1; // Operation not permitted +pub const ENOENT = 2; // No such file or directory +pub const ESRCH = 3; // No such process +pub const EINTR = 4; // Interrupted system call +pub const EIO = 5; // Input/output error +pub const ENXIO = 6; // Device not configured +pub const E2BIG = 7; // Argument list too long +pub const ENOEXEC = 8; // Exec format error +pub const EBADF = 9; // Bad file descriptor +pub const ECHILD = 10; // No child processes +pub const EDEADLK = 11; // Resource deadlock avoided +// 11 was EAGAIN +pub const ENOMEM = 12; // Cannot allocate memory +pub const EACCES = 13; // Permission denied +pub const EFAULT = 14; // Bad address +pub const ENOTBLK = 15; // Block device required +pub const EBUSY = 16; // Device busy +pub const EEXIST = 17; // File exists +pub const EXDEV = 18; // Cross-device link +pub const ENODEV = 19; // Operation not supported by device +pub const ENOTDIR = 20; // Not a directory +pub const EISDIR = 21; // Is a directory +pub const EINVAL = 22; // Invalid argument +pub const ENFILE = 23; // Too many open files in system +pub const EMFILE = 24; // Too many open files +pub const ENOTTY = 25; // Inappropriate ioctl for device +pub const ETXTBSY = 26; // Text file busy +pub const EFBIG = 27; // File too large +pub const ENOSPC = 28; // No space left on device +pub const ESPIPE = 29; // Illegal seek +pub const EROFS = 30; // Read-only filesystem +pub const EMLINK = 31; // Too many links +pub const EPIPE = 32; // Broken pipe + +// math software +pub const EDOM = 33; // Numerical argument out of domain +pub const ERANGE = 34; // Result too large + +// non-blocking and interrupt i/o +pub const EAGAIN = 35; // Resource temporarily unavailable +pub const EWOULDBLOCK = EAGAIN; // Operation would block +pub const EINPROGRESS = 36; // Operation now in progress +pub const EALREADY = 37; // Operation already in progress + +// ipc/network software -- argument errors +pub const ENOTSOCK = 38; // Socket operation on non-socket +pub const EDESTADDRREQ = 39; // Destination address required +pub const EMSGSIZE = 40; // Message too long +pub const EPROTOTYPE = 41; // Protocol wrong type for socket +pub const ENOPROTOOPT = 42; // Protocol not available +pub const EPROTONOSUPPORT = 43; // Protocol not supported +pub const ESOCKTNOSUPPORT = 44; // Socket type not supported +pub const EOPNOTSUPP = 45; // Operation not supported +pub const ENOTSUP = EOPNOTSUPP; // Operation not supported +pub const EPFNOSUPPORT = 46; // Protocol family not supported +pub const EAFNOSUPPORT = 47; // Address family not supported by protocol family +pub const EADDRINUSE = 48; // Address already in use +pub const EADDRNOTAVAIL = 49; // Can't assign requested address + +// ipc/network software -- operational errors +pub const ENETDOWN = 50; // Network is down +pub const ENETUNREACH = 51; // Network is unreachable +pub const ENETRESET = 52; // Network dropped connection on reset +pub const ECONNABORTED = 53; // Software caused connection abort +pub const ECONNRESET = 54; // Connection reset by peer +pub const ENOBUFS = 55; // No buffer space available +pub const EISCONN = 56; // Socket is already connected +pub const ENOTCONN = 57; // Socket is not connected +pub const ESHUTDOWN = 58; // Can't send after socket shutdown +pub const ETOOMANYREFS = 59; // Too many references: can't splice +pub const ETIMEDOUT = 60; // Operation timed out +pub const ECONNREFUSED = 61; // Connection refused + +pub const ELOOP = 62; // Too many levels of symbolic links +pub const ENAMETOOLONG = 63; // File name too long + +// should be rearranged +pub const EHOSTDOWN = 64; // Host is down +pub const EHOSTUNREACH = 65; // No route to host +pub const ENOTEMPTY = 66; // Directory not empty + +// quotas & mush +pub const EPROCLIM = 67; // Too many processes +pub const EUSERS = 68; // Too many users +pub const EDQUOT = 69; // Disc quota exceeded + +// Network File System +pub const ESTALE = 70; // Stale NFS file handle +pub const EREMOTE = 71; // Too many levels of remote in path +pub const EBADRPC = 72; // RPC struct is bad +pub const ERPCMISMATCH = 73; // RPC version wrong +pub const EPROGUNAVAIL = 74; // RPC prog. not avail +pub const EPROGMISMATCH = 75; // Program version wrong +pub const EPROCUNAVAIL = 76; // Bad procedure for program + +pub const ENOLCK = 77; // No locks available +pub const ENOSYS = 78; // Function not implemented + +pub const EFTYPE = 79; // Inappropriate file type or format +pub const EAUTH = 80; // Authentication error +pub const ENEEDAUTH = 81; // Need authenticator +pub const EIDRM = 82; // Identifier removed +pub const ENOMSG = 83; // No message of desired type +pub const EOVERFLOW = 84; // Value too large to be stored in data type +pub const ECANCELED = 85; // Operation canceled +pub const EILSEQ = 86; // Illegal byte sequence +pub const ENOATTR = 87; // Attribute not found + +pub const EDOOFUS = 88; // Programming error + +pub const EBADMSG = 89; // Bad message +pub const EMULTIHOP = 90; // Multihop attempted +pub const ENOLINK = 91; // Link has been severed +pub const EPROTO = 92; // Protocol error + +pub const ENOTCAPABLE = 93; // Capabilities insufficient +pub const ECAPMODE = 94; // Not permitted in capability mode +pub const ENOTRECOVERABLE = 95; // State not recoverable +pub const EOWNERDEAD = 96; // Previous owner died + +pub const ELAST = 96; // Must be equal largest errno diff --git a/std/c/posix/netbsd.zig b/std/c/posix/netbsd.zig @@ -0,0 +1,728 @@ +const std = @import("../../std.zig"); +const maxInt = std.math.maxInt; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i32, + flags: u32, + fflags: u32, + data: i64, + udata: usize, +}; + +pub const pthread_attr_t = extern struct { + pta_magic: u32, + pta_flags: c_int, + pta_private: *c_void, +}; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const Stat = extern struct { + dev: u64, + mode: u32, + ino: u64, + nlink: usize, + + uid: u32, + gid: u32, + rdev: u64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + birthtim: timespec, + + size: i64, + blocks: i64, + blksize: isize, + flags: u32, + gen: u32, + __spare: [2]u32, +}; + +pub const timespec = extern struct { + tv_sec: i64, + tv_nsec: isize, +}; + +pub const dirent = extern struct { + d_fileno: u64, + d_reclen: u16, + d_namlen: u16, + d_type: u8, + d_off: i64, + d_name: [512]u8, +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; + +pub const sockaddr = extern union { + in: sockaddr_in, + in6: sockaddr_in6, +}; + +pub const sockaddr_in = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + addr: u32, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + len: u8, + family: sa_family_t, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, +}; + +pub const CTL_KERN = 1; +pub const CTL_DEBUG = 5; + +pub const KERN_PROC_ARGS = 48; // struct: process argv/env +pub const KERN_PROC_PATHNAME = 5; // path to executable + +pub const PATH_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT_NONE = 0; +pub const PROT_READ = 1; +pub const PROT_WRITE = 2; +pub const PROT_EXEC = 4; + +pub const CLOCK_REALTIME = 0; +pub const CLOCK_VIRTUAL = 1; +pub const CLOCK_PROF = 2; +pub const CLOCK_MONOTONIC = 3; +pub const CLOCK_THREAD_CPUTIME_ID = 0x20000000; +pub const CLOCK_PROCESS_CPUTIME_ID = 0x40000000; + +pub const MAP_FAILED = maxInt(usize); +pub const MAP_SHARED = 0x0001; +pub const MAP_PRIVATE = 0x0002; +pub const MAP_REMAPDUP = 0x0004; +pub const MAP_FIXED = 0x0010; +pub const MAP_RENAME = 0x0020; +pub const MAP_NORESERVE = 0x0040; +pub const MAP_INHERIT = 0x0080; +pub const MAP_HASSEMAPHORE = 0x0200; +pub const MAP_TRYFIXED = 0x0400; +pub const MAP_WIRED = 0x0800; + +pub const MAP_FILE = 0x0000; +pub const MAP_NOSYNC = 0x0800; +pub const MAP_ANON = 0x1000; +pub const MAP_ANONYMOUS = MAP_ANON; +pub const MAP_STACK = 0x2000; + +pub const WNOHANG = 0x00000001; +pub const WUNTRACED = 0x00000002; +pub const WSTOPPED = WUNTRACED; +pub const WCONTINUED = 0x00000010; +pub const WNOWAIT = 0x00010000; +pub const WEXITED = 0x00000020; +pub const WTRAPPED = 0x00000040; + +pub const SA_ONSTACK = 0x0001; +pub const SA_RESTART = 0x0002; +pub const SA_RESETHAND = 0x0004; +pub const SA_NOCLDSTOP = 0x0008; +pub const SA_NODEFER = 0x0010; +pub const SA_NOCLDWAIT = 0x0020; +pub const SA_SIGINFO = 0x0040; + +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 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 SIGIO = 23; +pub const SIGXCPU = 24; +pub const SIGXFSZ = 25; +pub const SIGVTALRM = 26; +pub const SIGPROF = 27; +pub const SIGWINCH = 28; +pub const SIGINFO = 29; +pub const SIGUSR1 = 30; +pub const SIGUSR2 = 31; +pub const SIGPWR = 32; + +pub const SIGRTMIN = 33; +pub const SIGRTMAX = 63; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O_RDONLY = 0x0000; +pub const O_WRONLY = 0x0001; +pub const O_RDWR = 0x0002; +pub const O_ACCMODE = 0x0003; + +pub const O_CREAT = 0x0200; +pub const O_EXCL = 0x0800; +pub const O_NOCTTY = 0x8000; +pub const O_TRUNC = 0x0400; +pub const O_APPEND = 0x0008; +pub const O_NONBLOCK = 0x0004; +pub const O_DSYNC = 0x00010000; +pub const O_SYNC = 0x0080; +pub const O_RSYNC = 0x00020000; +pub const O_DIRECTORY = 0x00080000; +pub const O_NOFOLLOW = 0x00000100; +pub const O_CLOEXEC = 0x00400000; + +pub const O_ASYNC = 0x0040; +pub const O_DIRECT = 0x00080000; +pub const O_LARGEFILE = 0; +pub const O_NOATIME = 0; +pub const O_PATH = 0; +pub const O_TMPFILE = 0; +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_GETOWN = 5; +pub const F_SETOWN = 6; + +pub const F_GETLK = 7; +pub const F_SETLK = 8; +pub const F_SETLKW = 9; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + +pub const SIG_BLOCK = 1; +pub const SIG_UNBLOCK = 2; +pub const SIG_SETMASK = 3; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 2; +pub const SOCK_RAW = 3; +pub const SOCK_RDM = 4; +pub const SOCK_SEQPACKET = 5; + +pub const SOCK_CLOEXEC = 0x10000000; +pub const SOCK_NONBLOCK = 0x20000000; + +pub const PROTO_ip = 0; +pub const PROTO_icmp = 1; +pub const PROTO_igmp = 2; +pub const PROTO_ggp = 3; +pub const PROTO_ipencap = 4; +pub const PROTO_tcp = 6; +pub const PROTO_egp = 8; +pub const PROTO_pup = 12; +pub const PROTO_udp = 17; +pub const PROTO_xns_idp = 22; +pub const PROTO_iso_tp4 = 29; +pub const PROTO_ipv6 = 41; +pub const PROTO_ipv6_route = 43; +pub const PROTO_ipv6_frag = 44; +pub const PROTO_rsvp = 46; +pub const PROTO_gre = 47; +pub const PROTO_esp = 50; +pub const PROTO_ah = 51; +pub const PROTO_ipv6_icmp = 58; +pub const PROTO_ipv6_nonxt = 59; +pub const PROTO_ipv6_opts = 60; +pub const PROTO_encap = 98; +pub const PROTO_pim = 103; +pub const PROTO_raw = 255; + +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_APPLETALK = 16; +pub const PF_INET6 = 24; +pub const PF_DECnet = 12; +pub const PF_KEY = 29; +pub const PF_ROUTE = 34; +pub const PF_SNA = 11; +pub const PF_MPLS = 33; +pub const PF_CAN = 35; +pub const PF_BLUETOOTH = 31; +pub const PF_ISDN = 26; +pub const PF_MAX = 37; + +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_APPLETALK = PF_APPLETALK; +pub const AF_INET6 = PF_INET6; +pub const AF_KEY = PF_KEY; +pub const AF_ROUTE = PF_ROUTE; +pub const AF_SNA = PF_SNA; +pub const AF_MPLS = PF_MPLS; +pub const AF_CAN = PF_CAN; +pub const AF_BLUETOOTH = PF_BLUETOOTH; +pub const AF_ISDN = PF_ISDN; +pub const AF_MAX = PF_MAX; + +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; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +pub const EVFILT_READ = 0; +pub const EVFILT_WRITE = 1; + +/// attached to aio requests +pub const EVFILT_AIO = 2; + +/// attached to vnodes +pub const EVFILT_VNODE = 3; + +/// attached to struct proc +pub const EVFILT_PROC = 4; + +/// attached to struct proc +pub const EVFILT_SIGNAL = 5; + +/// timers +pub const EVFILT_TIMER = 6; + +/// Filesystem events +pub const EVFILT_FS = 7; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x08000000; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = 0xf0000000; + +pub const TIOCCBRK = 0x2000747a; +pub const TIOCCDTR = 0x20007478; +pub const TIOCCONS = 0x80047462; +pub const TIOCDCDTIMESTAMP = 0x40107458; +pub const TIOCDRAIN = 0x2000745e; +pub const TIOCEXCL = 0x2000740d; +pub const TIOCEXT = 0x80047460; +pub const TIOCFLAG_CDTRCTS = 0x10; +pub const TIOCFLAG_CLOCAL = 0x2; +pub const TIOCFLAG_CRTSCTS = 0x4; +pub const TIOCFLAG_MDMBUF = 0x8; +pub const TIOCFLAG_SOFTCAR = 0x1; +pub const TIOCFLUSH = 0x80047410; +pub const TIOCGETA = 0x402c7413; +pub const TIOCGETD = 0x4004741a; +pub const TIOCGFLAGS = 0x4004745d; +pub const TIOCGLINED = 0x40207442; +pub const TIOCGPGRP = 0x40047477; +pub const TIOCGQSIZE = 0x40047481; +pub const TIOCGRANTPT = 0x20007447; +pub const TIOCGSID = 0x40047463; +pub const TIOCGSIZE = 0x40087468; +pub const TIOCGWINSZ = 0x40087468; +pub const TIOCMBIC = 0x8004746b; +pub const TIOCMBIS = 0x8004746c; +pub const TIOCMGET = 0x4004746a; +pub const TIOCMSET = 0x8004746d; +pub const TIOCM_CAR = 0x40; +pub const TIOCM_CD = 0x40; +pub const TIOCM_CTS = 0x20; +pub const TIOCM_DSR = 0x100; +pub const TIOCM_DTR = 0x2; +pub const TIOCM_LE = 0x1; +pub const TIOCM_RI = 0x80; +pub const TIOCM_RNG = 0x80; +pub const TIOCM_RTS = 0x4; +pub const TIOCM_SR = 0x10; +pub const TIOCM_ST = 0x8; +pub const TIOCNOTTY = 0x20007471; +pub const TIOCNXCL = 0x2000740e; +pub const TIOCOUTQ = 0x40047473; +pub const TIOCPKT = 0x80047470; +pub const TIOCPKT_DATA = 0x0; +pub const TIOCPKT_DOSTOP = 0x20; +pub const TIOCPKT_FLUSHREAD = 0x1; +pub const TIOCPKT_FLUSHWRITE = 0x2; +pub const TIOCPKT_IOCTL = 0x40; +pub const TIOCPKT_NOSTOP = 0x10; +pub const TIOCPKT_START = 0x8; +pub const TIOCPKT_STOP = 0x4; +pub const TIOCPTMGET = 0x40287446; +pub const TIOCPTSNAME = 0x40287448; +pub const TIOCRCVFRAME = 0x80087445; +pub const TIOCREMOTE = 0x80047469; +pub const TIOCSBRK = 0x2000747b; +pub const TIOCSCTTY = 0x20007461; +pub const TIOCSDTR = 0x20007479; +pub const TIOCSETA = 0x802c7414; +pub const TIOCSETAF = 0x802c7416; +pub const TIOCSETAW = 0x802c7415; +pub const TIOCSETD = 0x8004741b; +pub const TIOCSFLAGS = 0x8004745c; +pub const TIOCSIG = 0x2000745f; +pub const TIOCSLINED = 0x80207443; +pub const TIOCSPGRP = 0x80047476; +pub const TIOCSQSIZE = 0x80047480; +pub const TIOCSSIZE = 0x80087467; +pub const TIOCSTART = 0x2000746e; +pub const TIOCSTAT = 0x80047465; +pub const TIOCSTI = 0x80017472; +pub const TIOCSTOP = 0x2000746f; +pub const TIOCSWINSZ = 0x80087467; +pub const TIOCUCNTL = 0x80047466; +pub const TIOCXMTFRAME = 0x80087444; + +fn unsigned(s: i32) u32 { + return @bitCast(u32, s); +} +fn signed(s: u32) i32 { + return @bitCast(i32, s); +} +pub fn WEXITSTATUS(s: i32) i32 { + return signed((unsigned(s) >> 8) & 0xff); +} +pub fn WTERMSIG(s: i32) i32 { + return signed(unsigned(s) & 0x7f); +} +pub fn WSTOPSIG(s: i32) i32 { + return WEXITSTATUS(s); +} +pub fn WIFEXITED(s: i32) bool { + return WTERMSIG(s) == 0; +} + +pub fn WIFCONTINUED(s: i32) bool { + return ((s & 0x7f) == 0xffff); +} + +pub fn WIFSTOPPED(s: i32) bool { + return ((s & 0x7f != 0x7f) and !WIFCONTINUED(s)); +} + +pub fn WIFSIGNALED(s: i32) bool { + return !WIFSTOPPED(s) and !WIFCONTINUED(s) and !WIFEXITED(s); +} + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 32; + +pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); +pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); +pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + /// signal handler + __sigaction_u: extern union { + __sa_handler: extern fn (i32) void, + __sa_sigaction: extern fn (i32, *__siginfo, usize) void, + }, + + /// see signal options + sa_flags: u32, + + /// signal mask to apply + sa_mask: sigset_t, +}; + +pub const _SIG_WORDS = 4; +pub const _SIG_MAXSIG = 128; + +pub inline fn _SIG_IDX(sig: usize) usize { + return sig - 1; +} +pub inline fn _SIG_WORD(sig: usize) usize { + return_SIG_IDX(sig) >> 5; +} +pub inline fn _SIG_BIT(sig: usize) usize { + return 1 << (_SIG_IDX(sig) & 31); +} +pub inline fn _SIG_VALID(sig: usize) usize { + return sig <= _SIG_MAXSIG and sig > 0; +} + +pub const sigset_t = extern struct { + __bits: [_SIG_WORDS]u32, +}; + +pub const EPERM = 1; // Operation not permitted +pub const ENOENT = 2; // No such file or directory +pub const ESRCH = 3; // No such process +pub const EINTR = 4; // Interrupted system call +pub const EIO = 5; // Input/output error +pub const ENXIO = 6; // Device not configured +pub const E2BIG = 7; // Argument list too long +pub const ENOEXEC = 8; // Exec format error +pub const EBADF = 9; // Bad file descriptor +pub const ECHILD = 10; // No child processes +pub const EDEADLK = 11; // Resource deadlock avoided +// 11 was EAGAIN +pub const ENOMEM = 12; // Cannot allocate memory +pub const EACCES = 13; // Permission denied +pub const EFAULT = 14; // Bad address +pub const ENOTBLK = 15; // Block device required +pub const EBUSY = 16; // Device busy +pub const EEXIST = 17; // File exists +pub const EXDEV = 18; // Cross-device link +pub const ENODEV = 19; // Operation not supported by device +pub const ENOTDIR = 20; // Not a directory +pub const EISDIR = 21; // Is a directory +pub const EINVAL = 22; // Invalid argument +pub const ENFILE = 23; // Too many open files in system +pub const EMFILE = 24; // Too many open files +pub const ENOTTY = 25; // Inappropriate ioctl for device +pub const ETXTBSY = 26; // Text file busy +pub const EFBIG = 27; // File too large +pub const ENOSPC = 28; // No space left on device +pub const ESPIPE = 29; // Illegal seek +pub const EROFS = 30; // Read-only file system +pub const EMLINK = 31; // Too many links +pub const EPIPE = 32; // Broken pipe + +// math software +pub const EDOM = 33; // Numerical argument out of domain +pub const ERANGE = 34; // Result too large or too small + +// non-blocking and interrupt i/o +pub const EAGAIN = 35; // Resource temporarily unavailable +pub const EWOULDBLOCK = EAGAIN; // Operation would block +pub const EINPROGRESS = 36; // Operation now in progress +pub const EALREADY = 37; // Operation already in progress + +// ipc/network software -- argument errors +pub const ENOTSOCK = 38; // Socket operation on non-socket +pub const EDESTADDRREQ = 39; // Destination address required +pub const EMSGSIZE = 40; // Message too long +pub const EPROTOTYPE = 41; // Protocol wrong type for socket +pub const ENOPROTOOPT = 42; // Protocol option not available +pub const EPROTONOSUPPORT = 43; // Protocol not supported +pub const ESOCKTNOSUPPORT = 44; // Socket type not supported +pub const EOPNOTSUPP = 45; // Operation not supported +pub const EPFNOSUPPORT = 46; // Protocol family not supported +pub const EAFNOSUPPORT = 47; // Address family not supported by protocol family +pub const EADDRINUSE = 48; // Address already in use +pub const EADDRNOTAVAIL = 49; // Can't assign requested address + +// ipc/network software -- operational errors +pub const ENETDOWN = 50; // Network is down +pub const ENETUNREACH = 51; // Network is unreachable +pub const ENETRESET = 52; // Network dropped connection on reset +pub const ECONNABORTED = 53; // Software caused connection abort +pub const ECONNRESET = 54; // Connection reset by peer +pub const ENOBUFS = 55; // No buffer space available +pub const EISCONN = 56; // Socket is already connected +pub const ENOTCONN = 57; // Socket is not connected +pub const ESHUTDOWN = 58; // Can't send after socket shutdown +pub const ETOOMANYREFS = 59; // Too many references: can't splice +pub const ETIMEDOUT = 60; // Operation timed out +pub const ECONNREFUSED = 61; // Connection refused + +pub const ELOOP = 62; // Too many levels of symbolic links +pub const ENAMETOOLONG = 63; // File name too long + +// should be rearranged +pub const EHOSTDOWN = 64; // Host is down +pub const EHOSTUNREACH = 65; // No route to host +pub const ENOTEMPTY = 66; // Directory not empty + +// quotas & mush +pub const EPROCLIM = 67; // Too many processes +pub const EUSERS = 68; // Too many users +pub const EDQUOT = 69; // Disc quota exceeded + +// Network File System +pub const ESTALE = 70; // Stale NFS file handle +pub const EREMOTE = 71; // Too many levels of remote in path +pub const EBADRPC = 72; // RPC struct is bad +pub const ERPCMISMATCH = 73; // RPC version wrong +pub const EPROGUNAVAIL = 74; // RPC prog. not avail +pub const EPROGMISMATCH = 75; // Program version wrong +pub const EPROCUNAVAIL = 76; // Bad procedure for program + +pub const ENOLCK = 77; // No locks available +pub const ENOSYS = 78; // Function not implemented + +pub const EFTYPE = 79; // Inappropriate file type or format +pub const EAUTH = 80; // Authentication error +pub const ENEEDAUTH = 81; // Need authenticator + +// SystemV IPC +pub const EIDRM = 82; // Identifier removed +pub const ENOMSG = 83; // No message of desired type +pub const EOVERFLOW = 84; // Value too large to be stored in data type + +// Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 +pub const EILSEQ = 85; // Illegal byte sequence + +// From IEEE Std 1003.1-2001 +// Base, Realtime, Threads or Thread Priority Scheduling option errors +pub const ENOTSUP = 86; // Not supported + +// Realtime option errors +pub const ECANCELED = 87; // Operation canceled + +// Realtime, XSI STREAMS option errors +pub const EBADMSG = 88; // Bad or Corrupt message + +// XSI STREAMS option errors +pub const ENODATA = 89; // No message available +pub const ENOSR = 90; // No STREAM resources +pub const ENOSTR = 91; // Not a STREAM +pub const ETIME = 92; // STREAM ioctl timeout + +// File system extended attribute errors +pub const ENOATTR = 93; // Attribute not found + +// Realtime, XSI STREAMS option errors +pub const EMULTIHOP = 94; // Multihop attempted +pub const ENOLINK = 95; // Link has been severed +pub const EPROTO = 96; // Protocol error + +pub const ELAST = 96; // Must equal largest errno diff --git a/std/c/posix/windows.zig b/std/c/posix/windows.zig @@ -0,0 +1,86 @@ +pub const EPERM = 1; +pub const ENOENT = 2; +pub const ESRCH = 3; +pub const EINTR = 4; +pub const EIO = 5; +pub const ENXIO = 6; +pub const E2BIG = 7; +pub const ENOEXEC = 8; +pub const EBADF = 9; +pub const ECHILD = 10; +pub const EAGAIN = 11; +pub const ENOMEM = 12; +pub const EACCES = 13; +pub const EFAULT = 14; +pub const EBUSY = 16; +pub const EEXIST = 17; +pub const EXDEV = 18; +pub const ENODEV = 19; +pub const ENOTDIR = 20; +pub const EISDIR = 21; +pub const ENFILE = 23; +pub const EMFILE = 24; +pub const ENOTTY = 25; +pub const EFBIG = 27; +pub const ENOSPC = 28; +pub const ESPIPE = 29; +pub const EROFS = 30; +pub const EMLINK = 31; +pub const EPIPE = 32; +pub const EDOM = 33; +pub const EDEADLK = 36; +pub const ENAMETOOLONG = 38; +pub const ENOLCK = 39; +pub const ENOSYS = 40; +pub const ENOTEMPTY = 41; + +pub const EINVAL = 22; +pub const ERANGE = 34; +pub const EILSEQ = 42; +pub const STRUNCATE = 80; + +// Support EDEADLOCK for compatibility with older Microsoft C versions +pub const EDEADLOCK = EDEADLK; + +// POSIX Supplement +pub const EADDRINUSE = 100; +pub const EADDRNOTAVAIL = 101; +pub const EAFNOSUPPORT = 102; +pub const EALREADY = 103; +pub const EBADMSG = 104; +pub const ECANCELED = 105; +pub const ECONNABORTED = 106; +pub const ECONNREFUSED = 107; +pub const ECONNRESET = 108; +pub const EDESTADDRREQ = 109; +pub const EHOSTUNREACH = 110; +pub const EIDRM = 111; +pub const EINPROGRESS = 112; +pub const EISCONN = 113; +pub const ELOOP = 114; +pub const EMSGSIZE = 115; +pub const ENETDOWN = 116; +pub const ENETRESET = 117; +pub const ENETUNREACH = 118; +pub const ENOBUFS = 119; +pub const ENODATA = 120; +pub const ENOLINK = 121; +pub const ENOMSG = 122; +pub const ENOPROTOOPT = 123; +pub const ENOSR = 124; +pub const ENOSTR = 125; +pub const ENOTCONN = 126; +pub const ENOTRECOVERABLE = 127; +pub const ENOTSOCK = 128; +pub const ENOTSUP = 129; +pub const EOPNOTSUPP = 130; +pub const EOTHER = 131; +pub const EOVERFLOW = 132; +pub const EOWNERDEAD = 133; +pub const EPROTO = 134; +pub const EPROTONOSUPPORT = 135; +pub const EPROTOTYPE = 136; +pub const ETIME = 137; +pub const ETIMEDOUT = 138; +pub const ETXTBSY = 139; +pub const EWOULDBLOCK = 140; diff --git a/std/event/fs.zig b/std/event/fs.zig @@ -8,6 +8,7 @@ const mem = std.mem; const posix = os.posix; const windows = os.windows; const Loop = event.Loop; +const fd_t = posix.fd_t; pub const RequestNode = std.atomic.Queue(Request).Node; @@ -30,7 +31,7 @@ pub const Request = struct { End, // special - means the fs thread should exit pub const PWriteV = struct { - fd: os.FileHandle, + fd: fd_t, iov: []const os.posix.iovec_const, offset: usize, result: Error!void, @@ -39,7 +40,7 @@ pub const Request = struct { }; pub const PReadV = struct { - fd: os.FileHandle, + fd: fd_t, iov: []const os.posix.iovec, offset: usize, result: Error!usize, @@ -52,7 +53,7 @@ pub const Request = struct { path: []const u8, flags: u32, mode: os.File.Mode, - result: Error!os.FileHandle, + result: Error!fd_t, pub const Error = os.File.OpenError; }; @@ -68,7 +69,7 @@ pub const Request = struct { }; pub const Close = struct { - fd: os.FileHandle, + fd: fd_t, }; }; }; @@ -76,7 +77,7 @@ pub const Request = struct { pub const PWriteVError = error{OutOfMemory} || os.File.WriteError; /// data - just the inner references - must live until pwritev promise completes. -pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, offset: usize) PWriteVError!void { +pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) PWriteVError!void { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -109,7 +110,7 @@ pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, o } /// data must outlive the returned promise -pub async fn pwritevWindows(loop: *Loop, fd: os.FileHandle, data: []const []const u8, offset: usize) os.WindowsWriteError!void { +pub async fn pwritevWindows(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) os.WindowsWriteError!void { if (data.len == 0) return; if (data.len == 1) return await (async pwriteWindows(loop, fd, data[0], offset) catch unreachable); @@ -121,7 +122,7 @@ pub async fn pwritevWindows(loop: *Loop, fd: os.FileHandle, data: []const []cons } } -pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, offset: u64) os.WindowsWriteError!void { +pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) os.WindowsWriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -169,7 +170,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off /// iovecs must live until pwritev promise completes. pub async fn pwritevPosix( loop: *Loop, - fd: os.FileHandle, + fd: fd_t, iovecs: []const posix.iovec_const, offset: usize, ) os.PosixWriteError!void { @@ -212,7 +213,7 @@ pub async fn pwritevPosix( pub const PReadVError = error{OutOfMemory} || os.File.ReadError; /// data - just the inner references - must live until preadv promise completes. -pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: usize) PReadVError!usize { +pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PReadVError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -247,7 +248,7 @@ pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: } /// data must outlive the returned promise -pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: u64) os.WindowsReadError!usize { +pub async fn preadvWindows(loop: *Loop, fd: fd_t, data: []const []u8, offset: u64) os.WindowsReadError!usize { assert(data.len != 0); if (data.len == 1) return await (async preadWindows(loop, fd, data[0], offset) catch unreachable); @@ -271,7 +272,7 @@ pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, o } } -pub async fn preadWindows(loop: *Loop, fd: os.FileHandle, data: []u8, offset: u64) !usize { +pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -318,7 +319,7 @@ pub async fn preadWindows(loop: *Loop, fd: os.FileHandle, data: []u8, offset: u6 /// iovecs must live until preadv promise completes pub async fn preadvPosix( loop: *Loop, - fd: os.FileHandle, + fd: fd_t, iovecs: []const posix.iovec, offset: usize, ) os.PosixReadError!usize { @@ -363,7 +364,7 @@ pub async fn openPosix( path: []const u8, flags: u32, mode: os.File.Mode, -) os.File.OpenError!os.FileHandle { +) os.File.OpenError!fd_t { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -402,7 +403,7 @@ pub async fn openPosix( return req_node.data.msg.Open.result; } -pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHandle { +pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; @@ -423,12 +424,12 @@ pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHa /// Creates if does not exist. Truncates the file if it exists. /// Uses the default mode. -pub async fn openWrite(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHandle { +pub async fn openWrite(loop: *Loop, path: []const u8) os.File.OpenError!fd_t { return await (async openWriteMode(loop, path, os.File.default_mode) catch unreachable); } /// Creates if does not exist. Truncates the file if it exists. -pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os.File.OpenError!os.FileHandle { +pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os.File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, @@ -454,7 +455,7 @@ pub async fn openReadWrite( loop: *Loop, path: []const u8, mode: os.File.Mode, -) os.File.OpenError!os.FileHandle { +) os.File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; @@ -487,7 +488,7 @@ pub const CloseOperation = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => OsDataPosix, builtin.Os.windows => struct { - handle: ?os.FileHandle, + handle: ?fd_t, }, else => @compileError("Unsupported OS"), @@ -551,7 +552,7 @@ pub const CloseOperation = struct { } } - pub fn setHandle(self: *CloseOperation, handle: os.FileHandle) void { + pub fn setHandle(self: *CloseOperation, handle: fd_t) void { switch (builtin.os) { builtin.Os.linux, builtin.Os.macosx, @@ -585,7 +586,7 @@ pub const CloseOperation = struct { } } - pub fn getHandle(self: *CloseOperation) os.FileHandle { + pub fn getHandle(self: *CloseOperation) fd_t { switch (builtin.os) { builtin.Os.linux, builtin.Os.macosx, @@ -1388,7 +1389,7 @@ async fn testFsWatch(loop: *Loop) !void { } pub const OutStream = struct { - fd: os.FileHandle, + fd: fd_t, stream: Stream, loop: *Loop, offset: usize, @@ -1396,7 +1397,7 @@ pub const OutStream = struct { pub const Error = os.File.WriteError; pub const Stream = event.io.OutStream(Error); - pub fn init(loop: *Loop, fd: os.FileHandle, offset: usize) OutStream { + pub fn init(loop: *Loop, fd: fd_t, offset: usize) OutStream { return OutStream{ .fd = fd, .loop = loop, @@ -1414,7 +1415,7 @@ pub const OutStream = struct { }; pub const InStream = struct { - fd: os.FileHandle, + fd: fd_t, stream: Stream, loop: *Loop, offset: usize, @@ -1422,7 +1423,7 @@ pub const InStream = struct { pub const Error = PReadVError; // TODO make this not have OutOfMemory pub const Stream = event.io.InStream(Error); - pub fn init(loop: *Loop, fd: os.FileHandle, offset: usize) InStream { + pub fn init(loop: *Loop, fd: fd_t, offset: usize) InStream { return InStream{ .fd = fd, .loop = loop, diff --git a/std/event/net.zig b/std/event/net.zig @@ -7,6 +7,8 @@ const os = std.os; const posix = os.posix; const Loop = std.event.Loop; +const fd_t = posix.fd_t; + pub const Server = struct { handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, os.File) void, @@ -139,7 +141,7 @@ pub const ReadError = error{ }; /// returns number of bytes read. 0 means EOF. -pub async fn read(loop: *std.event.Loop, fd: os.FileHandle, buffer: []u8) ReadError!usize { +pub async fn read(loop: *std.event.Loop, fd: fd_t, buffer: []u8) ReadError!usize { const iov = posix.iovec{ .iov_base = buffer.ptr, .iov_len = buffer.len, @@ -150,7 +152,7 @@ pub async fn read(loop: *std.event.Loop, fd: os.FileHandle, buffer: []u8) ReadEr pub const WriteError = error{}; -pub async fn write(loop: *std.event.Loop, fd: os.FileHandle, buffer: []const u8) WriteError!void { +pub async fn write(loop: *std.event.Loop, fd: fd_t, buffer: []const u8) WriteError!void { const iov = posix.iovec_const{ .iov_base = buffer.ptr, .iov_len = buffer.len, @@ -220,7 +222,7 @@ pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, cou } } -pub async fn writev(loop: *Loop, fd: os.FileHandle, data: []const []const u8) !void { +pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); defer loop.allocator.free(iovecs); @@ -234,7 +236,7 @@ pub async fn writev(loop: *Loop, fd: os.FileHandle, data: []const []const u8) !v return await (async writevPosix(loop, fd, iovecs.ptr, data.len) catch unreachable); } -pub async fn readv(loop: *Loop, fd: os.FileHandle, data: []const []u8) !usize { +pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); defer loop.allocator.free(iovecs); @@ -324,14 +326,14 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address, server: *Serv } pub const OutStream = struct { - fd: os.FileHandle, + fd: fd_t, stream: Stream, loop: *Loop, pub const Error = WriteError; pub const Stream = event.io.OutStream(Error); - pub fn init(loop: *Loop, fd: os.FileHandle) OutStream { + pub fn init(loop: *Loop, fd: fd_t) OutStream { return OutStream{ .fd = fd, .loop = loop, @@ -346,14 +348,14 @@ pub const OutStream = struct { }; pub const InStream = struct { - fd: os.FileHandle, + fd: fd_t, stream: Stream, loop: *Loop, pub const Error = ReadError; pub const Stream = event.io.InStream(Error); - pub fn init(loop: *Loop, fd: os.FileHandle) InStream { + pub fn init(loop: *Loop, fd: fd_t) InStream { return InStream{ .fd = fd, .loop = loop, diff --git a/std/os.zig b/std/os.zig @@ -11,7 +11,6 @@ comptime { test "std.os" { _ = @import("os/child_process.zig"); _ = @import("os/darwin.zig"); - _ = @import("os/darwin/errno.zig"); _ = @import("os/get_user_id.zig"); _ = @import("os/linux.zig"); _ = @import("os/path.zig"); @@ -32,14 +31,14 @@ pub const zen = @import("os/zen.zig"); pub const uefi = @import("os/uefi.zig"); pub const wasi = @import("os/wasi.zig"); -pub const system = if (builtin.link_libc) c else switch (builtin.os) { +pub const system = switch (builtin.os) { .linux => linux, .macosx, .ios, .watchos, .tvos => darwin, .freebsd => freebsd, .netbsd => netbsd, .zen => zen, .wasi => wasi, - else => @compileError("Unsupported OS"), + else => struct {}, }; pub const net = @import("net.zig"); @@ -93,8 +92,6 @@ pub const WindowsOpenError = windows_util.OpenError; pub const WindowsWriteError = windows_util.WriteError; pub const WindowsReadError = windows_util.ReadError; -pub const FileHandle = if (is_windows) windows.HANDLE else i32; - pub const getAppDataDir = @import("os/get_app_data_dir.zig").getAppDataDir; pub const GetAppDataDirError = @import("os/get_app_data_dir.zig").GetAppDataDirError; @@ -776,11 +773,13 @@ pub const Dir = struct { } while (true) { - const result = posix.getdirentries64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len, &self.handle.seek); - const err = posix.getErrno(result); - if (err > 0) { - switch (err) { - posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable, + const result = system.__getdirentries64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len, &self.handle.seek); + if (result == 0) return null; + if (result < 0) { + switch (system.getErrno(result)) { + posix.EBADF => unreachable, + posix.EFAULT => unreachable, + posix.ENOTDIR => unreachable, posix.EINVAL => { self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); continue; @@ -788,9 +787,8 @@ pub const Dir = struct { else => return unexpectedErrorPosix(err), } } - if (result == 0) return null; self.handle.index = 0; - self.handle.end_index = result; + self.handle.end_index = @intCast(usize, result); break; } } @@ -1357,28 +1355,16 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { Os.freebsd => { var mib = [4]c_int{ posix.CTL_KERN, posix.KERN_PROC, posix.KERN_PROC_PATHNAME, -1 }; var out_len: usize = out_buffer.len; - const err = posix.getErrno(posix.sysctl(&mib, 4, out_buffer, &out_len, null, 0)); - - if (err == 0) return mem.toSlice(u8, out_buffer); - - return switch (err) { - posix.EFAULT => error.BadAdress, - posix.EPERM => error.PermissionDenied, - else => unexpectedErrorPosix(err), - }; + try posix.sysctl(&mib, out_buffer, &out_len, null, 0); + // TODO could this slice from 0 to out_len instead? + return mem.toSlice(u8, out_buffer); }, Os.netbsd => { var mib = [4]c_int{ posix.CTL_KERN, posix.KERN_PROC_ARGS, -1, posix.KERN_PROC_PATHNAME }; var out_len: usize = out_buffer.len; - const err = posix.getErrno(posix.sysctl(&mib, 4, out_buffer, &out_len, null, 0)); - - if (err == 0) return mem.toSlice(u8, out_buffer); - - return switch (err) { - posix.EFAULT => error.BadAdress, - posix.EPERM => error.PermissionDenied, - else => unexpectedErrorPosix(err), - }; + try posix.sysctl(&mib, out_buffer, &out_len, null, 0); + // TODO could this slice from 0 to out_len instead? + return mem.toSlice(u8, out_buffer); }, Os.windows => { var utf16le_buf: [posix.PATH_MAX_WIDE]u16 = undefined; @@ -1744,22 +1730,12 @@ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize { .macosx, .freebsd, .netbsd => { var count: c_int = undefined; var count_len: usize = @sizeOf(c_int); - const rc = posix.sysctlbyname(switch (builtin.os) { + const name = switch (builtin.os) { builtin.Os.macosx => c"hw.logicalcpu", else => c"hw.ncpu", - }, @ptrCast(*c_void, &count), &count_len, null, 0); - const err = posix.getErrno(rc); - switch (err) { - 0 => return @intCast(usize, count), - posix.EFAULT => unreachable, - posix.EINVAL => unreachable, - posix.ENOMEM => return CpuCountError.OutOfMemory, - posix.ENOTDIR => unreachable, - posix.EISDIR => unreachable, - posix.ENOENT => unreachable, - posix.EPERM => unreachable, - else => return os.unexpectedErrorPosix(err), - } + }; + try posix.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); + return @intCast(usize, count); }, .linux => { const usize_count = 16; diff --git a/std/os/child_process.zig b/std/os/child_process.zig @@ -376,14 +376,7 @@ pub const ChildProcess = struct { const err_pipe = try makePipe(); errdefer destroyPipe(err_pipe); - const pid_result = posix.fork(); - const pid_err = posix.getErrno(pid_result); - if (pid_err > 0) { - return switch (pid_err) { - posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources, - else => os.unexpectedErrorPosix(pid_err), - }; - } + const pid_result = try posix.fork(); if (pid_result == 0) { // we are the child setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); diff --git a/std/os/darwin.zig b/std/os/darwin.zig @@ -1,892 +1,7 @@ const builtin = @import("builtin"); const std = @import("../std.zig"); -const c = std.c; -const assert = std.debug.assert; -const maxInt = std.math.maxInt; - pub const is_the_target = switch (builtin.os) { - .ios, .macosx, .watchos, .tvos => true, + .macosx, .tvos, .watchos, .ios => true, else => false, }; - -pub const errno_codes = @import("darwin/errno.zig"); -pub use errno_codes; - -pub const PATH_MAX = 1024; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -/// [MC2] no permissions -pub const PROT_NONE = 0x00; - -/// [MC2] pages can be read -pub const PROT_READ = 0x01; - -/// [MC2] pages can be written -pub const PROT_WRITE = 0x02; - -/// [MC2] pages can be executed -pub const PROT_EXEC = 0x04; - -/// allocated from memory, swap space -pub const MAP_ANONYMOUS = 0x1000; - -/// map from file (default) -pub const MAP_FILE = 0x0000; - -/// interpret addr exactly -pub const MAP_FIXED = 0x0010; - -/// region may contain semaphores -pub const MAP_HASSEMAPHORE = 0x0200; - -/// changes are private -pub const MAP_PRIVATE = 0x0002; - -/// share changes -pub const MAP_SHARED = 0x0001; - -/// don't cache pages for this mapping -pub const MAP_NOCACHE = 0x0400; - -/// don't reserve needed swap area -pub const MAP_NORESERVE = 0x0040; -pub const MAP_FAILED = maxInt(usize); - -/// [XSI] no hang in wait/no child to reap -pub const WNOHANG = 0x00000001; - -/// [XSI] notify on stop, untraced child -pub const WUNTRACED = 0x00000002; - -/// take signal on signal stack -pub const SA_ONSTACK = 0x0001; - -/// restart system on signal return -pub const SA_RESTART = 0x0002; - -/// reset to SIG_DFL when taking signal -pub const SA_RESETHAND = 0x0004; - -/// do not generate SIGCHLD on child stop -pub const SA_NOCLDSTOP = 0x0008; - -/// don't mask the signal we're delivering -pub const SA_NODEFER = 0x0010; - -/// don't keep zombies around -pub const SA_NOCLDWAIT = 0x0020; - -/// signal handler with SA_SIGINFO args -pub const SA_SIGINFO = 0x0040; - -/// do not bounce off kernel's sigtramp -pub const SA_USERTRAMP = 0x0100; - -/// signal handler with SA_SIGINFO args with 64bit regs information -pub const SA_64REGSET = 0x0200; - -pub const O_LARGEFILE = 0x0000; -pub const O_PATH = 0x0000; - -pub const F_OK = 0; -pub const X_OK = 1; -pub const W_OK = 2; -pub const R_OK = 4; - -/// open for reading only -pub const O_RDONLY = 0x0000; - -/// open for writing only -pub const O_WRONLY = 0x0001; - -/// open for reading and writing -pub const O_RDWR = 0x0002; - -/// do not block on open or for data to become available -pub const O_NONBLOCK = 0x0004; - -/// append on each write -pub const O_APPEND = 0x0008; - -/// create file if it does not exist -pub const O_CREAT = 0x0200; - -/// truncate size to 0 -pub const O_TRUNC = 0x0400; - -/// error if O_CREAT and the file exists -pub const O_EXCL = 0x0800; - -/// atomically obtain a shared lock -pub const O_SHLOCK = 0x0010; - -/// atomically obtain an exclusive lock -pub const O_EXLOCK = 0x0020; - -/// do not follow symlinks -pub const O_NOFOLLOW = 0x0100; - -/// allow open of symlinks -pub const O_SYMLINK = 0x200000; - -/// descriptor requested for event notifications only -pub const O_EVTONLY = 0x8000; - -/// mark as close-on-exec -pub const O_CLOEXEC = 0x1000000; - -pub const O_ACCMODE = 3; -pub const O_ALERT = 536870912; -pub const O_ASYNC = 64; -pub const O_DIRECTORY = 1048576; -pub const O_DP_GETRAWENCRYPTED = 1; -pub const O_DP_GETRAWUNENCRYPTED = 2; -pub const O_DSYNC = 4194304; -pub const O_FSYNC = O_SYNC; -pub const O_NOCTTY = 131072; -pub const O_POPUP = 2147483648; -pub const O_SYNC = 128; - -pub const SEEK_SET = 0x0; -pub const SEEK_CUR = 0x1; -pub const SEEK_END = 0x2; - -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; - -/// block specified signal set -pub const SIG_BLOCK = 1; - -/// unblock specified signal set -pub const SIG_UNBLOCK = 2; - -/// set specified signal set -pub const SIG_SETMASK = 3; - -/// hangup -pub const SIGHUP = 1; - -/// interrupt -pub const SIGINT = 2; - -/// quit -pub const SIGQUIT = 3; - -/// illegal instruction (not reset when caught) -pub const SIGILL = 4; - -/// trace trap (not reset when caught) -pub const SIGTRAP = 5; - -/// abort() -pub const SIGABRT = 6; - -/// pollable event ([XSR] generated, not supported) -pub const SIGPOLL = 7; - -/// compatibility -pub const SIGIOT = SIGABRT; - -/// EMT instruction -pub const SIGEMT = 7; - -/// floating point exception -pub const SIGFPE = 8; - -/// kill (cannot be caught or ignored) -pub const SIGKILL = 9; - -/// bus error -pub const SIGBUS = 10; - -/// segmentation violation -pub const SIGSEGV = 11; - -/// bad argument to system call -pub const SIGSYS = 12; - -/// write on a pipe with no one to read it -pub const SIGPIPE = 13; - -/// alarm clock -pub const SIGALRM = 14; - -/// software termination signal from kill -pub const SIGTERM = 15; - -/// urgent condition on IO channel -pub const SIGURG = 16; - -/// sendable stop signal not from tty -pub const SIGSTOP = 17; - -/// stop signal from tty -pub const SIGTSTP = 18; - -/// continue a stopped process -pub const SIGCONT = 19; - -/// to parent on child stop or exit -pub const SIGCHLD = 20; - -/// to readers pgrp upon background tty read -pub const SIGTTIN = 21; - -/// like TTIN for output if (tp->t_local&LTOSTOP) -pub const SIGTTOU = 22; - -/// input/output possible signal -pub const SIGIO = 23; - -/// exceeded CPU time limit -pub const SIGXCPU = 24; - -/// exceeded file size limit -pub const SIGXFSZ = 25; - -/// virtual time alarm -pub const SIGVTALRM = 26; - -/// profiling time alarm -pub const SIGPROF = 27; - -/// window size changes -pub const SIGWINCH = 28; - -/// information request -pub const SIGINFO = 29; - -/// user defined signal 1 -pub const SIGUSR1 = 30; - -/// user defined signal 2 -pub const SIGUSR2 = 31; - -/// no flag value -pub const KEVENT_FLAG_NONE = 0x000; - -/// immediate timeout -pub const KEVENT_FLAG_IMMEDIATE = 0x001; - -/// output events only include change -pub const KEVENT_FLAG_ERROR_EVENTS = 0x002; - -/// add event to kq (implies enable) -pub const EV_ADD = 0x0001; - -/// delete event from kq -pub const EV_DELETE = 0x0002; - -/// enable event -pub const EV_ENABLE = 0x0004; - -/// disable event (not reported) -pub const EV_DISABLE = 0x0008; - -/// only report one occurrence -pub const EV_ONESHOT = 0x0010; - -/// clear event state after reporting -pub const EV_CLEAR = 0x0020; - -/// force immediate event output -/// ... with or without EV_ERROR -/// ... use KEVENT_FLAG_ERROR_EVENTS -/// on syscalls supporting flags -pub const EV_RECEIPT = 0x0040; - -/// disable event after reporting -pub const EV_DISPATCH = 0x0080; - -/// unique kevent per udata value -pub const EV_UDATA_SPECIFIC = 0x0100; - -/// ... in combination with EV_DELETE -/// will defer delete until udata-specific -/// event enabled. EINPROGRESS will be -/// returned to indicate the deferral -pub const EV_DISPATCH2 = EV_DISPATCH | EV_UDATA_SPECIFIC; - -/// report that source has vanished -/// ... only valid with EV_DISPATCH2 -pub const EV_VANISHED = 0x0200; - -/// reserved by system -pub const EV_SYSFLAGS = 0xF000; - -/// filter-specific flag -pub const EV_FLAG0 = 0x1000; - -/// filter-specific flag -pub const EV_FLAG1 = 0x2000; - -/// EOF detected -pub const EV_EOF = 0x8000; - -/// error, data contains errno -pub const EV_ERROR = 0x4000; - -pub const EV_POLL = EV_FLAG0; -pub const EV_OOBAND = EV_FLAG1; - -pub const EVFILT_READ = -1; -pub const EVFILT_WRITE = -2; - -/// attached to aio requests -pub const EVFILT_AIO = -3; - -/// attached to vnodes -pub const EVFILT_VNODE = -4; - -/// attached to struct proc -pub const EVFILT_PROC = -5; - -/// attached to struct proc -pub const EVFILT_SIGNAL = -6; - -/// timers -pub const EVFILT_TIMER = -7; - -/// Mach portsets -pub const EVFILT_MACHPORT = -8; - -/// Filesystem events -pub const EVFILT_FS = -9; - -/// User events -pub const EVFILT_USER = -10; - -/// Virtual memory events -pub const EVFILT_VM = -12; - -/// Exception events -pub const EVFILT_EXCEPT = -15; - -pub const EVFILT_SYSCOUNT = 17; - -/// On input, NOTE_TRIGGER causes the event to be triggered for output. -pub const NOTE_TRIGGER = 0x01000000; - -/// ignore input fflags -pub const NOTE_FFNOP = 0x00000000; - -/// and fflags -pub const NOTE_FFAND = 0x40000000; - -/// or fflags -pub const NOTE_FFOR = 0x80000000; - -/// copy fflags -pub const NOTE_FFCOPY = 0xc0000000; - -/// mask for operations -pub const NOTE_FFCTRLMASK = 0xc0000000; -pub const NOTE_FFLAGSMASK = 0x00ffffff; - -/// low water mark -pub const NOTE_LOWAT = 0x00000001; - -/// OOB data -pub const NOTE_OOB = 0x00000002; - -/// vnode was removed -pub const NOTE_DELETE = 0x00000001; - -/// data contents changed -pub const NOTE_WRITE = 0x00000002; - -/// size increased -pub const NOTE_EXTEND = 0x00000004; - -/// attributes changed -pub const NOTE_ATTRIB = 0x00000008; - -/// link count changed -pub const NOTE_LINK = 0x00000010; - -/// vnode was renamed -pub const NOTE_RENAME = 0x00000020; - -/// vnode access was revoked -pub const NOTE_REVOKE = 0x00000040; - -/// No specific vnode event: to test for EVFILT_READ activation -pub const NOTE_NONE = 0x00000080; - -/// vnode was unlocked by flock(2) -pub const NOTE_FUNLOCK = 0x00000100; - -/// process exited -pub const NOTE_EXIT = 0x80000000; - -/// process forked -pub const NOTE_FORK = 0x40000000; - -/// process exec'd -pub const NOTE_EXEC = 0x20000000; - -/// shared with EVFILT_SIGNAL -pub const NOTE_SIGNAL = 0x08000000; - -/// exit status to be returned, valid for child process only -pub const NOTE_EXITSTATUS = 0x04000000; - -/// provide details on reasons for exit -pub const NOTE_EXIT_DETAIL = 0x02000000; - -/// mask for signal & exit status -pub const NOTE_PDATAMASK = 0x000fffff; -pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); - -pub const NOTE_EXIT_DETAIL_MASK = 0x00070000; -pub const NOTE_EXIT_DECRYPTFAIL = 0x00010000; -pub const NOTE_EXIT_MEMORY = 0x00020000; -pub const NOTE_EXIT_CSERROR = 0x00040000; - -/// will react on memory pressure -pub const NOTE_VM_PRESSURE = 0x80000000; - -/// will quit on memory pressure, possibly after cleaning up dirty state -pub const NOTE_VM_PRESSURE_TERMINATE = 0x40000000; - -/// will quit immediately on memory pressure -pub const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = 0x20000000; - -/// there was an error -pub const NOTE_VM_ERROR = 0x10000000; - -/// data is seconds -pub const NOTE_SECONDS = 0x00000001; - -/// data is microseconds -pub const NOTE_USECONDS = 0x00000002; - -/// data is nanoseconds -pub const NOTE_NSECONDS = 0x00000004; - -/// absolute timeout -pub const NOTE_ABSOLUTE = 0x00000008; - -/// ext[1] holds leeway for power aware timers -pub const NOTE_LEEWAY = 0x00000010; - -/// system does minimal timer coalescing -pub const NOTE_CRITICAL = 0x00000020; - -/// system does maximum timer coalescing -pub const NOTE_BACKGROUND = 0x00000040; -pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080; - -/// data is mach absolute time units -pub const NOTE_MACHTIME = 0x00000100; - -pub const AF_UNSPEC = 0; -pub const AF_LOCAL = 1; -pub const AF_UNIX = AF_LOCAL; -pub const AF_INET = 2; -pub const AF_SYS_CONTROL = 2; -pub const AF_IMPLINK = 3; -pub const AF_PUP = 4; -pub const AF_CHAOS = 5; -pub const AF_NS = 6; -pub const AF_ISO = 7; -pub const AF_OSI = AF_ISO; -pub const AF_ECMA = 8; -pub const AF_DATAKIT = 9; -pub const AF_CCITT = 10; -pub const AF_SNA = 11; -pub const AF_DECnet = 12; -pub const AF_DLI = 13; -pub const AF_LAT = 14; -pub const AF_HYLINK = 15; -pub const AF_APPLETALK = 16; -pub const AF_ROUTE = 17; -pub const AF_LINK = 18; -pub const AF_XTP = 19; -pub const AF_COIP = 20; -pub const AF_CNT = 21; -pub const AF_RTIP = 22; -pub const AF_IPX = 23; -pub const AF_SIP = 24; -pub const AF_PIP = 25; -pub const AF_ISDN = 28; -pub const AF_E164 = AF_ISDN; -pub const AF_KEY = 29; -pub const AF_INET6 = 30; -pub const AF_NATM = 31; -pub const AF_SYSTEM = 32; -pub const AF_NETBIOS = 33; -pub const AF_PPP = 34; -pub const AF_MAX = 40; - -pub const PF_UNSPEC = AF_UNSPEC; -pub const PF_LOCAL = AF_LOCAL; -pub const PF_UNIX = PF_LOCAL; -pub const PF_INET = AF_INET; -pub const PF_IMPLINK = AF_IMPLINK; -pub const PF_PUP = AF_PUP; -pub const PF_CHAOS = AF_CHAOS; -pub const PF_NS = AF_NS; -pub const PF_ISO = AF_ISO; -pub const PF_OSI = AF_ISO; -pub const PF_ECMA = AF_ECMA; -pub const PF_DATAKIT = AF_DATAKIT; -pub const PF_CCITT = AF_CCITT; -pub const PF_SNA = AF_SNA; -pub const PF_DECnet = AF_DECnet; -pub const PF_DLI = AF_DLI; -pub const PF_LAT = AF_LAT; -pub const PF_HYLINK = AF_HYLINK; -pub const PF_APPLETALK = AF_APPLETALK; -pub const PF_ROUTE = AF_ROUTE; -pub const PF_LINK = AF_LINK; -pub const PF_XTP = AF_XTP; -pub const PF_COIP = AF_COIP; -pub const PF_CNT = AF_CNT; -pub const PF_SIP = AF_SIP; -pub const PF_IPX = AF_IPX; -pub const PF_RTIP = AF_RTIP; -pub const PF_PIP = AF_PIP; -pub const PF_ISDN = AF_ISDN; -pub const PF_KEY = AF_KEY; -pub const PF_INET6 = AF_INET6; -pub const PF_NATM = AF_NATM; -pub const PF_SYSTEM = AF_SYSTEM; -pub const PF_NETBIOS = AF_NETBIOS; -pub const PF_PPP = AF_PPP; -pub const PF_MAX = AF_MAX; - -pub const SYSPROTO_EVENT = 1; -pub const SYSPROTO_CONTROL = 2; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; -pub const SOCK_MAXADDRLEN = 255; - -pub const IPPROTO_ICMP = 1; -pub const IPPROTO_ICMPV6 = 58; -pub const IPPROTO_TCP = 6; -pub const IPPROTO_UDP = 17; -pub const IPPROTO_IP = 0; -pub const IPPROTO_IPV6 = 41; - -fn wstatus(x: i32) i32 { - return x & 0o177; -} -const wstopped = 0o177; -pub fn WEXITSTATUS(x: i32) i32 { - return x >> 8; -} -pub fn WTERMSIG(x: i32) i32 { - return wstatus(x); -} -pub fn WSTOPSIG(x: i32) i32 { - return x >> 8; -} -pub fn WIFEXITED(x: i32) bool { - return wstatus(x) == 0; -} -pub fn WIFSTOPPED(x: i32) bool { - return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; -} -pub fn WIFSIGNALED(x: i32) bool { - return wstatus(x) != wstopped and wstatus(x) != 0; -} - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) usize { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; -} - -pub fn close(fd: i32) usize { - return errnoWrap(c.close(fd)); -} - -pub fn abort() noreturn { - c.abort(); -} - -// bind(int socket, const struct sockaddr *address, socklen_t address_len) -pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { - return errnoWrap(c.bind(@bitCast(c_int, fd), addr, len)); -} - -pub fn exit(code: i32) noreturn { - c.exit(code); -} - -pub fn isatty(fd: i32) bool { - return c.isatty(fd) != 0; -} - -pub fn fstat(fd: i32, buf: *c.Stat) usize { - return errnoWrap(c.@"fstat$INODE64"(fd, buf)); -} - -pub fn lseek(fd: i32, offset: isize, whence: c_int) usize { - return errnoWrap(c.lseek(fd, offset, whence)); -} - -// TODO https://github.com/ziglang/zig/issues/265 on the whole file -pub fn open(path: [*]const u8, flags: u32, mode: usize) usize { - return errnoWrap(c.open(path, @bitCast(c_int, flags), mode)); -} - -pub fn raise(sig: i32) usize { - return errnoWrap(c.raise(sig)); -} - -pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize { - return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte)); -} - -pub fn pread(fd: i32, buf: [*]u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pread(fd, @ptrCast(*c_void, buf), nbyte, offset)); -} - -pub fn stat(noalias path: [*]const u8, noalias buf: *stat) usize { - return errnoWrap(c.stat(path, buf)); -} - -pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize { - return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte)); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pwrite(fd, @ptrCast(*const c_void, buf), nbyte, offset)); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - const ptr_result = c.mmap( - @ptrCast(?*c_void, address), - length, - @bitCast(c_int, @intCast(c_uint, prot)), - @bitCast(c_int, c_uint(flags)), - fd, - offset, - ); - const isize_result = @bitCast(isize, @ptrToInt(ptr_result)); - return errnoWrap(isize_result); -} - -pub fn munmap(address: usize, length: usize) usize { - return errnoWrap(c.munmap(@intToPtr(?*c_void, address), length)); -} - -pub fn unlink(path: [*]const u8) usize { - return errnoWrap(c.unlink(path)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn waitpid(pid: i32, status: *i32, options: u32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options))); -} - -pub fn fork() usize { - return errnoWrap(c.fork()); -} - -pub fn access(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.access(path, mode)); -} - -pub fn pipe(fds: *[2]i32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.pipe(@ptrCast(*[2]c_int, fds))); -} - -pub fn getdirentries64(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize { - return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep))); -} - -pub fn kqueue() usize { - return errnoWrap(c.kqueue()); -} - -pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize { - return errnoWrap(c.kevent( - kq, - changelist.ptr, - @intCast(c_int, changelist.len), - eventlist.ptr, - @intCast(c_int, eventlist.len), - timeout, - )); -} - -pub fn kevent64( - kq: i32, - changelist: []const kevent64_s, - eventlist: []kevent64_s, - flags: u32, - timeout: ?*const timespec, -) usize { - return errnoWrap(c.kevent64(kq, changelist.ptr, changelist.len, eventlist.ptr, eventlist.len, flags, timeout)); -} - -pub fn mkdir(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.mkdir(path, mode)); -} - -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.symlink(existing, new)); -} - -pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen)); -} - -pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen)); -} - -pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize { - return errnoWrap(c.sysctlnametomib(name, wibp, sizep)); -} - -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.rename(old, new)); -} - -pub fn rmdir(path: [*]const u8) usize { - return errnoWrap(c.rmdir(path)); -} - -pub fn chdir(path: [*]const u8) usize { - return errnoWrap(c.chdir(path)); -} - -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return errnoWrap(c.execve(path, argv, envp)); -} - -pub fn dup2(old: i32, new: i32) usize { - return errnoWrap(c.dup2(old, new)); -} - -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return errnoWrap(c.readlink(path, buf_ptr, buf_len)); -} - -pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) usize { - return errnoWrap(c.gettimeofday(tv, tz)); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return errnoWrap(c.nanosleep(req, rem)); -} - -pub fn realpath(noalias filename: [*]const u8, noalias resolved_name: [*]u8) usize { - return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return errnoWrap(c.setreuid(ruid, euid)); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return errnoWrap(c.setregid(rgid, egid)); -} - -pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { - return errnoWrap(c.sigprocmask(@bitCast(c_int, flags), set, oldset)); -} - -pub fn sigaction(sig: u5, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { - assert(sig != SIGKILL); - assert(sig != SIGSTOP); - var cact = c.Sigaction{ - .handler = @ptrCast(extern fn (c_int) void, act.handler), - .sa_flags = @bitCast(c_int, act.flags), - .sa_mask = act.mask, - }; - var coact: c.Sigaction = undefined; - const result = errnoWrap(c.sigaction(sig, &cact, &coact)); - if (result != 0) { - return result; - } - if (oact) |old| { - old.* = Sigaction{ - .handler = @ptrCast(extern fn (i32) void, coact.handler), - .flags = @bitCast(u32, coact.sa_flags), - .mask = coact.sa_mask, - }; - } - return result; -} - -pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { - return errnoWrap(c.socket(@bitCast(c_int, domain), @bitCast(c_int, socket_type), @bitCast(c_int, protocol))); -} - -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 sigset_t = c.sigset_t; -pub const empty_sigset = sigset_t(0); - -pub const timespec = c.timespec; -pub const Stat = c.Stat; -pub const dirent = c.dirent; - -pub const in_port_t = c.in_port_t; -pub const sa_family_t = c.sa_family_t; -pub const socklen_t = c.socklen_t; - -pub const sockaddr = c.sockaddr; -pub const sockaddr_in = c.sockaddr_in; -pub const sockaddr_in6 = c.sockaddr_in6; - -/// Renamed from `kevent` to `Kevent` to avoid conflict with the syscall. -pub const Kevent = c.Kevent; -pub const kevent64_s = c.kevent64_s; - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = struct { - handler: extern fn (i32) void, - mask: sigset_t, - flags: u32, -}; - -pub fn sigaddset(set: *sigset_t, signo: u5) void { - set.* |= u32(1) << (signo - 1); -} - -/// Takes the return value from a syscall and formats it back in the way -/// that the kernel represents it to libc. Errno was a mistake, let's make -/// it go away forever. -fn errnoWrap(value: isize) usize { - return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value); -} - -pub const timezone = c.timezone; -pub const timeval = c.timeval; -pub const mach_timebase_info_data = c.mach_timebase_info_data; - -pub const mach_absolute_time = c.mach_absolute_time; -pub const mach_timebase_info = c.mach_timebase_info; +pub use std.c; diff --git a/std/os/darwin/errno.zig b/std/os/darwin/errno.zig @@ -1,328 +0,0 @@ -/// Operation not permitted -pub const EPERM = 1; - -/// No such file or directory -pub const ENOENT = 2; - -/// No such process -pub const ESRCH = 3; - -/// Interrupted system call -pub const EINTR = 4; - -/// Input/output error -pub const EIO = 5; - -/// Device not configured -pub const ENXIO = 6; - -/// Argument list too long -pub const E2BIG = 7; - -/// Exec format error -pub const ENOEXEC = 8; - -/// Bad file descriptor -pub const EBADF = 9; - -/// No child processes -pub const ECHILD = 10; - -/// Resource deadlock avoided -pub const EDEADLK = 11; - -/// Cannot allocate memory -pub const ENOMEM = 12; - -/// Permission denied -pub const EACCES = 13; - -/// Bad address -pub const EFAULT = 14; - -/// Block device required -pub const ENOTBLK = 15; - -/// Device / Resource busy -pub const EBUSY = 16; - -/// File exists -pub const EEXIST = 17; - -/// Cross-device link -pub const EXDEV = 18; - -/// Operation not supported by device -pub const ENODEV = 19; - -/// Not a directory -pub const ENOTDIR = 20; - -/// Is a directory -pub const EISDIR = 21; - -/// Invalid argument -pub const EINVAL = 22; - -/// Too many open files in system -pub const ENFILE = 23; - -/// Too many open files -pub const EMFILE = 24; - -/// Inappropriate ioctl for device -pub const ENOTTY = 25; - -/// Text file busy -pub const ETXTBSY = 26; - -/// File too large -pub const EFBIG = 27; - -/// No space left on device -pub const ENOSPC = 28; - -/// Illegal seek -pub const ESPIPE = 29; - -/// Read-only file system -pub const EROFS = 30; - -/// Too many links -pub const EMLINK = 31; -/// Broken pipe - -// math software -pub const EPIPE = 32; - -/// Numerical argument out of domain -pub const EDOM = 33; -/// Result too large - -// non-blocking and interrupt i/o -pub const ERANGE = 34; - -/// Resource temporarily unavailable -pub const EAGAIN = 35; - -/// Operation would block -pub const EWOULDBLOCK = EAGAIN; - -/// Operation now in progress -pub const EINPROGRESS = 36; -/// Operation already in progress - -// ipc/network software -- argument errors -pub const EALREADY = 37; - -/// Socket operation on non-socket -pub const ENOTSOCK = 38; - -/// Destination address required -pub const EDESTADDRREQ = 39; - -/// Message too long -pub const EMSGSIZE = 40; - -/// Protocol wrong type for socket -pub const EPROTOTYPE = 41; - -/// Protocol not available -pub const ENOPROTOOPT = 42; - -/// Protocol not supported -pub const EPROTONOSUPPORT = 43; - -/// Socket type not supported -pub const ESOCKTNOSUPPORT = 44; - -/// Operation not supported -pub const ENOTSUP = 45; - -/// Protocol family not supported -pub const EPFNOSUPPORT = 46; - -/// Address family not supported by protocol family -pub const EAFNOSUPPORT = 47; - -/// Address already in use -pub const EADDRINUSE = 48; -/// Can't assign requested address - -// ipc/network software -- operational errors -pub const EADDRNOTAVAIL = 49; - -/// Network is down -pub const ENETDOWN = 50; - -/// Network is unreachable -pub const ENETUNREACH = 51; - -/// Network dropped connection on reset -pub const ENETRESET = 52; - -/// Software caused connection abort -pub const ECONNABORTED = 53; - -/// Connection reset by peer -pub const ECONNRESET = 54; - -/// No buffer space available -pub const ENOBUFS = 55; - -/// Socket is already connected -pub const EISCONN = 56; - -/// Socket is not connected -pub const ENOTCONN = 57; - -/// Can't send after socket shutdown -pub const ESHUTDOWN = 58; - -/// Too many references: can't splice -pub const ETOOMANYREFS = 59; - -/// Operation timed out -pub const ETIMEDOUT = 60; - -/// Connection refused -pub const ECONNREFUSED = 61; - -/// Too many levels of symbolic links -pub const ELOOP = 62; - -/// File name too long -pub const ENAMETOOLONG = 63; - -/// Host is down -pub const EHOSTDOWN = 64; - -/// No route to host -pub const EHOSTUNREACH = 65; -/// Directory not empty - -// quotas & mush -pub const ENOTEMPTY = 66; - -/// Too many processes -pub const EPROCLIM = 67; - -/// Too many users -pub const EUSERS = 68; -/// Disc quota exceeded - -// Network File System -pub const EDQUOT = 69; - -/// Stale NFS file handle -pub const ESTALE = 70; - -/// Too many levels of remote in path -pub const EREMOTE = 71; - -/// RPC struct is bad -pub const EBADRPC = 72; - -/// RPC version wrong -pub const ERPCMISMATCH = 73; - -/// RPC prog. not avail -pub const EPROGUNAVAIL = 74; - -/// Program version wrong -pub const EPROGMISMATCH = 75; - -/// Bad procedure for program -pub const EPROCUNAVAIL = 76; - -/// No locks available -pub const ENOLCK = 77; - -/// Function not implemented -pub const ENOSYS = 78; - -/// Inappropriate file type or format -pub const EFTYPE = 79; - -/// Authentication error -pub const EAUTH = 80; -/// Need authenticator - -// Intelligent device errors -pub const ENEEDAUTH = 81; - -/// Device power is off -pub const EPWROFF = 82; - -/// Device error, e.g. paper out -pub const EDEVERR = 83; -/// Value too large to be stored in data type - -// Program loading errors -pub const EOVERFLOW = 84; - -/// Bad executable -pub const EBADEXEC = 85; - -/// Bad CPU type in executable -pub const EBADARCH = 86; - -/// Shared library version mismatch -pub const ESHLIBVERS = 87; - -/// Malformed Macho file -pub const EBADMACHO = 88; - -/// Operation canceled -pub const ECANCELED = 89; - -/// Identifier removed -pub const EIDRM = 90; - -/// No message of desired type -pub const ENOMSG = 91; - -/// Illegal byte sequence -pub const EILSEQ = 92; - -/// Attribute not found -pub const ENOATTR = 93; - -/// Bad message -pub const EBADMSG = 94; - -/// Reserved -pub const EMULTIHOP = 95; - -/// No message available on STREAM -pub const ENODATA = 96; - -/// Reserved -pub const ENOLINK = 97; - -/// No STREAM resources -pub const ENOSR = 98; - -/// Not a STREAM -pub const ENOSTR = 99; - -/// Protocol error -pub const EPROTO = 100; - -/// STREAM ioctl timeout -pub const ETIME = 101; - -/// No such policy registered -pub const ENOPOLICY = 103; - -/// State not recoverable -pub const ENOTRECOVERABLE = 104; - -/// Previous owner died -pub const EOWNERDEAD = 105; - -/// Interface output queue is full -pub const EQFULL = 106; - -/// Must be equal largest errno -pub const ELAST = 106; diff --git a/std/os/file.zig b/std/os/file.zig @@ -16,7 +16,7 @@ const is_windows = builtin.os == builtin.Os.windows; pub const File = struct { /// The OS-specific file descriptor or file handle. - handle: os.FileHandle, + handle: posix.fd_t, pub const Mode = switch (builtin.os) { Os.windows => void, @@ -138,83 +138,24 @@ pub const File = struct { return openHandle(handle); } - pub fn openHandle(handle: os.FileHandle) File { + pub fn openHandle(handle: posix.fd_t) File { return File{ .handle = handle }; } - pub const AccessError = error{ - PermissionDenied, - FileNotFound, - NameTooLong, - InputOutput, - SystemResources, - BadPathName, - - /// On Windows, file paths must be valid Unicode. - InvalidUtf8, - - Unexpected, - }; - - /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. - /// Otherwise use `access` or `accessC`. - pub fn accessW(path: [*]const u16) AccessError!void { - if (os.windows.GetFileAttributesW(path) != os.windows.INVALID_FILE_ATTRIBUTES) { - return; - } - - const err = windows.GetLastError(); - switch (err) { - windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, - windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, - windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, - else => return os.unexpectedErrorWindows(err), - } + /// Test for the existence of `path`. + /// `path` is UTF8-encoded. + pub fn exists(path: []const u8) AccessError!void { + return posix.access(path, posix.F_OK); } - /// Call if you have a UTF-8 encoded, null-terminated string. - /// Otherwise use `access` or `accessW`. - pub fn accessC(path: [*]const u8) AccessError!void { - if (is_windows) { - const path_w = try windows_util.cStrToPrefixedFileW(path); - return accessW(&path_w); - } - if (is_posix) { - const result = posix.access(path, posix.F_OK); - const err = posix.getErrno(result); - switch (err) { - 0 => return, - posix.EACCES => return error.PermissionDenied, - posix.EROFS => return error.PermissionDenied, - posix.ELOOP => return error.PermissionDenied, - posix.ETXTBSY => return error.PermissionDenied, - posix.ENOTDIR => return error.FileNotFound, - posix.ENOENT => return error.FileNotFound, - - posix.ENAMETOOLONG => return error.NameTooLong, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EIO => return error.InputOutput, - posix.ENOMEM => return error.SystemResources, - else => return os.unexpectedErrorPosix(err), - } - } - @compileError("Unsupported OS"); + /// Same as `exists` except the parameter is null-terminated UTF16LE-encoded. + pub fn existsW(path: [*]const u16) AccessError!void { + return posix.accessW(path, posix.F_OK); } - pub fn access(path: []const u8) AccessError!void { - if (is_windows) { - const path_w = try windows_util.sliceToPrefixedFileW(path); - return accessW(&path_w); - } - if (is_posix) { - var path_with_null: [posix.PATH_MAX]u8 = undefined; - if (path.len >= posix.PATH_MAX) return error.NameTooLong; - mem.copy(u8, path_with_null[0..], path); - path_with_null[path.len] = 0; - return accessC(&path_with_null); - } - @compileError("Unsupported OS"); + /// Same as `exists` except the parameter is null-terminated. + pub fn existsC(path: [*]const u8) AccessError!void { + return posix.accessC(path, posix.F_OK); } /// Upon success, the stream is in an uninitialized state. To continue using it, diff --git a/std/os/freebsd.zig b/std/os/freebsd.zig @@ -1,845 +1,4 @@ -const builtin = @import("builtin"); - -pub use @import("freebsd/errno.zig"); - const std = @import("../std.zig"); -const c = std.c; - -const assert = std.debug.assert; -const maxInt = std.math.maxInt; -pub const Kevent = c.Kevent; - -pub const CTL_KERN = 1; -pub const CTL_DEBUG = 5; - -pub const KERN_PROC = 14; // struct: process entries -pub const KERN_PROC_PATHNAME = 12; // path to executable - -pub const PATH_MAX = 1024; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -pub const PROT_NONE = 0; -pub const PROT_READ = 1; -pub const PROT_WRITE = 2; -pub const PROT_EXEC = 4; - -pub const CLOCK_REALTIME = 0; -pub const CLOCK_VIRTUAL = 1; -pub const CLOCK_PROF = 2; -pub const CLOCK_MONOTONIC = 4; -pub const CLOCK_UPTIME = 5; -pub const CLOCK_UPTIME_PRECISE = 7; -pub const CLOCK_UPTIME_FAST = 8; -pub const CLOCK_REALTIME_PRECISE = 9; -pub const CLOCK_REALTIME_FAST = 10; -pub const CLOCK_MONOTONIC_PRECISE = 11; -pub const CLOCK_MONOTONIC_FAST = 12; -pub const CLOCK_SECOND = 13; -pub const CLOCK_THREAD_CPUTIME_ID = 14; -pub const CLOCK_PROCESS_CPUTIME_ID = 15; - -pub const MAP_FAILED = maxInt(usize); -pub const MAP_SHARED = 0x0001; -pub const MAP_PRIVATE = 0x0002; -pub const MAP_FIXED = 0x0010; -pub const MAP_STACK = 0x0400; -pub const MAP_NOSYNC = 0x0800; -pub const MAP_ANON = 0x1000; -pub const MAP_ANONYMOUS = MAP_ANON; -pub const MAP_FILE = 0; -pub const MAP_NORESERVE = 0; - -pub const MAP_GUARD = 0x00002000; -pub const MAP_EXCL = 0x00004000; -pub const MAP_NOCORE = 0x00020000; -pub const MAP_PREFAULT_READ = 0x00040000; -pub const MAP_32BIT = 0x00080000; - -pub const WNOHANG = 1; -pub const WUNTRACED = 2; -pub const WSTOPPED = WUNTRACED; -pub const WCONTINUED = 4; -pub const WNOWAIT = 8; -pub const WEXITED = 16; -pub const WTRAPPED = 32; - -pub const SA_ONSTACK = 0x0001; -pub const SA_RESTART = 0x0002; -pub const SA_RESETHAND = 0x0004; -pub const SA_NOCLDSTOP = 0x0008; -pub const SA_NODEFER = 0x0010; -pub const SA_NOCLDWAIT = 0x0020; -pub const SA_SIGINFO = 0x0040; - -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 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 SIGIO = 23; -pub const SIGXCPU = 24; -pub const SIGXFSZ = 25; -pub const SIGVTALRM = 26; -pub const SIGPROF = 27; -pub const SIGWINCH = 28; -pub const SIGINFO = 29; -pub const SIGUSR1 = 30; -pub const SIGUSR2 = 31; -pub const SIGTHR = 32; -pub const SIGLWP = SIGTHR; -pub const SIGLIBRT = 33; - -pub const SIGRTMIN = 65; -pub const SIGRTMAX = 126; - -// access function -pub const F_OK = 0; // test for existence of file -pub const X_OK = 1; // test for execute or search permission -pub const W_OK = 2; // test for write permission -pub const R_OK = 4; // test for read permission - -pub const O_RDONLY = 0x0000; -pub const O_WRONLY = 0x0001; -pub const O_RDWR = 0x0002; -pub const O_ACCMODE = 0x0003; - -pub const O_CREAT = 0x0200; -pub const O_EXCL = 0x0800; -pub const O_NOCTTY = 0x8000; -pub const O_TRUNC = 0x0400; -pub const O_APPEND = 0x0008; -pub const O_NONBLOCK = 0x0004; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0x0080; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0x0100; -pub const O_CLOEXEC = 0x00100000; - -pub const O_ASYNC = 0x0040; -pub const O_DIRECT = 0x00010000; -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; - -pub const SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -pub const SIG_BLOCK = 1; -pub const SIG_UNBLOCK = 2; -pub const SIG_SETMASK = 3; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; - -pub const SOCK_CLOEXEC = 0x10000000; -pub const SOCK_NONBLOCK = 0x20000000; - -pub const PROTO_ip = 0o000; -pub const PROTO_icmp = 0o001; -pub const PROTO_igmp = 0o002; -pub const PROTO_ggp = 0o003; -pub const PROTO_ipencap = 0o004; -pub const PROTO_st = 0o005; -pub const PROTO_tcp = 0o006; -pub const PROTO_egp = 0o010; -pub const PROTO_pup = 0o014; -pub const PROTO_udp = 0o021; -pub const PROTO_hmp = 0o024; -pub const PROTO_xns_idp = 0o026; -pub const PROTO_rdp = 0o033; -pub const PROTO_iso_tp4 = 0o035; -pub const PROTO_xtp = 0o044; -pub const PROTO_ddp = 0o045; -pub const PROTO_idpr_cmtp = 0o046; -pub const PROTO_ipv6 = 0o051; -pub const PROTO_ipv6_route = 0o053; -pub const PROTO_ipv6_frag = 0o054; -pub const PROTO_idrp = 0o055; -pub const PROTO_rsvp = 0o056; -pub const PROTO_gre = 0o057; -pub const PROTO_esp = 0o062; -pub const PROTO_ah = 0o063; -pub const PROTO_skip = 0o071; -pub const PROTO_ipv6_icmp = 0o072; -pub const PROTO_ipv6_nonxt = 0o073; -pub const PROTO_ipv6_opts = 0o074; -pub const PROTO_rspf = 0o111; -pub const PROTO_vmtp = 0o121; -pub const PROTO_ospf = 0o131; -pub const PROTO_ipip = 0o136; -pub const PROTO_encap = 0o142; -pub const PROTO_pim = 0o147; -pub const PROTO_raw = 0o377; - -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_MAX = 41; - -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_MAX = PF_MAX; - -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; - -/// add event to kq (implies enable) -pub const EV_ADD = 0x0001; - -/// delete event from kq -pub const EV_DELETE = 0x0002; - -/// enable event -pub const EV_ENABLE = 0x0004; - -/// disable event (not reported) -pub const EV_DISABLE = 0x0008; - -/// only report one occurrence -pub const EV_ONESHOT = 0x0010; - -/// clear event state after reporting -pub const EV_CLEAR = 0x0020; - -/// force immediate event output -/// ... with or without EV_ERROR -/// ... use KEVENT_FLAG_ERROR_EVENTS -/// on syscalls supporting flags -pub const EV_RECEIPT = 0x0040; - -/// disable event after reporting -pub const EV_DISPATCH = 0x0080; - -pub const EVFILT_READ = -1; -pub const EVFILT_WRITE = -2; - -/// attached to aio requests -pub const EVFILT_AIO = -3; - -/// attached to vnodes -pub const EVFILT_VNODE = -4; - -/// attached to struct proc -pub const EVFILT_PROC = -5; - -/// attached to struct proc -pub const EVFILT_SIGNAL = -6; - -/// timers -pub const EVFILT_TIMER = -7; - -/// Process descriptors -pub const EVFILT_PROCDESC = -8; - -/// Filesystem events -pub const EVFILT_FS = -9; - -pub const EVFILT_LIO = -10; - -/// User events -pub const EVFILT_USER = -11; - -/// Sendfile events -pub const EVFILT_SENDFILE = -12; - -pub const EVFILT_EMPTY = -13; - -/// On input, NOTE_TRIGGER causes the event to be triggered for output. -pub const NOTE_TRIGGER = 0x01000000; - -/// ignore input fflags -pub const NOTE_FFNOP = 0x00000000; - -/// and fflags -pub const NOTE_FFAND = 0x40000000; - -/// or fflags -pub const NOTE_FFOR = 0x80000000; - -/// copy fflags -pub const NOTE_FFCOPY = 0xc0000000; - -/// mask for operations -pub const NOTE_FFCTRLMASK = 0xc0000000; -pub const NOTE_FFLAGSMASK = 0x00ffffff; - -/// low water mark -pub const NOTE_LOWAT = 0x00000001; - -/// behave like poll() -pub const NOTE_FILE_POLL = 0x00000002; - -/// vnode was removed -pub const NOTE_DELETE = 0x00000001; - -/// data contents changed -pub const NOTE_WRITE = 0x00000002; - -/// size increased -pub const NOTE_EXTEND = 0x00000004; - -/// attributes changed -pub const NOTE_ATTRIB = 0x00000008; - -/// link count changed -pub const NOTE_LINK = 0x00000010; - -/// vnode was renamed -pub const NOTE_RENAME = 0x00000020; - -/// vnode access was revoked -pub const NOTE_REVOKE = 0x00000040; - -/// vnode was opened -pub const NOTE_OPEN = 0x00000080; - -/// file closed, fd did not allow write -pub const NOTE_CLOSE = 0x00000100; - -/// file closed, fd did allow write -pub const NOTE_CLOSE_WRITE = 0x00000200; - -/// file was read -pub const NOTE_READ = 0x00000400; - -/// process exited -pub const NOTE_EXIT = 0x80000000; - -/// process forked -pub const NOTE_FORK = 0x40000000; - -/// process exec'd -pub const NOTE_EXEC = 0x20000000; - -/// mask for signal & exit status -pub const NOTE_PDATAMASK = 0x000fffff; -pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); - -/// data is seconds -pub const NOTE_SECONDS = 0x00000001; - -/// data is milliseconds -pub const NOTE_MSECONDS = 0x00000002; - -/// data is microseconds -pub const NOTE_USECONDS = 0x00000004; - -/// data is nanoseconds -pub const NOTE_NSECONDS = 0x00000008; - -/// timeout is absolute -pub const NOTE_ABSTIME = 0x00000010; - -pub const TCGETS = 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 = 0x5411; -pub const TIOCSTI = 0x5412; -pub const TIOCGWINSZ = 0x5413; -pub const TIOCSWINSZ = 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 = 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 sockaddr = c.sockaddr; -pub const sockaddr_in = c.sockaddr_in; -pub const sockaddr_in6 = c.sockaddr_in6; - -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); -} -fn signed(s: u32) i32 { - return @bitCast(i32, s); -} -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) & 0xff00) >> 8); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { - return WEXITSTATUS(s); -} -pub fn WIFEXITED(s: i32) bool { - return WTERMSIG(s) == 0; -} -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, (((unsigned(s) & 0xffff) *% 0x10001) >> 8)) > 0x7f00; -} -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; -} - -pub const winsize = extern struct { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16, -}; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) usize { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - return errnoWrap(c.dup2(old, new)); -} - -pub fn chdir(path: [*]const u8) usize { - return errnoWrap(c.chdir(path)); -} - -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return errnoWrap(c.execve(path, argv, envp)); -} - -pub fn fork() usize { - return errnoWrap(c.fork()); -} - -pub fn access(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.access(path, mode)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return errnoWrap(@bitCast(isize, c.getdents(fd, drip, count))); -} - -pub fn getdirentries(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize { - return errnoWrap(@bitCast(isize, c.getdirentries(fd, buf_ptr, buf_len, basep))); -} - -pub fn realpath(noalias filename: [*]const u8, noalias resolved_name: [*]u8) usize { - return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn isatty(fd: i32) bool { - return c.isatty(fd) != 0; -} - -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return errnoWrap(c.readlink(path, buf_ptr, buf_len)); -} - -pub fn mkdir(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.mkdir(path, mode)); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - const ptr_result = c.mmap( - @ptrCast(?*c_void, address), - length, - @bitCast(c_int, @intCast(c_uint, prot)), - @bitCast(c_int, c_uint(flags)), - fd, - offset, - ); - const isize_result = @bitCast(isize, @ptrToInt(ptr_result)); - return errnoWrap(isize_result); -} - -pub fn munmap(address: usize, length: usize) usize { - return errnoWrap(c.munmap(@intToPtr(?*c_void, address), length)); -} - -pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize { - return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte)); -} - -pub fn rmdir(path: [*]const u8) usize { - return errnoWrap(c.rmdir(path)); -} - -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.symlink(existing, new)); -} - -pub fn pread(fd: i32, buf: [*]u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pread(fd, @ptrCast(*c_void, buf), nbyte, offset)); -} - -pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize { - return errnoWrap(c.preadv(fd, @ptrCast(*const c_void, iov), @intCast(c_int, count), offset)); -} - -pub fn pipe(fd: *[2]i32) usize { - return pipe2(fd, 0); -} - -pub fn pipe2(fd: *[2]i32, flags: u32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.pipe2(@ptrCast(*[2]c_int, fd), flags)); -} - -pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize { - return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte)); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pwrite(fd, @ptrCast(*const c_void, buf), nbyte, offset)); -} - -pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize { - return errnoWrap(c.pwritev(fd, @ptrCast(*const c_void, iov), @intCast(c_int, count), offset)); -} - -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.rename(old, new)); -} - -pub fn open(path: [*]const u8, flags: u32, mode: usize) usize { - return errnoWrap(c.open(path, @bitCast(c_int, flags), mode)); -} - -pub fn create(path: [*]const u8, perm: usize) usize { - return arch.syscall2(SYS_creat, @ptrToInt(path), perm); -} - -pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize { - return errnoWrap(c.openat(@bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode)); -} - -pub fn close(fd: i32) usize { - return errnoWrap(c.close(fd)); -} - -pub fn lseek(fd: i32, offset: isize, whence: c_int) usize { - return errnoWrap(c.lseek(fd, offset, whence)); -} - -pub fn exit(code: i32) noreturn { - c.exit(code); -} - -pub fn kill(pid: i32, sig: i32) usize { - return errnoWrap(c.kill(pid, sig)); -} - -pub fn unlink(path: [*]const u8) usize { - return errnoWrap(c.unlink(path)); -} - -pub fn waitpid(pid: i32, status: *i32, options: u32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options))); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return errnoWrap(c.nanosleep(req, rem)); -} - -pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { - return errnoWrap(c.clock_gettime(clk_id, tp)); -} - -pub fn clock_getres(clk_id: i32, tp: *timespec) usize { - return errnoWrap(c.clock_getres(clk_id, tp)); -} - -pub fn setuid(uid: u32) usize { - return errnoWrap(c.setuid(uid)); -} - -pub fn setgid(gid: u32) usize { - return errnoWrap(c.setgid(gid)); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return errnoWrap(c.setreuid(ruid, euid)); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return errnoWrap(c.setregid(rgid, egid)); -} - -const NSIG = 32; - -pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); -pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); -pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = extern struct { - /// signal handler - __sigaction_u: extern union { - __sa_handler: extern fn (i32) void, - __sa_sigaction: extern fn (i32, *__siginfo, usize) void, - }, - - /// see signal options - sa_flags: u32, - - /// signal mask to apply - sa_mask: sigset_t, -}; - -pub const _SIG_WORDS = 4; -pub const _SIG_MAXSIG = 128; - -pub inline fn _SIG_IDX(sig: usize) usize { - return sig - 1; -} -pub inline fn _SIG_WORD(sig: usize) usize { - return_SIG_IDX(sig) >> 5; -} -pub inline fn _SIG_BIT(sig: usize) usize { - return 1 << (_SIG_IDX(sig) & 31); -} -pub inline fn _SIG_VALID(sig: usize) usize { - return sig <= _SIG_MAXSIG and sig > 0; -} - -pub const sigset_t = extern struct { - __bits: [_SIG_WORDS]u32, -}; - -pub fn raise(sig: i32) usize { - return errnoWrap(c.raise(sig)); -} - -pub const Stat = c.Stat; -pub const dirent = c.dirent; -pub const timespec = c.timespec; - -pub fn fstat(fd: i32, buf: *c.Stat) usize { - return errnoWrap(c.fstat(fd, buf)); -} -pub const iovec = extern struct { - iov_base: [*]u8, - iov_len: usize, -}; - -pub const iovec_const = extern struct { - iov_base: [*]const u8, - iov_len: usize, -}; - -// TODO avoid libc dependency -pub fn kqueue() usize { - return errnoWrap(c.kqueue()); -} - -// TODO avoid libc dependency -pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize { - return errnoWrap(c.kevent( - kq, - changelist.ptr, - @intCast(c_int, changelist.len), - eventlist.ptr, - @intCast(c_int, eventlist.len), - timeout, - )); -} - -// TODO avoid libc dependency -pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen)); -} - -// TODO avoid libc dependency -pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen)); -} - -// TODO avoid libc dependency -pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize { - return errnoWrap(c.sysctlnametomib(name, wibp, sizep)); -} - -// TODO avoid libc dependency - -/// Takes the return value from a syscall and formats it back in the way -/// that the kernel represents it to libc. Errno was a mistake, let's make -/// it go away forever. -fn errnoWrap(value: isize) usize { - return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value); -} +const builtin = @import("builtin"); +pub const is_the_target = builtin.os == .freebsd; +pub use std.c; diff --git a/std/os/freebsd/errno.zig b/std/os/freebsd/errno.zig @@ -1,121 +0,0 @@ -pub const EPERM = 1; // Operation not permitted -pub const ENOENT = 2; // No such file or directory -pub const ESRCH = 3; // No such process -pub const EINTR = 4; // Interrupted system call -pub const EIO = 5; // Input/output error -pub const ENXIO = 6; // Device not configured -pub const E2BIG = 7; // Argument list too long -pub const ENOEXEC = 8; // Exec format error -pub const EBADF = 9; // Bad file descriptor -pub const ECHILD = 10; // No child processes -pub const EDEADLK = 11; // Resource deadlock avoided -// 11 was EAGAIN -pub const ENOMEM = 12; // Cannot allocate memory -pub const EACCES = 13; // Permission denied -pub const EFAULT = 14; // Bad address -pub const ENOTBLK = 15; // Block device required -pub const EBUSY = 16; // Device busy -pub const EEXIST = 17; // File exists -pub const EXDEV = 18; // Cross-device link -pub const ENODEV = 19; // Operation not supported by device -pub const ENOTDIR = 20; // Not a directory -pub const EISDIR = 21; // Is a directory -pub const EINVAL = 22; // Invalid argument -pub const ENFILE = 23; // Too many open files in system -pub const EMFILE = 24; // Too many open files -pub const ENOTTY = 25; // Inappropriate ioctl for device -pub const ETXTBSY = 26; // Text file busy -pub const EFBIG = 27; // File too large -pub const ENOSPC = 28; // No space left on device -pub const ESPIPE = 29; // Illegal seek -pub const EROFS = 30; // Read-only filesystem -pub const EMLINK = 31; // Too many links -pub const EPIPE = 32; // Broken pipe - -// math software -pub const EDOM = 33; // Numerical argument out of domain -pub const ERANGE = 34; // Result too large - -// non-blocking and interrupt i/o -pub const EAGAIN = 35; // Resource temporarily unavailable -pub const EWOULDBLOCK = EAGAIN; // Operation would block -pub const EINPROGRESS = 36; // Operation now in progress -pub const EALREADY = 37; // Operation already in progress - -// ipc/network software -- argument errors -pub const ENOTSOCK = 38; // Socket operation on non-socket -pub const EDESTADDRREQ = 39; // Destination address required -pub const EMSGSIZE = 40; // Message too long -pub const EPROTOTYPE = 41; // Protocol wrong type for socket -pub const ENOPROTOOPT = 42; // Protocol not available -pub const EPROTONOSUPPORT = 43; // Protocol not supported -pub const ESOCKTNOSUPPORT = 44; // Socket type not supported -pub const EOPNOTSUPP = 45; // Operation not supported -pub const ENOTSUP = EOPNOTSUPP; // Operation not supported -pub const EPFNOSUPPORT = 46; // Protocol family not supported -pub const EAFNOSUPPORT = 47; // Address family not supported by protocol family -pub const EADDRINUSE = 48; // Address already in use -pub const EADDRNOTAVAIL = 49; // Can't assign requested address - -// ipc/network software -- operational errors -pub const ENETDOWN = 50; // Network is down -pub const ENETUNREACH = 51; // Network is unreachable -pub const ENETRESET = 52; // Network dropped connection on reset -pub const ECONNABORTED = 53; // Software caused connection abort -pub const ECONNRESET = 54; // Connection reset by peer -pub const ENOBUFS = 55; // No buffer space available -pub const EISCONN = 56; // Socket is already connected -pub const ENOTCONN = 57; // Socket is not connected -pub const ESHUTDOWN = 58; // Can't send after socket shutdown -pub const ETOOMANYREFS = 59; // Too many references: can't splice -pub const ETIMEDOUT = 60; // Operation timed out -pub const ECONNREFUSED = 61; // Connection refused - -pub const ELOOP = 62; // Too many levels of symbolic links -pub const ENAMETOOLONG = 63; // File name too long - -// should be rearranged -pub const EHOSTDOWN = 64; // Host is down -pub const EHOSTUNREACH = 65; // No route to host -pub const ENOTEMPTY = 66; // Directory not empty - -// quotas & mush -pub const EPROCLIM = 67; // Too many processes -pub const EUSERS = 68; // Too many users -pub const EDQUOT = 69; // Disc quota exceeded - -// Network File System -pub const ESTALE = 70; // Stale NFS file handle -pub const EREMOTE = 71; // Too many levels of remote in path -pub const EBADRPC = 72; // RPC struct is bad -pub const ERPCMISMATCH = 73; // RPC version wrong -pub const EPROGUNAVAIL = 74; // RPC prog. not avail -pub const EPROGMISMATCH = 75; // Program version wrong -pub const EPROCUNAVAIL = 76; // Bad procedure for program - -pub const ENOLCK = 77; // No locks available -pub const ENOSYS = 78; // Function not implemented - -pub const EFTYPE = 79; // Inappropriate file type or format -pub const EAUTH = 80; // Authentication error -pub const ENEEDAUTH = 81; // Need authenticator -pub const EIDRM = 82; // Identifier removed -pub const ENOMSG = 83; // No message of desired type -pub const EOVERFLOW = 84; // Value too large to be stored in data type -pub const ECANCELED = 85; // Operation canceled -pub const EILSEQ = 86; // Illegal byte sequence -pub const ENOATTR = 87; // Attribute not found - -pub const EDOOFUS = 88; // Programming error - -pub const EBADMSG = 89; // Bad message -pub const EMULTIHOP = 90; // Multihop attempted -pub const ENOLINK = 91; // Link has been severed -pub const EPROTO = 92; // Protocol error - -pub const ENOTCAPABLE = 93; // Capabilities insufficient -pub const ECAPMODE = 94; // Not permitted in capability mode -pub const ENOTRECOVERABLE = 95; // State not recoverable -pub const EOWNERDEAD = 96; // Previous owner died - -pub const ELAST = 96; // Must be equal largest errno diff --git a/std/os/linux.zig b/std/os/linux.zig @@ -7,700 +7,17 @@ pub const tls = @import("linux/tls.zig"); const vdso = @import("linux/vdso.zig"); const dl = @import("../dynamic_library.zig"); pub use switch (builtin.arch) { - builtin.Arch.x86_64 => @import("linux/x86_64.zig"), - builtin.Arch.i386 => @import("linux/i386.zig"), - builtin.Arch.aarch64 => @import("linux/arm64.zig"), - else => @compileError("unsupported arch"), + .x86_64 => @import("linux/x86_64.zig"), + .aarch64 => @import("linux/arm64.zig"), + else => struct {}, }; pub const is_the_target = builtin.os == .linux; -pub const errno_codes = @import("linux/errno.zig"); -pub use errno_codes; +pub const posix = @import("linux/posix.zig"); +pub use posix; /// See `std.os.posix.getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; -pub const PATH_MAX = 4096; -pub const IOV_MAX = 1024; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -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_PRIVATE_FLAG = 128; - -pub const FUTEX_CLOCK_REALTIME = 256; - -pub const PROT_NONE = 0; -pub const PROT_READ = 1; -pub const PROT_WRITE = 2; -pub const PROT_EXEC = 4; -pub const PROT_GROWSDOWN = 0x01000000; -pub const PROT_GROWSUP = 0x02000000; - -pub const MAP_FAILED = maxInt(usize); -pub const MAP_SHARED = 0x01; -pub const MAP_PRIVATE = 0x02; -pub const MAP_TYPE = 0x0f; -pub const MAP_FIXED = 0x10; -pub const MAP_ANONYMOUS = 0x20; -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_POPULATE = 0x8000; -pub const MAP_NONBLOCK = 0x10000; -pub const MAP_STACK = 0x20000; -pub const MAP_HUGETLB = 0x40000; -pub const MAP_FILE = 0; - -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; - -pub const SA_NOCLDSTOP = 1; -pub const SA_NOCLDWAIT = 2; -pub const SA_SIGINFO = 4; -pub const SA_ONSTACK = 0x08000000; -pub const SA_RESTART = 0x10000000; -pub const SA_NODEFER = 0x40000000; -pub const SA_RESETHAND = 0x80000000; -pub const SA_RESTORER = 0x04000000; - -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 SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -pub const SIG_BLOCK = 0; -pub const SIG_UNBLOCK = 1; -pub const SIG_SETMASK = 2; - -pub const PROTO_ip = 0o000; -pub const PROTO_icmp = 0o001; -pub const PROTO_igmp = 0o002; -pub const PROTO_ggp = 0o003; -pub const PROTO_ipencap = 0o004; -pub const PROTO_st = 0o005; -pub const PROTO_tcp = 0o006; -pub const PROTO_egp = 0o010; -pub const PROTO_pup = 0o014; -pub const PROTO_udp = 0o021; -pub const PROTO_hmp = 0o024; -pub const PROTO_xns_idp = 0o026; -pub const PROTO_rdp = 0o033; -pub const PROTO_iso_tp4 = 0o035; -pub const PROTO_xtp = 0o044; -pub const PROTO_ddp = 0o045; -pub const PROTO_idpr_cmtp = 0o046; -pub const PROTO_ipv6 = 0o051; -pub const PROTO_ipv6_route = 0o053; -pub const PROTO_ipv6_frag = 0o054; -pub const PROTO_idrp = 0o055; -pub const PROTO_rsvp = 0o056; -pub const PROTO_gre = 0o057; -pub const PROTO_esp = 0o062; -pub const PROTO_ah = 0o063; -pub const PROTO_skip = 0o071; -pub const PROTO_ipv6_icmp = 0o072; -pub const PROTO_ipv6_nonxt = 0o073; -pub const PROTO_ipv6_opts = 0o074; -pub const PROTO_rspf = 0o111; -pub const PROTO_vmtp = 0o121; -pub const PROTO_ospf = 0o131; -pub const PROTO_ipip = 0o136; -pub const PROTO_encap = 0o142; -pub const PROTO_pim = 0o147; -pub const PROTO_raw = 0o377; - -pub const SHUT_RD = 0; -pub const SHUT_WR = 1; -pub const SHUT_RDWR = 2; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 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 = 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_MAX = 44; - -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_MAX = PF_MAX; - -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_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 = 29; -pub const SCM_TIMESTAMP = SO_TIMESTAMP; - -pub const SO_PEERSEC = 31; -pub const SO_PASSSEC = 34; -pub const SO_TIMESTAMPNS = 35; -pub const SCM_TIMESTAMPNS = SO_TIMESTAMPNS; -pub const SO_MARK = 36; -pub const SO_TIMESTAMPING = 37; -pub const SCM_TIMESTAMPING = SO_TIMESTAMPING; -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 SOL_SOCKET = 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 SOMAXCONN = 128; - -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 = 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 = 0x5411; -pub const TIOCSTI = 0x5412; -pub const TIOCGWINSZ = 0x5413; -pub const TIOCSWINSZ = 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 = 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 = 0x100; -pub const EPOLLWRBAND = 0x200; -pub const EPOLLMSG = 0x400; -pub const EPOLLERR = 0x008; -pub const EPOLLHUP = 0x010; -pub const EPOLLRDHUP = 0x2000; -pub const EPOLLEXCLUSIVE = (u32(1) << 28); -pub const EPOLLWAKEUP = (u32(1) << 29); -pub const EPOLLONESHOT = (u32(1) << 30); -pub const EPOLLET = (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_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; - -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 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); - -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); -} -fn signed(s: u32) i32 { - return @bitCast(i32, s); -} -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) & 0xff00) >> 8); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { - return WEXITSTATUS(s); -} -pub fn WIFEXITED(s: i32) bool { - return WTERMSIG(s) == 0; -} -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; -} -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; -} - -pub const winsize = extern struct { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16, -}; - /// Get the errno from a syscall return value, or 0 for no error. pub fn getErrno(r: usize) u12 { const signed_r = @bitCast(isize, r); @@ -708,7 +25,7 @@ pub fn getErrno(r: usize) u12 { } pub fn dup2(old: i32, new: i32) usize { - return dup3(old, new, 0); + return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); } pub fn dup3(old: i32, new: i32, flags: u32) usize { @@ -731,7 +48,7 @@ pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[* } pub fn fork() usize { - return clone2(SIGCHLD, 0); + return syscall0(SYS_fork); } /// This must be inline, and inline call the syscall function, because if the @@ -755,6 +72,10 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { return syscall2(SYS_getcwd, @ptrToInt(buf), size); } +pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); } @@ -773,7 +94,7 @@ pub fn inotify_rm_watch(fd: i32, wd: i32) usize { // TODO https://github.com/ziglang/zig/issues/265 pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return readlinkat(AT_FDCWD, path, buf_ptr, buf_len); + return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); } // TODO https://github.com/ziglang/zig/issues/265 @@ -783,7 +104,7 @@ pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, // TODO https://github.com/ziglang/zig/issues/265 pub fn mkdir(path: [*]const u8, mode: u32) usize { - return mkdirat(AT_FDCWD, path, mode); + return syscall2(SYS_mkdir, @ptrToInt(path), mode); } // TODO https://github.com/ziglang/zig/issues/265 @@ -840,12 +161,12 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us // TODO https://github.com/ziglang/zig/issues/265 pub fn rmdir(path: [*]const u8) usize { - return unlinkat(AT_FDCWD, path, AT_REMOVEDIR); + return syscall1(SYS_rmdir, @ptrToInt(path)); } // TODO https://github.com/ziglang/zig/issues/265 pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - return symlinkat(existing, AT_FDCWD, new); + return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -860,9 +181,10 @@ pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { // TODO https://github.com/ziglang/zig/issues/265 pub fn access(path: [*]const u8, mode: u32) usize { - return faccessat(AT_FDCWD, path, mode); + return syscall2(SYS_access, @ptrToInt(path), mode); } +// TODO https://github.com/ziglang/zig/issues/265 pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); } @@ -885,7 +207,7 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { // TODO https://github.com/ziglang/zig/issues/265 pub fn rename(old: [*]const u8, new: [*]const u8) usize { - return renameat2(AT_FDCWD, old, AT_FDCWD, new, 0); + return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -895,7 +217,7 @@ pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const // TODO https://github.com/ziglang/zig/issues/265 pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { - return openat(AT_FDCWD, path, flags, perm); + return syscall3(SYS_open, @ptrToInt(path), flags, perm); } // TODO https://github.com/ziglang/zig/issues/265 @@ -947,7 +269,7 @@ pub fn kill(pid: i32, sig: i32) usize { // TODO https://github.com/ziglang/zig/issues/265 pub fn unlink(path: [*]const u8) usize { - return unlinkat(AT_FDCWD, path, 0); + return syscall1(SYS_unlink, @ptrToInt(path)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -1113,30 +435,6 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti return 0; } -const NSIG = 65; -const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []u32{ 0xffffffff, 0xffffffff }; -const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; - -const k_sigaction = extern struct { - handler: extern fn (i32) void, - flags: usize, - restorer: extern fn () void, - mask: [2]u32, -}; - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = struct { - handler: extern fn (i32) void, - mask: sigset_t, - flags: u32, -}; - -pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); -pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); -pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); -pub const empty_sigset = []usize{0} ** sigset_t.len; - fn blockAllSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); } @@ -1159,56 +457,6 @@ pub fn sigismember(set: *const sigset_t, sig: u6) bool { return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; } -pub const in_port_t = u16; -pub const sa_family_t = u16; -pub const socklen_t = u32; - -/// This intentionally only has ip4 and ip6 -pub const sockaddr = extern union { - in: sockaddr_in, - in6: sockaddr_in6, -}; - -pub const sockaddr_in = extern struct { - family: sa_family_t, - port: in_port_t, - addr: u32, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - family: sa_family_t, - port: in_port_t, - flowinfo: u32, - addr: [16]u8, - scope_id: u32, -}; - -pub const sockaddr_un = extern struct { - family: sa_family_t, - path: [108]u8, -}; - -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 mmsghdr = extern struct { - msg_hdr: msghdr, - msg_len: u32, -}; - -pub const mmsghdr_const = extern struct { - msg_hdr: msghdr_const, - msg_len: u32, -}; - pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); } @@ -1319,12 +567,12 @@ pub fn fstat(fd: i32, stat_buf: *Stat) usize { // TODO https://github.com/ziglang/zig/issues/265 pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { - return fstatat(AT_FDCWD, pathname, statbuf, AT_NO_AUTOMOUNT); + return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); } // TODO https://github.com/ziglang/zig/issues/265 pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { - return fstatat(AF_FDCWD, pathname, statbuf, AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT); + return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -1395,26 +643,6 @@ pub fn sched_getaffinity(pid: i32, set: []usize) usize { return syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), set.len * @sizeOf(usize), @ptrToInt(set.ptr)); } -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 = if (builtin.arch != .x86_64) - extern struct { - events: u32, - data: epoll_data, - } -else - packed struct { - events: u32, - data: epoll_data, - }; - pub fn epoll_create() usize { return epoll_create1(0); } @@ -1428,7 +656,13 @@ pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { } pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { - return epoll_pwait(epoll_fd, events, maxevents, timeout, null); + return syscall4( + SYS_epoll_wait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + maxevents, + @bitCast(usize, isize(timeout)), + ); } pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { @@ -1464,112 +698,6 @@ pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_va return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); } -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(u8: x) bool { - return x >= 0 and x <= CAP_LAST_CAP; -} - -pub fn CAP_TO_MASK(cap: u8) u32 { - return u32(1) << 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 fn unshare(flags: usize) usize { return syscall1(SYS_unshare, flags); } @@ -1582,29 +710,6 @@ pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); } -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 const dl_phdr_info = extern struct { - dlpi_addr: usize, - dlpi_name: ?[*]const u8, - dlpi_phdr: [*]elf.Phdr, - dlpi_phnum: u16, -}; - // XXX: This should be weak extern const __ehdr_start: elf.Ehdr = undefined; diff --git a/std/os/linux/arm64.zig b/std/os/linux/arm64.zig @@ -1,344 +1,3 @@ -const std = @import("../../std.zig"); -const linux = std.os.linux; -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; -const iovec_const = linux.iovec_const; - -pub const SYS_io_setup = 0; -pub const SYS_io_destroy = 1; -pub const SYS_io_submit = 2; -pub const SYS_io_cancel = 3; -pub const SYS_io_getevents = 4; -pub const SYS_setxattr = 5; -pub const SYS_lsetxattr = 6; -pub const SYS_fsetxattr = 7; -pub const SYS_getxattr = 8; -pub const SYS_lgetxattr = 9; -pub const SYS_fgetxattr = 10; -pub const SYS_listxattr = 11; -pub const SYS_llistxattr = 12; -pub const SYS_flistxattr = 13; -pub const SYS_removexattr = 14; -pub const SYS_lremovexattr = 15; -pub const SYS_fremovexattr = 16; -pub const SYS_getcwd = 17; -pub const SYS_lookup_dcookie = 18; -pub const SYS_eventfd2 = 19; -pub const SYS_epoll_create1 = 20; -pub const SYS_epoll_ctl = 21; -pub const SYS_epoll_pwait = 22; -pub const SYS_dup = 23; -pub const SYS_dup3 = 24; -pub const SYS_fcntl = 25; -pub const SYS_inotify_init1 = 26; -pub const SYS_inotify_add_watch = 27; -pub const SYS_inotify_rm_watch = 28; -pub const SYS_ioctl = 29; -pub const SYS_ioprio_set = 30; -pub const SYS_ioprio_get = 31; -pub const SYS_flock = 32; -pub const SYS_mknodat = 33; -pub const SYS_mkdirat = 34; -pub const SYS_unlinkat = 35; -pub const SYS_symlinkat = 36; -pub const SYS_linkat = 37; -pub const SYS_renameat = 38; -pub const SYS_umount2 = 39; -pub const SYS_mount = 40; -pub const SYS_pivot_root = 41; -pub const SYS_nfsservctl = 42; -pub const SYS_statfs = 43; -pub const SYS_fstatfs = 44; -pub const SYS_truncate = 45; -pub const SYS_ftruncate = 46; -pub const SYS_fallocate = 47; -pub const SYS_faccessat = 48; -pub const SYS_chdir = 49; -pub const SYS_fchdir = 50; -pub const SYS_chroot = 51; -pub const SYS_fchmod = 52; -pub const SYS_fchmodat = 53; -pub const SYS_fchownat = 54; -pub const SYS_fchown = 55; -pub const SYS_openat = 56; -pub const SYS_close = 57; -pub const SYS_vhangup = 58; -pub const SYS_pipe2 = 59; -pub const SYS_quotactl = 60; -pub const SYS_getdents64 = 61; -pub const SYS_lseek = 62; -pub const SYS_read = 63; -pub const SYS_write = 64; -pub const SYS_readv = 65; -pub const SYS_writev = 66; -pub const SYS_pread64 = 67; -pub const SYS_pwrite64 = 68; -pub const SYS_preadv = 69; -pub const SYS_pwritev = 70; -pub const SYS_pselect6 = 72; -pub const SYS_ppoll = 73; -pub const SYS_signalfd4 = 74; -pub const SYS_vmsplice = 75; -pub const SYS_splice = 76; -pub const SYS_tee = 77; -pub const SYS_readlinkat = 78; -pub const SYS_fstatat = 79; -pub const SYS_fstat = 80; -pub const SYS_sync = 81; -pub const SYS_fsync = 82; -pub const SYS_fdatasync = 83; -pub const SYS_sync_file_range2 = 84; -pub const SYS_sync_file_range = 84; -pub const SYS_timerfd_create = 85; -pub const SYS_timerfd_settime = 86; -pub const SYS_timerfd_gettime = 87; -pub const SYS_utimensat = 88; -pub const SYS_acct = 89; -pub const SYS_capget = 90; -pub const SYS_capset = 91; -pub const SYS_personality = 92; -pub const SYS_exit = 93; -pub const SYS_exit_group = 94; -pub const SYS_waitid = 95; -pub const SYS_set_tid_address = 96; -pub const SYS_unshare = 97; -pub const SYS_futex = 98; -pub const SYS_set_robust_list = 99; -pub const SYS_get_robust_list = 100; -pub const SYS_nanosleep = 101; -pub const SYS_getitimer = 102; -pub const SYS_setitimer = 103; -pub const SYS_kexec_load = 104; -pub const SYS_init_module = 105; -pub const SYS_delete_module = 106; -pub const SYS_timer_create = 107; -pub const SYS_timer_gettime = 108; -pub const SYS_timer_getoverrun = 109; -pub const SYS_timer_settime = 110; -pub const SYS_timer_delete = 111; -pub const SYS_clock_settime = 112; -pub const SYS_clock_gettime = 113; -pub const SYS_clock_getres = 114; -pub const SYS_clock_nanosleep = 115; -pub const SYS_syslog = 116; -pub const SYS_ptrace = 117; -pub const SYS_sched_setparam = 118; -pub const SYS_sched_setscheduler = 119; -pub const SYS_sched_getscheduler = 120; -pub const SYS_sched_getparam = 121; -pub const SYS_sched_setaffinity = 122; -pub const SYS_sched_getaffinity = 123; -pub const SYS_sched_yield = 124; -pub const SYS_sched_get_priority_max = 125; -pub const SYS_sched_get_priority_min = 126; -pub const SYS_sched_rr_get_interval = 127; -pub const SYS_restart_syscall = 128; -pub const SYS_kill = 129; -pub const SYS_tkill = 130; -pub const SYS_tgkill = 131; -pub const SYS_sigaltstack = 132; -pub const SYS_rt_sigsuspend = 133; -pub const SYS_rt_sigaction = 134; -pub const SYS_rt_sigprocmask = 135; -pub const SYS_rt_sigpending = 136; -pub const SYS_rt_sigtimedwait = 137; -pub const SYS_rt_sigqueueinfo = 138; -pub const SYS_rt_sigreturn = 139; -pub const SYS_setpriority = 140; -pub const SYS_getpriority = 141; -pub const SYS_reboot = 142; -pub const SYS_setregid = 143; -pub const SYS_setgid = 144; -pub const SYS_setreuid = 145; -pub const SYS_setuid = 146; -pub const SYS_setresuid = 147; -pub const SYS_getresuid = 148; -pub const SYS_setresgid = 149; -pub const SYS_getresgid = 150; -pub const SYS_setfsuid = 151; -pub const SYS_setfsgid = 152; -pub const SYS_times = 153; -pub const SYS_setpgid = 154; -pub const SYS_getpgid = 155; -pub const SYS_getsid = 156; -pub const SYS_setsid = 157; -pub const SYS_getgroups = 158; -pub const SYS_setgroups = 159; -pub const SYS_uname = 160; -pub const SYS_sethostname = 161; -pub const SYS_setdomainname = 162; -pub const SYS_getrlimit = 163; -pub const SYS_setrlimit = 164; -pub const SYS_getrusage = 165; -pub const SYS_umask = 166; -pub const SYS_prctl = 167; -pub const SYS_getcpu = 168; -pub const SYS_gettimeofday = 169; -pub const SYS_settimeofday = 170; -pub const SYS_adjtimex = 171; -pub const SYS_getpid = 172; -pub const SYS_getppid = 173; -pub const SYS_getuid = 174; -pub const SYS_geteuid = 175; -pub const SYS_getgid = 176; -pub const SYS_getegid = 177; -pub const SYS_gettid = 178; -pub const SYS_sysinfo = 179; -pub const SYS_mq_open = 180; -pub const SYS_mq_unlink = 181; -pub const SYS_mq_timedsend = 182; -pub const SYS_mq_timedreceive = 183; -pub const SYS_mq_notify = 184; -pub const SYS_mq_getsetattr = 185; -pub const SYS_msgget = 186; -pub const SYS_msgctl = 187; -pub const SYS_msgrcv = 188; -pub const SYS_msgsnd = 189; -pub const SYS_semget = 190; -pub const SYS_semctl = 191; -pub const SYS_semtimedop = 192; -pub const SYS_semop = 193; -pub const SYS_shmget = 194; -pub const SYS_shmctl = 195; -pub const SYS_shmat = 196; -pub const SYS_shmdt = 197; -pub const SYS_socket = 198; -pub const SYS_socketpair = 199; -pub const SYS_bind = 200; -pub const SYS_listen = 201; -pub const SYS_accept = 202; -pub const SYS_connect = 203; -pub const SYS_getsockname = 204; -pub const SYS_getpeername = 205; -pub const SYS_sendto = 206; -pub const SYS_recvfrom = 207; -pub const SYS_setsockopt = 208; -pub const SYS_getsockopt = 209; -pub const SYS_shutdown = 210; -pub const SYS_sendmsg = 211; -pub const SYS_recvmsg = 212; -pub const SYS_readahead = 213; -pub const SYS_brk = 214; -pub const SYS_munmap = 215; -pub const SYS_mremap = 216; -pub const SYS_add_key = 217; -pub const SYS_request_key = 218; -pub const SYS_keyctl = 219; -pub const SYS_clone = 220; -pub const SYS_execve = 221; -pub const SYS_mmap = 222; -pub const SYS_fadvise64 = 223; -pub const SYS_swapon = 224; -pub const SYS_swapoff = 225; -pub const SYS_mprotect = 226; -pub const SYS_msync = 227; -pub const SYS_mlock = 228; -pub const SYS_munlock = 229; -pub const SYS_mlockall = 230; -pub const SYS_munlockall = 231; -pub const SYS_mincore = 232; -pub const SYS_madvise = 233; -pub const SYS_remap_file_pages = 234; -pub const SYS_mbind = 235; -pub const SYS_get_mempolicy = 236; -pub const SYS_set_mempolicy = 237; -pub const SYS_migrate_pages = 238; -pub const SYS_move_pages = 239; -pub const SYS_rt_tgsigqueueinfo = 240; -pub const SYS_perf_event_open = 241; -pub const SYS_accept4 = 242; -pub const SYS_recvmmsg = 243; -pub const SYS_arch_specific_syscall = 244; -pub const SYS_wait4 = 260; -pub const SYS_prlimit64 = 261; -pub const SYS_fanotify_init = 262; -pub const SYS_fanotify_mark = 263; -pub const SYS_clock_adjtime = 266; -pub const SYS_syncfs = 267; -pub const SYS_setns = 268; -pub const SYS_sendmmsg = 269; -pub const SYS_process_vm_readv = 270; -pub const SYS_process_vm_writev = 271; -pub const SYS_kcmp = 272; -pub const SYS_finit_module = 273; -pub const SYS_sched_setattr = 274; -pub const SYS_sched_getattr = 275; -pub const SYS_renameat2 = 276; -pub const SYS_seccomp = 277; -pub const SYS_getrandom = 278; -pub const SYS_memfd_create = 279; -pub const SYS_bpf = 280; -pub const SYS_execveat = 281; -pub const SYS_userfaultfd = 282; -pub const SYS_membarrier = 283; -pub const SYS_mlock2 = 284; -pub const SYS_copy_file_range = 285; -pub const SYS_preadv2 = 286; -pub const SYS_pwritev2 = 287; -pub const SYS_pkey_mprotect = 288; -pub const SYS_pkey_alloc = 289; -pub const SYS_pkey_free = 290; -pub const SYS_statx = 291; -pub const SYS_io_pgetevents = 292; -pub const SYS_rseq = 293; -pub const SYS_kexec_file_load = 294; -pub const SYS_pidfd_send_signal = 424; -pub const SYS_io_uring_setup = 425; -pub const SYS_io_uring_enter = 426; -pub const SYS_io_uring_register = 427; - -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; - -pub const AT_FDCWD = -100; -pub const AT_SYMLINK_NOFOLLOW = 0x100; -pub const AT_REMOVEDIR = 0x200; -pub const AT_SYMLINK_FOLLOW = 0x400; -pub const AT_NO_AUTOMOUNT = 0x800; -pub const AT_EMPTY_PATH = 0x1000; - -pub const VDSO_USEFUL = true; -pub const VDSO_CGT_SYM = "__kernel_clock_gettime"; -pub const VDSO_CGT_VER = "LINUX_2.6.39"; - pub fn syscall0(number: usize) usize { return asm volatile ("svc #0" : [ret] "={x0}" (-> usize) @@ -419,65 +78,3 @@ pub fn syscall6( /// This matches the libc clone function. pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize; - -pub const msghdr = extern struct { - msg_name: ?*sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec, - msg_iovlen: i32, - __pad1: i32, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t, - 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, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t, - msg_flags: i32, -}; - -/// Renamed to Stat to not conflict with the stat function. -pub const Stat = extern struct { - dev: u64, - ino: u64, - nlink: usize, - - mode: u32, - uid: u32, - gid: u32, - __pad0: u32, - rdev: u64, - size: i64, - blksize: isize, - blocks: i64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - __unused: [3]isize, -}; - -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; diff --git a/std/os/linux/posix.zig b/std/os/linux/posix.zig @@ -0,0 +1,917 @@ +// Declarations that are intended to be imported into the POSIX namespace. +// This includes Linux-only APIs. + +pub use @import("errno.zig"); +pub use switch (builtin.arch) { + .x86_64 => @import("posix/x86_64.zig"), + .aarch64 => @import("posix/arm64.zig"), + else => struct {}, +}; + +pub const pid_t = i32; +pub const fd_t = i32; + +pub const PATH_MAX = 4096; +pub const IOV_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +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_PRIVATE_FLAG = 128; + +pub const FUTEX_CLOCK_REALTIME = 256; + +pub const PROT_NONE = 0; +pub const PROT_READ = 1; +pub const PROT_WRITE = 2; +pub const PROT_EXEC = 4; +pub const PROT_GROWSDOWN = 0x01000000; +pub const PROT_GROWSUP = 0x02000000; + +pub const MAP_FAILED = maxInt(usize); +pub const MAP_SHARED = 0x01; +pub const MAP_PRIVATE = 0x02; +pub const MAP_TYPE = 0x0f; +pub const MAP_FIXED = 0x10; +pub const MAP_ANONYMOUS = 0x20; +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_POPULATE = 0x8000; +pub const MAP_NONBLOCK = 0x10000; +pub const MAP_STACK = 0x20000; +pub const MAP_HUGETLB = 0x40000; +pub const MAP_FILE = 0; + +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; + +pub const SA_NOCLDSTOP = 1; +pub const SA_NOCLDWAIT = 2; +pub const SA_SIGINFO = 4; +pub const SA_ONSTACK = 0x08000000; +pub const SA_RESTART = 0x10000000; +pub const SA_NODEFER = 0x40000000; +pub const SA_RESETHAND = 0x80000000; +pub const SA_RESTORER = 0x04000000; + +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 SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + +pub const SIG_BLOCK = 0; +pub const SIG_UNBLOCK = 1; +pub const SIG_SETMASK = 2; + +pub const PROTO_ip = 0o000; +pub const PROTO_icmp = 0o001; +pub const PROTO_igmp = 0o002; +pub const PROTO_ggp = 0o003; +pub const PROTO_ipencap = 0o004; +pub const PROTO_st = 0o005; +pub const PROTO_tcp = 0o006; +pub const PROTO_egp = 0o010; +pub const PROTO_pup = 0o014; +pub const PROTO_udp = 0o021; +pub const PROTO_hmp = 0o024; +pub const PROTO_xns_idp = 0o026; +pub const PROTO_rdp = 0o033; +pub const PROTO_iso_tp4 = 0o035; +pub const PROTO_xtp = 0o044; +pub const PROTO_ddp = 0o045; +pub const PROTO_idpr_cmtp = 0o046; +pub const PROTO_ipv6 = 0o051; +pub const PROTO_ipv6_route = 0o053; +pub const PROTO_ipv6_frag = 0o054; +pub const PROTO_idrp = 0o055; +pub const PROTO_rsvp = 0o056; +pub const PROTO_gre = 0o057; +pub const PROTO_esp = 0o062; +pub const PROTO_ah = 0o063; +pub const PROTO_skip = 0o071; +pub const PROTO_ipv6_icmp = 0o072; +pub const PROTO_ipv6_nonxt = 0o073; +pub const PROTO_ipv6_opts = 0o074; +pub const PROTO_rspf = 0o111; +pub const PROTO_vmtp = 0o121; +pub const PROTO_ospf = 0o131; +pub const PROTO_ipip = 0o136; +pub const PROTO_encap = 0o142; +pub const PROTO_pim = 0o147; +pub const PROTO_raw = 0o377; + +pub const SHUT_RD = 0; +pub const SHUT_WR = 1; +pub const SHUT_RDWR = 2; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 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 = 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_MAX = 44; + +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_MAX = PF_MAX; + +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_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 = 29; +pub const SCM_TIMESTAMP = SO_TIMESTAMP; + +pub const SO_PEERSEC = 31; +pub const SO_PASSSEC = 34; +pub const SO_TIMESTAMPNS = 35; +pub const SCM_TIMESTAMPNS = SO_TIMESTAMPNS; +pub const SO_MARK = 36; +pub const SO_TIMESTAMPING = 37; +pub const SCM_TIMESTAMPING = SO_TIMESTAMPING; +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 SOL_SOCKET = 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 SOMAXCONN = 128; + +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 = 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 = 0x5411; +pub const TIOCSTI = 0x5412; +pub const TIOCGWINSZ = 0x5413; +pub const TIOCSWINSZ = 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 = 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 = 0x100; +pub const EPOLLWRBAND = 0x200; +pub const EPOLLMSG = 0x400; +pub const EPOLLERR = 0x008; +pub const EPOLLHUP = 0x010; +pub const EPOLLRDHUP = 0x2000; +pub const EPOLLEXCLUSIVE = (u32(1) << 28); +pub const EPOLLWAKEUP = (u32(1) << 29); +pub const EPOLLONESHOT = (u32(1) << 30); +pub const EPOLLET = (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_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; + +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 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); + +fn unsigned(s: i32) u32 { + return @bitCast(u32, s); +} +fn signed(s: u32) i32 { + return @bitCast(i32, s); +} +pub fn WEXITSTATUS(s: i32) i32 { + return signed((unsigned(s) & 0xff00) >> 8); +} +pub fn WTERMSIG(s: i32) i32 { + return signed(unsigned(s) & 0x7f); +} +pub fn WSTOPSIG(s: i32) i32 { + return WEXITSTATUS(s); +} +pub fn WIFEXITED(s: i32) bool { + return WTERMSIG(s) == 0; +} +pub fn WIFSTOPPED(s: i32) bool { + return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; +} +pub fn WIFSIGNALED(s: i32) bool { + return (unsigned(s) & 0xffff) -% 1 < 0xff; +} + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 65; +const sigset_t = [128 / @sizeOf(usize)]usize; +const all_mask = []u32{ 0xffffffff, 0xffffffff }; +const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; + +const k_sigaction = extern struct { + handler: extern fn (i32) void, + flags: usize, + restorer: extern fn () void, + mask: [2]u32, +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = struct { + handler: extern fn (i32) void, + mask: sigset_t, + flags: u32, +}; + +pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); +pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); +pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); +pub const empty_sigset = []usize{0} ** sigset_t.len; + +pub const in_port_t = u16; +pub const sa_family_t = u16; +pub const socklen_t = u32; + +/// This intentionally only has ip4 and ip6 +pub const sockaddr = extern union { + in: sockaddr_in, + in6: sockaddr_in6, +}; + +pub const sockaddr_in = extern struct { + family: sa_family_t, + port: in_port_t, + addr: u32, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + family: sa_family_t, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, +}; + +pub const sockaddr_un = extern struct { + family: sa_family_t, + path: [108]u8, +}; + +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 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 = if (builtin.arch != .x86_64) + extern struct { + events: u32, + data: epoll_data, + } +else + packed 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(u8: x) bool { + return x >= 0 and x <= CAP_LAST_CAP; +} + +pub fn CAP_TO_MASK(cap: u8) u32 { + return u32(1) << 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 const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*]const u8, + dlpi_phdr: [*]elf.Phdr, + dlpi_phnum: u16, +}; diff --git a/std/os/linux/posix/arm64.zig b/std/os/linux/posix/arm64.zig @@ -0,0 +1,405 @@ +// arm64-specific declarations that are intended to be imported into the POSIX namespace. +// This does include Linux-only APIs. + +const std = @import("../../std.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; +const iovec_const = linux.iovec_const; + +pub const SYS_io_setup = 0; +pub const SYS_io_destroy = 1; +pub const SYS_io_submit = 2; +pub const SYS_io_cancel = 3; +pub const SYS_io_getevents = 4; +pub const SYS_setxattr = 5; +pub const SYS_lsetxattr = 6; +pub const SYS_fsetxattr = 7; +pub const SYS_getxattr = 8; +pub const SYS_lgetxattr = 9; +pub const SYS_fgetxattr = 10; +pub const SYS_listxattr = 11; +pub const SYS_llistxattr = 12; +pub const SYS_flistxattr = 13; +pub const SYS_removexattr = 14; +pub const SYS_lremovexattr = 15; +pub const SYS_fremovexattr = 16; +pub const SYS_getcwd = 17; +pub const SYS_lookup_dcookie = 18; +pub const SYS_eventfd2 = 19; +pub const SYS_epoll_create1 = 20; +pub const SYS_epoll_ctl = 21; +pub const SYS_epoll_pwait = 22; +pub const SYS_dup = 23; +pub const SYS_dup3 = 24; +pub const SYS_fcntl = 25; +pub const SYS_inotify_init1 = 26; +pub const SYS_inotify_add_watch = 27; +pub const SYS_inotify_rm_watch = 28; +pub const SYS_ioctl = 29; +pub const SYS_ioprio_set = 30; +pub const SYS_ioprio_get = 31; +pub const SYS_flock = 32; +pub const SYS_mknodat = 33; +pub const SYS_mkdirat = 34; +pub const SYS_unlinkat = 35; +pub const SYS_symlinkat = 36; +pub const SYS_linkat = 37; +pub const SYS_renameat = 38; +pub const SYS_umount2 = 39; +pub const SYS_mount = 40; +pub const SYS_pivot_root = 41; +pub const SYS_nfsservctl = 42; +pub const SYS_statfs = 43; +pub const SYS_fstatfs = 44; +pub const SYS_truncate = 45; +pub const SYS_ftruncate = 46; +pub const SYS_fallocate = 47; +pub const SYS_faccessat = 48; +pub const SYS_chdir = 49; +pub const SYS_fchdir = 50; +pub const SYS_chroot = 51; +pub const SYS_fchmod = 52; +pub const SYS_fchmodat = 53; +pub const SYS_fchownat = 54; +pub const SYS_fchown = 55; +pub const SYS_openat = 56; +pub const SYS_close = 57; +pub const SYS_vhangup = 58; +pub const SYS_pipe2 = 59; +pub const SYS_quotactl = 60; +pub const SYS_getdents64 = 61; +pub const SYS_lseek = 62; +pub const SYS_read = 63; +pub const SYS_write = 64; +pub const SYS_readv = 65; +pub const SYS_writev = 66; +pub const SYS_pread64 = 67; +pub const SYS_pwrite64 = 68; +pub const SYS_preadv = 69; +pub const SYS_pwritev = 70; +pub const SYS_pselect6 = 72; +pub const SYS_ppoll = 73; +pub const SYS_signalfd4 = 74; +pub const SYS_vmsplice = 75; +pub const SYS_splice = 76; +pub const SYS_tee = 77; +pub const SYS_readlinkat = 78; +pub const SYS_fstatat = 79; +pub const SYS_fstat = 80; +pub const SYS_sync = 81; +pub const SYS_fsync = 82; +pub const SYS_fdatasync = 83; +pub const SYS_sync_file_range2 = 84; +pub const SYS_sync_file_range = 84; +pub const SYS_timerfd_create = 85; +pub const SYS_timerfd_settime = 86; +pub const SYS_timerfd_gettime = 87; +pub const SYS_utimensat = 88; +pub const SYS_acct = 89; +pub const SYS_capget = 90; +pub const SYS_capset = 91; +pub const SYS_personality = 92; +pub const SYS_exit = 93; +pub const SYS_exit_group = 94; +pub const SYS_waitid = 95; +pub const SYS_set_tid_address = 96; +pub const SYS_unshare = 97; +pub const SYS_futex = 98; +pub const SYS_set_robust_list = 99; +pub const SYS_get_robust_list = 100; +pub const SYS_nanosleep = 101; +pub const SYS_getitimer = 102; +pub const SYS_setitimer = 103; +pub const SYS_kexec_load = 104; +pub const SYS_init_module = 105; +pub const SYS_delete_module = 106; +pub const SYS_timer_create = 107; +pub const SYS_timer_gettime = 108; +pub const SYS_timer_getoverrun = 109; +pub const SYS_timer_settime = 110; +pub const SYS_timer_delete = 111; +pub const SYS_clock_settime = 112; +pub const SYS_clock_gettime = 113; +pub const SYS_clock_getres = 114; +pub const SYS_clock_nanosleep = 115; +pub const SYS_syslog = 116; +pub const SYS_ptrace = 117; +pub const SYS_sched_setparam = 118; +pub const SYS_sched_setscheduler = 119; +pub const SYS_sched_getscheduler = 120; +pub const SYS_sched_getparam = 121; +pub const SYS_sched_setaffinity = 122; +pub const SYS_sched_getaffinity = 123; +pub const SYS_sched_yield = 124; +pub const SYS_sched_get_priority_max = 125; +pub const SYS_sched_get_priority_min = 126; +pub const SYS_sched_rr_get_interval = 127; +pub const SYS_restart_syscall = 128; +pub const SYS_kill = 129; +pub const SYS_tkill = 130; +pub const SYS_tgkill = 131; +pub const SYS_sigaltstack = 132; +pub const SYS_rt_sigsuspend = 133; +pub const SYS_rt_sigaction = 134; +pub const SYS_rt_sigprocmask = 135; +pub const SYS_rt_sigpending = 136; +pub const SYS_rt_sigtimedwait = 137; +pub const SYS_rt_sigqueueinfo = 138; +pub const SYS_rt_sigreturn = 139; +pub const SYS_setpriority = 140; +pub const SYS_getpriority = 141; +pub const SYS_reboot = 142; +pub const SYS_setregid = 143; +pub const SYS_setgid = 144; +pub const SYS_setreuid = 145; +pub const SYS_setuid = 146; +pub const SYS_setresuid = 147; +pub const SYS_getresuid = 148; +pub const SYS_setresgid = 149; +pub const SYS_getresgid = 150; +pub const SYS_setfsuid = 151; +pub const SYS_setfsgid = 152; +pub const SYS_times = 153; +pub const SYS_setpgid = 154; +pub const SYS_getpgid = 155; +pub const SYS_getsid = 156; +pub const SYS_setsid = 157; +pub const SYS_getgroups = 158; +pub const SYS_setgroups = 159; +pub const SYS_uname = 160; +pub const SYS_sethostname = 161; +pub const SYS_setdomainname = 162; +pub const SYS_getrlimit = 163; +pub const SYS_setrlimit = 164; +pub const SYS_getrusage = 165; +pub const SYS_umask = 166; +pub const SYS_prctl = 167; +pub const SYS_getcpu = 168; +pub const SYS_gettimeofday = 169; +pub const SYS_settimeofday = 170; +pub const SYS_adjtimex = 171; +pub const SYS_getpid = 172; +pub const SYS_getppid = 173; +pub const SYS_getuid = 174; +pub const SYS_geteuid = 175; +pub const SYS_getgid = 176; +pub const SYS_getegid = 177; +pub const SYS_gettid = 178; +pub const SYS_sysinfo = 179; +pub const SYS_mq_open = 180; +pub const SYS_mq_unlink = 181; +pub const SYS_mq_timedsend = 182; +pub const SYS_mq_timedreceive = 183; +pub const SYS_mq_notify = 184; +pub const SYS_mq_getsetattr = 185; +pub const SYS_msgget = 186; +pub const SYS_msgctl = 187; +pub const SYS_msgrcv = 188; +pub const SYS_msgsnd = 189; +pub const SYS_semget = 190; +pub const SYS_semctl = 191; +pub const SYS_semtimedop = 192; +pub const SYS_semop = 193; +pub const SYS_shmget = 194; +pub const SYS_shmctl = 195; +pub const SYS_shmat = 196; +pub const SYS_shmdt = 197; +pub const SYS_socket = 198; +pub const SYS_socketpair = 199; +pub const SYS_bind = 200; +pub const SYS_listen = 201; +pub const SYS_accept = 202; +pub const SYS_connect = 203; +pub const SYS_getsockname = 204; +pub const SYS_getpeername = 205; +pub const SYS_sendto = 206; +pub const SYS_recvfrom = 207; +pub const SYS_setsockopt = 208; +pub const SYS_getsockopt = 209; +pub const SYS_shutdown = 210; +pub const SYS_sendmsg = 211; +pub const SYS_recvmsg = 212; +pub const SYS_readahead = 213; +pub const SYS_brk = 214; +pub const SYS_munmap = 215; +pub const SYS_mremap = 216; +pub const SYS_add_key = 217; +pub const SYS_request_key = 218; +pub const SYS_keyctl = 219; +pub const SYS_clone = 220; +pub const SYS_execve = 221; +pub const SYS_mmap = 222; +pub const SYS_fadvise64 = 223; +pub const SYS_swapon = 224; +pub const SYS_swapoff = 225; +pub const SYS_mprotect = 226; +pub const SYS_msync = 227; +pub const SYS_mlock = 228; +pub const SYS_munlock = 229; +pub const SYS_mlockall = 230; +pub const SYS_munlockall = 231; +pub const SYS_mincore = 232; +pub const SYS_madvise = 233; +pub const SYS_remap_file_pages = 234; +pub const SYS_mbind = 235; +pub const SYS_get_mempolicy = 236; +pub const SYS_set_mempolicy = 237; +pub const SYS_migrate_pages = 238; +pub const SYS_move_pages = 239; +pub const SYS_rt_tgsigqueueinfo = 240; +pub const SYS_perf_event_open = 241; +pub const SYS_accept4 = 242; +pub const SYS_recvmmsg = 243; +pub const SYS_arch_specific_syscall = 244; +pub const SYS_wait4 = 260; +pub const SYS_prlimit64 = 261; +pub const SYS_fanotify_init = 262; +pub const SYS_fanotify_mark = 263; +pub const SYS_clock_adjtime = 266; +pub const SYS_syncfs = 267; +pub const SYS_setns = 268; +pub const SYS_sendmmsg = 269; +pub const SYS_process_vm_readv = 270; +pub const SYS_process_vm_writev = 271; +pub const SYS_kcmp = 272; +pub const SYS_finit_module = 273; +pub const SYS_sched_setattr = 274; +pub const SYS_sched_getattr = 275; +pub const SYS_renameat2 = 276; +pub const SYS_seccomp = 277; +pub const SYS_getrandom = 278; +pub const SYS_memfd_create = 279; +pub const SYS_bpf = 280; +pub const SYS_execveat = 281; +pub const SYS_userfaultfd = 282; +pub const SYS_membarrier = 283; +pub const SYS_mlock2 = 284; +pub const SYS_copy_file_range = 285; +pub const SYS_preadv2 = 286; +pub const SYS_pwritev2 = 287; +pub const SYS_pkey_mprotect = 288; +pub const SYS_pkey_alloc = 289; +pub const SYS_pkey_free = 290; +pub const SYS_statx = 291; +pub const SYS_io_pgetevents = 292; +pub const SYS_rseq = 293; +pub const SYS_kexec_file_load = 294; +pub const SYS_pidfd_send_signal = 424; +pub const SYS_io_uring_setup = 425; +pub const SYS_io_uring_enter = 426; +pub const SYS_io_uring_register = 427; + +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; + +pub const AT_FDCWD = -100; +pub const AT_SYMLINK_NOFOLLOW = 0x100; +pub const AT_REMOVEDIR = 0x200; +pub const AT_SYMLINK_FOLLOW = 0x400; +pub const AT_NO_AUTOMOUNT = 0x800; +pub const AT_EMPTY_PATH = 0x1000; + +pub const VDSO_USEFUL = true; +pub const VDSO_CGT_SYM = "__kernel_clock_gettime"; +pub const VDSO_CGT_VER = "LINUX_2.6.39"; + +pub const msghdr = extern struct { + msg_name: ?*sockaddr, + msg_namelen: socklen_t, + msg_iov: [*]iovec, + msg_iovlen: i32, + __pad1: i32, + msg_control: ?*c_void, + msg_controllen: socklen_t, + __pad2: socklen_t, + 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, + msg_control: ?*c_void, + msg_controllen: socklen_t, + __pad2: socklen_t, + msg_flags: i32, +}; + +/// Renamed to Stat to not conflict with the stat function. +pub const Stat = extern struct { + dev: u64, + ino: u64, + nlink: usize, + + mode: u32, + uid: u32, + gid: u32, + __pad0: u32, + rdev: u64, + size: i64, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [3]isize, +}; + +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; diff --git a/std/os/linux/posix/x86_64.zig b/std/os/linux/posix/x86_64.zig @@ -0,0 +1,470 @@ +// x86-64-specific declarations that are intended to be imported into the POSIX namespace. +const std = @import("../../../std.zig"); + +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 SYS_read = 0; +pub const SYS_write = 1; +pub const SYS_open = 2; +pub const SYS_close = 3; +pub const SYS_stat = 4; +pub const SYS_fstat = 5; +pub const SYS_lstat = 6; +pub const SYS_poll = 7; +pub const SYS_lseek = 8; +pub const SYS_mmap = 9; +pub const SYS_mprotect = 10; +pub const SYS_munmap = 11; +pub const SYS_brk = 12; +pub const SYS_rt_sigaction = 13; +pub const SYS_rt_sigprocmask = 14; +pub const SYS_rt_sigreturn = 15; +pub const SYS_ioctl = 16; +pub const SYS_pread = 17; +pub const SYS_pwrite = 18; +pub const SYS_readv = 19; +pub const SYS_writev = 20; +pub const SYS_access = 21; +pub const SYS_pipe = 22; +pub const SYS_select = 23; +pub const SYS_sched_yield = 24; +pub const SYS_mremap = 25; +pub const SYS_msync = 26; +pub const SYS_mincore = 27; +pub const SYS_madvise = 28; +pub const SYS_shmget = 29; +pub const SYS_shmat = 30; +pub const SYS_shmctl = 31; +pub const SYS_dup = 32; +pub const SYS_dup2 = 33; +pub const SYS_pause = 34; +pub const SYS_nanosleep = 35; +pub const SYS_getitimer = 36; +pub const SYS_alarm = 37; +pub const SYS_setitimer = 38; +pub const SYS_getpid = 39; +pub const SYS_sendfile = 40; +pub const SYS_socket = 41; +pub const SYS_connect = 42; +pub const SYS_accept = 43; +pub const SYS_sendto = 44; +pub const SYS_recvfrom = 45; +pub const SYS_sendmsg = 46; +pub const SYS_recvmsg = 47; +pub const SYS_shutdown = 48; +pub const SYS_bind = 49; +pub const SYS_listen = 50; +pub const SYS_getsockname = 51; +pub const SYS_getpeername = 52; +pub const SYS_socketpair = 53; +pub const SYS_setsockopt = 54; +pub const SYS_getsockopt = 55; +pub const SYS_clone = 56; +pub const SYS_fork = 57; +pub const SYS_vfork = 58; +pub const SYS_execve = 59; +pub const SYS_exit = 60; +pub const SYS_wait4 = 61; +pub const SYS_kill = 62; +pub const SYS_uname = 63; +pub const SYS_semget = 64; +pub const SYS_semop = 65; +pub const SYS_semctl = 66; +pub const SYS_shmdt = 67; +pub const SYS_msgget = 68; +pub const SYS_msgsnd = 69; +pub const SYS_msgrcv = 70; +pub const SYS_msgctl = 71; +pub const SYS_fcntl = 72; +pub const SYS_flock = 73; +pub const SYS_fsync = 74; +pub const SYS_fdatasync = 75; +pub const SYS_truncate = 76; +pub const SYS_ftruncate = 77; +pub const SYS_getdents = 78; +pub const SYS_getcwd = 79; +pub const SYS_chdir = 80; +pub const SYS_fchdir = 81; +pub const SYS_rename = 82; +pub const SYS_mkdir = 83; +pub const SYS_rmdir = 84; +pub const SYS_creat = 85; +pub const SYS_link = 86; +pub const SYS_unlink = 87; +pub const SYS_symlink = 88; +pub const SYS_readlink = 89; +pub const SYS_chmod = 90; +pub const SYS_fchmod = 91; +pub const SYS_chown = 92; +pub const SYS_fchown = 93; +pub const SYS_lchown = 94; +pub const SYS_umask = 95; +pub const SYS_gettimeofday = 96; +pub const SYS_getrlimit = 97; +pub const SYS_getrusage = 98; +pub const SYS_sysinfo = 99; +pub const SYS_times = 100; +pub const SYS_ptrace = 101; +pub const SYS_getuid = 102; +pub const SYS_syslog = 103; +pub const SYS_getgid = 104; +pub const SYS_setuid = 105; +pub const SYS_setgid = 106; +pub const SYS_geteuid = 107; +pub const SYS_getegid = 108; +pub const SYS_setpgid = 109; +pub const SYS_getppid = 110; +pub const SYS_getpgrp = 111; +pub const SYS_setsid = 112; +pub const SYS_setreuid = 113; +pub const SYS_setregid = 114; +pub const SYS_getgroups = 115; +pub const SYS_setgroups = 116; +pub const SYS_setresuid = 117; +pub const SYS_getresuid = 118; +pub const SYS_setresgid = 119; +pub const SYS_getresgid = 120; +pub const SYS_getpgid = 121; +pub const SYS_setfsuid = 122; +pub const SYS_setfsgid = 123; +pub const SYS_getsid = 124; +pub const SYS_capget = 125; +pub const SYS_capset = 126; +pub const SYS_rt_sigpending = 127; +pub const SYS_rt_sigtimedwait = 128; +pub const SYS_rt_sigqueueinfo = 129; +pub const SYS_rt_sigsuspend = 130; +pub const SYS_sigaltstack = 131; +pub const SYS_utime = 132; +pub const SYS_mknod = 133; +pub const SYS_uselib = 134; +pub const SYS_personality = 135; +pub const SYS_ustat = 136; +pub const SYS_statfs = 137; +pub const SYS_fstatfs = 138; +pub const SYS_sysfs = 139; +pub const SYS_getpriority = 140; +pub const SYS_setpriority = 141; +pub const SYS_sched_setparam = 142; +pub const SYS_sched_getparam = 143; +pub const SYS_sched_setscheduler = 144; +pub const SYS_sched_getscheduler = 145; +pub const SYS_sched_get_priority_max = 146; +pub const SYS_sched_get_priority_min = 147; +pub const SYS_sched_rr_get_interval = 148; +pub const SYS_mlock = 149; +pub const SYS_munlock = 150; +pub const SYS_mlockall = 151; +pub const SYS_munlockall = 152; +pub const SYS_vhangup = 153; +pub const SYS_modify_ldt = 154; +pub const SYS_pivot_root = 155; +pub const SYS__sysctl = 156; +pub const SYS_prctl = 157; +pub const SYS_arch_prctl = 158; +pub const SYS_adjtimex = 159; +pub const SYS_setrlimit = 160; +pub const SYS_chroot = 161; +pub const SYS_sync = 162; +pub const SYS_acct = 163; +pub const SYS_settimeofday = 164; +pub const SYS_mount = 165; +pub const SYS_umount2 = 166; +pub const SYS_swapon = 167; +pub const SYS_swapoff = 168; +pub const SYS_reboot = 169; +pub const SYS_sethostname = 170; +pub const SYS_setdomainname = 171; +pub const SYS_iopl = 172; +pub const SYS_ioperm = 173; +pub const SYS_create_module = 174; +pub const SYS_init_module = 175; +pub const SYS_delete_module = 176; +pub const SYS_get_kernel_syms = 177; +pub const SYS_query_module = 178; +pub const SYS_quotactl = 179; +pub const SYS_nfsservctl = 180; +pub const SYS_getpmsg = 181; +pub const SYS_putpmsg = 182; +pub const SYS_afs_syscall = 183; +pub const SYS_tuxcall = 184; +pub const SYS_security = 185; +pub const SYS_gettid = 186; +pub const SYS_readahead = 187; +pub const SYS_setxattr = 188; +pub const SYS_lsetxattr = 189; +pub const SYS_fsetxattr = 190; +pub const SYS_getxattr = 191; +pub const SYS_lgetxattr = 192; +pub const SYS_fgetxattr = 193; +pub const SYS_listxattr = 194; +pub const SYS_llistxattr = 195; +pub const SYS_flistxattr = 196; +pub const SYS_removexattr = 197; +pub const SYS_lremovexattr = 198; +pub const SYS_fremovexattr = 199; +pub const SYS_tkill = 200; +pub const SYS_time = 201; +pub const SYS_futex = 202; +pub const SYS_sched_setaffinity = 203; +pub const SYS_sched_getaffinity = 204; +pub const SYS_set_thread_area = 205; +pub const SYS_io_setup = 206; +pub const SYS_io_destroy = 207; +pub const SYS_io_getevents = 208; +pub const SYS_io_submit = 209; +pub const SYS_io_cancel = 210; +pub const SYS_get_thread_area = 211; +pub const SYS_lookup_dcookie = 212; +pub const SYS_epoll_create = 213; +pub const SYS_epoll_ctl_old = 214; +pub const SYS_epoll_wait_old = 215; +pub const SYS_remap_file_pages = 216; +pub const SYS_getdents64 = 217; +pub const SYS_set_tid_address = 218; +pub const SYS_restart_syscall = 219; +pub const SYS_semtimedop = 220; +pub const SYS_fadvise64 = 221; +pub const SYS_timer_create = 222; +pub const SYS_timer_settime = 223; +pub const SYS_timer_gettime = 224; +pub const SYS_timer_getoverrun = 225; +pub const SYS_timer_delete = 226; +pub const SYS_clock_settime = 227; +pub const SYS_clock_gettime = 228; +pub const SYS_clock_getres = 229; +pub const SYS_clock_nanosleep = 230; +pub const SYS_exit_group = 231; +pub const SYS_epoll_wait = 232; +pub const SYS_epoll_ctl = 233; +pub const SYS_tgkill = 234; +pub const SYS_utimes = 235; +pub const SYS_vserver = 236; +pub const SYS_mbind = 237; +pub const SYS_set_mempolicy = 238; +pub const SYS_get_mempolicy = 239; +pub const SYS_mq_open = 240; +pub const SYS_mq_unlink = 241; +pub const SYS_mq_timedsend = 242; +pub const SYS_mq_timedreceive = 243; +pub const SYS_mq_notify = 244; +pub const SYS_mq_getsetattr = 245; +pub const SYS_kexec_load = 246; +pub const SYS_waitid = 247; +pub const SYS_add_key = 248; +pub const SYS_request_key = 249; +pub const SYS_keyctl = 250; +pub const SYS_ioprio_set = 251; +pub const SYS_ioprio_get = 252; +pub const SYS_inotify_init = 253; +pub const SYS_inotify_add_watch = 254; +pub const SYS_inotify_rm_watch = 255; +pub const SYS_migrate_pages = 256; +pub const SYS_openat = 257; +pub const SYS_mkdirat = 258; +pub const SYS_mknodat = 259; +pub const SYS_fchownat = 260; +pub const SYS_futimesat = 261; +pub const SYS_newfstatat = 262; +pub const SYS_fstatat = 262; +pub const SYS_unlinkat = 263; +pub const SYS_renameat = 264; +pub const SYS_linkat = 265; +pub const SYS_symlinkat = 266; +pub const SYS_readlinkat = 267; +pub const SYS_fchmodat = 268; +pub const SYS_faccessat = 269; +pub const SYS_pselect6 = 270; +pub const SYS_ppoll = 271; +pub const SYS_unshare = 272; +pub const SYS_set_robust_list = 273; +pub const SYS_get_robust_list = 274; +pub const SYS_splice = 275; +pub const SYS_tee = 276; +pub const SYS_sync_file_range = 277; +pub const SYS_vmsplice = 278; +pub const SYS_move_pages = 279; +pub const SYS_utimensat = 280; +pub const SYS_epoll_pwait = 281; +pub const SYS_signalfd = 282; +pub const SYS_timerfd_create = 283; +pub const SYS_eventfd = 284; +pub const SYS_fallocate = 285; +pub const SYS_timerfd_settime = 286; +pub const SYS_timerfd_gettime = 287; +pub const SYS_accept4 = 288; +pub const SYS_signalfd4 = 289; +pub const SYS_eventfd2 = 290; +pub const SYS_epoll_create1 = 291; +pub const SYS_dup3 = 292; +pub const SYS_pipe2 = 293; +pub const SYS_inotify_init1 = 294; +pub const SYS_preadv = 295; +pub const SYS_pwritev = 296; +pub const SYS_rt_tgsigqueueinfo = 297; +pub const SYS_perf_event_open = 298; +pub const SYS_recvmmsg = 299; +pub const SYS_fanotify_init = 300; +pub const SYS_fanotify_mark = 301; +pub const SYS_prlimit64 = 302; +pub const SYS_name_to_handle_at = 303; +pub const SYS_open_by_handle_at = 304; +pub const SYS_clock_adjtime = 305; +pub const SYS_syncfs = 306; +pub const SYS_sendmmsg = 307; +pub const SYS_setns = 308; +pub const SYS_getcpu = 309; +pub const SYS_process_vm_readv = 310; +pub const SYS_process_vm_writev = 311; +pub const SYS_kcmp = 312; +pub const SYS_finit_module = 313; +pub const SYS_sched_setattr = 314; +pub const SYS_sched_getattr = 315; +pub const SYS_renameat2 = 316; +pub const SYS_seccomp = 317; +pub const SYS_getrandom = 318; +pub const SYS_memfd_create = 319; +pub const SYS_kexec_file_load = 320; +pub const SYS_bpf = 321; +pub const SYS_execveat = 322; +pub const SYS_userfaultfd = 323; +pub const SYS_membarrier = 324; +pub const SYS_mlock2 = 325; +pub const SYS_copy_file_range = 326; +pub const SYS_preadv2 = 327; +pub const SYS_pwritev2 = 328; +pub const SYS_pkey_mprotect = 329; +pub const SYS_pkey_alloc = 330; +pub const SYS_pkey_free = 331; +pub const SYS_statx = 332; +pub const SYS_io_pgetevents = 333; +pub const SYS_rseq = 334; +pub const SYS_pidfd_send_signal = 424; +pub const SYS_io_uring_setup = 425; +pub const SYS_io_uring_enter = 426; +pub const SYS_io_uring_register = 427; + +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; + +pub const AT_FDCWD = -100; +pub const AT_SYMLINK_NOFOLLOW = 0x100; +pub const AT_REMOVEDIR = 0x200; +pub const AT_SYMLINK_FOLLOW = 0x400; +pub const AT_NO_AUTOMOUNT = 0x800; +pub const AT_EMPTY_PATH = 0x1000; + +pub const VDSO_USEFUL = true; +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 msghdr = extern struct { + msg_name: ?*sockaddr, + msg_namelen: socklen_t, + msg_iov: [*]iovec, + msg_iovlen: i32, + __pad1: i32, + msg_control: ?*c_void, + msg_controllen: socklen_t, + __pad2: socklen_t, + 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, + msg_control: ?*c_void, + msg_controllen: socklen_t, + __pad2: socklen_t, + msg_flags: i32, +}; + +/// Renamed to Stat to not conflict with the stat function. +pub const Stat = extern struct { + dev: u64, + ino: u64, + nlink: usize, + + mode: u32, + uid: u32, + gid: u32, + __pad0: u32, + rdev: u64, + size: i64, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [3]isize, +}; + +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; diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig @@ -1,410 +1,4 @@ -const std = @import("../../std.zig"); -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 SYS_read = 0; -pub const SYS_write = 1; -pub const SYS_open = 2; -pub const SYS_close = 3; -pub const SYS_stat = 4; -pub const SYS_fstat = 5; -pub const SYS_lstat = 6; -pub const SYS_poll = 7; -pub const SYS_lseek = 8; -pub const SYS_mmap = 9; -pub const SYS_mprotect = 10; -pub const SYS_munmap = 11; -pub const SYS_brk = 12; -pub const SYS_rt_sigaction = 13; -pub const SYS_rt_sigprocmask = 14; -pub const SYS_rt_sigreturn = 15; -pub const SYS_ioctl = 16; -pub const SYS_pread = 17; -pub const SYS_pwrite = 18; -pub const SYS_readv = 19; -pub const SYS_writev = 20; -pub const SYS_access = 21; -pub const SYS_pipe = 22; -pub const SYS_select = 23; -pub const SYS_sched_yield = 24; -pub const SYS_mremap = 25; -pub const SYS_msync = 26; -pub const SYS_mincore = 27; -pub const SYS_madvise = 28; -pub const SYS_shmget = 29; -pub const SYS_shmat = 30; -pub const SYS_shmctl = 31; -pub const SYS_dup = 32; -pub const SYS_dup2 = 33; -pub const SYS_pause = 34; -pub const SYS_nanosleep = 35; -pub const SYS_getitimer = 36; -pub const SYS_alarm = 37; -pub const SYS_setitimer = 38; -pub const SYS_getpid = 39; -pub const SYS_sendfile = 40; -pub const SYS_socket = 41; -pub const SYS_connect = 42; -pub const SYS_accept = 43; -pub const SYS_sendto = 44; -pub const SYS_recvfrom = 45; -pub const SYS_sendmsg = 46; -pub const SYS_recvmsg = 47; -pub const SYS_shutdown = 48; -pub const SYS_bind = 49; -pub const SYS_listen = 50; -pub const SYS_getsockname = 51; -pub const SYS_getpeername = 52; -pub const SYS_socketpair = 53; -pub const SYS_setsockopt = 54; -pub const SYS_getsockopt = 55; -pub const SYS_clone = 56; -pub const SYS_fork = 57; -pub const SYS_vfork = 58; -pub const SYS_execve = 59; -pub const SYS_exit = 60; -pub const SYS_wait4 = 61; -pub const SYS_kill = 62; -pub const SYS_uname = 63; -pub const SYS_semget = 64; -pub const SYS_semop = 65; -pub const SYS_semctl = 66; -pub const SYS_shmdt = 67; -pub const SYS_msgget = 68; -pub const SYS_msgsnd = 69; -pub const SYS_msgrcv = 70; -pub const SYS_msgctl = 71; -pub const SYS_fcntl = 72; -pub const SYS_flock = 73; -pub const SYS_fsync = 74; -pub const SYS_fdatasync = 75; -pub const SYS_truncate = 76; -pub const SYS_ftruncate = 77; -pub const SYS_getdents = 78; -pub const SYS_getcwd = 79; -pub const SYS_chdir = 80; -pub const SYS_fchdir = 81; -pub const SYS_rename = 82; -pub const SYS_mkdir = 83; -pub const SYS_rmdir = 84; -pub const SYS_creat = 85; -pub const SYS_link = 86; -pub const SYS_unlink = 87; -pub const SYS_symlink = 88; -pub const SYS_readlink = 89; -pub const SYS_chmod = 90; -pub const SYS_fchmod = 91; -pub const SYS_chown = 92; -pub const SYS_fchown = 93; -pub const SYS_lchown = 94; -pub const SYS_umask = 95; -pub const SYS_gettimeofday = 96; -pub const SYS_getrlimit = 97; -pub const SYS_getrusage = 98; -pub const SYS_sysinfo = 99; -pub const SYS_times = 100; -pub const SYS_ptrace = 101; -pub const SYS_getuid = 102; -pub const SYS_syslog = 103; -pub const SYS_getgid = 104; -pub const SYS_setuid = 105; -pub const SYS_setgid = 106; -pub const SYS_geteuid = 107; -pub const SYS_getegid = 108; -pub const SYS_setpgid = 109; -pub const SYS_getppid = 110; -pub const SYS_getpgrp = 111; -pub const SYS_setsid = 112; -pub const SYS_setreuid = 113; -pub const SYS_setregid = 114; -pub const SYS_getgroups = 115; -pub const SYS_setgroups = 116; -pub const SYS_setresuid = 117; -pub const SYS_getresuid = 118; -pub const SYS_setresgid = 119; -pub const SYS_getresgid = 120; -pub const SYS_getpgid = 121; -pub const SYS_setfsuid = 122; -pub const SYS_setfsgid = 123; -pub const SYS_getsid = 124; -pub const SYS_capget = 125; -pub const SYS_capset = 126; -pub const SYS_rt_sigpending = 127; -pub const SYS_rt_sigtimedwait = 128; -pub const SYS_rt_sigqueueinfo = 129; -pub const SYS_rt_sigsuspend = 130; -pub const SYS_sigaltstack = 131; -pub const SYS_utime = 132; -pub const SYS_mknod = 133; -pub const SYS_uselib = 134; -pub const SYS_personality = 135; -pub const SYS_ustat = 136; -pub const SYS_statfs = 137; -pub const SYS_fstatfs = 138; -pub const SYS_sysfs = 139; -pub const SYS_getpriority = 140; -pub const SYS_setpriority = 141; -pub const SYS_sched_setparam = 142; -pub const SYS_sched_getparam = 143; -pub const SYS_sched_setscheduler = 144; -pub const SYS_sched_getscheduler = 145; -pub const SYS_sched_get_priority_max = 146; -pub const SYS_sched_get_priority_min = 147; -pub const SYS_sched_rr_get_interval = 148; -pub const SYS_mlock = 149; -pub const SYS_munlock = 150; -pub const SYS_mlockall = 151; -pub const SYS_munlockall = 152; -pub const SYS_vhangup = 153; -pub const SYS_modify_ldt = 154; -pub const SYS_pivot_root = 155; -pub const SYS__sysctl = 156; -pub const SYS_prctl = 157; -pub const SYS_arch_prctl = 158; -pub const SYS_adjtimex = 159; -pub const SYS_setrlimit = 160; -pub const SYS_chroot = 161; -pub const SYS_sync = 162; -pub const SYS_acct = 163; -pub const SYS_settimeofday = 164; -pub const SYS_mount = 165; -pub const SYS_umount2 = 166; -pub const SYS_swapon = 167; -pub const SYS_swapoff = 168; -pub const SYS_reboot = 169; -pub const SYS_sethostname = 170; -pub const SYS_setdomainname = 171; -pub const SYS_iopl = 172; -pub const SYS_ioperm = 173; -pub const SYS_create_module = 174; -pub const SYS_init_module = 175; -pub const SYS_delete_module = 176; -pub const SYS_get_kernel_syms = 177; -pub const SYS_query_module = 178; -pub const SYS_quotactl = 179; -pub const SYS_nfsservctl = 180; -pub const SYS_getpmsg = 181; -pub const SYS_putpmsg = 182; -pub const SYS_afs_syscall = 183; -pub const SYS_tuxcall = 184; -pub const SYS_security = 185; -pub const SYS_gettid = 186; -pub const SYS_readahead = 187; -pub const SYS_setxattr = 188; -pub const SYS_lsetxattr = 189; -pub const SYS_fsetxattr = 190; -pub const SYS_getxattr = 191; -pub const SYS_lgetxattr = 192; -pub const SYS_fgetxattr = 193; -pub const SYS_listxattr = 194; -pub const SYS_llistxattr = 195; -pub const SYS_flistxattr = 196; -pub const SYS_removexattr = 197; -pub const SYS_lremovexattr = 198; -pub const SYS_fremovexattr = 199; -pub const SYS_tkill = 200; -pub const SYS_time = 201; -pub const SYS_futex = 202; -pub const SYS_sched_setaffinity = 203; -pub const SYS_sched_getaffinity = 204; -pub const SYS_set_thread_area = 205; -pub const SYS_io_setup = 206; -pub const SYS_io_destroy = 207; -pub const SYS_io_getevents = 208; -pub const SYS_io_submit = 209; -pub const SYS_io_cancel = 210; -pub const SYS_get_thread_area = 211; -pub const SYS_lookup_dcookie = 212; -pub const SYS_epoll_create = 213; -pub const SYS_epoll_ctl_old = 214; -pub const SYS_epoll_wait_old = 215; -pub const SYS_remap_file_pages = 216; -pub const SYS_getdents64 = 217; -pub const SYS_set_tid_address = 218; -pub const SYS_restart_syscall = 219; -pub const SYS_semtimedop = 220; -pub const SYS_fadvise64 = 221; -pub const SYS_timer_create = 222; -pub const SYS_timer_settime = 223; -pub const SYS_timer_gettime = 224; -pub const SYS_timer_getoverrun = 225; -pub const SYS_timer_delete = 226; -pub const SYS_clock_settime = 227; -pub const SYS_clock_gettime = 228; -pub const SYS_clock_getres = 229; -pub const SYS_clock_nanosleep = 230; -pub const SYS_exit_group = 231; -pub const SYS_epoll_wait = 232; -pub const SYS_epoll_ctl = 233; -pub const SYS_tgkill = 234; -pub const SYS_utimes = 235; -pub const SYS_vserver = 236; -pub const SYS_mbind = 237; -pub const SYS_set_mempolicy = 238; -pub const SYS_get_mempolicy = 239; -pub const SYS_mq_open = 240; -pub const SYS_mq_unlink = 241; -pub const SYS_mq_timedsend = 242; -pub const SYS_mq_timedreceive = 243; -pub const SYS_mq_notify = 244; -pub const SYS_mq_getsetattr = 245; -pub const SYS_kexec_load = 246; -pub const SYS_waitid = 247; -pub const SYS_add_key = 248; -pub const SYS_request_key = 249; -pub const SYS_keyctl = 250; -pub const SYS_ioprio_set = 251; -pub const SYS_ioprio_get = 252; -pub const SYS_inotify_init = 253; -pub const SYS_inotify_add_watch = 254; -pub const SYS_inotify_rm_watch = 255; -pub const SYS_migrate_pages = 256; -pub const SYS_openat = 257; -pub const SYS_mkdirat = 258; -pub const SYS_mknodat = 259; -pub const SYS_fchownat = 260; -pub const SYS_futimesat = 261; -pub const SYS_newfstatat = 262; -// https://github.com/ziglang/zig/issues/1439 -pub const SYS_fstatat = 262; -pub const SYS_unlinkat = 263; -pub const SYS_renameat = 264; -pub const SYS_linkat = 265; -pub const SYS_symlinkat = 266; -pub const SYS_readlinkat = 267; -pub const SYS_fchmodat = 268; -pub const SYS_faccessat = 269; -pub const SYS_pselect6 = 270; -pub const SYS_ppoll = 271; -pub const SYS_unshare = 272; -pub const SYS_set_robust_list = 273; -pub const SYS_get_robust_list = 274; -pub const SYS_splice = 275; -pub const SYS_tee = 276; -pub const SYS_sync_file_range = 277; -pub const SYS_vmsplice = 278; -pub const SYS_move_pages = 279; -pub const SYS_utimensat = 280; -pub const SYS_epoll_pwait = 281; -pub const SYS_signalfd = 282; -pub const SYS_timerfd_create = 283; -pub const SYS_eventfd = 284; -pub const SYS_fallocate = 285; -pub const SYS_timerfd_settime = 286; -pub const SYS_timerfd_gettime = 287; -pub const SYS_accept4 = 288; -pub const SYS_signalfd4 = 289; -pub const SYS_eventfd2 = 290; -pub const SYS_epoll_create1 = 291; -pub const SYS_dup3 = 292; -pub const SYS_pipe2 = 293; -pub const SYS_inotify_init1 = 294; -pub const SYS_preadv = 295; -pub const SYS_pwritev = 296; -pub const SYS_rt_tgsigqueueinfo = 297; -pub const SYS_perf_event_open = 298; -pub const SYS_recvmmsg = 299; -pub const SYS_fanotify_init = 300; -pub const SYS_fanotify_mark = 301; -pub const SYS_prlimit64 = 302; -pub const SYS_name_to_handle_at = 303; -pub const SYS_open_by_handle_at = 304; -pub const SYS_clock_adjtime = 305; -pub const SYS_syncfs = 306; -pub const SYS_sendmmsg = 307; -pub const SYS_setns = 308; -pub const SYS_getcpu = 309; -pub const SYS_process_vm_readv = 310; -pub const SYS_process_vm_writev = 311; -pub const SYS_kcmp = 312; -pub const SYS_finit_module = 313; -pub const SYS_sched_setattr = 314; -pub const SYS_sched_getattr = 315; -pub const SYS_renameat2 = 316; -pub const SYS_seccomp = 317; -pub const SYS_getrandom = 318; -pub const SYS_memfd_create = 319; -pub const SYS_kexec_file_load = 320; -pub const SYS_bpf = 321; -pub const SYS_execveat = 322; -pub const SYS_userfaultfd = 323; -pub const SYS_membarrier = 324; -pub const SYS_mlock2 = 325; -pub const SYS_copy_file_range = 326; -pub const SYS_preadv2 = 327; -pub const SYS_pwritev2 = 328; -pub const SYS_pkey_mprotect = 329; -pub const SYS_pkey_alloc = 330; -pub const SYS_pkey_free = 331; -pub const SYS_statx = 332; -pub const SYS_io_pgetevents = 333; -pub const SYS_rseq = 334; -pub const SYS_pidfd_send_signal = 424; -pub const SYS_io_uring_setup = 425; -pub const SYS_io_uring_enter = 426; -pub const SYS_io_uring_register = 427; - -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; - -pub const AT_FDCWD = -100; -pub const AT_SYMLINK_NOFOLLOW = 0x100; -pub const AT_REMOVEDIR = 0x200; -pub const AT_SYMLINK_FOLLOW = 0x400; -pub const AT_NO_AUTOMOUNT = 0x800; -pub const AT_EMPTY_PATH = 0x1000; - -pub const VDSO_USEFUL = true; -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; +use @import("posix/x86_64.zig"); pub fn syscall0(number: usize) usize { return asm volatile ("syscall" @@ -501,65 +95,3 @@ pub nakedcc fn restore_rt() void { : "rcx", "r11" ); } - -pub const msghdr = extern struct { - msg_name: ?*sockaddr, - msg_namelen: socklen_t, - msg_iov: [*]iovec, - msg_iovlen: i32, - __pad1: i32, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t, - 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, - msg_control: ?*c_void, - msg_controllen: socklen_t, - __pad2: socklen_t, - msg_flags: i32, -}; - -/// Renamed to Stat to not conflict with the stat function. -pub const Stat = extern struct { - dev: u64, - ino: u64, - nlink: usize, - - mode: u32, - uid: u32, - gid: u32, - __pad0: u32, - rdev: u64, - size: i64, - blksize: isize, - blocks: i64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - __unused: [3]isize, -}; - -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; diff --git a/std/os/netbsd.zig b/std/os/netbsd.zig @@ -1,724 +1,4 @@ const builtin = @import("builtin"); - -pub use @import("netbsd/errno.zig"); - const std = @import("../std.zig"); -const c = std.c; - -const assert = std.debug.assert; -const maxInt = std.math.maxInt; -pub const Kevent = c.Kevent; - -pub const CTL_KERN = 1; -pub const CTL_DEBUG = 5; - -pub const KERN_PROC_ARGS = 48; // struct: process argv/env -pub const KERN_PROC_PATHNAME = 5; // path to executable - -pub const PATH_MAX = 1024; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -pub const PROT_NONE = 0; -pub const PROT_READ = 1; -pub const PROT_WRITE = 2; -pub const PROT_EXEC = 4; - -pub const CLOCK_REALTIME = 0; -pub const CLOCK_VIRTUAL = 1; -pub const CLOCK_PROF = 2; -pub const CLOCK_MONOTONIC = 3; -pub const CLOCK_THREAD_CPUTIME_ID = 0x20000000; -pub const CLOCK_PROCESS_CPUTIME_ID = 0x40000000; - -pub const MAP_FAILED = maxInt(usize); -pub const MAP_SHARED = 0x0001; -pub const MAP_PRIVATE = 0x0002; -pub const MAP_REMAPDUP = 0x0004; -pub const MAP_FIXED = 0x0010; -pub const MAP_RENAME = 0x0020; -pub const MAP_NORESERVE = 0x0040; -pub const MAP_INHERIT = 0x0080; -pub const MAP_HASSEMAPHORE = 0x0200; -pub const MAP_TRYFIXED = 0x0400; -pub const MAP_WIRED = 0x0800; - -pub const MAP_FILE = 0x0000; -pub const MAP_NOSYNC = 0x0800; -pub const MAP_ANON = 0x1000; -pub const MAP_ANONYMOUS = MAP_ANON; -pub const MAP_STACK = 0x2000; - -pub const WNOHANG = 0x00000001; -pub const WUNTRACED = 0x00000002; -pub const WSTOPPED = WUNTRACED; -pub const WCONTINUED = 0x00000010; -pub const WNOWAIT = 0x00010000; -pub const WEXITED = 0x00000020; -pub const WTRAPPED = 0x00000040; - -pub const SA_ONSTACK = 0x0001; -pub const SA_RESTART = 0x0002; -pub const SA_RESETHAND = 0x0004; -pub const SA_NOCLDSTOP = 0x0008; -pub const SA_NODEFER = 0x0010; -pub const SA_NOCLDWAIT = 0x0020; -pub const SA_SIGINFO = 0x0040; - -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 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 SIGIO = 23; -pub const SIGXCPU = 24; -pub const SIGXFSZ = 25; -pub const SIGVTALRM = 26; -pub const SIGPROF = 27; -pub const SIGWINCH = 28; -pub const SIGINFO = 29; -pub const SIGUSR1 = 30; -pub const SIGUSR2 = 31; -pub const SIGPWR = 32; - -pub const SIGRTMIN = 33; -pub const SIGRTMAX = 63; - -// access function -pub const F_OK = 0; // test for existence of file -pub const X_OK = 1; // test for execute or search permission -pub const W_OK = 2; // test for write permission -pub const R_OK = 4; // test for read permission - -pub const O_RDONLY = 0x0000; -pub const O_WRONLY = 0x0001; -pub const O_RDWR = 0x0002; -pub const O_ACCMODE = 0x0003; - -pub const O_CREAT = 0x0200; -pub const O_EXCL = 0x0800; -pub const O_NOCTTY = 0x8000; -pub const O_TRUNC = 0x0400; -pub const O_APPEND = 0x0008; -pub const O_NONBLOCK = 0x0004; -pub const O_DSYNC = 0x00010000; -pub const O_SYNC = 0x0080; -pub const O_RSYNC = 0x00020000; -pub const O_DIRECTORY = 0x00080000; -pub const O_NOFOLLOW = 0x00000100; -pub const O_CLOEXEC = 0x00400000; - -pub const O_ASYNC = 0x0040; -pub const O_DIRECT = 0x00080000; -pub const O_LARGEFILE = 0; -pub const O_NOATIME = 0; -pub const O_PATH = 0; -pub const O_TMPFILE = 0; -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_GETOWN = 5; -pub const F_SETOWN = 6; - -pub const F_GETLK = 7; -pub const F_SETLK = 8; -pub const F_SETLKW = 9; - -pub const SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -pub const SIG_BLOCK = 1; -pub const SIG_UNBLOCK = 2; -pub const SIG_SETMASK = 3; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; - -pub const SOCK_CLOEXEC = 0x10000000; -pub const SOCK_NONBLOCK = 0x20000000; - -pub const PROTO_ip = 0; -pub const PROTO_icmp = 1; -pub const PROTO_igmp = 2; -pub const PROTO_ggp = 3; -pub const PROTO_ipencap = 4; -pub const PROTO_tcp = 6; -pub const PROTO_egp = 8; -pub const PROTO_pup = 12; -pub const PROTO_udp = 17; -pub const PROTO_xns_idp = 22; -pub const PROTO_iso_tp4 = 29; -pub const PROTO_ipv6 = 41; -pub const PROTO_ipv6_route = 43; -pub const PROTO_ipv6_frag = 44; -pub const PROTO_rsvp = 46; -pub const PROTO_gre = 47; -pub const PROTO_esp = 50; -pub const PROTO_ah = 51; -pub const PROTO_ipv6_icmp = 58; -pub const PROTO_ipv6_nonxt = 59; -pub const PROTO_ipv6_opts = 60; -pub const PROTO_encap = 98; -pub const PROTO_pim = 103; -pub const PROTO_raw = 255; - -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_APPLETALK = 16; -pub const PF_INET6 = 24; -pub const PF_DECnet = 12; -pub const PF_KEY = 29; -pub const PF_ROUTE = 34; -pub const PF_SNA = 11; -pub const PF_MPLS = 33; -pub const PF_CAN = 35; -pub const PF_BLUETOOTH = 31; -pub const PF_ISDN = 26; -pub const PF_MAX = 37; - -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_APPLETALK = PF_APPLETALK; -pub const AF_INET6 = PF_INET6; -pub const AF_KEY = PF_KEY; -pub const AF_ROUTE = PF_ROUTE; -pub const AF_SNA = PF_SNA; -pub const AF_MPLS = PF_MPLS; -pub const AF_CAN = PF_CAN; -pub const AF_BLUETOOTH = PF_BLUETOOTH; -pub const AF_ISDN = PF_ISDN; -pub const AF_MAX = PF_MAX; - -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; - -/// add event to kq (implies enable) -pub const EV_ADD = 0x0001; - -/// delete event from kq -pub const EV_DELETE = 0x0002; - -/// enable event -pub const EV_ENABLE = 0x0004; - -/// disable event (not reported) -pub const EV_DISABLE = 0x0008; - -/// only report one occurrence -pub const EV_ONESHOT = 0x0010; - -/// clear event state after reporting -pub const EV_CLEAR = 0x0020; - -/// force immediate event output -/// ... with or without EV_ERROR -/// ... use KEVENT_FLAG_ERROR_EVENTS -/// on syscalls supporting flags -pub const EV_RECEIPT = 0x0040; - -/// disable event after reporting -pub const EV_DISPATCH = 0x0080; - -pub const EVFILT_READ = 0; -pub const EVFILT_WRITE = 1; - -/// attached to aio requests -pub const EVFILT_AIO = 2; - -/// attached to vnodes -pub const EVFILT_VNODE = 3; - -/// attached to struct proc -pub const EVFILT_PROC = 4; - -/// attached to struct proc -pub const EVFILT_SIGNAL = 5; - -/// timers -pub const EVFILT_TIMER = 6; - -/// Filesystem events -pub const EVFILT_FS = 7; - -/// On input, NOTE_TRIGGER causes the event to be triggered for output. -pub const NOTE_TRIGGER = 0x08000000; - -/// low water mark -pub const NOTE_LOWAT = 0x00000001; - -/// vnode was removed -pub const NOTE_DELETE = 0x00000001; - -/// data contents changed -pub const NOTE_WRITE = 0x00000002; - -/// size increased -pub const NOTE_EXTEND = 0x00000004; - -/// attributes changed -pub const NOTE_ATTRIB = 0x00000008; - -/// link count changed -pub const NOTE_LINK = 0x00000010; - -/// vnode was renamed -pub const NOTE_RENAME = 0x00000020; - -/// vnode access was revoked -pub const NOTE_REVOKE = 0x00000040; - -/// process exited -pub const NOTE_EXIT = 0x80000000; - -/// process forked -pub const NOTE_FORK = 0x40000000; - -/// process exec'd -pub const NOTE_EXEC = 0x20000000; - -/// mask for signal & exit status -pub const NOTE_PDATAMASK = 0x000fffff; -pub const NOTE_PCTRLMASK = 0xf0000000; - -pub const TIOCCBRK = 0x2000747a; -pub const TIOCCDTR = 0x20007478; -pub const TIOCCONS = 0x80047462; -pub const TIOCDCDTIMESTAMP = 0x40107458; -pub const TIOCDRAIN = 0x2000745e; -pub const TIOCEXCL = 0x2000740d; -pub const TIOCEXT = 0x80047460; -pub const TIOCFLAG_CDTRCTS = 0x10; -pub const TIOCFLAG_CLOCAL = 0x2; -pub const TIOCFLAG_CRTSCTS = 0x4; -pub const TIOCFLAG_MDMBUF = 0x8; -pub const TIOCFLAG_SOFTCAR = 0x1; -pub const TIOCFLUSH = 0x80047410; -pub const TIOCGETA = 0x402c7413; -pub const TIOCGETD = 0x4004741a; -pub const TIOCGFLAGS = 0x4004745d; -pub const TIOCGLINED = 0x40207442; -pub const TIOCGPGRP = 0x40047477; -pub const TIOCGQSIZE = 0x40047481; -pub const TIOCGRANTPT = 0x20007447; -pub const TIOCGSID = 0x40047463; -pub const TIOCGSIZE = 0x40087468; -pub const TIOCGWINSZ = 0x40087468; -pub const TIOCMBIC = 0x8004746b; -pub const TIOCMBIS = 0x8004746c; -pub const TIOCMGET = 0x4004746a; -pub const TIOCMSET = 0x8004746d; -pub const TIOCM_CAR = 0x40; -pub const TIOCM_CD = 0x40; -pub const TIOCM_CTS = 0x20; -pub const TIOCM_DSR = 0x100; -pub const TIOCM_DTR = 0x2; -pub const TIOCM_LE = 0x1; -pub const TIOCM_RI = 0x80; -pub const TIOCM_RNG = 0x80; -pub const TIOCM_RTS = 0x4; -pub const TIOCM_SR = 0x10; -pub const TIOCM_ST = 0x8; -pub const TIOCNOTTY = 0x20007471; -pub const TIOCNXCL = 0x2000740e; -pub const TIOCOUTQ = 0x40047473; -pub const TIOCPKT = 0x80047470; -pub const TIOCPKT_DATA = 0x0; -pub const TIOCPKT_DOSTOP = 0x20; -pub const TIOCPKT_FLUSHREAD = 0x1; -pub const TIOCPKT_FLUSHWRITE = 0x2; -pub const TIOCPKT_IOCTL = 0x40; -pub const TIOCPKT_NOSTOP = 0x10; -pub const TIOCPKT_START = 0x8; -pub const TIOCPKT_STOP = 0x4; -pub const TIOCPTMGET = 0x40287446; -pub const TIOCPTSNAME = 0x40287448; -pub const TIOCRCVFRAME = 0x80087445; -pub const TIOCREMOTE = 0x80047469; -pub const TIOCSBRK = 0x2000747b; -pub const TIOCSCTTY = 0x20007461; -pub const TIOCSDTR = 0x20007479; -pub const TIOCSETA = 0x802c7414; -pub const TIOCSETAF = 0x802c7416; -pub const TIOCSETAW = 0x802c7415; -pub const TIOCSETD = 0x8004741b; -pub const TIOCSFLAGS = 0x8004745c; -pub const TIOCSIG = 0x2000745f; -pub const TIOCSLINED = 0x80207443; -pub const TIOCSPGRP = 0x80047476; -pub const TIOCSQSIZE = 0x80047480; -pub const TIOCSSIZE = 0x80087467; -pub const TIOCSTART = 0x2000746e; -pub const TIOCSTAT = 0x80047465; -pub const TIOCSTI = 0x80017472; -pub const TIOCSTOP = 0x2000746f; -pub const TIOCSWINSZ = 0x80087467; -pub const TIOCUCNTL = 0x80047466; -pub const TIOCXMTFRAME = 0x80087444; - -pub const sockaddr = c.sockaddr; -pub const sockaddr_in = c.sockaddr_in; -pub const sockaddr_in6 = c.sockaddr_in6; - -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); -} -fn signed(s: u32) i32 { - return @bitCast(i32, s); -} -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) >> 8) & 0xff); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { - return WEXITSTATUS(s); -} -pub fn WIFEXITED(s: i32) bool { - return WTERMSIG(s) == 0; -} - -pub fn WIFCONTINUED(s: i32) bool { - return ((s & 0x7f) == 0xffff); -} - -pub fn WIFSTOPPED(s: i32) bool { - return ((s & 0x7f != 0x7f) and !WIFCONTINUED(s)); -} - -pub fn WIFSIGNALED(s: i32) bool { - return !WIFSTOPPED(s) and !WIFCONTINUED(s) and !WIFEXITED(s); -} - -pub const winsize = extern struct { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16, -}; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) usize { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - return errnoWrap(c.dup2(old, new)); -} - -pub fn chdir(path: [*]const u8) usize { - return errnoWrap(c.chdir(path)); -} - -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return errnoWrap(c.execve(path, argv, envp)); -} - -pub fn fork() usize { - return errnoWrap(c.fork()); -} - -pub fn access(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.access(path, mode)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return errnoWrap(@bitCast(isize, c.getdents(fd, drip, count))); -} - -pub fn getdirentries(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize { - return errnoWrap(@bitCast(isize, c.getdirentries(fd, buf_ptr, buf_len, basep))); -} - -pub fn realpath(noalias filename: [*]const u8, noalias resolved_name: [*]u8) usize { - return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0; -} - -pub fn isatty(fd: i32) bool { - return c.isatty(fd) != 0; -} - -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return errnoWrap(c.readlink(path, buf_ptr, buf_len)); -} - -pub fn mkdir(path: [*]const u8, mode: u32) usize { - return errnoWrap(c.mkdir(path, mode)); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - const ptr_result = c.mmap( - @ptrCast(?*c_void, address), - length, - @bitCast(c_int, @intCast(c_uint, prot)), - @bitCast(c_int, c_uint(flags)), - fd, - offset, - ); - const isize_result = @bitCast(isize, @ptrToInt(ptr_result)); - return errnoWrap(isize_result); -} - -pub fn munmap(address: usize, length: usize) usize { - return errnoWrap(c.munmap(@intToPtr(*c_void, address), length)); -} - -pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize { - return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte)); -} - -pub fn rmdir(path: [*]const u8) usize { - return errnoWrap(c.rmdir(path)); -} - -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.symlink(existing, new)); -} - -pub fn pread(fd: i32, buf: [*]u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pread(fd, @ptrCast(*c_void, buf), nbyte, offset)); -} - -pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize { - return errnoWrap(c.preadv(fd, @ptrCast(*const c_void, iov), @intCast(c_int, count), offset)); -} - -pub fn pipe(fd: *[2]i32) usize { - return pipe2(fd, 0); -} - -pub fn pipe2(fd: *[2]i32, flags: u32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.pipe2(@ptrCast(*[2]c_int, fd), flags)); -} - -pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize { - return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte)); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, nbyte: usize, offset: u64) usize { - return errnoWrap(c.pwrite(fd, @ptrCast(*const c_void, buf), nbyte, offset)); -} - -pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize { - return errnoWrap(c.pwritev(fd, @ptrCast(*const c_void, iov), @intCast(c_int, count), offset)); -} - -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - return errnoWrap(c.rename(old, new)); -} - -pub fn open(path: [*]const u8, flags: u32, mode: usize) usize { - return errnoWrap(c.open(path, @bitCast(c_int, flags), mode)); -} - -pub fn create(path: [*]const u8, perm: usize) usize { - return arch.syscall2(SYS_creat, @ptrToInt(path), perm); -} - -pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize { - return errnoWrap(c.openat(@bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode)); -} - -pub fn close(fd: i32) usize { - return errnoWrap(c.close(fd)); -} - -pub fn lseek(fd: i32, offset: isize, whence: c_int) usize { - return errnoWrap(c.lseek(fd, offset, whence)); -} - -pub fn exit(code: i32) noreturn { - c.exit(code); -} - -pub fn kill(pid: i32, sig: i32) usize { - return errnoWrap(c.kill(pid, sig)); -} - -pub fn unlink(path: [*]const u8) usize { - return errnoWrap(c.unlink(path)); -} - -pub fn waitpid(pid: i32, status: *i32, options: u32) usize { - comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options))); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return errnoWrap(c.nanosleep(req, rem)); -} - -pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { - return errnoWrap(c.clock_gettime(clk_id, tp)); -} - -pub fn clock_getres(clk_id: i32, tp: *timespec) usize { - return errnoWrap(c.clock_getres(clk_id, tp)); -} - -pub fn setuid(uid: u32) usize { - return errnoWrap(c.setuid(uid)); -} - -pub fn setgid(gid: u32) usize { - return errnoWrap(c.setgid(gid)); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return errnoWrap(c.setreuid(ruid, euid)); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return errnoWrap(c.setregid(rgid, egid)); -} - -const NSIG = 32; - -pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); -pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); -pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = extern struct { - /// signal handler - __sigaction_u: extern union { - __sa_handler: extern fn (i32) void, - __sa_sigaction: extern fn (i32, *__siginfo, usize) void, - }, - - /// see signal options - sa_flags: u32, - - /// signal mask to apply - sa_mask: sigset_t, -}; - -pub const _SIG_WORDS = 4; -pub const _SIG_MAXSIG = 128; - -pub inline fn _SIG_IDX(sig: usize) usize { - return sig - 1; -} -pub inline fn _SIG_WORD(sig: usize) usize { - return_SIG_IDX(sig) >> 5; -} -pub inline fn _SIG_BIT(sig: usize) usize { - return 1 << (_SIG_IDX(sig) & 31); -} -pub inline fn _SIG_VALID(sig: usize) usize { - return sig <= _SIG_MAXSIG and sig > 0; -} - -pub const sigset_t = extern struct { - __bits: [_SIG_WORDS]u32, -}; - -pub fn raise(sig: i32) usize { - return errnoWrap(c.raise(sig)); -} - -pub const Stat = c.Stat; -pub const dirent = c.dirent; -pub const timespec = c.timespec; - -pub fn fstat(fd: i32, buf: *c.Stat) usize { - return errnoWrap(c.fstat(fd, buf)); -} -pub const iovec = extern struct { - iov_base: [*]u8, - iov_len: usize, -}; - -pub const iovec_const = extern struct { - iov_base: [*]const u8, - iov_len: usize, -}; - -// TODO avoid libc dependency -pub fn kqueue() usize { - return errnoWrap(c.kqueue()); -} - -// TODO avoid libc dependency -pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize { - return errnoWrap(c.kevent( - kq, - changelist.ptr, - @intCast(c_int, changelist.len), - eventlist.ptr, - @intCast(c_int, eventlist.len), - timeout, - )); -} - -// TODO avoid libc dependency -pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen)); -} - -// TODO avoid libc dependency -pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { - return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen)); -} - -// TODO avoid libc dependency -pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize { - return errnoWrap(c.sysctlnametomib(name, wibp, sizep)); -} - -// TODO avoid libc dependency - -/// Takes the return value from a syscall and formats it back in the way -/// that the kernel represents it to libc. Errno was a mistake, let's make -/// it go away forever. -fn errnoWrap(value: isize) usize { - return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value); -} +pub const is_the_target = builtin.os == .netbsd; +pub use std.c; diff --git a/std/os/netbsd/errno.zig b/std/os/netbsd/errno.zig @@ -1,134 +0,0 @@ -pub const EPERM = 1; // Operation not permitted -pub const ENOENT = 2; // No such file or directory -pub const ESRCH = 3; // No such process -pub const EINTR = 4; // Interrupted system call -pub const EIO = 5; // Input/output error -pub const ENXIO = 6; // Device not configured -pub const E2BIG = 7; // Argument list too long -pub const ENOEXEC = 8; // Exec format error -pub const EBADF = 9; // Bad file descriptor -pub const ECHILD = 10; // No child processes -pub const EDEADLK = 11; // Resource deadlock avoided -// 11 was EAGAIN -pub const ENOMEM = 12; // Cannot allocate memory -pub const EACCES = 13; // Permission denied -pub const EFAULT = 14; // Bad address -pub const ENOTBLK = 15; // Block device required -pub const EBUSY = 16; // Device busy -pub const EEXIST = 17; // File exists -pub const EXDEV = 18; // Cross-device link -pub const ENODEV = 19; // Operation not supported by device -pub const ENOTDIR = 20; // Not a directory -pub const EISDIR = 21; // Is a directory -pub const EINVAL = 22; // Invalid argument -pub const ENFILE = 23; // Too many open files in system -pub const EMFILE = 24; // Too many open files -pub const ENOTTY = 25; // Inappropriate ioctl for device -pub const ETXTBSY = 26; // Text file busy -pub const EFBIG = 27; // File too large -pub const ENOSPC = 28; // No space left on device -pub const ESPIPE = 29; // Illegal seek -pub const EROFS = 30; // Read-only file system -pub const EMLINK = 31; // Too many links -pub const EPIPE = 32; // Broken pipe - -// math software -pub const EDOM = 33; // Numerical argument out of domain -pub const ERANGE = 34; // Result too large or too small - -// non-blocking and interrupt i/o -pub const EAGAIN = 35; // Resource temporarily unavailable -pub const EWOULDBLOCK = EAGAIN; // Operation would block -pub const EINPROGRESS = 36; // Operation now in progress -pub const EALREADY = 37; // Operation already in progress - -// ipc/network software -- argument errors -pub const ENOTSOCK = 38; // Socket operation on non-socket -pub const EDESTADDRREQ = 39; // Destination address required -pub const EMSGSIZE = 40; // Message too long -pub const EPROTOTYPE = 41; // Protocol wrong type for socket -pub const ENOPROTOOPT = 42; // Protocol option not available -pub const EPROTONOSUPPORT = 43; // Protocol not supported -pub const ESOCKTNOSUPPORT = 44; // Socket type not supported -pub const EOPNOTSUPP = 45; // Operation not supported -pub const EPFNOSUPPORT = 46; // Protocol family not supported -pub const EAFNOSUPPORT = 47; // Address family not supported by protocol family -pub const EADDRINUSE = 48; // Address already in use -pub const EADDRNOTAVAIL = 49; // Can't assign requested address - -// ipc/network software -- operational errors -pub const ENETDOWN = 50; // Network is down -pub const ENETUNREACH = 51; // Network is unreachable -pub const ENETRESET = 52; // Network dropped connection on reset -pub const ECONNABORTED = 53; // Software caused connection abort -pub const ECONNRESET = 54; // Connection reset by peer -pub const ENOBUFS = 55; // No buffer space available -pub const EISCONN = 56; // Socket is already connected -pub const ENOTCONN = 57; // Socket is not connected -pub const ESHUTDOWN = 58; // Can't send after socket shutdown -pub const ETOOMANYREFS = 59; // Too many references: can't splice -pub const ETIMEDOUT = 60; // Operation timed out -pub const ECONNREFUSED = 61; // Connection refused - -pub const ELOOP = 62; // Too many levels of symbolic links -pub const ENAMETOOLONG = 63; // File name too long - -// should be rearranged -pub const EHOSTDOWN = 64; // Host is down -pub const EHOSTUNREACH = 65; // No route to host -pub const ENOTEMPTY = 66; // Directory not empty - -// quotas & mush -pub const EPROCLIM = 67; // Too many processes -pub const EUSERS = 68; // Too many users -pub const EDQUOT = 69; // Disc quota exceeded - -// Network File System -pub const ESTALE = 70; // Stale NFS file handle -pub const EREMOTE = 71; // Too many levels of remote in path -pub const EBADRPC = 72; // RPC struct is bad -pub const ERPCMISMATCH = 73; // RPC version wrong -pub const EPROGUNAVAIL = 74; // RPC prog. not avail -pub const EPROGMISMATCH = 75; // Program version wrong -pub const EPROCUNAVAIL = 76; // Bad procedure for program - -pub const ENOLCK = 77; // No locks available -pub const ENOSYS = 78; // Function not implemented - -pub const EFTYPE = 79; // Inappropriate file type or format -pub const EAUTH = 80; // Authentication error -pub const ENEEDAUTH = 81; // Need authenticator - -// SystemV IPC -pub const EIDRM = 82; // Identifier removed -pub const ENOMSG = 83; // No message of desired type -pub const EOVERFLOW = 84; // Value too large to be stored in data type - -// Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 -pub const EILSEQ = 85; // Illegal byte sequence - -// From IEEE Std 1003.1-2001 -// Base, Realtime, Threads or Thread Priority Scheduling option errors -pub const ENOTSUP = 86; // Not supported - -// Realtime option errors -pub const ECANCELED = 87; // Operation canceled - -// Realtime, XSI STREAMS option errors -pub const EBADMSG = 88; // Bad or Corrupt message - -// XSI STREAMS option errors -pub const ENODATA = 89; // No message available -pub const ENOSR = 90; // No STREAM resources -pub const ENOSTR = 91; // Not a STREAM -pub const ETIME = 92; // STREAM ioctl timeout - -// File system extended attribute errors -pub const ENOATTR = 93; // Attribute not found - -// Realtime, XSI STREAMS option errors -pub const EMULTIHOP = 94; // Multihop attempted -pub const ENOLINK = 95; // Link has been severed -pub const EPROTO = 96; // Protocol error - -pub const ELAST = 96; // Must equal largest errno diff --git a/std/os/path.zig b/std/os/path.zig @@ -1162,10 +1162,8 @@ pub const RealError = error{ /// On Windows, file paths must be valid Unicode. InvalidUtf8, - /// TODO remove this possibility PathAlreadyExists, - /// TODO remove this possibility Unexpected, }; diff --git a/std/os/posix.zig b/std/os/posix.zig @@ -33,10 +33,7 @@ const wasi = os.wasi; const linux = os.linux; const testing = std.testing; -pub const FileHandle = if (windows.is_the_target) windows.HANDLE else if (wasi.is_the_target) wasi.fd_t else i32; -pub use system.errno_codes; - -pub const PATH_MAX = system.PATH_MAX; +pub use system.posix; /// > The maximum path of 32,767 characters is approximate, because the "\\?\" /// > prefix may be expanded to a longer string by the system at run time, and @@ -44,9 +41,6 @@ pub const PATH_MAX = system.PATH_MAX; /// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation pub const PATH_MAX_WIDE = 32767; -pub const iovec = system.iovec; -pub const iovec_const = system.iovec_const; - /// See also `getenv`. pub var environ: [][*]u8 = undefined; @@ -58,23 +52,23 @@ pub var environ: [][*]u8 = undefined; /// value of. pub const errno = system.getErrno; -/// Closes the file handle. +/// Closes the file descriptor. /// This function is not capable of returning any indication of failure. An /// application which wants to ensure writes have succeeded before closing /// must call `fsync` before `close`. /// Note: The Zig standard library does not support POSIX thread cancellation. -pub fn close(handle: FileHandle) void { +pub fn close(fd: fd_t) void { if (windows.is_the_target and !builtin.link_libc) { - assert(windows.CloseHandle(handle) != 0); + assert(windows.CloseHandle(fd) != 0); return; } if (wasi.is_the_target) { - switch (wasi.fd_close(handle)) { + switch (wasi.fd_close(fd)) { 0 => return, else => |err| return unexpectedErrno(err), } } - switch (system.getErrno(system.close(handle))) { + switch (errno(system.close(fd))) { EBADF => unreachable, // Always a race condition. EINTR => return, // This is still a success. See https://github.com/ziglang/zig/issues/2425 else => return, @@ -103,7 +97,7 @@ pub fn getrandom(buf: []u8) GetRandomError!void { } if (linux.is_the_target) { while (true) { - switch (system.getErrno(system.getrandom(buf.ptr, buf.len, 0))) { + switch (errno(system.getrandom(buf.ptr, buf.len, 0))) { 0 => return, EINVAL => unreachable, EFAULT => unreachable, @@ -146,7 +140,7 @@ test "os.getRandomBytes" { pub fn abort() noreturn { @setCold(true); if (builtin.link_libc) { - c.abort(); + system.abort(); } if (windows.is_the_target) { if (builtin.mode == .Debug) { @@ -171,7 +165,7 @@ pub const RaiseError = error{}; pub fn raise(sig: u8) RaiseError!void { if (builtin.link_libc) { - switch (system.getErrno(system.raise(sig))) { + switch (errno(system.raise(sig))) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -193,7 +187,7 @@ pub fn raise(sig: u8) RaiseError!void { const tid = system.syscall0(system.SYS_gettid); const rc = system.syscall2(system.SYS_tkill, tid, sig); system.restoreSignals(&set); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -202,7 +196,7 @@ pub fn raise(sig: u8) RaiseError!void { /// Exits the program cleanly with the specified status code. pub fn exit(status: u8) noreturn { if (builtin.link_libc) { - std.c.exit(status); + system.exit(status); } if (windows.is_the_target) { windows.ExitProcess(status); @@ -229,7 +223,7 @@ pub const ReadError = error{ /// buf.len. If 0 bytes were read, that means EOF. /// This function is for blocking file descriptors only. For non-blocking, see /// `readAsync`. -pub fn read(fd: FileHandle, buf: []u8) ReadError!usize { +pub fn read(fd: fd_t, buf: []u8) ReadError!usize { if (windows.is_the_target and !builtin.link_libc) { var index: usize = 0; while (index < buffer.len) { @@ -270,7 +264,7 @@ pub fn read(fd: FileHandle, buf: []u8) ReadError!usize { while (index < buf.len) { const want_to_read = math.min(buf.len - index, usize(max_buf_len)); const rc = system.read(fd, buf.ptr + index, want_to_read); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => { index += rc; if (rc == want_to_read) continue; @@ -295,7 +289,7 @@ pub fn read(fd: FileHandle, buf: []u8) ReadError!usize { /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// This function is for blocking file descriptors only. For non-blocking, see /// `preadvAsync`. -pub fn preadv(fd: FileHandle, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { +pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { if (os.darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; @@ -335,8 +329,7 @@ pub fn preadv(fd: FileHandle, iov: [*]const iovec, count: usize, offset: u64) Re } while (true) { const rc = system.preadv(fd, iov, count, offset); - const err = system.getErrno(rc); - switch (err) { + switch (errno(rc)) { 0 => return rc, EINTR => continue, EINVAL => unreachable, @@ -347,7 +340,7 @@ pub fn preadv(fd: FileHandle, iov: [*]const iovec, count: usize, offset: u64) Re EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, - else => return unexpectedErrno(err), + else => |err| return unexpectedErrno(err), } } } @@ -367,7 +360,7 @@ pub const WriteError = error{ /// Write to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `writeAsync`. -pub fn write(fd: FileHandle, bytes: []const u8) WriteError!void { +pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { if (windows.is_the_target and !builtin.link_libc) { var bytes_written: windows.DWORD = undefined; // TODO replace this @intCast with a loop that writes all the bytes @@ -404,8 +397,7 @@ pub fn write(fd: FileHandle, bytes: []const u8) WriteError!void { while (index < bytes.len) { const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len)); const rc = system.write(fd, bytes.ptr + index, amt_to_write); - const write_err = system.getErrno(rc); - switch (write_err) { + switch (errno(rc)) { 0 => { index += rc; continue; @@ -422,7 +414,7 @@ pub fn write(fd: FileHandle, bytes: []const u8) WriteError!void { ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, - else => return unexpectedErrno(write_err), + else => |err| return unexpectedErrno(err), } } } @@ -430,7 +422,7 @@ pub fn write(fd: FileHandle, bytes: []const u8) WriteError!void { /// Write multiple buffers to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `pwritevAsync`. -pub fn pwritev(fd: FileHandle, iov: [*]const iovec_const, count: usize, offset: u64) WriteError!void { +pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) WriteError!void { if (darwin.is_the_target) { // Darwin does not have pwritev but it does have pwrite. var off: usize = 0; @@ -473,8 +465,7 @@ pub fn pwritev(fd: FileHandle, iov: [*]const iovec_const, count: usize, offset: while (true) { const rc = system.pwritev(fd, iov, count, offset); - const err = system.getErrno(rc); - switch (err) { + switch (errno(rc)) { 0 => return, EINTR => continue, EINVAL => unreachable, @@ -488,7 +479,7 @@ pub fn pwritev(fd: FileHandle, iov: [*]const iovec_const, count: usize, offset: ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, - else => return unexpectedErrno(err), + else => |err| return unexpectedErrno(err), } } } @@ -514,7 +505,7 @@ pub const OpenError = error{ /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` needs to be copied in memory to add a null terminating byte. /// See also `openC`. -pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!FileHandle { +pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t { const file_path_c = try toPosixPath(file_path); return openC(&file_path_c, flags, perm); } @@ -522,11 +513,11 @@ pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!FileHandle /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `open`. /// TODO https://github.com/ziglang/zig/issues/265 -pub fn openC(file_path: [*]const u8, flags: u32, perm: usize) OpenError!FileHandle { +pub fn openC(file_path: [*]const u8, flags: u32, perm: usize) OpenError!fd_t { while (true) { const rc = system.open(file_path, flags, perm); - switch (system.getErrno(rc)) { - 0 => return @intCast(FileHandle, rc), + switch (errno(rc)) { + 0 => return @intCast(fd_t, rc), EINTR => continue, EFAULT => unreachable, @@ -582,7 +573,7 @@ pub fn openWindows( share_mode: windows.DWORD, creation_disposition: windows.DWORD, flags_and_attrs: windows.DWORD, -) WindowsOpenError!FileHandle { +) WindowsOpenError!fd_t { const file_path_w = try sliceToPrefixedFileW(file_path); return openW(&file_path_w, desired_access, share_mode, creation_disposition, flags_and_attrs); } @@ -613,9 +604,9 @@ pub fn openW( return result; } -pub fn dup2(old_fd: FileHandle, new_fd: FileHandle) !void { +pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { while (true) { - switch (system.getErrno(system.dup2(old_fd, new_fd))) { + switch (errno(system.dup2(old_fd, new_fd))) { 0 => return, EBUSY, EINTR => continue, EMFILE => return error.ProcessFdQuotaExceeded, @@ -630,6 +621,7 @@ pub fn dup2(old_fd: FileHandle, new_fd: FileHandle) !void { /// pointers after the args and after the environment variables. /// `argv[0]` is the executable path. /// This function also uses the PATH environment variable to get the full path to the executable. +/// TODO provide execveC which does not take an allocator pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const BufMap) !void { const argv_buf = try allocator.alloc(?[*]u8, argv.len + 1); mem.set(?[*]u8, argv_buf, null); @@ -654,7 +646,7 @@ pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const B const exe_path = argv[0]; if (mem.indexOfScalar(u8, exe_path, '/') != null) { - return execveErrnoToErr(system.getErrno(system.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr))); + return execveErrnoToErr(errno(system.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr))); } const PATH = getenv("PATH") orelse "/usr/local/bin:/bin/:/usr/bin"; @@ -671,7 +663,7 @@ pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const B path_buf[search_path.len] = '/'; mem.copy(u8, path_buf[search_path.len + 1 ..], exe_path); path_buf[search_path.len + exe_path.len + 1] = 0; - err = system.getErrno(system.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr)); + err = errno(system.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr)); assert(err > 0); if (err == EACCES) { seen_eacces = true; @@ -776,7 +768,7 @@ pub fn getenv(key: []const u8) ?[]const u8 { /// TODO https://github.com/ziglang/zig/issues/265 pub fn getenvC(key: [*]const u8) ?[]const u8 { if (builtin.link_libc) { - const value = std.c.getenv(key) orelse return null; + const value = system.getenv(key) orelse return null; return mem.toSliceConst(u8, value); } return getenv(mem.toSliceConst(u8, key)); @@ -785,7 +777,7 @@ pub fn getenvC(key: [*]const u8) ?[]const u8 { /// See std.elf for the constants. pub fn getauxval(index: usize) usize { if (builtin.link_libc) { - return usize(std.c.getauxval(index)); + return usize(system.getauxval(index)); } else if (linux.elf_aux_maybe) |auxv| { var i: usize = 0; while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { @@ -829,9 +821,9 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } const err = if (builtin.link_libc) blk: { - break :blk if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; + break :blk if (system.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else system._errno().*; } else blk: { - break :blk system.getErrno(system.getcwd(out_buffer, out_buffer.len)); + break :blk errno(system.getcwd(out_buffer, out_buffer.len)); }; switch (err) { 0 => return mem.toSlice(u8, out_buffer), @@ -883,21 +875,14 @@ pub fn symlink(target_path: []const u8, new_path: []const u8) SymLinkError!void } } -pub fn symlinkat(target_path: []const u8, newdirfd: FileHandle, new_path: []const u8) SymLinkError!void { +pub fn symlinkat(target_path: []const u8, newdirfd: fd_t, new_path: []const u8) SymLinkError!void { const target_path_c = try toPosixPath(target_path); const new_path_c = try toPosixPath(new_path); return symlinkatC(target_path_c, newdirfd, new_path_c); } -pub fn symlinkatC(target_path: [*]const u8, newdirfd: FileHandle, new_path: [*]const u8) SymLinkError!void { - const err = blk: { - if (builtin.link_libc) { - break :blk if (std.c.symlinkat(target_path, newdirfd, new_path) == -1) errno().* else 0; - } else { - break :blk system.getErrno(system.symlinkat(target_path, newdirfd, new_path)); - } - }; - switch (err) { +pub fn symlinkatC(target_path: [*]const u8, newdirfd: fd_t, new_path: [*]const u8) SymLinkError!void { + switch (errno(system.symlinkat(target_path, newdirfd, new_path))) { 0 => return, EFAULT => unreachable, EINVAL => unreachable, @@ -913,7 +898,7 @@ pub fn symlinkatC(target_path: [*]const u8, newdirfd: FileHandle, new_path: [*]c ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, EROFS => return error.ReadOnlyFileSystem, - else => return unexpectedErrno(err), + else => |err| return unexpectedErrno(err), } } @@ -925,12 +910,10 @@ pub fn symlinkC(target_path: [*]const u8, new_path: [*]const u8) SymLinkError!vo const new_path_w = try cStrToPrefixedFileW(new_path); return symlinkW(&target_path_w, &new_path_w); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.symlink(target_path, new_path) == -1) errno().* else 0; - } else if (@hasDecl(system, "symlink")) blk: { - break :blk system.getErrno(system.symlink(target_path, new_path)); + const err = if (builtin.link_libc or @hasDecl(system, "SYS_symlink")) blk: { + break :blk errno(system.symlink(target_path, new_path)); } else blk: { - break :blk system.getErrno(system.symlinkat(target_path, AT_FDCWD, new_path)); + break :blk errno(system.symlinkat(target_path, AT_FDCWD, new_path)); }; switch (err) { 0 => return, @@ -1018,12 +1001,10 @@ pub fn unlinkC(file_path: [*]const u8) UnlinkError!void { const file_path_w = try cStrToPrefixedFileW(file_path); return unlinkW(&file_path_w); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.unlink(file_path) == -1) errno().* else 0; - } else if (@hasDecl(system, "unlink")) blk: { - break :blk system.getErrno(system.unlink(file_path)); + const err = if (builtin.link_libc or @hasDecl(system, "SYS_unlink")) blk: { + break :blk errno(system.unlink(file_path)); } else blk: { - break :blk system.getErrno(system.unlinkat(AT_FDCWD, file_path, 0)); + break :blk errno(system.unlinkat(AT_FDCWD, file_path, 0)); }; switch (err) { 0 => return, @@ -1066,14 +1047,12 @@ pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void { const new_path_w = try cStrToPrefixedFileW(new_path); return renameW(&old_path_w, &new_path_w); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.rename(old_path, new_path) == -1) errno().* else 0; - } else if (@hasDecl(system, "rename")) blk: { - break :blk system.getErrno(system.rename(old_path, new_path)); - } else if (@hasDecl(system, "renameat")) blk: { - break :blk system.getErrno(system.renameat(AT_FDCWD, old_path, AT_FDCWD, new_path)); + const err = if (builtin.link_libc or @hasDecl(system, "SYS_rename")) blk: { + break :blk errno(system.rename(old_path, new_path)); + } else if (@hasDecl(system, "SYS_renameat")) blk: { + break :blk errno(system.renameat(AT_FDCWD, old_path, AT_FDCWD, new_path)); } else blk: { - break :blk system.getErrno(system.renameat2(AT_FDCWD, old_path, AT_FDCWD, new_path, 0)); + break :blk errno(system.renameat2(AT_FDCWD, old_path, AT_FDCWD, new_path, 0)); }; switch (err) { 0 => return, @@ -1131,12 +1110,10 @@ pub fn mkdirC(dir_path: [*]const u8, mode: u32) MakeDirError!void { const dir_path_w = try cStrToPrefixedFileW(dir_path); return mkdirW(&dir_path_w, mode); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.mkdir(dir_path, mode) == -1) errno().* else 0; - } else if (@hasDecl(system, "mkdir")) blk: { - break :blk system.getErrno(system.mkdir(dir_path, mode)); + const err = if (builtin.link_libc or @hasDecl(system, "SYS_mkdir")) blk: { + break :blk errno(system.mkdir(dir_path, mode)); } else blk: { - break :blk system.getErrno(system.mkdirat(AT_FDCWD, dir_path, mode)); + break :blk errno(system.mkdirat(AT_FDCWD, dir_path, mode)); }; switch (err) { 0 => return, @@ -1203,12 +1180,10 @@ pub fn rmdirC(dir_path: [*]const u8) DeleteDirError!void { const dir_path_w = try cStrToPrefixedFileW(dir_path); return rmdirW(&dir_path_w); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.rmdir(dir_path) == -1) errno().* else 0; - } else if (@hasDecl(system, "rmdir")) blk: { - break :blk system.getErrno(system.rmdir(dir_path)); + const err = if (builtin.link_libc or @hasDecl(system, "SYS_rmdir")) blk: { + break :blk errno(system.rmdir(dir_path)); } else blk: { - break :blk system.getErrno(system.unlinkat(AT_FDCWD, dir_path, AT_REMOVEDIR)); + break :blk errno(system.unlinkat(AT_FDCWD, dir_path, AT_REMOVEDIR)); }; switch (err) { 0 => return, @@ -1262,12 +1237,7 @@ pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void { const dir_path_w = try cStrToPrefixedFileW(dir_path); return chdirW(&dir_path_w); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.chdir(dir_path) == -1) errno().* else 0; - } else blk: { - break :blk system.getErrno(system.chdir(dir_path)); - }; - switch (err) { + switch (errno(system.chdir(dir_path))) { 0 => return, EACCES => return error.AccessDenied, EFAULT => unreachable, @@ -1277,7 +1247,7 @@ pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void { ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOTDIR => return error.NotDir, - else => return unexpectedErrno(err), + else => |err| return unexpectedErrno(err), } } @@ -1307,15 +1277,12 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 { const file_path_w = try cStrToPrefixedFileW(file_path); return readlinkW(&file_path_w, out_buffer); } - const err = if (builtin.link_libc) blk: { - break :blk if (std.c.readlink(file_path, out_buffer.ptr, out_buffer.len) == -1) errno().* else 0; - } else if (@hasDecl(system, "readlink")) blk: { - break :blk system.getErrno(system.readlink(file_path, out_buffer.ptr, out_buffer.len)); + const rc = if (builtin.link_libc or @hasDecl(system, "SYS_readlink")) blk: { + break :blk system.readlink(file_path, out_buffer.ptr, out_buffer.len); } else blk: { - break :blk system.getErrno(system.readlinkat(AT_FDCWD, file_path, out_buffer.ptr, out_buffer.len)); + break :blk system.readlinkat(AT_FDCWD, file_path, out_buffer.ptr, out_buffer.len); }; - const rc = system.readlink(file_path, out_buffer, out_buffer.len); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return out_buffer[0..rc], EACCES => return error.AccessDenied, EFAULT => unreachable, @@ -1338,7 +1305,7 @@ pub const SetIdError = error{ }; pub fn setuid(uid: u32) SetIdError!void { - switch (system.getErrno(system.setuid(uid))) { + switch (errno(system.setuid(uid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, @@ -1348,7 +1315,7 @@ pub fn setuid(uid: u32) SetIdError!void { } pub fn setreuid(ruid: u32, euid: u32) SetIdError!void { - switch (system.getErrno(system.setreuid(ruid, euid))) { + switch (errno(system.setreuid(ruid, euid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, @@ -1358,7 +1325,7 @@ pub fn setreuid(ruid: u32, euid: u32) SetIdError!void { } pub fn setgid(gid: u32) SetIdError!void { - switch (system.getErrno(system.setgid(gid))) { + switch (errno(system.setgid(gid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, @@ -1368,7 +1335,7 @@ pub fn setgid(gid: u32) SetIdError!void { } pub fn setregid(rgid: u32, egid: u32) SetIdError!void { - switch (system.getErrno(system.setregid(rgid, egid))) { + switch (errno(system.setregid(rgid, egid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, @@ -1382,7 +1349,7 @@ pub const GetStdHandleError = error{ Unexpected, }; -pub fn GetStdHandle(handle_id: windows.DWORD) GetStdHandleError!FileHandle { +pub fn GetStdHandle(handle_id: windows.DWORD) GetStdHandleError!fd_t { if (windows.is_the_target) { const handle = windows.GetStdHandle(handle_id) orelse return error.NoStandardHandleAttached; if (handle == windows.INVALID_HANDLE_VALUE) { @@ -1402,9 +1369,9 @@ pub fn GetStdHandle(handle_id: windows.DWORD) GetStdHandleError!FileHandle { } /// Test whether a file descriptor refers to a terminal. -pub fn isatty(handle: FileHandle) bool { +pub fn isatty(handle: fd_t) bool { if (builtin.link_libc) { - return c.isatty(handle) != 0; + return system.isatty(handle) != 0; } if (windows.is_the_target) { if (isCygwinPty(handle)) @@ -1421,7 +1388,7 @@ pub fn isatty(handle: FileHandle) bool { return system.syscall3(system.SYS_ioctl, @bitCast(usize, isize(handle)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; } -pub fn isCygwinPty(handle: FileHandle) bool { +pub fn isCygwinPty(handle: fd_t) bool { if (!windows.is_the_target) return false; const size = @sizeOf(windows.FILE_NAME_INFO); @@ -1472,7 +1439,7 @@ pub const SocketError = error{ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!i32 { const rc = system.socket(domain, socket_type, protocol); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EACCES => return error.PermissionDenied, EAFNOSUPPORT => return error.AddressFamilyNotSupported, @@ -1525,7 +1492,7 @@ pub const BindError = error{ /// addr is `*const T` where T is one of the sockaddr pub fn bind(fd: i32, addr: *const sockaddr) BindError!void { const rc = system.bind(fd, system, @sizeOf(sockaddr)); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return, EACCES => return error.AccessDenied, EADDRINUSE => return error.AddressInUse, @@ -1563,7 +1530,7 @@ const ListenError = error{ pub fn listen(sockfd: i32, backlog: u32) ListenError!void { const rc = system.listen(sockfd, backlog); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return, EADDRINUSE => return error.AddressInUse, EBADF => unreachable, @@ -1606,7 +1573,7 @@ pub fn accept4(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 { while (true) { var sockaddr_size = u32(@sizeOf(sockaddr)); const rc = system.accept4(fd, addr, &sockaddr_size, flags); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EINTR => continue, else => |err| return unexpectedErrno(err), @@ -1634,7 +1601,7 @@ pub fn accept4_async(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 { while (true) { var sockaddr_size = u32(@sizeOf(sockaddr)); const rc = system.accept4(fd, addr, &sockaddr_size, flags); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EINTR => continue, else => |err| return unexpectedErrno(err), @@ -1674,7 +1641,7 @@ pub const EpollCreateError = error{ pub fn epoll_create1(flags: u32) EpollCreateError!i32 { const rc = system.epoll_create1(flags); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), @@ -1715,7 +1682,7 @@ pub const EpollCtlError = error{ pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: *epoll_event) EpollCtlError!void { const rc = system.epoll_ctl(epfd, op, fd, event); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return, else => |err| return unexpectedErrno(err), @@ -1737,7 +1704,7 @@ pub fn epoll_wait(epfd: i32, events: []epoll_event, timeout: i32) usize { while (true) { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return rc, EINTR => continue, EBADF => unreachable, @@ -1757,7 +1724,7 @@ pub const EventFdError = error{ pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 { const rc = system.eventfd(initval, flags); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), @@ -1779,7 +1746,7 @@ pub const GetSockNameError = error{ pub fn getsockname(sockfd: i32) GetSockNameError!sockaddr { var addr: sockaddr = undefined; var addrlen: socklen_t = @sizeOf(sockaddr); - switch (system.getErrno(system.getsockname(sockfd, &addr, &addrlen))) { + switch (errno(system.getsockname(sockfd, &addr, &addrlen))) { 0 => return addr, else => |err| return unexpectedErrno(err), @@ -1832,7 +1799,7 @@ pub const ConnectError = error{ /// For non-blocking, see `connect_async`. pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { while (true) { - switch (system.getErrno(system.connect(sockfd, sockaddr, @sizeOf(sockaddr)))) { + switch (errno(system.connect(sockfd, sockaddr, @sizeOf(sockaddr)))) { 0 => return, else => |err| return unexpectedErrno(err), @@ -1861,10 +1828,10 @@ pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { /// It expects to receive EINPROGRESS`. pub fn connect_async(sockfd: i32, sockaddr: *const c_void, len: u32) ConnectError!void { while (true) { - switch (system.getErrno(system.connect(sockfd, sockaddr, len))) { + switch (errno(system.connect(sockfd, sockaddr, len))) { 0, EINPROGRESS => return, EINTR => continue, - else => return unexpectedErrno(err), + else => |err| return unexpectedErrno(err), EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, @@ -1890,7 +1857,7 @@ pub fn getsockoptError(sockfd: i32) ConnectError!void { var size: u32 = @sizeOf(i32); const rc = system.getsockopt(sockfd, SOL_SOCKET, SO_ERROR, @ptrCast([*]u8, &err_code), &size); assert(size == 4); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => switch (err_code) { 0 => return, EACCES => return error.PermissionDenied, @@ -1919,10 +1886,10 @@ pub fn getsockoptError(sockfd: i32) ConnectError!void { } } -pub fn wait(pid: i32) i32 { +pub fn waitpid(pid: i32) i32 { var status: i32 = undefined; while (true) { - switch (system.getErrno(system.waitpid(pid, &status, 0))) { + switch (errno(system.waitpid(pid, &status, 0))) { 0 => return status, EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. @@ -1932,16 +1899,28 @@ pub fn wait(pid: i32) i32 { } } -pub fn fstat(fd: FileHandle) !Stat { +pub const FStatError = error{ + SystemResources, + Unexpected, +}; + +pub fn fstat(fd: fd_t) FStatError!Stat { var stat: Stat = undefined; - switch (system.getErrno(system.fstat(fd, &stat))) { + if (os.darwin.is_the_target) { + switch (errno(system.@"fstat$INODE64"(fd, buf))) { + 0 => return stat, + EBADF => unreachable, // Always a race condition. + ENOMEM => return error.SystemResources, + else => |err| return unexpectedErrno(err), + } + } + + switch (errno(system.fstat(fd, &stat))) { 0 => return stat, EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } - - return stat; } pub const KQueueError = error{ @@ -1956,7 +1935,7 @@ pub const KQueueError = error{ pub fn kqueue() KQueueError!i32 { const rc = system.kqueue(); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, @@ -1986,7 +1965,7 @@ pub fn kevent( ) KEventError!usize { while (true) { const rc = system.kevent(kq, changelist, eventlist, timeout); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return rc, EACCES => return error.AccessDenied, EFAULT => unreachable, @@ -2011,7 +1990,7 @@ pub const INotifyInitError = error{ /// initialize an inotify instance pub fn inotify_init1(flags: u32) INotifyInitError!i32 { const rc = system.inotify_init1(flags); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EINVAL => unreachable, EMFILE => return error.ProcessFdQuotaExceeded, @@ -2039,7 +2018,7 @@ pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INoti /// Same as `inotify_add_watch` except pathname is null-terminated. pub fn inotify_add_watchC(inotify_fd: i32, pathname: [*]const u8, mask: u32) INotifyAddWatchError!i32 { const rc = system.inotify_add_watch(inotify_fd, pathname, mask); - switch (system.getErrno(rc)) { + switch (errno(rc)) { 0 => return @intCast(i32, rc), EACCES => return error.AccessDenied, EBADF => unreachable, @@ -2055,7 +2034,7 @@ pub fn inotify_add_watchC(inotify_fd: i32, pathname: [*]const u8, mask: u32) INo /// remove an existing watch from an inotify instance pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void { - switch (system.getErrno(system.inotify_rm_watch(inotify_fd, wd))) { + switch (errno(system.inotify_rm_watch(inotify_fd, wd))) { 0 => return, EBADF => unreachable, EINVAL => unreachable, @@ -2071,12 +2050,12 @@ pub const MProtectError = error{ /// address and length must be page-aligned pub fn mprotect(address: usize, length: usize, protection: u32) MProtectError!void { - const negative_page_size = @bitCast(usize, -isize(page_size)); + const negative_page_size = @bitCast(usize, -isize(os.page_size)); const aligned_address = address & negative_page_size; - const aligned_end = (address + length + page_size - 1) & negative_page_size; + const aligned_end = (address + length + os.page_size - 1) & negative_page_size; assert(address == aligned_address); assert(length == aligned_end - aligned_address); - switch (system.getErrno(system.mprotect(address, length, protection))) { + switch (errno(system.mprotect(address, length, protection))) { 0 => return, EINVAL => unreachable, EACCES => return error.AccessDenied, @@ -2085,6 +2064,238 @@ pub fn mprotect(address: usize, length: usize, protection: u32) MProtectError!vo } } +pub const ForkError = error{ + SystemResources, + Unexpected, +}; + +pub fn fork() ForkError!pid_t { + if (builtin.link_libc) { + return system.fork(); + } + if (linux.is_the_target) { + const rc = if (@hasDecl(system, "SYS_fork")) system.fork() else system.clone2(SIGCHLD, 0); + switch (errno(rc)) { + 0 => return rc, + EAGAIN => return error.SystemResources, + ENOMEM => return error.SystemResources, + else => |err| return unexpectedErrno(err), + } + } +} + +pub const MMapError = error{ + AccessDenied, + PermissionDenied, + LockedMemoryLimitExceeded, + SystemFdQuotaExceeded, + MemoryMappingNotSupported, + OutOfMemory, +}; + +/// Map files or devices into memory. +/// Use of a mapped region can result in these signals: +/// * SIGSEGV - Attempted write into a region mapped as read-only. +/// * SIGBUS - Attempted access to a portion of the buffer that does not correspond to the file +pub fn mmap(address: ?[*]u8, length: usize, prot: u32, flags: u32, fd: fd_t, offset: isize) MMapError!usize { + const err = if (builtin.link_libc) blk: { + const rc = system.mmap(address, length, prot, flags, fd, offset); + if (rc != system.MMAP_FAILED) return rc; + break :blk system._errno().*; + } else blk: { + const rc = system.mmap(address, length, prot, flags, fd, offset); + const err = errno(rc); + if (err == 0) return rc; + break :blk err; + }; + switch (err) { + ETXTBSY => return error.AccessDenied, + EACCES => return error.AccessDenied, + EPERM => return error.PermissionDenied, + EAGAIN => return error.LockedMemoryLimitExceeded, + EBADF => unreachable, // Always a race condition. + EOVERFLOW => unreachable, // The number of pages used for length + offset would overflow. + ENFILE => return error.SystemFdQuotaExceeded, + ENODEV => return error.MemoryMappingNotSupported, + EINVAL => unreachable, // Invalid parameters to mmap() + ENOMEM => return error.OutOfMemory, + else => return unexpectedErrno(err), + } +} + +/// Deletes the mappings for the specified address range, causing +/// further references to addresses within the range to generate invalid memory references. +/// Note that while POSIX allows unmapping a region in the middle of an existing mapping, +/// Zig's munmap function does not, for two reasons: +/// * It violates the Zig principle that resource deallocation must succeed. +/// * The Windows function, VirtualFree, has this restriction. +pub fn munmap(address: usize, length: usize) void { + switch (errno(system.munmap(address, length))) { + 0 => return, + EINVAL => unreachable, // Invalid parameters. + ENOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. + else => unreachable, + } +} + +pub const AccessError = error{ + PermissionDenied, + FileNotFound, + NameTooLong, + InputOutput, + SystemResources, + BadPathName, + + /// On Windows, file paths must be valid Unicode. + InvalidUtf8, + + Unexpected, +}; + +/// check user's permissions for a file +pub fn access(path: []const u8, mode: u32) AccessError!void { + if (windows.is_the_target and !builtin.link_libc) { + const path_w = try sliceToPrefixedFileW(path); + return accessW(&path_w, mode); + } + const path_c = try toPosixPath(path); + return accessC(&path_c, mode); +} + +/// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. +/// Otherwise use `access` or `accessC`. +/// TODO currently this ignores `mode`. +pub fn accessW(path: [*]const u16, mode: u32) AccessError!void { + if (windows.GetFileAttributesW(path) != windows.INVALID_FILE_ATTRIBUTES) { + return; + } + switch (windows.GetLastError()) { + windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, + windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, + windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, + else => |err| return unexpectedErrorWindows(err), + } +} + +/// Call if you have a UTF-8 encoded, null-terminated string. +/// Otherwise use `access` or `accessW`. +pub fn accessC(path: [*]const u8, mode: u32) AccessError!void { + if (windows.is_the_target) { + const path_w = try cStrToPrefixedFileW(path); + return accessW(&path_w, mode); + } + switch (errno(system.access(path, mode))) { + 0 => return, + EACCES => return error.PermissionDenied, + EROFS => return error.PermissionDenied, + ELOOP => return error.PermissionDenied, + ETXTBSY => return error.PermissionDenied, + ENOTDIR => return error.FileNotFound, + ENOENT => return error.FileNotFound, + + ENAMETOOLONG => return error.NameTooLong, + EINVAL => unreachable, + EFAULT => unreachable, + EIO => return error.InputOutput, + ENOMEM => return error.SystemResources, + else => |err| return unexpectedErrno(err), + } +} + +pub const PipeError = error{ + SystemFdQuotaExceeded, + ProcessFdQuotaExceeded, +}; + +/// Creates a unidirectional data channel that can be used for interprocess communication. +pub fn pipe(fds: *[2]fd_t) PipeError!void { + const rc = if (builtin.link_libc or @hasDecl(system, SYS_pipe)) + system.pipe(fds) + else + system.pipe2(fds, 0); + switch (errno(rc)) { + 0 => return, + EINVAL => unreachable, // Invalid parameters to pipe() + EFAULT => unreachable, // Invalid fds pointer + ENFILE => return error.SystemFdQuotaExceeded, + EMFILE => return error.ProcessFdQuotaExceeded, + else => |err| return unexpectedErrno(err), + } +} + +pub fn pipe2(fds: *[2]fd_t, flags: u32) PipeError!void { + switch (errno(system.pipe2(fds, flags))) { + 0 => return, + EINVAL => unreachable, // Invalid flags + EFAULT => unreachable, // Invalid fds pointer + ENFILE => return error.SystemFdQuotaExceeded, + EMFILE => return error.ProcessFdQuotaExceeded, + else => |err| return unexpectedErrno(err), + } +} + +pub const SysCtlError = error{ + PermissionDenied, + SystemResources, + Unexpected, +}; + +pub fn sysctl( + name: []const c_int, + oldp: ?*c_void, + oldlenp: ?*usize, + newp: ?*c_void, + newlen: usize, +) SysCtlError!void { + switch (errno(system.sysctl(name.ptr, name.len, oldp, oldlenp, newp, newlen))) { + 0 => return, + EFAULT => unreachable, + EPERM => return error.PermissionDenied, + ENOMEM => return error.SystemResources, + else => |err| return unexpectedErrno(err), + } +} + +pub fn sysctlbynameC( + name: [*]const u8, + oldp: ?*c_void, + oldlenp: ?*usize, + newp: ?*c_void, + newlen: usize, +) SysCtlError!void { + switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) { + 0 => return, + EFAULT => unreachable, + EPERM => return error.PermissionDenied, + ENOMEM => return error.SystemResources, + else => |err| return unexpectedErrno(err), + } +} + +pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { + switch (errno(system.gettimeofday(tv, tz))) { + 0 => return, + EINVAL => unreachable, + else => unreachable, + } +} + +pub fn nanosleep(req: timespec) void { + var rem = req; + while (true) { + switch (errno(system.nanosleep(&rem, &rem))) { + 0 => return, + EINVAL => unreachable, // Invalid parameters. + EFAULT => unreachable, + EINTR => continue, + } + } +} + +pub const realpath = std.os.path.real; +pub const realpathC = std.os.path.realC; +pub const realpathW = std.os.path.realW; + /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { diff --git a/std/os/wasi.zig b/std/os/wasi.zig @@ -4,10 +4,8 @@ const std = @import("std"); const assert = std.debug.assert; pub const is_the_target = @import("builtin").os == .wasi; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; +pub const posix = @import("wasi/posix.zig"); +pub use posix; comptime { assert(@alignOf(i8) == 1); @@ -20,320 +18,6 @@ comptime { assert(@alignOf(u64) == 8); } -pub const advice_t = u8; -pub const ADVICE_NORMAL: advice_t = 0; -pub const ADVICE_SEQUENTIAL: advice_t = 1; -pub const ADVICE_RANDOM: advice_t = 2; -pub const ADVICE_WILLNEED: advice_t = 3; -pub const ADVICE_DONTNEED: advice_t = 4; -pub const ADVICE_NOREUSE: advice_t = 5; - -pub const ciovec_t = extern struct { - buf: [*]const u8, - buf_len: usize, -}; - -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 device_t = u64; - -pub const dircookie_t = u64; -pub const DIRCOOKIE_START: dircookie_t = 0; - -pub const dirent_t = extern struct { - d_next: dircookie_t, - d_ino: inode_t, - d_namlen: u32, - d_type: filetype_t, -}; - -pub const errno_t = u16; -pub const ESUCCESS: errno_t = 0; -pub const E2BIG: errno_t = 1; -pub const EACCES: errno_t = 2; -pub const EADDRINUSE: errno_t = 3; -pub const EADDRNOTAVAIL: errno_t = 4; -pub const EAFNOSUPPORT: errno_t = 5; -pub const EAGAIN: errno_t = 6; -pub const EALREADY: errno_t = 7; -pub const EBADF: errno_t = 8; -pub const EBADMSG: errno_t = 9; -pub const EBUSY: errno_t = 10; -pub const ECANCELED: errno_t = 11; -pub const ECHILD: errno_t = 12; -pub const ECONNABORTED: errno_t = 13; -pub const ECONNREFUSED: errno_t = 14; -pub const ECONNRESET: errno_t = 15; -pub const EDEADLK: errno_t = 16; -pub const EDESTADDRREQ: errno_t = 17; -pub const EDOM: errno_t = 18; -pub const EDQUOT: errno_t = 19; -pub const EEXIST: errno_t = 20; -pub const EFAULT: errno_t = 21; -pub const EFBIG: errno_t = 22; -pub const EHOSTUNREACH: errno_t = 23; -pub const EIDRM: errno_t = 24; -pub const EILSEQ: errno_t = 25; -pub const EINPROGRESS: errno_t = 26; -pub const EINTR: errno_t = 27; -pub const EINVAL: errno_t = 28; -pub const EIO: errno_t = 29; -pub const EISCONN: errno_t = 30; -pub const EISDIR: errno_t = 31; -pub const ELOOP: errno_t = 32; -pub const EMFILE: errno_t = 33; -pub const EMLINK: errno_t = 34; -pub const EMSGSIZE: errno_t = 35; -pub const EMULTIHOP: errno_t = 36; -pub const ENAMETOOLONG: errno_t = 37; -pub const ENETDOWN: errno_t = 38; -pub const ENETRESET: errno_t = 39; -pub const ENETUNREACH: errno_t = 40; -pub const ENFILE: errno_t = 41; -pub const ENOBUFS: errno_t = 42; -pub const ENODEV: errno_t = 43; -pub const ENOENT: errno_t = 44; -pub const ENOEXEC: errno_t = 45; -pub const ENOLCK: errno_t = 46; -pub const ENOLINK: errno_t = 47; -pub const ENOMEM: errno_t = 48; -pub const ENOMSG: errno_t = 49; -pub const ENOPROTOOPT: errno_t = 50; -pub const ENOSPC: errno_t = 51; -pub const ENOSYS: errno_t = 52; -pub const ENOTCONN: errno_t = 53; -pub const ENOTDIR: errno_t = 54; -pub const ENOTEMPTY: errno_t = 55; -pub const ENOTRECOVERABLE: errno_t = 56; -pub const ENOTSOCK: errno_t = 57; -pub const ENOTSUP: errno_t = 58; -pub const ENOTTY: errno_t = 59; -pub const ENXIO: errno_t = 60; -pub const EOVERFLOW: errno_t = 61; -pub const EOWNERDEAD: errno_t = 62; -pub const EPERM: errno_t = 63; -pub const EPIPE: errno_t = 64; -pub const EPROTO: errno_t = 65; -pub const EPROTONOSUPPORT: errno_t = 66; -pub const EPROTOTYPE: errno_t = 67; -pub const ERANGE: errno_t = 68; -pub const EROFS: errno_t = 69; -pub const ESPIPE: errno_t = 70; -pub const ESRCH: errno_t = 71; -pub const ESTALE: errno_t = 72; -pub const ETIMEDOUT: errno_t = 73; -pub const ETXTBSY: errno_t = 74; -pub const EXDEV: errno_t = 75; -pub const ENOTCAPABLE: errno_t = 76; - -pub const event_t = extern struct { - userdata: userdata_t, - @"error": errno_t, - @"type": eventtype_t, - u: extern union { - fd_readwrite: extern struct { - nbytes: filesize_t, - flags: eventrwflags_t, - }, - }, -}; - -pub const eventrwflags_t = u16; -pub const EVENT_FD_READWRITE_HANGUP: eventrwflags_t = 0x0001; - -pub const eventtype_t = u8; -pub const EVENTTYPE_CLOCK: eventtype_t = 0; -pub const EVENTTYPE_FD_READ: eventtype_t = 1; -pub const EVENTTYPE_FD_WRITE: eventtype_t = 2; - -pub const exitcode_t = u32; - -pub const fd_t = u32; - -pub const fdflags_t = u16; -pub const FDFLAG_APPEND: fdflags_t = 0x0001; -pub const FDFLAG_DSYNC: fdflags_t = 0x0002; -pub const FDFLAG_NONBLOCK: fdflags_t = 0x0004; -pub const FDFLAG_RSYNC: fdflags_t = 0x0008; -pub const FDFLAG_SYNC: fdflags_t = 0x0010; - -const fdstat_t = extern struct { - fs_filetype: filetype_t, - fs_flags: fdflags_t, - fs_rights_base: rights_t, - fs_rights_inheriting: rights_t, -}; - -pub const filedelta_t = i64; - -pub const filesize_t = u64; - -pub const filestat_t = extern struct { - st_dev: device_t, - st_ino: inode_t, - st_filetype: filetype_t, - st_nlink: linkcount_t, - st_size: filesize_t, - st_atim: timestamp_t, - st_mtim: timestamp_t, - st_ctim: timestamp_t, -}; - -pub const filetype_t = u8; -pub const FILETYPE_UNKNOWN: filetype_t = 0; -pub const FILETYPE_BLOCK_DEVICE: filetype_t = 1; -pub const FILETYPE_CHARACTER_DEVICE: filetype_t = 2; -pub const FILETYPE_DIRECTORY: filetype_t = 3; -pub const FILETYPE_REGULAR_FILE: filetype_t = 4; -pub const FILETYPE_SOCKET_DGRAM: filetype_t = 5; -pub const FILETYPE_SOCKET_STREAM: filetype_t = 6; -pub const FILETYPE_SYMBOLIC_LINK: filetype_t = 7; - -pub const fstflags_t = u16; -pub const FILESTAT_SET_ATIM: fstflags_t = 0x0001; -pub const FILESTAT_SET_ATIM_NOW: fstflags_t = 0x0002; -pub const FILESTAT_SET_MTIM: fstflags_t = 0x0004; -pub const FILESTAT_SET_MTIM_NOW: fstflags_t = 0x0008; - -pub const inode_t = u64; - -pub const iovec_t = extern struct { - buf: [*]u8, - buf_len: usize, -}; - -pub const linkcount_t = u32; - -pub const lookupflags_t = u32; -pub const LOOKUP_SYMLINK_FOLLOW: lookupflags_t = 0x00000001; - -pub const oflags_t = u16; -pub const O_CREAT: oflags_t = 0x0001; -pub const O_DIRECTORY: oflags_t = 0x0002; -pub const O_EXCL: oflags_t = 0x0004; -pub const O_TRUNC: oflags_t = 0x0008; - -pub const preopentype_t = u8; -pub const PREOPENTYPE_DIR: preopentype_t = 0; - -pub const prestat_t = extern struct { - pr_type: preopentype_t, - u: extern union { - dir: extern struct { - pr_name_len: usize, - }, - }, -}; - -pub const riflags_t = u16; -pub const SOCK_RECV_PEEK: riflags_t = 0x0001; -pub const SOCK_RECV_WAITALL: riflags_t = 0x0002; - -pub const rights_t = u64; -pub const RIGHT_FD_DATASYNC: rights_t = 0x0000000000000001; -pub const RIGHT_FD_READ: rights_t = 0x0000000000000002; -pub const RIGHT_FD_SEEK: rights_t = 0x0000000000000004; -pub const RIGHT_FD_FDSTAT_SET_FLAGS: rights_t = 0x0000000000000008; -pub const RIGHT_FD_SYNC: rights_t = 0x0000000000000010; -pub const RIGHT_FD_TELL: rights_t = 0x0000000000000020; -pub const RIGHT_FD_WRITE: rights_t = 0x0000000000000040; -pub const RIGHT_FD_ADVISE: rights_t = 0x0000000000000080; -pub const RIGHT_FD_ALLOCATE: rights_t = 0x0000000000000100; -pub const RIGHT_PATH_CREATE_DIRECTORY: rights_t = 0x0000000000000200; -pub const RIGHT_PATH_CREATE_FILE: rights_t = 0x0000000000000400; -pub const RIGHT_PATH_LINK_SOURCE: rights_t = 0x0000000000000800; -pub const RIGHT_PATH_LINK_TARGET: rights_t = 0x0000000000001000; -pub const RIGHT_PATH_OPEN: rights_t = 0x0000000000002000; -pub const RIGHT_FD_READDIR: rights_t = 0x0000000000004000; -pub const RIGHT_PATH_READLINK: rights_t = 0x0000000000008000; -pub const RIGHT_PATH_RENAME_SOURCE: rights_t = 0x0000000000010000; -pub const RIGHT_PATH_RENAME_TARGET: rights_t = 0x0000000000020000; -pub const RIGHT_PATH_FILESTAT_GET: rights_t = 0x0000000000040000; -pub const RIGHT_PATH_FILESTAT_SET_SIZE: rights_t = 0x0000000000080000; -pub const RIGHT_PATH_FILESTAT_SET_TIMES: rights_t = 0x0000000000100000; -pub const RIGHT_FD_FILESTAT_GET: rights_t = 0x0000000000200000; -pub const RIGHT_FD_FILESTAT_SET_SIZE: rights_t = 0x0000000000400000; -pub const RIGHT_FD_FILESTAT_SET_TIMES: rights_t = 0x0000000000800000; -pub const RIGHT_PATH_SYMLINK: rights_t = 0x0000000001000000; -pub const RIGHT_PATH_REMOVE_DIRECTORY: rights_t = 0x0000000002000000; -pub const RIGHT_PATH_UNLINK_FILE: rights_t = 0x0000000004000000; -pub const RIGHT_POLL_FD_READWRITE: rights_t = 0x0000000008000000; -pub const RIGHT_SOCK_SHUTDOWN: rights_t = 0x0000000010000000; - -pub const roflags_t = u16; -pub const SOCK_RECV_DATA_TRUNCATED: roflags_t = 0x0001; - -pub const sdflags_t = u8; -pub const SHUT_RD: sdflags_t = 0x01; -pub const SHUT_WR: sdflags_t = 0x02; - -pub const siflags_t = u16; - -pub const signal_t = u8; -pub const SIGHUP: signal_t = 1; -pub const SIGINT: signal_t = 2; -pub const SIGQUIT: signal_t = 3; -pub const SIGILL: signal_t = 4; -pub const SIGTRAP: signal_t = 5; -pub const SIGABRT: signal_t = 6; -pub const SIGBUS: signal_t = 7; -pub const SIGFPE: signal_t = 8; -pub const SIGKILL: signal_t = 9; -pub const SIGUSR1: signal_t = 10; -pub const SIGSEGV: signal_t = 11; -pub const SIGUSR2: signal_t = 12; -pub const SIGPIPE: signal_t = 13; -pub const SIGALRM: signal_t = 14; -pub const SIGTERM: signal_t = 15; -pub const SIGCHLD: signal_t = 16; -pub const SIGCONT: signal_t = 17; -pub const SIGSTOP: signal_t = 18; -pub const SIGTSTP: signal_t = 19; -pub const SIGTTIN: signal_t = 20; -pub const SIGTTOU: signal_t = 21; -pub const SIGURG: signal_t = 22; -pub const SIGXCPU: signal_t = 23; -pub const SIGXFSZ: signal_t = 24; -pub const SIGVTALRM: signal_t = 25; -pub const SIGPROF: signal_t = 26; -pub const SIGWINCH: signal_t = 27; -pub const SIGPOLL: signal_t = 28; -pub const SIGPWR: signal_t = 29; -pub const SIGSYS: signal_t = 30; - -pub const subclockflags_t = u16; -pub const SUBSCRIPTION_CLOCK_ABSTIME: subclockflags_t = 0x0001; - -pub const subscription_t = extern struct { - userdata: userdata_t, - @"type": eventtype_t, - u: extern union { - clock: extern struct { - identifier: userdata_t, - clock_id: clockid_t, - timeout: timestamp_t, - precision: timestamp_t, - flags: subclockflags_t, - }, - fd_readwrite: extern struct { - fd: fd_t, - }, - }, -}; - -pub const timestamp_t = u64; - -pub const userdata_t = u64; - -pub const whence_t = u8; -pub const WHENCE_CUR: whence_t = 0; -pub const WHENCE_END: whence_t = 1; -pub const WHENCE_SET: whence_t = 2; - pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t; pub extern "wasi_unstable" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t; diff --git a/std/os/wasi/posix.zig b/std/os/wasi/posix.zig @@ -0,0 +1,320 @@ +// Declarations that are intended to be imported into the POSIX namespace. +// This includes WASI-only APIs. + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const advice_t = u8; +pub const ADVICE_NORMAL: advice_t = 0; +pub const ADVICE_SEQUENTIAL: advice_t = 1; +pub const ADVICE_RANDOM: advice_t = 2; +pub const ADVICE_WILLNEED: advice_t = 3; +pub const ADVICE_DONTNEED: advice_t = 4; +pub const ADVICE_NOREUSE: advice_t = 5; + +pub const ciovec_t = extern struct { + buf: [*]const u8, + buf_len: usize, +}; + +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 device_t = u64; + +pub const dircookie_t = u64; +pub const DIRCOOKIE_START: dircookie_t = 0; + +pub const dirent_t = extern struct { + d_next: dircookie_t, + d_ino: inode_t, + d_namlen: u32, + d_type: filetype_t, +}; + +pub const errno_t = u16; +pub const ESUCCESS: errno_t = 0; +pub const E2BIG: errno_t = 1; +pub const EACCES: errno_t = 2; +pub const EADDRINUSE: errno_t = 3; +pub const EADDRNOTAVAIL: errno_t = 4; +pub const EAFNOSUPPORT: errno_t = 5; +pub const EAGAIN: errno_t = 6; +pub const EALREADY: errno_t = 7; +pub const EBADF: errno_t = 8; +pub const EBADMSG: errno_t = 9; +pub const EBUSY: errno_t = 10; +pub const ECANCELED: errno_t = 11; +pub const ECHILD: errno_t = 12; +pub const ECONNABORTED: errno_t = 13; +pub const ECONNREFUSED: errno_t = 14; +pub const ECONNRESET: errno_t = 15; +pub const EDEADLK: errno_t = 16; +pub const EDESTADDRREQ: errno_t = 17; +pub const EDOM: errno_t = 18; +pub const EDQUOT: errno_t = 19; +pub const EEXIST: errno_t = 20; +pub const EFAULT: errno_t = 21; +pub const EFBIG: errno_t = 22; +pub const EHOSTUNREACH: errno_t = 23; +pub const EIDRM: errno_t = 24; +pub const EILSEQ: errno_t = 25; +pub const EINPROGRESS: errno_t = 26; +pub const EINTR: errno_t = 27; +pub const EINVAL: errno_t = 28; +pub const EIO: errno_t = 29; +pub const EISCONN: errno_t = 30; +pub const EISDIR: errno_t = 31; +pub const ELOOP: errno_t = 32; +pub const EMFILE: errno_t = 33; +pub const EMLINK: errno_t = 34; +pub const EMSGSIZE: errno_t = 35; +pub const EMULTIHOP: errno_t = 36; +pub const ENAMETOOLONG: errno_t = 37; +pub const ENETDOWN: errno_t = 38; +pub const ENETRESET: errno_t = 39; +pub const ENETUNREACH: errno_t = 40; +pub const ENFILE: errno_t = 41; +pub const ENOBUFS: errno_t = 42; +pub const ENODEV: errno_t = 43; +pub const ENOENT: errno_t = 44; +pub const ENOEXEC: errno_t = 45; +pub const ENOLCK: errno_t = 46; +pub const ENOLINK: errno_t = 47; +pub const ENOMEM: errno_t = 48; +pub const ENOMSG: errno_t = 49; +pub const ENOPROTOOPT: errno_t = 50; +pub const ENOSPC: errno_t = 51; +pub const ENOSYS: errno_t = 52; +pub const ENOTCONN: errno_t = 53; +pub const ENOTDIR: errno_t = 54; +pub const ENOTEMPTY: errno_t = 55; +pub const ENOTRECOVERABLE: errno_t = 56; +pub const ENOTSOCK: errno_t = 57; +pub const ENOTSUP: errno_t = 58; +pub const ENOTTY: errno_t = 59; +pub const ENXIO: errno_t = 60; +pub const EOVERFLOW: errno_t = 61; +pub const EOWNERDEAD: errno_t = 62; +pub const EPERM: errno_t = 63; +pub const EPIPE: errno_t = 64; +pub const EPROTO: errno_t = 65; +pub const EPROTONOSUPPORT: errno_t = 66; +pub const EPROTOTYPE: errno_t = 67; +pub const ERANGE: errno_t = 68; +pub const EROFS: errno_t = 69; +pub const ESPIPE: errno_t = 70; +pub const ESRCH: errno_t = 71; +pub const ESTALE: errno_t = 72; +pub const ETIMEDOUT: errno_t = 73; +pub const ETXTBSY: errno_t = 74; +pub const EXDEV: errno_t = 75; +pub const ENOTCAPABLE: errno_t = 76; + +pub const event_t = extern struct { + userdata: userdata_t, + @"error": errno_t, + @"type": eventtype_t, + u: extern union { + fd_readwrite: extern struct { + nbytes: filesize_t, + flags: eventrwflags_t, + }, + }, +}; + +pub const eventrwflags_t = u16; +pub const EVENT_FD_READWRITE_HANGUP: eventrwflags_t = 0x0001; + +pub const eventtype_t = u8; +pub const EVENTTYPE_CLOCK: eventtype_t = 0; +pub const EVENTTYPE_FD_READ: eventtype_t = 1; +pub const EVENTTYPE_FD_WRITE: eventtype_t = 2; + +pub const exitcode_t = u32; + +pub const fd_t = u32; + +pub const fdflags_t = u16; +pub const FDFLAG_APPEND: fdflags_t = 0x0001; +pub const FDFLAG_DSYNC: fdflags_t = 0x0002; +pub const FDFLAG_NONBLOCK: fdflags_t = 0x0004; +pub const FDFLAG_RSYNC: fdflags_t = 0x0008; +pub const FDFLAG_SYNC: fdflags_t = 0x0010; + +const fdstat_t = extern struct { + fs_filetype: filetype_t, + fs_flags: fdflags_t, + fs_rights_base: rights_t, + fs_rights_inheriting: rights_t, +}; + +pub const filedelta_t = i64; + +pub const filesize_t = u64; + +pub const filestat_t = extern struct { + st_dev: device_t, + st_ino: inode_t, + st_filetype: filetype_t, + st_nlink: linkcount_t, + st_size: filesize_t, + st_atim: timestamp_t, + st_mtim: timestamp_t, + st_ctim: timestamp_t, +}; + +pub const filetype_t = u8; +pub const FILETYPE_UNKNOWN: filetype_t = 0; +pub const FILETYPE_BLOCK_DEVICE: filetype_t = 1; +pub const FILETYPE_CHARACTER_DEVICE: filetype_t = 2; +pub const FILETYPE_DIRECTORY: filetype_t = 3; +pub const FILETYPE_REGULAR_FILE: filetype_t = 4; +pub const FILETYPE_SOCKET_DGRAM: filetype_t = 5; +pub const FILETYPE_SOCKET_STREAM: filetype_t = 6; +pub const FILETYPE_SYMBOLIC_LINK: filetype_t = 7; + +pub const fstflags_t = u16; +pub const FILESTAT_SET_ATIM: fstflags_t = 0x0001; +pub const FILESTAT_SET_ATIM_NOW: fstflags_t = 0x0002; +pub const FILESTAT_SET_MTIM: fstflags_t = 0x0004; +pub const FILESTAT_SET_MTIM_NOW: fstflags_t = 0x0008; + +pub const inode_t = u64; + +pub const iovec_t = extern struct { + buf: [*]u8, + buf_len: usize, +}; + +pub const linkcount_t = u32; + +pub const lookupflags_t = u32; +pub const LOOKUP_SYMLINK_FOLLOW: lookupflags_t = 0x00000001; + +pub const oflags_t = u16; +pub const O_CREAT: oflags_t = 0x0001; +pub const O_DIRECTORY: oflags_t = 0x0002; +pub const O_EXCL: oflags_t = 0x0004; +pub const O_TRUNC: oflags_t = 0x0008; + +pub const preopentype_t = u8; +pub const PREOPENTYPE_DIR: preopentype_t = 0; + +pub const prestat_t = extern struct { + pr_type: preopentype_t, + u: extern union { + dir: extern struct { + pr_name_len: usize, + }, + }, +}; + +pub const riflags_t = u16; +pub const SOCK_RECV_PEEK: riflags_t = 0x0001; +pub const SOCK_RECV_WAITALL: riflags_t = 0x0002; + +pub const rights_t = u64; +pub const RIGHT_FD_DATASYNC: rights_t = 0x0000000000000001; +pub const RIGHT_FD_READ: rights_t = 0x0000000000000002; +pub const RIGHT_FD_SEEK: rights_t = 0x0000000000000004; +pub const RIGHT_FD_FDSTAT_SET_FLAGS: rights_t = 0x0000000000000008; +pub const RIGHT_FD_SYNC: rights_t = 0x0000000000000010; +pub const RIGHT_FD_TELL: rights_t = 0x0000000000000020; +pub const RIGHT_FD_WRITE: rights_t = 0x0000000000000040; +pub const RIGHT_FD_ADVISE: rights_t = 0x0000000000000080; +pub const RIGHT_FD_ALLOCATE: rights_t = 0x0000000000000100; +pub const RIGHT_PATH_CREATE_DIRECTORY: rights_t = 0x0000000000000200; +pub const RIGHT_PATH_CREATE_FILE: rights_t = 0x0000000000000400; +pub const RIGHT_PATH_LINK_SOURCE: rights_t = 0x0000000000000800; +pub const RIGHT_PATH_LINK_TARGET: rights_t = 0x0000000000001000; +pub const RIGHT_PATH_OPEN: rights_t = 0x0000000000002000; +pub const RIGHT_FD_READDIR: rights_t = 0x0000000000004000; +pub const RIGHT_PATH_READLINK: rights_t = 0x0000000000008000; +pub const RIGHT_PATH_RENAME_SOURCE: rights_t = 0x0000000000010000; +pub const RIGHT_PATH_RENAME_TARGET: rights_t = 0x0000000000020000; +pub const RIGHT_PATH_FILESTAT_GET: rights_t = 0x0000000000040000; +pub const RIGHT_PATH_FILESTAT_SET_SIZE: rights_t = 0x0000000000080000; +pub const RIGHT_PATH_FILESTAT_SET_TIMES: rights_t = 0x0000000000100000; +pub const RIGHT_FD_FILESTAT_GET: rights_t = 0x0000000000200000; +pub const RIGHT_FD_FILESTAT_SET_SIZE: rights_t = 0x0000000000400000; +pub const RIGHT_FD_FILESTAT_SET_TIMES: rights_t = 0x0000000000800000; +pub const RIGHT_PATH_SYMLINK: rights_t = 0x0000000001000000; +pub const RIGHT_PATH_REMOVE_DIRECTORY: rights_t = 0x0000000002000000; +pub const RIGHT_PATH_UNLINK_FILE: rights_t = 0x0000000004000000; +pub const RIGHT_POLL_FD_READWRITE: rights_t = 0x0000000008000000; +pub const RIGHT_SOCK_SHUTDOWN: rights_t = 0x0000000010000000; + +pub const roflags_t = u16; +pub const SOCK_RECV_DATA_TRUNCATED: roflags_t = 0x0001; + +pub const sdflags_t = u8; +pub const SHUT_RD: sdflags_t = 0x01; +pub const SHUT_WR: sdflags_t = 0x02; + +pub const siflags_t = u16; + +pub const signal_t = u8; +pub const SIGHUP: signal_t = 1; +pub const SIGINT: signal_t = 2; +pub const SIGQUIT: signal_t = 3; +pub const SIGILL: signal_t = 4; +pub const SIGTRAP: signal_t = 5; +pub const SIGABRT: signal_t = 6; +pub const SIGBUS: signal_t = 7; +pub const SIGFPE: signal_t = 8; +pub const SIGKILL: signal_t = 9; +pub const SIGUSR1: signal_t = 10; +pub const SIGSEGV: signal_t = 11; +pub const SIGUSR2: signal_t = 12; +pub const SIGPIPE: signal_t = 13; +pub const SIGALRM: signal_t = 14; +pub const SIGTERM: signal_t = 15; +pub const SIGCHLD: signal_t = 16; +pub const SIGCONT: signal_t = 17; +pub const SIGSTOP: signal_t = 18; +pub const SIGTSTP: signal_t = 19; +pub const SIGTTIN: signal_t = 20; +pub const SIGTTOU: signal_t = 21; +pub const SIGURG: signal_t = 22; +pub const SIGXCPU: signal_t = 23; +pub const SIGXFSZ: signal_t = 24; +pub const SIGVTALRM: signal_t = 25; +pub const SIGPROF: signal_t = 26; +pub const SIGWINCH: signal_t = 27; +pub const SIGPOLL: signal_t = 28; +pub const SIGPWR: signal_t = 29; +pub const SIGSYS: signal_t = 30; + +pub const subclockflags_t = u16; +pub const SUBSCRIPTION_CLOCK_ABSTIME: subclockflags_t = 0x0001; + +pub const subscription_t = extern struct { + userdata: userdata_t, + @"type": eventtype_t, + u: extern union { + clock: extern struct { + identifier: userdata_t, + clock_id: clockid_t, + timeout: timestamp_t, + precision: timestamp_t, + flags: subclockflags_t, + }, + fd_readwrite: extern struct { + fd: fd_t, + }, + }, +}; + +pub const timestamp_t = u64; + +pub const userdata_t = u64; + +pub const whence_t = u8; +pub const WHENCE_CUR: whence_t = 0; +pub const WHENCE_END: whence_t = 1; +pub const WHENCE_SET: whence_t = 2; diff --git a/std/os/windows.zig b/std/os/windows.zig @@ -6,6 +6,8 @@ pub const is_the_target = switch (builtin.os) { .windows => true, else => false, }; +pub const posix = @import("windows/posix.zig"); +pub use posix; pub use @import("windows/advapi32.zig"); pub use @import("windows/kernel32.zig"); @@ -20,7 +22,6 @@ test "import" { } pub const ERROR = @import("windows/error.zig"); -pub const errno_codes = @import("windows/errno.zig"); pub const SHORT = c_short; pub const BOOL = c_int; @@ -65,15 +66,6 @@ pub const LONGLONG = i64; pub const TRUE = 1; pub const FALSE = 0; -/// The standard input device. Initially, this is the console input buffer, CONIN$. -pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; - -/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; - -/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; - pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize)); pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD)); diff --git a/std/os/windows/errno.zig b/std/os/windows/errno.zig @@ -1 +0,0 @@ -// TODO get these values from msvcrt diff --git a/std/os/windows/posix.zig b/std/os/windows/posix.zig @@ -0,0 +1,15 @@ +// Declarations that are intended to be imported into the POSIX namespace. +const std = @import("../../std.zig"); +const maxInt = std.math.maxInt; +use std.os.windows; + +pub const fd_t = HANDLE; + +/// The standard input device. Initially, this is the console input buffer, CONIN$. +pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; + +/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; + +/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig @@ -11,8 +11,6 @@ const cstr = std.cstr; pub const WaitError = error{ WaitAbandoned, WaitTimeOut, - - /// See https://github.com/ziglang/zig/issues/1396 Unexpected, };