diff options
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | cmake/Findllvm.cmake | 1 | ||||
| -rw-r--r-- | src-self-hosted/libc_installation.zig | 2 | ||||
| -rw-r--r-- | src/analyze.cpp | 11 | ||||
| -rw-r--r-- | src/codegen.cpp | 3 | ||||
| -rw-r--r-- | src/link.cpp | 10 | ||||
| -rw-r--r-- | src/os.cpp | 19 | ||||
| -rw-r--r-- | src/os.hpp | 2 | ||||
| -rw-r--r-- | src/target.cpp | 2 | ||||
| -rw-r--r-- | std/c/index.zig | 1 | ||||
| -rw-r--r-- | std/c/netbsd.zig | 116 | ||||
| -rw-r--r-- | std/debug/index.zig | 6 | ||||
| -rw-r--r-- | std/event/fs.zig | 31 | ||||
| -rw-r--r-- | std/event/loop.zig | 21 | ||||
| -rw-r--r-- | std/heap.zig | 6 | ||||
| -rw-r--r-- | std/os/file.zig | 6 | ||||
| -rw-r--r-- | std/os/get_app_data_dir.zig | 2 | ||||
| -rw-r--r-- | std/os/get_user_id.zig | 2 | ||||
| -rw-r--r-- | std/os/index.zig | 48 | ||||
| -rw-r--r-- | std/os/netbsd/errno.zig | 134 | ||||
| -rw-r--r-- | std/os/netbsd/index.zig | 728 | ||||
| -rw-r--r-- | std/os/path.zig | 4 | ||||
| -rw-r--r-- | std/os/time.zig | 10 |
23 files changed, 1101 insertions, 67 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index db5f50908c..11b53c6f59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -447,6 +447,7 @@ set(ZIG_STD_FILES "c/freebsd.zig" "c/index.zig" "c/linux.zig" + "c/netbsd.zig" "c/windows.zig" "coff.zig" "crypto/blake2.zig" @@ -587,6 +588,8 @@ set(ZIG_STD_FILES "os/linux/index.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" + "os/netbsd/errno.zig" + "os/netbsd/index.zig" "os/path.zig" "os/time.zig" "os/uefi.zig" diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake index 2f0afa09b7..490bdae66e 100644 --- a/cmake/Findllvm.cmake +++ b/cmake/Findllvm.cmake @@ -50,7 +50,6 @@ NEED_TARGET("AMDGPU") NEED_TARGET("ARM") NEED_TARGET("BPF") NEED_TARGET("Hexagon") -NEED_TARGET("Lanai") NEED_TARGET("Mips") NEED_TARGET("MSP430") NEED_TARGET("NVPTX") diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index d3a461bcd9..b196d7bc70 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -172,7 +172,7 @@ pub const LibCInstallation = struct { try group.call(findNativeStaticLibDir, self, loop); try group.call(findNativeDynamicLinker, self, loop); }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.include_dir = try std.mem.dupe(loop.allocator, u8, "/usr/include"); }, else => @compileError("unimplemented: find libc for this OS"), diff --git a/src/analyze.cpp b/src/analyze.cpp index 9941104bc4..37c0041314 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4732,7 +4732,8 @@ void find_libc_include_path(CodeGen *g) { } } else if (g->zig_target.os == OsLinux || g->zig_target.os == OsMacOSX || - g->zig_target.os == OsFreeBSD) + g->zig_target.os == OsFreeBSD || + g->zig_target.os == OsNetBSD) { g->libc_include_dir = get_posix_libc_include_path(); } else { @@ -4781,7 +4782,7 @@ void find_libc_lib_path(CodeGen *g) { } else if (g->zig_target.os == OsLinux) { g->libc_lib_dir = get_linux_libc_lib_path("crt1.o"); - } else if (g->zig_target.os == OsFreeBSD) { + } else if ((g->zig_target.os == OsFreeBSD) || (g->zig_target.os == OsNetBSD)) { g->libc_lib_dir = buf_create_from_str("/usr/lib"); } else { zig_panic("Unable to determine libc lib path."); @@ -4795,7 +4796,7 @@ void find_libc_lib_path(CodeGen *g) { return; } else if (g->zig_target.os == OsLinux) { g->libc_static_lib_dir = get_linux_libc_lib_path("crtbegin.o"); - } else if (g->zig_target.os == OsFreeBSD) { + } else if ((g->zig_target.os == OsFreeBSD) || (g->zig_target.os == OsNetBSD)) { g->libc_static_lib_dir = buf_create_from_str("/usr/lib"); } else { zig_panic("Unable to determine libc static lib path."); @@ -6710,7 +6711,9 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) { if (is_libc && g->libc_link_lib != nullptr) return g->libc_link_lib; - if (g->enable_cache && is_libc && g->zig_target.os != OsMacOSX && g->zig_target.os != OsIOS && g->zig_target.os != OsFreeBSD) { + if (g->enable_cache && is_libc && g->zig_target.os != OsMacOSX && + g->zig_target.os != OsIOS && g->zig_target.os != OsFreeBSD && + g->zig_target.os != OsNetBSD) { fprintf(stderr, "TODO linking against libc is currently incompatible with `--cache on`.\n" "Zig is not yet capable of determining whether the libc installation has changed on subsequent builds.\n"); exit(1); diff --git a/src/codegen.cpp b/src/codegen.cpp index d2b2836b0c..d54d06f3b5 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -184,7 +184,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out // On Darwin/MacOS/iOS, we always link libSystem which contains libc. if (g->zig_target.os == OsMacOSX || g->zig_target.os == OsIOS || - g->zig_target.os == OsFreeBSD) + g->zig_target.os == OsFreeBSD || + g->zig_target.os == OsNetBSD) { g->libc_link_lib = create_link_lib(buf_create_from_str("c")); g->link_libs_list.append(g->libc_link_lib); diff --git a/src/link.cpp b/src/link.cpp index 58221a99ea..956e5b6bfc 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -198,6 +198,9 @@ static Buf *get_dynamic_linker_path(CodeGen *g) { if (g->zig_target.os == OsFreeBSD) { return buf_create_from_str("/libexec/ld-elf.so.1"); } + if (g->zig_target.os == OsNetBSD) { + return buf_create_from_str("/libexec/ld.elf_so"); + } if (g->is_native_target && g->zig_target.arch.arch == ZigLLVM_x86_64) { static const char *ld_names[] = { "ld-linux-x86-64.so.2", @@ -263,10 +266,13 @@ static void construct_linker_job_elf(LinkJob *lj) { if (lj->link_in_crt) { const char *crt1o; const char *crtbegino; - if (g->is_static) { + if (g->zig_target.os == OsNetBSD) { + crt1o = "crt0.o"; + crtbegino = "crtbegin.o"; + } else if (g->is_static) { crt1o = "crt1.o"; crtbegino = "crtbeginT.o"; - } else { + } else { crt1o = "Scrt1.o"; crtbegino = "crtbegin.o"; } diff --git a/src/os.cpp b/src/os.cpp index 8eb6b34654..c11840b1cf 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -50,11 +50,11 @@ typedef SSIZE_T ssize_t; #endif -#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) #include <link.h> #endif -#if defined(ZIG_OS_FREEBSD) +#if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) #include <sys/sysctl.h> #endif @@ -78,7 +78,7 @@ static clock_serv_t cclock; #if defined(__APPLE__) && !defined(environ) #include <crt_externs.h> #define environ (*_NSGetEnviron()) -#elif defined(ZIG_OS_FREEBSD) +#elif defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) extern char **environ; #endif @@ -1458,6 +1458,15 @@ Error os_self_exe_path(Buf *out_path) { } buf_resize(out_path, cb - 1); return ErrorNone; +#elif defined(ZIG_OS_NETBSD) + buf_resize(out_path, PATH_MAX); + int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; + size_t cb = PATH_MAX; + if (sysctl(mib, 4, buf_ptr(out_path), &cb, nullptr, 0) != 0) { + return ErrorUnexpected; + } + buf_resize(out_path, cb - 1); + return ErrorNone; #endif return ErrorFileNotFound; } @@ -1776,7 +1785,7 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { } -#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) { ZigList<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data); if (info->dlpi_name[0] == '/') { @@ -1787,7 +1796,7 @@ static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, #endif Error os_self_exe_shared_libs(ZigList<Buf *> &paths) { -#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) paths.resize(0); dl_iterate_phdr(self_exe_shared_libs_callback, &paths); return ErrorNone; diff --git a/src/os.hpp b/src/os.hpp index 68ac6e0171..dab7b84896 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -25,6 +25,8 @@ #define ZIG_OS_LINUX #elif defined(__FreeBSD__) #define ZIG_OS_FREEBSD +#elif defined(__NetBSD__) +#define ZIG_OS_NETBSD #else #define ZIG_OS_UNKNOWN #endif diff --git a/src/target.cpp b/src/target.cpp index b1434c6871..cea7826313 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -743,6 +743,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) { case OsMacOSX: case OsZen: case OsFreeBSD: + case OsNetBSD: case OsOpenBSD: switch (id) { case CIntTypeShort: @@ -783,7 +784,6 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) { case OsIOS: case OsKFreeBSD: case OsLv2: - case OsNetBSD: case OsSolaris: case OsHaiku: case OsMinix: diff --git a/std/c/index.zig b/std/c/index.zig index 058d879c09..74cdd884ca 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -6,6 +6,7 @@ pub use switch (builtin.os) { Os.windows => @import("windows.zig"), Os.macosx, Os.ios => @import("darwin.zig"), Os.freebsd => @import("freebsd.zig"), + Os.netbsd => @import("netbsd.zig"), else => empty_import, }; const empty_import = @import("../empty.zig"); diff --git a/std/c/netbsd.zig b/std/c/netbsd.zig new file mode 100644 index 0000000000..796d45dafc --- /dev/null +++ b/std/c/netbsd.zig @@ -0,0 +1,116 @@ +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 { + msg_name: *u8, + msg_namelen: socklen_t, + msg_iov: *iovec, + msg_iovlen: i32, + __pad1: i32, + msg_control: *u8, + msg_controllen: socklen_t, + __pad2: socklen_t, + 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/debug/index.zig b/std/debug/index.zig index 7e5be9acef..b8ea7e7f84 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -240,7 +240,7 @@ pub fn writeCurrentStackTraceWindows( pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { switch (builtin.os) { builtin.Os.macosx => return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color), - builtin.Os.linux, builtin.Os.freebsd => return printSourceAtAddressLinux(debug_info, out_stream, address, tty_color), + builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return printSourceAtAddressLinux(debug_info, out_stream, address, tty_color), builtin.Os.windows => return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color), else => return error.UnsupportedOperatingSystem, } @@ -717,7 +717,7 @@ pub const OpenSelfDebugInfoError = error{ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { switch (builtin.os) { - builtin.Os.linux, builtin.Os.freebsd => return openSelfDebugInfoLinux(allocator), + builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return openSelfDebugInfoLinux(allocator), builtin.Os.macosx, builtin.Os.ios => return openSelfDebugInfoMacOs(allocator), builtin.Os.windows => return openSelfDebugInfoWindows(allocator), else => return error.UnsupportedOperatingSystem, @@ -1141,7 +1141,7 @@ pub const DebugInfo = switch (builtin.os) { sect_contribs: []pdb.SectionContribEntry, modules: []Module, }, - builtin.Os.linux, builtin.Os.freebsd => DwarfInfo, + builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => DwarfInfo, else => @compileError("Unsupported OS"), }; diff --git a/std/event/fs.zig b/std/event/fs.zig index 0e7482ec60..6535945573 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -85,6 +85,7 @@ pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, o builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, + builtin.Os.netbsd, => { const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); defer loop.allocator.free(iovecs); @@ -222,6 +223,7 @@ pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, + builtin.Os.netbsd, => { const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); defer loop.allocator.free(iovecs); @@ -402,7 +404,11 @@ pub async fn openPosix( pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHandle { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd => { + builtin.Os.macosx, + builtin.Os.linux, + builtin.Os.freebsd, + builtin.Os.netbsd + => { const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); }, @@ -431,6 +437,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, + builtin.Os.netbsd, => { const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); @@ -453,7 +460,7 @@ pub async fn openReadWrite( mode: os.File.Mode, ) os.File.OpenError!os.FileHandle { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd => { + 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; return await (async openPosix(loop, path, flags, mode) catch unreachable); }, @@ -481,7 +488,7 @@ pub const CloseOperation = struct { os_data: OsData, const OsData = switch (builtin.os) { - builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd => OsDataPosix, + builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => OsDataPosix, builtin.Os.windows => struct { handle: ?os.FileHandle, @@ -500,7 +507,7 @@ pub const CloseOperation = struct { self.* = CloseOperation{ .loop = loop, .os_data = switch (builtin.os) { - builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd => initOsDataPosix(self), + builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => initOsDataPosix(self), builtin.Os.windows => OsData{ .handle = null }, else => @compileError("Unsupported OS"), }, @@ -530,6 +537,7 @@ pub const CloseOperation = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => { if (self.os_data.have_fd) { self.loop.posixFsRequest(&self.os_data.close_req_node); @@ -552,6 +560,7 @@ pub const CloseOperation = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => { self.os_data.close_req_node.data.msg.Close.fd = handle; self.os_data.have_fd = true; @@ -569,6 +578,7 @@ pub const CloseOperation = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => { self.os_data.have_fd = false; }, @@ -584,6 +594,7 @@ pub const CloseOperation = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => { assert(self.os_data.have_fd); return self.os_data.close_req_node.data.msg.Close.fd; @@ -608,6 +619,7 @@ pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => return await (async writeFileModeThread(loop, path, contents, mode) catch unreachable), builtin.Os.windows => return await (async writeFileWindows(loop, path, contents) catch unreachable), else => @compileError("Unsupported OS"), @@ -713,7 +725,7 @@ pub fn Watch(comptime V: type) type { os_data: OsData, const OsData = switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => struct { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => struct { file_table: FileTable, table_lock: event.Lock, @@ -802,7 +814,7 @@ pub fn Watch(comptime V: type) type { return self; }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { const self = try loop.allocator.create(Self); errdefer loop.allocator.destroy(self); @@ -822,7 +834,7 @@ pub fn Watch(comptime V: type) type { /// All addFile calls and removeFile calls must have completed. pub fn destroy(self: *Self) void { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { // TODO we need to cancel the coroutines before destroying the lock self.os_data.table_lock.deinit(); var it = self.os_data.file_table.iterator(); @@ -864,7 +876,10 @@ pub fn Watch(comptime V: type) type { pub async fn addFile(self: *Self, file_path: []const u8, value: V) !?V { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => return await (async addFileKEvent(self, file_path, value) catch unreachable), + builtin.Os.macosx, + builtin.Os.freebsd, + builtin.Os.netbsd + => return await (async addFileKEvent(self, file_path, value) catch unreachable), builtin.Os.linux => return await (async addFileLinux(self, file_path, value) catch unreachable), builtin.Os.windows => return await (async addFileWindows(self, file_path, value) catch unreachable), else => @compileError("Unsupported OS"), diff --git a/std/event/loop.zig b/std/event/loop.zig index b92bf41982..55a1538f0c 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -50,7 +50,7 @@ pub const Loop = struct { }; pub const EventFd = switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => KEventFd, + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => KEventFd, builtin.Os.linux => struct { base: ResumeNode, epoll_op: u32, @@ -69,7 +69,7 @@ pub const Loop = struct { }; pub const Basic = switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => KEventBasic, + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => KEventBasic, builtin.Os.linux => struct { base: ResumeNode, }, @@ -221,7 +221,7 @@ pub const Loop = struct { self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); } }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.os_data.kqfd = try os.bsdKQueue(); errdefer os.close(self.os_data.kqfd); @@ -386,7 +386,7 @@ pub const Loop = struct { os.close(self.os_data.epollfd); self.allocator.free(self.eventfd_resume_nodes); }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { os.close(self.os_data.kqfd); os.close(self.os_data.fs_kqfd); }, @@ -501,7 +501,7 @@ pub const Loop = struct { const eventfd_node = &resume_stack_node.data; eventfd_node.base.handle = next_tick_node.data; switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { const kevent_array = (*[1]posix.Kevent)(&eventfd_node.kevent); const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch { @@ -564,6 +564,7 @@ pub const Loop = struct { builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, + builtin.Os.netbsd, => self.os_data.fs_thread.wait(), else => {}, } @@ -628,7 +629,7 @@ pub const Loop = struct { os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; return; }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.posixFsRequest(&self.os_data.fs_end_request); const final_kevent = (*[1]posix.Kevent)(&self.os_data.final_kevent); const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; @@ -686,7 +687,7 @@ pub const Loop = struct { } } }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { var eventlist: [1]posix.Kevent = undefined; const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; const count = os.bsdKEvent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable; @@ -749,7 +750,7 @@ pub const Loop = struct { self.beginOneEvent(); // finished in posixFsRun after processing the msg self.os_data.fs_queue.put(request_node); switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wake); const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable; @@ -819,7 +820,7 @@ pub const Loop = struct { else => unreachable, } }, - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wait); var out_kevs: [1]posix.Kevent = undefined; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable; @@ -831,7 +832,7 @@ pub const Loop = struct { const OsData = switch (builtin.os) { builtin.Os.linux => LinuxOsData, - builtin.Os.macosx, builtin.Os.freebsd => KEventData, + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => KEventData, builtin.Os.windows => struct { io_port: windows.HANDLE, extra_thread_count: usize, diff --git a/std/heap.zig b/std/heap.zig index e7088150a8..e257977f9d 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -71,7 +71,7 @@ pub const DirectAllocator = struct { const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const p = os.posix; const alloc_size = if (alignment <= os.page_size) n else n + alignment; const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0); @@ -120,7 +120,7 @@ pub const DirectAllocator = struct { const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { if (new_size <= old_mem.len) { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; @@ -164,7 +164,7 @@ pub const DirectAllocator = struct { const self = @fieldParentPtr(DirectAllocator, "allocator", allocator); switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { _ = os.posix.munmap(@ptrToInt(bytes.ptr), bytes.len); }, Os.windows => { diff --git a/std/os/file.zig b/std/os/file.zig index 2ae547c694..85aa6e1215 100644 --- a/std/os/file.zig +++ b/std/os/file.zig @@ -237,7 +237,7 @@ pub const File = struct { pub fn seekForward(self: File, amount: isize) SeekError!void { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const result = posix.lseek(self.handle, amount, posix.SEEK_CUR); const err = posix.getErrno(result); if (err > 0) { @@ -268,7 +268,7 @@ pub const File = struct { pub fn seekTo(self: File, pos: usize) SeekError!void { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const ipos = try math.cast(isize, pos); const result = posix.lseek(self.handle, ipos, posix.SEEK_SET); const err = posix.getErrno(result); @@ -309,7 +309,7 @@ pub const File = struct { pub fn getPos(self: File) GetSeekPosError!usize { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const result = posix.lseek(self.handle, 0, posix.SEEK_CUR); const err = posix.getErrno(result); if (err > 0) { diff --git a/std/os/get_app_data_dir.zig b/std/os/get_app_data_dir.zig index f5e0b78eec..a49d164f46 100644 --- a/std/os/get_app_data_dir.zig +++ b/std/os/get_app_data_dir.zig @@ -43,7 +43,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD }; return os.path.join(allocator, [][]const u8{ home_dir, "Library", "Application Support", appname }); }, - builtin.Os.linux, builtin.Os.freebsd => { + builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { const home_dir = os.getEnvPosix("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; diff --git a/std/os/get_user_id.zig b/std/os/get_user_id.zig index 9a4d1ab275..6747cc4557 100644 --- a/std/os/get_user_id.zig +++ b/std/os/get_user_id.zig @@ -11,7 +11,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) !UserInfo { return switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => posixGetUserInfo(name), + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } diff --git a/std/os/index.zig b/std/os/index.zig index f52c12c2b6..5d749479de 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const Os = builtin.Os; const is_windows = builtin.os == Os.windows; const is_posix = switch (builtin.os) { - builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd => true, + builtin.Os.linux, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => true, else => false, }; const os = @This(); @@ -30,6 +30,7 @@ pub const windows = @import("windows/index.zig"); pub const darwin = @import("darwin.zig"); pub const linux = @import("linux/index.zig"); pub const freebsd = @import("freebsd/index.zig"); +pub const netbsd = @import("netbsd/index.zig"); pub const zen = @import("zen.zig"); pub const uefi = @import("uefi.zig"); @@ -37,6 +38,7 @@ pub const posix = switch (builtin.os) { Os.linux => linux, Os.macosx, Os.ios => darwin, Os.freebsd => freebsd, + Os.netbsd => netbsd, Os.zen => zen, else => @compileError("Unsupported OS"), }; @@ -50,7 +52,7 @@ pub const time = @import("time.zig"); pub const page_size = 4 * 1024; pub const MAX_PATH_BYTES = switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => posix.PATH_MAX, + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => posix.PATH_MAX, // Each UTF-16LE character may be expanded to 3 UTF-8 bytes. // If it would require 4 UTF-8 bytes, then there would be a surrogate // pair in the UTF-16LE, and we (over)account 3 bytes for it that way. @@ -125,7 +127,7 @@ pub fn getRandomBytes(buf: []u8) !void { else => return unexpectedErrorPosix(errno), } }, - Os.macosx, Os.ios, Os.freebsd => return getRandomBytesDevURandom(buf), + Os.macosx, Os.ios, Os.freebsd, Os.netbsd => return getRandomBytesDevURandom(buf), Os.windows => { // Call RtlGenRandom() instead of CryptGetRandom() on Windows // https://github.com/rust-lang-nursery/rand/issues/111 @@ -185,7 +187,7 @@ pub fn abort() noreturn { c.abort(); } switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { _ = posix.raise(posix.SIGABRT); _ = posix.raise(posix.SIGKILL); while (true) {} @@ -211,7 +213,7 @@ pub fn exit(status: u8) noreturn { c.exit(status); } switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { posix.exit(status); }, Os.windows => { @@ -327,7 +329,7 @@ pub fn posix_preadv(fd: i32, iov: [*]const posix.iovec, count: usize, offset: u6 } } }, - builtin.Os.linux, builtin.Os.freebsd => while (true) { + builtin.Os.linux, builtin.Os.freebsd, Os.netbsd => while (true) { const rc = posix.preadv(fd, iov, count, offset); const err = posix.getErrno(rc); switch (err) { @@ -434,7 +436,7 @@ pub fn posix_pwritev(fd: i32, iov: [*]const posix.iovec_const, count: usize, off } } }, - builtin.Os.linux, builtin.Os.freebsd => while (true) { + builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => while (true) { const rc = posix.pwritev(fd, iov, count, offset); const err = posix.getErrno(rc); switch (err) { @@ -699,7 +701,7 @@ pub fn getBaseAddress() usize { const phdr = linuxGetAuxVal(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); }, - builtin.Os.macosx, builtin.Os.freebsd => return @ptrToInt(&std.c._mh_execute_header), + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => return @ptrToInt(&std.c._mh_execute_header), builtin.Os.windows => return @ptrToInt(windows.GetModuleHandleW(null)), else => @compileError("Unsupported OS"), } @@ -1339,7 +1341,7 @@ pub fn deleteDirC(dir_path: [*]const u8) DeleteDirError!void { const dir_path_w = try windows_util.cStrToPrefixedFileW(dir_path); return deleteDirW(&dir_path_w); }, - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const err = posix.getErrno(posix.rmdir(dir_path)); switch (err) { 0 => return, @@ -1382,7 +1384,7 @@ pub fn deleteDir(dir_path: []const u8) DeleteDirError!void { const dir_path_w = try windows_util.sliceToPrefixedFileW(dir_path); return deleteDirW(&dir_path_w); }, - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const dir_path_c = try toPosixPath(dir_path); return deleteDirC(&dir_path_c); }, @@ -1501,7 +1503,7 @@ pub const Dir = struct { allocator: *Allocator, pub const Handle = switch (builtin.os) { - Os.macosx, Os.ios, Os.freebsd => struct { + Os.macosx, Os.ios, Os.freebsd, Os.netbsd => struct { fd: i32, seek: i64, buf: []u8, @@ -1578,7 +1580,7 @@ pub const Dir = struct { .name_data = undefined, }; }, - Os.macosx, Os.ios, Os.freebsd => Handle{ + Os.macosx, Os.ios, Os.freebsd, Os.netbsd => Handle{ .fd = try posixOpen( dir_path, posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, @@ -1609,7 +1611,7 @@ pub const Dir = struct { Os.windows => { _ = windows.FindClose(self.handle.handle); }, - Os.macosx, Os.ios, Os.linux, Os.freebsd => { + Os.macosx, Os.ios, Os.linux, Os.freebsd, Os.netbsd => { self.allocator.free(self.handle.buf); os.close(self.handle.fd); }, @@ -1625,6 +1627,7 @@ pub const Dir = struct { Os.macosx, Os.ios => return self.nextDarwin(), Os.windows => return self.nextWindows(), Os.freebsd => return self.nextFreebsd(), + Os.netbsd => return self.nextFreebsd(), else => @compileError("unimplemented"), } } @@ -2256,7 +2259,7 @@ pub fn unexpectedErrorWindows(err: windows.DWORD) UnexpectedError { pub fn openSelfExe() !os.File { switch (builtin.os) { Os.linux => return os.File.openReadC(c"/proc/self/exe"), - Os.macosx, Os.ios, Os.freebsd => { + Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { var buf: [MAX_PATH_BYTES]u8 = undefined; const self_exe_path = try selfExePath(&buf); buf[self_exe_path.len] = 0; @@ -2317,6 +2320,19 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { else => unexpectedErrorPosix(err), }; }, + 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), + }; + }, Os.windows => { var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; const utf16le_slice = try selfExePathW(&utf16le_buf); @@ -2355,7 +2371,7 @@ pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) ![]const u8 { // will not return null. return path.dirname(full_exe_path).?; }, - Os.windows, Os.macosx, Os.ios, Os.freebsd => { + Os.windows, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const self_exe_path = try selfExePath(out_buffer); // Assume that the OS APIs return absolute paths, and therefore dirname // will not return null. @@ -3227,7 +3243,7 @@ pub const CpuCountError = error{ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.freebsd => { + builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { var count: c_int = undefined; var count_len: usize = @sizeOf(c_int); const rc = posix.sysctlbyname(switch (builtin.os) { diff --git a/std/os/netbsd/errno.zig b/std/os/netbsd/errno.zig new file mode 100644 index 0000000000..1772c2ab07 --- /dev/null +++ b/std/os/netbsd/errno.zig @@ -0,0 +1,134 @@ +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/netbsd/index.zig b/std/os/netbsd/index.zig new file mode 100644 index 0000000000..53de61126c --- /dev/null +++ b/std/os/netbsd/index.zig @@ -0,0 +1,728 @@ +const builtin = @import("builtin"); + +pub use @import("errno.zig"); + +const std = @import("../../index.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; + +/// XXX no EVFILT_USER, but what is it +pub const EVFILT_USER = 0; + +/// 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); +} diff --git a/std/os/path.zig b/std/os/path.zig index 5ba345fceb..290f8ba17f 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -1226,7 +1226,7 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro const pathname_w = try windows_util.cStrToPrefixedFileW(pathname); return realW(out_buffer, pathname_w); }, - Os.freebsd, Os.macosx, Os.ios => { + Os.freebsd, Os.netbsd, Os.macosx, Os.ios => { // TODO instead of calling the libc function here, port the implementation to Zig const err = posix.getErrno(posix.realpath(pathname, out_buffer)); switch (err) { @@ -1267,7 +1267,7 @@ pub fn real(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: []const u8) RealError! const pathname_w = try windows_util.sliceToPrefixedFileW(pathname); return realW(out_buffer, &pathname_w); }, - Os.macosx, Os.ios, Os.linux, Os.freebsd => { + Os.macosx, Os.ios, Os.linux, Os.freebsd, Os.netbsd => { const pathname_c = try os.toPosixPath(pathname); return realC(out_buffer, &pathname_c); }, diff --git a/std/os/time.zig b/std/os/time.zig index 638fb41ff1..bce6f265de 100644 --- a/std/os/time.zig +++ b/std/os/time.zig @@ -14,7 +14,7 @@ pub const epoch = @import("epoch.zig"); /// Sleep for the specified duration pub fn sleep(nanoseconds: u64) void { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd => { + Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { const s = nanoseconds / ns_per_s; const ns = nanoseconds % ns_per_s; posixSleep(@intCast(u63, s), @intCast(u63, ns)); @@ -62,7 +62,7 @@ pub fn timestamp() u64 { /// Get the posix timestamp, UTC, in milliseconds pub const milliTimestamp = switch (builtin.os) { Os.windows => milliTimestampWindows, - Os.linux, Os.freebsd => milliTimestampPosix, + Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix, Os.macosx, Os.ios => milliTimestampDarwin, else => @compileError("Unsupported OS"), }; @@ -178,7 +178,7 @@ pub const Timer = struct { debug.assert(err != windows.FALSE); self.start_time = @intCast(u64, start_time); }, - Os.linux, Os.freebsd => { + Os.linux, Os.freebsd, Os.netbsd => { //On Linux, seccomp can do arbitrary things to our ability to call // syscalls, including return any errno value it wants and // inconsistently throwing errors. Since we can't account for @@ -214,7 +214,7 @@ pub const Timer = struct { var clock = clockNative() - self.start_time; return switch (builtin.os) { Os.windows => @divFloor(clock * ns_per_s, self.frequency), - Os.linux, Os.freebsd => clock, + Os.linux, Os.freebsd, Os.netbsd => clock, Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom), else => @compileError("Unsupported OS"), }; @@ -235,7 +235,7 @@ pub const Timer = struct { const clockNative = switch (builtin.os) { Os.windows => clockWindows, - Os.linux, Os.freebsd => clockLinux, + Os.linux, Os.freebsd, Os.netbsd => clockLinux, Os.macosx, Os.ios => clockDarwin, else => @compileError("Unsupported OS"), }; |
