From 44f38b04b0cc374fcd377df0fe68f29c824185ff Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 31 Jan 2018 11:13:39 -0500 Subject: fix assertion fail when using global var number literal closes #697 --- src/analyze.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/analyze.cpp b/src/analyze.cpp index 4fa8dad7ce..2812c9d79e 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3295,6 +3295,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { is_const, init_val, &tld_var->base); tld_var->var->linkage = linkage; + if (implicit_type != nullptr && type_is_invalid(implicit_type)) { + tld_var->var->value->type = g->builtin_types.entry_invalid; + } + if (var_decl->align_expr != nullptr) { if (!analyze_const_align(g, tld_var->base.parent_scope, var_decl->align_expr, &tld_var->var->align_bytes)) { tld_var->var->value->type = g->builtin_types.entry_invalid; -- cgit v1.2.3 From 44d8d654a0ba463a1d4cf34d435c8422bfcd1c81 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 5 Feb 2018 09:26:39 -0500 Subject: fix test failure, organize code, add new compile error --- CMakeLists.txt | 9 +- src/ir.cpp | 18 ++ std/c/linux.zig | 2 +- std/io.zig | 2 +- std/os/index.zig | 37 ++- std/os/linux.zig | 797 ------------------------------------------------ std/os/linux/errno.zig | 146 +++++++++ std/os/linux/i386.zig | 505 ++++++++++++++++++++++++++++++ std/os/linux/index.zig | 796 +++++++++++++++++++++++++++++++++++++++++++++++ std/os/linux/test.zig | 38 +++ std/os/linux/x86_64.zig | 490 +++++++++++++++++++++++++++++ std/os/linux_errno.zig | 146 --------- std/os/linux_i386.zig | 504 ------------------------------ std/os/linux_random.zig | 246 --------------- std/os/linux_test.zig | 38 --- std/os/linux_x86_64.zig | 489 ----------------------------- test/compile_errors.zig | 6 + 17 files changed, 2033 insertions(+), 2236 deletions(-) delete mode 100644 std/os/linux.zig create mode 100644 std/os/linux/errno.zig create mode 100644 std/os/linux/i386.zig create mode 100644 std/os/linux/index.zig create mode 100644 std/os/linux/test.zig create mode 100644 std/os/linux/x86_64.zig delete mode 100644 std/os/linux_errno.zig delete mode 100644 std/os/linux_i386.zig delete mode 100644 std/os/linux_random.zig delete mode 100644 std/os/linux_test.zig delete mode 100644 std/os/linux_x86_64.zig (limited to 'src') diff --git a/CMakeLists.txt b/CMakeLists.txt index b41640e72a..012ad075aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -439,11 +439,10 @@ set(ZIG_STD_FILES "os/darwin_errno.zig" "os/get_user_id.zig" "os/index.zig" - "os/linux.zig" - "os/linux_errno.zig" - "os/linux_random.zig" - "os/linux_i386.zig" - "os/linux_x86_64.zig" + "os/linux/index.zig" + "os/linux/errno.zig" + "os/linux/i386.zig" + "os/linux/x86_64.zig" "os/path.zig" "os/windows/error.zig" "os/windows/index.zig" diff --git a/src/ir.cpp b/src/ir.cpp index 9d5f59d187..1dc717286a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6193,6 +6193,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc if (other_type->id == TypeTableEntryIdFloat) { return true; } else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) { + if (!other_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) { + Buf *val_buf = buf_alloc(); + bigint_append_buf(val_buf, &const_val->data.x_bigint, 10); + ir_add_error(ira, instruction, + buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'", + buf_ptr(val_buf), + buf_ptr(&other_type->name))); + return false; + } if (bigint_fits_in_bits(&const_val->data.x_bigint, other_type->data.integral.bit_count, other_type->data.integral.is_signed)) { @@ -6205,6 +6214,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc if (const_val_fits_in_num_lit(const_val, child_type)) { return true; } else if (child_type->id == TypeTableEntryIdInt && const_val_is_int) { + if (!child_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) { + Buf *val_buf = buf_alloc(); + bigint_append_buf(val_buf, &const_val->data.x_bigint, 10); + ir_add_error(ira, instruction, + buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'", + buf_ptr(val_buf), + buf_ptr(&child_type->name))); + return false; + } if (bigint_fits_in_bits(&const_val->data.x_bigint, child_type->data.integral.bit_count, child_type->data.integral.is_signed)) diff --git a/std/c/linux.zig b/std/c/linux.zig index 6378955795..b2ac05eba5 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -1,4 +1,4 @@ -pub use @import("../os/linux_errno.zig"); +pub use @import("../os/linux/errno.zig"); pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int; extern "c" fn __errno_location() &c_int; diff --git a/std/io.zig b/std/io.zig index 41f32622f8..2fe57e4dfe 100644 --- a/std/io.zig +++ b/std/io.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const Os = builtin.Os; const system = switch(builtin.os) { - Os.linux => @import("os/linux.zig"), + Os.linux => @import("os/linux/index.zig"), Os.macosx, Os.ios => @import("os/darwin.zig"), Os.windows => @import("os/windows/index.zig"), else => @compileError("Unsupported OS"), diff --git a/std/os/index.zig b/std/os/index.zig index 00a0d7b94c..ed1e262f82 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -6,7 +6,7 @@ const os = this; pub const windows = @import("windows/index.zig"); pub const darwin = @import("darwin.zig"); -pub const linux = @import("linux.zig"); +pub const linux = @import("linux/index.zig"); pub const zen = @import("zen.zig"); pub const posix = switch(builtin.os) { Os.linux => linux, @@ -78,13 +78,28 @@ error WouldBlock; pub fn getRandomBytes(buf: []u8) %void { switch (builtin.os) { Os.linux => while (true) { - const err = posix.getErrno(posix.getRandomBytes(buf)); - if (err > 0) return unexpectedErrorPosix(err); + // TODO check libc version and potentially call c.getrandom. + // See #397 + const err = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)); + if (err > 0) { + switch (err) { + posix.EINVAL => unreachable, + posix.EFAULT => unreachable, + posix.EINTR => continue, + posix.ENOSYS => { + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); + defer close(fd); + + try posixRead(fd, buf); + return; + }, + else => return unexpectedErrorPosix(err), + } + } return; }, Os.macosx, Os.ios => { - const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, - 0, null); + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); defer close(fd); try posixRead(fd, buf); @@ -253,8 +268,12 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al mem.copy(u8, path0, file_path); path0[file_path.len] = 0; + return posixOpenC(path0.ptr, flags, perm); +} + +pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) %i32 { while (true) { - const result = posix.open(path0.ptr, flags, perm); + const result = posix.open(file_path, flags, perm); const err = posix.getErrno(result); if (err > 0) { return switch (err) { @@ -1486,10 +1505,10 @@ test "std.os" { _ = @import("darwin_errno.zig"); _ = @import("darwin.zig"); _ = @import("get_user_id.zig"); - _ = @import("linux_errno.zig"); + _ = @import("linux/errno.zig"); //_ = @import("linux_i386.zig"); - _ = @import("linux_x86_64.zig"); - _ = @import("linux.zig"); + _ = @import("linux/x86_64.zig"); + _ = @import("linux/index.zig"); _ = @import("path.zig"); _ = @import("windows/index.zig"); } diff --git a/std/os/linux.zig b/std/os/linux.zig deleted file mode 100644 index 79ae623a1c..0000000000 --- a/std/os/linux.zig +++ /dev/null @@ -1,797 +0,0 @@ -const std = @import("../index.zig"); -const assert = std.debug.assert; -const builtin = @import("builtin"); -const arch = switch (builtin.arch) { - builtin.Arch.x86_64 => @import("linux_x86_64.zig"), - builtin.Arch.i386 => @import("linux_i386.zig"), - else => @compileError("unsupported arch"), -}; -pub use @import("linux_errno.zig"); -pub use @import("linux_random.zig"); - -pub const PATH_MAX = 4096; - -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 PROT_GROWSDOWN = 0x01000000; -pub const PROT_GROWSUP = 0x02000000; - -pub const MAP_FAILED = @maxValue(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 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 O_CREAT = arch.O_CREAT; -pub const O_EXCL = arch.O_EXCL; -pub const O_NOCTTY = arch.O_NOCTTY; -pub const O_TRUNC = arch.O_TRUNC; -pub const O_APPEND = arch.O_APPEND; -pub const O_NONBLOCK = arch.O_NONBLOCK; -pub const O_DSYNC = arch.O_DSYNC; -pub const O_SYNC = arch.O_SYNC; -pub const O_RSYNC = arch.O_RSYNC; -pub const O_DIRECTORY = arch.O_DIRECTORY; -pub const O_NOFOLLOW = arch.O_NOFOLLOW; -pub const O_CLOEXEC = arch.O_CLOEXEC; - -pub const O_ASYNC = arch.O_ASYNC; -pub const O_DIRECT = arch.O_DIRECT; -pub const O_LARGEFILE = arch.O_LARGEFILE; -pub const O_NOATIME = arch.O_NOATIME; -pub const O_PATH = arch.O_PATH; -pub const O_TMPFILE = arch.O_TMPFILE; -pub const O_NDELAY = arch.O_NDELAY; - -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 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 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; - - -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_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 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 (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) usize(-signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); -} - -pub fn chdir(path: &const u8) usize { - return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); -} - -pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize { - return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); -} - -pub fn fork() usize { - return arch.syscall0(arch.SYS_fork); -} - -pub fn getcwd(buf: &u8, size: usize) usize { - return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size); -} - -pub fn getdents(fd: i32, dirp: &u8, count: usize) usize { - return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); -} - -pub fn isatty(fd: i32) bool { - var wsz: winsize = undefined; - return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; -} - -pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize { - return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); -} - -pub fn mkdir(path: &const u8, mode: u32) usize { - return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); -} - -pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize { - return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), - @bitCast(usize, offset)); -} - -pub fn munmap(address: &u8, length: usize) usize { - return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length); -} - -pub fn read(fd: i32, buf: &u8, count: usize) usize { - return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count); -} - -pub fn rmdir(path: &const u8) usize { - return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); -} - -pub fn symlink(existing: &const u8, new: &const u8) usize { - return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); -} - -pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize { - return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); -} - -pub fn pipe(fd: &[2]i32) usize { - return pipe2(fd, 0); -} - -pub fn pipe2(fd: &[2]i32, flags: usize) usize { - return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); -} - -pub fn write(fd: i32, buf: &const u8, count: usize) usize { - return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count); -} - -pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize { - return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset); -} - -pub fn rename(old: &const u8, new: &const u8) usize { - return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); -} - -pub fn open(path: &const u8, flags: u32, perm: usize) usize { - return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); -} - -pub fn create(path: &const u8, perm: usize) usize { - return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); -} - -pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize { - return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); -} - -pub fn close(fd: i32) usize { - return arch.syscall1(arch.SYS_close, usize(fd)); -} - -pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { - return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos); -} - -pub fn exit(status: i32) noreturn { - _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn getrandom(buf: &u8, count: usize, flags: u32) usize { - return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)); -} - -pub fn kill(pid: i32, sig: i32) usize { - return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)); -} - -pub fn unlink(path: &const u8) usize { - return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); -} - -pub fn waitpid(pid: i32, status: &i32, options: i32) usize { - return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); -} - -pub fn nanosleep(req: &const timespec, rem: ?×pec) usize { - return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); -} - -pub fn setuid(uid: u32) usize { - return arch.syscall1(arch.SYS_setuid, uid); -} - -pub fn setgid(gid: u32) usize { - return arch.syscall1(arch.SYS_setgid, gid); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return arch.syscall2(arch.SYS_setreuid, ruid, euid); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return arch.syscall2(arch.SYS_setregid, rgid, egid); -} - -pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize { - return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8); -} - -pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize { - assert(sig >= 1); - assert(sig != SIGKILL); - assert(sig != SIGSTOP); - var ksa = k_sigaction { - .handler = act.handler, - .flags = act.flags | SA_RESTORER, - .mask = undefined, - .restorer = @ptrCast(extern fn()void, arch.restore_rt), - }; - var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8); - const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); - const err = getErrno(result); - if (err != 0) { - return result; - } - if (oact) |old| { - old.handler = ksa_old.handler; - old.flags = @truncate(u32, ksa_old.flags); - @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); - } - return 0; -} - -const NSIG = 65; -const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []usize{@maxValue(usize)}; -const app_mask = []usize{0xfffffffc7fffffff}; - -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, @maxValue(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 fn raise(sig: i32) usize { - var set: sigset_t = undefined; - blockAppSignals(&set); - const tid = i32(arch.syscall0(arch.SYS_gettid)); - const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)); - restoreSignals(&set); - return ret; -} - -fn blockAllSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8); -} - -fn blockAppSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8); -} - -fn restoreSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8); -} - -pub fn sigaddset(set: &sigset_t, sig: u6) void { - const s = sig - 1; - (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1)); -} - -pub fn sigismember(set: &const sigset_t, sig: u6) bool { - const s = sig - 1; - return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0; -} - - -pub const sa_family_t = u16; -pub const socklen_t = u32; -pub const in_addr = u32; -pub const in6_addr = [16]u8; - -pub const sockaddr = extern struct { - family: sa_family_t, - port: u16, - data: [12]u8, -}; - -pub const sockaddr_in = extern struct { - family: sa_family_t, - port: u16, - addr: in_addr, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - family: sa_family_t, - port: u16, - flowinfo: u32, - addr: in6_addr, - scope_id: u32, -}; - -pub const iovec = extern struct { - iov_base: &u8, - iov_len: usize, -}; - -pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize { - return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)); -} - -pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize { - return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)); -} - -pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize { - return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)); -} - -pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize { - return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags); -} - -pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize { - return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)); -} - -pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize { - return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags); -} - -pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, - noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize -{ - return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); -} - -pub fn shutdown(fd: i32, how: i32) usize { - return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)); -} - -pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize { - return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)); -} - -pub fn listen(fd: i32, backlog: i32) usize { - return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)); -} - -pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize { - return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)); -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { - return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])); -} - -pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return accept4(fd, addr, len, 0); -} - -pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize { - return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags); -} - -// error NameTooLong; -// error SystemResources; -// error Io; -// -// pub fn if_nametoindex(name: []u8) %u32 { -// var ifr: ifreq = undefined; -// -// if (name.len >= ifr.ifr_name.len) { -// return error.NameTooLong; -// } -// -// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); -// const socket_err = getErrno(socket_ret); -// if (socket_err > 0) { -// return error.SystemResources; -// } -// const socket_fd = i32(socket_ret); -// @memcpy(&ifr.ifr_name[0], &name[0], name.len); -// ifr.ifr_name[name.len] = 0; -// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); -// close(socket_fd); -// const ioctl_err = getErrno(ioctl_ret); -// if (ioctl_err > 0) { -// return error.Io; -// } -// return ifr.ifr_ifindex; -// } - -pub const Stat = arch.Stat; -pub const timespec = arch.timespec; - -pub fn fstat(fd: i32, stat_buf: &Stat) usize { - return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)); -} - -pub const epoll_data = u64; - -pub const epoll_event = extern struct { - events: u32, - data: epoll_data -}; - -pub fn epoll_create() usize { - return arch.syscall1(arch.SYS_epoll_create, usize(1)); -} - -pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize { - return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)); -} - -pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize { - return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)); -} - -pub fn timerfd_create(clockid: i32, flags: u32) usize { - return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)); -} - -pub const itimerspec = extern struct { - it_interval: timespec, - it_value: timespec -}; - -pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize { - return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)); -} - -pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize { - return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); -} - -test "import linux_test" { - // TODO lazy analysis should prevent this test from being compiled on windows, but - // it is still compiled on windows - if (builtin.os == builtin.Os.linux) { - _ = @import("linux_test.zig"); - } -} diff --git a/std/os/linux/errno.zig b/std/os/linux/errno.zig new file mode 100644 index 0000000000..39f4e37a10 --- /dev/null +++ b/std/os/linux/errno.zig @@ -0,0 +1,146 @@ +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; /// I/O error +pub const ENXIO = 6; /// No such device or address +pub const E2BIG = 7; /// Arg list too long +pub const ENOEXEC = 8; /// Exec format error +pub const EBADF = 9; /// Bad file number +pub const ECHILD = 10; /// No child processes +pub const EAGAIN = 11; /// Try again +pub const ENOMEM = 12; /// Out of 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 or resource busy +pub const EEXIST = 17; /// File exists +pub const EXDEV = 18; /// Cross-device link +pub const ENODEV = 19; /// No such 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; /// File table overflow +pub const EMFILE = 24; /// Too many open files +pub const ENOTTY = 25; /// Not a typewriter +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 +pub const EDOM = 33; /// Math argument out of domain of func +pub const ERANGE = 34; /// Math result not representable +pub const EDEADLK = 35; /// Resource deadlock would occur +pub const ENAMETOOLONG = 36; /// File name too long +pub const ENOLCK = 37; /// No record locks available +pub const ENOSYS = 38; /// Function not implemented +pub const ENOTEMPTY = 39; /// Directory not empty +pub const ELOOP = 40; /// Too many symbolic links encountered +pub const EWOULDBLOCK = EAGAIN; /// Operation would block +pub const ENOMSG = 42; /// No message of desired type +pub const EIDRM = 43; /// Identifier removed +pub const ECHRNG = 44; /// Channel number out of range +pub const EL2NSYNC = 45; /// Level 2 not synchronized +pub const EL3HLT = 46; /// Level 3 halted +pub const EL3RST = 47; /// Level 3 reset +pub const ELNRNG = 48; /// Link number out of range +pub const EUNATCH = 49; /// Protocol driver not attached +pub const ENOCSI = 50; /// No CSI structure available +pub const EL2HLT = 51; /// Level 2 halted +pub const EBADE = 52; /// Invalid exchange +pub const EBADR = 53; /// Invalid request descriptor +pub const EXFULL = 54; /// Exchange full +pub const ENOANO = 55; /// No anode +pub const EBADRQC = 56; /// Invalid request code +pub const EBADSLT = 57; /// Invalid slot + +pub const EBFONT = 59; /// Bad font file format +pub const ENOSTR = 60; /// Device not a stream +pub const ENODATA = 61; /// No data available +pub const ETIME = 62; /// Timer expired +pub const ENOSR = 63; /// Out of streams resources +pub const ENONET = 64; /// Machine is not on the network +pub const ENOPKG = 65; /// Package not installed +pub const EREMOTE = 66; /// Object is remote +pub const ENOLINK = 67; /// Link has been severed +pub const EADV = 68; /// Advertise error +pub const ESRMNT = 69; /// Srmount error +pub const ECOMM = 70; /// Communication error on send +pub const EPROTO = 71; /// Protocol error +pub const EMULTIHOP = 72; /// Multihop attempted +pub const EDOTDOT = 73; /// RFS specific error +pub const EBADMSG = 74; /// Not a data message +pub const EOVERFLOW = 75; /// Value too large for defined data type +pub const ENOTUNIQ = 76; /// Name not unique on network +pub const EBADFD = 77; /// File descriptor in bad state +pub const EREMCHG = 78; /// Remote address changed +pub const ELIBACC = 79; /// Can not access a needed shared library +pub const ELIBBAD = 80; /// Accessing a corrupted shared library +pub const ELIBSCN = 81; /// .lib section in a.out corrupted +pub const ELIBMAX = 82; /// Attempting to link in too many shared libraries +pub const ELIBEXEC = 83; /// Cannot exec a shared library directly +pub const EILSEQ = 84; /// Illegal byte sequence +pub const ERESTART = 85; /// Interrupted system call should be restarted +pub const ESTRPIPE = 86; /// Streams pipe error +pub const EUSERS = 87; /// Too many users +pub const ENOTSOCK = 88; /// Socket operation on non-socket +pub const EDESTADDRREQ = 89; /// Destination address required +pub const EMSGSIZE = 90; /// Message too long +pub const EPROTOTYPE = 91; /// Protocol wrong type for socket +pub const ENOPROTOOPT = 92; /// Protocol not available +pub const EPROTONOSUPPORT = 93; /// Protocol not supported +pub const ESOCKTNOSUPPORT = 94; /// Socket type not supported +pub const EOPNOTSUPP = 95; /// Operation not supported on transport endpoint +pub const EPFNOSUPPORT = 96; /// Protocol family not supported +pub const EAFNOSUPPORT = 97; /// Address family not supported by protocol +pub const EADDRINUSE = 98; /// Address already in use +pub const EADDRNOTAVAIL = 99; /// Cannot assign requested address +pub const ENETDOWN = 100; /// Network is down +pub const ENETUNREACH = 101; /// Network is unreachable +pub const ENETRESET = 102; /// Network dropped connection because of reset +pub const ECONNABORTED = 103; /// Software caused connection abort +pub const ECONNRESET = 104; /// Connection reset by peer +pub const ENOBUFS = 105; /// No buffer space available +pub const EISCONN = 106; /// Transport endpoint is already connected +pub const ENOTCONN = 107; /// Transport endpoint is not connected +pub const ESHUTDOWN = 108; /// Cannot send after transport endpoint shutdown +pub const ETOOMANYREFS = 109; /// Too many references: cannot splice +pub const ETIMEDOUT = 110; /// Connection timed out +pub const ECONNREFUSED = 111; /// Connection refused +pub const EHOSTDOWN = 112; /// Host is down +pub const EHOSTUNREACH = 113; /// No route to host +pub const EALREADY = 114; /// Operation already in progress +pub const EINPROGRESS = 115; /// Operation now in progress +pub const ESTALE = 116; /// Stale NFS file handle +pub const EUCLEAN = 117; /// Structure needs cleaning +pub const ENOTNAM = 118; /// Not a XENIX named type file +pub const ENAVAIL = 119; /// No XENIX semaphores available +pub const EISNAM = 120; /// Is a named type file +pub const EREMOTEIO = 121; /// Remote I/O error +pub const EDQUOT = 122; /// Quota exceeded + +pub const ENOMEDIUM = 123; /// No medium found +pub const EMEDIUMTYPE = 124; /// Wrong medium type + +// nameserver query return codes +pub const ENSROK = 0; /// DNS server returned answer with no data +pub const ENSRNODATA = 160; /// DNS server returned answer with no data +pub const ENSRFORMERR = 161; /// DNS server claims query was misformatted +pub const ENSRSERVFAIL = 162; /// DNS server returned general failure +pub const ENSRNOTFOUND = 163; /// Domain name not found +pub const ENSRNOTIMP = 164; /// DNS server does not implement requested operation +pub const ENSRREFUSED = 165; /// DNS server refused query +pub const ENSRBADQUERY = 166; /// Misformatted DNS query +pub const ENSRBADNAME = 167; /// Misformatted domain name +pub const ENSRBADFAMILY = 168; /// Unsupported address family +pub const ENSRBADRESP = 169; /// Misformatted DNS reply +pub const ENSRCONNREFUSED = 170; /// Could not contact DNS servers +pub const ENSRTIMEOUT = 171; /// Timeout while contacting DNS servers +pub const ENSROF = 172; /// End of file +pub const ENSRFILE = 173; /// Error reading file +pub const ENSRNOMEM = 174; /// Out of memory +pub const ENSRDESTRUCTION = 175; /// Application terminated lookup +pub const ENSRQUERYDOMAINTOOLONG = 176; /// Domain name is too long +pub const ENSRCNAMELOOP = 177; /// Domain name is too long diff --git a/std/os/linux/i386.zig b/std/os/linux/i386.zig new file mode 100644 index 0000000000..691377e24e --- /dev/null +++ b/std/os/linux/i386.zig @@ -0,0 +1,505 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +pub const SYS_restart_syscall = 0; +pub const SYS_exit = 1; +pub const SYS_fork = 2; +pub const SYS_read = 3; +pub const SYS_write = 4; +pub const SYS_open = 5; +pub const SYS_close = 6; +pub const SYS_waitpid = 7; +pub const SYS_creat = 8; +pub const SYS_link = 9; +pub const SYS_unlink = 10; +pub const SYS_execve = 11; +pub const SYS_chdir = 12; +pub const SYS_time = 13; +pub const SYS_mknod = 14; +pub const SYS_chmod = 15; +pub const SYS_lchown = 16; +pub const SYS_break = 17; +pub const SYS_oldstat = 18; +pub const SYS_lseek = 19; +pub const SYS_getpid = 20; +pub const SYS_mount = 21; +pub const SYS_umount = 22; +pub const SYS_setuid = 23; +pub const SYS_getuid = 24; +pub const SYS_stime = 25; +pub const SYS_ptrace = 26; +pub const SYS_alarm = 27; +pub const SYS_oldfstat = 28; +pub const SYS_pause = 29; +pub const SYS_utime = 30; +pub const SYS_stty = 31; +pub const SYS_gtty = 32; +pub const SYS_access = 33; +pub const SYS_nice = 34; +pub const SYS_ftime = 35; +pub const SYS_sync = 36; +pub const SYS_kill = 37; +pub const SYS_rename = 38; +pub const SYS_mkdir = 39; +pub const SYS_rmdir = 40; +pub const SYS_dup = 41; +pub const SYS_pipe = 42; +pub const SYS_times = 43; +pub const SYS_prof = 44; +pub const SYS_brk = 45; +pub const SYS_setgid = 46; +pub const SYS_getgid = 47; +pub const SYS_signal = 48; +pub const SYS_geteuid = 49; +pub const SYS_getegid = 50; +pub const SYS_acct = 51; +pub const SYS_umount2 = 52; +pub const SYS_lock = 53; +pub const SYS_ioctl = 54; +pub const SYS_fcntl = 55; +pub const SYS_mpx = 56; +pub const SYS_setpgid = 57; +pub const SYS_ulimit = 58; +pub const SYS_oldolduname = 59; +pub const SYS_umask = 60; +pub const SYS_chroot = 61; +pub const SYS_ustat = 62; +pub const SYS_dup2 = 63; +pub const SYS_getppid = 64; +pub const SYS_getpgrp = 65; +pub const SYS_setsid = 66; +pub const SYS_sigaction = 67; +pub const SYS_sgetmask = 68; +pub const SYS_ssetmask = 69; +pub const SYS_setreuid = 70; +pub const SYS_setregid = 71; +pub const SYS_sigsuspend = 72; +pub const SYS_sigpending = 73; +pub const SYS_sethostname = 74; +pub const SYS_setrlimit = 75; +pub const SYS_getrlimit = 76; +pub const SYS_getrusage = 77; +pub const SYS_gettimeofday = 78; +pub const SYS_settimeofday = 79; +pub const SYS_getgroups = 80; +pub const SYS_setgroups = 81; +pub const SYS_select = 82; +pub const SYS_symlink = 83; +pub const SYS_oldlstat = 84; +pub const SYS_readlink = 85; +pub const SYS_uselib = 86; +pub const SYS_swapon = 87; +pub const SYS_reboot = 88; +pub const SYS_readdir = 89; +pub const SYS_mmap = 90; +pub const SYS_munmap = 91; +pub const SYS_truncate = 92; +pub const SYS_ftruncate = 93; +pub const SYS_fchmod = 94; +pub const SYS_fchown = 95; +pub const SYS_getpriority = 96; +pub const SYS_setpriority = 97; +pub const SYS_profil = 98; +pub const SYS_statfs = 99; +pub const SYS_fstatfs = 100; +pub const SYS_ioperm = 101; +pub const SYS_socketcall = 102; +pub const SYS_syslog = 103; +pub const SYS_setitimer = 104; +pub const SYS_getitimer = 105; +pub const SYS_stat = 106; +pub const SYS_lstat = 107; +pub const SYS_fstat = 108; +pub const SYS_olduname = 109; +pub const SYS_iopl = 110; +pub const SYS_vhangup = 111; +pub const SYS_idle = 112; +pub const SYS_vm86old = 113; +pub const SYS_wait4 = 114; +pub const SYS_swapoff = 115; +pub const SYS_sysinfo = 116; +pub const SYS_ipc = 117; +pub const SYS_fsync = 118; +pub const SYS_sigreturn = 119; +pub const SYS_clone = 120; +pub const SYS_setdomainname = 121; +pub const SYS_uname = 122; +pub const SYS_modify_ldt = 123; +pub const SYS_adjtimex = 124; +pub const SYS_mprotect = 125; +pub const SYS_sigprocmask = 126; +pub const SYS_create_module = 127; +pub const SYS_init_module = 128; +pub const SYS_delete_module = 129; +pub const SYS_get_kernel_syms = 130; +pub const SYS_quotactl = 131; +pub const SYS_getpgid = 132; +pub const SYS_fchdir = 133; +pub const SYS_bdflush = 134; +pub const SYS_sysfs = 135; +pub const SYS_personality = 136; +pub const SYS_afs_syscall = 137; +pub const SYS_setfsuid = 138; +pub const SYS_setfsgid = 139; +pub const SYS__llseek = 140; +pub const SYS_getdents = 141; +pub const SYS__newselect = 142; +pub const SYS_flock = 143; +pub const SYS_msync = 144; +pub const SYS_readv = 145; +pub const SYS_writev = 146; +pub const SYS_getsid = 147; +pub const SYS_fdatasync = 148; +pub const SYS__sysctl = 149; +pub const SYS_mlock = 150; +pub const SYS_munlock = 151; +pub const SYS_mlockall = 152; +pub const SYS_munlockall = 153; +pub const SYS_sched_setparam = 154; +pub const SYS_sched_getparam = 155; +pub const SYS_sched_setscheduler = 156; +pub const SYS_sched_getscheduler = 157; +pub const SYS_sched_yield = 158; +pub const SYS_sched_get_priority_max = 159; +pub const SYS_sched_get_priority_min = 160; +pub const SYS_sched_rr_get_interval = 161; +pub const SYS_nanosleep = 162; +pub const SYS_mremap = 163; +pub const SYS_setresuid = 164; +pub const SYS_getresuid = 165; +pub const SYS_vm86 = 166; +pub const SYS_query_module = 167; +pub const SYS_poll = 168; +pub const SYS_nfsservctl = 169; +pub const SYS_setresgid = 170; +pub const SYS_getresgid = 171; +pub const SYS_prctl = 172; +pub const SYS_rt_sigreturn = 173; +pub const SYS_rt_sigaction = 174; +pub const SYS_rt_sigprocmask = 175; +pub const SYS_rt_sigpending = 176; +pub const SYS_rt_sigtimedwait = 177; +pub const SYS_rt_sigqueueinfo = 178; +pub const SYS_rt_sigsuspend = 179; +pub const SYS_pread64 = 180; +pub const SYS_pwrite64 = 181; +pub const SYS_chown = 182; +pub const SYS_getcwd = 183; +pub const SYS_capget = 184; +pub const SYS_capset = 185; +pub const SYS_sigaltstack = 186; +pub const SYS_sendfile = 187; +pub const SYS_getpmsg = 188; +pub const SYS_putpmsg = 189; +pub const SYS_vfork = 190; +pub const SYS_ugetrlimit = 191; +pub const SYS_mmap2 = 192; +pub const SYS_truncate64 = 193; +pub const SYS_ftruncate64 = 194; +pub const SYS_stat64 = 195; +pub const SYS_lstat64 = 196; +pub const SYS_fstat64 = 197; +pub const SYS_lchown32 = 198; +pub const SYS_getuid32 = 199; +pub const SYS_getgid32 = 200; +pub const SYS_geteuid32 = 201; +pub const SYS_getegid32 = 202; +pub const SYS_setreuid32 = 203; +pub const SYS_setregid32 = 204; +pub const SYS_getgroups32 = 205; +pub const SYS_setgroups32 = 206; +pub const SYS_fchown32 = 207; +pub const SYS_setresuid32 = 208; +pub const SYS_getresuid32 = 209; +pub const SYS_setresgid32 = 210; +pub const SYS_getresgid32 = 211; +pub const SYS_chown32 = 212; +pub const SYS_setuid32 = 213; +pub const SYS_setgid32 = 214; +pub const SYS_setfsuid32 = 215; +pub const SYS_setfsgid32 = 216; +pub const SYS_pivot_root = 217; +pub const SYS_mincore = 218; +pub const SYS_madvise = 219; +pub const SYS_madvise1 = 219; +pub const SYS_getdents64 = 220; +pub const SYS_fcntl64 = 221; +pub const SYS_gettid = 224; +pub const SYS_readahead = 225; +pub const SYS_setxattr = 226; +pub const SYS_lsetxattr = 227; +pub const SYS_fsetxattr = 228; +pub const SYS_getxattr = 229; +pub const SYS_lgetxattr = 230; +pub const SYS_fgetxattr = 231; +pub const SYS_listxattr = 232; +pub const SYS_llistxattr = 233; +pub const SYS_flistxattr = 234; +pub const SYS_removexattr = 235; +pub const SYS_lremovexattr = 236; +pub const SYS_fremovexattr = 237; +pub const SYS_tkill = 238; +pub const SYS_sendfile64 = 239; +pub const SYS_futex = 240; +pub const SYS_sched_setaffinity = 241; +pub const SYS_sched_getaffinity = 242; +pub const SYS_set_thread_area = 243; +pub const SYS_get_thread_area = 244; +pub const SYS_io_setup = 245; +pub const SYS_io_destroy = 246; +pub const SYS_io_getevents = 247; +pub const SYS_io_submit = 248; +pub const SYS_io_cancel = 249; +pub const SYS_fadvise64 = 250; +pub const SYS_exit_group = 252; +pub const SYS_lookup_dcookie = 253; +pub const SYS_epoll_create = 254; +pub const SYS_epoll_ctl = 255; +pub const SYS_epoll_wait = 256; +pub const SYS_remap_file_pages = 257; +pub const SYS_set_tid_address = 258; +pub const SYS_timer_create = 259; +pub const SYS_timer_settime = SYS_timer_create+1; +pub const SYS_timer_gettime = SYS_timer_create+2; +pub const SYS_timer_getoverrun = SYS_timer_create+3; +pub const SYS_timer_delete = SYS_timer_create+4; +pub const SYS_clock_settime = SYS_timer_create+5; +pub const SYS_clock_gettime = SYS_timer_create+6; +pub const SYS_clock_getres = SYS_timer_create+7; +pub const SYS_clock_nanosleep = SYS_timer_create+8; +pub const SYS_statfs64 = 268; +pub const SYS_fstatfs64 = 269; +pub const SYS_tgkill = 270; +pub const SYS_utimes = 271; +pub const SYS_fadvise64_64 = 272; +pub const SYS_vserver = 273; +pub const SYS_mbind = 274; +pub const SYS_get_mempolicy = 275; +pub const SYS_set_mempolicy = 276; +pub const SYS_mq_open = 277; +pub const SYS_mq_unlink = SYS_mq_open+1; +pub const SYS_mq_timedsend = SYS_mq_open+2; +pub const SYS_mq_timedreceive = SYS_mq_open+3; +pub const SYS_mq_notify = SYS_mq_open+4; +pub const SYS_mq_getsetattr = SYS_mq_open+5; +pub const SYS_kexec_load = 283; +pub const SYS_waitid = 284; +pub const SYS_add_key = 286; +pub const SYS_request_key = 287; +pub const SYS_keyctl = 288; +pub const SYS_ioprio_set = 289; +pub const SYS_ioprio_get = 290; +pub const SYS_inotify_init = 291; +pub const SYS_inotify_add_watch = 292; +pub const SYS_inotify_rm_watch = 293; +pub const SYS_migrate_pages = 294; +pub const SYS_openat = 295; +pub const SYS_mkdirat = 296; +pub const SYS_mknodat = 297; +pub const SYS_fchownat = 298; +pub const SYS_futimesat = 299; +pub const SYS_fstatat64 = 300; +pub const SYS_unlinkat = 301; +pub const SYS_renameat = 302; +pub const SYS_linkat = 303; +pub const SYS_symlinkat = 304; +pub const SYS_readlinkat = 305; +pub const SYS_fchmodat = 306; +pub const SYS_faccessat = 307; +pub const SYS_pselect6 = 308; +pub const SYS_ppoll = 309; +pub const SYS_unshare = 310; +pub const SYS_set_robust_list = 311; +pub const SYS_get_robust_list = 312; +pub const SYS_splice = 313; +pub const SYS_sync_file_range = 314; +pub const SYS_tee = 315; +pub const SYS_vmsplice = 316; +pub const SYS_move_pages = 317; +pub const SYS_getcpu = 318; +pub const SYS_epoll_pwait = 319; +pub const SYS_utimensat = 320; +pub const SYS_signalfd = 321; +pub const SYS_timerfd_create = 322; +pub const SYS_eventfd = 323; +pub const SYS_fallocate = 324; +pub const SYS_timerfd_settime = 325; +pub const SYS_timerfd_gettime = 326; +pub const SYS_signalfd4 = 327; +pub const SYS_eventfd2 = 328; +pub const SYS_epoll_create1 = 329; +pub const SYS_dup3 = 330; +pub const SYS_pipe2 = 331; +pub const SYS_inotify_init1 = 332; +pub const SYS_preadv = 333; +pub const SYS_pwritev = 334; +pub const SYS_rt_tgsigqueueinfo = 335; +pub const SYS_perf_event_open = 336; +pub const SYS_recvmmsg = 337; +pub const SYS_fanotify_init = 338; +pub const SYS_fanotify_mark = 339; +pub const SYS_prlimit64 = 340; +pub const SYS_name_to_handle_at = 341; +pub const SYS_open_by_handle_at = 342; +pub const SYS_clock_adjtime = 343; +pub const SYS_syncfs = 344; +pub const SYS_sendmmsg = 345; +pub const SYS_setns = 346; +pub const SYS_process_vm_readv = 347; +pub const SYS_process_vm_writev = 348; +pub const SYS_kcmp = 349; +pub const SYS_finit_module = 350; +pub const SYS_sched_setattr = 351; +pub const SYS_sched_getattr = 352; +pub const SYS_renameat2 = 353; +pub const SYS_seccomp = 354; +pub const SYS_getrandom = 355; +pub const SYS_memfd_create = 356; +pub const SYS_bpf = 357; +pub const SYS_execveat = 358; +pub const SYS_socket = 359; +pub const SYS_socketpair = 360; +pub const SYS_bind = 361; +pub const SYS_connect = 362; +pub const SYS_listen = 363; +pub const SYS_accept4 = 364; +pub const SYS_getsockopt = 365; +pub const SYS_setsockopt = 366; +pub const SYS_getsockname = 367; +pub const SYS_getpeername = 368; +pub const SYS_sendto = 369; +pub const SYS_sendmsg = 370; +pub const SYS_recvfrom = 371; +pub const SYS_recvmsg = 372; +pub const SYS_shutdown = 373; +pub const SYS_userfaultfd = 374; +pub const SYS_membarrier = 375; +pub const SYS_mlock2 = 376; + + +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 = 0o100000; +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 = 12; +pub const F_SETLK = 13; +pub const F_SETLKW = 14; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +pub inline fn syscall0(number: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number)) +} + +pub inline fn syscall1(number: usize, arg1: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1)) +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2)) +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3)) +} + +pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4)) +} + +pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize) -> usize +{ + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5)) +} + +pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize, arg6: usize) -> usize +{ + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5), + [arg6] "{ebp}" (arg6)) +} + +pub nakedcc fn restore() void { + asm volatile ( + \\popl %%eax + \\movl $119, %%eax + \\int $0x80 + : + : + : "rcx", "r11") +} + +pub nakedcc fn restore_rt() void { + asm volatile ("int $0x80" + : + : [number] "{eax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11") +} diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig new file mode 100644 index 0000000000..43d175b74d --- /dev/null +++ b/std/os/linux/index.zig @@ -0,0 +1,796 @@ +const std = @import("../../index.zig"); +const assert = std.debug.assert; +const builtin = @import("builtin"); +const arch = switch (builtin.arch) { + builtin.Arch.x86_64 => @import("x86_64.zig"), + builtin.Arch.i386 => @import("i386.zig"), + else => @compileError("unsupported arch"), +}; +pub use @import("errno.zig"); + +pub const PATH_MAX = 4096; + +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 PROT_GROWSDOWN = 0x01000000; +pub const PROT_GROWSUP = 0x02000000; + +pub const MAP_FAILED = @maxValue(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 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 O_CREAT = arch.O_CREAT; +pub const O_EXCL = arch.O_EXCL; +pub const O_NOCTTY = arch.O_NOCTTY; +pub const O_TRUNC = arch.O_TRUNC; +pub const O_APPEND = arch.O_APPEND; +pub const O_NONBLOCK = arch.O_NONBLOCK; +pub const O_DSYNC = arch.O_DSYNC; +pub const O_SYNC = arch.O_SYNC; +pub const O_RSYNC = arch.O_RSYNC; +pub const O_DIRECTORY = arch.O_DIRECTORY; +pub const O_NOFOLLOW = arch.O_NOFOLLOW; +pub const O_CLOEXEC = arch.O_CLOEXEC; + +pub const O_ASYNC = arch.O_ASYNC; +pub const O_DIRECT = arch.O_DIRECT; +pub const O_LARGEFILE = arch.O_LARGEFILE; +pub const O_NOATIME = arch.O_NOATIME; +pub const O_PATH = arch.O_PATH; +pub const O_TMPFILE = arch.O_TMPFILE; +pub const O_NDELAY = arch.O_NDELAY; + +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 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 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; + + +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_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 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 (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) usize(-signed_r) else 0; +} + +pub fn dup2(old: i32, new: i32) usize { + return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); +} + +pub fn chdir(path: &const u8) usize { + return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); +} + +pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize { + return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); +} + +pub fn fork() usize { + return arch.syscall0(arch.SYS_fork); +} + +pub fn getcwd(buf: &u8, size: usize) usize { + return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size); +} + +pub fn getdents(fd: i32, dirp: &u8, count: usize) usize { + return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); +} + +pub fn isatty(fd: i32) bool { + var wsz: winsize = undefined; + return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; +} + +pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize { + return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); +} + +pub fn mkdir(path: &const u8, mode: u32) usize { + return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); +} + +pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize { + return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), + @bitCast(usize, offset)); +} + +pub fn munmap(address: &u8, length: usize) usize { + return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length); +} + +pub fn read(fd: i32, buf: &u8, count: usize) usize { + return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count); +} + +pub fn rmdir(path: &const u8) usize { + return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); +} + +pub fn symlink(existing: &const u8, new: &const u8) usize { + return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); +} + +pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize { + return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); +} + +pub fn pipe(fd: &[2]i32) usize { + return pipe2(fd, 0); +} + +pub fn pipe2(fd: &[2]i32, flags: usize) usize { + return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); +} + +pub fn write(fd: i32, buf: &const u8, count: usize) usize { + return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count); +} + +pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize { + return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset); +} + +pub fn rename(old: &const u8, new: &const u8) usize { + return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); +} + +pub fn open(path: &const u8, flags: u32, perm: usize) usize { + return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); +} + +pub fn create(path: &const u8, perm: usize) usize { + return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); +} + +pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize { + return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); +} + +pub fn close(fd: i32) usize { + return arch.syscall1(arch.SYS_close, usize(fd)); +} + +pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { + return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos); +} + +pub fn exit(status: i32) noreturn { + _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn getrandom(buf: &u8, count: usize, flags: u32) usize { + return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)); +} + +pub fn kill(pid: i32, sig: i32) usize { + return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)); +} + +pub fn unlink(path: &const u8) usize { + return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); +} + +pub fn waitpid(pid: i32, status: &i32, options: i32) usize { + return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); +} + +pub fn nanosleep(req: &const timespec, rem: ?×pec) usize { + return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); +} + +pub fn setuid(uid: u32) usize { + return arch.syscall1(arch.SYS_setuid, uid); +} + +pub fn setgid(gid: u32) usize { + return arch.syscall1(arch.SYS_setgid, gid); +} + +pub fn setreuid(ruid: u32, euid: u32) usize { + return arch.syscall2(arch.SYS_setreuid, ruid, euid); +} + +pub fn setregid(rgid: u32, egid: u32) usize { + return arch.syscall2(arch.SYS_setregid, rgid, egid); +} + +pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize { + return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8); +} + +pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize { + assert(sig >= 1); + assert(sig != SIGKILL); + assert(sig != SIGSTOP); + var ksa = k_sigaction { + .handler = act.handler, + .flags = act.flags | SA_RESTORER, + .mask = undefined, + .restorer = @ptrCast(extern fn()void, arch.restore_rt), + }; + var ksa_old: k_sigaction = undefined; + @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8); + const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); + const err = getErrno(result); + if (err != 0) { + return result; + } + if (oact) |old| { + old.handler = ksa_old.handler; + old.flags = @truncate(u32, ksa_old.flags); + @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); + } + return 0; +} + +const NSIG = 65; +const sigset_t = [128 / @sizeOf(usize)]usize; +const all_mask = []usize{@maxValue(usize)}; +const app_mask = []usize{0xfffffffc7fffffff}; + +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, @maxValue(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 fn raise(sig: i32) usize { + var set: sigset_t = undefined; + blockAppSignals(&set); + const tid = i32(arch.syscall0(arch.SYS_gettid)); + const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)); + restoreSignals(&set); + return ret; +} + +fn blockAllSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8); +} + +fn blockAppSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8); +} + +fn restoreSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8); +} + +pub fn sigaddset(set: &sigset_t, sig: u6) void { + const s = sig - 1; + (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1)); +} + +pub fn sigismember(set: &const sigset_t, sig: u6) bool { + const s = sig - 1; + return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0; +} + + +pub const sa_family_t = u16; +pub const socklen_t = u32; +pub const in_addr = u32; +pub const in6_addr = [16]u8; + +pub const sockaddr = extern struct { + family: sa_family_t, + port: u16, + data: [12]u8, +}; + +pub const sockaddr_in = extern struct { + family: sa_family_t, + port: u16, + addr: in_addr, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + family: sa_family_t, + port: u16, + flowinfo: u32, + addr: in6_addr, + scope_id: u32, +}; + +pub const iovec = extern struct { + iov_base: &u8, + iov_len: usize, +}; + +pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize { + return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)); +} + +pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize { + return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)); +} + +pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize { + return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)); +} + +pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize { + return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags); +} + +pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize { + return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)); +} + +pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize { + return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags); +} + +pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, + noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize +{ + return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); +} + +pub fn shutdown(fd: i32, how: i32) usize { + return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)); +} + +pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize { + return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)); +} + +pub fn listen(fd: i32, backlog: i32) usize { + return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)); +} + +pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize { + return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)); +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { + return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])); +} + +pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return accept4(fd, addr, len, 0); +} + +pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize { + return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags); +} + +// error NameTooLong; +// error SystemResources; +// error Io; +// +// pub fn if_nametoindex(name: []u8) %u32 { +// var ifr: ifreq = undefined; +// +// if (name.len >= ifr.ifr_name.len) { +// return error.NameTooLong; +// } +// +// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); +// const socket_err = getErrno(socket_ret); +// if (socket_err > 0) { +// return error.SystemResources; +// } +// const socket_fd = i32(socket_ret); +// @memcpy(&ifr.ifr_name[0], &name[0], name.len); +// ifr.ifr_name[name.len] = 0; +// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); +// close(socket_fd); +// const ioctl_err = getErrno(ioctl_ret); +// if (ioctl_err > 0) { +// return error.Io; +// } +// return ifr.ifr_ifindex; +// } + +pub const Stat = arch.Stat; +pub const timespec = arch.timespec; + +pub fn fstat(fd: i32, stat_buf: &Stat) usize { + return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)); +} + +pub const epoll_data = u64; + +pub const epoll_event = extern struct { + events: u32, + data: epoll_data +}; + +pub fn epoll_create() usize { + return arch.syscall1(arch.SYS_epoll_create, usize(1)); +} + +pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize { + return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)); +} + +pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize { + return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)); +} + +pub fn timerfd_create(clockid: i32, flags: u32) usize { + return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)); +} + +pub const itimerspec = extern struct { + it_interval: timespec, + it_value: timespec +}; + +pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize { + return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)); +} + +pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize { + return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); +} + +test "import linux test" { + // TODO lazy analysis should prevent this test from being compiled on windows, but + // it is still compiled on windows + if (builtin.os == builtin.Os.linux) { + _ = @import("test.zig"); + } +} diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig new file mode 100644 index 0000000000..c7dbeab67f --- /dev/null +++ b/std/os/linux/test.zig @@ -0,0 +1,38 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const assert = std.debug.assert; + +test "timer" { + const epoll_fd = linux.epoll_create(); + var err = linux.getErrno(epoll_fd); + assert(err == 0); + + const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); + assert(linux.getErrno(timer_fd) == 0); + + const time_interval = linux.timespec { + .tv_sec = 0, + .tv_nsec = 2000000 + }; + + const new_time = linux.itimerspec { + .it_interval = time_interval, + .it_value = time_interval + }; + + err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null); + assert(err == 0); + + var event = linux.epoll_event { + .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET, + .data = 0 + }; + + err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event); + assert(err == 0); + + const events_one: linux.epoll_event = undefined; + var events = []linux.epoll_event{events_one} ** 8; + + err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1); +} diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig new file mode 100644 index 0000000000..3a76ca4f87 --- /dev/null +++ b/std/os/linux/x86_64.zig @@ -0,0 +1,490 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +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_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 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 fn syscall0(number: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number) + : "rcx", "r11"); +} + +pub fn syscall1(number: usize, arg1: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1) + : "rcx", "r11"); +} + +pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2) + : "rcx", "r11"); +} + +pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3) + : "rcx", "r11"); +} + +pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4) + : "rcx", "r11"); +} + +pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5) + : "rcx", "r11"); +} + +pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, + arg5: usize, arg6: usize) usize +{ + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5), + [arg6] "{r9}" (arg6) + : "rcx", "r11"); +} + +pub nakedcc fn restore_rt() void { + return asm volatile ("syscall" + : + : [number] "{rax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11"); +} + + +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, +}; + +/// 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, +}; diff --git a/std/os/linux_errno.zig b/std/os/linux_errno.zig deleted file mode 100644 index 39f4e37a10..0000000000 --- a/std/os/linux_errno.zig +++ /dev/null @@ -1,146 +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; /// I/O error -pub const ENXIO = 6; /// No such device or address -pub const E2BIG = 7; /// Arg list too long -pub const ENOEXEC = 8; /// Exec format error -pub const EBADF = 9; /// Bad file number -pub const ECHILD = 10; /// No child processes -pub const EAGAIN = 11; /// Try again -pub const ENOMEM = 12; /// Out of 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 or resource busy -pub const EEXIST = 17; /// File exists -pub const EXDEV = 18; /// Cross-device link -pub const ENODEV = 19; /// No such 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; /// File table overflow -pub const EMFILE = 24; /// Too many open files -pub const ENOTTY = 25; /// Not a typewriter -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 -pub const EDOM = 33; /// Math argument out of domain of func -pub const ERANGE = 34; /// Math result not representable -pub const EDEADLK = 35; /// Resource deadlock would occur -pub const ENAMETOOLONG = 36; /// File name too long -pub const ENOLCK = 37; /// No record locks available -pub const ENOSYS = 38; /// Function not implemented -pub const ENOTEMPTY = 39; /// Directory not empty -pub const ELOOP = 40; /// Too many symbolic links encountered -pub const EWOULDBLOCK = EAGAIN; /// Operation would block -pub const ENOMSG = 42; /// No message of desired type -pub const EIDRM = 43; /// Identifier removed -pub const ECHRNG = 44; /// Channel number out of range -pub const EL2NSYNC = 45; /// Level 2 not synchronized -pub const EL3HLT = 46; /// Level 3 halted -pub const EL3RST = 47; /// Level 3 reset -pub const ELNRNG = 48; /// Link number out of range -pub const EUNATCH = 49; /// Protocol driver not attached -pub const ENOCSI = 50; /// No CSI structure available -pub const EL2HLT = 51; /// Level 2 halted -pub const EBADE = 52; /// Invalid exchange -pub const EBADR = 53; /// Invalid request descriptor -pub const EXFULL = 54; /// Exchange full -pub const ENOANO = 55; /// No anode -pub const EBADRQC = 56; /// Invalid request code -pub const EBADSLT = 57; /// Invalid slot - -pub const EBFONT = 59; /// Bad font file format -pub const ENOSTR = 60; /// Device not a stream -pub const ENODATA = 61; /// No data available -pub const ETIME = 62; /// Timer expired -pub const ENOSR = 63; /// Out of streams resources -pub const ENONET = 64; /// Machine is not on the network -pub const ENOPKG = 65; /// Package not installed -pub const EREMOTE = 66; /// Object is remote -pub const ENOLINK = 67; /// Link has been severed -pub const EADV = 68; /// Advertise error -pub const ESRMNT = 69; /// Srmount error -pub const ECOMM = 70; /// Communication error on send -pub const EPROTO = 71; /// Protocol error -pub const EMULTIHOP = 72; /// Multihop attempted -pub const EDOTDOT = 73; /// RFS specific error -pub const EBADMSG = 74; /// Not a data message -pub const EOVERFLOW = 75; /// Value too large for defined data type -pub const ENOTUNIQ = 76; /// Name not unique on network -pub const EBADFD = 77; /// File descriptor in bad state -pub const EREMCHG = 78; /// Remote address changed -pub const ELIBACC = 79; /// Can not access a needed shared library -pub const ELIBBAD = 80; /// Accessing a corrupted shared library -pub const ELIBSCN = 81; /// .lib section in a.out corrupted -pub const ELIBMAX = 82; /// Attempting to link in too many shared libraries -pub const ELIBEXEC = 83; /// Cannot exec a shared library directly -pub const EILSEQ = 84; /// Illegal byte sequence -pub const ERESTART = 85; /// Interrupted system call should be restarted -pub const ESTRPIPE = 86; /// Streams pipe error -pub const EUSERS = 87; /// Too many users -pub const ENOTSOCK = 88; /// Socket operation on non-socket -pub const EDESTADDRREQ = 89; /// Destination address required -pub const EMSGSIZE = 90; /// Message too long -pub const EPROTOTYPE = 91; /// Protocol wrong type for socket -pub const ENOPROTOOPT = 92; /// Protocol not available -pub const EPROTONOSUPPORT = 93; /// Protocol not supported -pub const ESOCKTNOSUPPORT = 94; /// Socket type not supported -pub const EOPNOTSUPP = 95; /// Operation not supported on transport endpoint -pub const EPFNOSUPPORT = 96; /// Protocol family not supported -pub const EAFNOSUPPORT = 97; /// Address family not supported by protocol -pub const EADDRINUSE = 98; /// Address already in use -pub const EADDRNOTAVAIL = 99; /// Cannot assign requested address -pub const ENETDOWN = 100; /// Network is down -pub const ENETUNREACH = 101; /// Network is unreachable -pub const ENETRESET = 102; /// Network dropped connection because of reset -pub const ECONNABORTED = 103; /// Software caused connection abort -pub const ECONNRESET = 104; /// Connection reset by peer -pub const ENOBUFS = 105; /// No buffer space available -pub const EISCONN = 106; /// Transport endpoint is already connected -pub const ENOTCONN = 107; /// Transport endpoint is not connected -pub const ESHUTDOWN = 108; /// Cannot send after transport endpoint shutdown -pub const ETOOMANYREFS = 109; /// Too many references: cannot splice -pub const ETIMEDOUT = 110; /// Connection timed out -pub const ECONNREFUSED = 111; /// Connection refused -pub const EHOSTDOWN = 112; /// Host is down -pub const EHOSTUNREACH = 113; /// No route to host -pub const EALREADY = 114; /// Operation already in progress -pub const EINPROGRESS = 115; /// Operation now in progress -pub const ESTALE = 116; /// Stale NFS file handle -pub const EUCLEAN = 117; /// Structure needs cleaning -pub const ENOTNAM = 118; /// Not a XENIX named type file -pub const ENAVAIL = 119; /// No XENIX semaphores available -pub const EISNAM = 120; /// Is a named type file -pub const EREMOTEIO = 121; /// Remote I/O error -pub const EDQUOT = 122; /// Quota exceeded - -pub const ENOMEDIUM = 123; /// No medium found -pub const EMEDIUMTYPE = 124; /// Wrong medium type - -// nameserver query return codes -pub const ENSROK = 0; /// DNS server returned answer with no data -pub const ENSRNODATA = 160; /// DNS server returned answer with no data -pub const ENSRFORMERR = 161; /// DNS server claims query was misformatted -pub const ENSRSERVFAIL = 162; /// DNS server returned general failure -pub const ENSRNOTFOUND = 163; /// Domain name not found -pub const ENSRNOTIMP = 164; /// DNS server does not implement requested operation -pub const ENSRREFUSED = 165; /// DNS server refused query -pub const ENSRBADQUERY = 166; /// Misformatted DNS query -pub const ENSRBADNAME = 167; /// Misformatted domain name -pub const ENSRBADFAMILY = 168; /// Unsupported address family -pub const ENSRBADRESP = 169; /// Misformatted DNS reply -pub const ENSRCONNREFUSED = 170; /// Could not contact DNS servers -pub const ENSRTIMEOUT = 171; /// Timeout while contacting DNS servers -pub const ENSROF = 172; /// End of file -pub const ENSRFILE = 173; /// Error reading file -pub const ENSRNOMEM = 174; /// Out of memory -pub const ENSRDESTRUCTION = 175; /// Application terminated lookup -pub const ENSRQUERYDOMAINTOOLONG = 176; /// Domain name is too long -pub const ENSRCNAMELOOP = 177; /// Domain name is too long diff --git a/std/os/linux_i386.zig b/std/os/linux_i386.zig deleted file mode 100644 index 353461562b..0000000000 --- a/std/os/linux_i386.zig +++ /dev/null @@ -1,504 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -pub const SYS_restart_syscall = 0; -pub const SYS_exit = 1; -pub const SYS_fork = 2; -pub const SYS_read = 3; -pub const SYS_write = 4; -pub const SYS_open = 5; -pub const SYS_close = 6; -pub const SYS_waitpid = 7; -pub const SYS_creat = 8; -pub const SYS_link = 9; -pub const SYS_unlink = 10; -pub const SYS_execve = 11; -pub const SYS_chdir = 12; -pub const SYS_time = 13; -pub const SYS_mknod = 14; -pub const SYS_chmod = 15; -pub const SYS_lchown = 16; -pub const SYS_break = 17; -pub const SYS_oldstat = 18; -pub const SYS_lseek = 19; -pub const SYS_getpid = 20; -pub const SYS_mount = 21; -pub const SYS_umount = 22; -pub const SYS_setuid = 23; -pub const SYS_getuid = 24; -pub const SYS_stime = 25; -pub const SYS_ptrace = 26; -pub const SYS_alarm = 27; -pub const SYS_oldfstat = 28; -pub const SYS_pause = 29; -pub const SYS_utime = 30; -pub const SYS_stty = 31; -pub const SYS_gtty = 32; -pub const SYS_access = 33; -pub const SYS_nice = 34; -pub const SYS_ftime = 35; -pub const SYS_sync = 36; -pub const SYS_kill = 37; -pub const SYS_rename = 38; -pub const SYS_mkdir = 39; -pub const SYS_rmdir = 40; -pub const SYS_dup = 41; -pub const SYS_pipe = 42; -pub const SYS_times = 43; -pub const SYS_prof = 44; -pub const SYS_brk = 45; -pub const SYS_setgid = 46; -pub const SYS_getgid = 47; -pub const SYS_signal = 48; -pub const SYS_geteuid = 49; -pub const SYS_getegid = 50; -pub const SYS_acct = 51; -pub const SYS_umount2 = 52; -pub const SYS_lock = 53; -pub const SYS_ioctl = 54; -pub const SYS_fcntl = 55; -pub const SYS_mpx = 56; -pub const SYS_setpgid = 57; -pub const SYS_ulimit = 58; -pub const SYS_oldolduname = 59; -pub const SYS_umask = 60; -pub const SYS_chroot = 61; -pub const SYS_ustat = 62; -pub const SYS_dup2 = 63; -pub const SYS_getppid = 64; -pub const SYS_getpgrp = 65; -pub const SYS_setsid = 66; -pub const SYS_sigaction = 67; -pub const SYS_sgetmask = 68; -pub const SYS_ssetmask = 69; -pub const SYS_setreuid = 70; -pub const SYS_setregid = 71; -pub const SYS_sigsuspend = 72; -pub const SYS_sigpending = 73; -pub const SYS_sethostname = 74; -pub const SYS_setrlimit = 75; -pub const SYS_getrlimit = 76; -pub const SYS_getrusage = 77; -pub const SYS_gettimeofday = 78; -pub const SYS_settimeofday = 79; -pub const SYS_getgroups = 80; -pub const SYS_setgroups = 81; -pub const SYS_select = 82; -pub const SYS_symlink = 83; -pub const SYS_oldlstat = 84; -pub const SYS_readlink = 85; -pub const SYS_uselib = 86; -pub const SYS_swapon = 87; -pub const SYS_reboot = 88; -pub const SYS_readdir = 89; -pub const SYS_mmap = 90; -pub const SYS_munmap = 91; -pub const SYS_truncate = 92; -pub const SYS_ftruncate = 93; -pub const SYS_fchmod = 94; -pub const SYS_fchown = 95; -pub const SYS_getpriority = 96; -pub const SYS_setpriority = 97; -pub const SYS_profil = 98; -pub const SYS_statfs = 99; -pub const SYS_fstatfs = 100; -pub const SYS_ioperm = 101; -pub const SYS_socketcall = 102; -pub const SYS_syslog = 103; -pub const SYS_setitimer = 104; -pub const SYS_getitimer = 105; -pub const SYS_stat = 106; -pub const SYS_lstat = 107; -pub const SYS_fstat = 108; -pub const SYS_olduname = 109; -pub const SYS_iopl = 110; -pub const SYS_vhangup = 111; -pub const SYS_idle = 112; -pub const SYS_vm86old = 113; -pub const SYS_wait4 = 114; -pub const SYS_swapoff = 115; -pub const SYS_sysinfo = 116; -pub const SYS_ipc = 117; -pub const SYS_fsync = 118; -pub const SYS_sigreturn = 119; -pub const SYS_clone = 120; -pub const SYS_setdomainname = 121; -pub const SYS_uname = 122; -pub const SYS_modify_ldt = 123; -pub const SYS_adjtimex = 124; -pub const SYS_mprotect = 125; -pub const SYS_sigprocmask = 126; -pub const SYS_create_module = 127; -pub const SYS_init_module = 128; -pub const SYS_delete_module = 129; -pub const SYS_get_kernel_syms = 130; -pub const SYS_quotactl = 131; -pub const SYS_getpgid = 132; -pub const SYS_fchdir = 133; -pub const SYS_bdflush = 134; -pub const SYS_sysfs = 135; -pub const SYS_personality = 136; -pub const SYS_afs_syscall = 137; -pub const SYS_setfsuid = 138; -pub const SYS_setfsgid = 139; -pub const SYS__llseek = 140; -pub const SYS_getdents = 141; -pub const SYS__newselect = 142; -pub const SYS_flock = 143; -pub const SYS_msync = 144; -pub const SYS_readv = 145; -pub const SYS_writev = 146; -pub const SYS_getsid = 147; -pub const SYS_fdatasync = 148; -pub const SYS__sysctl = 149; -pub const SYS_mlock = 150; -pub const SYS_munlock = 151; -pub const SYS_mlockall = 152; -pub const SYS_munlockall = 153; -pub const SYS_sched_setparam = 154; -pub const SYS_sched_getparam = 155; -pub const SYS_sched_setscheduler = 156; -pub const SYS_sched_getscheduler = 157; -pub const SYS_sched_yield = 158; -pub const SYS_sched_get_priority_max = 159; -pub const SYS_sched_get_priority_min = 160; -pub const SYS_sched_rr_get_interval = 161; -pub const SYS_nanosleep = 162; -pub const SYS_mremap = 163; -pub const SYS_setresuid = 164; -pub const SYS_getresuid = 165; -pub const SYS_vm86 = 166; -pub const SYS_query_module = 167; -pub const SYS_poll = 168; -pub const SYS_nfsservctl = 169; -pub const SYS_setresgid = 170; -pub const SYS_getresgid = 171; -pub const SYS_prctl = 172; -pub const SYS_rt_sigreturn = 173; -pub const SYS_rt_sigaction = 174; -pub const SYS_rt_sigprocmask = 175; -pub const SYS_rt_sigpending = 176; -pub const SYS_rt_sigtimedwait = 177; -pub const SYS_rt_sigqueueinfo = 178; -pub const SYS_rt_sigsuspend = 179; -pub const SYS_pread64 = 180; -pub const SYS_pwrite64 = 181; -pub const SYS_chown = 182; -pub const SYS_getcwd = 183; -pub const SYS_capget = 184; -pub const SYS_capset = 185; -pub const SYS_sigaltstack = 186; -pub const SYS_sendfile = 187; -pub const SYS_getpmsg = 188; -pub const SYS_putpmsg = 189; -pub const SYS_vfork = 190; -pub const SYS_ugetrlimit = 191; -pub const SYS_mmap2 = 192; -pub const SYS_truncate64 = 193; -pub const SYS_ftruncate64 = 194; -pub const SYS_stat64 = 195; -pub const SYS_lstat64 = 196; -pub const SYS_fstat64 = 197; -pub const SYS_lchown32 = 198; -pub const SYS_getuid32 = 199; -pub const SYS_getgid32 = 200; -pub const SYS_geteuid32 = 201; -pub const SYS_getegid32 = 202; -pub const SYS_setreuid32 = 203; -pub const SYS_setregid32 = 204; -pub const SYS_getgroups32 = 205; -pub const SYS_setgroups32 = 206; -pub const SYS_fchown32 = 207; -pub const SYS_setresuid32 = 208; -pub const SYS_getresuid32 = 209; -pub const SYS_setresgid32 = 210; -pub const SYS_getresgid32 = 211; -pub const SYS_chown32 = 212; -pub const SYS_setuid32 = 213; -pub const SYS_setgid32 = 214; -pub const SYS_setfsuid32 = 215; -pub const SYS_setfsgid32 = 216; -pub const SYS_pivot_root = 217; -pub const SYS_mincore = 218; -pub const SYS_madvise = 219; -pub const SYS_madvise1 = 219; -pub const SYS_getdents64 = 220; -pub const SYS_fcntl64 = 221; -pub const SYS_gettid = 224; -pub const SYS_readahead = 225; -pub const SYS_setxattr = 226; -pub const SYS_lsetxattr = 227; -pub const SYS_fsetxattr = 228; -pub const SYS_getxattr = 229; -pub const SYS_lgetxattr = 230; -pub const SYS_fgetxattr = 231; -pub const SYS_listxattr = 232; -pub const SYS_llistxattr = 233; -pub const SYS_flistxattr = 234; -pub const SYS_removexattr = 235; -pub const SYS_lremovexattr = 236; -pub const SYS_fremovexattr = 237; -pub const SYS_tkill = 238; -pub const SYS_sendfile64 = 239; -pub const SYS_futex = 240; -pub const SYS_sched_setaffinity = 241; -pub const SYS_sched_getaffinity = 242; -pub const SYS_set_thread_area = 243; -pub const SYS_get_thread_area = 244; -pub const SYS_io_setup = 245; -pub const SYS_io_destroy = 246; -pub const SYS_io_getevents = 247; -pub const SYS_io_submit = 248; -pub const SYS_io_cancel = 249; -pub const SYS_fadvise64 = 250; -pub const SYS_exit_group = 252; -pub const SYS_lookup_dcookie = 253; -pub const SYS_epoll_create = 254; -pub const SYS_epoll_ctl = 255; -pub const SYS_epoll_wait = 256; -pub const SYS_remap_file_pages = 257; -pub const SYS_set_tid_address = 258; -pub const SYS_timer_create = 259; -pub const SYS_timer_settime = SYS_timer_create+1; -pub const SYS_timer_gettime = SYS_timer_create+2; -pub const SYS_timer_getoverrun = SYS_timer_create+3; -pub const SYS_timer_delete = SYS_timer_create+4; -pub const SYS_clock_settime = SYS_timer_create+5; -pub const SYS_clock_gettime = SYS_timer_create+6; -pub const SYS_clock_getres = SYS_timer_create+7; -pub const SYS_clock_nanosleep = SYS_timer_create+8; -pub const SYS_statfs64 = 268; -pub const SYS_fstatfs64 = 269; -pub const SYS_tgkill = 270; -pub const SYS_utimes = 271; -pub const SYS_fadvise64_64 = 272; -pub const SYS_vserver = 273; -pub const SYS_mbind = 274; -pub const SYS_get_mempolicy = 275; -pub const SYS_set_mempolicy = 276; -pub const SYS_mq_open = 277; -pub const SYS_mq_unlink = SYS_mq_open+1; -pub const SYS_mq_timedsend = SYS_mq_open+2; -pub const SYS_mq_timedreceive = SYS_mq_open+3; -pub const SYS_mq_notify = SYS_mq_open+4; -pub const SYS_mq_getsetattr = SYS_mq_open+5; -pub const SYS_kexec_load = 283; -pub const SYS_waitid = 284; -pub const SYS_add_key = 286; -pub const SYS_request_key = 287; -pub const SYS_keyctl = 288; -pub const SYS_ioprio_set = 289; -pub const SYS_ioprio_get = 290; -pub const SYS_inotify_init = 291; -pub const SYS_inotify_add_watch = 292; -pub const SYS_inotify_rm_watch = 293; -pub const SYS_migrate_pages = 294; -pub const SYS_openat = 295; -pub const SYS_mkdirat = 296; -pub const SYS_mknodat = 297; -pub const SYS_fchownat = 298; -pub const SYS_futimesat = 299; -pub const SYS_fstatat64 = 300; -pub const SYS_unlinkat = 301; -pub const SYS_renameat = 302; -pub const SYS_linkat = 303; -pub const SYS_symlinkat = 304; -pub const SYS_readlinkat = 305; -pub const SYS_fchmodat = 306; -pub const SYS_faccessat = 307; -pub const SYS_pselect6 = 308; -pub const SYS_ppoll = 309; -pub const SYS_unshare = 310; -pub const SYS_set_robust_list = 311; -pub const SYS_get_robust_list = 312; -pub const SYS_splice = 313; -pub const SYS_sync_file_range = 314; -pub const SYS_tee = 315; -pub const SYS_vmsplice = 316; -pub const SYS_move_pages = 317; -pub const SYS_getcpu = 318; -pub const SYS_epoll_pwait = 319; -pub const SYS_utimensat = 320; -pub const SYS_signalfd = 321; -pub const SYS_timerfd_create = 322; -pub const SYS_eventfd = 323; -pub const SYS_fallocate = 324; -pub const SYS_timerfd_settime = 325; -pub const SYS_timerfd_gettime = 326; -pub const SYS_signalfd4 = 327; -pub const SYS_eventfd2 = 328; -pub const SYS_epoll_create1 = 329; -pub const SYS_dup3 = 330; -pub const SYS_pipe2 = 331; -pub const SYS_inotify_init1 = 332; -pub const SYS_preadv = 333; -pub const SYS_pwritev = 334; -pub const SYS_rt_tgsigqueueinfo = 335; -pub const SYS_perf_event_open = 336; -pub const SYS_recvmmsg = 337; -pub const SYS_fanotify_init = 338; -pub const SYS_fanotify_mark = 339; -pub const SYS_prlimit64 = 340; -pub const SYS_name_to_handle_at = 341; -pub const SYS_open_by_handle_at = 342; -pub const SYS_clock_adjtime = 343; -pub const SYS_syncfs = 344; -pub const SYS_sendmmsg = 345; -pub const SYS_setns = 346; -pub const SYS_process_vm_readv = 347; -pub const SYS_process_vm_writev = 348; -pub const SYS_kcmp = 349; -pub const SYS_finit_module = 350; -pub const SYS_sched_setattr = 351; -pub const SYS_sched_getattr = 352; -pub const SYS_renameat2 = 353; -pub const SYS_seccomp = 354; -pub const SYS_getrandom = 355; -pub const SYS_memfd_create = 356; -pub const SYS_bpf = 357; -pub const SYS_execveat = 358; -pub const SYS_socket = 359; -pub const SYS_socketpair = 360; -pub const SYS_bind = 361; -pub const SYS_connect = 362; -pub const SYS_listen = 363; -pub const SYS_accept4 = 364; -pub const SYS_getsockopt = 365; -pub const SYS_setsockopt = 366; -pub const SYS_getsockname = 367; -pub const SYS_getpeername = 368; -pub const SYS_sendto = 369; -pub const SYS_sendmsg = 370; -pub const SYS_recvfrom = 371; -pub const SYS_recvmsg = 372; -pub const SYS_shutdown = 373; -pub const SYS_userfaultfd = 374; -pub const SYS_membarrier = 375; -pub const SYS_mlock2 = 376; - - -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 = 0o100000; -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 = 12; -pub const F_SETLK = 13; -pub const F_SETLKW = 14; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -pub inline fn syscall0(number: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number)) -} - -pub inline fn syscall1(number: usize, arg1: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1)) -} - -pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2)) -} - -pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3)) -} - -pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4)) -} - -pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize) -> usize -{ - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5)) -} - -pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize, arg6: usize) -> usize -{ - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5), - [arg6] "{ebp}" (arg6)) -} - -pub nakedcc fn restore() void { - asm volatile ( - \\popl %%eax - \\movl $119, %%eax - \\int $0x80 - : - : - : "rcx", "r11") -} - -pub nakedcc fn restore_rt() void { - asm volatile ("int $0x80" - : - : [number] "{eax}" (usize(SYS_rt_sigreturn)) - : "rcx", "r11") -} diff --git a/std/os/linux_random.zig b/std/os/linux_random.zig deleted file mode 100644 index 0fb10d1ee9..0000000000 --- a/std/os/linux_random.zig +++ /dev/null @@ -1,246 +0,0 @@ -const std = @import("../index.zig"); -const builtin = @import("builtin"); -const assert = std.debug.assert; -const linux = std.os.linux; -const math = std.math; -const mem = std.mem; -const os = std.os; - -use @import("linux_errno.zig"); - -const arch = switch (builtin.arch) { - builtin.Arch.x86_64 => @import("linux_x86_64.zig"), - builtin.Arch.i386 => @import("linux_i386.zig"), - else => @compileError("unsupported arch"), -}; - -const Method = enum { - Syscall, - Sysctl, - Urandom, -}; - -const Callback = fn(&i32, []u8) usize; - -const Context = struct { - syscall: Callback, - sysctl: Callback, - urandom: Callback, -}; - -pub fn getRandomBytes(buf: []u8) usize { - const ctx = Context { - .syscall = syscall, - .sysctl = sysctl, - .urandom = urandom, - }; - return withContext(ctx, buf); -} - -fn withContext(comptime ctx: Context, buf: []u8) usize { - if (buf.len == 0) return 0; - - var fd: i32 = -1; - defer if (fd != -1) { - const _ = linux.close(fd); // Ignore errors, can't do anything sensible. - }; - - // TODO(bnoordhuis) Remember the method across invocations so we don't make - // unnecessary system calls that are going to fail with ENOSYS anyway. - var method = Method.Syscall; - var i: usize = 0; - while (i < buf.len) { - const rc = switch (method) { - Method.Syscall => ctx.syscall(&fd, buf[i..]), - Method.Sysctl => ctx.sysctl(&fd, buf[i..]), - Method.Urandom => ctx.urandom(&fd, buf[i..]), - }; - if (rc == 0) return usize(-EIO); // Can't really happen. - if (!isErr(rc)) { - i += rc; - continue; - } - if (rc == usize(-EINTR)) continue; - if (rc == usize(-ENOSYS) and method == Method.Syscall) { - method = Method.Urandom; - continue; - } - if (method == Method.Urandom) { - method = Method.Sysctl; - continue; - } - return rc; // Unexpected error. - } - - return i; -} - -fn syscall(_: &i32, buf: []u8) usize { - return arch.syscall3(arch.SYS_getrandom, @ptrToInt(&buf[0]), buf.len, 0); -} - -// Note: reads only 14 bytes at a time. -fn sysctl(_: &i32, buf: []u8) usize { - const __sysctl_args = extern struct { - name: &c_int, - nlen: c_int, - oldval: &u8, - oldlenp: &usize, - newval: ?&u8, - newlen: usize, - unused: [4]usize, - }; - - var name = [3]c_int { 1, 40, 6 }; // { CTL_KERN, KERN_RANDOM, RANDOM_UUID } - var uuid: [16]u8 = undefined; - - const expected: usize = @sizeOf(@typeOf(uuid)); - var len = expected; - - var args = __sysctl_args { - .name = &name[0], - .nlen = c_int(name.len), - .oldval = &uuid[0], - .oldlenp = &len, - .newval = null, - .newlen = 0, - .unused = []usize {0} ** 4, - }; - - const rc = arch.syscall1(arch.SYS__sysctl, @ptrToInt(&args)); - if (rc != 0) return rc; - if (len != expected) return 0; // Can't happen. - - // uuid[] is now a type 4 UUID; bytes 6 and 8 (counting from zero) - // contain 4 and 5 bits of entropy, respectively. For ease of use, - // we skip those and only use 14 of the 16 bytes. - uuid[6] = uuid[14]; - uuid[8] = uuid[15]; - - const n = math.min(buf.len, usize(14)); - @memcpy(&buf[0], &uuid[0], n); - return n; -} - -fn urandom(fd: &i32, buf: []u8) usize { - if (*fd == -1) { - const flags = linux.O_CLOEXEC|linux.O_RDONLY; - const rc = linux.open(c"/dev/urandom", flags, 0); - if (isErr(rc)) return rc; - *fd = i32(rc); - } - // read() doesn't like reads > INT_MAX. - const n = math.min(buf.len, usize(0x7FFFFFFF)); - return linux.read(*fd, &buf[0], n); -} - -fn isErr(rc: usize) bool { - return rc > usize(-4096); -} - -test "os.linux.getRandomBytes" { - try check(42, getRandomBytesTrampoline); -} - -test "os.linux.getRandomBytes syscall" { - try check(42, syscall); -} - -test "os.linux.getRandomBytes sysctl" { - try check(14, sysctl); -} - -test "os.linux.getRandomBytes /dev/urandom" { - try check(42, urandom); -} - -test "os.linux.getRandomBytes state machine" { - const ctx = Context { - .syscall = fortytwo, - .urandom = fail, - .sysctl = fail, - }; - var buf = []u8 {0}; - assert(1 == withContext(ctx, buf[0..])); - assert(42 == buf[0]); -} - -test "os.linux.getRandomBytes no-syscall state machine" { - const ctx = Context { - .syscall = enosys, - .urandom = fortytwo, - .sysctl = fail, - }; - var buf = []u8 {0}; - assert(1 == withContext(ctx, buf[0..])); - assert(42 == buf[0]); -} - -test "os.linux.getRandomBytes no-urandom state machine" { - const ctx = Context { - .syscall = enosys, - .urandom = einval, - .sysctl = fortytwo, - }; - var buf = []u8 {0}; - assert(1 == withContext(ctx, buf[0..])); - assert(42 == buf[0]); -} - -test "os.linux.getRandomBytes no-sysctl state machine" { - const ctx = Context { - .syscall = enosys, - .urandom = einval, - .sysctl = einval, - }; - var buf = []u8 {0}; - assert(usize(-EINVAL) == withContext(ctx, buf[0..])); - assert(0 == buf[0]); -} - -fn einval(_: &i32, buf: []u8) usize { - return usize(-EINVAL); -} - -fn enosys(_: &i32, buf: []u8) usize { - return usize(-ENOSYS); -} - -fn fail(_: &i32, buf: []u8) usize { - os.abort(); -} - -fn fortytwo(_: &i32, buf: []u8) usize { - assert(buf.len == 1); - buf[0] = 42; - return 1; -} - -fn check(comptime N: usize, cb: Callback) %void { - if (builtin.os == builtin.Os.linux) { - var fd: i32 = -1; - defer if (fd != -1) { - const _ = linux.close(fd); // Ignore errors, can't do anything sensible. - }; - - var bufs = [3][N]u8 { - []u8 {0} ** N, - []u8 {0} ** N, - []u8 {0} ** N, - }; - - for (bufs) |*buf| { - const err = cb(&fd, (*buf)[0..]); - assert(err == N); - } - - for (bufs) |*a| - for (bufs) |*b| - if (a != b) - assert(!mem.eql(u8, *a, *b)); - } -} - -fn getRandomBytesTrampoline(_: &i32, buf: []u8) usize { - return getRandomBytes(buf); -} diff --git a/std/os/linux_test.zig b/std/os/linux_test.zig deleted file mode 100644 index 265d0a17f9..0000000000 --- a/std/os/linux_test.zig +++ /dev/null @@ -1,38 +0,0 @@ -const std = @import("std"); -const linux = std.os.linux; -const assert = std.debug.assert; - -test "timer" { - const epoll_fd = linux.epoll_create(); - var err = linux.getErrno(epoll_fd); - assert(err == 0); - - const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); - assert(linux.getErrno(timer_fd) == 0); - - const time_interval = linux.timespec { - .tv_sec = 0, - .tv_nsec = 2000000 - }; - - const new_time = linux.itimerspec { - .it_interval = time_interval, - .it_value = time_interval - }; - - err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null); - assert(err == 0); - - var event = linux.epoll_event { - .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET, - .data = 0 - }; - - err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event); - assert(err == 0); - - const events_one: linux.epoll_event = undefined; - var events = []linux.epoll_event{events_one} ** 8; - - err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1); -} diff --git a/std/os/linux_x86_64.zig b/std/os/linux_x86_64.zig deleted file mode 100644 index 3706633745..0000000000 --- a/std/os/linux_x86_64.zig +++ /dev/null @@ -1,489 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -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_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 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 fn syscall0(number: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number) - : "rcx", "r11"); -} - -pub fn syscall1(number: usize, arg1: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1) - : "rcx", "r11"); -} - -pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2) - : "rcx", "r11"); -} - -pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3) - : "rcx", "r11"); -} - -pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4) - : "rcx", "r11"); -} - -pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5) - : "rcx", "r11"); -} - -pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, - arg5: usize, arg6: usize) usize -{ - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5), - [arg6] "{r9}" (arg6) - : "rcx", "r11"); -} - -pub nakedcc fn restore_rt() void { - return asm volatile ("syscall" - : - : [number] "{rax}" (usize(SYS_rt_sigreturn)) - : "rcx", "r11"); -} - - -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, -}; - -/// 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, -}; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 3267ea7435..92066d7e0e 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,12 @@ const tests = @import("tests.zig"); pub fn addCases(cases: &tests.CompileErrorContext) void { + cases.add("cast negative integer literal to usize", + \\export fn entry() void { + \\ const x = usize(-10); + \\} + , ".tmp_source.zig:2:21: error: cannot cast negative value -10 to unsigned integer type 'usize'"); + cases.add("use invalid number literal as array index", \\var v = 25; \\export fn entry() void { -- cgit v1.2.3 From 0090c2d70b89cc6b5ab02bdc02aa49c4caf71a5a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 7 Feb 2018 20:38:49 +0100 Subject: DRY 'is slice?' conditionals in parser (#750) --- src/analyze.cpp | 6 +----- src/ir.cpp | 8 ++------ 2 files changed, 3 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/analyze.cpp b/src/analyze.cpp index 2812c9d79e..4c982c160c 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3380,11 +3380,7 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry * } // slice const - if (expected_type->id == TypeTableEntryIdStruct && - actual_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && - actual_type->data.structure.is_slice) - { + if (is_slice(expected_type) && is_slice(actual_type)) { TypeTableEntry *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry; TypeTableEntry *expected_ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry; if ((!actual_ptr_type->data.pointer.is_const || expected_ptr_type->data.pointer.is_const) && diff --git a/src/ir.cpp b/src/ir.cpp index 1dc717286a..ae8ef00f2f 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6369,10 +6369,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira, } // implicit [N]T to []const T - if (expected_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && - actual_type->id == TypeTableEntryIdArray) - { + if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdArray) { TypeTableEntry *ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry; assert(ptr_type->id == TypeTableEntryIdPointer); @@ -6384,8 +6381,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira, } // implicit &const [N]T to []const T - if (expected_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && + if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdPointer && actual_type->data.pointer.is_const && actual_type->data.pointer.child_type->id == TypeTableEntryIdArray) -- cgit v1.2.3 From c88e6e8aeec1b9a7d6888f298fef7949770c288b Mon Sep 17 00:00:00 2001 From: Jeff Fowler Date: Wed, 7 Feb 2018 23:45:20 +0100 Subject: improve behavior of `zig build` (#754) See #748 --- src/main.cpp | 33 +++++++++++++++++++++++---------- std/special/build_runner.zig | 1 + 2 files changed, 24 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index e3ef62be07..3df16e62b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -339,6 +339,7 @@ int main(int argc, char **argv) { const char *zig_exe_path = arg0; const char *build_file = "build.zig"; bool asked_for_help = false; + bool asked_to_init = false; init_all_targets(); @@ -350,6 +351,9 @@ int main(int argc, char **argv) { if (strcmp(argv[i], "--help") == 0) { asked_for_help = true; args.append(argv[i]); + } else if (strcmp(argv[i], "--init") == 0) { + asked_to_init = true; + args.append(argv[i]); } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { build_file = argv[i + 1]; i += 1; @@ -414,6 +418,7 @@ int main(int argc, char **argv) { "\n" "General Options:\n" " --help Print this help and exit\n" + " --init Generate a build.zig template\n" " --build-file [file] Override path to build.zig\n" " --cache-dir [path] Override path to cache directory\n" " --verbose Print commands before executing them\n" @@ -426,7 +431,6 @@ int main(int argc, char **argv) { " --prefix [path] Override default install prefix\n" "\n" "Project-specific options become available when the build file is found.\n" - "Run this command with no options to generate a build.zig template.\n" "\n" "Advanced Options:\n" " --build-file [file] Override path to build.zig\n" @@ -439,17 +443,26 @@ int main(int argc, char **argv) { " --verbose-cimport Enable compiler debug output for C imports\n" "\n" , zig_exe_path); - return 0; - } - Buf *build_template_path = buf_alloc(); - os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); + return EXIT_SUCCESS; + } else if (asked_to_init) { + Buf *build_template_path = buf_alloc(); + os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); - if ((err = os_copy_file(build_template_path, &build_file_abs))) { - fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); - } else { - fprintf(stderr, "Wrote build.zig template\n"); + if ((err = os_copy_file(build_template_path, &build_file_abs))) { + fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); + } else { + fprintf(stderr, "Wrote build.zig template\n"); + } + return EXIT_SUCCESS; } - return 1; + + fprintf(stderr, + "No 'build.zig' file found.\n" + "Initialize a 'build.zig' template file with `zig build --init`,\n" + "or build an executable directly with `zig build-exe $FILENAME.zig`.\n" + "See: `zig build --help` or `zig help` for more options.\n" + ); + return EXIT_FAILURE; } PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index e1648276aa..c06b28167c 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -149,6 +149,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) \\ \\General Options: \\ --help Print this help and exit + \\ --init Generate a build.zig template \\ --verbose Print commands before executing them \\ --prefix [path] Override default install prefix \\ --search-prefix [path] Add a path to look for binaries, libraries, headers -- cgit v1.2.3