From 9fb4d1fd6c521e1bc3b656315b9c693a9e8aa715 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 7 Sep 2017 23:10:23 -0400 Subject: [PATCH] std: os.ChildProcess knows when its child died using signal handlers --- src/ir.cpp | 22 ++++ std/build.zig | 4 +- std/linked_list.zig | 87 ++++++-------- std/mem.zig | 15 ++- std/os/child_process.zig | 252 +++++++++++++++++++++++++++++---------- std/os/linux.zig | 91 ++++++++++++-- std/os/linux_i386.zig | 17 +++ std/os/linux_x86_64.zig | 8 ++ test/cases/array.zig | 10 ++ test/tests.zig | 16 +-- 10 files changed, 386 insertions(+), 136 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 9e15bb1827..a30851d010 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11513,6 +11513,28 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru buf_ptr(&child_type->name), buf_ptr(field_name))); return ira->codegen->builtin_types.entry_invalid; } + } else if (child_type->id == TypeTableEntryIdArray) { + if (buf_eql_str(field_name, "child")) { + bool ptr_is_const = true; + bool ptr_is_volatile = false; + return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, + create_const_type(ira->codegen, child_type->data.array.child_type), + ira->codegen->builtin_types.entry_type, + ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); + } else if (buf_eql_str(field_name, "len")) { + bool ptr_is_const = true; + bool ptr_is_volatile = false; + return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, + create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int, + child_type->data.array.len, false), + ira->codegen->builtin_types.entry_num_lit_int, + ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile); + } else { + ir_add_error(ira, &field_ptr_instruction->base, + buf_sprintf("type '%s' has no member called '%s'", + buf_ptr(&child_type->name), buf_ptr(field_name))); + return ira->codegen->builtin_types.entry_invalid; + } } else { ir_add_error(ira, &field_ptr_instruction->base, buf_sprintf("type '%s' does not support field access", buf_ptr(&child_type->name))); diff --git a/std/build.zig b/std/build.zig index a42434da50..cddf674085 100644 --- a/std/build.zig +++ b/std/build.zig @@ -545,7 +545,7 @@ pub const Builder = struct { } var child = os.ChildProcess.spawn(exe_path, args, cwd, env_map, - StdIo.Inherit, StdIo.Inherit, StdIo.Inherit, self.allocator) %% |err| + StdIo.Inherit, StdIo.Inherit, StdIo.Inherit, null, self.allocator) %% |err| { %%io.stderr.printf("Unable to spawn {}: {}\n", exe_path, @errorName(err)); return err; @@ -556,7 +556,7 @@ pub const Builder = struct { return err; }; switch (term) { - Term.Clean => |code| { + Term.Exited => |code| { if (code != 0) { %%io.stderr.printf("Process {} exited with error code {}\n", exe_path, code); return error.UncleanExit; diff --git a/std/linked_list.zig b/std/linked_list.zig index 6811f5d99f..a1ff526105 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -1,7 +1,6 @@ const debug = @import("debug.zig"); const assert = debug.assert; const mem = @import("mem.zig"); -const Allocator = mem.Allocator; /// Generic doubly linked list. pub fn LinkedList(comptime T: type) -> type { @@ -13,26 +12,29 @@ pub fn LinkedList(comptime T: type) -> type { prev: ?&Node, next: ?&Node, data: T, + + pub fn init(data: &const T) -> Node { + Node { + .data = *data, + .prev = null, + .next = null, + } + } }; first: ?&Node, last: ?&Node, len: usize, - allocator: &Allocator, /// Initialize a linked list. /// - /// Arguments: - /// allocator: Dynamic memory allocator. - /// /// Returns: /// An empty linked list. - pub fn init(allocator: &Allocator) -> Self { + pub fn init() -> Self { Self { .first = null, .last = null, .len = 0, - .allocator = allocator, } } @@ -155,55 +157,38 @@ pub fn LinkedList(comptime T: type) -> type { return first; } - /// Allocate a new node. - /// - /// Returns: - /// A pointer to the new node. - pub fn allocateNode(list: &Self) -> %&Node { - list.allocator.create(Node) - } - - /// Deallocate a node. - /// - /// Arguments: - /// node: Pointer to the node to deallocate. - pub fn destroyNode(list: &Self, node: &Node) { - list.allocator.destroy(node); - } - - /// Allocate and initialize a node and its data. - /// - /// Arguments: - /// data: The data to put inside the node. - /// - /// Returns: - /// A pointer to the new node. - pub fn createNode(list: &Self, data: &const T) -> %&Node { - var node = %return list.allocateNode(); - *node = Node { - .prev = null, - .next = null, - .data = *data, - }; - return node; - } } } -test "basic linked list test" { - var list = LinkedList(u32).init(&debug.global_allocator); +pub fn testAllocateNode(comptime T: type, list: &LinkedList(T), allocator: &mem.Allocator) -> %&LinkedList(T).Node { + allocator.create(LinkedList(T).Node) +} - var one = %%list.createNode(1); - var two = %%list.createNode(2); - var three = %%list.createNode(3); - var four = %%list.createNode(4); - var five = %%list.createNode(5); +pub fn testDestroyNode(comptime T: type, list: &LinkedList(T), node: &LinkedList(T).Node, allocator: &mem.Allocator) { + allocator.destroy(node); +} + +pub fn testCreateNode(comptime T: type, list: &LinkedList(T), data: &const T, allocator: &mem.Allocator) -> %&LinkedList(T).Node { + var node = %return testAllocateNode(T, list, allocator); + *node = LinkedList(T).Node.init(data); + return node; +} + +test "basic linked list test" { + const allocator = &debug.global_allocator; + var list = LinkedList(u32).init(); + + var one = %%testCreateNode(u32, &list, 1, allocator); + var two = %%testCreateNode(u32, &list, 2, allocator); + var three = %%testCreateNode(u32, &list, 3, allocator); + var four = %%testCreateNode(u32, &list, 4, allocator); + var five = %%testCreateNode(u32, &list, 5, allocator); defer { - list.destroyNode(one); - list.destroyNode(two); - list.destroyNode(three); - list.destroyNode(four); - list.destroyNode(five); + testDestroyNode(u32, &list, one, allocator); + testDestroyNode(u32, &list, two, allocator); + testDestroyNode(u32, &list, three, allocator); + testDestroyNode(u32, &list, four, allocator); + testDestroyNode(u32, &list, five, allocator); } list.append(two); // {2} diff --git a/std/mem.zig b/std/mem.zig index 671ea8de87..86c7a5a445 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -49,11 +49,16 @@ pub const Allocator = struct { } fn free(self: &Allocator, memory: var) { - const const_slice = ([]const u8)(memory); - if (memory.len == 0) - return; - const ptr = @intToPtr(&u8, @ptrToInt(const_slice.ptr)); - self.freeFn(self, ptr); + const ptr = if (@typeId(@typeOf(memory)) == builtin.TypeId.Pointer) { + memory + } else { + const const_slice = ([]const u8)(memory); + if (memory.len == 0) + return; + const_slice.ptr + }; + const non_const_ptr = @intToPtr(&u8, @ptrToInt(ptr)); + self.freeFn(self, non_const_ptr); } }; diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 149c3aca12..c02ffe8a7a 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -8,20 +8,31 @@ const assert = debug.assert; const BufMap = @import("../buf_map.zig").BufMap; const builtin = @import("builtin"); const Os = builtin.Os; +const LinkedList = @import("../linked_list.zig").LinkedList; error PermissionDenied; error ProcessNotFound; +var children_nodes = LinkedList(&ChildProcess).init(); + pub const ChildProcess = struct { pid: i32, - err_pipe: [2]i32, - stdin: ?io.OutStream, - stdout: ?io.InStream, - stderr: ?io.InStream, + err_pipe: [2]i32, + llnode: LinkedList(&ChildProcess).Node, + allocator: &mem.Allocator, + + stdin: ?&io.OutStream, + stdout: ?&io.InStream, + stderr: ?&io.InStream, + + term: ?%Term, + + /// Possibly called from a signal handler. + onTerm: ?fn(&ChildProcess), pub const Term = enum { - Clean: i32, + Exited: i32, Signal: i32, Stopped: i32, Unknown: i32, @@ -34,13 +45,15 @@ pub const ChildProcess = struct { Close, }; + /// onTerm can be called before `spawn` returns. pub fn spawn(exe_path: []const u8, args: []const []const u8, cwd: ?[]const u8, env_map: &const BufMap, - stdin: StdIo, stdout: StdIo, stderr: StdIo, allocator: &Allocator) -> %ChildProcess + stdin: StdIo, stdout: StdIo, stderr: StdIo, + onTerm: ?fn(&ChildProcess), allocator: &Allocator) -> %&ChildProcess { switch (builtin.os) { Os.linux, Os.macosx, Os.ios, Os.darwin => { - return spawnPosix(exe_path, args, cwd, env_map, stdin, stdout, stderr, allocator); + return spawnPosix(exe_path, args, cwd, env_map, stdin, stdout, stderr, onTerm, allocator); }, else => @compileError("Unsupported OS"), } @@ -48,6 +61,12 @@ pub const ChildProcess = struct { /// Forcibly terminates child process and then cleans up all resources. pub fn kill(self: &ChildProcess) -> %Term { + block_SIGCHLD(); + defer restore_SIGCHLD(); + + if (self.term) |term| { + return term; + } const ret = posix.kill(self.pid, posix.SIGTERM); const err = posix.getErrno(ret); if (err > 0) { @@ -58,37 +77,60 @@ pub const ChildProcess = struct { else => error.Unexpected, }; } - return self.wait(); + self.waitUnwrapped(); + return ??self.term; } /// Blocks until child process terminates and then cleans up all resources. pub fn wait(self: &ChildProcess) -> %Term { - defer { - os.posixClose(self.err_pipe[0]); - os.posixClose(self.err_pipe[1]); - }; + block_SIGCHLD(); + defer restore_SIGCHLD(); + if (self.term) |term| { + return term; + } + + self.waitUnwrapped(); + return ??self.term; + } + + fn waitUnwrapped(self: &ChildProcess) { var status: i32 = undefined; while (true) { const err = posix.getErrno(posix.waitpid(self.pid, &status, 0)); if (err > 0) { switch (err) { - posix.EINVAL, posix.ECHILD => unreachable, posix.EINTR => continue, - else => { - if (self.stdin) |*stdin| { stdin.close(); } - if (self.stdout) |*stdout| { stdout.close(); } - if (self.stderr) |*stderr| { stderr.close(); } - return error.Unexpected; - }, + else => unreachable, } } - break; + self.cleanupStreams(); + self.handleWaitResult(status); + return; } + } - if (self.stdin) |*stdin| { stdin.close(); } - if (self.stdout) |*stdout| { stdout.close(); } - if (self.stderr) |*stderr| { stderr.close(); } + fn handleWaitResult(self: &ChildProcess, status: i32) { + self.term = self.cleanupAfterWait(status); + + if (self.onTerm) |onTerm| { + onTerm(self); + } + } + + fn cleanupStreams(self: &ChildProcess) { + if (self.stdin) |stdin| { stdin.close(); self.allocator.free(stdin); } + if (self.stdout) |stdout| { stdout.close(); self.allocator.free(stdout); } + if (self.stderr) |stderr| { stderr.close(); self.allocator.free(stderr); } + } + + fn cleanupAfterWait(self: &ChildProcess, status: i32) -> %Term { + children_nodes.remove(&self.llnode); + + defer { + os.posixClose(self.err_pipe[0]); + os.posixClose(self.err_pipe[1]); + }; // Write @maxValue(ErrInt) to the write end of the err_pipe. This is after // waitpid, so this write is guaranteed to be after the child @@ -108,7 +150,7 @@ pub const ChildProcess = struct { fn statusToTerm(status: i32) -> Term { return if (posix.WIFEXITED(status)) { - Term.Clean { posix.WEXITSTATUS(status) } + Term.Exited { posix.WEXITSTATUS(status) } } else if (posix.WIFSIGNALED(status)) { Term.Signal { posix.WTERMSIG(status) } } else if (posix.WIFSTOPPED(status)) { @@ -120,8 +162,12 @@ pub const ChildProcess = struct { fn spawnPosix(exe_path: []const u8, args: []const []const u8, maybe_cwd: ?[]const u8, env_map: &const BufMap, - stdin: StdIo, stdout: StdIo, stderr: StdIo, allocator: &Allocator) -> %ChildProcess + stdin: StdIo, stdout: StdIo, stderr: StdIo, + onTerm: ?fn(&ChildProcess), allocator: &Allocator) -> %&ChildProcess { + // TODO atomically set a flag saying that we already did this + install_SIGCHLD_handler(); + const stdin_pipe = if (stdin == StdIo.Pipe) %return makePipe() else undefined; %defer if (stdin == StdIo.Pipe) { destroyPipe(stdin_pipe); }; @@ -143,16 +189,39 @@ pub const ChildProcess = struct { const err_pipe = %return makePipe(); %defer destroyPipe(err_pipe); - const pid = posix.fork(); - const pid_err = posix.getErrno(pid); + const child = %return allocator.create(ChildProcess); + %defer allocator.destroy(child); + + const stdin_ptr = if (stdin == StdIo.Pipe) { + %return allocator.create(io.OutStream) + } else { + null + }; + const stdout_ptr = if (stdout == StdIo.Pipe) { + %return allocator.create(io.InStream) + } else { + null + }; + const stderr_ptr = if (stderr == StdIo.Pipe) { + %return allocator.create(io.InStream) + } else { + null + }; + + block_SIGCHLD(); + const pid_result = posix.fork(); + const pid_err = posix.getErrno(pid_result); if (pid_err > 0) { + restore_SIGCHLD(); return switch (pid_err) { posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources, else => error.Unexpected, }; } - if (pid == 0) { + if (pid_result == 0) { // we are the child + restore_SIGCHLD(); + setUpChildIo(stdin, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) %% |err| forkChildErrReport(err_pipe[1], err); setUpChildIo(stdout, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) %% @@ -170,45 +239,53 @@ pub const ChildProcess = struct { } // we are the parent + const pid = i32(pid_result); + if (stdin_ptr) |outstream| { + *outstream = io.OutStream { + .fd = stdin_pipe[1], + .handle = {}, + .handle_id = {}, + .buffer = undefined, + .index = 0, + }; + } + if (stdout_ptr) |instream| { + *instream = io.InStream { + .fd = stdout_pipe[0], + .handle = {}, + .handle_id = {}, + }; + } + if (stderr_ptr) |instream| { + *instream = io.InStream { + .fd = stderr_pipe[0], + .handle = {}, + .handle_id = {}, + }; + } + + *child = ChildProcess { + .allocator = allocator, + .pid = pid, + .err_pipe = err_pipe, + .llnode = LinkedList(&ChildProcess).Node.init(child), + .term = null, + .onTerm = onTerm, + .stdin = stdin_ptr, + .stdout = stdout_ptr, + .stderr = stderr_ptr, + }; + + children_nodes.prepend(&child.llnode); + + restore_SIGCHLD(); + if (stdin == StdIo.Pipe) { os.posixClose(stdin_pipe[0]); } if (stdout == StdIo.Pipe) { os.posixClose(stdout_pipe[1]); } if (stderr == StdIo.Pipe) { os.posixClose(stderr_pipe[1]); } if (any_ignore) { os.posixClose(dev_null_fd); } - return ChildProcess { - .pid = i32(pid), - .err_pipe = err_pipe, - - .stdin = if (stdin == StdIo.Pipe) { - io.OutStream { - .fd = stdin_pipe[1], - .handle = {}, - .handle_id = {}, - .buffer = undefined, - .index = 0, - } - } else { - null - }, - .stdout = if (stdout == StdIo.Pipe) { - io.InStream { - .fd = stdout_pipe[0], - .handle = {}, - .handle_id = {}, - } - } else { - null - }, - .stderr = if (stderr == StdIo.Pipe) { - io.InStream { - .fd = stderr_pipe[0], - .handle = {}, - .handle_id = {}, - } - } else { - null - }, - }; + return child; } fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) -> %void { @@ -258,3 +335,54 @@ fn readIntFd(fd: i32) -> %ErrInt { os.posixRead(fd, bytes[0..]) %% return error.SystemResources; return mem.readInt(bytes[0..], ErrInt, true); } + +extern fn sigchld_handler(_: i32) { + while (true) { + var status: i32 = undefined; + const pid_result = posix.waitpid(-1, &status, posix.WNOHANG); + const err = posix.getErrno(pid_result); + if (err == posix.ECHILD) { + return; + } + handleTerm(i32(pid_result), status); + } +} + +fn handleTerm(pid: i32, status: i32) { + var it = children_nodes.first; + while (it) |node| : (it = node.next) { + if (node.data.pid == pid) { + assert(node.data.term == null); + node.data.handleWaitResult(status); + return; + } + } + unreachable; +} + +const sigchld_set = { + var signal_set = posix.empty_sigset; + posix.sigaddset(&signal_set, posix.SIGCHLD); + signal_set +}; + +fn block_SIGCHLD() { + const err = posix.getErrno(posix.sigprocmask(posix.SIG_BLOCK, &sigchld_set, null)); + assert(err == 0); +} + +fn restore_SIGCHLD() { + const err = posix.getErrno(posix.sigprocmask(posix.SIG_UNBLOCK, &sigchld_set, null)); + assert(err == 0); +} + +const sigchld_action = posix.Sigaction { + .handler = sigchld_handler, + .mask = posix.empty_sigset, + .flags = posix.SA_RESTART | posix.SA_NOCLDSTOP, +}; + +fn install_SIGCHLD_handler() { + const err = posix.getErrno(posix.sigaction(posix.SIGCHLD, &sigchld_action, null)); + assert(err == 0); +} diff --git a/std/os/linux.zig b/std/os/linux.zig index d57e966ab5..ea47c0fc12 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -1,3 +1,4 @@ +const assert = @import("../debug.zig").assert; const builtin = @import("builtin"); const arch = switch (builtin.arch) { builtin.Arch.x86_64 => @import("linux_x86_64.zig"), @@ -36,6 +37,22 @@ 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; @@ -100,9 +117,9 @@ pub const SEEK_SET = 0; pub const SEEK_CUR = 1; pub const SEEK_END = 2; -const SIG_BLOCK = 0; -const SIG_UNBLOCK = 1; -const SIG_SETMASK = 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; @@ -448,7 +465,7 @@ pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize { } pub fn kill(pid: i32, sig: i32) -> usize { - arch.syscall2(arch.SYS_kill, usize(pid), usize(sig)) + arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)) } pub fn unlink(path: &const u8) -> usize { @@ -456,17 +473,65 @@ pub fn unlink(path: &const u8) -> usize { } pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize { - arch.syscall4(arch.SYS_wait4, usize(pid), @ptrToInt(status), @bitCast(usize, isize(options)), 0) + 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 { arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)) } +pub fn sigprocmask(flags: u32, set: &const sigset_t, oldset: ?&sigset_t) -> usize { + 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(), 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]u8; -const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; -const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, }; +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), + flags: usize, + restorer: extern fn(), + mask: [2]u32, +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = struct { + handler: extern fn(i32), + mask: sigset_t, + flags: u32, +}; + +pub const SIG_ERR = @intToPtr(extern fn(i32), @maxValue(usize)); +pub const SIG_DFL = @intToPtr(extern fn(i32), 0); +pub const SIG_IGN = @intToPtr(extern fn(i32), 1); +pub const empty_sigset = []usize{0} ** sigset_t.len; pub fn raise(sig: i32) -> usize { var set: sigset_t = undefined; @@ -489,6 +554,16 @@ fn restoreSignals(set: &sigset_t) { _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8); } +pub fn sigaddset(set: &sigset_t, sig: u6) { + 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; diff --git a/std/os/linux_i386.zig b/std/os/linux_i386.zig index d76d614a36..215670e3a9 100644 --- a/std/os/linux_i386.zig +++ b/std/os/linux_i386.zig @@ -486,6 +486,23 @@ pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, [arg6] "{ebp}" (arg6)) } +pub nakedcc fn restore() { + asm volatile ( + \\popl %%eax + \\movl $119, %%eax + \\int $0x80 + : + : + : "rcx", "r11") +} + +pub nakedcc fn restore_rt() { + asm volatile ("int $0x80" + : + : [number] "{eax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11") +} + export struct msghdr { msg_name: &u8, msg_namelen: socklen_t, diff --git a/std/os/linux_x86_64.zig b/std/os/linux_x86_64.zig index ad418d173b..d60f5496f9 100644 --- a/std/os/linux_x86_64.zig +++ b/std/os/linux_x86_64.zig @@ -442,6 +442,14 @@ pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz : "rcx", "r11") } +pub nakedcc fn restore_rt() { + asm volatile ("syscall" + : + : [number] "{rax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11") +} + + pub const msghdr = extern struct { msg_name: &u8, msg_namelen: socklen_t, diff --git a/test/cases/array.zig b/test/cases/array.zig index 07448ecd22..9e9d83c2e1 100644 --- a/test/cases/array.zig +++ b/test/cases/array.zig @@ -86,3 +86,13 @@ test "array literal with specified size" { assert(array[0] == 1); assert(array[1] == 2); } + +test "array child property" { + var x: [5]i32 = undefined; + assert(@typeOf(x).child == i32); +} + +test "array len property" { + var x: [5]i32 = undefined; + assert(@typeOf(x).len == 5); +} diff --git a/test/tests.zig b/test/tests.zig index 1a69ac0553..8832b85254 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -238,7 +238,7 @@ pub const CompareOutputContext = struct { %%io.stderr.printf("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name); var child = os.ChildProcess.spawn(full_exe_path, [][]u8{}, null, &b.env_map, - StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, b.allocator) %% |err| + StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, null, b.allocator) %% |err| { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; @@ -253,7 +253,7 @@ pub const CompareOutputContext = struct { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; switch (term) { - Term.Clean => |code| { + Term.Exited => |code| { if (code != 0) { %%io.stderr.printf("Process {} exited with error code {}\n", full_exe_path, code); return error.TestFailed; @@ -313,7 +313,7 @@ pub const CompareOutputContext = struct { %%io.stderr.printf("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name); var child = os.ChildProcess.spawn(full_exe_path, [][]u8{}, null, &b.env_map, - StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, b.allocator) %% |err| + StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, null, b.allocator) %% |err| { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; @@ -324,7 +324,7 @@ pub const CompareOutputContext = struct { const debug_trap_signal: i32 = 5; switch (term) { - Term.Clean => |code| { + Term.Exited => |code| { %%io.stderr.printf("\nProgram expected to hit debug trap (signal {}) " ++ "but exited with return code {}\n", debug_trap_signal, code); return error.TestFailed; @@ -557,7 +557,7 @@ pub const CompileErrorContext = struct { } var child = os.ChildProcess.spawn(b.zig_exe, zig_args.toSliceConst(), null, &b.env_map, - StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, b.allocator) %% |err| + StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, null, b.allocator) %% |err| { debug.panic("Unable to spawn {}: {}\n", b.zig_exe, @errorName(err)); }; @@ -572,7 +572,7 @@ pub const CompileErrorContext = struct { debug.panic("Unable to spawn {}: {}\n", b.zig_exe, @errorName(err)); }; switch (term) { - Term.Clean => |code| { + Term.Exited => |code| { if (code == 0) { %%io.stderr.printf("Compilation incorrectly succeeded\n"); return error.TestFailed; @@ -819,7 +819,7 @@ pub const ParseCContext = struct { } var child = os.ChildProcess.spawn(b.zig_exe, zig_args.toSliceConst(), null, &b.env_map, - StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, b.allocator) %% |err| + StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, null, b.allocator) %% |err| { debug.panic("Unable to spawn {}: {}\n", b.zig_exe, @errorName(err)); }; @@ -834,7 +834,7 @@ pub const ParseCContext = struct { debug.panic("Unable to spawn {}: {}\n", b.zig_exe, @errorName(err)); }; switch (term) { - Term.Clean => |code| { + Term.Exited => |code| { if (code != 0) { %%io.stderr.printf("Compilation failed with exit code {}\n", code); return error.TestFailed;