From 17b0166e004e81d9c433576f4c642e743fc527b7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 24 May 2019 18:27:18 -0400 Subject: do Jay's suggestion with posix/os API naming & layout --- std/process.zig | 583 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 583 insertions(+) create mode 100644 std/process.zig (limited to 'std/process.zig') diff --git a/std/process.zig b/std/process.zig new file mode 100644 index 0000000000..f2f1dbf4fd --- /dev/null +++ b/std/process.zig @@ -0,0 +1,583 @@ +const std = @import("std.zig"); +const posix = std.os.posix; +const BufMap = std.BufMap; +const mem = std.mem; +const Allocator = mem.Allocator; +const assert = std.debug.assert; +const testing = std.testing; + +pub const abort = posix.abort; +pub const exit = posix.exit; + +/// Caller must free result when done. +/// TODO make this go through libc when we have it +pub fn getEnvMap(allocator: *Allocator) !BufMap { + var result = BufMap.init(allocator); + errdefer result.deinit(); + + if (is_windows) { + const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory; + defer assert(windows.FreeEnvironmentStringsW(ptr) != 0); + + var i: usize = 0; + while (true) { + if (ptr[i] == 0) return result; + + const key_start = i; + + while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {} + const key_w = ptr[key_start..i]; + const key = try std.unicode.utf16leToUtf8Alloc(allocator, key_w); + errdefer allocator.free(key); + + if (ptr[i] == '=') i += 1; + + const value_start = i; + while (ptr[i] != 0) : (i += 1) {} + const value_w = ptr[value_start..i]; + const value = try std.unicode.utf16leToUtf8Alloc(allocator, value_w); + errdefer allocator.free(value); + + i += 1; // skip over null byte + + try result.setMove(key, value); + } + } else if (builtin.os == Os.wasi) { + var environ_count: usize = undefined; + var environ_buf_size: usize = undefined; + + const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); + if (environ_sizes_get_ret != os.wasi.ESUCCESS) { + return unexpectedErrorPosix(environ_sizes_get_ret); + } + + // TODO: Verify that the documentation is incorrect + // https://github.com/WebAssembly/WASI/issues/27 + var environ = try allocator.alloc(?[*]u8, environ_count + 1); + defer allocator.free(environ); + var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size); + defer allocator.free(environ_buf); + + const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr); + if (environ_get_ret != os.wasi.ESUCCESS) { + return unexpectedErrorPosix(environ_get_ret); + } + + for (environ) |env| { + if (env) |ptr| { + const pair = mem.toSlice(u8, ptr); + var parts = mem.separate(pair, "="); + const key = parts.next().?; + const value = parts.next().?; + try result.set(key, value); + } + } + return result; + } else { + for (posix.environ) |ptr| { + var line_i: usize = 0; + while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} + const key = ptr[0..line_i]; + + var end_i: usize = line_i; + while (ptr[end_i] != 0) : (end_i += 1) {} + const value = ptr[line_i + 1 .. end_i]; + + try result.set(key, value); + } + return result; + } +} + +test "os.getEnvMap" { + var env = try getEnvMap(std.debug.global_allocator); + defer env.deinit(); +} + +pub const GetEnvVarOwnedError = error{ + OutOfMemory, + EnvironmentVariableNotFound, + + /// See https://github.com/ziglang/zig/issues/1774 + InvalidUtf8, +}; + +/// Caller must free returned memory. +/// TODO make this go through libc when we have it +pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 { + if (is_windows) { + const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key); + defer allocator.free(key_with_null); + + var buf = try allocator.alloc(u16, 256); + defer allocator.free(buf); + + while (true) { + const windows_buf_len = math.cast(windows.DWORD, buf.len) catch return error.OutOfMemory; + const result = windows.GetEnvironmentVariableW(key_with_null.ptr, buf.ptr, windows_buf_len); + + if (result == 0) { + const err = windows.GetLastError(); + return switch (err) { + windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound, + else => { + windows.unexpectedError(err) catch {}; + return error.EnvironmentVariableNotFound; + }, + }; + } + + if (result > buf.len) { + buf = try allocator.realloc(buf, result); + continue; + } + + return std.unicode.utf16leToUtf8Alloc(allocator, buf) catch |err| switch (err) { + error.DanglingSurrogateHalf => return error.InvalidUtf8, + error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, + error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, + error.OutOfMemory => return error.OutOfMemory, + }; + } + } else { + const result = getEnvPosix(key) orelse return error.EnvironmentVariableNotFound; + return mem.dupe(allocator, u8, result); + } +} + +test "os.getEnvVarOwned" { + var ga = std.debug.global_allocator; + testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV")); +} + +pub const ArgIteratorPosix = struct { + index: usize, + count: usize, + + pub fn init() ArgIteratorPosix { + return ArgIteratorPosix{ + .index = 0, + .count = raw.len, + }; + } + + pub fn next(self: *ArgIteratorPosix) ?[]const u8 { + if (self.index == self.count) return null; + + const s = raw[self.index]; + self.index += 1; + return cstr.toSlice(s); + } + + pub fn skip(self: *ArgIteratorPosix) bool { + if (self.index == self.count) return false; + + self.index += 1; + return true; + } + + /// This is marked as public but actually it's only meant to be used + /// internally by zig's startup code. + pub var raw: [][*]u8 = undefined; +}; + +pub const ArgIteratorWindows = struct { + index: usize, + cmd_line: [*]const u8, + in_quote: bool, + quote_count: usize, + seen_quote_count: usize, + + pub const NextError = error{OutOfMemory}; + + pub fn init() ArgIteratorWindows { + return initWithCmdLine(windows.GetCommandLineA()); + } + + pub fn initWithCmdLine(cmd_line: [*]const u8) ArgIteratorWindows { + return ArgIteratorWindows{ + .index = 0, + .cmd_line = cmd_line, + .in_quote = false, + .quote_count = countQuotes(cmd_line), + .seen_quote_count = 0, + }; + } + + /// You must free the returned memory when done. + pub fn next(self: *ArgIteratorWindows, allocator: *Allocator) ?(NextError![]u8) { + // march forward over whitespace + while (true) : (self.index += 1) { + const byte = self.cmd_line[self.index]; + switch (byte) { + 0 => return null, + ' ', '\t' => continue, + else => break, + } + } + + return self.internalNext(allocator); + } + + pub fn skip(self: *ArgIteratorWindows) bool { + // march forward over whitespace + while (true) : (self.index += 1) { + const byte = self.cmd_line[self.index]; + switch (byte) { + 0 => return false, + ' ', '\t' => continue, + else => break, + } + } + + var backslash_count: usize = 0; + while (true) : (self.index += 1) { + const byte = self.cmd_line[self.index]; + switch (byte) { + 0 => return true, + '"' => { + const quote_is_real = backslash_count % 2 == 0; + if (quote_is_real) { + self.seen_quote_count += 1; + } + }, + '\\' => { + backslash_count += 1; + }, + ' ', '\t' => { + if (self.seen_quote_count % 2 == 0 or self.seen_quote_count == self.quote_count) { + return true; + } + backslash_count = 0; + }, + else => { + backslash_count = 0; + continue; + }, + } + } + } + + fn internalNext(self: *ArgIteratorWindows, allocator: *Allocator) NextError![]u8 { + var buf = try Buffer.initSize(allocator, 0); + defer buf.deinit(); + + var backslash_count: usize = 0; + while (true) : (self.index += 1) { + const byte = self.cmd_line[self.index]; + switch (byte) { + 0 => return buf.toOwnedSlice(), + '"' => { + const quote_is_real = backslash_count % 2 == 0; + try self.emitBackslashes(&buf, backslash_count / 2); + backslash_count = 0; + + if (quote_is_real) { + self.seen_quote_count += 1; + if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) { + try buf.appendByte('"'); + } + } else { + try buf.appendByte('"'); + } + }, + '\\' => { + backslash_count += 1; + }, + ' ', '\t' => { + try self.emitBackslashes(&buf, backslash_count); + backslash_count = 0; + if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) { + try buf.appendByte(byte); + } else { + return buf.toOwnedSlice(); + } + }, + else => { + try self.emitBackslashes(&buf, backslash_count); + backslash_count = 0; + try buf.appendByte(byte); + }, + } + } + } + + fn emitBackslashes(self: *ArgIteratorWindows, buf: *Buffer, emit_count: usize) !void { + var i: usize = 0; + while (i < emit_count) : (i += 1) { + try buf.appendByte('\\'); + } + } + + fn countQuotes(cmd_line: [*]const u8) usize { + var result: usize = 0; + var backslash_count: usize = 0; + var index: usize = 0; + while (true) : (index += 1) { + const byte = cmd_line[index]; + switch (byte) { + 0 => return result, + '\\' => backslash_count += 1, + '"' => { + result += 1 - (backslash_count % 2); + backslash_count = 0; + }, + else => { + backslash_count = 0; + }, + } + } + } +}; + +pub const ArgIterator = struct { + const InnerType = if (builtin.os == Os.windows) ArgIteratorWindows else ArgIteratorPosix; + + inner: InnerType, + + pub fn init() ArgIterator { + if (builtin.os == Os.wasi) { + // TODO: Figure out a compatible interface accomodating WASI + @compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead."); + } + + return ArgIterator{ .inner = InnerType.init() }; + } + + pub const NextError = ArgIteratorWindows.NextError; + + /// You must free the returned memory when done. + pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) { + if (builtin.os == Os.windows) { + return self.inner.next(allocator); + } else { + return mem.dupe(allocator, u8, self.inner.next() orelse return null); + } + } + + /// If you only are targeting posix you can call this and not need an allocator. + pub fn nextPosix(self: *ArgIterator) ?[]const u8 { + return self.inner.next(); + } + + /// Parse past 1 argument without capturing it. + /// Returns `true` if skipped an arg, `false` if we are at the end. + pub fn skip(self: *ArgIterator) bool { + return self.inner.skip(); + } +}; + +pub fn args() ArgIterator { + return ArgIterator.init(); +} + +/// Caller must call argsFree on result. +pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { + if (builtin.os == Os.wasi) { + var count: usize = undefined; + var buf_size: usize = undefined; + + const args_sizes_get_ret = os.wasi.args_sizes_get(&count, &buf_size); + if (args_sizes_get_ret != os.wasi.ESUCCESS) { + return unexpectedErrorPosix(args_sizes_get_ret); + } + + var argv = try allocator.alloc([*]u8, count); + defer allocator.free(argv); + + var argv_buf = try allocator.alloc(u8, buf_size); + const args_get_ret = os.wasi.args_get(argv.ptr, argv_buf.ptr); + if (args_get_ret != os.wasi.ESUCCESS) { + return unexpectedErrorPosix(args_get_ret); + } + + var result_slice = try allocator.alloc([]u8, count); + + var i: usize = 0; + while (i < count) : (i += 1) { + result_slice[i] = mem.toSlice(u8, argv[i]); + } + + return result_slice; + } + + // TODO refactor to only make 1 allocation. + var it = args(); + var contents = try Buffer.initSize(allocator, 0); + defer contents.deinit(); + + var slice_list = ArrayList(usize).init(allocator); + defer slice_list.deinit(); + + while (it.next(allocator)) |arg_or_err| { + const arg = try arg_or_err; + defer allocator.free(arg); + try contents.append(arg); + try slice_list.append(arg.len); + } + + const contents_slice = contents.toSliceConst(); + const slice_sizes = slice_list.toSliceConst(); + const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len); + const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len); + const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); + errdefer allocator.free(buf); + + const result_slice_list = @bytesToSlice([]u8, buf[0..slice_list_bytes]); + const result_contents = buf[slice_list_bytes..]; + mem.copy(u8, result_contents, contents_slice); + + var contents_index: usize = 0; + for (slice_sizes) |len, i| { + const new_index = contents_index + len; + result_slice_list[i] = result_contents[contents_index..new_index]; + contents_index = new_index; + } + + return result_slice_list; +} + +pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { + if (builtin.os == Os.wasi) { + const last_item = args_alloc[args_alloc.len - 1]; + const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated + const first_item_ptr = args_alloc[0].ptr; + const len = last_byte_addr - @ptrToInt(first_item_ptr); + allocator.free(first_item_ptr[0..len]); + + return allocator.free(args_alloc); + } + + var total_bytes: usize = 0; + for (args_alloc) |arg| { + total_bytes += @sizeOf([]u8) + arg.len; + } + const unaligned_allocated_buf = @ptrCast([*]const u8, args_alloc.ptr)[0..total_bytes]; + const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf); + return allocator.free(aligned_allocated_buf); +} + +test "windows arg parsing" { + testWindowsCmdLine(c"a b\tc d", [][]const u8{ "a", "b", "c", "d" }); + testWindowsCmdLine(c"\"abc\" d e", [][]const u8{ "abc", "d", "e" }); + testWindowsCmdLine(c"a\\\\\\b d\"e f\"g h", [][]const u8{ "a\\\\\\b", "de fg", "h" }); + testWindowsCmdLine(c"a\\\\\\\"b c d", [][]const u8{ "a\\\"b", "c", "d" }); + testWindowsCmdLine(c"a\\\\\\\\\"b c\" d e", [][]const u8{ "a\\\\b c", "d", "e" }); + testWindowsCmdLine(c"a b\tc \"d f", [][]const u8{ "a", "b", "c", "\"d", "f" }); + + testWindowsCmdLine(c"\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", [][]const u8{ + ".\\..\\zig-cache\\build", + "bin\\zig.exe", + ".\\..", + ".\\..\\zig-cache", + "--help", + }); +} + +fn testWindowsCmdLine(input_cmd_line: [*]const u8, expected_args: []const []const u8) void { + var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line); + for (expected_args) |expected_arg| { + const arg = it.next(std.debug.global_allocator).? catch unreachable; + testing.expectEqualSlices(u8, expected_arg, arg); + } + testing.expect(it.next(std.debug.global_allocator) == null); +} + +pub const UserInfo = struct { + uid: u32, + gid: u32, +}; + +/// POSIX function which gets a uid from username. +pub fn getUserInfo(name: []const u8) !UserInfo { + return switch (builtin.os) { + .linux, .macosx, .ios, .freebsd, .netbsd => posixGetUserInfo(name), + else => @compileError("Unsupported OS"), + }; +} + +/// TODO this reads /etc/passwd. But sometimes the user/id mapping is in something else +/// like NIS, AD, etc. See `man nss` or look at an strace for `id myuser`. +pub fn posixGetUserInfo(name: []const u8) !UserInfo { + var in_stream = try io.InStream.open("/etc/passwd", null); + defer in_stream.close(); + + const State = enum { + Start, + WaitForNextLine, + SkipPassword, + ReadUserId, + ReadGroupId, + }; + + var buf: [std.mem.page_size]u8 = undefined; + var name_index: usize = 0; + var state = State.Start; + var uid: u32 = 0; + var gid: u32 = 0; + + while (true) { + const amt_read = try in_stream.read(buf[0..]); + for (buf[0..amt_read]) |byte| { + switch (state) { + .Start => switch (byte) { + ':' => { + state = if (name_index == name.len) State.SkipPassword else State.WaitForNextLine; + }, + '\n' => return error.CorruptPasswordFile, + else => { + if (name_index == name.len or name[name_index] != byte) { + state = .WaitForNextLine; + } + name_index += 1; + }, + }, + .WaitForNextLine => switch (byte) { + '\n' => { + name_index = 0; + state = .Start; + }, + else => continue, + }, + .SkipPassword => switch (byte) { + '\n' => return error.CorruptPasswordFile, + ':' => { + state = .ReadUserId; + }, + else => continue, + }, + .ReadUserId => switch (byte) { + ':' => { + state = .ReadGroupId; + }, + '\n' => return error.CorruptPasswordFile, + else => { + const digit = switch (byte) { + '0'...'9' => byte - '0', + else => return error.CorruptPasswordFile, + }; + if (@mulWithOverflow(u32, uid, 10, *uid)) return error.CorruptPasswordFile; + if (@addWithOverflow(u32, uid, digit, *uid)) return error.CorruptPasswordFile; + }, + }, + .ReadGroupId => switch (byte) { + '\n', ':' => { + return UserInfo{ + .uid = uid, + .gid = gid, + }; + }, + else => { + const digit = switch (byte) { + '0'...'9' => byte - '0', + else => return error.CorruptPasswordFile, + }; + if (@mulWithOverflow(u32, gid, 10, *gid)) return error.CorruptPasswordFile; + if (@addWithOverflow(u32, gid, digit, *gid)) return error.CorruptPasswordFile; + }, + }, + } + } + if (amt_read < buf.len) return error.UserNotFound; + } +} -- cgit v1.2.3 From 7cb6279ac0cec065234347bda5944be64fe8b3da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 25 May 2019 13:07:44 -0400 Subject: clean up references to posix --- CMakeLists.txt | 1 - std/atomic/queue.zig | 8 +- std/atomic/stack.zig | 8 +- std/c/darwin.zig | 2 +- std/c/linux.zig | 1 + std/child_process.zig | 18 +- std/crypto.zig | 2 +- std/debug.zig | 9 +- std/event/fs.zig | 38 +- std/event/loop.zig | 142 +++--- std/event/net.zig | 113 +++-- std/fs/path.zig | 37 +- std/heap.zig | 324 +++++++------ std/mem.zig | 6 +- std/mutex.zig | 4 +- std/net.zig | 24 +- std/os.zig | 35 +- std/os/bits/linux.zig | 25 + std/os/linux.zig | 874 ++++++++++++++++++++++++++++++++++- std/os/linux/sys.zig | 859 ---------------------------------- std/os/linux/tls.zig | 13 +- std/os/linux/x86_64.zig | 2 +- std/os/test.zig | 29 +- std/os/windows.zig | 16 +- std/process.zig | 34 +- std/special/bootstrap.zig | 4 +- std/statically_initialized_mutex.zig | 4 +- std/thread.zig | 150 +++--- 28 files changed, 1390 insertions(+), 1392 deletions(-) delete mode 100644 std/os/linux/sys.zig (limited to 'std/process.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c7ceb88b9..9f6af9a9de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -620,7 +620,6 @@ set(ZIG_STD_FILES "os/freebsd.zig" "os/linux.zig" "os/linux/arm64.zig" - "os/linux/sys.zig" "os/linux/tls.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 34cd0d22f8..bf5700c51e 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -186,13 +186,13 @@ test "std.atomic.Queue" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 773bdf6f1b..e9d070ac7b 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -120,13 +120,13 @@ test "std.atomic.stack" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 3d0c7f4dd2..e45a158f68 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -3,7 +3,7 @@ const assert = std.debug.assert; const builtin = @import("builtin"); const macho = std.macho; -use @import("posix/darwin.zig"); +use @import("../os/bits.zig"); extern "c" fn __error() *c_int; pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int; diff --git a/std/c/linux.zig b/std/c/linux.zig index 388d8edab8..00fc600dab 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -2,6 +2,7 @@ const std = @import("../std.zig"); use std.c; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; +pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; diff --git a/std/child_process.zig b/std/child_process.zig index fa20a5e814..95d11d46a3 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -351,22 +351,22 @@ pub const ChildProcess = struct { const err_pipe = try os.pipe(); errdefer destroyPipe(err_pipe); - const pid_result = try posix.fork(); + const pid_result = try os.fork(); if (pid_result == 0) { // we are the child - setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdin_behavior, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdout_behavior, stdout_pipe[1], os.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stderr_behavior, stderr_pipe[1], os.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - if (self.stdin_behavior == StdIo.Pipe) { + if (self.stdin_behavior == .Pipe) { os.close(stdin_pipe[0]); os.close(stdin_pipe[1]); } - if (self.stdout_behavior == StdIo.Pipe) { + if (self.stdout_behavior == .Pipe) { os.close(stdout_pipe[0]); os.close(stdout_pipe[1]); } - if (self.stderr_behavior == StdIo.Pipe) { + if (self.stderr_behavior == .Pipe) { os.close(stderr_pipe[0]); os.close(stderr_pipe[1]); } @@ -383,7 +383,7 @@ pub const ChildProcess = struct { os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } - os.posix.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); + os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); } // we are the parent @@ -736,7 +736,7 @@ fn destroyPipe(pipe: [2]i32) void { // Then the child exits. fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn { writeIntFd(fd, ErrInt(@errorToInt(err))) catch {}; - posix.exit(1); + os.exit(1); } const ErrInt = @IntType(false, @sizeOf(anyerror) * 8); diff --git a/std/crypto.zig b/std/crypto.zig index 2c3d8eba9e..2b57de9e60 100644 --- a/std/crypto.zig +++ b/std/crypto.zig @@ -33,7 +33,7 @@ pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305; pub const X25519 = @import("crypto/x25519.zig").X25519; const std = @import("std.zig"); -pub const randomBytes = std.posix.getrandom; +pub const randomBytes = std.os.getrandom; test "crypto" { _ = @import("crypto/blake2.zig"); diff --git a/std/debug.zig b/std/debug.zig index 83daad6dc5..a309c31bbe 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1012,16 +1012,15 @@ fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { errdefer S.self_exe_file.close(); const self_exe_mmap_len = try S.self_exe_file.getEndPos(); - const self_exe_mmap = os.posix.mmap( + const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, - os.posix.PROT_READ, - os.posix.MAP_SHARED, + os.PROT_READ, + os.MAP_SHARED, S.self_exe_file.handle, 0, ); - if (self_exe_mmap == os.posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(os.posix.munmap(self_exe_mmap, self_exe_mmap_len) == 0); + errdefer os.munmap(self_exe_mmap, self_exe_mmap_len); const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len]; S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice); diff --git a/std/event/fs.zig b/std/event/fs.zig index 346a6c6b69..fbdac65889 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -5,10 +5,9 @@ const assert = std.debug.assert; const testing = std.testing; const os = std.os; const mem = std.mem; -const posix = os.posix; const windows = os.windows; const Loop = event.Loop; -const fd_t = posix.fd_t; +const fd_t = os.fd_t; const File = std.fs.File; pub const RequestNode = std.atomic.Queue(Request).Node; @@ -33,7 +32,7 @@ pub const Request = struct { pub const PWriteV = struct { fd: fd_t, - iov: []const os.posix.iovec_const, + iov: []const os.iovec_const, offset: usize, result: Error!void, @@ -42,7 +41,7 @@ pub const Request = struct { pub const PReadV = struct { fd: fd_t, - iov: []const os.posix.iovec, + iov: []const os.iovec, offset: usize, result: Error!usize, @@ -89,11 +88,11 @@ pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: us builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -171,7 +170,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) pub async fn pwritevPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec_const, + iovecs: []const os.iovec_const, offset: usize, ) os.PosixWriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -226,11 +225,11 @@ pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PR builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -319,7 +318,7 @@ pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize pub async fn preadvPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec, + iovecs: []const os.iovec, offset: usize, ) os.PosixReadError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -405,7 +404,7 @@ pub async fn openPosix( pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDONLY | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, @@ -435,7 +434,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File. builtin.Os.freebsd, builtin.Os.netbsd, => { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, builtin.Os.windows => return os.windowsOpen( @@ -457,7 +456,7 @@ pub async fn openReadWrite( ) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDWR | os.O_CREAT | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, mode) catch unreachable); }, @@ -888,10 +887,7 @@ pub fn Watch(comptime V: type) type { var close_op_consumed = false; defer if (!close_op_consumed) close_op.finish(); - const flags = switch (builtin.os) { - builtin.Os.macosx => posix.O_SYMLINK | posix.O_EVTONLY, - else => 0, - }; + const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0; const mode = 0; const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable); close_op.setHandle(fd); @@ -943,16 +939,16 @@ pub fn Watch(comptime V: type) type { while (true) { if (await (async self.channel.loop.bsdWaitKev( @intCast(usize, close_op.getHandle()), - posix.EVFILT_VNODE, - posix.NOTE_WRITE | posix.NOTE_DELETE, + os.EVFILT_VNODE, + os.NOTE_WRITE | os.NOTE_DELETE, ) catch unreachable)) |kev| { // TODO handle EV_ERROR - if (kev.fflags & posix.NOTE_DELETE != 0) { + if (kev.fflags & os.NOTE_DELETE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.Delete, .data = value_copy, }) catch unreachable); - } else if (kev.fflags & posix.NOTE_WRITE != 0) { + } else if (kev.fflags & os.NOTE_WRITE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.CloseWrite, .data = value_copy, diff --git a/std/event/loop.zig b/std/event/loop.zig index 71bc45d48f..e1848684c7 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -7,9 +7,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; const fs = std.event.fs; const os = std.os; -const posix = os.posix; const windows = os.windows; const maxInt = std.math.maxInt; +const Thread = std.Thread; pub const Loop = struct { allocator: *mem.Allocator, @@ -17,7 +17,7 @@ pub const Loop = struct { os_data: OsData, final_resume_node: ResumeNode, pending_event_count: usize, - extra_threads: []*os.Thread, + extra_threads: []*Thread, // pre-allocated eventfds. all permanently active. // this is how we send promises to be resumed on other threads. @@ -65,7 +65,7 @@ pub const Loop = struct { const KEventFd = struct { base: ResumeNode, - kevent: posix.Kevent, + kevent: os.Kevent, }; pub const Basic = switch (builtin.os) { @@ -81,7 +81,7 @@ pub const Loop = struct { const KEventBasic = struct { base: ResumeNode, - kev: posix.Kevent, + kev: os.Kevent, }; }; @@ -127,7 +127,7 @@ pub const Loop = struct { ); errdefer self.allocator.free(self.eventfd_resume_nodes); - self.extra_threads = try self.allocator.alloc(*os.Thread, extra_thread_count); + self.extra_threads = try self.allocator.alloc(*Thread, extra_thread_count); errdefer self.allocator.free(self.extra_threads); try self.initOsData(extra_thread_count); @@ -172,32 +172,32 @@ pub const Loop = struct { .handle = undefined, .overlapped = ResumeNode.overlapped_init, }, - .eventfd = try os.linuxEventFd(1, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK), - .epoll_op = posix.EPOLL_CTL_ADD, + .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), + .epoll_op = os.EPOLL_CTL_ADD, }, .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); } - self.os_data.epollfd = try os.linuxEpollCreate(posix.EPOLL_CLOEXEC); + self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC); errdefer os.close(self.os_data.epollfd); - self.os_data.final_eventfd = try os.linuxEventFd(0, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK); + self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); errdefer os.close(self.os_data.final_eventfd); - self.os_data.final_eventfd_event = posix.epoll_event{ - .events = posix.EPOLLIN, - .data = posix.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, + self.os_data.final_eventfd_event = os.epoll_event{ + .events = os.EPOLLIN, + .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, }; try os.linuxEpollCtl( self.os_data.epollfd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, self.os_data.final_eventfd, &self.os_data.final_eventfd_event, ); - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -218,7 +218,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { @@ -240,7 +240,7 @@ pub const Loop = struct { }, }; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; for (self.eventfd_resume_nodes) |*eventfd_node, i| { eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{ @@ -251,10 +251,10 @@ pub const Loop = struct { .overlapped = ResumeNode.overlapped_init, }, // this one is for sending events - .kevent = posix.Kevent{ + .kevent = os.Kevent{ .ident = i, - .filter = posix.EVFILT_USER, - .flags = posix.EV_CLEAR | posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_CLEAR | os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&eventfd_node.data.base), @@ -263,46 +263,46 @@ pub const Loop = struct { .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent); + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.data.kevent); _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); - eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE; - eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER; + eventfd_node.data.kevent.flags = os.EV_CLEAR | os.EV_ENABLE; + eventfd_node.data.kevent.fflags = os.NOTE_TRIGGER; } // Pre-add so that we cannot get error.SystemResources // later when we try to activate it. - self.os_data.final_kevent = posix.Kevent{ + self.os_data.final_kevent = os.Kevent{ .ident = extra_thread_count, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&self.final_resume_node), }; - const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent); + const final_kev_arr = (*const [1]os.Kevent)(&self.os_data.final_kevent); _ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null); - self.os_data.final_kevent.flags = posix.EV_ENABLE; - self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER; + self.os_data.final_kevent.flags = os.EV_ENABLE; + self.os_data.final_kevent.fflags = os.NOTE_TRIGGER; - self.os_data.fs_kevent_wake = posix.Kevent{ + self.os_data.fs_kevent_wake = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_ENABLE, - .fflags = posix.NOTE_TRIGGER, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_ENABLE, + .fflags = os.NOTE_TRIGGER, .data = 0, .udata = undefined, }; - self.os_data.fs_kevent_wait = posix.Kevent{ + self.os_data.fs_kevent_wait = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_CLEAR, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_CLEAR, .fflags = 0, .data = 0, .udata = undefined, }; - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -322,7 +322,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.windows => { @@ -371,7 +371,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, else => {}, @@ -400,19 +400,19 @@ pub const Loop = struct { /// resume_node must live longer than the promise that it holds a reference to. /// flags must contain EPOLLET pub fn linuxAddFd(self: *Loop, fd: i32, resume_node: *ResumeNode, flags: u32) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); self.beginOneEvent(); errdefer self.finishOneEvent(); try self.linuxModFd( fd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, flags, resume_node, ); } pub fn linuxModFd(self: *Loop, fd: i32, op: u32, flags: u32, resume_node: *ResumeNode) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); var ev = os.linux.epoll_event{ .events = flags, .data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) }, @@ -440,7 +440,7 @@ pub const Loop = struct { } } - pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !posix.Kevent { + pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent { // TODO #1194 suspend { resume @handle(); @@ -464,30 +464,30 @@ pub const Loop = struct { pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { self.beginOneEvent(); errdefer self.finishOneEvent(); - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_ADD | posix.EV_ENABLE | posix.EV_CLEAR, + .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, .fflags = fflags, .data = 0, .udata = @ptrToInt(&resume_node.base), }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); } pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_DELETE, + .flags = os.EV_DELETE, .fflags = 0, .data = 0, .udata = 0, }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined; self.finishOneEvent(); } @@ -502,8 +502,8 @@ pub const Loop = struct { eventfd_node.base.handle = next_tick_node.data; switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch { self.next_tick_queue.unget(next_tick_node); self.available_eventfd_resume_nodes.push(resume_stack_node); @@ -512,7 +512,7 @@ pub const Loop = struct { }, builtin.Os.linux => { // the pending count is already accounted for - const epoll_events = posix.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | + const epoll_events = os.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | os.linux.EPOLLET; self.linuxModFd( eventfd_node.eventfd, @@ -631,8 +631,8 @@ pub const Loop = struct { }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.posixFsRequest(&self.os_data.fs_end_request); - const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const final_kevent = (*const [1]os.Kevent)(&self.os_data.final_kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; // cannot fail because we already added it and this just enables it _ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable; return; @@ -676,7 +676,7 @@ pub const Loop = struct { ResumeNode.Id.Stop => return, ResumeNode.Id.EventFd => { const event_fd_node = @fieldParentPtr(ResumeNode.EventFd, "base", resume_node); - event_fd_node.epoll_op = posix.EPOLL_CTL_MOD; + event_fd_node.epoll_op = os.EPOLL_CTL_MOD; const stack_node = @fieldParentPtr(std.atomic.Stack(ResumeNode.EventFd).Node, "data", event_fd_node); self.available_eventfd_resume_nodes.push(stack_node); }, @@ -688,8 +688,8 @@ pub const Loop = struct { } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - var eventlist: [1]posix.Kevent = undefined; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + var eventlist: [1]os.Kevent = undefined; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; const count = os.bsdKEvent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable; for (eventlist[0..count]) |ev| { const resume_node = @intToPtr(*ResumeNode, ev.udata); @@ -751,8 +751,8 @@ pub const Loop = struct { self.os_data.fs_queue.put(request_node); switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wake); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable; }, builtin.Os.linux => { @@ -760,7 +760,7 @@ pub const Loop = struct { const rc = os.linux.futex_wake(&self.os_data.fs_queue_item, os.linux.FUTEX_WAKE, 1); switch (os.linux.getErrno(rc)) { 0 => {}, - posix.EINVAL => unreachable, + os.EINVAL => unreachable, else => unreachable, } }, @@ -793,8 +793,8 @@ pub const Loop = struct { }, @TagType(fs.Request.Msg).Close => |*msg| os.close(msg.fd), @TagType(fs.Request.Msg).WriteFile => |*msg| blk: { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | - posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | + os.O_CLOEXEC | os.O_TRUNC; const fd = os.openC(msg.path.ptr, flags, msg.mode) catch |err| { msg.result = err; break :blk; @@ -816,13 +816,13 @@ pub const Loop = struct { builtin.Os.linux => { const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null); switch (os.linux.getErrno(rc)) { - 0, posix.EINTR, posix.EAGAIN => continue, + 0, os.EINTR, os.EAGAIN => continue, else => unreachable, } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait); - var out_kevs: [1]posix.Kevent = undefined; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wait); + var out_kevs: [1]os.Kevent = undefined; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable; }, else => @compileError("Unsupported OS"), @@ -842,10 +842,10 @@ pub const Loop = struct { const KEventData = struct { kqfd: i32, - final_kevent: posix.Kevent, - fs_kevent_wake: posix.Kevent, - fs_kevent_wait: posix.Kevent, - fs_thread: *os.Thread, + final_kevent: os.Kevent, + fs_kevent_wake: os.Kevent, + fs_kevent_wait: os.Kevent, + fs_thread: *Thread, fs_kqfd: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, @@ -855,7 +855,7 @@ pub const Loop = struct { epollfd: i32, final_eventfd: i32, final_eventfd_event: os.linux.epoll_event, - fs_thread: *os.Thread, + fs_thread: *Thread, fs_queue_item: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, diff --git a/std/event/net.zig b/std/event/net.zig index bb492f1715..2346b1eb23 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -4,11 +4,9 @@ const testing = std.testing; const event = std.event; const mem = std.mem; const os = std.os; -const posix = os.posix; const Loop = std.event.Loop; const File = std.fs.File; - -const fd_t = posix.fd_t; +const fd_t = os.fd_t; pub const Server = struct { handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void, @@ -47,19 +45,19 @@ pub const Server = struct { ) !void { self.handleRequestFn = handleRequestFn; - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); self.sockfd = sockfd; try os.posixBind(sockfd, &address.os_addr); - try os.posixListen(sockfd, posix.SOMAXCONN); + try os.posixListen(sockfd, os.SOMAXCONN); self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd)); self.accept_coro = try async Server.handler(self); errdefer cancel self.accept_coro.?; self.listen_resume_node.handle = self.accept_coro.?; - try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); errdefer self.loop.removeFd(sockfd); } @@ -78,7 +76,7 @@ pub const Server = struct { while (true) { var accepted_addr: std.net.Address = undefined; // TODO just inline the following function here and don't expose it as posixAsyncAccept - if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| { + if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { if (accepted_fd == -1) { // would block suspend; // we will get resumed by epoll_wait in the event loop @@ -108,22 +106,22 @@ pub const Server = struct { pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { const sockfd = try os.posixSocket( - posix.AF_UNIX, - posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, + os.AF_UNIX, + os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, 0, ); errdefer os.close(sockfd); - var sock_addr = posix.sockaddr_un{ - .family = posix.AF_UNIX, + var sock_addr = os.sockaddr_un{ + .family = os.AF_UNIX, .path = undefined, }; if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong; mem.copy(u8, sock_addr.path[0..], path); - const size = @intCast(u32, @sizeOf(posix.sa_family_t) + path.len); + const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); try os.posixConnectAsync(sockfd, &sock_addr, size); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return sockfd; @@ -143,50 +141,48 @@ pub const ReadError = error{ /// returns number of bytes read. 0 means EOF. pub async fn read(loop: *std.event.Loop, fd: fd_t, buffer: []u8) ReadError!usize { - const iov = posix.iovec{ + const iov = os.iovec{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec = &iov; + const iovs: *const [1]os.iovec = &iov; return await (async readvPosix(loop, fd, iovs, 1) catch unreachable); } pub const WriteError = error{}; pub async fn write(loop: *std.event.Loop, fd: fd_t, buffer: []const u8) WriteError!void { - const iov = posix.iovec_const{ + const iov = os.iovec_const{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec_const = &iov; + const iovs: *const [1]os.iovec_const = &iov; return await (async writevPosix(loop, fd, iovs, 1) catch unreachable); } -pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, count: usize) !void { +pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const os.iovec_const, count: usize) !void { while (true) { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.linux => { - const rc = posix.writev(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + .macosx, .linux => { + switch (os.errno(os.system.writev(fd, iov, count))) { 0 => return, - posix.EINTR => continue, - posix.ESPIPE => unreachable, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLOUT) catch unreachable); + os.EINTR => continue, + os.ESPIPE => unreachable, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLOUT) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EDESTADDRREQ => unreachable, // connect was never called - posix.EDQUOT => unreachable, - posix.EFBIG => unreachable, - posix.EIO => return error.InputOutput, - posix.ENOSPC => unreachable, - posix.EPERM => return error.AccessDenied, - posix.EPIPE => unreachable, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EDESTADDRREQ => unreachable, // connect was never called + os.EDQUOT => unreachable, + os.EFBIG => unreachable, + os.EIO => return error.InputOutput, + os.ENOSPC => unreachable, + os.EPERM => return error.AccessDenied, + os.EPIPE => unreachable, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -195,27 +191,26 @@ pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, } /// returns number of bytes read. 0 means EOF. -pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, count: usize) !usize { +pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]os.iovec, count: usize) !usize { while (true) { switch (builtin.os) { builtin.Os.linux, builtin.Os.freebsd, builtin.Os.macosx => { - const rc = posix.readv(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.system.readv(fd, iov, count); + switch (os.errno(rc)) { 0 => return rc, - posix.EINTR => continue, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLIN) catch unreachable); + os.EINTR => continue, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLIN) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EIO => return error.InputOutput, - posix.EISDIR => unreachable, - posix.ENOBUFS => return error.SystemResources, - posix.ENOMEM => return error.SystemResources, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EIO => return error.InputOutput, + os.EISDIR => unreachable, + os.ENOBUFS => return error.SystemResources, + os.ENOMEM => return error.SystemResources, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -224,11 +219,11 @@ pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, cou } pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -238,11 +233,11 @@ pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { } pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -254,11 +249,11 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); - try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in)); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return File.openHandle(sockfd); diff --git a/std/fs/path.zig b/std/fs/path.zig index 4c73834609..b82ddc8117 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -9,24 +9,21 @@ const fmt = std.fmt; const Allocator = mem.Allocator; const os = std.os; const math = std.math; -const posix = os.posix; const windows = os.windows; const cstr = std.cstr; pub const sep_windows = '\\'; pub const sep_posix = '/'; -pub const sep = if (is_windows) sep_windows else sep_posix; +pub const sep = if (windows.is_the_target) sep_windows else sep_posix; pub const sep_str = [1]u8{sep}; pub const delimiter_windows = ';'; pub const delimiter_posix = ':'; -pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix; - -const is_windows = builtin.os == builtin.Os.windows; +pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix; pub fn isSep(byte: u8) bool { - if (is_windows) { + if (windows.is_the_target) { return byte == '/' or byte == '\\'; } else { return byte == '/'; @@ -76,7 +73,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u return buf; } -pub const join = if (is_windows) joinWindows else joinPosix; +pub const join = if (windows.is_the_target) joinWindows else joinPosix; /// Naively combines a series of paths with the native path seperator. /// Allocates memory for the result, which must be freed by the caller. @@ -133,7 +130,7 @@ test "join" { } pub fn isAbsolute(path: []const u8) bool { - if (is_windows) { + if (windows.is_the_target) { return isAbsoluteWindows(path); } else { return isAbsolutePosix(path); @@ -312,7 +309,7 @@ test "windowsParsePath" { } pub fn diskDesignator(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return diskDesignatorWindows(path); } else { return ""; @@ -377,7 +374,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool { /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return resolveWindows(allocator, paths); } else { return resolvePosix(allocator, paths); @@ -394,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -489,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { result_disk_designator = result[0..result_index]; }, WindowsPath.Kind.None => { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); @@ -504,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, } } else { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); @@ -575,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -597,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (have_abs) { result = try allocator.alloc(u8, max_size); } else { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -638,7 +635,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { test "resolve" { const cwd = try os.getCwdAlloc(debug.global_allocator); - if (is_windows) { + if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); } @@ -650,7 +647,7 @@ test "resolve" { } test "resolveWindows" { - if (is_windows) { + if (windows.is_the_target) { const cwd = try os.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { @@ -716,7 +713,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 { /// If the path is a file in the current directory (no directory component) /// then returns null pub fn dirname(path: []const u8) ?[]const u8 { - if (is_windows) { + if (windows.is_the_target) { return dirnameWindows(path); } else { return dirnamePosix(path); @@ -848,7 +845,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void { } pub fn basename(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return basenameWindows(path); } else { return basenamePosix(path); @@ -964,7 +961,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void { /// string is returned. /// On Windows this canonicalizes the drive to a capital letter and paths to `\\`. pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return relativeWindows(allocator, from, to); } else { return relativePosix(allocator, from, to); diff --git a/std/heap.zig b/std/heap.zig index 3bbb35e65d..4ff36f3f5f 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -5,7 +5,6 @@ const testing = std.testing; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); -const Os = builtin.Os; const c = std.c; const maxInt = std.math.maxInt; @@ -51,201 +50,190 @@ pub const DirectAllocator = struct { if (n == 0) return (([*]u8)(undefined))[0..0]; - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - const p = os.posix; - const alloc_size = if (alignment <= os.page_size) n else n + alignment; - const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0); - if (addr == p.MAP_FAILED) return error.OutOfMemory; - if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; - - const aligned_addr = mem.alignForward(addr, alignment); - - // Unmap the extra bytes that were only requested in order to guarantee - // that the range of memory we were provided had a proper alignment in - // it somewhere. The extra bytes could be at the beginning, or end, or both. - const unused_start_len = aligned_addr - addr; - if (unused_start_len != 0) { - const err = p.munmap(addr, unused_start_len); - assert(p.getErrno(err) == 0); - } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); - const unused_end_len = addr + alloc_size - aligned_end_addr; - if (unused_end_len != 0) { - const err = p.munmap(aligned_end_addr, unused_end_len); - assert(p.getErrno(err) == 0); - } + if (os.windows.is_the_target) { + const w = os.windows; + + // Although officially it's at least aligned to page boundary, + // Windows is known to reserve pages on a 64K boundary. It's + // even more likely that the requested alignment is <= 64K than + // 4K, so we're just allocating blindly and hoping for the best. + // see https://devblogs.microsoft.com/oldnewthing/?p=42223 + const addr = w.VirtualAlloc( + null, + n, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) catch return error.OutOfMemory; + + // If the allocation is sufficiently aligned, use it. + if (@ptrToInt(addr) & (alignment - 1) == 0) { + return @ptrCast([*]u8, addr)[0..n]; + } - return @intToPtr([*]u8, aligned_addr)[0..n]; - }, - .windows => { - const w = os.windows; - - // Although officially it's at least aligned to page boundary, - // Windows is known to reserve pages on a 64K boundary. It's - // even more likely that the requested alignment is <= 64K than - // 4K, so we're just allocating blindly and hoping for the best. - // see https://devblogs.microsoft.com/oldnewthing/?p=42223 - const addr = w.VirtualAlloc( + // If it wasn't, actually do an explicitely aligned allocation. + w.VirtualFree(addr, 0, w.MEM_RELEASE); + const alloc_size = n + alignment; + + const final_addr = while (true) { + // Reserve a range of memory large enough to find a sufficiently + // aligned address. + const reserved_addr = w.VirtualAlloc( null, + alloc_size, + w.MEM_RESERVE, + w.PAGE_NOACCESS, + ) catch return error.OutOfMemory; + const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); + + // Release the reserved pages (not actually used). + w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE); + + // At this point, it is possible that another thread has + // obtained some memory space that will cause the next + // VirtualAlloc call to fail. To handle this, we will retry + // until it succeeds. + return w.VirtualAlloc( + @intToPtr(*c_void, aligned_addr), n, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, - ) orelse return error.OutOfMemory; + ) catch continue; + }; - // If the allocation is sufficiently aligned, use it. - if (@ptrToInt(addr) & (alignment - 1) == 0) { - return @ptrCast([*]u8, addr)[0..n]; - } + return @ptrCast([*]u8, final_addr)[0..n]; + } - // If it wasn't, actually do an explicitely aligned allocation. - if (w.VirtualFree(addr, 0, w.MEM_RELEASE) == 0) unreachable; - const alloc_size = n + alignment; - - const final_addr = while (true) { - // Reserve a range of memory large enough to find a sufficiently - // aligned address. - const reserved_addr = w.VirtualAlloc( - null, - alloc_size, - w.MEM_RESERVE, - w.PAGE_NOACCESS, - ) orelse return error.OutOfMemory; - const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); - - // Release the reserved pages (not actually used). - if (w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE) == 0) unreachable; - - // At this point, it is possible that another thread has - // obtained some memory space that will cause the next - // VirtualAlloc call to fail. To handle this, we will retry - // until it succeeds. - if (w.VirtualAlloc( - @intToPtr(*c_void, aligned_addr), - n, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - )) |ptr| break ptr; - } else unreachable; // TODO else unreachable should not be necessary - - return @ptrCast([*]u8, final_addr)[0..n]; - }, - else => @compileError("Unsupported OS"), + const alloc_size = if (alignment <= os.page_size) n else n + alignment; + const addr = os.mmap( + null, + alloc_size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch return error.OutOfMemory; + if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; + + const aligned_addr = mem.alignForward(addr, alignment); + + // Unmap the extra bytes that were only requested in order to guarantee + // that the range of memory we were provided had a proper alignment in + // it somewhere. The extra bytes could be at the beginning, or end, or both. + const unused_start_len = aligned_addr - addr; + if (unused_start_len != 0) { + os.munmap(addr, unused_start_len); + } + const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); + const unused_end_len = addr + alloc_size - aligned_end_addr; + if (unused_end_len != 0) { + os.munmap(aligned_end_addr, unused_end_len); } + + return @intToPtr([*]u8, aligned_addr)[0..n]; } fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { + if (os.windows.is_the_target) { + const w = os.windows; + if (new_size == 0) { + // From the docs: + // "If the dwFreeType parameter is MEM_RELEASE, this parameter + // must be 0 (zero). The function frees the entire region that + // is reserved in the initial allocation call to VirtualAlloc." + // So we can only use MEM_RELEASE when actually releasing the + // whole allocation. + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + } else { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); if (old_addr_end > new_addr_end_rounded) { - _ = os.posix.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); - } - return old_mem[0..new_size]; - }, - .windows => { - const w = os.windows; - if (new_size == 0) { - // From the docs: - // "If the dwFreeType parameter is MEM_RELEASE, this parameter - // must be 0 (zero). The function frees the entire region that - // is reserved in the initial allocation call to VirtualAlloc." - // So we can only use MEM_RELEASE when actually releasing the - // whole allocation. - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - } else { - const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (old_addr_end > new_addr_end_rounded) { - // For shrinking that is not releasing, we will only - // decommit the pages not needed anymore. - if (w.VirtualFree( - @intToPtr(*c_void, new_addr_end_rounded), - old_addr_end - new_addr_end_rounded, - w.MEM_DECOMMIT, - ) == 0) unreachable; - } + // For shrinking that is not releasing, we will only + // decommit the pages not needed anymore. + w.VirtualFree( + @intToPtr(*c_void, new_addr_end_rounded), + old_addr_end - new_addr_end_rounded, + w.MEM_DECOMMIT, + ); } - return old_mem[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + return old_mem[0..new_size]; } + const base_addr = @ptrToInt(old_mem.ptr); + const old_addr_end = base_addr + old_mem.len; + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (old_addr_end > new_addr_end_rounded) { + os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); + } + return old_mem[0..new_size]; } fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - const result = try alloc(allocator, new_size, new_align); - if (old_mem.len != 0) { - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len); - } - return result; - }, - .windows => { - if (old_mem.len == 0) { - return alloc(allocator, new_size, new_align); - } - - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - - const w = os.windows; - const base_addr = @ptrToInt(old_mem.ptr); + if (os.windows.is_the_target) { + if (old_mem.len == 0) { + return alloc(allocator, new_size, new_align); + } - if (new_align > old_align and base_addr & (new_align - 1) != 0) { - // Current allocation doesn't satisfy the new alignment. - // For now we'll do a new one no matter what, but maybe - // there is something smarter to do instead. - const result = try alloc(allocator, new_size, new_align); - assert(old_mem.len != 0); - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); + } - return result; - } + const w = os.windows; + const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (new_addr_end_rounded == old_addr_end_rounded) { - // The reallocation fits in the already allocated pages. - return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - } - assert(new_addr_end_rounded > old_addr_end_rounded); + if (new_align > old_align and base_addr & (new_align - 1) != 0) { + // Current allocation doesn't satisfy the new alignment. + // For now we'll do a new one no matter what, but maybe + // there is something smarter to do instead. + const result = try alloc(allocator, new_size, new_align); + assert(old_mem.len != 0); + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); - // We need to commit new pages. - const additional_size = new_addr_end - old_addr_end_rounded; - const realloc_addr = w.VirtualAlloc( - @intToPtr(*c_void, old_addr_end_rounded), - additional_size, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - ) orelse { - // Committing new pages at the end of the existing allocation - // failed, we need to try a new one. - const new_alloc_mem = try alloc(allocator, new_size, new_align); - @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - - return new_alloc_mem; - }; + return result; + } - assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + const old_addr_end = base_addr + old_mem.len; + const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (new_addr_end_rounded == old_addr_end_rounded) { + // The reallocation fits in the already allocated pages. return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + assert(new_addr_end_rounded > old_addr_end_rounded); + + // We need to commit new pages. + const additional_size = new_addr_end - old_addr_end_rounded; + const realloc_addr = w.kernel32.VirtualAlloc( + @intToPtr(*c_void, old_addr_end_rounded), + additional_size, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) orelse { + // Committing new pages at the end of the existing allocation + // failed, we need to try a new one. + const new_alloc_mem = try alloc(allocator, new_size, new_align); + @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + + return new_alloc_mem; + }; + + assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; + } + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); } + const result = try alloc(allocator, new_size, new_align); + if (old_mem.len != 0) { + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + os.munmap(@ptrToInt(old_mem.ptr), old_mem.len); + } + return result; } }; diff --git a/std/mem.zig b/std/mem.zig index 50ed2771ec..4f7ea768d7 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1466,17 +1466,17 @@ test "std.mem.alignForward" { pub fn getBaseAddress() usize { switch (builtin.os) { .linux => { - const base = std.os.posix.getauxval(std.elf.AT_BASE); + const base = std.os.system.getauxval(std.elf.AT_BASE); if (base != 0) { return base; } - const phdr = std.os.posix.getauxval(std.elf.AT_PHDR); + const phdr = std.os.system.getauxval(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); }, .macosx, .freebsd, .netbsd => { return @ptrToInt(&std.c._mh_execute_header); }, - .windows => return @ptrToInt(windows.GetModuleHandleW(null)), + .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)), else => @compileError("Unsupported OS"), } } diff --git a/std/mutex.zig b/std/mutex.zig index 2b3ac4e366..6b8e586ea8 100644 --- a/std/mutex.zig +++ b/std/mutex.zig @@ -152,9 +152,9 @@ test "std.Mutex" { testing.expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, worker); + t.* = try std.Thread.spawn(&context, worker); } for (threads) |t| t.wait(); diff --git a/std/net.zig b/std/net.zig index bb292efd83..b426759701 100644 --- a/std/net.zig +++ b/std/net.zig @@ -2,8 +2,8 @@ const std = @import("std.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; const net = @This(); -const posix = std.os.posix; const mem = std.mem; +const os = std.os; pub const TmpWinAddr = struct { family: u8, @@ -12,7 +12,7 @@ pub const TmpWinAddr = struct { pub const OsAddress = switch (builtin.os) { builtin.Os.windows => TmpWinAddr, - else => posix.sockaddr, + else => os.sockaddr, }; pub const Address = struct { @@ -20,9 +20,9 @@ pub const Address = struct { pub fn initIp4(ip4: u32, _port: u16) Address { return Address{ - .os_addr = posix.sockaddr{ - .in = posix.sockaddr_in{ - .family = posix.AF_INET, + .os_addr = os.sockaddr{ + .in = os.sockaddr_in{ + .family = os.AF_INET, .port = mem.nativeToBig(u16, _port), .addr = ip4, .zero = []u8{0} ** 8, @@ -33,10 +33,10 @@ pub const Address = struct { pub fn initIp6(ip6: *const Ip6Addr, _port: u16) Address { return Address{ - .family = posix.AF_INET6, - .os_addr = posix.sockaddr{ - .in6 = posix.sockaddr_in6{ - .family = posix.AF_INET6, + .family = os.AF_INET6, + .os_addr = os.sockaddr{ + .in6 = os.sockaddr_in6{ + .family = os.AF_INET6, .port = mem.nativeToBig(u16, _port), .flowinfo = 0, .addr = ip6.addr, @@ -50,18 +50,18 @@ pub const Address = struct { return mem.bigToNative(u16, self.os_addr.in.port); } - pub fn initPosix(addr: posix.sockaddr) Address { + pub fn initPosix(addr: os.sockaddr) Address { return Address{ .os_addr = addr }; } pub fn format(self: *const Address, out_stream: var) !void { switch (self.os_addr.in.family) { - posix.AF_INET => { + os.AF_INET => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port); const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]); try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port); }, - posix.AF_INET6 => { + os.AF_INET6 => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port); try out_stream.print("[TODO render ip6 address]:{}", native_endian_port); }, diff --git a/std/os.zig b/std/os.zig index 4cd847cf8f..3b29570fcd 100644 --- a/std/os.zig +++ b/std/os.zig @@ -164,7 +164,7 @@ pub fn raise(sig: u8) RaiseError!void { } if (windows.is_the_target) { - @compileError("TODO implement std.posix.raise for Windows"); + @compileError("TODO implement std.os.raise for Windows"); } var set: system.sigset_t = undefined; @@ -690,20 +690,6 @@ pub fn getenvC(key: [*]const u8) ?[]const u8 { return getenv(mem.toSliceConst(u8, key)); } -/// See std.elf for the constants. -pub fn getauxval(index: usize) usize { - if (builtin.link_libc) { - return usize(system.getauxval(index)); - } else if (linux.elf_aux_maybe) |auxv| { - var i: usize = 0; - while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { - if (auxv[i].a_type == index) - return auxv[i].a_un.a_val; - } - } - return 0; -} - pub const GetCwdError = error{ NameTooLong, CurrentWorkingDirectoryUnlinked, @@ -1198,7 +1184,7 @@ pub fn isatty(handle: fd_t) bool { return windows.kernel32.GetConsoleMode(handle, &out) != 0; } if (wasi.is_the_target) { - @compileError("TODO implement std.os.posix.isatty for WASI"); + @compileError("TODO implement std.os.isatty for WASI"); } if (linux.is_the_target) { var wsz: system.winsize = undefined; @@ -2365,6 +2351,23 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { } } +pub const SchedGetAffinityError = error{ + PermissionDenied, + Unexpected, +}; + +pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { + var set: cpu_set_t = undefined; + switch (errno(system.sched_getaffinity(pid, &set))) { + 0 => return set, + EFAULT => unreachable, + EINVAL => unreachable, + ESRCH => unreachable, + EPERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } +} + /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 7957187896..f40beeaf68 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,3 +1,5 @@ +const std = @import("../../std.zig"); + pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), @@ -907,3 +909,26 @@ pub const pthread_attr_t = extern struct { __size: [56]u8, __align: c_long, }; + +pub const CPU_SETSIZE = 128; +pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize; +pub const cpu_count_t = @IntType(false, std.math.log2(CPU_SETSIZE * 8)); + +pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t { + var sum: cpu_count_t = 0; + for (set) |x| { + sum += @popCount(usize, x); + } + return sum; +} + +// TODO port these over +//#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) +//#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) +//#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) +//#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) +//#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) +//#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) diff --git a/std/os/linux.zig b/std/os/linux.zig index 40e9fa5108..88a2980129 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -1,8 +1,878 @@ +// This file provides the system interface functions for Linux matching those +// that are provided by libc, whether or not libc is linked. The following +// abstractions are made: +// * Work around kernel bugs and limitations. For example, see sendmmsg. +// * Implement all the syscalls in the same way that libc functions will +// provide `rename` when only the `renameat` syscall exists. +// * Does not support POSIX thread cancellation. const std = @import("../std.zig"); const builtin = @import("builtin"); +const assert = std.debug.assert; +const maxInt = std.math.maxInt; +const elf = std.elf; +const vdso = @import("linux/vdso.zig"); +const dl = @import("../dynamic_library.zig"); + pub const is_the_target = builtin.os == .linux; -pub const sys = @import("linux/sys.zig"); -pub use if (builtin.link_libc) std.c else sys; +pub use switch (builtin.arch) { + .x86_64 => @import("linux/x86_64.zig"), + .aarch64 => @import("linux/arm64.zig"), + else => struct {}, +}; +pub use @import("bits.zig"); + +/// Set by startup code, used by `getauxval`. +pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; + +/// See `std.elf` for the constants. +pub fn getauxval(index: usize) usize { + const auxv = elf_aux_maybe orelse return 0; + var i: usize = 0; + while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { + if (auxv[i].a_type == index) + return auxv[i].a_un.a_val; + } + return 0; +} + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) u12 { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; +} + +pub fn dup2(old: i32, new: i32) usize { + if (@hasDecl(@This(), "SYS_dup2")) { + return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); + } else { + if (old == new) { + if (std.debug.runtime_safety) { + const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); + if (@bitCast(isize, rc) < 0) return rc; + } + return @intCast(usize, old); + } else { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); + } + } +} + +pub fn dup3(old: i32, new: i32, flags: u32) usize { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chdir(path: [*]const u8) usize { + return syscall1(SYS_chdir, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chroot(path: [*]const u8) usize { + return syscall1(SYS_chroot, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { + return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); +} + +pub fn fork() usize { + if (@hasDecl(@This(), "SYS_fork")) { + return syscall0(SYS_fork); + } else { + return syscall2(SYS_clone, SIGCHLD, 0); + } +} + +/// This must be inline, and inline call the syscall function, because if the +/// child does a return it will clobber the parent's stack. +/// It is advised to avoid this function and use clone instead, because +/// the compiler is not aware of how vfork affects control flow and you may +/// see different results in optimized builds. +pub inline fn vfork() usize { + return @inlineCall(syscall0, SYS_vfork); +} + +pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { + return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); +} + +pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { + return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); +} + +pub fn getcwd(buf: [*]u8, size: usize) usize { + return syscall2(SYS_getcwd, @ptrToInt(buf), size); +} + +pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn inotify_init1(flags: u32) usize { + return syscall1(SYS_inotify_init1, flags); +} + +pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { + return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); +} + +pub fn inotify_rm_watch(fd: i32, wd: i32) usize { + return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + if (@hasDecl(@This(), "SYS_readlink")) { + return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } else { + return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdir(path: [*]const u8, mode: u32) usize { + if (@hasDecl(@This(), "SYS_mkdir")) { + return syscall2(SYS_mkdir, @ptrToInt(path), mode); + } else { + return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { + return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount(special: [*]const u8) usize { + return syscall2(SYS_umount2, @ptrToInt(special), 0); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount2(special: [*]const u8, flags: u32) usize { + return syscall2(SYS_umount2, @ptrToInt(special), flags); +} + +pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { + return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); +} + +pub fn mprotect(address: usize, length: usize, protection: usize) usize { + return syscall3(SYS_mprotect, address, length, protection); +} + +pub fn munmap(address: usize, length: usize) usize { + return syscall2(SYS_munmap, address, length); +} + +pub fn read(fd: i32, buf: [*]u8, count: usize) usize { + return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { + return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { + return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { + return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { + return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rmdir(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rmdir")) { + return syscall1(SYS_rmdir, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_symlink")) { + return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); + } else { + return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { + return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn access(path: [*]const u8, mode: u32) usize { + return syscall2(SYS_access, @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +pub fn pipe(fd: *[2]i32) usize { + if (@hasDecl(@This(), "SYS_pipe")) { + return syscall1(SYS_pipe, @ptrToInt(fd)); + } else { + return syscall2(SYS_pipe2, @ptrToInt(fd), 0); + } +} + +pub fn pipe2(fd: *[2]i32, flags: u32) usize { + return syscall2(SYS_pipe2, @ptrToInt(fd), flags); +} + +pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { + return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { + return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rename(old: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rename")) { + return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); + } else if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); + } else { + return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); + } +} + +pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4( + SYS_renameat, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + ); + } else { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + 0, + ); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(oldpath), + @bitCast(usize, isize(newfd)), + @ptrToInt(newpath), + flags, + ); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { + return syscall3(SYS_open, @ptrToInt(path), flags, perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn create(path: [*]const u8, perm: usize) usize { + return syscall2(SYS_creat, @ptrToInt(path), perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { + // dirfd could be negative, for example AT_FDCWD is -100 + return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { + return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone2(flags: u32, child_stack_ptr: usize) usize { + return syscall2(SYS_clone, flags, child_stack_ptr); +} + +pub fn close(fd: i32) usize { + return syscall1(SYS_close, @bitCast(usize, isize(fd))); +} + +/// Can only be called on 32 bit systems. For 64 bit see `lseek`. +pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { + return syscall5( + SYS__llseek, + @bitCast(usize, isize(fd)), + @truncate(usize, offset >> 32), + @truncate(usize, offset), + @ptrToInt(result), + whence, + ); +} + +/// Can only be called on 64 bit systems. For 32 bit see `llseek`. +pub fn lseek(fd: i32, offset: i64, whence: usize) usize { + return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); +} + +pub fn exit(status: i32) noreturn { + _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn exit_group(status: i32) noreturn { + _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { + return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); +} + +pub fn kill(pid: i32, sig: i32) usize { + return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlink(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_unlink")) { + return syscall1(SYS_unlink, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { + return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); +} + +pub fn waitpid(pid: i32, status: *i32, options: i32) usize { + return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); +} + +var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); + +// We must follow the C calling convention when we call into the VDSO +const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; + +pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { + if (VDSO_CGT_SYM.len != 0) { + const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + const rc = f(clk_id, tp); + switch (rc) { + 0, @bitCast(usize, isize(-EINVAL)) => return rc, + else => {}, + } + } + } + return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { + const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); + // Note that we may not have a VDSO at all, update the stub address anyway + // so that clock_gettime will fall back on the good old (and slow) syscall + _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); + // Call into the VDSO if available + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + return f(clk, ts); + } + return @bitCast(usize, isize(-ENOSYS)); +} + +pub fn clock_getres(clk_id: i32, tp: *timespec) usize { + return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { + return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { + return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { + return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { + return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); +} + +pub fn setuid(uid: u32) usize { + return syscall1(SYS_setuid, uid); +} + +pub fn setgid(gid: u32) usize { + return syscall1(SYS_setgid, gid); +} + +pub fn setreuid(ruid: u32, euid: u32) usize { + return syscall2(SYS_setreuid, ruid, euid); +} + +pub fn setregid(rgid: u32, egid: u32) usize { + return syscall2(SYS_setregid, rgid, egid); +} + +pub fn getuid() u32 { + return u32(syscall0(SYS_getuid)); +} + +pub fn getgid() u32 { + return u32(syscall0(SYS_getgid)); +} + +pub fn geteuid() u32 { + return u32(syscall0(SYS_geteuid)); +} + +pub fn getegid() u32 { + return u32(syscall0(SYS_getegid)); +} + +pub fn seteuid(euid: u32) usize { + return syscall1(SYS_seteuid, euid); +} + +pub fn setegid(egid: u32) usize { + return syscall1(SYS_setegid, egid); +} + +pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { + return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); +} + +pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { + return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); +} + +pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { + return syscall3(SYS_setresuid, ruid, euid, suid); +} + +pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { + return syscall3(SYS_setresgid, rgid, egid, sgid); +} + +pub fn getgroups(size: usize, list: *u32) usize { + return syscall2(SYS_getgroups, size, @ptrToInt(list)); +} + +pub fn setgroups(size: usize, list: *const u32) usize { + return syscall2(SYS_setgroups, size, @ptrToInt(list)); +} + +pub fn getpid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); +} + +pub fn gettid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); +} + +pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { + return syscall4(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, restore_rt), + }; + var ksa_old: k_sigaction = undefined; + @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); + const result = syscall4(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; +} + +fn blockAllSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); +} + +fn blockAppSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); +} + +fn restoreSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); +} + +pub fn sigaddset(set: *sigset_t, sig: u6) void { + const s = sig - 1; + (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); +} + +pub fn sigismember(set: *const sigset_t, sig: u6) bool { + const s = sig - 1; + return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; +} + +pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { + return syscall3(SYS_socket, domain, socket_type, protocol); +} + +pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { + return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); +} + +pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { + return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); +} + +pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { + return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); +} + +pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { + if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { + // workaround kernel brokenness: + // if adding up all iov_len overflows a i32 then split into multiple calls + // see https://www.openwall.com/lists/musl/2014/06/07/5 + const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel + var next_unsent: usize = 0; + for (msgvec[0..kvlen]) |*msg, i| { + var size: i32 = 0; + const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned + for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { + if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { + // batch-send all messages up to the current message + if (next_unsent < i) { + const batch_size = i - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return next_unsent; + if (r < batch_size) return next_unsent + r; + } + // send current message as own packet + const r = sendmsg(fd, &msg.msg_hdr, flags); + if (getErrno(r) != 0) return r; + // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. + msg.msg_len = @intCast(u32, r); + next_unsent = i + 1; + break; + } + } + } + if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) + const batch_size = kvlen - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return r; + return next_unsent + r; + } + return kvlen; + } + return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); +} + +pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { + return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); +} + +pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { + return syscall3(SYS_recvmsg, @bitCast(usize, isize(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 syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); +} + +pub fn shutdown(fd: i32, how: i32) usize { + return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); +} + +pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { + return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); +} + +pub fn listen(fd: i32, backlog: u32) usize { + return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); +} + +pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { + return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { + return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(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 syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); +} + +pub fn fstat(fd: i32, stat_buf: *Stat) usize { + return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { + return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { + return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fremovexattr(fd: usize, name: [*]const u8) usize { + return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); +} + +pub fn sched_getaffinity(pid: i32, size: usize, set: *cpu_set_t) usize { + const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), size, @ptrToInt(set)); + if (@bitCast(isize, rc) < 0) return rc; + if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc); + return 0; +} + +pub fn epoll_create() usize { + return epoll_create1(0); +} + +pub fn epoll_create1(flags: usize) usize { + return syscall1(SYS_epoll_create1, flags); +} + +pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { + return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); +} + +pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { + return syscall4( + SYS_epoll_wait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + maxevents, + @bitCast(usize, isize(timeout)), + ); +} + +pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { + return syscall6( + SYS_epoll_pwait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + @intCast(usize, maxevents), + @bitCast(usize, isize(timeout)), + @ptrToInt(sigmask), + @sizeOf(sigset_t), + ); +} + +pub fn eventfd(count: u32, flags: u32) usize { + return syscall2(SYS_eventfd2, count, flags); +} + +pub fn timerfd_create(clockid: i32, flags: u32) usize { + return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); +} + +pub const itimerspec = extern struct { + it_interval: timespec, + it_value: timespec, +}; + +pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { + return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); +} + +pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { + return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); +} + +pub fn unshare(flags: usize) usize { + return syscall1(SYS_unshare, flags); +} + +pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { + return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { + return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +// XXX: This should be weak +extern const __ehdr_start: elf.Ehdr = undefined; + +pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { + if (builtin.link_libc) { + return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); + } + + const elf_base = @ptrToInt(&__ehdr_start); + const n_phdr = __ehdr_start.e_phnum; + const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; + + var it = dl.linkmap_iterator(phdrs) catch return 0; + + // The executable has no dynamic link segment, create a single entry for + // the whole ELF image + if (it.end()) { + var info = dl_phdr_info{ + .dlpi_addr = elf_base, + .dlpi_name = c"/proc/self/exe", + .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), + .dlpi_phnum = __ehdr_start.e_phnum, + }; + + return callback(&info, @sizeOf(dl_phdr_info), data); + } + + // Last return value from the callback function + var last_r: isize = 0; + while (it.next()) |entry| { + var dlpi_phdr: usize = undefined; + var dlpi_phnum: u16 = undefined; + + if (entry.l_addr != 0) { + const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); + dlpi_phdr = entry.l_addr + elf_header.e_phoff; + dlpi_phnum = elf_header.e_phnum; + } else { + // This is the running ELF image + dlpi_phdr = elf_base + __ehdr_start.e_phoff; + dlpi_phnum = __ehdr_start.e_phnum; + } + + var info = dl_phdr_info{ + .dlpi_addr = entry.l_addr, + .dlpi_name = entry.l_name, + .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), + .dlpi_phnum = dlpi_phnum, + }; + + last_r = callback(&info, @sizeOf(dl_phdr_info), data); + if (last_r != 0) break; + } + + return last_r; +} test "" { if (is_the_target) { diff --git a/std/os/linux/sys.zig b/std/os/linux/sys.zig deleted file mode 100644 index 272124f06c..0000000000 --- a/std/os/linux/sys.zig +++ /dev/null @@ -1,859 +0,0 @@ -// This file provides the system interface functions for Linux matching those -// that are provided by libc, whether or not libc is linked. The following -// abstractions are made: -// * Work around kernel bugs and limitations. For example, see sendmmsg. -// * Implement all the syscalls in the same way that libc functions will -// provide `rename` when only the `renameat` syscall exists. -// * Does not support POSIX thread cancellation. -const std = @import("../../std.zig"); -const builtin = @import("builtin"); -const assert = std.debug.assert; -const maxInt = std.math.maxInt; -const elf = std.elf; -const vdso = @import("vdso.zig"); -const dl = @import("../../dynamic_library.zig"); -pub use switch (builtin.arch) { - .x86_64 => @import("x86_64.zig"), - .aarch64 => @import("arm64.zig"), - else => struct {}, -}; -pub use @import("../bits.zig"); - -/// See `std.os.posix.getauxval`. -pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) u12 { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - if (@hasDecl(@This(), "SYS_dup2")) { - return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); - } else { - if (old == new) { - if (std.debug.runtime_safety) { - const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); - if (@bitCast(isize, rc) < 0) return rc; - } - return @intCast(usize, old); - } else { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); - } - } -} - -pub fn dup3(old: i32, new: i32, flags: u32) usize { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chdir(path: [*]const u8) usize { - return syscall1(SYS_chdir, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chroot(path: [*]const u8) usize { - return syscall1(SYS_chroot, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); -} - -pub fn fork() usize { - if (@hasDecl(@This(), "SYS_fork")) { - return syscall0(SYS_fork); - } else { - return syscall2(SYS_clone, SIGCHLD, 0); - } -} - -/// This must be inline, and inline call the syscall function, because if the -/// child does a return it will clobber the parent's stack. -/// It is advised to avoid this function and use clone instead, because -/// the compiler is not aware of how vfork affects control flow and you may -/// see different results in optimized builds. -pub inline fn vfork() usize { - return @inlineCall(syscall0, SYS_vfork); -} - -pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { - return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); -} - -pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { - return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return syscall2(SYS_getcwd, @ptrToInt(buf), size); -} - -pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn inotify_init1(flags: u32) usize { - return syscall1(SYS_inotify_init1, flags); -} - -pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { - return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); -} - -pub fn inotify_rm_watch(fd: i32, wd: i32) usize { - return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - if (@hasDecl(@This(), "SYS_readlink")) { - return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } else { - return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdir(path: [*]const u8, mode: u32) usize { - if (@hasDecl(@This(), "SYS_mkdir")) { - return syscall2(SYS_mkdir, @ptrToInt(path), mode); - } else { - return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { - return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount(special: [*]const u8) usize { - return syscall2(SYS_umount2, @ptrToInt(special), 0); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount2(special: [*]const u8, flags: u32) usize { - return syscall2(SYS_umount2, @ptrToInt(special), flags); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); -} - -pub fn mprotect(address: usize, length: usize, protection: usize) usize { - return syscall3(SYS_mprotect, address, length, protection); -} - -pub fn munmap(address: usize, length: usize) usize { - return syscall2(SYS_munmap, address, length); -} - -pub fn read(fd: i32, buf: [*]u8, count: usize) usize { - return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { - return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { - return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { - return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { - return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rmdir(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rmdir")) { - return syscall1(SYS_rmdir, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_symlink")) { - return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); - } else { - return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { - return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn access(path: [*]const u8, mode: u32) usize { - return syscall2(SYS_access, @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -pub fn pipe(fd: *[2]i32) usize { - if (@hasDecl(@This(), "SYS_pipe")) { - return syscall1(SYS_pipe, @ptrToInt(fd)); - } else { - return syscall2(SYS_pipe2, @ptrToInt(fd), 0); - } -} - -pub fn pipe2(fd: *[2]i32, flags: u32) usize { - return syscall2(SYS_pipe2, @ptrToInt(fd), flags); -} - -pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { - return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { - return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rename")) { - return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); - } else if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); - } else { - return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); - } -} - -pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4( - SYS_renameat, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - ); - } else { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - 0, - ); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(oldpath), - @bitCast(usize, isize(newfd)), - @ptrToInt(newpath), - flags, - ); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { - return syscall3(SYS_open, @ptrToInt(path), flags, perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn create(path: [*]const u8, perm: usize) usize { - return syscall2(SYS_creat, @ptrToInt(path), perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { - // dirfd could be negative, for example AT_FDCWD is -100 - return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { - return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone2(flags: u32, child_stack_ptr: usize) usize { - return syscall2(SYS_clone, flags, child_stack_ptr); -} - -pub fn close(fd: i32) usize { - return syscall1(SYS_close, @bitCast(usize, isize(fd))); -} - -/// Can only be called on 32 bit systems. For 64 bit see `lseek`. -pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { - return syscall5( - SYS__llseek, - @bitCast(usize, isize(fd)), - @truncate(usize, offset >> 32), - @truncate(usize, offset), - @ptrToInt(result), - whence, - ); -} - -/// Can only be called on 64 bit systems. For 32 bit see `llseek`. -pub fn lseek(fd: i32, offset: i64, whence: usize) usize { - return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); -} - -pub fn exit(status: i32) noreturn { - _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn exit_group(status: i32) noreturn { - _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { - return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); -} - -pub fn kill(pid: i32, sig: i32) usize { - return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlink(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_unlink")) { - return syscall1(SYS_unlink, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { - return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); -} - -pub fn waitpid(pid: i32, status: *i32, options: i32) usize { - return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); -} - -var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); - -// We must follow the C calling convention when we call into the VDSO -const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; - -pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { - if (VDSO_CGT_SYM.len != 0) { - const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - const rc = f(clk_id, tp); - switch (rc) { - 0, @bitCast(usize, isize(-EINVAL)) => return rc, - else => {}, - } - } - } - return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { - const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); - // Note that we may not have a VDSO at all, update the stub address anyway - // so that clock_gettime will fall back on the good old (and slow) syscall - _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); - // Call into the VDSO if available - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - return f(clk, ts); - } - return @bitCast(usize, isize(-ENOSYS)); -} - -pub fn clock_getres(clk_id: i32, tp: *timespec) usize { - return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { - return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { - return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { - return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); -} - -pub fn setuid(uid: u32) usize { - return syscall1(SYS_setuid, uid); -} - -pub fn setgid(gid: u32) usize { - return syscall1(SYS_setgid, gid); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return syscall2(SYS_setreuid, ruid, euid); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return syscall2(SYS_setregid, rgid, egid); -} - -pub fn getuid() u32 { - return u32(syscall0(SYS_getuid)); -} - -pub fn getgid() u32 { - return u32(syscall0(SYS_getgid)); -} - -pub fn geteuid() u32 { - return u32(syscall0(SYS_geteuid)); -} - -pub fn getegid() u32 { - return u32(syscall0(SYS_getegid)); -} - -pub fn seteuid(euid: u32) usize { - return syscall1(SYS_seteuid, euid); -} - -pub fn setegid(egid: u32) usize { - return syscall1(SYS_setegid, egid); -} - -pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { - return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); -} - -pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { - return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); -} - -pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { - return syscall3(SYS_setresuid, ruid, euid, suid); -} - -pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { - return syscall3(SYS_setresgid, rgid, egid, sgid); -} - -pub fn getgroups(size: usize, list: *u32) usize { - return syscall2(SYS_getgroups, size, @ptrToInt(list)); -} - -pub fn setgroups(size: usize, list: *const u32) usize { - return syscall2(SYS_setgroups, size, @ptrToInt(list)); -} - -pub fn getpid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); -} - -pub fn gettid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); -} - -pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { - return syscall4(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, restore_rt), - }; - var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); - const result = syscall4(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; -} - -fn blockAllSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); -} - -fn blockAppSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); -} - -fn restoreSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); -} - -pub fn sigaddset(set: *sigset_t, sig: u6) void { - const s = sig - 1; - (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); -} - -pub fn sigismember(set: *const sigset_t, sig: u6) bool { - const s = sig - 1; - return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; -} - -pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { - return syscall3(SYS_socket, domain, socket_type, protocol); -} - -pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { - return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); -} - -pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { - return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); -} - -pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { - return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); -} - -pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { - if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { - // workaround kernel brokenness: - // if adding up all iov_len overflows a i32 then split into multiple calls - // see https://www.openwall.com/lists/musl/2014/06/07/5 - const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel - var next_unsent: usize = 0; - for (msgvec[0..kvlen]) |*msg, i| { - var size: i32 = 0; - const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned - for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { - if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { - // batch-send all messages up to the current message - if (next_unsent < i) { - const batch_size = i - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return next_unsent; - if (r < batch_size) return next_unsent + r; - } - // send current message as own packet - const r = sendmsg(fd, &msg.msg_hdr, flags); - if (getErrno(r) != 0) return r; - // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. - msg.msg_len = @intCast(u32, r); - next_unsent = i + 1; - break; - } - } - } - if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) - const batch_size = kvlen - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return r; - return next_unsent + r; - } - return kvlen; - } - return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); -} - -pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { - return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); -} - -pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { - return syscall3(SYS_recvmsg, @bitCast(usize, isize(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 syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); -} - -pub fn shutdown(fd: i32, how: i32) usize { - return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); -} - -pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { - return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); -} - -pub fn listen(fd: i32, backlog: u32) usize { - return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); -} - -pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { - return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { - return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(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 syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); -} - -pub fn fstat(fd: i32, stat_buf: *Stat) usize { - return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { - return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { - return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fremovexattr(fd: usize, name: [*]const u8) usize { - return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); -} - -pub fn sched_getaffinity(pid: i32, set: []usize) usize { - return syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), set.len * @sizeOf(usize), @ptrToInt(set.ptr)); -} - -pub fn epoll_create() usize { - return epoll_create1(0); -} - -pub fn epoll_create1(flags: usize) usize { - return syscall1(SYS_epoll_create1, flags); -} - -pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { - return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); -} - -pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { - return syscall4( - SYS_epoll_wait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - maxevents, - @bitCast(usize, isize(timeout)), - ); -} - -pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { - return syscall6( - SYS_epoll_pwait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - @intCast(usize, maxevents), - @bitCast(usize, isize(timeout)), - @ptrToInt(sigmask), - @sizeOf(sigset_t), - ); -} - -pub fn eventfd(count: u32, flags: u32) usize { - return syscall2(SYS_eventfd2, count, flags); -} - -pub fn timerfd_create(clockid: i32, flags: u32) usize { - return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); -} - -pub const itimerspec = extern struct { - it_interval: timespec, - it_value: timespec, -}; - -pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { - return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); -} - -pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { - return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); -} - -pub fn unshare(flags: usize) usize { - return syscall1(SYS_unshare, flags); -} - -pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { - return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { - return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -// XXX: This should be weak -extern const __ehdr_start: elf.Ehdr = undefined; - -pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { - if (builtin.link_libc) { - return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); - } - - const elf_base = @ptrToInt(&__ehdr_start); - const n_phdr = __ehdr_start.e_phnum; - const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; - - var it = dl.linkmap_iterator(phdrs) catch return 0; - - // The executable has no dynamic link segment, create a single entry for - // the whole ELF image - if (it.end()) { - var info = dl_phdr_info{ - .dlpi_addr = elf_base, - .dlpi_name = c"/proc/self/exe", - .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), - .dlpi_phnum = __ehdr_start.e_phnum, - }; - - return callback(&info, @sizeOf(dl_phdr_info), data); - } - - // Last return value from the callback function - var last_r: isize = 0; - while (it.next()) |entry| { - var dlpi_phdr: usize = undefined; - var dlpi_phnum: u16 = undefined; - - if (entry.l_addr != 0) { - const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); - dlpi_phdr = entry.l_addr + elf_header.e_phoff; - dlpi_phnum = elf_header.e_phnum; - } else { - // This is the running ELF image - dlpi_phdr = elf_base + __ehdr_start.e_phoff; - dlpi_phnum = __ehdr_start.e_phnum; - } - - var info = dl_phdr_info{ - .dlpi_addr = entry.l_addr, - .dlpi_name = entry.l_name, - .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), - .dlpi_phnum = dlpi_phnum, - }; - - last_r = callback(&info, @sizeOf(dl_phdr_info), data); - if (last_r != 0) break; - } - - return last_r; -} diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index e040a45bc0..f06ed144b9 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -1,6 +1,6 @@ const std = @import("std"); +const os = std.os; const mem = std.mem; -const posix = std.os.posix; const elf = std.elf; const builtin = @import("builtin"); const assert = std.debug.assert; @@ -237,9 +237,14 @@ pub fn allocateTLS(size: usize) usize { return @ptrToInt(&main_thread_tls_buffer); } - const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0); - - if (posix.getErrno(addr) != 0) @panic("out of memory"); + const addr = os.mmap( + null, + size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch @panic("out of memory"); return addr; } diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig index 9909b6ee3f..fa866cff4c 100644 --- a/std/os/linux/x86_64.zig +++ b/std/os/linux/x86_64.zig @@ -1,4 +1,4 @@ -use @import("posix/x86_64.zig"); +use @import("../bits.zig"); pub fn syscall0(number: usize) usize { return asm volatile ("syscall" diff --git a/std/os/test.zig b/std/os/test.zig index 54371e94ab..d4bdb06278 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -5,6 +5,7 @@ const expect = std.testing.expect; const io = std.io; const mem = std.mem; const File = std.fs.File; +const Thread = std.Thread; const a = std.debug.global_allocator; @@ -37,25 +38,25 @@ test "access file" { try os.deleteTree(a, "os_test_tmp"); } -fn testThreadIdFn(thread_id: *os.Thread.Id) void { - thread_id.* = os.Thread.getCurrentId(); +fn testThreadIdFn(thread_id: *Thread.Id) void { + thread_id.* = Thread.getCurrentId(); } -test "std.os.Thread.getCurrentId" { +test "std.Thread.getCurrentId" { if (builtin.single_threaded) return error.SkipZigTest; - var thread_current_id: os.Thread.Id = undefined; - const thread = try os.spawnThread(&thread_current_id, testThreadIdFn); + var thread_current_id: Thread.Id = undefined; + const thread = try Thread.spawn(&thread_current_id, testThreadIdFn); const thread_id = thread.handle(); thread.wait(); - if (os.Thread.use_pthreads) { + if (Thread.use_pthreads) { expect(thread_current_id == thread_id); } else { switch (builtin.os) { - builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id), + builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id), else => { // If the thread completes very quickly, then thread_id can be 0. See the - // documentation comments for `std.os.Thread.handle`. + // documentation comments for `std.Thread.handle`. expect(thread_id == 0 or thread_current_id == thread_id); }, } @@ -67,10 +68,10 @@ test "spawn threads" { var shared_ctx: i32 = 1; - const thread1 = try std.os.spawnThread({}, start1); - const thread2 = try std.os.spawnThread(&shared_ctx, start2); - const thread3 = try std.os.spawnThread(&shared_ctx, start2); - const thread4 = try std.os.spawnThread(&shared_ctx, start2); + const thread1 = try Thread.spawn({}, start1); + const thread2 = try Thread.spawn(&shared_ctx, start2); + const thread3 = try Thread.spawn(&shared_ctx, start2); + const thread4 = try Thread.spawn(&shared_ctx, start2); thread1.wait(); thread2.wait(); @@ -116,8 +117,8 @@ test "AtomicFile" { test "thread local storage" { if (builtin.single_threaded) return error.SkipZigTest; - const thread1 = try std.os.spawnThread({}, testTls); - const thread2 = try std.os.spawnThread({}, testTls); + const thread1 = try Thread.spawn({}, testTls); + const thread2 = try Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); diff --git a/std/os/windows.zig b/std/os/windows.zig index e35d8bbbd2..c18adc66fa 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -591,7 +591,7 @@ pub fn CreateFileW( ERROR.ACCESS_DENIED => return error.AccessDenied, ERROR.PIPE_BUSY => return error.PipeBusy, ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, - else => |err| return unexpectedErrorWindows(err), + else => |err| return unexpectedError(err), } } @@ -1089,6 +1089,20 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError } } +pub const VirtualAllocError = error{Unexpected}; + +pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID { + return kernel32.VirtualAlloc(addr, size, alloc_type, flProtect) orelse { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + }; +} + +pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void { + assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/process.zig b/std/process.zig index f2f1dbf4fd..1e29170e88 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,13 +1,13 @@ const std = @import("std.zig"); -const posix = std.os.posix; +const os = std.os; const BufMap = std.BufMap; const mem = std.mem; const Allocator = mem.Allocator; const assert = std.debug.assert; const testing = std.testing; -pub const abort = posix.abort; -pub const exit = posix.exit; +pub const abort = os.abort; +pub const exit = os.exit; /// Caller must free result when done. /// TODO make this go through libc when we have it @@ -42,13 +42,13 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { try result.setMove(key, value); } - } else if (builtin.os == Os.wasi) { + } else if (builtin.os == .wasi) { var environ_count: usize = undefined; var environ_buf_size: usize = undefined; - const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); + const environ_sizes_get_ret = os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); if (environ_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_sizes_get_ret); + return os.unexpectedErrno(environ_sizes_get_ret); } // TODO: Verify that the documentation is incorrect @@ -58,9 +58,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size); defer allocator.free(environ_buf); - const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr); + const environ_get_ret = os.wasi.environ_get(environ.ptr, environ_buf.ptr); if (environ_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_get_ret); + return os.unexpectedErrno(environ_get_ret); } for (environ) |env| { @@ -74,7 +74,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { } return result; } else { - for (posix.environ) |ptr| { + for (os.environ) |ptr| { var line_i: usize = 0; while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const key = ptr[0..line_i]; @@ -331,12 +331,12 @@ pub const ArgIteratorWindows = struct { }; pub const ArgIterator = struct { - const InnerType = if (builtin.os == Os.windows) ArgIteratorWindows else ArgIteratorPosix; + const InnerType = if (builtin.os == .windows) ArgIteratorWindows else ArgIteratorPosix; inner: InnerType, pub fn init() ArgIterator { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { // TODO: Figure out a compatible interface accomodating WASI @compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead."); } @@ -348,7 +348,7 @@ pub const ArgIterator = struct { /// You must free the returned memory when done. pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) { - if (builtin.os == Os.windows) { + if (builtin.os == .windows) { return self.inner.next(allocator); } else { return mem.dupe(allocator, u8, self.inner.next() orelse return null); @@ -373,13 +373,13 @@ pub fn args() ArgIterator { /// Caller must call argsFree on result. pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { var count: usize = undefined; var buf_size: usize = undefined; const args_sizes_get_ret = os.wasi.args_sizes_get(&count, &buf_size); if (args_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_sizes_get_ret); + return os.unexpectedErrno(args_sizes_get_ret); } var argv = try allocator.alloc([*]u8, count); @@ -388,7 +388,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { var argv_buf = try allocator.alloc(u8, buf_size); const args_get_ret = os.wasi.args_get(argv.ptr, argv_buf.ptr); if (args_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_get_ret); + return os.unexpectedErrno(args_get_ret); } var result_slice = try allocator.alloc([]u8, count); @@ -438,7 +438,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { } pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { const last_item = args_alloc[args_alloc.len - 1]; const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated const first_item_ptr = args_alloc[0].ptr; @@ -491,7 +491,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) !UserInfo { return switch (builtin.os) { - .linux, .macosx, .ios, .freebsd, .netbsd => posixGetUserInfo(name), + .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index c6630a594c..86228acb9f 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -92,14 +92,14 @@ fn posixCallMainAndExit() noreturn { } } - std.os.posix.exit(callMainWithArgs(argc, argv, envp)); + std.os.exit(callMainWithArgs(argc, argv, envp)); } // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { std.os.ArgIteratorPosix.raw = argv[0..argc]; - std.os.posix.environ = envp; + std.os.environ = envp; return callMain(); } diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index cfcaf036d3..9c59fccefe 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -96,9 +96,9 @@ test "std.StaticallyInitializedMutex" { expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, TestContext.worker); + t.* = try std.Thread.spawn(&context, TestContext.worker); } for (threads) |t| t.wait(); diff --git a/std/thread.zig b/std/thread.zig index 729d4aff60..eac6b73f7d 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -1,6 +1,8 @@ const builtin = @import("builtin"); const std = @import("std.zig"); +const os = std.os; const windows = std.os.windows; +const c = std.c; pub const Thread = struct { data: Data, @@ -13,8 +15,8 @@ pub const Thread = struct { pub const Handle = if (use_pthreads) c.pthread_t else switch (builtin.os) { - builtin.Os.linux => i32, - builtin.Os.windows => windows.HANDLE, + .linux => i32, + .windows => windows.HANDLE, else => @compileError("Unsupported OS"), }; @@ -22,7 +24,7 @@ pub const Thread = struct { /// May be an integer or pointer depending on the platform. /// On Linux and POSIX, this is the same as Handle. pub const Id = switch (builtin.os) { - builtin.Os.windows => windows.DWORD, + .windows => windows.DWORD, else => Handle, }; @@ -33,12 +35,12 @@ pub const Thread = struct { mmap_len: usize, } else switch (builtin.os) { - builtin.Os.linux => struct { + .linux => struct { handle: Thread.Handle, mmap_addr: usize, mmap_len: usize, }, - builtin.Os.windows => struct { + .windows => struct { handle: Thread.Handle, alloc_start: *c_void, heap_handle: windows.HANDLE, @@ -54,8 +56,8 @@ pub const Thread = struct { return c.pthread_self(); } else return switch (builtin.os) { - builtin.Os.linux => linux.gettid(), - builtin.Os.windows => windows.GetCurrentThreadId(), + .linux => linux.gettid(), + .windows => windows.GetCurrentThreadId(), else => @compileError("Unsupported OS"), }; } @@ -75,28 +77,28 @@ pub const Thread = struct { const err = c.pthread_join(self.data.handle, null); switch (err) { 0 => {}, - posix.EINVAL => unreachable, - posix.ESRCH => unreachable, - posix.EDEADLK => unreachable, + os.EINVAL => unreachable, + os.ESRCH => unreachable, + os.EDEADLK => unreachable, else => unreachable, } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); } else switch (builtin.os) { - builtin.Os.linux => { + .linux => { while (true) { const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); switch (linux.getErrno(rc)) { 0 => continue, - posix.EINTR => continue, - posix.EAGAIN => continue, + os.EINTR => continue, + os.EAGAIN => continue, else => unreachable, } } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); }, - builtin.Os.windows => { + .windows => { assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0); assert(windows.CloseHandle(self.data.handle) != 0); assert(windows.HeapFree(self.data.heap_handle, 0, self.data.alloc_start) != 0); @@ -153,10 +155,10 @@ pub const Thread = struct { extern fn threadMain(raw_arg: windows.LPVOID) windows.DWORD { const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -196,10 +198,10 @@ pub const Thread = struct { const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -217,7 +219,7 @@ pub const Thread = struct { } }; - const MAP_GROWSDOWN = if (builtin.os == builtin.Os.linux) linux.MAP_GROWSDOWN else 0; + const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0; var stack_end_offset: usize = undefined; var thread_start_offset: usize = undefined; @@ -247,9 +249,8 @@ pub const Thread = struct { } break :blk l; }; - const mmap_addr = posix.mmap(null, mmap_len, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); - if (mmap_addr == posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(posix.munmap(mmap_addr, mmap_len) == 0); + const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); + errdefer os.munmap(mmap_addr, mmap_len); const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset)); thread_ptr.data.mmap_addr = mmap_addr; @@ -273,31 +274,30 @@ pub const Thread = struct { const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg)); switch (err) { 0 => return thread_ptr, - posix.EAGAIN => return error.SystemResources, - posix.EPERM => unreachable, - posix.EINVAL => unreachable, - else => return unexpectedErrorPosix(@intCast(usize, err)), + os.EAGAIN => return error.SystemResources, + os.EPERM => unreachable, + os.EINVAL => unreachable, + else => return os.unexpectedErrno(@intCast(usize, err)), } - } else if (builtin.os == builtin.Os.linux) { - var flags: u32 = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND | - posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | - posix.CLONE_DETACHED; + } else if (builtin.os == .linux) { + var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND | + os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID | + os.CLONE_DETACHED; var newtls: usize = undefined; if (linux.tls.tls_image) |tls_img| { newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset); - flags |= posix.CLONE_SETTLS; + flags |= os.CLONE_SETTLS; } - const rc = posix.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); + switch (os.errno(rc)) { 0 => return thread_ptr, - posix.EAGAIN => return error.ThreadQuotaExceeded, - posix.EINVAL => unreachable, - posix.ENOMEM => return error.SystemResources, - posix.ENOSPC => unreachable, - posix.EPERM => unreachable, - posix.EUSERS => unreachable, - else => return unexpectedErrorPosix(err), + os.EAGAIN => return error.ThreadQuotaExceeded, + os.EINVAL => unreachable, + os.ENOMEM => return error.SystemResources, + os.ENOSPC => unreachable, + os.EPERM => unreachable, + os.EUSERS => unreachable, + else => |err| return os.unexpectedErrno(err), } } else { @compileError("Unsupported OS"); @@ -310,56 +310,20 @@ pub const Thread = struct { Unexpected, }; - pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize { - switch (builtin.os) { - .macosx, .freebsd, .netbsd => { - var count: c_int = undefined; - var count_len: usize = @sizeOf(c_int); - const name = switch (builtin.os) { - builtin.Os.macosx => c"hw.logicalcpu", - else => c"hw.ncpu", - }; - try posix.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); - return @intCast(usize, count); - }, - .linux => { - const usize_count = 16; - const allocator = std.heap.stackFallback(usize_count * @sizeOf(usize), fallback_allocator).get(); - - var set = try allocator.alloc(usize, usize_count); - defer allocator.free(set); - - while (true) { - const rc = posix.sched_getaffinity(0, set); - const err = posix.getErrno(rc); - switch (err) { - 0 => { - if (rc < set.len * @sizeOf(usize)) { - const result = set[0 .. rc / @sizeOf(usize)]; - var sum: usize = 0; - for (result) |x| { - sum += @popCount(usize, x); - } - return sum; - } else { - set = try allocator.realloc(set, set.len * 2); - continue; - } - }, - posix.EFAULT => unreachable, - posix.EINVAL => unreachable, - posix.EPERM => return CpuCountError.PermissionDenied, - posix.ESRCH => unreachable, - else => return os.unexpectedErrorPosix(err), - } - } - }, - .windows => { - var system_info: windows.SYSTEM_INFO = undefined; - windows.GetSystemInfo(&system_info); - return @intCast(usize, system_info.dwNumberOfProcessors); - }, - else => @compileError("unsupported OS"), + pub fn cpuCount() CpuCountError!usize { + if (os.linux.is_the_target) { + const cpu_set = try os.sched_getaffinity(0); + return os.CPU_COUNT(cpu_set); + } + if (os.windows.is_the_target) { + var system_info: windows.SYSTEM_INFO = undefined; + windows.GetSystemInfo(&system_info); + return @intCast(usize, system_info.dwNumberOfProcessors); } + var count: c_int = undefined; + var count_len: usize = @sizeOf(c_int); + const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu"; + try os.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); + return @intCast(usize, count); } }; -- cgit v1.2.3 From 2f040a23c8b968db56ab4c4725d6651f5ea3418e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 May 2019 13:17:34 -0400 Subject: clean up references to os --- build.zig | 28 +++--- doc/docgen.zig | 58 ++++++------ doc/langref.html.in | 2 +- src-self-hosted/compilation.zig | 23 +++-- src-self-hosted/errmsg.zig | 9 +- src-self-hosted/introspect.zig | 12 +-- src-self-hosted/libc_installation.zig | 35 ++++---- src-self-hosted/link.zig | 6 +- src-self-hosted/main.zig | 80 +++++++++-------- src-self-hosted/stage1.zig | 59 ++++++------ src-self-hosted/test.zig | 22 ++--- std/buffer.zig | 4 +- std/build.zig | 165 +++++++++++++++++----------------- std/child_process.zig | 48 +++++----- std/cstr.zig | 15 ---- std/debug.zig | 140 ++++++++++++++--------------- std/dynamic_library.zig | 5 +- std/event/fs.zig | 12 +-- std/fmt.zig | 2 +- std/fs.zig | 20 ++--- std/fs/file.zig | 24 ----- std/fs/get_app_data_dir.zig | 13 +-- std/fs/path.zig | 22 +++-- std/heap.zig | 36 ++++---- std/io.zig | 27 +++++- std/mem.zig | 18 ---- std/os.zig | 107 +++++++++++----------- std/os/bits/linux.zig | 10 +-- std/os/bits/wasi.zig | 10 +-- std/os/linux.zig | 7 +- std/os/linux/vdso.zig | 5 +- std/os/test.zig | 13 +-- std/os/windows.zig | 39 +++++++- std/process.zig | 68 ++++++++------ std/special/bootstrap.zig | 4 +- std/special/build_runner.zig | 8 +- std/thread.zig | 2 +- test/cli.zig | 40 +++++---- test/tests.zig | 90 +++++++++---------- 39 files changed, 648 insertions(+), 640 deletions(-) (limited to 'std/process.zig') diff --git a/build.zig b/build.zig index 2bc0229475..ec0bd9c02c 100644 --- a/build.zig +++ b/build.zig @@ -2,28 +2,28 @@ const builtin = @import("builtin"); const std = @import("std"); const Builder = std.build.Builder; const tests = @import("test/tests.zig"); -const os = std.os; const BufMap = std.BufMap; const warn = std.debug.warn; const mem = std.mem; const ArrayList = std.ArrayList; const Buffer = std.Buffer; const io = std.io; +const fs = std.fs; pub fn build(b: *Builder) !void { const mode = b.standardReleaseOptions(); var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig"); - const rel_zig_exe = try os.path.relative(b.allocator, b.build_root, b.zig_exe); - const langref_out_path = os.path.join( + const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe); + const langref_out_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, "langref.html" }, ) catch unreachable; var docgen_cmd = docgen_exe.run(); docgen_cmd.addArgs([][]const u8{ rel_zig_exe, - "doc" ++ os.path.sep_str ++ "langref.html.in", + "doc" ++ fs.path.sep_str ++ "langref.html.in", langref_out_path, }); docgen_cmd.step.dependOn(&docgen_exe.step); @@ -137,7 +137,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { for (dep.libdirs.toSliceConst()) |lib_dir| { lib_exe_obj.addLibPath(lib_dir); } - const lib_dir = os.path.join( + const lib_dir = fs.path.join( b.allocator, [][]const u8{ dep.prefix, "lib" }, ) catch unreachable; @@ -146,7 +146,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { ([]const u8)("libncurses.a") else b.fmt("lib{}.a", lib); - const static_lib_name = os.path.join( + const static_lib_name = fs.path.join( b.allocator, [][]const u8{ lib_dir, static_bare_name }, ) catch unreachable; @@ -166,7 +166,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { } fn fileExists(filename: []const u8) !bool { - os.File.access(filename) catch |err| switch (err) { + fs.File.access(filename) catch |err| switch (err) { error.PermissionDenied, error.FileNotFound, => return false, @@ -177,7 +177,7 @@ fn fileExists(filename: []const u8) !bool { fn addCppLib(b: *Builder, lib_exe_obj: var, cmake_binary_dir: []const u8, lib_name: []const u8) void { const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib"; - lib_exe_obj.addObjectFile(os.path.join(b.allocator, [][]const u8{ + lib_exe_obj.addObjectFile(fs.path.join(b.allocator, [][]const u8{ cmake_binary_dir, "zig_cpp", b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt()), @@ -223,7 +223,7 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { if (mem.startsWith(u8, lib_arg, "-l")) { try result.system_libs.append(lib_arg[2..]); } else { - if (os.path.isAbsolute(lib_arg)) { + if (fs.path.isAbsolute(lib_arg)) { try result.libs.append(lib_arg); } else { try result.system_libs.append(lib_arg); @@ -257,8 +257,8 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { pub fn installStdLib(b: *Builder, stdlib_files: []const u8) void { var it = mem.tokenize(stdlib_files, ";"); while (it.next()) |stdlib_file| { - const src_path = os.path.join(b.allocator, [][]const u8{ "std", stdlib_file }) catch unreachable; - const dest_path = os.path.join( + const src_path = fs.path.join(b.allocator, [][]const u8{ "std", stdlib_file }) catch unreachable; + const dest_path = fs.path.join( b.allocator, [][]const u8{ "lib", "zig", "std", stdlib_file }, ) catch unreachable; @@ -269,8 +269,8 @@ pub fn installStdLib(b: *Builder, stdlib_files: []const u8) void { pub fn installCHeaders(b: *Builder, c_header_files: []const u8) void { var it = mem.tokenize(c_header_files, ";"); while (it.next()) |c_header_file| { - const src_path = os.path.join(b.allocator, [][]const u8{ "c_headers", c_header_file }) catch unreachable; - const dest_path = os.path.join( + const src_path = fs.path.join(b.allocator, [][]const u8{ "c_headers", c_header_file }) catch unreachable; + const dest_path = fs.path.join( b.allocator, [][]const u8{ "lib", "zig", "include", c_header_file }, ) catch unreachable; @@ -315,7 +315,7 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void { } dependOnLib(b, exe, ctx.llvm); - if (exe.target.getOs() == builtin.Os.linux) { + if (exe.target.getOs() == .linux) { try addCxxKnownPath(b, ctx, exe, "libstdc++.a", \\Unable to determine path to libstdc++.a \\On Fedora, install libstdc++-static and try again. diff --git a/doc/docgen.zig b/doc/docgen.zig index a0e1b39bf6..998db54bbc 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -1,7 +1,9 @@ const builtin = @import("builtin"); const std = @import("std"); const io = std.io; -const os = std.os; +const fs = std.fs; +const process = std.process; +const ChildProcess = std.ChildProcess; const warn = std.debug.warn; const mem = std.mem; const testing = std.testing; @@ -11,7 +13,7 @@ const max_doc_file_size = 10 * 1024 * 1024; const exe_ext = std.build.Target(std.build.Target.Native).exeFileExt(); const obj_ext = std.build.Target(std.build.Target.Native).oFileExt(); const tmp_dir_name = "docgen_tmp"; -const test_out_path = tmp_dir_name ++ os.path.sep_str ++ "test" ++ exe_ext; +const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext; pub fn main() !void { var direct_allocator = std.heap.DirectAllocator.init(); @@ -22,7 +24,7 @@ pub fn main() !void { const allocator = &arena.allocator; - var args_it = os.args(); + var args_it = process.args(); if (!args_it.skip()) @panic("expected self arg"); @@ -35,10 +37,10 @@ pub fn main() !void { const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg")); defer allocator.free(out_file_name); - var in_file = try os.File.openRead(in_file_name); + var in_file = try fs.File.openRead(in_file_name); defer in_file.close(); - var out_file = try os.File.openWrite(out_file_name); + var out_file = try fs.File.openWrite(out_file_name); defer out_file.close(); var file_in_stream = in_file.inStream(); @@ -46,13 +48,13 @@ pub fn main() !void { const input_file_bytes = try file_in_stream.stream.readAllAlloc(allocator, max_doc_file_size); var file_out_stream = out_file.outStream(); - var buffered_out_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream); + var buffered_out_stream = io.BufferedOutStream(fs.File.WriteError).init(&file_out_stream.stream); var tokenizer = Tokenizer.init(in_file_name, input_file_bytes); var toc = try genToc(allocator, &tokenizer); - try os.makePath(allocator, tmp_dir_name); - defer os.deleteTree(allocator, tmp_dir_name) catch {}; + try fs.makePath(allocator, tmp_dir_name); + defer fs.deleteTree(allocator, tmp_dir_name) catch {}; try genHtml(allocator, &tokenizer, &toc, &buffered_out_stream.stream, zig_exe); try buffered_out_stream.flush(); @@ -950,7 +952,7 @@ fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: var, source_token: Token) fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void { var code_progress_index: usize = 0; - var env_map = try os.getEnvMap(allocator); + var env_map = try process.getEnvMap(allocator); try env_map.set("ZIG_DEBUG_COLOR", "1"); const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe); @@ -1012,7 +1014,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try tokenizeAndPrint(tokenizer, out, code.source_token); try out.write(""); const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name); - const tmp_source_file_name = try os.path.join( + const tmp_source_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_ext }, ); @@ -1021,7 +1023,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var switch (code.id) { Code.Id.Exe => |expected_outcome| code_block: { const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, exe_ext); - const tmp_bin_file_name = try os.path.join( + const tmp_bin_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_bin_ext }, ); @@ -1056,7 +1058,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } for (code.link_objects) |link_object| { const name_with_ext = try std.fmt.allocPrint(allocator, "{}{}", link_object, obj_ext); - const full_path_object = try os.path.join( + const full_path_object = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_with_ext }, ); @@ -1076,7 +1078,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } } if (expected_outcome == .BuildFail) { - const result = try os.ChildProcess.exec( + const result = try ChildProcess.exec( allocator, build_args.toSliceConst(), null, @@ -1084,7 +1086,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var max_doc_file_size, ); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (build_args.toSliceConst()) |arg| @@ -1113,7 +1115,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var if (code.target_str) |triple| { if (mem.startsWith(u8, triple, "wasm32") or mem.startsWith(u8, triple, "x86_64-linux") and - (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64)) + (builtin.os != .linux or builtin.arch != .x86_64)) { // skip execution try out.print("\n"); @@ -1124,9 +1126,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var const run_args = [][]const u8{tmp_bin_file_name}; const result = if (expected_outcome == ExpectedOutcome.Fail) blk: { - const result = try os.ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (run_args) |arg| @@ -1216,9 +1218,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try out.print(" --release-small"); }, } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (test_args.toSliceConst()) |arg| @@ -1274,9 +1276,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var }, } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (test_args.toSliceConst()) |arg| @@ -1310,7 +1312,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var }, Code.Id.Obj => |maybe_error_match| { const name_plus_obj_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, obj_ext); - const tmp_obj_file_name = try os.path.join( + const tmp_obj_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_obj_ext }, ); @@ -1318,7 +1320,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var defer build_args.deinit(); const name_plus_h_ext = try std.fmt.allocPrint(allocator, "{}.h", code.name); - const output_h_file_name = try os.path.join( + const output_h_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_h_ext }, ); @@ -1367,9 +1369,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } if (maybe_error_match) |error_match| { - const result = try os.ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (build_args.toSliceConst()) |arg| @@ -1448,10 +1450,10 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } } -fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !os.ChildProcess.ExecResult { - const result = try os.ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size); +fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult { + const result = try ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code != 0) { warn("{}\nThe following command exited with code {}:\n", result.stderr, exit_code); for (args) |arg| diff --git a/doc/langref.html.in b/doc/langref.html.in index 8310f2e2ab..5b9beba644 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -195,7 +195,7 @@ const std = @import("std"); pub fn main() !void { // If this program is run without stdout attached, exit with an error. - const stdout_file = try std.os.File.stdout(); + const stdout_file = try std.io.getStdOut(); // If this program encounters pipe failure when printing to stdout, exit // with an error. try stdout_file.write("Hello, world!\n"); diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 7a30bbad98..720c26945d 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const os = std.os; const io = std.io; const mem = std.mem; const Allocator = mem.Allocator; @@ -54,7 +53,7 @@ pub const ZigCompiler = struct { }; var seed_bytes: [@sizeOf(u64)]u8 = undefined; - try std.os.getRandomBytes(seed_bytes[0..]); + try std.crypto.randomBytes(seed_bytes[0..]); const seed = mem.readIntNative(u64, &seed_bytes); return ZigCompiler{ @@ -487,7 +486,7 @@ pub const Compilation = struct { comp.name = try Buffer.init(comp.arena(), name); comp.llvm_triple = try target.getTriple(comp.arena()); comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple); - comp.zig_std_dir = try std.os.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" }); + comp.zig_std_dir = try std.fs.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" }); const opt_level = switch (build_mode) { builtin.Mode.Debug => llvm.CodeGenLevelNone, @@ -529,8 +528,8 @@ pub const Compilation = struct { defer comp.events.destroy(); if (root_src_path) |root_src| { - const dirname = std.os.path.dirname(root_src) orelse "."; - const basename = std.os.path.basename(root_src); + const dirname = std.fs.path.dirname(root_src) orelse "."; + const basename = std.fs.path.basename(root_src); comp.root_package = try Package.create(comp.arena(), dirname, basename); comp.std_package = try Package.create(comp.arena(), comp.zig_std_dir, "std.zig"); @@ -558,7 +557,7 @@ pub const Compilation = struct { if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| { // TODO evented I/O? - os.deleteTree(comp.arena(), tmp_dir) catch {}; + std.fs.deleteTree(comp.arena(), tmp_dir) catch {}; } else |_| {}; } @@ -970,8 +969,8 @@ pub const Compilation = struct { async fn initialCompile(self: *Compilation) !void { if (self.root_src_path) |root_src_path| { const root_scope = blk: { - // TODO async/await os.path.real - const root_src_real_path = os.path.realAlloc(self.gpa(), root_src_path) catch |err| { + // TODO async/await std.fs.realpath + const root_src_real_path = std.fs.realpathAlloc(self.gpa(), root_src_path) catch |err| { try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err)); return; }; @@ -1196,7 +1195,7 @@ pub const Compilation = struct { const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", file_prefix[0..], suffix); defer self.gpa().free(file_name); - const full_path = try os.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] }); + const full_path = try std.fs.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] }); errdefer self.gpa().free(full_path); return Buffer.fromOwnedSlice(self.gpa(), full_path); @@ -1217,8 +1216,8 @@ pub const Compilation = struct { const zig_dir_path = try getZigDir(self.gpa()); defer self.gpa().free(zig_dir_path); - const tmp_dir = try os.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] }); - try os.makePath(self.gpa(), tmp_dir); + const tmp_dir = try std.fs.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] }); + try std.fs.makePath(self.gpa(), tmp_dir); return tmp_dir; } @@ -1384,7 +1383,7 @@ async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void { } fn getZigDir(allocator: *mem.Allocator) ![]u8 { - return os.getAppDataDir(allocator, "zig"); + return std.fs.getAppDataDir(allocator, "zig"); } async fn analyzeFnType( diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig index fc49fad410..eef5817d58 100644 --- a/src-self-hosted/errmsg.zig +++ b/src-self-hosted/errmsg.zig @@ -1,6 +1,7 @@ const std = @import("std"); const mem = std.mem; -const os = std.os; +const fs = std.fs; +const process = std.process; const Token = std.zig.Token; const ast = std.zig.ast; const TokenIndex = std.zig.ast.TokenIndex; @@ -239,10 +240,10 @@ pub const Msg = struct { const allocator = msg.getAllocator(); const tree = msg.getTree(); - const cwd = try os.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); - const relpath = try os.path.relative(allocator, cwd, msg.realpath); + const relpath = try fs.path.relative(allocator, cwd, msg.realpath); defer allocator.free(relpath); const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath; @@ -276,7 +277,7 @@ pub const Msg = struct { try stream.write("\n"); } - pub fn printToFile(msg: *const Msg, file: os.File, color: Color) !void { + pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void { const color_on = switch (color) { Color.Auto => file.isTty(), Color.On => true, diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig index 8f859a82ce..538232e620 100644 --- a/src-self-hosted/introspect.zig +++ b/src-self-hosted/introspect.zig @@ -2,19 +2,19 @@ const std = @import("std"); const mem = std.mem; -const os = std.os; +const fs = std.fs; const warn = std.debug.warn; /// Caller must free result pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 { - const test_zig_dir = try os.path.join(allocator, [][]const u8{ test_path, "lib", "zig" }); + const test_zig_dir = try fs.path.join(allocator, [][]const u8{ test_path, "lib", "zig" }); errdefer allocator.free(test_zig_dir); - const test_index_file = try os.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" }); + const test_index_file = try fs.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" }); defer allocator.free(test_index_file); - var file = try os.File.openRead(test_index_file); + var file = try fs.File.openRead(test_index_file); file.close(); return test_zig_dir; @@ -22,12 +22,12 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ /// Caller must free result pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { - const self_exe_path = try os.selfExeDirPathAlloc(allocator); + const self_exe_path = try fs.selfExeDirPathAlloc(allocator); defer allocator.free(self_exe_path); var cur_path: []const u8 = self_exe_path; while (true) { - const test_dir = os.path.dirname(cur_path) orelse "."; + const test_dir = fs.path.dirname(cur_path) orelse "."; if (mem.eql(u8, test_dir, cur_path)) { break; diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index 6a530da1f0..53790ec8f4 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const event = std.event; const Target = @import("target.zig").Target; const c = @import("c.zig"); +const fs = std.fs; /// See the render function implementation for documentation of the fields. pub const LibCInstallation = struct { @@ -30,7 +31,7 @@ pub const LibCInstallation = struct { self: *LibCInstallation, allocator: *std.mem.Allocator, libc_file: []const u8, - stderr: *std.io.OutStream(std.os.File.WriteError), + stderr: *std.io.OutStream(fs.File.WriteError), ) !void { self.initEmpty(); @@ -100,7 +101,7 @@ pub const LibCInstallation = struct { } } - pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(std.os.File.WriteError)) !void { + pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(fs.File.WriteError)) !void { @setEvalBranchQuota(4000); try out.print( \\# The directory that contains `stdlib.h`. @@ -148,7 +149,7 @@ pub const LibCInstallation = struct { errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk)); switch (builtin.os) { - builtin.Os.windows => { + .windows => { var sdk: *c.ZigWindowsSDK = undefined; switch (c.zig_find_windows_sdk(@ptrCast(?[*]?[*]c.ZigWindowsSDK, &sdk))) { c.ZigFindWindowsSdkError.None => { @@ -166,13 +167,13 @@ pub const LibCInstallation = struct { c.ZigFindWindowsSdkError.PathTooLong => return error.NotFound, } }, - builtin.Os.linux => { + .linux => { try group.call(findNativeIncludeDirLinux, self, loop); try group.call(findNativeLibDirLinux, self, loop); try group.call(findNativeStaticLibDir, self, loop); try group.call(findNativeDynamicLinker, self, loop); }, - builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { + .macosx, .freebsd, .netbsd => { self.include_dir = try std.mem.dupe(loop.allocator, u8, "/usr/include"); }, else => @compileError("unimplemented: find libc for this OS"), @@ -181,7 +182,7 @@ pub const LibCInstallation = struct { } async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void { - const cc_exe = std.os.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; const argv = []const []const u8{ cc_exe, "-E", @@ -190,7 +191,7 @@ pub const LibCInstallation = struct { "/dev/null", }; // TODO make this use event loop - const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); + const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); const exec_result = if (std.debug.runtime_safety) blk: { break :blk errorable_result catch unreachable; } else blk: { @@ -205,7 +206,7 @@ pub const LibCInstallation = struct { } switch (exec_result.term) { - std.os.ChildProcess.Term.Exited => |code| { + std.ChildProcess.Term.Exited => |code| { if (code != 0) return error.CCompilerExitCode; }, else => { @@ -230,7 +231,7 @@ pub const LibCInstallation = struct { while (path_i < search_paths.len) : (path_i += 1) { const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1); const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " "); - const stdlib_path = try std.os.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" }); + const stdlib_path = try fs.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" }); defer loop.allocator.free(stdlib_path); if (try fileExists(stdlib_path)) { @@ -254,7 +255,7 @@ pub const LibCInstallation = struct { const stream = &std.io.BufferOutStream.init(&result_buf).stream; try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version); - const stdlib_path = try std.os.path.join( + const stdlib_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "stdlib.h" }, ); @@ -286,7 +287,7 @@ pub const LibCInstallation = struct { builtin.Arch.aarch64 => try stream.write("arm"), else => return error.UnsupportedArchitecture, } - const ucrt_lib_path = try std.os.path.join( + const ucrt_lib_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "ucrt.lib" }, ); @@ -364,7 +365,7 @@ pub const LibCInstallation = struct { builtin.Arch.aarch64 => try stream.write("arm\\"), else => return error.UnsupportedArchitecture, } - const kernel32_path = try std.os.path.join( + const kernel32_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "kernel32.lib" }, ); @@ -391,14 +392,14 @@ pub const LibCInstallation = struct { /// caller owns returned memory async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 { - const cc_exe = std.os.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file); defer loop.allocator.free(arg1); const argv = []const []const u8{ cc_exe, arg1 }; // TODO This simulates evented I/O for the child process exec await (async loop.yield() catch unreachable); - const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); + const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); const exec_result = if (std.debug.runtime_safety) blk: { break :blk errorable_result catch unreachable; } else blk: { @@ -412,7 +413,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo loop.allocator.free(exec_result.stderr); } switch (exec_result.term) { - std.os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) return error.CCompilerExitCode; }, else => { @@ -421,7 +422,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo } var it = std.mem.tokenize(exec_result.stdout, "\n\r"); const line = it.next() orelse return error.LibCRuntimeNotFound; - const dirname = std.os.path.dirname(line) orelse return error.LibCRuntimeNotFound; + const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound; if (want_dirname) { return std.mem.dupe(loop.allocator, u8, dirname); @@ -459,7 +460,7 @@ fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search { } fn fileExists(path: []const u8) !bool { - if (std.os.File.access(path)) |_| { + if (fs.File.access(path)) |_| { return true; } else |err| switch (err) { error.FileNotFound, error.PermissionDenied => return false, diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index 5689ee7925..a47dbbbe92 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -302,7 +302,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { try ctx.args.append(c"--allow-shlib-undefined"); } - if (ctx.comp.target.getOs() == builtin.Os.zen) { + if (ctx.comp.target.getOs() == .zen) { try ctx.args.append(c"-e"); try ctx.args.append(c"_start"); @@ -311,7 +311,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { } fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void { - const full_path = try std.os.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename }); + const full_path = try std.fs.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename }); const full_path_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, full_path); try ctx.args.append(full_path_with_null.ptr); } @@ -668,7 +668,7 @@ const DarwinPlatform = struct { break :blk ver; }, Compilation.DarwinVersionMin.None => blk: { - assert(comp.target.getOs() == builtin.Os.macosx); + assert(comp.target.getOs() == .macosx); result.kind = Kind.MacOS; break :blk "10.10"; }, diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index cbbf73f3f5..1d2ac4917b 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -4,7 +4,9 @@ const builtin = @import("builtin"); const event = std.event; const os = std.os; const io = std.io; +const fs = std.fs; const mem = std.mem; +const process = std.process; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; const Buffer = std.Buffer; @@ -20,9 +22,9 @@ const Target = @import("target.zig").Target; const errmsg = @import("errmsg.zig"); const LibCInstallation = @import("libc_installation.zig").LibCInstallation; -var stderr_file: os.File = undefined; -var stderr: *io.OutStream(os.File.WriteError) = undefined; -var stdout: *io.OutStream(os.File.WriteError) = undefined; +var stderr_file: fs.File = undefined; +var stderr: *io.OutStream(fs.File.WriteError) = undefined; +var stdout: *io.OutStream(fs.File.WriteError) = undefined; pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB @@ -62,14 +64,14 @@ pub fn main() !void { var stderr_out_stream = stderr_file.outStream(); stderr = &stderr_out_stream.stream; - const args = try os.argsAlloc(allocator); + const args = try process.argsAlloc(allocator); // TODO I'm getting unreachable code here, which shouldn't happen - //defer os.argsFree(allocator, args); + //defer process.argsFree(allocator, args); if (args.len <= 1) { try stderr.write("expected command argument\n\n"); try stderr.write(usage); - os.exit(1); + process.exit(1); } const commands = []Command{ @@ -125,7 +127,7 @@ pub fn main() !void { try stderr.print("unknown command: {}\n\n", args[1]); try stderr.write(usage); - os.exit(1); + process.exit(1); } const usage_build_generic = @@ -256,7 +258,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co if (flags.present("help")) { try stdout.write(usage_build_generic); - os.exit(0); + process.exit(0); } const build_mode = blk: { @@ -324,14 +326,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co cur_pkg = parent; } else { try stderr.print("encountered --pkg-end with no matching --pkg-begin\n"); - os.exit(1); + process.exit(1); } } } if (cur_pkg.parent != null) { try stderr.print("unmatched --pkg-begin\n"); - os.exit(1); + process.exit(1); } const provided_name = flags.single("name"); @@ -340,18 +342,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co 1 => flags.positionals.at(0), else => { try stderr.print("unexpected extra parameter: {}\n", flags.positionals.at(1)); - os.exit(1); + process.exit(1); }, }; const root_name = if (provided_name) |n| n else blk: { if (root_source_file) |file| { - const basename = os.path.basename(file); + const basename = fs.path.basename(file); var it = mem.separate(basename, "."); break :blk it.next() orelse basename; } else { try stderr.write("--name [name] not provided and unable to infer\n"); - os.exit(1); + process.exit(1); } }; @@ -361,12 +363,12 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co const link_objects = flags.many("object"); if (root_source_file == null and link_objects.len == 0 and assembly_files.len == 0) { try stderr.write("Expected source file argument or at least one --object or --assembly argument\n"); - os.exit(1); + process.exit(1); } if (out_type == Compilation.Kind.Obj and link_objects.len != 0) { try stderr.write("When building an object file, --object arguments are invalid\n"); - os.exit(1); + process.exit(1); } var clang_argv_buf = ArrayList([]const u8).init(allocator); @@ -379,7 +381,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co } try ZigCompiler.setLlvmArgv(allocator, mllvm_flags); - const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1); + const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch process.exit(1); defer allocator.free(zig_lib_dir); var override_libc: LibCInstallation = undefined; @@ -448,7 +450,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co if (flags.single("mmacosx-version-min") != null and flags.single("mios-version-min") != null) { try stderr.write("-mmacosx-version-min and -mios-version-min options not allowed together\n"); - os.exit(1); + process.exit(1); } if (flags.single("mmacosx-version-min")) |ver| { @@ -478,16 +480,16 @@ async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void { switch (build_event) { Compilation.Event.Ok => { - stderr.print("Build {} succeeded\n", count) catch os.exit(1); + stderr.print("Build {} succeeded\n", count) catch process.exit(1); }, Compilation.Event.Error => |err| { - stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch os.exit(1); + stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch process.exit(1); }, Compilation.Event.Fail => |msgs| { - stderr.print("Build {} compile errors:\n", count) catch os.exit(1); + stderr.print("Build {} compile errors:\n", count) catch process.exit(1); for (msgs) |msg| { defer msg.destroy(); - msg.printToFile(stderr_file, color) catch os.exit(1); + msg.printToFile(stderr_file, color) catch process.exit(1); } }, } @@ -550,8 +552,8 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil "Try running `zig libc` to see an example for the native target.\n", libc_paths_file, @errorName(err), - ) catch os.exit(1); - os.exit(1); + ) catch process.exit(1); + process.exit(1); }; } @@ -565,7 +567,7 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void { }, else => { try stderr.print("unexpected extra parameter: {}\n", args[1]); - os.exit(1); + process.exit(1); }, } @@ -584,10 +586,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void { async fn findLibCAsync(zig_compiler: *ZigCompiler) void { const libc = (await (async zig_compiler.getNativeLibC() catch unreachable)) catch |err| { - stderr.print("unable to find libc: {}\n", @errorName(err)) catch os.exit(1); - os.exit(1); + stderr.print("unable to find libc: {}\n", @errorName(err)) catch process.exit(1); + process.exit(1); }; - libc.render(stdout) catch os.exit(1); + libc.render(stdout) catch process.exit(1); } fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { @@ -596,7 +598,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.present("help")) { try stdout.write(usage_fmt); - os.exit(0); + process.exit(0); } const color = blk: { @@ -616,7 +618,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.present("stdin")) { if (flags.positionals.len != 0) { try stderr.write("cannot use --stdin with positional arguments\n"); - os.exit(1); + process.exit(1); } var stdin_file = try io.getStdIn(); @@ -627,7 +629,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { const tree = std.zig.parse(allocator, source_code) catch |err| { try stderr.print("error parsing stdin: {}\n", err); - os.exit(1); + process.exit(1); }; defer tree.deinit(); @@ -639,12 +641,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { try msg.printToFile(stderr_file, color); } if (tree.errors.len != 0) { - os.exit(1); + process.exit(1); } if (flags.present("check")) { const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree); const code = if (anything_changed) u8(1) else u8(0); - os.exit(code); + process.exit(code); } _ = try std.zig.render(allocator, stdout, tree); @@ -653,7 +655,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.positionals.len == 0) { try stderr.write("expected at least one source file argument\n"); - os.exit(1); + process.exit(1); } var loop: event.Loop = undefined; @@ -700,7 +702,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, -} || os.File.OpenError; +} || fs.File.OpenError; async fn asyncFmtMain( loop: *event.Loop, @@ -725,7 +727,7 @@ async fn asyncFmtMain( } try await (async group.wait() catch unreachable); if (fmt.any_error) { - os.exit(1); + process.exit(1); } } @@ -747,13 +749,13 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro )) catch |err| switch (err) { error.IsDir, error.AccessDenied => { // TODO make event based (and dir.next()) - var dir = try std.os.Dir.open(fmt.loop.allocator, file_path); + var dir = try fs.Dir.open(fmt.loop.allocator, file_path); defer dir.close(); var group = event.Group(FmtError!void).init(fmt.loop); while (try dir.next()) |entry| { - if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { - const full_path = try os.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name }); + if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { + const full_path = try fs.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name }); try group.call(fmtPath, fmt, full_path, check_mode); } } @@ -891,7 +893,7 @@ const usage_internal = fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void { if (args.len == 0) { try stderr.write(usage_internal); - os.exit(1); + process.exit(1); } const sub_commands = []Command{Command{ diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 7a44e8f3a0..dd1c137c74 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -1,8 +1,24 @@ // This is Zig code that is used by both stage1 and stage2. // The prototypes in src/userland.h must match these definitions. -const std = @import("std"); const builtin = @import("builtin"); +const std = @import("std"); +const io = std.io; +const mem = std.mem; +const fs = std.fs; +const process = std.process; +const Allocator = mem.Allocator; +const ArrayList = std.ArrayList; +const Buffer = std.Buffer; +const arg = @import("arg.zig"); +const self_hosted_main = @import("main.zig"); +const Args = arg.Args; +const Flag = arg.Flag; +const errmsg = @import("errmsg.zig"); + +var stderr_file: fs.File = undefined; +var stderr: *io.OutStream(fs.File.WriteError) = undefined; +var stdout: *io.OutStream(fs.File.WriteError) = undefined; // ABI warning export fn stage2_zen(ptr: *[*]const u8, len: *usize) void { @@ -157,7 +173,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.present("help")) { try stdout.write(self_hosted_main.usage_fmt); - os.exit(0); + process.exit(0); } const color = blk: { @@ -177,7 +193,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.present("stdin")) { if (flags.positionals.len != 0) { try stderr.write("cannot use --stdin with positional arguments\n"); - os.exit(1); + process.exit(1); } var stdin_file = try io.getStdIn(); @@ -188,7 +204,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { const tree = std.zig.parse(allocator, source_code) catch |err| { try stderr.print("error parsing stdin: {}\n", err); - os.exit(1); + process.exit(1); }; defer tree.deinit(); @@ -197,12 +213,12 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { try printErrMsgToFile(allocator, parse_error, tree, "", stderr_file, color); } if (tree.errors.len != 0) { - os.exit(1); + process.exit(1); } if (flags.present("check")) { const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree); const code = if (anything_changed) u8(1) else u8(0); - os.exit(code); + process.exit(code); } _ = try std.zig.render(allocator, stdout, tree); @@ -211,7 +227,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.positionals.len == 0) { try stderr.write("expected at least one source file argument\n"); - os.exit(1); + process.exit(1); } var fmt = Fmt{ @@ -227,7 +243,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { try fmtPath(&fmt, file_path, check_mode); } if (fmt.any_error) { - os.exit(1); + process.exit(1); } } @@ -250,7 +266,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, -} || os.File.OpenError; +} || fs.File.OpenError; fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void { const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref); @@ -261,12 +277,12 @@ fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void const source_code = io.readFileAlloc(fmt.allocator, file_path) catch |err| switch (err) { error.IsDir, error.AccessDenied => { // TODO make event based (and dir.next()) - var dir = try std.os.Dir.open(fmt.allocator, file_path); + var dir = try fs.Dir.open(fmt.allocator, file_path); defer dir.close(); while (try dir.next()) |entry| { - if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { - const full_path = try os.path.join(fmt.allocator, [][]const u8{ file_path, entry.name }); + if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { + const full_path = try fs.path.join(fmt.allocator, [][]const u8{ file_path, entry.name }); try fmtPath(fmt, full_path, check_mode); } } @@ -329,7 +345,7 @@ fn printErrMsgToFile( parse_error: *const ast.Error, tree: *ast.Tree, path: []const u8, - file: os.File, + file: fs.File, color: errmsg.Color, ) !void { const color_on = switch (color) { @@ -377,20 +393,3 @@ fn printErrMsgToFile( try stream.writeByteNTimes('~', last_token.end - first_token.start); try stream.write("\n"); } - -const os = std.os; -const io = std.io; -const mem = std.mem; -const Allocator = mem.Allocator; -const ArrayList = std.ArrayList; -const Buffer = std.Buffer; - -const arg = @import("arg.zig"); -const self_hosted_main = @import("main.zig"); -const Args = arg.Args; -const Flag = arg.Flag; -const errmsg = @import("errmsg.zig"); - -var stderr_file: os.File = undefined; -var stderr: *io.OutStream(os.File.WriteError) = undefined; -var stdout: *io.OutStream(os.File.WriteError) = undefined; diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig index 4be6d53932..526518ca47 100644 --- a/src-self-hosted/test.zig +++ b/src-self-hosted/test.zig @@ -55,12 +55,12 @@ pub const TestContext = struct { self.zig_lib_dir = try introspect.resolveZigLibDir(allocator); errdefer allocator.free(self.zig_lib_dir); - try std.os.makePath(allocator, tmp_dir_name); - errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {}; + try std.fs.makePath(allocator, tmp_dir_name); + errdefer std.fs.deleteTree(allocator, tmp_dir_name) catch {}; } fn deinit(self: *TestContext) void { - std.os.deleteTree(allocator, tmp_dir_name) catch {}; + std.fs.deleteTree(allocator, tmp_dir_name) catch {}; allocator.free(self.zig_lib_dir); self.zig_compiler.deinit(); self.loop.deinit(); @@ -87,10 +87,10 @@ pub const TestContext = struct { ) !void { var file_index_buf: [20]u8 = undefined; const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr()); - const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); + const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); - if (std.os.path.dirname(file1_path)) |dirname| { - try std.os.makePath(allocator, dirname); + if (std.fs.path.dirname(file1_path)) |dirname| { + try std.fs.makePath(allocator, dirname); } // TODO async I/O @@ -120,11 +120,11 @@ pub const TestContext = struct { ) !void { var file_index_buf: [20]u8 = undefined; const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr()); - const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); + const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, Target(Target.Native).exeFileExt()); - if (std.os.path.dirname(file1_path)) |dirname| { - try std.os.makePath(allocator, dirname); + if (std.fs.path.dirname(file1_path)) |dirname| { + try std.fs.makePath(allocator, dirname); } // TODO async I/O @@ -164,9 +164,9 @@ pub const TestContext = struct { Compilation.Event.Ok => { const argv = []const []const u8{exe_file_2}; // TODO use event loop - const child = try std.os.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024); + const child = try std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024); switch (child.term) { - std.os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { return error.BadReturnCode; } diff --git a/std/buffer.zig b/std/buffer.zig index 32228af558..bc6aa254da 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -139,15 +139,13 @@ pub const Buffer = struct { }; test "simple Buffer" { - const cstr = @import("cstr.zig"); - var buf = try Buffer.init(debug.global_allocator, ""); testing.expect(buf.len() == 0); try buf.append("hello"); try buf.append(" "); try buf.append("world"); testing.expect(buf.eql("hello world")); - testing.expect(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst())); + testing.expect(mem.eql(u8, mem.toSliceConst(u8, buf.toSliceConst().ptr), buf.toSliceConst())); var buf2 = try Buffer.initFromBuffer(buf); testing.expect(buf.eql(buf2.toSliceConst())); diff --git a/std/build.zig b/std/build.zig index 0f2d6093c4..115cb2c999 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1,6 +1,7 @@ const std = @import("std.zig"); const builtin = @import("builtin"); const io = std.io; +const fs = std.fs; const mem = std.mem; const debug = std.debug; const assert = debug.assert; @@ -8,9 +9,7 @@ const warn = std.debug.warn; const ArrayList = std.ArrayList; const HashMap = std.HashMap; const Allocator = mem.Allocator; -const os = std.os; -const StdIo = os.ChildProcess.StdIo; -const Term = os.ChildProcess.Term; +const process = std.process; const BufSet = std.BufSet; const BufMap = std.BufMap; const fmt_lib = std.fmt; @@ -96,11 +95,11 @@ pub const Builder = struct { pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder { const env_map = allocator.create(BufMap) catch unreachable; - env_map.* = os.getEnvMap(allocator) catch unreachable; + env_map.* = process.getEnvMap(allocator) catch unreachable; var self = Builder{ .zig_exe = zig_exe, .build_root = build_root, - .cache_root = os.path.relative(allocator, build_root, cache_root) catch unreachable, + .cache_root = fs.path.relative(allocator, build_root, cache_root) catch unreachable, .verbose = false, .verbose_tokenize = false, .verbose_ast = false, @@ -154,8 +153,8 @@ pub const Builder = struct { pub fn setInstallPrefix(self: *Builder, maybe_prefix: ?[]const u8) void { self.prefix = maybe_prefix orelse "/usr/local"; // TODO better default - self.lib_dir = os.path.join(self.allocator, [][]const u8{ self.prefix, "lib" }) catch unreachable; - self.exe_dir = os.path.join(self.allocator, [][]const u8{ self.prefix, "bin" }) catch unreachable; + self.lib_dir = fs.path.join(self.allocator, [][]const u8{ self.prefix, "lib" }) catch unreachable; + self.exe_dir = fs.path.join(self.allocator, [][]const u8{ self.prefix, "bin" }) catch unreachable; } pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { @@ -287,7 +286,7 @@ pub const Builder = struct { if (self.verbose) { warn("rm {}\n", installed_file); } - os.deleteFile(installed_file) catch {}; + fs.deleteFile(installed_file) catch {}; } // TODO remove empty directories @@ -326,7 +325,7 @@ pub const Builder = struct { fn detectNativeSystemPaths(self: *Builder) void { var is_nixos = false; - if (os.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { + if (process.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { is_nixos = true; var it = mem.tokenize(nix_cflags_compile, " "); while (true) { @@ -345,7 +344,7 @@ pub const Builder = struct { } else |err| { assert(err == error.EnvironmentVariableNotFound); } - if (os.getEnvVarOwned(self.allocator, "NIX_LDFLAGS")) |nix_ldflags| { + if (process.getEnvVarOwned(self.allocator, "NIX_LDFLAGS")) |nix_ldflags| { is_nixos = true; var it = mem.tokenize(nix_ldflags, " "); while (true) { @@ -369,7 +368,7 @@ pub const Builder = struct { } if (is_nixos) return; switch (builtin.os) { - builtin.Os.windows => {}, + .windows => {}, else => { const triple = (CrossTarget{ .arch = builtin.arch, @@ -602,7 +601,7 @@ pub const Builder = struct { printCmd(cwd, argv); } - const child = os.ChildProcess.init(argv, self.allocator) catch unreachable; + const child = std.ChildProcess.init(argv, self.allocator) catch unreachable; defer child.deinit(); child.cwd = cwd; @@ -614,7 +613,7 @@ pub const Builder = struct { }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("The following command exited with error code {}:\n", code); printCmd(cwd, argv); @@ -631,7 +630,7 @@ pub const Builder = struct { } pub fn makePath(self: *Builder, path: []const u8) !void { - os.makePath(self.allocator, self.pathFromRoot(path)) catch |err| { + fs.makePath(self.allocator, self.pathFromRoot(path)) catch |err| { warn("Unable to create path {}: {}\n", path, @errorName(err)); return err; }; @@ -652,7 +651,7 @@ pub const Builder = struct { ///::dest_rel_path is relative to prefix path or it can be an absolute path pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep { - const full_dest_path = os.path.resolve( + const full_dest_path = fs.path.resolve( self.allocator, [][]const u8{ self.prefix, dest_rel_path }, ) catch unreachable; @@ -677,20 +676,20 @@ pub const Builder = struct { warn("cp {} {}\n", source_path, dest_path); } - const dirname = os.path.dirname(dest_path) orelse "."; + const dirname = fs.path.dirname(dest_path) orelse "."; const abs_source_path = self.pathFromRoot(source_path); - os.makePath(self.allocator, dirname) catch |err| { + fs.makePath(self.allocator, dirname) catch |err| { warn("Unable to create path {}: {}\n", dirname, @errorName(err)); return err; }; - os.copyFileMode(abs_source_path, dest_path, mode) catch |err| { + fs.copyFileMode(abs_source_path, dest_path, mode) catch |err| { warn("Unable to copy {} to {}: {}\n", abs_source_path, dest_path, @errorName(err)); return err; }; } fn pathFromRoot(self: *Builder, rel_path: []const u8) []u8 { - return os.path.resolve(self.allocator, [][]const u8{ self.build_root, rel_path }) catch unreachable; + return fs.path.resolve(self.allocator, [][]const u8{ self.build_root, rel_path }) catch unreachable; } pub fn fmt(self: *Builder, comptime format: []const u8, args: ...) []u8 { @@ -702,11 +701,11 @@ pub const Builder = struct { const exe_extension = (Target{ .Native = {} }).exeFileExt(); for (self.search_prefixes.toSliceConst()) |search_prefix| { for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } - const full_path = try os.path.join(self.allocator, [][]const u8{ search_prefix, "bin", self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ search_prefix, "bin", self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -715,13 +714,13 @@ pub const Builder = struct { } if (self.env_map.get("PATH")) |PATH| { for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } - var it = mem.tokenize(PATH, []u8{os.path.delimiter}); + var it = mem.tokenize(PATH, []u8{fs.path.delimiter}); while (it.next()) |path| { - const full_path = try os.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -730,12 +729,12 @@ pub const Builder = struct { } } for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } for (paths) |path| { - const full_path = try os.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -749,12 +748,12 @@ pub const Builder = struct { assert(argv.len != 0); const max_output_size = 100 * 1024; - const child = try os.ChildProcess.init(argv, self.allocator); + const child = try std.ChildProcess.init(argv, self.allocator); defer child.deinit(); - child.stdin_behavior = os.ChildProcess.StdIo.Ignore; - child.stdout_behavior = os.ChildProcess.StdIo.Pipe; - child.stderr_behavior = os.ChildProcess.StdIo.Inherit; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Inherit; try child.spawn(); @@ -766,7 +765,7 @@ pub const Builder = struct { const term = child.wait() catch |err| std.debug.panic("unable to spawn {}: {}", argv[0], err); switch (term) { - os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("The following command exited with error code {}:\n", code); printCmd(null, argv); @@ -846,7 +845,7 @@ pub const Target = union(enum) { } } - pub fn oFileExt(self: *const Target) []const u8 { + pub fn oFileExt(self: Target) []const u8 { const abi = switch (self.*) { Target.Native => builtin.abi, Target.Cross => |t| t.abi, @@ -857,49 +856,49 @@ pub const Target = union(enum) { }; } - pub fn exeFileExt(self: *const Target) []const u8 { + pub fn exeFileExt(self: Target) []const u8 { return switch (self.getOs()) { - builtin.Os.windows => ".exe", + .windows => ".exe", else => "", }; } - pub fn libFileExt(self: *const Target) []const u8 { + pub fn libFileExt(self: Target) []const u8 { return switch (self.getOs()) { - builtin.Os.windows => ".lib", + .windows => ".lib", else => ".a", }; } - pub fn getOs(self: *const Target) builtin.Os { - return switch (self.*) { + pub fn getOs(self: Target) builtin.Os { + return switch (self) { Target.Native => builtin.os, Target.Cross => |t| t.os, }; } - pub fn isDarwin(self: *const Target) bool { + pub fn isDarwin(self: Target) bool { return switch (self.getOs()) { - builtin.Os.ios, builtin.Os.macosx => true, + .ios, .macosx, .watchos, .tvos => true, else => false, }; } - pub fn isWindows(self: *const Target) bool { + pub fn isWindows(self: Target) bool { return switch (self.getOs()) { - builtin.Os.windows => true, + .windows => true, else => false, }; } - pub fn isFreeBSD(self: *const Target) bool { + pub fn isFreeBSD(self: Target) bool { return switch (self.getOs()) { - builtin.Os.freebsd => true, + .freebsd => true, else => false, }; } - pub fn wantSharedLibSymLinks(self: *const Target) bool { + pub fn wantSharedLibSymLinks(self: Target) bool { return !self.isWindows(); } }; @@ -1065,19 +1064,19 @@ pub const LibExeObjStep = struct { fn computeOutFileNames(self: *LibExeObjStep) void { switch (self.kind) { - Kind.Obj => { + .Obj => { self.out_filename = self.builder.fmt("{}{}", self.name, self.target.oFileExt()); }, - Kind.Exe => { + .Exe => { self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt()); }, - Kind.Test => { + .Test => { self.out_filename = self.builder.fmt("test{}", self.target.exeFileExt()); }, - Kind.Lib => { + .Lib => { if (!self.is_dynamic) { switch (self.target.getOs()) { - builtin.Os.windows => { + .windows => { self.out_filename = self.builder.fmt("{}.lib", self.name); }, else => { @@ -1087,13 +1086,13 @@ pub const LibExeObjStep = struct { self.out_lib_filename = self.out_filename; } else { switch (self.target.getOs()) { - builtin.Os.ios, builtin.Os.macosx => { + .ios, .macosx => { self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch); self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major); self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name); self.out_lib_filename = self.out_filename; }, - builtin.Os.windows => { + .windows => { self.out_filename = self.builder.fmt("{}.dll", self.name); self.out_lib_filename = self.builder.fmt("{}.lib", self.name); }, @@ -1228,7 +1227,7 @@ pub const LibExeObjStep = struct { /// the make step, from a step that has declared a dependency on this one. /// To run an executable built with zig build, use `run`, or create an install step and invoke it. pub fn getOutputPath(self: *LibExeObjStep) []const u8 { - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_filename }, ) catch unreachable; @@ -1238,7 +1237,7 @@ pub const LibExeObjStep = struct { /// the make step, from a step that has declared a dependency on this one. pub fn getOutputLibPath(self: *LibExeObjStep) []const u8 { assert(self.kind == Kind.Lib); - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_lib_filename }, ) catch unreachable; @@ -1249,7 +1248,7 @@ pub const LibExeObjStep = struct { pub fn getOutputHPath(self: *LibExeObjStep) []const u8 { assert(self.kind != Kind.Exe); assert(!self.disable_gen_h); - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_h_filename }, ) catch unreachable; @@ -1365,7 +1364,7 @@ pub const LibExeObjStep = struct { try zig_args.append("--library"); try zig_args.append(full_path_lib); - if (os.path.dirname(full_path_lib)) |dirname| { + if (fs.path.dirname(full_path_lib)) |dirname| { try zig_args.append("-rpath"); try zig_args.append(dirname); } @@ -1391,7 +1390,7 @@ pub const LibExeObjStep = struct { } if (self.build_options_contents.len() > 0) { - const build_options_file = try os.path.join( + const build_options_file = try fs.path.join( builder.allocator, [][]const u8{ builder.cache_root, builder.fmt("{}_build_options.zig", self.name) }, ); @@ -1503,7 +1502,7 @@ pub const LibExeObjStep = struct { IncludeDir.OtherStep => |other| { const h_path = other.getOutputHPath(); try zig_args.append("-isystem"); - try zig_args.append(os.path.dirname(h_path).?); + try zig_args.append(fs.path.dirname(h_path).?); }, } } @@ -1576,7 +1575,7 @@ pub const LibExeObjStep = struct { const output_path_nl = try builder.exec(zig_args.toSliceConst()); const output_path = mem.trimRight(u8, output_path_nl, "\r\n"); - self.output_dir = os.path.dirname(output_path).?; + self.output_dir = fs.path.dirname(output_path).?; } if (self.kind == Kind.Lib and self.is_dynamic and self.target.wantSharedLibSymLinks()) { @@ -1637,20 +1636,20 @@ pub const RunStep = struct { } pub fn addPathDir(self: *RunStep, search_path: []const u8) void { - const PATH = if (builtin.os == builtin.Os.windows) "Path" else "PATH"; + const PATH = if (std.os.windows.is_the_target) "Path" else "PATH"; const env_map = self.getEnvMap(); const prev_path = env_map.get(PATH) orelse { env_map.set(PATH, search_path) catch unreachable; return; }; - const new_path = self.builder.fmt("{}" ++ [1]u8{os.path.delimiter} ++ "{}", prev_path, search_path); + const new_path = self.builder.fmt("{}" ++ [1]u8{fs.path.delimiter} ++ "{}", prev_path, search_path); env_map.set(PATH, new_path) catch unreachable; } pub fn getEnvMap(self: *RunStep) *BufMap { return self.env_map orelse { const env_map = self.builder.allocator.create(BufMap) catch unreachable; - env_map.* = os.getEnvMap(self.builder.allocator) catch unreachable; + env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable; self.env_map = env_map; return env_map; }; @@ -1688,7 +1687,7 @@ pub const RunStep = struct { switch (link_object) { LibExeObjStep.LinkObject.OtherStep => |other| { if (other.target.isWindows() and other.isDynamicLibrary()) { - self.addPathDir(os.path.dirname(other.getOutputPath()).?); + self.addPathDir(fs.path.dirname(other.getOutputPath()).?); self.addPathForDynLibs(other); } }, @@ -1718,7 +1717,7 @@ const InstallArtifactStep = struct { .builder = builder, .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make), .artifact = artifact, - .dest_file = os.path.join( + .dest_file = fs.path.join( builder.allocator, [][]const u8{ dest_dir, artifact.out_filename }, ) catch unreachable, @@ -1726,11 +1725,11 @@ const InstallArtifactStep = struct { self.step.dependOn(&artifact.step); builder.pushInstalledFile(self.dest_file); if (self.artifact.kind == LibExeObjStep.Kind.Lib and self.artifact.is_dynamic) { - builder.pushInstalledFile(os.path.join( + builder.pushInstalledFile(fs.path.join( builder.allocator, [][]const u8{ builder.lib_dir, artifact.major_only_filename }, ) catch unreachable); - builder.pushInstalledFile(os.path.join( + builder.pushInstalledFile(fs.path.join( builder.allocator, [][]const u8{ builder.lib_dir, artifact.name_only_filename }, ) catch unreachable); @@ -1743,12 +1742,12 @@ const InstallArtifactStep = struct { const builder = self.builder; const mode = switch (builtin.os) { - builtin.Os.windows => {}, + .windows => {}, else => switch (self.artifact.kind) { - LibExeObjStep.Kind.Obj => unreachable, - LibExeObjStep.Kind.Test => unreachable, - LibExeObjStep.Kind.Exe => u32(0o755), - LibExeObjStep.Kind.Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755), + .Obj => unreachable, + .Test => unreachable, + .Exe => u32(0o755), + .Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755), }, }; try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); @@ -1797,8 +1796,8 @@ pub const WriteFileStep = struct { fn make(step: *Step) !void { const self = @fieldParentPtr(WriteFileStep, "step", step); const full_path = self.builder.pathFromRoot(self.file_path); - const full_path_dir = os.path.dirname(full_path) orelse "."; - os.makePath(self.builder.allocator, full_path_dir) catch |err| { + const full_path_dir = fs.path.dirname(full_path) orelse "."; + fs.makePath(self.builder.allocator, full_path_dir) catch |err| { warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); return err; }; @@ -1845,7 +1844,7 @@ pub const RemoveDirStep = struct { const self = @fieldParentPtr(RemoveDirStep, "step", step); const full_path = self.builder.pathFromRoot(self.dir_path); - os.deleteTree(self.builder.allocator, full_path) catch |err| { + fs.deleteTree(self.builder.allocator, full_path) catch |err| { warn("Unable to remove {}: {}\n", full_path, @errorName(err)); return err; }; @@ -1887,23 +1886,23 @@ pub const Step = struct { }; fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void { - const out_dir = os.path.dirname(output_path) orelse "."; - const out_basename = os.path.basename(output_path); + const out_dir = fs.path.dirname(output_path) orelse "."; + const out_basename = fs.path.basename(output_path); // sym link for libfoo.so.1 to libfoo.so.1.2.3 - const major_only_path = os.path.join( + const major_only_path = fs.path.join( allocator, [][]const u8{ out_dir, filename_major_only }, ) catch unreachable; - os.atomicSymLink(allocator, out_basename, major_only_path) catch |err| { + fs.atomicSymLink(allocator, out_basename, major_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", major_only_path, out_basename); return err; }; // sym link for libfoo.so to libfoo.so.1 - const name_only_path = os.path.join( + const name_only_path = fs.path.join( allocator, [][]const u8{ out_dir, filename_name_only }, ) catch unreachable; - os.atomicSymLink(allocator, filename_major_only, name_only_path) catch |err| { + fs.atomicSymLink(allocator, filename_major_only, name_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", name_only_path, filename_major_only); return err; }; diff --git a/std/child_process.zig b/std/child_process.zig index 95d11d46a3..6305f10344 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -2,7 +2,9 @@ const std = @import("std.zig"); const cstr = std.cstr; const unicode = std.unicode; const io = std.io; +const fs = std.fs; const os = std.os; +const process = std.process; const File = std.fs.File; const windows = os.windows; const mem = std.mem; @@ -14,12 +16,10 @@ const Os = builtin.Os; const LinkedList = std.LinkedList; const maxInt = std.math.maxInt; -const is_windows = builtin.os == .windows; - pub const ChildProcess = struct { - pub pid: if (is_windows) void else i32, - pub handle: if (is_windows) windows.HANDLE else void, - pub thread_handle: if (is_windows) windows.HANDLE else void, + pub pid: if (os.windows.is_the_target) void else i32, + pub handle: if (os.windows.is_the_target) windows.HANDLE else void, + pub thread_handle: if (os.windows.is_the_target) windows.HANDLE else void, pub allocator: *mem.Allocator, @@ -39,16 +39,16 @@ pub const ChildProcess = struct { pub stderr_behavior: StdIo, /// Set to change the user id when spawning the child process. - pub uid: if (is_windows) void else ?u32, + pub uid: if (os.windows.is_the_target) void else ?u32, /// Set to change the group id when spawning the child process. - pub gid: if (is_windows) void else ?u32, + pub gid: if (os.windows.is_the_target) void else ?u32, /// Set to change the current working directory when spawning the child process. pub cwd: ?[]const u8, - err_pipe: if (is_windows) void else [2]i32, - llnode: if (is_windows) void else LinkedList(*ChildProcess).Node, + err_pipe: if (os.windows.is_the_target) void else [2]i32, + llnode: if (os.windows.is_the_target) void else LinkedList(*ChildProcess).Node, pub const SpawnError = error{ ProcessFdQuotaExceeded, @@ -98,10 +98,8 @@ pub const ChildProcess = struct { .term = null, .env_map = null, .cwd = null, - .uid = if (is_windows) {} else - null, - .gid = if (is_windows) {} else - null, + .uid = if (os.windows.is_the_target) {} else null, + .gid = if (os.windows.is_the_target) {} else null, .stdin = null, .stdout = null, .stderr = null, @@ -121,7 +119,7 @@ pub const ChildProcess = struct { /// On success must call `kill` or `wait`. pub fn spawn(self: *ChildProcess) !void { - if (is_windows) { + if (os.windows.is_the_target) { return self.spawnWindows(); } else { return self.spawnPosix(); @@ -135,7 +133,7 @@ pub const ChildProcess = struct { /// Forcibly terminates child process and then cleans up all resources. pub fn kill(self: *ChildProcess) !Term { - if (is_windows) { + if (os.windows.is_the_target) { return self.killWindows(1); } else { return self.killPosix(); @@ -165,7 +163,7 @@ pub const ChildProcess = struct { /// Blocks until child process terminates and then cleans up all resources. pub fn wait(self: *ChildProcess) !Term { - if (is_windows) { + if (os.windows.is_the_target) { return self.waitWindows(); } else { return self.waitPosix(); @@ -339,7 +337,7 @@ pub const ChildProcess = struct { break :x env_map; } else x: { we_own_env_map = true; - env_map_owned = try os.getEnvMap(self.allocator); + env_map_owned = try process.getEnvMap(self.allocator); break :x &env_map_owned; }; defer { @@ -372,15 +370,15 @@ pub const ChildProcess = struct { } if (self.cwd) |cwd| { - os.changeCurDir(cwd) catch |err| forkChildErrReport(err_pipe[1], err); + os.chdir(cwd) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.gid) |gid| { - os.posix_setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err); + os.setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.uid) |uid| { - os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); + os.setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); @@ -541,7 +539,7 @@ pub const ChildProcess = struct { // to match posix semantics const app_name = x: { if (self.cwd) |cwd| { - const resolved = try os.path.resolve(self.allocator, [][]const u8{ cwd, self.argv[0] }); + const resolved = try fs.path.resolve(self.allocator, [][]const u8{ cwd, self.argv[0] }); defer self.allocator.free(resolved); break :x try cstr.addNullByte(self.allocator, resolved); } else { @@ -559,12 +557,12 @@ pub const ChildProcess = struct { windowsCreateProcess(app_name_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| { if (no_path_err != error.FileNotFound) return no_path_err; - const PATH = try os.getEnvVarOwned(self.allocator, "PATH"); + const PATH = try process.getEnvVarOwned(self.allocator, "PATH"); defer self.allocator.free(PATH); var it = mem.tokenize(PATH, ";"); while (it.next()) |search_path| { - const joined_path = try os.path.join(self.allocator, [][]const u8{ search_path, app_name }); + const joined_path = try fs.path.join(self.allocator, [][]const u8{ search_path, app_name }); defer self.allocator.free(joined_path); const joined_path_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, joined_path); @@ -616,10 +614,10 @@ pub const ChildProcess = struct { fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void { switch (stdio) { - StdIo.Pipe => try os.posixDup2(pipe_fd, std_fileno), + StdIo.Pipe => try os.dup2(pipe_fd, std_fileno), StdIo.Close => os.close(std_fileno), StdIo.Inherit => {}, - StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno), + StdIo.Ignore => try os.dup2(dev_null_fd, std_fileno), } } }; diff --git a/std/cstr.zig b/std/cstr.zig index 49d6373732..c8c3447921 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -9,11 +9,6 @@ pub const line_sep = switch (builtin.os) { else => "\n", }; -/// Deprecated, use mem.len -pub fn len(ptr: [*]const u8) usize { - return mem.len(u8, ptr); -} - pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { var index: usize = 0; while (a[index] == b[index] and a[index] != 0) : (index += 1) {} @@ -26,16 +21,6 @@ pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { } } -/// Deprecated, use mem.toSliceConst -pub fn toSliceConst(str: [*]const u8) []const u8 { - return mem.toSliceConst(u8, str); -} - -/// Deprecated, use mem.toSlice -pub fn toSlice(str: [*]u8) []u8 { - return mem.toSlice(u8, str); -} - test "cstr fns" { comptime testCStrFnsImpl(); testCStrFnsImpl(); diff --git a/std/debug.zig b/std/debug.zig index a309c31bbe..2c623d42f9 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -3,16 +3,18 @@ const math = std.math; const mem = std.mem; const io = std.io; const os = std.os; +const fs = std.fs; +const process = std.process; const elf = std.elf; const DW = std.dwarf; const macho = std.macho; const coff = std.coff; const pdb = std.pdb; -const windows = os.windows; const ArrayList = std.ArrayList; const builtin = @import("builtin"); const maxInt = std.math.maxInt; const File = std.fs.File; +const windows = std.os.windows; const leb = @import("debug/leb128.zig"); @@ -20,8 +22,8 @@ pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAlloc pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator; pub const runtime_safety = switch (builtin.mode) { - builtin.Mode.Debug, builtin.Mode.ReleaseSafe => true, - builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false, + .Debug, .ReleaseSafe => true, + .ReleaseFast, .ReleaseSmall => false, }; const Module = struct { @@ -76,7 +78,7 @@ pub fn getSelfDebugInfo() !*DebugInfo { fn wantTtyColor() bool { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - return if (std.os.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); + return if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); } /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. @@ -100,47 +102,44 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { /// chopping off the irrelevant frames and shifting so that the returned addresses pointer /// equals the passed in addresses pointer. pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void { - switch (builtin.os) { - builtin.Os.windows => { - const addrs = stack_trace.instruction_addresses; - const u32_addrs_len = @intCast(u32, addrs.len); - const first_addr = first_address orelse { - stack_trace.index = windows.RtlCaptureStackBackTrace( - 0, - u32_addrs_len, - @ptrCast(**c_void, addrs.ptr), - null, - ); - return; - }; - var addr_buf_stack: [32]usize = undefined; - const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs; - const n = windows.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**c_void, addr_buf.ptr), null); - const first_index = for (addr_buf[0..n]) |addr, i| { - if (addr == first_addr) { - break i; - } - } else { - stack_trace.index = 0; + if (windows.is_the_target) { + const addrs = stack_trace.instruction_addresses; + const u32_addrs_len = @intCast(u32, addrs.len); + const first_addr = first_address orelse { + stack_trace.index = windows.ntdll.RtlCaptureStackBackTrace( + 0, + u32_addrs_len, + @ptrCast(**c_void, addrs.ptr), + null, + ); + return; + }; + var addr_buf_stack: [32]usize = undefined; + const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs; + const n = windows.ntdll.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**c_void, addr_buf.ptr), null); + const first_index = for (addr_buf[0..n]) |addr, i| { + if (addr == first_addr) { + break i; + } + } else { + stack_trace.index = 0; + return; + }; + const slice = addr_buf[first_index..n]; + // We use a for loop here because slice and addrs may alias. + for (slice) |addr, i| { + addrs[i] = addr; + } + stack_trace.index = slice.len; + } else { + var it = StackIterator.init(first_address); + for (stack_trace.instruction_addresses) |*addr, i| { + addr.* = it.next() orelse { + stack_trace.index = i; return; }; - const slice = addr_buf[first_index..n]; - // We use a for loop here because slice and addrs may alias. - for (slice) |addr, i| { - addrs[i] = addr; - } - stack_trace.index = slice.len; - }, - else => { - var it = StackIterator.init(first_address); - for (stack_trace.instruction_addresses) |*addr, i| { - addr.* = it.next() orelse { - stack_trace.index = i; - return; - }; - } - stack_trace.index = stack_trace.instruction_addresses.len; - }, + } + stack_trace.index = stack_trace.instruction_addresses.len; } } @@ -260,9 +259,8 @@ pub const StackIterator = struct { }; pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void { - switch (builtin.os) { - builtin.Os.windows => return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr), - else => {}, + if (windows.is_the_target) { + return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr); } var it = StackIterator.init(start_addr); while (it.next()) |return_address| { @@ -277,7 +275,7 @@ pub fn writeCurrentStackTraceWindows( start_addr: ?usize, ) !void { var addr_buf: [1024]usize = undefined; - const n = windows.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); + const n = windows.ntdll.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); const addrs = addr_buf[0..n]; var start_i: usize = if (start_addr) |saddr| blk: { for (addrs) |addr, i| { @@ -291,17 +289,18 @@ pub fn writeCurrentStackTraceWindows( } pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { - switch (builtin.os) { - builtin.Os.macosx => return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color), - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return printSourceAtAddressLinux(debug_info, out_stream, address, tty_color), - builtin.Os.windows => return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color), - else => return error.UnsupportedOperatingSystem, + if (windows.is_the_target) { + return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color); } + if (os.darwin.is_the_target) { + return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color); + } + return printSourceAtAddressPosix(debug_info, out_stream, address, tty_color); } fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_address: usize, tty_color: bool) !void { const allocator = getDebugInfoAllocator(); - const base_address = os.getBaseAddress(); + const base_address = process.getBaseAddress(); const relative_address = relocated_address - base_address; var coff_section: *coff.Section = undefined; @@ -331,7 +330,7 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres const mod = &di.modules[mod_index]; try populateModule(di, mod); - const obj_basename = os.path.basename(mod.obj_file_name); + const obj_basename = fs.path.basename(mod.obj_file_name); var symbol_i: usize = 0; const symbol_name = while (symbol_i != mod.symbols.len) { @@ -634,7 +633,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach } fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { - const base_addr = std.os.getBaseAddress(); + const base_addr = process.getBaseAddress(); const adjusted_addr = 0x100000000 + (address - base_addr); const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse { @@ -649,7 +648,7 @@ fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tt const symbol_name = mem.toSliceConst(u8, di.strings.ptr + symbol.nlist.n_strx); const compile_unit_name = if (symbol.ofile) |ofile| blk: { const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx); - break :blk os.path.basename(ofile_path); + break :blk fs.path.basename(ofile_path); } else "???"; if (getLineNumberInfoMacOs(di, symbol.*, adjusted_addr)) |line_info| { defer line_info.deinit(); @@ -716,7 +715,7 @@ pub fn printSourceAtAddressDwarf( } } -pub fn printSourceAtAddressLinux(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { +pub fn printSourceAtAddressPosix(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { return printSourceAtAddressDwarf(debug_info, out_stream, address, tty_color, printLineFromFileAnyOs); } @@ -776,16 +775,17 @@ pub const OpenSelfDebugInfoError = error{ }; pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { - switch (builtin.os) { - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return openSelfDebugInfoLinux(allocator), - builtin.Os.macosx, builtin.Os.ios => return openSelfDebugInfoMacOs(allocator), - builtin.Os.windows => return openSelfDebugInfoWindows(allocator), - else => return error.UnsupportedOperatingSystem, + if (windows.is_the_target) { + return openSelfDebugInfoWindows(allocator); + } + if (os.darwin.is_the_target) { + return openSelfDebugInfoMacOs(allocator); } + return openSelfDebugInfoPosix(allocator); } fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { - const self_file = try os.openSelfExe(); + const self_file = try fs.openSelfExe(); defer self_file.close(); const coff_obj = try allocator.create(coff.Coff); @@ -812,7 +812,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { const len = try di.coff.getPdbPath(path_buf[0..]); const raw_path = path_buf[0..len]; - const path = try os.path.resolve(allocator, [][]const u8{raw_path}); + const path = try fs.path.resolve(allocator, [][]const u8{raw_path}); try di.pdb.openFile(di.coff, path); @@ -1002,13 +1002,13 @@ pub fn openElfDebugInfo( return di; } -fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { +fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { const S = struct { var self_exe_file: File = undefined; var self_exe_mmap_seekable: io.SliceSeekableInStream = undefined; }; - S.self_exe_file = try os.openSelfExe(); + S.self_exe_file = try fs.openSelfExe(); errdefer S.self_exe_file.close(); const self_exe_mmap_len = try S.self_exe_file.getEndPos(); @@ -1195,7 +1195,7 @@ pub const DwarfInfo = struct { }; pub const DebugInfo = switch (builtin.os) { - builtin.Os.macosx, builtin.Os.ios => struct { + .macosx, .ios, .watchos, .tvos => struct { symbols: []const MachoSymbol, strings: []const u8, ofiles: OFileTable, @@ -1211,13 +1211,13 @@ pub const DebugInfo = switch (builtin.os) { return self.ofiles.allocator; } }, - builtin.Os.uefi, builtin.Os.windows => struct { + .uefi, .windows => struct { pdb: pdb.Pdb, coff: *coff.Coff, sect_contribs: []pdb.SectionContribEntry, modules: []Module, }, - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => DwarfInfo, + .linux, .freebsd, .netbsd => DwarfInfo, else => @compileError("Unsupported OS"), }; @@ -1411,7 +1411,7 @@ const LineNumberProgram = struct { return error.InvalidDebugInfo; } else self.include_dirs[file_entry.dir_index]; - const file_name = try os.path.join(self.file_entries.allocator, [][]const u8{ dir_name, file_entry.file_name }); + const file_name = try fs.path.join(self.file_entries.allocator, [][]const u8{ dir_name, file_entry.file_name }); errdefer self.file_entries.allocator.free(file_name); return LineInfo{ .line = if (self.prev_line >= 0) @intCast(u64, self.prev_line) else 0, diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index 2832b54dbb..ce721350ea 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -2,7 +2,6 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const mem = std.mem; -const cstr = std.cstr; const os = std.os; const assert = std.debug.assert; const testing = std.testing; @@ -223,7 +222,7 @@ pub const ElfLib = struct { if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == self.syms[i].st_shndx) continue; - if (!mem.eql(u8, name, cstr.toSliceConst(self.strings + self.syms[i].st_name))) continue; + if (!mem.eql(u8, name, mem.toSliceConst(u8, self.strings + self.syms[i].st_name))) continue; if (maybe_versym) |versym| { if (!checkver(self.verdef.?, versym[i], vername, self.strings)) continue; @@ -246,7 +245,7 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); } const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); - return mem.eql(u8, vername, cstr.toSliceConst(strings + aux.vda_name)); + return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); } pub const WindowsDynLib = struct { diff --git a/std/event/fs.zig b/std/event/fs.zig index fbdac65889..221070a062 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -879,7 +879,7 @@ pub fn Watch(comptime V: type) type { } async fn addFileKEvent(self: *Self, file_path: []const u8, value: V) !?V { - const resolved_path = try os.path.resolve(self.channel.loop.allocator, [][]const u8{file_path}); + const resolved_path = try std.fs.path.resolve(self.channel.loop.allocator, [][]const u8{file_path}); var resolved_path_consumed = false; defer if (!resolved_path_consumed) self.channel.loop.allocator.free(resolved_path); @@ -967,12 +967,12 @@ pub fn Watch(comptime V: type) type { async fn addFileLinux(self: *Self, file_path: []const u8, value: V) !?V { const value_copy = value; - const dirname = os.path.dirname(file_path) orelse "."; + const dirname = std.fs.path.dirname(file_path) orelse "."; const dirname_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, dirname); var dirname_with_null_consumed = false; defer if (!dirname_with_null_consumed) self.channel.loop.allocator.free(dirname_with_null); - const basename = os.path.basename(file_path); + const basename = std.fs.path.basename(file_path); const basename_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, basename); var basename_with_null_consumed = false; defer if (!basename_with_null_consumed) self.channel.loop.allocator.free(basename_with_null); @@ -1013,7 +1013,7 @@ pub fn Watch(comptime V: type) type { const value_copy = value; // TODO we might need to convert dirname and basename to canonical file paths ("short"?) - const dirname = try std.mem.dupe(self.channel.loop.allocator, u8, os.path.dirname(file_path) orelse "."); + const dirname = try std.mem.dupe(self.channel.loop.allocator, u8, std.fs.path.dirname(file_path) orelse "."); var dirname_consumed = false; defer if (!dirname_consumed) self.channel.loop.allocator.free(dirname); @@ -1021,7 +1021,7 @@ pub fn Watch(comptime V: type) type { defer self.channel.loop.allocator.free(dirname_utf16le); // TODO https://github.com/ziglang/zig/issues/265 - const basename = os.path.basename(file_path); + const basename = std.fs.path.basename(file_path); const basename_utf16le_null = try std.unicode.utf8ToUtf16LeWithNull(self.channel.loop.allocator, basename); var basename_utf16le_null_consumed = false; defer if (!basename_utf16le_null_consumed) self.channel.loop.allocator.free(basename_utf16le_null); @@ -1334,7 +1334,7 @@ async fn testFsWatchCantFail(loop: *Loop, result: *(anyerror!void)) void { } async fn testFsWatch(loop: *Loop) !void { - const file_path = try os.path.join(loop.allocator, [][]const u8{ test_tmp_dir, "file.txt" }); + const file_path = try std.fs.path.join(loop.allocator, [][]const u8{ test_tmp_dir, "file.txt" }); defer loop.allocator.free(file_path); const contents = diff --git a/std/fmt.zig b/std/fmt.zig index 27a8abba6e..74c36f7086 100644 --- a/std/fmt.zig +++ b/std/fmt.zig @@ -226,7 +226,7 @@ pub fn formatType( builtin.TypeInfo.Pointer.Size.Many => { if (ptr_info.child == u8) { if (fmt.len > 0 and fmt[0] == 's') { - const len = std.cstr.len(value); + const len = mem.len(u8, value); return formatText(value[0..len], fmt, context, Errors, output); } } diff --git a/std/fs.zig b/std/fs.zig index 91e4aca824..b81bc38a75 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -1,6 +1,8 @@ +const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; const mem = std.mem; +const Allocator = std.mem.Allocator; pub const path = @import("fs/path.zig"); pub const File = @import("fs/file.zig").File; @@ -12,8 +14,6 @@ pub const deleteFileC = os.unlinkC; pub const rename = os.rename; pub const renameC = os.renameC; pub const renameW = os.renameW; -pub const changeCurDir = os.chdir; -pub const changeCurDirC = os.chdirC; pub const realpath = os.realpath; pub const realpathC = os.realpathC; pub const realpathW = os.realpathW; @@ -65,13 +65,13 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: else => return err, // TODO zig should know this set does not include PathAlreadyExists } - const dirname = os.path.dirname(new_path) orelse "."; + const dirname = path.dirname(new_path) orelse "."; var rand_buf: [12]u8 = undefined; const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len)); defer allocator.free(tmp_path); mem.copy(u8, tmp_path[0..], dirname); - tmp_path[dirname.len] = os.path.sep; + tmp_path[dirname.len] = path.sep; while (true) { try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf); @@ -143,7 +143,7 @@ pub const AtomicFile = struct { /// TODO once we have null terminated pointers, use the /// openWriteNoClobberN function pub fn init(dest_path: []const u8, mode: File.Mode) InitError!AtomicFile { - const dirname = os.path.dirname(dest_path); + const dirname = path.dirname(dest_path); var rand_buf: [12]u8 = undefined; const dirname_component_len = if (dirname) |d| d.len + 1 else 0; const encoded_rand_len = comptime base64.Base64Encoder.calcSize(rand_buf.len); @@ -153,7 +153,7 @@ pub const AtomicFile = struct { if (dirname) |dir| { mem.copy(u8, tmp_path_buf[0..], dir); - tmp_path_buf[dir.len] = os.path.sep; + tmp_path_buf[dir.len] = path.sep; } tmp_path_buf[tmp_path_len] = 0; @@ -240,7 +240,7 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { // march end_index backward until next path component while (true) { end_index -= 1; - if (os.path.isSep(resolved_path[end_index])) break; + if (path.isSep(resolved_path[end_index])) break; } continue; }, @@ -250,7 +250,7 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { // march end_index forward until next path component while (true) { end_index += 1; - if (end_index == resolved_path.len or os.path.isSep(resolved_path[end_index])) break; + if (end_index == resolved_path.len or path.isSep(resolved_path[end_index])) break; } } } @@ -614,7 +614,7 @@ pub const Dir = struct { const next_index = self.handle.index + linux_entry.d_reclen; self.handle.index = next_index; - const name = cstr.toSlice(@ptrCast([*]u8, &linux_entry.d_name)); + const name = mem.toSlice(u8, @ptrCast([*]u8, &linux_entry.d_name)); // skip . and .. entries if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { @@ -709,7 +709,7 @@ pub fn readLinkC(pathname: [*]const u8, buffer: *[os.PATH_MAX]u8) ![]u8 { return os.readlinkC(pathname, buffer); } -pub const OpenSelfExeError = error{}; +pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError; pub fn openSelfExe() OpenSelfExeError!File { if (os.linux.is_the_target) { diff --git a/std/fs/file.zig b/std/fs/file.zig index 8de5d5eb3e..7cbbd4a4f0 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -307,28 +307,4 @@ pub const File = struct { return self.file.getPos(); } }; - - pub fn stdout() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_OUTPUT_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDOUT_FILENO); - } - - pub fn stderr() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_ERROR_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDERR_FILENO); - } - - pub fn stdin() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_INPUT_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDIN_FILENO); - } }; diff --git a/std/fs/get_app_data_dir.zig b/std/fs/get_app_data_dir.zig index 23dbaad86d..713b40b22b 100644 --- a/std/fs/get_app_data_dir.zig +++ b/std/fs/get_app_data_dir.zig @@ -2,6 +2,7 @@ const std = @import("../std.zig"); const builtin = @import("builtin"); const unicode = std.unicode; const mem = std.mem; +const fs = std.fs; const os = std.os; pub const GetAppDataDirError = error{ @@ -15,7 +16,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD switch (builtin.os) { .windows => { var dir_path_ptr: [*]u16 = undefined; - switch (os.windows.SHGetKnownFolderPath( + switch (os.windows.shell32.SHGetKnownFolderPath( &os.windows.FOLDERID_LocalAppData, os.windows.KF_FLAG_CREATE, null, @@ -30,25 +31,25 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD error.OutOfMemory => return error.OutOfMemory, }; defer allocator.free(global_dir); - return os.path.join(allocator, [][]const u8{ global_dir, appname }); + return fs.path.join(allocator, [][]const u8{ global_dir, appname }); }, os.windows.E_OUTOFMEMORY => return error.OutOfMemory, else => return error.AppDataDirUnavailable, } }, .macosx => { - const home_dir = os.getEnvPosix("HOME") orelse { + const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; - return os.path.join(allocator, [][]const u8{ home_dir, "Library", "Application Support", appname }); + return fs.path.join(allocator, [][]const u8{ home_dir, "Library", "Application Support", appname }); }, .linux, .freebsd, .netbsd => { - const home_dir = os.getEnvPosix("HOME") orelse { + const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; - return os.path.join(allocator, [][]const u8{ home_dir, ".local", "share", appname }); + return fs.path.join(allocator, [][]const u8{ home_dir, ".local", "share", appname }); }, else => @compileError("Unsupported OS"), } diff --git a/std/fs/path.zig b/std/fs/path.zig index b82ddc8117..217bb44327 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -1,16 +1,14 @@ -const std = @import("../std.zig"); const builtin = @import("builtin"); -const Os = builtin.Os; +const std = @import("../std.zig"); const debug = std.debug; const assert = debug.assert; const testing = std.testing; const mem = std.mem; const fmt = std.fmt; const Allocator = mem.Allocator; -const os = std.os; const math = std.math; -const windows = os.windows; -const cstr = std.cstr; +const windows = std.os.windows; +const fs = std.fs; pub const sep_windows = '\\'; pub const sep_posix = '/'; @@ -392,7 +390,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - return os.getCwdAlloc(allocator); + return fs.getCwdAlloc(allocator); } // determine which disk designator we will result with, if any @@ -487,7 +485,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, WindowsPath.Kind.None => { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); @@ -503,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { } else { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -573,7 +571,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - return os.getCwdAlloc(allocator); + return fs.getCwdAlloc(allocator); } var first_index: usize = 0; @@ -595,7 +593,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { result = try allocator.alloc(u8, max_size); } else { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); @@ -634,7 +632,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { } test "resolve" { - const cwd = try os.getCwdAlloc(debug.global_allocator); + const cwd = try fs.getCwdAlloc(debug.global_allocator); if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); @@ -648,7 +646,7 @@ test "resolve" { test "resolveWindows" { if (windows.is_the_target) { - const cwd = try os.getCwdAlloc(debug.global_allocator); + const cwd = try fs.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }); diff --git a/std/heap.zig b/std/heap.zig index 4ff36f3f5f..1abe1da4fb 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -103,7 +103,7 @@ pub const DirectAllocator = struct { return @ptrCast([*]u8, final_addr)[0..n]; } - const alloc_size = if (alignment <= os.page_size) n else n + alignment; + const alloc_size = if (alignment <= mem.page_size) n else n + alignment; const addr = os.mmap( null, alloc_size, @@ -123,7 +123,7 @@ pub const DirectAllocator = struct { if (unused_start_len != 0) { os.munmap(addr, unused_start_len); } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); + const aligned_end_addr = std.mem.alignForward(aligned_addr + n, mem.page_size); const unused_end_len = addr + alloc_size - aligned_end_addr; if (unused_end_len != 0) { os.munmap(aligned_end_addr, unused_end_len); @@ -147,7 +147,7 @@ pub const DirectAllocator = struct { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (old_addr_end > new_addr_end_rounded) { // For shrinking that is not releasing, we will only // decommit the pages not needed anymore. @@ -163,7 +163,7 @@ pub const DirectAllocator = struct { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (old_addr_end > new_addr_end_rounded) { os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); } @@ -196,9 +196,9 @@ pub const DirectAllocator = struct { } const old_addr_end = base_addr + old_mem.len; - const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); + const old_addr_end_rounded = mem.alignForward(old_addr_end, mem.page_size); const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (new_addr_end_rounded == old_addr_end_rounded) { // The reallocation fits in the already allocated pages. return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; @@ -374,7 +374,7 @@ pub const ArenaAllocator = struct { var len = prev_len; while (true) { len += len / 2; - len += os.page_size - @rem(len, os.page_size); + len += mem.page_size - @rem(len, mem.page_size); if (len >= actual_min_size) break; } const buf = try self.child_allocator.alignedAlloc(u8, @alignOf(BufNode), len); @@ -520,11 +520,11 @@ const WasmAllocator = struct { const adjusted_index = self.end_index + (adjusted_addr - addr); const new_end_index = adjusted_index + size; - if (new_end_index > self.num_pages * os.page_size) { - const required_memory = new_end_index - (self.num_pages * os.page_size); + if (new_end_index > self.num_pages * mem.page_size) { + const required_memory = new_end_index - (self.num_pages * mem.page_size); - var num_pages: usize = required_memory / os.page_size; - if (required_memory % os.page_size != 0) { + var num_pages: usize = required_memory / mem.page_size; + if (required_memory % mem.page_size != 0) { num_pages += 1; } @@ -553,14 +553,14 @@ const WasmAllocator = struct { // Initialize start_ptr at the first realloc if (self.num_pages == 0) { - self.start_ptr = @intToPtr([*]u8, @intCast(usize, @"llvm.wasm.memory.size.i32"(0)) * os.page_size); + self.start_ptr = @intToPtr([*]u8, @intCast(usize, @"llvm.wasm.memory.size.i32"(0)) * mem.page_size); } if (is_last_item(allocator, old_mem, new_align)) { const start_index = self.end_index - old_mem.len; const new_end_index = start_index + new_size; - if (new_end_index > self.num_pages * os.page_size) { + if (new_end_index > self.num_pages * mem.page_size) { _ = try alloc(allocator, new_end_index - self.end_index, new_align); } const result = self.start_ptr[start_index..new_end_index]; @@ -876,10 +876,10 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { //Maybe a platform's page_size is actually the same as or // very near usize? - if (os.page_size << 2 > maxInt(usize)) return; + if (mem.page_size << 2 > maxInt(usize)) return; const USizeShift = @IntType(false, std.math.log2(usize.bit_count)); - const large_align = u29(os.page_size << 2); + const large_align = u29(mem.page_size << 2); var align_mask: usize = undefined; _ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(u29, large_align)), &align_mask); @@ -906,7 +906,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi var debug_buffer: [1000]u8 = undefined; const debug_allocator = &FixedBufferAllocator.init(&debug_buffer).allocator; - const alloc_size = os.page_size * 2 + 50; + const alloc_size = mem.page_size * 2 + 50; var slice = try allocator.alignedAlloc(u8, 16, alloc_size); defer allocator.free(slice); @@ -915,7 +915,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi // which is 16 pages, hence the 32. This test may require to increase // the size of the allocations feeding the `allocator` parameter if they // fail, because of this high over-alignment we want to have. - while (@ptrToInt(slice.ptr) == mem.alignForward(@ptrToInt(slice.ptr), os.page_size * 32)) { + while (@ptrToInt(slice.ptr) == mem.alignForward(@ptrToInt(slice.ptr), mem.page_size * 32)) { try stuff_to_free.append(slice); slice = try allocator.alignedAlloc(u8, 16, alloc_size); } @@ -926,7 +926,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi slice[60] = 0x34; // realloc to a smaller size but with a larger alignment - slice = try allocator.alignedRealloc(slice, os.page_size * 32, alloc_size / 2); + slice = try allocator.alignedRealloc(slice, mem.page_size * 32, alloc_size / 2); testing.expect(slice[0] == 0x12); testing.expect(slice[60] == 0x34); } diff --git a/std/io.zig b/std/io.zig index 5ca011cb4c..41dc38f245 100644 --- a/std/io.zig +++ b/std/io.zig @@ -15,8 +15,31 @@ const fmt = std.fmt; const File = std.fs.File; const testing = std.testing; -const is_posix = builtin.os != builtin.Os.windows; -const is_windows = builtin.os == builtin.Os.windows; +pub const GetStdIoError = os.windows.GetStdHandleError; + +pub fn getStdOut() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDOUT_FILENO); +} + +pub fn getStdErr() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDERR_FILENO); +} + +pub fn getStdIn() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDIN_FILENO); +} pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream; pub const SliceSeekableInStream = @import("io/seekable_stream.zig").SliceSeekableInStream; diff --git a/std/mem.zig b/std/mem.zig index 4f7ea768d7..643d297191 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1462,21 +1462,3 @@ test "std.mem.alignForward" { testing.expect(alignForward(16, 8) == 16); testing.expect(alignForward(17, 8) == 24); } - -pub fn getBaseAddress() usize { - switch (builtin.os) { - .linux => { - const base = std.os.system.getauxval(std.elf.AT_BASE); - if (base != 0) { - return base; - } - const phdr = std.os.system.getauxval(std.elf.AT_PHDR); - return phdr - @sizeOf(std.elf.Ehdr); - }, - .macosx, .freebsd, .netbsd => { - return @ptrToInt(&std.c._mh_execute_header); - }, - .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)), - else => @compileError("Unsupported OS"), - } -} diff --git a/std/os.zig b/std/os.zig index 3b29570fcd..ef4d695aff 100644 --- a/std/os.zig +++ b/std/os.zig @@ -8,7 +8,7 @@ // cross platform abstracting. // * When there exists a corresponding libc function and linking libc, the libc // implementation is used. Exceptions are made for known buggy areas of libc. -// On Linux libc can be side-stepped by using `std.os.linux.sys`. +// On Linux libc can be side-stepped by using `std.os.linux` directly. // * For Windows, this file represents the API that libc would provide for // Windows. For thin wrappers around Windows-specific APIs, see `std.os.windows`. // Note: The Zig standard library does not support POSIX thread cancellation, and @@ -16,7 +16,9 @@ const std = @import("std.zig"); const builtin = @import("builtin"); +const assert = std.debug.assert; const math = std.math; +const mem = std.mem; const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; comptime { @@ -46,9 +48,14 @@ pub const system = if (builtin.link_libc) std.c else switch (builtin.os) { pub use @import("os/bits.zig"); -/// See also `getenv`. +/// See also `getenv`. Populated by startup code before main(). pub var environ: [][*]u8 = undefined; +/// Populated by startup code before main(). +/// Not available on Windows. See `std.process.args` +/// for obtaining the process arguments. +pub var argv: [][*]u8 = undefined; + /// To obtain errno, call this function with the return value of the /// system function call. For some systems this will obtain the value directly /// from the return code; for others it will use a thread-local errno variable. @@ -103,7 +110,7 @@ pub fn getrandom(buf: []u8) GetRandomError!void { } } if (wasi.is_the_target) { - switch (os.wasi.random_get(buf.ptr, buf.len)) { + switch (wasi.random_get(buf.ptr, buf.len)) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -138,15 +145,15 @@ pub fn abort() noreturn { while (true) {} } - raise(SIGABRT); + raise(SIGABRT) catch {}; // TODO the rest of the implementation of abort() from musl libc here - raise(SIGKILL); + raise(SIGKILL) catch {}; exit(127); } -pub const RaiseError = error{}; +pub const RaiseError = error{Unexpected}; pub fn raise(sig: u8) RaiseError!void { if (builtin.link_libc) { @@ -163,19 +170,19 @@ pub fn raise(sig: u8) RaiseError!void { } } - if (windows.is_the_target) { - @compileError("TODO implement std.os.raise for Windows"); + if (linux.is_the_target) { + var set: linux.sigset_t = undefined; + linux.blockAppSignals(&set); + const tid = linux.syscall0(linux.SYS_gettid); + const rc = linux.syscall2(linux.SYS_tkill, tid, sig); + linux.restoreSignals(&set); + switch (errno(rc)) { + 0 => return, + else => |err| return unexpectedErrno(err), + } } - var set: system.sigset_t = undefined; - system.blockAppSignals(&set); - const tid = system.syscall0(system.SYS_gettid); - const rc = system.syscall2(system.SYS_tkill, tid, sig); - system.restoreSignals(&set); - switch (errno(rc)) { - 0 => return, - else => |err| return unexpectedErrno(err), - } + @compileError("std.os.raise unimplemented for this target"); } pub const KillError = error{ @@ -229,13 +236,13 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { } if (wasi.is_the_target and !builtin.link_libc) { - const iovs = [1]was.iovec_t{wasi.iovec_t{ - .buf = buf.ptr, - .buf_len = buf.len, + const iovs = [1]iovec{iovec{ + .iov_base = buf.ptr, + .iov_len = buf.len, }}; var nread: usize = undefined; - switch (fd_read(fd, &iovs, iovs.len, &nread)) { + switch (wasi.fd_read(fd, &iovs, iovs.len, &nread)) { 0 => return nread, else => |err| return unexpectedErrno(err), } @@ -277,7 +284,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { /// This function is for blocking file descriptors only. For non-blocking, see /// `preadvAsync`. pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { - if (os.darwin.is_the_target) { + if (darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; var iov_i: usize = 0; @@ -353,12 +360,12 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { } if (wasi.is_the_target and !builtin.link_libc) { - const ciovs = [1]wasi.ciovec_t{wasi.ciovec_t{ - .buf = bytes.ptr, - .buf_len = bytes.len, + const ciovs = [1]iovec_const{iovec_const{ + .iov_base = bytes.ptr, + .iov_len = bytes.len, }}; var nwritten: usize = undefined; - switch (fd_write(fd, &ciovs, ciovs.len, &nwritten)) { + switch (wasi.fd_write(fd, &ciovs, ciovs.len, &nwritten)) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -538,29 +545,29 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { /// `argv[0]` is the executable path. /// This function also uses the PATH environment variable to get the full path to the executable. /// TODO provide execveC which does not take an allocator -pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const BufMap) !void { - const argv_buf = try allocator.alloc(?[*]u8, argv.len + 1); +pub fn execve(allocator: *mem.Allocator, argv_slice: []const []const u8, env_map: *const std.BufMap) !void { + const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1); mem.set(?[*]u8, argv_buf, null); defer { for (argv_buf) |arg| { - const arg_buf = if (arg) |ptr| cstr.toSlice(ptr) else break; + const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break; allocator.free(arg_buf); } allocator.free(argv_buf); } - for (argv) |arg, i| { + for (argv_slice) |arg, i| { const arg_buf = try allocator.alloc(u8, arg.len + 1); @memcpy(arg_buf.ptr, arg.ptr, arg.len); arg_buf[arg.len] = 0; argv_buf[i] = arg_buf.ptr; } - argv_buf[argv.len] = null; + argv_buf[argv_slice.len] = null; const envp_buf = try createNullDelimitedEnvMap(allocator, env_map); defer freeNullDelimitedEnvMap(allocator, envp_buf); - const exe_path = argv[0]; + const exe_path = argv_slice[0]; if (mem.indexOfScalar(u8, exe_path, '/') != null) { return execveErrnoToErr(errno(system.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr))); } @@ -593,7 +600,7 @@ pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const B return execveErrnoToErr(err); } -pub fn createNullDelimitedEnvMap(allocator: *Allocator, env_map: *const BufMap) ![]?[*]u8 { +pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 { const envp_count = env_map.count(); const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1); mem.set(?[*]u8, envp_buf, null); @@ -616,9 +623,9 @@ pub fn createNullDelimitedEnvMap(allocator: *Allocator, env_map: *const BufMap) return envp_buf; } -pub fn freeNullDelimitedEnvMap(allocator: *Allocator, envp_buf: []?[*]u8) void { +pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void { for (envp_buf) |env| { - const env_buf = if (env) |ptr| ptr[0 .. cstr.len(ptr) + 1] else break; + const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; allocator.free(env_buf); } allocator.free(envp_buf); @@ -633,6 +640,8 @@ pub const ExecveError = error{ FileNotFound, NotDir, FileBusy, + ProcessFdQuotaExceeded, + NameTooLong, Unexpected, }; @@ -703,17 +712,17 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } const err = if (builtin.link_libc) blk: { - break :blk if (system.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else system._errno().*; + break :blk if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; } else blk: { - break :blk errno(system.getcwd(out_buffer, out_buffer.len)); + break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); }; switch (err) { - 0 => return mem.toSlice(u8, out_buffer), + 0 => return mem.toSlice(u8, out_buffer.ptr), EFAULT => unreachable, EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, - else => |err| return unexpectedErrno(err), + else => return unexpectedErrno(err), } } @@ -1711,8 +1720,8 @@ pub const FStatError = error{ pub fn fstat(fd: fd_t) FStatError!Stat { var stat: Stat = undefined; - if (os.darwin.is_the_target) { - switch (errno(system.@"fstat$INODE64"(fd, buf))) { + if (darwin.is_the_target) { + switch (errno(system.@"fstat$INODE64"(fd, &stat))) { 0 => return stat, EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, @@ -1877,7 +1886,7 @@ pub const ForkError = error{ pub fn fork() ForkError!pid_t { const rc = system.fork(); switch (errno(rc)) { - 0 => return rc, + 0 => return @intCast(pid_t, rc), EAGAIN => return error.SystemResources, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), @@ -1891,6 +1900,7 @@ pub const MMapError = error{ SystemFdQuotaExceeded, MemoryMappingNotSupported, OutOfMemory, + Unexpected, }; /// Map files or devices into memory. @@ -1992,6 +2002,7 @@ pub fn accessC(path: [*]const u8, mode: u32) AccessError!void { pub const PipeError = error{ SystemFdQuotaExceeded, ProcessFdQuotaExceeded, + Unexpected, }; /// Creates a unidirectional data channel that can be used for interprocess communication. @@ -2065,18 +2076,6 @@ pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { } } -pub fn nanosleep(req: timespec) void { - var rem = req; - while (true) { - switch (errno(system.nanosleep(&rem, &rem))) { - 0 => return, - EINVAL => unreachable, // Invalid parameters. - EFAULT => unreachable, - EINTR => continue, - } - } -} - pub const SeekError = error{ Unseekable, Unexpected, diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index f40beeaf68..b4f51f3518 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -692,12 +692,12 @@ pub const winsize = extern struct { ws_ypixel: u16, }; -const NSIG = 65; -const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []u32{ 0xffffffff, 0xffffffff }; -const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; +pub const NSIG = 65; +pub const sigset_t = [128 / @sizeOf(usize)]usize; +pub const all_mask = []u32{ 0xffffffff, 0xffffffff }; +pub const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; -const k_sigaction = extern struct { +pub const k_sigaction = extern struct { handler: extern fn (i32) void, flags: usize, restorer: extern fn () void, diff --git a/std/os/bits/wasi.zig b/std/os/bits/wasi.zig index 4a8be6890a..cf6542a159 100644 --- a/std/os/bits/wasi.zig +++ b/std/os/bits/wasi.zig @@ -13,10 +13,7 @@ pub const ADVICE_WILLNEED: advice_t = 3; pub const ADVICE_DONTNEED: advice_t = 4; pub const ADVICE_NOREUSE: advice_t = 5; -pub const ciovec_t = extern struct { - buf: [*]const u8, - buf_len: usize, -}; +pub const ciovec_t = iovec_const; pub const clockid_t = u32; pub const CLOCK_REALTIME: clockid_t = 0; @@ -186,10 +183,7 @@ pub const FILESTAT_SET_MTIM_NOW: fstflags_t = 0x0008; pub const inode_t = u64; -pub const iovec_t = extern struct { - buf: [*]u8, - buf_len: usize, -}; +pub const iovec_t = iovec; pub const linkcount_t = u32; diff --git a/std/os/linux.zig b/std/os/linux.zig index 88a2980129..958c699dc5 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -20,6 +20,7 @@ pub use switch (builtin.arch) { else => struct {}, }; pub use @import("bits.zig"); +pub const tls = @import("linux/tls.zig"); /// Set by startup code, used by `getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; @@ -539,15 +540,15 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti return 0; } -fn blockAllSignals(set: *sigset_t) void { +pub fn blockAllSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); } -fn blockAppSignals(set: *sigset_t) void { +pub fn blockAppSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); } -fn restoreSignals(set: *sigset_t) void { +pub fn restoreSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); } diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig index efd69d8542..44cd0271cd 100644 --- a/std/os/linux/vdso.zig +++ b/std/os/linux/vdso.zig @@ -1,7 +1,6 @@ const std = @import("../../std.zig"); const elf = std.elf; const linux = std.os.linux; -const cstr = std.cstr; const mem = std.mem; const maxInt = std.math.maxInt; @@ -66,7 +65,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { if (0 == (u32(1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (u32(1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == syms[i].st_shndx) continue; - if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue; + if (!mem.eql(u8, name, mem.toSliceConst(u8, strings + syms[i].st_name))) continue; if (maybe_versym) |versym| { if (!checkver(maybe_verdef.?, versym[i], vername, strings)) continue; @@ -88,5 +87,5 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); } const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); - return mem.eql(u8, vername, cstr.toSliceConst(strings + aux.vda_name)); + return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); } diff --git a/std/os/test.zig b/std/os/test.zig index d4bdb06278..f7d40e1f03 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -3,6 +3,7 @@ const os = std.os; const testing = std.testing; const expect = std.testing.expect; const io = std.io; +const fs = std.fs; const mem = std.mem; const File = std.fs.File; const Thread = std.Thread; @@ -14,9 +15,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; test "makePath, put some files in it, deleteTree" { - try os.makePath(a, "os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c"); - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c" ++ os.path.sep_str ++ "file.txt", "nonsense"); - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "file2.txt", "blah"); + try os.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); try os.deleteTree(a, "os_test_tmp"); if (os.Dir.open(a, "os_test_tmp")) |dir| { @panic("expected error"); @@ -27,14 +28,14 @@ test "makePath, put some files in it, deleteTree" { test "access file" { try os.makePath(a, "os_test_tmp"); - if (File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { + if (File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { expect(err == error.FileNotFound); } - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", ""); - try File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); + try File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt"); try os.deleteTree(a, "os_test_tmp"); } diff --git a/std/os/windows.zig b/std/os/windows.zig index c18adc66fa..fbb8ff42c5 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -4,6 +4,7 @@ // * When null-terminated or UTF16LE byte buffers are required, provide APIs which accept // slices as well as APIs which accept null-terminated UTF16LE byte buffers. +const builtin = @import("builtin"); const std = @import("../std.zig"); const assert = std.debug.assert; const maxInt = std.math.maxInt; @@ -1103,6 +1104,42 @@ pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void { assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0); } +pub const SetConsoleTextAttributeError = error{Unexpected}; + +pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void { + if (kernel32.SetConsoleTextAttribute(hConsoleOutput, wAttributes) == 0) { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + } +} + +pub const GetEnvironmentStringsError = error{OutOfMemory}; + +pub fn GetEnvironmentStringsW() GetEnvironmentStringsError![*]u16 { + return kernel32.GetEnvironmentStringsW() orelse return error.OutOfMemory; +} + +pub fn FreeEnvironmentStringsW(penv: [*]u16) void { + assert(kernel32.FreeEnvironmentStringsW(penv) != 0); +} + +pub const GetEnvironmentVariableError = error{ + EnvironmentVariableNotFound, + Unexpected, +}; + +pub fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: LPWSTR, nSize: DWORD) GetEnvironmentVariableError!DWORD { + const rc = kernel32.GetEnvironmentVariableW(lpName, lpBuffer, nSize); + if (rc == 0) { + switch (kernel32.GetLastError()) { + ERROR.ENVVAR_NOT_FOUND => return error.EnvironmentVariableNotFound, + else => |err| return unexpectedError(err), + } + } + return rc; +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } @@ -1127,7 +1164,7 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) else => {}, } } - const start_index = if (mem.startsWith(u8, s, "\\\\") or !os.path.isAbsolute(s)) 0 else blk: { + const start_index = if (mem.startsWith(u8, s, "\\\\") or !std.fs.path.isAbsolute(s)) 0 else blk: { const prefix = []u16{ '\\', '\\', '?', '\\' }; mem.copy(u16, result[0..], prefix); break :blk prefix.len; diff --git a/std/process.zig b/std/process.zig index 1e29170e88..baccbccb10 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,13 +1,17 @@ +const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; const BufMap = std.BufMap; const mem = std.mem; +const math = std.math; const Allocator = mem.Allocator; const assert = std.debug.assert; const testing = std.testing; pub const abort = os.abort; pub const exit = os.exit; +pub const changeCurDir = os.chdir; +pub const changeCurDirC = os.chdirC; /// Caller must free result when done. /// TODO make this go through libc when we have it @@ -15,9 +19,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { var result = BufMap.init(allocator); errdefer result.deinit(); - if (is_windows) { - const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory; - defer assert(windows.FreeEnvironmentStringsW(ptr) != 0); + if (os.windows.is_the_target) { + const ptr = try os.windows.GetEnvironmentStringsW(); + defer os.windows.FreeEnvironmentStringsW(ptr); var i: usize = 0; while (true) { @@ -105,7 +109,7 @@ pub const GetEnvVarOwnedError = error{ /// Caller must free returned memory. /// TODO make this go through libc when we have it pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 { - if (is_windows) { + if (os.windows.is_the_target) { const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key); defer allocator.free(key_with_null); @@ -113,19 +117,15 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned defer allocator.free(buf); while (true) { - const windows_buf_len = math.cast(windows.DWORD, buf.len) catch return error.OutOfMemory; - const result = windows.GetEnvironmentVariableW(key_with_null.ptr, buf.ptr, windows_buf_len); - - if (result == 0) { - const err = windows.GetLastError(); - return switch (err) { - windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound, - else => { - windows.unexpectedError(err) catch {}; - return error.EnvironmentVariableNotFound; - }, - }; - } + const windows_buf_len = math.cast(os.windows.DWORD, buf.len) catch return error.OutOfMemory; + const result = os.windows.GetEnvironmentVariableW( + key_with_null.ptr, + buf.ptr, + windows_buf_len, + ) catch |err| switch (err) { + error.Unexpected => return error.EnvironmentVariableNotFound, + else => return err, + }; if (result > buf.len) { buf = try allocator.realloc(buf, result); @@ -136,11 +136,11 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned error.DanglingSurrogateHalf => return error.InvalidUtf8, error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, - error.OutOfMemory => return error.OutOfMemory, + else => return err, }; } } else { - const result = getEnvPosix(key) orelse return error.EnvironmentVariableNotFound; + const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound; return mem.dupe(allocator, u8, result); } } @@ -157,16 +157,16 @@ pub const ArgIteratorPosix = struct { pub fn init() ArgIteratorPosix { return ArgIteratorPosix{ .index = 0, - .count = raw.len, + .count = os.argv.len, }; } pub fn next(self: *ArgIteratorPosix) ?[]const u8 { if (self.index == self.count) return null; - const s = raw[self.index]; + const s = os.argv[self.index]; self.index += 1; - return cstr.toSlice(s); + return mem.toSlice(u8, s); } pub fn skip(self: *ArgIteratorPosix) bool { @@ -175,10 +175,6 @@ pub const ArgIteratorPosix = struct { self.index += 1; return true; } - - /// This is marked as public but actually it's only meant to be used - /// internally by zig's startup code. - pub var raw: [][*]u8 = undefined; }; pub const ArgIteratorWindows = struct { @@ -191,7 +187,7 @@ pub const ArgIteratorWindows = struct { pub const NextError = error{OutOfMemory}; pub fn init() ArgIteratorWindows { - return initWithCmdLine(windows.GetCommandLineA()); + return initWithCmdLine(os.windows.kernel32.GetCommandLineA()); } pub fn initWithCmdLine(cmd_line: [*]const u8) ArgIteratorWindows { @@ -581,3 +577,21 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo { if (amt_read < buf.len) return error.UserNotFound; } } + +pub fn getBaseAddress() usize { + switch (builtin.os) { + .linux => { + const base = os.system.getauxval(std.elf.AT_BASE); + if (base != 0) { + return base; + } + const phdr = os.system.getauxval(std.elf.AT_PHDR); + return phdr - @sizeOf(std.elf.Ehdr); + }, + .macosx, .freebsd, .netbsd => { + return @ptrToInt(&std.c._mh_execute_header); + }, + .windows => return @ptrToInt(os.windows.kernel32.GetModuleHandleW(null)), + else => @compileError("Unsupported OS"), + } +} diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 86228acb9f..bf14116edf 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -78,7 +78,7 @@ fn posixCallMainAndExit() noreturn { while (envp_optional[envp_count]) |_| : (envp_count += 1) {} const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; - if (builtin.os == builtin.Os.linux) { + if (builtin.os == .linux) { // Find the beginning of the auxiliary vector const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1); std.os.linux.elf_aux_maybe = auxv; @@ -98,7 +98,7 @@ fn posixCallMainAndExit() noreturn { // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { - std.os.ArgIteratorPosix.raw = argv[0..argc]; + std.os.argv = argv[0..argc]; std.os.environ = envp; return callMain(); } diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index f9a322cbeb..a0a18d21d2 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -3,15 +3,15 @@ const std = @import("std"); const builtin = @import("builtin"); const io = std.io; const fmt = std.fmt; -const os = std.os; const Builder = std.build.Builder; const mem = std.mem; +const process = std.process; const ArrayList = std.ArrayList; const warn = std.debug.warn; const File = std.fs.File; pub fn main() !void { - var arg_it = os.args(); + var arg_it = process.args(); // Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived, // one shot program. We don't need to waste time freeing memory and finding places to squish @@ -139,7 +139,7 @@ pub fn main() !void { error.InvalidStepName => { return usageAndErr(&builder, true, try stderr_stream); }, - error.UncleanExit => os.exit(1), + error.UncleanExit => process.exit(1), else => return err, } }; @@ -215,7 +215,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void { fn usageAndErr(builder: *Builder, already_ran_build: bool, out_stream: var) void { usage(builder, already_ran_build, out_stream) catch {}; - os.exit(1); + process.exit(1); } const UnwrapArgError = error{OutOfMemory}; diff --git a/std/thread.zig b/std/thread.zig index eac6b73f7d..53f42e555f 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -227,7 +227,7 @@ pub const Thread = struct { var tls_start_offset: usize = undefined; const mmap_len = blk: { // First in memory will be the stack, which grows downwards. - var l: usize = mem.alignForward(default_stack_size, os.page_size); + var l: usize = mem.alignForward(default_stack_size, mem.page_size); stack_end_offset = l; // Above the stack, so that it can be in the same mmap call, put the Thread object. l = mem.alignForward(l, @alignOf(Thread)); diff --git a/test/cli.zig b/test/cli.zig index 1e7f1d0a73..cee030e4db 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -1,7 +1,9 @@ const std = @import("std"); const builtin = @import("builtin"); -const os = std.os; const testing = std.testing; +const process = std.process; +const fs = std.fs; +const ChildProcess = std.ChildProcess; var a: *std.mem.Allocator = undefined; @@ -12,7 +14,7 @@ pub fn main() !void { var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator); defer arena.deinit(); - var arg_it = os.args(); + var arg_it = process.args(); // skip my own exe name _ = arg_it.skip(); @@ -27,9 +29,9 @@ pub fn main() !void { std.debug.warn("Expected second argument to be cache root directory path\n"); return error.InvalidArgs; }); - const zig_exe = try os.path.resolve(a, [][]const u8{zig_exe_rel}); + const zig_exe = try fs.path.resolve(a, [][]const u8{zig_exe_rel}); - const dir_path = try os.path.join(a, [][]const u8{ cache_root, "clitest" }); + const dir_path = try fs.path.join(a, [][]const u8{ cache_root, "clitest" }); const TestFn = fn ([]const u8, []const u8) anyerror!void; const test_fns = []TestFn{ testZigInitLib, @@ -37,8 +39,8 @@ pub fn main() !void { testGodboltApi, }; for (test_fns) |testFn| { - try os.deleteTree(a, dir_path); - try os.makeDir(dir_path); + try fs.deleteTree(a, dir_path); + try fs.makeDir(dir_path); try testFn(zig_exe, dir_path); } } @@ -58,15 +60,15 @@ fn printCmd(cwd: []const u8, argv: []const []const u8) void { std.debug.warn("\n"); } -fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult { +fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult { const max_output_size = 100 * 1024; - const result = os.ChildProcess.exec(a, argv, cwd, null, max_output_size) catch |err| { + const result = ChildProcess.exec(a, argv, cwd, null, max_output_size) catch |err| { std.debug.warn("The following command failed:\n"); printCmd(cwd, argv); return err; }; switch (result.term) { - os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { std.debug.warn("The following command exited with error code {}:\n", code); printCmd(cwd, argv); @@ -97,10 +99,10 @@ fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void { } fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { - if (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64) return; + if (builtin.os != .linux or builtin.arch != .x86_64) return; - const example_zig_path = try os.path.join(a, [][]const u8{ dir_path, "example.zig" }); - const example_s_path = try os.path.join(a, [][]const u8{ dir_path, "example.s" }); + const example_zig_path = try fs.path.join(a, [][]const u8{ dir_path, "example.zig" }); + const example_s_path = try fs.path.join(a, [][]const u8{ dir_path, "example.s" }); try std.io.writeFile(example_zig_path, \\// Type your code here, or load an example. @@ -114,13 +116,13 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { ); const args = [][]const u8{ - zig_exe, "build-obj", - "--cache-dir", dir_path, - "--name", "example", - "--output-dir", dir_path, - "--emit", "asm", - "-mllvm", "--x86-asm-syntax=intel", - "--strip", "--release-fast", + zig_exe, "build-obj", + "--cache-dir", dir_path, + "--name", "example", + "--output-dir", dir_path, + "--emit", "asm", + "-mllvm", "--x86-asm-syntax=intel", + "--strip", "--release-fast", example_zig_path, "--disable-gen-h", }; _ = try exec(dir_path, args); diff --git a/test/tests.zig b/test/tests.zig index a17938fc3c..061d4cb55f 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -2,11 +2,9 @@ const std = @import("std"); const debug = std.debug; const warn = debug.warn; const build = std.build; -const os = std.os; -const StdIo = os.ChildProcess.StdIo; -const Term = os.ChildProcess.Term; const Buffer = std.Buffer; const io = std.io; +const fs = std.fs; const mem = std.mem; const fmt = std.fmt; const ArrayList = std.ArrayList; @@ -30,19 +28,19 @@ const TestTarget = struct { const test_targets = []TestTarget{ TestTarget{ - .os = builtin.Os.linux, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.gnu, + .os = .linux, + .arch = .x86_64, + .abi = .gnu, }, TestTarget{ - .os = builtin.Os.macosx, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.gnu, + .os = .macosx, + .arch = .x86_64, + .abi = .gnu, }, TestTarget{ - .os = builtin.Os.windows, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.msvc, + .os = .windows, + .arch = .x86_64, + .abi = .msvc, }, }; @@ -114,7 +112,7 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M const exe = b.addExecutable("test-cli", "test/cli.zig"); const run_cmd = exe.run(); run_cmd.addArgs([][]const u8{ - os.path.realAlloc(b.allocator, b.zig_exe) catch unreachable, + fs.path.realAlloc(b.allocator, b.zig_exe) catch unreachable, b.pathFromRoot(b.cache_root), }); @@ -301,12 +299,12 @@ pub const CompareOutputContext = struct { warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); - const child = os.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.env_map = b.env_map; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); @@ -324,7 +322,7 @@ pub const CompareOutputContext = struct { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("Process {} exited with error code {}\n", full_exe_path, code); printInvocation(args.toSliceConst()); @@ -383,13 +381,13 @@ pub const CompareOutputContext = struct { warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); - const child = os.ChildProcess.init([][]const u8{full_exe_path}, b.allocator) catch unreachable; + const child = std.ChildProcess.init([][]const u8{full_exe_path}, b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Ignore; - child.stderr_behavior = StdIo.Ignore; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Ignore; + child.stderr_behavior = .Ignore; const term = child.spawnAndWait() catch |err| { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); @@ -397,13 +395,13 @@ pub const CompareOutputContext = struct { const expected_exit_code: i32 = 126; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != expected_exit_code) { warn("\nProgram expected to exit with code {} " ++ "but exited with code {}\n", expected_exit_code, code); return error.TestFailed; } }, - Term.Signal => |sig| { + .Signal => |sig| { warn("\nProgram expected to exit with code {} " ++ "but instead signaled {}\n", expected_exit_code, sig); return error.TestFailed; }, @@ -459,7 +457,7 @@ pub const CompareOutputContext = struct { pub fn addCase(self: *CompareOutputContext, case: TestCase) void { const b = self.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, case.sources.items[0].filename }, ) catch unreachable; @@ -475,7 +473,7 @@ pub const CompareOutputContext = struct { exe.addAssemblyFile(root_src); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -507,7 +505,7 @@ pub const CompareOutputContext = struct { } for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -538,7 +536,7 @@ pub const CompareOutputContext = struct { } for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -633,7 +631,7 @@ pub const CompileErrorContext = struct { const self = @fieldParentPtr(CompileCmpOutputStep, "step", step); const b = self.context.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, self.case.sources.items[0].filename }, ) catch unreachable; @@ -669,13 +667,13 @@ pub const CompileErrorContext = struct { printInvocation(zig_args.toSliceConst()); } - const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err)); @@ -692,7 +690,7 @@ pub const CompileErrorContext = struct { debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code == 0) { printInvocation(zig_args.toSliceConst()); return error.CompilationIncorrectlySucceeded; @@ -823,7 +821,7 @@ pub const CompileErrorContext = struct { self.step.dependOn(&compile_and_cmp_errors.step); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -858,7 +856,7 @@ pub const BuildExamplesContext = struct { } var zig_args = ArrayList([]const u8).init(b.allocator); - const rel_zig_exe = os.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable; + const rel_zig_exe = fs.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable; zig_args.append(rel_zig_exe) catch unreachable; zig_args.append("build") catch unreachable; @@ -958,7 +956,7 @@ pub const TranslateCContext = struct { const self = @fieldParentPtr(TranslateCCmpOutputStep, "step", step); const b = self.context.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, self.case.sources.items[0].filename }, ) catch unreachable; @@ -976,13 +974,13 @@ pub const TranslateCContext = struct { printInvocation(zig_args.toSliceConst()); } - const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err)); @@ -999,14 +997,14 @@ pub const TranslateCContext = struct { debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("Compilation failed with exit code {}\n", code); printInvocation(zig_args.toSliceConst()); return error.TestFailed; } }, - Term.Signal => |code| { + .Signal => |code| { warn("Compilation failed with signal {}\n", code); printInvocation(zig_args.toSliceConst()); return error.TestFailed; @@ -1131,7 +1129,7 @@ pub const TranslateCContext = struct { self.step.dependOn(&translate_c_and_cmp.step); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -1254,7 +1252,7 @@ pub const GenHContext = struct { pub fn addCase(self: *GenHContext, case: *const TestCase) void { const b = self.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, case.sources.items[0].filename }, ) catch unreachable; @@ -1269,7 +1267,7 @@ pub const GenHContext = struct { obj.setBuildMode(mode); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; -- cgit v1.2.3 From 0c6ab61b228211398841cf11912c7252362009b7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 May 2019 23:35:26 -0400 Subject: tests passing on linux --- build.zig | 6 +-- doc/langref.html.in | 4 +- example/cat/main.zig | 9 +++-- example/guess_number/main.zig | 3 +- example/hello_world/hello_libc.zig | 2 +- src-self-hosted/compilation.zig | 1 + src-self-hosted/libc_installation.zig | 6 +-- src-self-hosted/main.zig | 19 +++++----- std/c.zig | 13 +++++-- std/c/linux.zig | 22 +++++++++-- std/child_process.zig | 18 ++++----- std/cstr.zig | 2 +- std/dynamic_library.zig | 38 +++++++------------ std/event/fs.zig | 14 +++---- std/event/loop.zig | 32 ++++++++-------- std/event/net.zig | 27 +++++++------ std/fs.zig | 26 ++----------- std/fs/file.zig | 15 +++++--- std/fs/path.zig | 15 ++++---- std/heap.zig | 2 +- std/io/test.zig | 8 ++-- std/os.zig | 71 ++++++++++++++++++++--------------- std/os/bits/darwin.zig | 16 ++++---- std/os/bits/freebsd.zig | 28 ++++++-------- std/os/bits/linux.zig | 30 +++++++-------- std/os/bits/netbsd.zig | 26 +++++-------- std/os/linux.zig | 2 +- std/os/linux/test.zig | 2 +- std/os/linux/vdso.zig | 2 +- std/os/test.zig | 35 ++++++++--------- std/os/windows.zig | 38 +++++++++++++++++++ std/os/zen.zig | 2 +- std/process.zig | 22 ++++++++++- std/thread.zig | 56 +++++++++++++++++---------- std/time.zig | 24 ++++-------- test/compare_output.zig | 6 +-- test/standalone/empty_env/main.zig | 2 +- test/tests.zig | 2 +- 38 files changed, 348 insertions(+), 298 deletions(-) (limited to 'std/process.zig') diff --git a/build.zig b/build.zig index bb23502d82..82949ad5bc 100644 --- a/build.zig +++ b/build.zig @@ -166,10 +166,8 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { } fn fileExists(filename: []const u8) !bool { - fs.File.exists(filename) catch |err| switch (err) { - error.PermissionDenied, - error.FileNotFound, - => return false, + fs.File.access(filename) catch |err| switch (err) { + error.FileNotFound => return false, else => return err, }; return true; diff --git a/doc/langref.html.in b/doc/langref.html.in index 5b9beba644..19839904b1 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -796,8 +796,8 @@ const assert = std.debug.assert; threadlocal var x: i32 = 1234; test "thread local storage" { - const thread1 = try std.os.spawnThread({}, testTls); - const thread2 = try std.os.spawnThread({}, testTls); + const thread1 = try std.Thread.spawn({}, testTls); + const thread2 = try std.Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); diff --git a/example/cat/main.zig b/example/cat/main.zig index 7255217831..c57c1e4bcb 100644 --- a/example/cat/main.zig +++ b/example/cat/main.zig @@ -1,12 +1,13 @@ const std = @import("std"); const io = std.io; +const process = std.process; +const File = std.fs.File; const mem = std.mem; -const os = std.os; const warn = std.debug.warn; const allocator = std.debug.global_allocator; pub fn main() !void { - var args_it = os.args(); + var args_it = process.args(); const exe = try unwrapArg(args_it.next(allocator).?); var catted_anything = false; var stdout_file = try io.getStdOut(); @@ -20,7 +21,7 @@ pub fn main() !void { } else if (arg[0] == '-') { return usage(exe); } else { - var file = os.File.openRead(arg) catch |err| { + var file = File.openRead(arg) catch |err| { warn("Unable to open file: {}\n", @errorName(err)); return err; }; @@ -41,7 +42,7 @@ fn usage(exe: []const u8) !void { return error.Invalid; } -fn cat_file(stdout: *os.File, file: *os.File) !void { +fn cat_file(stdout: *File, file: *File) !void { var buf: [1024 * 4]u8 = undefined; while (true) { diff --git a/example/guess_number/main.zig b/example/guess_number/main.zig index b4eb1c292a..b1e557fd20 100644 --- a/example/guess_number/main.zig +++ b/example/guess_number/main.zig @@ -2,7 +2,6 @@ const builtin = @import("builtin"); const std = @import("std"); const io = std.io; const fmt = std.fmt; -const os = std.os; pub fn main() !void { var stdout_file = try io.getStdOut(); @@ -11,7 +10,7 @@ pub fn main() !void { try stdout.print("Welcome to the Guess Number Game in Zig.\n"); var seed_bytes: [@sizeOf(u64)]u8 = undefined; - os.getRandomBytes(seed_bytes[0..]) catch |err| { + std.crypto.randomBytes(seed_bytes[0..]) catch |err| { std.debug.warn("unable to seed random number generator: {}", err); return err; }; diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig index 3f077283db..d18b1c26b3 100644 --- a/example/hello_world/hello_libc.zig +++ b/example/hello_world/hello_libc.zig @@ -5,6 +5,6 @@ const c = @cImport({ }); export fn main(argc: c_int, argv: [*]?[*]u8) c_int { - c.fprintf(c.stderr, c"Hello, world!\n"); + _ = c.fprintf(c.stderr, c"Hello, world!\n"); return 0; } diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 720c26945d..450fde7219 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -301,6 +301,7 @@ pub const Compilation = struct { InvalidUtf8, BadPathName, DeviceBusy, + CurrentWorkingDirectoryUnlinked, }; pub const Event = union(enum) { diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index 53790ec8f4..7ca849d10c 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -182,7 +182,7 @@ pub const LibCInstallation = struct { } async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void { - const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.os.getenv("CC") orelse "cc"; const argv = []const []const u8{ cc_exe, "-E", @@ -392,7 +392,7 @@ pub const LibCInstallation = struct { /// caller owns returned memory async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 { - const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.os.getenv("CC") orelse "cc"; const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file); defer loop.allocator.free(arg1); const argv = []const []const u8{ cc_exe, arg1 }; @@ -463,7 +463,7 @@ fn fileExists(path: []const u8) !bool { if (fs.File.access(path)) |_| { return true; } else |err| switch (err) { - error.FileNotFound, error.PermissionDenied => return false, + error.FileNotFound => return false, else => return error.FileSystem, } } diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 1d2ac4917b..4095caa8c4 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -702,6 +702,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, + CurrentWorkingDirectoryUnlinked, } || fs.File.OpenError; async fn asyncFmtMain( @@ -851,7 +852,7 @@ fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void { } fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void { - try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); + try stdout.print("{}\n", std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING)); } const args_test_spec = []Flag{Flag.Bool("--help")}; @@ -924,14 +925,14 @@ fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void { \\ZIG_DIA_GUIDS_LIB {} \\ , - std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR), - std.cstr.toSliceConst(c.ZIG_CXX_COMPILER), - std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE), - std.cstr.toSliceConst(c.ZIG_LLD_INCLUDE_PATH), - std.cstr.toSliceConst(c.ZIG_LLD_LIBRARIES), - std.cstr.toSliceConst(c.ZIG_STD_FILES), - std.cstr.toSliceConst(c.ZIG_C_HEADER_FILES), - std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB), + std.mem.toSliceConst(u8, c.ZIG_CMAKE_BINARY_DIR), + std.mem.toSliceConst(u8, c.ZIG_CXX_COMPILER), + std.mem.toSliceConst(u8, c.ZIG_LLVM_CONFIG_EXE), + std.mem.toSliceConst(u8, c.ZIG_LLD_INCLUDE_PATH), + std.mem.toSliceConst(u8, c.ZIG_LLD_LIBRARIES), + std.mem.toSliceConst(u8, c.ZIG_STD_FILES), + std.mem.toSliceConst(u8, c.ZIG_C_HEADER_FILES), + std.mem.toSliceConst(u8, c.ZIG_DIA_GUIDS_LIB), ); } diff --git a/std/c.zig b/std/c.zig index 9171a99ec9..90d8f0056e 100644 --- a/std/c.zig +++ b/std/c.zig @@ -39,8 +39,8 @@ pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int; pub extern "c" fn raise(sig: c_int) c_int; pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize; pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; -pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; +pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: usize) isize; +pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: usize) isize; pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int; pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize; pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; @@ -49,7 +49,7 @@ pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int; pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int; pub extern "c" fn unlink(path: [*]const u8) c_int; pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8; -pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int; +pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_uint, options: c_uint) c_int; pub extern "c" fn fork() c_int; pub extern "c" fn access(path: [*]const u8, mode: c_uint) c_int; pub extern "c" fn pipe(fds: *[2]fd_t) c_int; @@ -76,7 +76,12 @@ pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usi pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int; -pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int; +pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; +pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int; +pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int; +pub extern "c" fn connect(sockfd: fd_t, sock_addr: *const sockaddr, addrlen: socklen_t) c_int; +pub extern "c" fn accept4(sockfd: fd_t, addr: *sockaddr, addrlen: *socklen_t, flags: c_uint) c_int; +pub extern "c" fn getsockopt(sockfd: fd_t, level: c_int, optname: c_int, optval: *c_void, optlen: *socklen_t) c_int; pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int; pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize; pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int; diff --git a/std/c/linux.zig b/std/c/linux.zig index 00fc600dab..9e028728c7 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -1,13 +1,27 @@ const std = @import("../std.zig"); use std.c; -pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; -pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; +pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; +pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; +pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int; +pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: *epoll_event) c_int; +pub extern "c" fn epoll_create1(flags: c_uint) c_int; +pub extern "c" fn epoll_wait(epfd: fd_t, events: [*]epoll_event, maxevents: c_uint, timeout: c_int) c_int; +pub extern "c" fn epoll_pwait( + epfd: fd_t, + events: [*]epoll_event, + maxevents: c_int, + timeout: c_int, + sigmask: *const sigset_t, +) c_int; +pub extern "c" fn inotify_init1(flags: c_uint) c_int; +pub extern "c" fn inotify_add_watch(fd: fd_t, pathname: [*]const u8, mask: u32) c_int; + /// See std.elf for constants for this -pub extern fn getauxval(__type: c_ulong) c_ulong; +pub extern "c" fn getauxval(__type: c_ulong) c_ulong; pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int; -pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; +pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; diff --git a/std/child_process.zig b/std/child_process.zig index f870f5e083..2209b2acc3 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -54,10 +54,10 @@ pub const ChildProcess = struct { os.ChangeCurDirError || windows.CreateProcessError; pub const Term = union(enum) { - Exited: i32, - Signal: i32, - Stopped: i32, - Unknown: i32, + Exited: u32, + Signal: u32, + Stopped: u32, + Unknown: u32, }; pub const StdIo = enum { @@ -155,7 +155,7 @@ pub const ChildProcess = struct { } pub const ExecResult = struct { - term: os.ChildProcess.Term, + term: Term, stdout: []u8, stderr: []u8, }; @@ -224,7 +224,7 @@ pub const ChildProcess = struct { if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) { break :x Term{ .Unknown = 0 }; } else { - break :x Term{ .Exited = @bitCast(i32, exit_code) }; + break :x Term{ .Exited = exit_code }; } }); @@ -240,7 +240,7 @@ pub const ChildProcess = struct { self.handleWaitResult(status); } - fn handleWaitResult(self: *ChildProcess, status: i32) void { + fn handleWaitResult(self: *ChildProcess, status: u32) void { self.term = self.cleanupAfterWait(status); } @@ -259,7 +259,7 @@ pub const ChildProcess = struct { } } - fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term { + fn cleanupAfterWait(self: *ChildProcess, status: u32) !Term { defer { os.close(self.err_pipe[0]); os.close(self.err_pipe[1]); @@ -281,7 +281,7 @@ pub const ChildProcess = struct { return statusToTerm(status); } - fn statusToTerm(status: i32) Term { + fn statusToTerm(status: u32) Term { return if (os.WIFEXITED(status)) Term{ .Exited = os.WEXITSTATUS(status) } else if (os.WIFSIGNALED(status)) diff --git a/std/cstr.zig b/std/cstr.zig index c8c3447921..dd28e50449 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -28,7 +28,7 @@ test "cstr fns" { fn testCStrFnsImpl() void { testing.expect(cmp(c"aoeu", c"aoez") == -1); - testing.expect(len(c"123456789") == 9); + testing.expect(mem.len(u8, c"123456789") == 9); } /// Returns a mutable slice with 1 more byte of length which is a null byte. diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index ce721350ea..3ae3b4c66a 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -6,8 +6,7 @@ const os = std.os; const assert = std.debug.assert; const testing = std.testing; const elf = std.elf; -const windows = os.windows; -const win_util = @import("os/windows/util.zig"); +const windows = std.os.windows; const maxInt = std.math.maxInt; pub const DynLib = switch (builtin.os) { @@ -102,17 +101,16 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator { pub const LinuxDynLib = struct { elf_lib: ElfLib, fd: i32, - map_addr: usize, - map_size: usize, + memory: []align(mem.page_size) u8, /// Trusts the file pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib { const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC); - errdefer std.os.close(fd); + errdefer os.close(fd); - const size = @intCast(usize, (try std.os.posixFStat(fd)).size); + const size = @intCast(usize, (try os.fstat(fd)).size); - const addr = os.mmap( + const bytes = try os.mmap( null, size, os.PROT_READ | os.PROT_EXEC, @@ -120,21 +118,18 @@ pub const LinuxDynLib = struct { fd, 0, ); - errdefer os.munmap(addr, size); - - const bytes = @intToPtr([*]align(mem.page_size) u8, addr)[0..size]; + errdefer os.munmap(bytes); return DynLib{ .elf_lib = try ElfLib.init(bytes), .fd = fd, - .map_addr = addr, - .map_size = size, + .memory = bytes, }; } pub fn close(self: *DynLib) void { - os.munmap(self.map_addr, self.map_size); - std.os.close(self.fd); + os.munmap(self.memory); + os.close(self.fd); self.* = undefined; } @@ -253,28 +248,21 @@ pub const WindowsDynLib = struct { dll: windows.HMODULE, pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib { - const wpath = try win_util.sliceToPrefixedFileW(path); + const wpath = try windows.sliceToPrefixedFileW(path); return WindowsDynLib{ .allocator = allocator, - .dll = windows.LoadLibraryW(&wpath) orelse { - switch (windows.GetLastError()) { - windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, - windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, - windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound, - else => |err| return windows.unexpectedError(err), - } - }, + .dll = try windows.LoadLibraryW(&wpath), }; } pub fn close(self: *WindowsDynLib) void { - assert(windows.FreeLibrary(self.dll) != 0); + windows.FreeLibrary(self.dll); self.* = undefined; } pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize { - return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr)); + return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr)); } }; diff --git a/std/event/fs.zig b/std/event/fs.zig index 221070a062..81d180235e 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -36,7 +36,7 @@ pub const Request = struct { offset: usize, result: Error!void, - pub const Error = os.PosixWriteError; + pub const Error = os.WriteError; }; pub const PReadV = struct { @@ -45,7 +45,7 @@ pub const Request = struct { offset: usize, result: Error!usize, - pub const Error = os.PosixReadError; + pub const Error = os.ReadError; }; pub const Open = struct { @@ -172,7 +172,7 @@ pub async fn pwritevPosix( fd: fd_t, iovecs: []const os.iovec_const, offset: usize, -) os.PosixWriteError!void { +) os.WriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -320,7 +320,7 @@ pub async fn preadvPosix( fd: fd_t, iovecs: []const os.iovec, offset: usize, -) os.PosixReadError!usize { +) os.ReadError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -786,7 +786,7 @@ pub fn Watch(comptime V: type) type { switch (builtin.os) { builtin.Os.linux => { - const inotify_fd = try os.linuxINotifyInit1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC); + const inotify_fd = try os.inotify_init1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC); errdefer os.close(inotify_fd); var result: *Self = undefined; @@ -977,7 +977,7 @@ pub fn Watch(comptime V: type) type { var basename_with_null_consumed = false; defer if (!basename_with_null_consumed) self.channel.loop.allocator.free(basename_with_null); - const wd = try os.linuxINotifyAddWatchC( + const wd = try os.inotify_add_watchC( self.os_data.inotify_fd, dirname_with_null.ptr, os.linux.IN_CLOSE_WRITE | os.linux.IN_ONLYDIR | os.linux.IN_EXCL_UNLINK, @@ -1255,7 +1255,7 @@ pub fn Watch(comptime V: type) type { ev = @ptrCast(*os.linux.inotify_event, ptr); if (ev.mask & os.linux.IN_CLOSE_WRITE == os.linux.IN_CLOSE_WRITE) { const basename_ptr = ptr + @sizeOf(os.linux.inotify_event); - const basename_with_null = basename_ptr[0 .. std.cstr.len(basename_ptr) + 1]; + const basename_with_null = basename_ptr[0 .. std.mem.len(u8, basename_ptr) + 1]; const user_value = blk: { const held = await (async watch.os_data.table_lock.acquire() catch unreachable); defer held.release(); diff --git a/std/event/loop.zig b/std/event/loop.zig index e1848684c7..ae19ebb0f8 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -99,7 +99,7 @@ pub const Loop = struct { /// have the correct pointer value. pub fn initMultiThreaded(self: *Loop, allocator: *mem.Allocator) !void { if (builtin.single_threaded) @compileError("initMultiThreaded unavailable when building in single-threaded mode"); - const core_count = try os.cpuCount(allocator); + const core_count = try Thread.cpuCount(); return self.initInternal(allocator, core_count); } @@ -139,9 +139,9 @@ pub const Loop = struct { self.allocator.free(self.extra_threads); } - const InitOsDataError = os.LinuxEpollCreateError || mem.Allocator.Error || os.LinuxEventFdError || - os.SpawnThreadError || os.LinuxEpollCtlError || os.BsdKEventError || - os.WindowsCreateIoCompletionPortError; + const InitOsDataError = os.EpollCreateError || mem.Allocator.Error || os.EventFdError || + Thread.SpawnError || os.EpollCtlError || os.KEventError || + windows.CreateIoCompletionPortError; const wakeup_bytes = []u8{0x1} ** 8; @@ -172,7 +172,7 @@ pub const Loop = struct { .handle = undefined, .overlapped = ResumeNode.overlapped_init, }, - .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), + .eventfd = try os.eventfd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), .epoll_op = os.EPOLL_CTL_ADD, }, .next = undefined, @@ -180,17 +180,17 @@ pub const Loop = struct { self.available_eventfd_resume_nodes.push(eventfd_node); } - self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC); + self.os_data.epollfd = try os.epoll_create1(os.EPOLL_CLOEXEC); errdefer os.close(self.os_data.epollfd); - self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); + self.os_data.final_eventfd = try os.eventfd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); errdefer os.close(self.os_data.final_eventfd); self.os_data.final_eventfd_event = os.epoll_event{ .events = os.EPOLLIN, .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, }; - try os.linuxEpollCtl( + try os.epoll_ctl( self.os_data.epollfd, os.EPOLL_CTL_ADD, self.os_data.final_eventfd, @@ -211,7 +211,7 @@ pub const Loop = struct { var extra_thread_index: usize = 0; errdefer { // writing 8 bytes to an eventfd cannot fail - os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; + os.write(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; while (extra_thread_index != 0) { extra_thread_index -= 1; self.extra_threads[extra_thread_index].wait(); @@ -417,11 +417,11 @@ pub const Loop = struct { .events = flags, .data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) }, }; - try os.linuxEpollCtl(self.os_data.epollfd, op, fd, &ev); + try os.epoll_ctl(self.os_data.epollfd, op, fd, &ev); } pub fn linuxRemoveFd(self: *Loop, fd: i32) void { - os.linuxEpollCtl(self.os_data.epollfd, os.linux.EPOLL_CTL_DEL, fd, undefined) catch {}; + os.epoll_ctl(self.os_data.epollfd, os.linux.EPOLL_CTL_DEL, fd, undefined) catch {}; self.finishOneEvent(); } @@ -626,7 +626,7 @@ pub const Loop = struct { builtin.Os.linux => { self.posixFsRequest(&self.os_data.fs_end_request); // writing 8 bytes to an eventfd cannot fail - os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; + os.write(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; return; }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { @@ -666,7 +666,7 @@ pub const Loop = struct { builtin.Os.linux => { // only process 1 event so we don't steal from other threads var events: [1]os.linux.epoll_event = undefined; - const count = os.linuxEpollWait(self.os_data.epollfd, events[0..], -1); + const count = os.epoll_wait(self.os_data.epollfd, events[0..], -1); for (events[0..count]) |ev| { const resume_node = @intToPtr(*ResumeNode, ev.data.ptr); const handle = resume_node.handle; @@ -783,10 +783,10 @@ pub const Loop = struct { switch (node.data.msg) { @TagType(fs.Request.Msg).End => return, @TagType(fs.Request.Msg).PWriteV => |*msg| { - msg.result = os.posix_pwritev(msg.fd, msg.iov.ptr, msg.iov.len, msg.offset); + msg.result = os.pwritev(msg.fd, msg.iov, msg.offset); }, @TagType(fs.Request.Msg).PReadV => |*msg| { - msg.result = os.posix_preadv(msg.fd, msg.iov.ptr, msg.iov.len, msg.offset); + msg.result = os.preadv(msg.fd, msg.iov, msg.offset); }, @TagType(fs.Request.Msg).Open => |*msg| { msg.result = os.openC(msg.path.ptr, msg.flags, msg.mode); @@ -800,7 +800,7 @@ pub const Loop = struct { break :blk; }; defer os.close(fd); - msg.result = os.posixWrite(fd, msg.contents); + msg.result = os.write(fd, msg.contents); }, } switch (node.data.finish) { diff --git a/std/event/net.zig b/std/event/net.zig index 2346b1eb23..f4398196e3 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -45,13 +45,13 @@ pub const Server = struct { ) !void { self.handleRequestFn = handleRequestFn; - const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); + const sockfd = try os.socket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); self.sockfd = sockfd; - try os.posixBind(sockfd, &address.os_addr); - try os.posixListen(sockfd, os.SOMAXCONN); - self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd)); + try os.bind(sockfd, &address.os_addr); + try os.listen(sockfd, os.SOMAXCONN); + self.listen_address = std.net.Address.initPosix(try os.getsockname(sockfd)); self.accept_coro = try async Server.handler(self); errdefer cancel self.accept_coro.?; @@ -64,7 +64,10 @@ pub const Server = struct { /// Stop listening pub fn close(self: *Server) void { self.loop.linuxRemoveFd(self.sockfd.?); - os.close(self.sockfd.?); + if (self.sockfd) |fd| { + os.close(fd); + self.sockfd = null; + } } pub fn deinit(self: *Server) void { @@ -76,7 +79,7 @@ pub const Server = struct { while (true) { var accepted_addr: std.net.Address = undefined; // TODO just inline the following function here and don't expose it as posixAsyncAccept - if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { + if (os.accept4_async(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { if (accepted_fd == -1) { // would block suspend; // we will get resumed by epoll_wait in the event loop @@ -105,7 +108,7 @@ pub const Server = struct { }; pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { - const sockfd = try os.posixSocket( + const sockfd = try os.socket( os.AF_UNIX, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, 0, @@ -120,9 +123,9 @@ pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong; mem.copy(u8, sock_addr.path[0..], path); const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); - try os.posixConnectAsync(sockfd, &sock_addr, size); + try os.connect_async(sockfd, &sock_addr, size); try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); - try os.posixGetSockOptConnectError(sockfd); + try os.getsockoptError(sockfd); return sockfd; } @@ -249,12 +252,12 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 - const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); + const sockfd = try os.socket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); - try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); + try os.connect_async(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); - try os.posixGetSockOptConnectError(sockfd); + try os.getsockoptError(sockfd); return File.openHandle(sockfd); } diff --git a/std/fs.zig b/std/fs.zig index 229f3099c2..5c0db71435 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -38,24 +38,6 @@ pub const MAX_PATH_BYTES = switch (builtin.os) { else => @compileError("Unsupported OS"), }; -/// The result is a slice of `out_buffer`, from index `0`. -pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { - return os.getcwd(out_buffer); -} - -/// Caller must free the returned memory. -pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { - var buf: [MAX_PATH_BYTES]u8 = undefined; - return mem.dupe(allocator, u8, try os.getcwd(&buf)); -} - -test "getCwdAlloc" { - // at least call it so it gets compiled - var buf: [1000]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; - _ = getCwdAlloc(allocator) catch {}; -} - // here we replace the standard +/ with -_ so that it can be used in a file name const b64_fs_encoder = base64.Base64Encoder.init("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", base64.standard_pad_char); @@ -260,17 +242,17 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { /// Returns `error.DirNotEmpty` if the directory is not empty. /// To delete a directory recursively, see `deleteTree`. -pub fn deleteDir(dir_path: []const u8) DeleteDirError!void { +pub fn deleteDir(dir_path: []const u8) !void { return os.rmdir(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF8-encoded string. -pub fn deleteDirC(dir_path: [*]const u8) DeleteDirError!void { +pub fn deleteDirC(dir_path: [*]const u8) !void { return os.rmdirC(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF16LE-encoded string. -pub fn deleteDirW(dir_path: [*]const u16) DeleteDirError!void { +pub fn deleteDirW(dir_path: [*]const u16) !void { return os.rmdirW(dir_path); } @@ -362,7 +344,7 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError! }; defer dir.close(); - var full_entry_buf = ArrayList(u8).init(allocator); + var full_entry_buf = std.ArrayList(u8).init(allocator); defer full_entry_buf.deinit(); while (try dir.next()) |entry| { diff --git a/std/fs/file.zig b/std/fs/file.zig index 915dae331f..7cb722292c 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -137,24 +137,27 @@ pub const File = struct { /// Test for the existence of `path`. /// `path` is UTF8-encoded. - pub fn exists(path: []const u8) !void { + /// In general it is recommended to avoid this function. For example, + /// instead of testing if a file exists and then opening it, just + /// open it and handle the error for file not found. + pub fn access(path: []const u8) !void { return os.access(path, os.F_OK); } - /// Same as `exists` except the parameter is null-terminated. - pub fn existsC(path: [*]const u8) !void { + /// Same as `access` except the parameter is null-terminated. + pub fn accessC(path: [*]const u8) !void { return os.accessC(path, os.F_OK); } - /// Same as `exists` except the parameter is null-terminated UTF16LE-encoded. - pub fn existsW(path: [*]const u16) !void { + /// Same as `access` except the parameter is null-terminated UTF16LE-encoded. + pub fn accessW(path: [*]const u16) !void { return os.accessW(path, os.F_OK); } /// Upon success, the stream is in an uninitialized state. To continue using it, /// you must use the open() function. pub fn close(self: File) void { - os.close(self.handle); + return os.close(self.handle); } /// Test whether the file refers to a terminal. diff --git a/std/fs/path.zig b/std/fs/path.zig index 217bb44327..7b95a3e4d2 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -9,6 +9,7 @@ const Allocator = mem.Allocator; const math = std.math; const windows = std.os.windows; const fs = std.fs; +const process = std.process; pub const sep_windows = '\\'; pub const sep_posix = '/'; @@ -390,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - return fs.getCwdAlloc(allocator); + return process.getCwdAlloc(allocator); } // determine which disk designator we will result with, if any @@ -485,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, WindowsPath.Kind.None => { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); @@ -501,7 +502,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { } else { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -571,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - return fs.getCwdAlloc(allocator); + return process.getCwdAlloc(allocator); } var first_index: usize = 0; @@ -593,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { result = try allocator.alloc(u8, max_size); } else { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); @@ -632,7 +633,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { } test "resolve" { - const cwd = try fs.getCwdAlloc(debug.global_allocator); + const cwd = try process.getCwdAlloc(debug.global_allocator); if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); @@ -646,7 +647,7 @@ test "resolve" { test "resolveWindows" { if (windows.is_the_target) { - const cwd = try fs.getCwdAlloc(debug.global_allocator); + const cwd = try process.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }); diff --git a/std/heap.zig b/std/heap.zig index 198eb2d073..6c6a97d037 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -112,7 +112,7 @@ pub const DirectAllocator = struct { -1, 0, ) catch return error.OutOfMemory; - if (alloc_size == n) return slice; + if (alloc_size == n) return slice[0..n]; const aligned_addr = mem.alignForward(@ptrToInt(slice.ptr), alignment); diff --git a/std/io/test.zig b/std/io/test.zig index 7928baa597..fc3b0f8902 100644 --- a/std/io/test.zig +++ b/std/io/test.zig @@ -7,7 +7,7 @@ const DefaultPrng = std.rand.DefaultPrng; const expect = std.testing.expect; const expectError = std.testing.expectError; const mem = std.mem; -const os = std.os; +const fs = std.fs; const File = std.fs.File; test "write a file, read it, then delete it" { @@ -58,7 +58,7 @@ test "write a file, read it, then delete it" { expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data)); expect(mem.eql(u8, contents[contents.len - "end".len ..], "end")); } - try os.deleteFile(tmp_file_name); + try fs.deleteFile(tmp_file_name); } test "BufferOutStream" { @@ -316,7 +316,7 @@ test "BitStreams with File Stream" { expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1)); } - try os.deleteFile(tmp_file_name); + try fs.deleteFile(tmp_file_name); } fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packing: io.Packing) !void { @@ -596,7 +596,7 @@ test "c out stream" { const filename = c"tmp_io_test_file.txt"; const out_file = std.c.fopen(filename, c"w") orelse return error.UnableToOpenTestFile; - defer std.os.deleteFileC(filename) catch {}; + defer fs.deleteFileC(filename) catch {}; const out_stream = &io.COutStream.init(out_file).stream; try out_stream.print("hi: {}\n", i32(123)); diff --git a/std/os.zig b/std/os.zig index 1debb16234..087d5e12a5 100644 --- a/std/os.zig +++ b/std/os.zig @@ -284,7 +284,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// This function is for blocking file descriptors only. For non-blocking, see /// `preadvAsync`. -pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { +pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize { if (darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; @@ -301,7 +301,7 @@ pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadErro if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; - if (iov_i == count) { + if (iov_i == iov.len) { return off; } } @@ -323,9 +323,10 @@ pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadErro } } while (true) { - const rc = system.preadv(fd, iov, count, offset); + // TODO handle the case when iov_len is too large and get rid of this @intCast + const rc = system.preadv(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { - 0 => return rc, + 0 => return @bitCast(usize, rc), EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, @@ -407,7 +408,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { /// Write multiple buffers to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `pwritevAsync`. -pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) WriteError!void { +pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void { if (darwin.is_the_target) { // Darwin does not have pwritev but it does have pwrite. var off: usize = 0; @@ -424,7 +425,7 @@ pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) W if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; - if (iov_i == count) { + if (iov_i == iov.len) { return; } } @@ -449,7 +450,8 @@ pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) W } while (true) { - const rc = system.pwritev(fd, iov, count, offset); + // TODO handle the case when iov_len is too large and get rid of this @intCast + const rc = system.pwritev(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { 0 => return, EINTR => continue, @@ -724,7 +726,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, - else => return unexpectedErrno(err), + else => return unexpectedErrno(@intCast(usize, err)), } } @@ -1121,7 +1123,7 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 { } const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len); switch (errno(rc)) { - 0 => return out_buffer[0..rc], + 0 => return out_buffer[0..@bitCast(usize, rc)], EACCES => return error.AccessDenied, EFAULT => unreachable, EINVAL => unreachable, @@ -1307,7 +1309,7 @@ pub const BindError = error{ /// addr is `*const T` where T is one of the sockaddr pub fn bind(fd: i32, addr: *const sockaddr) BindError!void { - const rc = system.bind(fd, system, @sizeOf(sockaddr)); + const rc = system.bind(fd, addr, @sizeOf(sockaddr)); switch (errno(rc)) { 0 => return, EACCES => return error.AccessDenied, @@ -1521,7 +1523,7 @@ pub fn epoll_wait(epfd: i32, events: []epoll_event, timeout: i32) usize { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout); switch (errno(rc)) { - 0 => return rc, + 0 => return @intCast(usize, rc), EINTR => continue, EBADF => unreachable, EFAULT => unreachable, @@ -1613,12 +1615,10 @@ pub const ConnectError = error{ /// Initiate a connection on a socket. /// This is for blocking file descriptors only. /// For non-blocking, see `connect_async`. -pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { +pub fn connect(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { - switch (errno(system.connect(sockfd, sockaddr, @sizeOf(sockaddr)))) { + switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) { 0 => return, - else => |err| return unexpectedErrno(err), - EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, @@ -1636,19 +1636,18 @@ pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, + else => |err| return unexpectedErrno(err), } } } /// Same as `connect` except it is for blocking socket file descriptors. /// It expects to receive EINPROGRESS`. -pub fn connect_async(sockfd: i32, sockaddr: *const c_void, len: u32) ConnectError!void { +pub fn connect_async(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { - switch (errno(system.connect(sockfd, sockaddr, len))) { - 0, EINPROGRESS => return, + switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) { EINTR => continue, - else => |err| return unexpectedErrno(err), - + 0, EINPROGRESS => return, EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, @@ -1664,13 +1663,14 @@ pub fn connect_async(sockfd: i32, sockaddr: *const c_void, len: u32) ConnectErro ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, + else => |err| return unexpectedErrno(err), } } } pub fn getsockoptError(sockfd: i32) ConnectError!void { - var err_code: i32 = undefined; - var size: u32 = @sizeOf(i32); + var err_code: u32 = undefined; + var size: u32 = @sizeOf(u32); const rc = system.getsockopt(sockfd, SOL_SOCKET, SO_ERROR, @ptrCast([*]u8, &err_code), &size); assert(size == 4); switch (errno(rc)) { @@ -1702,11 +1702,13 @@ pub fn getsockoptError(sockfd: i32) ConnectError!void { } } -pub fn waitpid(pid: i32, flags: u32) i32 { - var status: i32 = undefined; +pub fn waitpid(pid: i32, flags: u32) u32 { + // TODO allow implicit pointer cast from *u32 to *c_uint ? + const Status = if (builtin.link_libc) c_uint else u32; + var status: Status = undefined; while (true) { switch (errno(system.waitpid(pid, &status, flags))) { - 0 => return status, + 0 => return @bitCast(u32, status), EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. EINVAL => unreachable, // The options argument was invalid @@ -1892,11 +1894,19 @@ pub fn fork() ForkError!pid_t { } pub const MMapError = error{ + /// The underlying filesystem of the specified file does not support memory mapping. + MemoryMappingNotSupported, + + /// A file descriptor refers to a non-regular file. Or a file mapping was requested, + /// but the file descriptor is not open for reading. Or `MAP_SHARED` was requested + /// and `PROT_WRITE` is set, but the file descriptor is not open in `O_RDWR` mode. + /// Or `PROT_WRITE` is set, but the file is append-only. AccessDenied, + + /// The `prot` argument asks for `PROT_EXEC` but the mapped area belongs to a file on + /// a filesystem that was mounted no-exec. PermissionDenied, LockedMemoryLimitExceeded, - SystemFdQuotaExceeded, - MemoryMappingNotSupported, OutOfMemory, Unexpected, }; @@ -1932,7 +1942,6 @@ pub fn mmap( EAGAIN => return error.LockedMemoryLimitExceeded, EBADF => unreachable, // Always a race condition. EOVERFLOW => unreachable, // The number of pages used for length + offset would overflow. - ENFILE => return error.SystemFdQuotaExceeded, ENODEV => return error.MemoryMappingNotSupported, EINVAL => unreachable, // Invalid parameters to mmap() ENOMEM => return error.OutOfMemory, @@ -2265,7 +2274,7 @@ pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPat ENAMETOOLONG => return error.NameTooLong, ELOOP => return error.SymLinkLoop, EIO => return error.InputOutput, - else => |err| return unexpectedErrno(err), + else => |err| return unexpectedErrno(@intCast(usize, err)), }; return mem.toSlice(u8, result_path); } @@ -2349,7 +2358,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { } pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { - switch (errno(system.clock_getres(clk_id, tp))) { + switch (errno(system.clock_getres(clk_id, res))) { 0 => return, EFAULT => unreachable, EINVAL => return error.UnsupportedClock, @@ -2364,7 +2373,7 @@ pub const SchedGetAffinityError = error{ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { var set: cpu_set_t = undefined; - switch (errno(system.sched_getaffinity(pid, &set))) { + switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) { 0 => return set, EFAULT => unreachable, EINVAL => unreachable, diff --git a/std/os/bits/darwin.zig b/std/os/bits/darwin.zig index ed64a6426a..6c578bc57e 100644 --- a/std/os/bits/darwin.zig +++ b/std/os/bits/darwin.zig @@ -219,7 +219,7 @@ pub const MAP_NOCACHE = 0x0400; /// don't reserve needed swap area pub const MAP_NORESERVE = 0x0040; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); /// [XSI] no hang in wait/no child to reap pub const WNOHANG = 0x00000001; @@ -749,26 +749,26 @@ pub const IPPROTO_UDP = 17; pub const IPPROTO_IP = 0; pub const IPPROTO_IPV6 = 41; -fn wstatus(x: i32) i32 { +fn wstatus(x: u32) u32 { return x & 0o177; } const wstopped = 0o177; -pub fn WEXITSTATUS(x: i32) i32 { +pub fn WEXITSTATUS(x: u32) u32 { return x >> 8; } -pub fn WTERMSIG(x: i32) i32 { +pub fn WTERMSIG(x: u32) u32 { return wstatus(x); } -pub fn WSTOPSIG(x: i32) i32 { +pub fn WSTOPSIG(x: u32) u32 { return x >> 8; } -pub fn WIFEXITED(x: i32) bool { +pub fn WIFEXITED(x: u32) bool { return wstatus(x) == 0; } -pub fn WIFSTOPPED(x: i32) bool { +pub fn WIFSTOPPED(x: u32) bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; } -pub fn WIFSIGNALED(x: i32) bool { +pub fn WIFSIGNALED(x: u32) bool { return wstatus(x) != wstopped and wstatus(x) != 0; } diff --git a/std/os/bits/freebsd.zig b/std/os/bits/freebsd.zig index 941181f221..d4f49033e1 100644 --- a/std/os/bits/freebsd.zig +++ b/std/os/bits/freebsd.zig @@ -161,7 +161,7 @@ pub const CLOCK_SECOND = 13; pub const CLOCK_THREAD_CPUTIME_ID = 14; pub const CLOCK_PROCESS_CPUTIME_ID = 15; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_FIXED = 0x0010; @@ -644,29 +644,23 @@ pub const TIOCGPKT = 0x80045438; pub const TIOCGPTLCK = 0x80045439; pub const TIOCGEXCL = 0x80045440; -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); +pub fn WEXITSTATUS(s: u32) u32 { + return (s & 0xff00) >> 8; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -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 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, (((unsigned(s) & 0xffff) *% 0x10001) >> 8)) > 0x7f00; +pub fn WIFSTOPPED(s: u32) bool { + return @intCast(u16, (((s & 0xffff) *% 0x10001) >> 8)) > 0x7f00; } -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; +pub fn WIFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; } pub const winsize = extern struct { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 5473299488..6532e72362 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,5 +1,7 @@ +const builtin = @import("builtin"); const std = @import("../../std.zig"); const maxInt = std.math.maxInt; +use @import("../bits.zig"); pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { @@ -661,29 +663,23 @@ 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); +pub fn WEXITSTATUS(s: u32) u32 { + return (s & 0xff00) >> 8; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -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 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; +pub fn WIFSTOPPED(s: u32) bool { + return @intCast(u16, ((s & 0xffff) *% 0x10001) >> 8) > 0x7f00; } -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; +pub fn WIFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; } pub const winsize = extern struct { @@ -902,7 +898,7 @@ pub const dirent64 = extern struct { pub const dl_phdr_info = extern struct { dlpi_addr: usize, dlpi_name: ?[*]const u8, - dlpi_phdr: [*]elf.Phdr, + dlpi_phdr: [*]std.elf.Phdr, dlpi_phnum: u16, }; diff --git a/std/os/bits/netbsd.zig b/std/os/bits/netbsd.zig index 4a9cf0391c..fc4c2904e0 100644 --- a/std/os/bits/netbsd.zig +++ b/std/os/bits/netbsd.zig @@ -152,7 +152,7 @@ pub const CLOCK_MONOTONIC = 3; pub const CLOCK_THREAD_CPUTIME_ID = 0x20000000; pub const CLOCK_PROCESS_CPUTIME_ID = 0x40000000; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_REMAPDUP = 0x0004; @@ -516,34 +516,28 @@ pub const TIOCSWINSZ = 0x80087467; pub const TIOCUCNTL = 0x80047466; pub const TIOCXMTFRAME = 0x80087444; -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); +pub fn WEXITSTATUS(s: u32) u32 { + return (s >> 8) & 0xff; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) >> 8) & 0xff); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFCONTINUED(s: i32) bool { +pub fn WIFCONTINUED(s: u32) bool { return ((s & 0x7f) == 0xffff); } -pub fn WIFSTOPPED(s: i32) bool { +pub fn WIFSTOPPED(s: u32) bool { return ((s & 0x7f != 0x7f) and !WIFCONTINUED(s)); } -pub fn WIFSIGNALED(s: i32) bool { +pub fn WIFSIGNALED(s: u32) bool { return !WIFSTOPPED(s) and !WIFCONTINUED(s) and !WIFEXITED(s); } diff --git a/std/os/linux.zig b/std/os/linux.zig index 4b37399079..282aa19bf1 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -382,7 +382,7 @@ pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); } -pub fn waitpid(pid: i32, status: *i32, flags: u32) usize { +pub fn waitpid(pid: i32, status: *u32, flags: u32) usize { return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), flags, 0); } diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig index 1949d00298..c78b071c74 100644 --- a/std/os/linux/test.zig +++ b/std/os/linux/test.zig @@ -11,7 +11,7 @@ test "getpid" { test "timer" { const epoll_fd = linux.epoll_create(); - var err = linux.getErrno(epoll_fd); + var err: usize = linux.getErrno(epoll_fd); expect(err == 0); const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig index 44cd0271cd..86d54bfbf8 100644 --- a/std/os/linux/vdso.zig +++ b/std/os/linux/vdso.zig @@ -5,7 +5,7 @@ const mem = std.mem; const maxInt = std.math.maxInt; pub fn lookup(vername: []const u8, name: []const u8) usize { - const vdso_addr = std.os.linuxGetAuxVal(std.elf.AT_SYSINFO_EHDR); + const vdso_addr = std.os.system.getauxval(std.elf.AT_SYSINFO_EHDR); if (vdso_addr == 0) return 0; const eh = @intToPtr(*elf.Ehdr, vdso_addr); diff --git a/std/os/test.zig b/std/os/test.zig index f7d40e1f03..d4d662e97f 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -15,11 +15,11 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; test "makePath, put some files in it, deleteTree" { - try os.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); + try fs.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); - try os.deleteTree(a, "os_test_tmp"); - if (os.Dir.open(a, "os_test_tmp")) |dir| { + try fs.deleteTree(a, "os_test_tmp"); + if (fs.Dir.open(a, "os_test_tmp")) |dir| { @panic("expected error"); } else |err| { expect(err == error.FileNotFound); @@ -27,7 +27,7 @@ test "makePath, put some files in it, deleteTree" { } test "access file" { - try os.makePath(a, "os_test_tmp"); + try fs.makePath(a, "os_test_tmp"); if (File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { @@ -35,8 +35,8 @@ test "access file" { } try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); - try File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt"); - try os.deleteTree(a, "os_test_tmp"); + try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK); + try fs.deleteTree(a, "os_test_tmp"); } fn testThreadIdFn(thread_id: *Thread.Id) void { @@ -52,15 +52,12 @@ test "std.Thread.getCurrentId" { thread.wait(); if (Thread.use_pthreads) { expect(thread_current_id == thread_id); + } else if (os.windows.is_the_target) { + expect(Thread.getCurrentId() != thread_current_id); } else { - switch (builtin.os) { - builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id), - else => { - // If the thread completes very quickly, then thread_id can be 0. See the - // documentation comments for `std.Thread.handle`. - expect(thread_id == 0 or thread_current_id == thread_id); - }, - } + // If the thread completes very quickly, then thread_id can be 0. See the + // documentation comments for `std.Thread.handle`. + expect(thread_id == 0 or thread_current_id == thread_id); } } @@ -92,7 +89,7 @@ fn start2(ctx: *i32) u8 { } test "cpu count" { - const cpu_count = try std.os.cpuCount(a); + const cpu_count = try Thread.cpuCount(); expect(cpu_count >= 1); } @@ -105,7 +102,7 @@ test "AtomicFile" { \\ this is a test file ; { - var af = try os.AtomicFile.init(test_out_file, File.default_mode); + var af = try fs.AtomicFile.init(test_out_file, File.default_mode); defer af.deinit(); try af.file.write(test_content); try af.finish(); @@ -113,7 +110,7 @@ test "AtomicFile" { const content = try io.readFileAlloc(allocator, test_out_file); expect(mem.eql(u8, content, test_content)); - try os.deleteFile(test_out_file); + try fs.deleteFile(test_out_file); } test "thread local storage" { @@ -145,10 +142,10 @@ test "getrandom" { test "getcwd" { // at least call it so it gets compiled var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - _ = os.getcwd(&buf) catch {}; + _ = os.getcwd(&buf) catch undefined; } test "realpath" { var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - testing.expectError(error.FileNotFound, os.realpath("definitely_bogus_does_not_exist1234", &buf)); + testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf)); } diff --git a/std/os/windows.zig b/std/os/windows.zig index 9bfeee9fe1..acad139955 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -1180,6 +1180,44 @@ pub fn CreateProcessW( } } +pub const LoadLibraryError = error{ + FileNotFound, + Unexpected, +}; + +pub fn LoadLibraryW(lpLibFileName: [*]const u16) LoadLibraryError!HMODULE { + return kernel32.LoadLibraryW(lpLibFileName) orelse { + switch (kernel32.GetLastError()) { + ERROR.FILE_NOT_FOUND => return error.FileNotFound, + ERROR.PATH_NOT_FOUND => return error.FileNotFound, + ERROR.MOD_NOT_FOUND => return error.FileNotFound, + else => |err| return unexpectedError(err), + } + }; +} + +pub fn FreeLibrary(hModule: HMODULE) void { + assert(kernel32.FreeLibrary(hModule) != 0); +} + +pub fn QueryPerformanceFrequency() u64 { + // "On systems that run Windows XP or later, the function will always succeed" + // https://docs.microsoft.com/en-us/windows/desktop/api/profileapi/nf-profileapi-queryperformancefrequency + var result: LARGE_INTEGER = undefined; + assert(kernel32.QueryPerformanceFrequency(&result) != 0); + // The kernel treats this integer as unsigned. + return @bitCast(u64, result); +} + +pub fn QueryPerformanceCounter() u64 { + // "On systems that run Windows XP or later, the function will always succeed" + // https://docs.microsoft.com/en-us/windows/desktop/api/profileapi/nf-profileapi-queryperformancecounter + var result: LARGE_INTEGER = undefined; + assert(kernel32.QueryPerformanceCounter(&result) != 0); + // The kernel treats this integer as unsigned. + return @bitCast(u64, result); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/os/zen.zig b/std/os/zen.zig index 6931b07f78..8d2f963486 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -80,7 +80,7 @@ pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; // FIXME: let's borrow Linux's error numbers for now. -use @import("../bits/linux/errno.zig"); +use @import("bits/linux/errno.zig"); // 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); diff --git a/std/process.zig b/std/process.zig index baccbccb10..b45074d67c 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,7 +1,9 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; +const fs = std.fs; const BufMap = std.BufMap; +const Buffer = std.Buffer; const mem = std.mem; const math = std.math; const Allocator = mem.Allocator; @@ -13,6 +15,24 @@ pub const exit = os.exit; pub const changeCurDir = os.chdir; pub const changeCurDirC = os.chdirC; +/// The result is a slice of `out_buffer`, from index `0`. +pub fn getCwd(out_buffer: *[fs.MAX_PATH_BYTES]u8) ![]u8 { + return os.getcwd(out_buffer); +} + +/// Caller must free the returned memory. +pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; + return mem.dupe(allocator, u8, try os.getcwd(&buf)); +} + +test "getCwdAlloc" { + // at least call it so it gets compiled + var buf: [1000]u8 = undefined; + const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; + _ = getCwdAlloc(allocator) catch undefined; +} + /// Caller must free result when done. /// TODO make this go through libc when we have it pub fn getEnvMap(allocator: *Allocator) !BufMap { @@ -402,7 +422,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { var contents = try Buffer.initSize(allocator, 0); defer contents.deinit(); - var slice_list = ArrayList(usize).init(allocator); + var slice_list = std.ArrayList(usize).init(allocator); defer slice_list.deinit(); while (it.next(allocator)) |arg_or_err| { diff --git a/std/thread.zig b/std/thread.zig index 53f42e555f..53be5a8c2d 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -1,8 +1,10 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; +const mem = std.mem; const windows = std.os.windows; const c = std.c; +const assert = std.debug.assert; pub const Thread = struct { data: Data, @@ -31,14 +33,12 @@ pub const Thread = struct { pub const Data = if (use_pthreads) struct { handle: Thread.Handle, - mmap_addr: usize, - mmap_len: usize, + memory: []align(mem.page_size) u8, } else switch (builtin.os) { .linux => struct { handle: Thread.Handle, - mmap_addr: usize, - mmap_len: usize, + memory: []align(mem.page_size) u8, }, .windows => struct { handle: Thread.Handle, @@ -56,7 +56,7 @@ pub const Thread = struct { return c.pthread_self(); } else return switch (builtin.os) { - .linux => linux.gettid(), + .linux => os.linux.gettid(), .windows => windows.GetCurrentThreadId(), else => @compileError("Unsupported OS"), }; @@ -82,21 +82,21 @@ pub const Thread = struct { os.EDEADLK => unreachable, else => unreachable, } - os.munmap(self.data.mmap_addr, self.data.mmap_len); + os.munmap(self.data.memory); } else switch (builtin.os) { .linux => { while (true) { const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; - const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); - switch (linux.getErrno(rc)) { + const rc = os.linux.futex_wait(&self.data.handle, os.linux.FUTEX_WAIT, pid_value, null); + switch (os.linux.getErrno(rc)) { 0 => continue, os.EINTR => continue, os.EAGAIN => continue, else => unreachable, } } - os.munmap(self.data.mmap_addr, self.data.mmap_len); + os.munmap(self.data.memory); }, .windows => { assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0); @@ -130,6 +130,10 @@ pub const Thread = struct { /// Not enough userland memory to spawn the thread. OutOfMemory, + /// `mlockall` is enabled, and the memory needed to spawn the thread + /// would exceed the limit. + LockedMemoryLimitExceeded, + Unexpected, }; @@ -219,7 +223,7 @@ pub const Thread = struct { } }; - const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0; + const MAP_GROWSDOWN = if (os.linux.is_the_target) os.linux.MAP_GROWSDOWN else 0; var stack_end_offset: usize = undefined; var thread_start_offset: usize = undefined; @@ -241,7 +245,7 @@ pub const Thread = struct { } // Finally, the Thread Local Storage, if any. if (!Thread.use_pthreads) { - if (linux.tls.tls_image) |tls_img| { + if (os.linux.tls.tls_image) |tls_img| { l = mem.alignForward(l, @alignOf(usize)); tls_start_offset = l; l += tls_img.alloc_size; @@ -249,12 +253,24 @@ pub const Thread = struct { } break :blk l; }; - const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); - errdefer os.munmap(mmap_addr, mmap_len); + const mmap_slice = os.mmap( + null, + mem.alignForward(mmap_len, mem.page_size), + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, + -1, + 0, + ) catch |err| switch (err) { + error.MemoryMappingNotSupported => unreachable, // no file descriptor + error.AccessDenied => unreachable, // no file descriptor + error.PermissionDenied => unreachable, // no file descriptor + else => |e| return e, + }; + errdefer os.munmap(mmap_slice); + const mmap_addr = @ptrToInt(mmap_slice.ptr); const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset)); - thread_ptr.data.mmap_addr = mmap_addr; - thread_ptr.data.mmap_len = mmap_len; + thread_ptr.data.memory = mmap_slice; var arg: usize = undefined; if (@sizeOf(Context) != 0) { @@ -269,7 +285,7 @@ pub const Thread = struct { if (c.pthread_attr_init(&attr) != 0) return error.SystemResources; defer assert(c.pthread_attr_destroy(&attr) == 0); - assert(c.pthread_attr_setstack(&attr, @intToPtr(*c_void, mmap_addr), stack_end_offset) == 0); + assert(c.pthread_attr_setstack(&attr, mmap_slice.ptr, stack_end_offset) == 0); const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg)); switch (err) { @@ -279,13 +295,13 @@ pub const Thread = struct { os.EINVAL => unreachable, else => return os.unexpectedErrno(@intCast(usize, err)), } - } else if (builtin.os == .linux) { + } else if (os.linux.is_the_target) { var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND | os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID | os.CLONE_DETACHED; var newtls: usize = undefined; - if (linux.tls.tls_image) |tls_img| { - newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset); + if (os.linux.tls.tls_image) |tls_img| { + newtls = os.linux.tls.copyTLS(mmap_addr + tls_start_offset); flags |= os.CLONE_SETTLS; } const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); @@ -313,7 +329,7 @@ pub const Thread = struct { pub fn cpuCount() CpuCountError!usize { if (os.linux.is_the_target) { const cpu_set = try os.sched_getaffinity(0); - return os.CPU_COUNT(cpu_set); + return usize(os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast } if (os.windows.is_the_target) { var system_info: windows.SYSTEM_INFO = undefined; diff --git a/std/time.zig b/std/time.zig index 1483a0a132..d4a349880e 100644 --- a/std/time.zig +++ b/std/time.zig @@ -95,7 +95,7 @@ pub const Timer = struct { /// be less precise frequency: switch (builtin.os) { .windows => u64, - .macosx, .ios, .tvos, .watchos => darwin.mach_timebase_info_data, + .macosx, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data, else => void, }, resolution: u64, @@ -119,20 +119,13 @@ pub const Timer = struct { var self: Timer = undefined; if (os.windows.is_the_target) { - var freq: i64 = undefined; - var err = windows.QueryPerformanceFrequency(&freq); - if (err == windows.FALSE) return error.TimerUnsupported; - self.frequency = @intCast(u64, freq); + self.frequency = os.windows.QueryPerformanceFrequency(); self.resolution = @divFloor(ns_per_s, self.frequency); - - var start_time: i64 = undefined; - err = windows.QueryPerformanceCounter(&start_time); - assert(err != windows.FALSE); - self.start_time = @intCast(u64, start_time); + self.start_time = os.windows.QueryPerformanceCounter(); } else if (os.darwin.is_the_target) { - darwin.mach_timebase_info(&self.frequency); + os.darwin.mach_timebase_info(&self.frequency); self.resolution = @divFloor(self.frequency.numer, self.frequency.denom); - self.start_time = darwin.mach_absolute_time(); + self.start_time = os.darwin.mach_absolute_time(); } else { //On Linux, seccomp can do arbitrary things to our ability to call // syscalls, including return any errno value it wants and @@ -177,13 +170,10 @@ pub const Timer = struct { fn clockNative() u64 { if (os.windows.is_the_target) { - var result: i64 = undefined; - var err = windows.QueryPerformanceCounter(&result); - assert(err != windows.FALSE); - return @intCast(u64, result); + return os.windows.QueryPerformanceCounter(); } if (os.darwin.is_the_target) { - return darwin.mach_absolute_time(); + return os.darwin.mach_absolute_time(); } var ts: os.timespec = undefined; os.clock_gettime(monotonic_clock_id, &ts) catch unreachable; diff --git a/test/compare_output.zig b/test/compare_output.zig index 72f4e223aa..ee84e7caa7 100644 --- a/test/compare_output.zig +++ b/test/compare_output.zig @@ -377,7 +377,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\ stdout.print("before\n") catch unreachable; \\ defer stdout.print("defer1\n") catch unreachable; \\ defer stdout.print("defer2\n") catch unreachable; - \\ var args_it = @import("std").os.args(); + \\ var args_it = @import("std").process.args(); \\ if (args_it.skip() and !args_it.skip()) return; \\ defer stdout.print("defer3\n") catch unreachable; \\ stdout.print("after\n") catch unreachable; @@ -444,7 +444,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\const allocator = std.debug.global_allocator; \\ \\pub fn main() !void { - \\ var args_it = os.args(); + \\ var args_it = std.process.args(); \\ var stdout_file = try io.getStdOut(); \\ var stdout_adapter = stdout_file.outStream(); \\ const stdout = &stdout_adapter.stream; @@ -485,7 +485,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\const allocator = std.debug.global_allocator; \\ \\pub fn main() !void { - \\ var args_it = os.args(); + \\ var args_it = std.process.args(); \\ var stdout_file = try io.getStdOut(); \\ var stdout_adapter = stdout_file.outStream(); \\ const stdout = &stdout_adapter.stream; diff --git a/test/standalone/empty_env/main.zig b/test/standalone/empty_env/main.zig index 20b45c3137..d6d5ecb5af 100644 --- a/test/standalone/empty_env/main.zig +++ b/test/standalone/empty_env/main.zig @@ -1,6 +1,6 @@ const std = @import("std"); pub fn main() void { - const env_map = std.os.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map"); + const env_map = std.process.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map"); std.testing.expect(env_map.count() == 0); } diff --git a/test/tests.zig b/test/tests.zig index 9bd9292b84..030864aee0 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -393,7 +393,7 @@ pub const CompareOutputContext = struct { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; - const expected_exit_code: i32 = 126; + const expected_exit_code: u32 = 126; switch (term) { .Exited => |code| { if (code != expected_exit_code) { -- cgit v1.2.3 From 6be79d79aab204718116428c9b23be7de54e14da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 27 May 2019 00:48:56 -0400 Subject: fixes for Windows and WASI --- CMakeLists.txt | 1 + std/debug.zig | 18 +- std/heap.zig | 4 +- std/os.zig | 3 +- std/os/bits/wasi.zig | 3 +- std/os/bits/windows.zig | 66 ++++- std/os/windows.zig | 543 +---------------------------------- std/os/windows/advapi32.zig | 2 +- std/os/windows/bits.zig | 527 +++++++++++++++++++++++++++++++++ std/os/windows/kernel32.zig | 2 +- std/os/windows/ntdll.zig | 2 +- std/os/windows/ole32.zig | 2 +- std/os/windows/shell32.zig | 2 +- std/pdb.zig | 2 +- std/process.zig | 4 +- std/special/bootstrap.zig | 2 +- std/statically_initialized_mutex.zig | 12 +- 17 files changed, 638 insertions(+), 557 deletions(-) create mode 100644 std/os/windows/bits.zig (limited to 'std/process.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f6af9a9de..5157406896 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -628,6 +628,7 @@ set(ZIG_STD_FILES "os/wasi.zig" "os/windows.zig" "os/windows/advapi32.zig" + "os/windows/bits.zig" "os/windows/error.zig" "os/windows/kernel32.zig" "os/windows/ntdll.zig" diff --git a/std/debug.zig b/std/debug.zig index 0b5136f7cd..b62bd5da64 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -510,7 +510,7 @@ const TtyColor = enum { /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt fn setTtyColor(tty_color: TtyColor) void { - if (os.supportsAnsiEscapeCodes(stderr_file.handle)) { + if (stderr_file.supportsAnsiEscapeCodes()) { switch (tty_color) { TtyColor.Red => { stderr_file.write(RED) catch return; @@ -540,29 +540,29 @@ fn setTtyColor(tty_color: TtyColor) void { S.init_attrs = true; var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; // TODO handle error - _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); + _ = windows.kernel32.GetConsoleScreenBufferInfo(stderr_file.handle, &info); S.attrs = info.wAttributes; } // TODO handle errors switch (tty_color) { TtyColor.Red => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Green => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Cyan => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.White, TtyColor.Bold => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Dim => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Reset => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs) catch {}; }, } } @@ -894,7 +894,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { if (this_record_len % 4 != 0) { const round_to_next_4 = (this_record_len | 0x3) + 1; const march_forward_bytes = round_to_next_4 - this_record_len; - try dbi.seekBy(march_forward_bytes); + try dbi.seekBy(@intCast(isize, march_forward_bytes)); this_record_len += march_forward_bytes; } diff --git a/std/heap.zig b/std/heap.zig index 6c6a97d037..3bae7f3c46 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -92,12 +92,14 @@ pub const DirectAllocator = struct { // obtained some memory space that will cause the next // VirtualAlloc call to fail. To handle this, we will retry // until it succeeds. - return w.VirtualAlloc( + const ptr = w.VirtualAlloc( @intToPtr(*c_void, aligned_addr), n, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, ) catch continue; + + return @ptrCast([*]u8, ptr)[0..n]; }; return @ptrCast([*]u8, final_addr)[0..n]; diff --git a/std/os.zig b/std/os.zig index 087d5e12a5..84e92a15d9 100644 --- a/std/os.zig +++ b/std/os.zig @@ -99,7 +99,8 @@ pub fn getrandom(buf: []u8) GetRandomError!void { } if (linux.is_the_target) { while (true) { - switch (errno(system.getrandom(buf.ptr, buf.len, 0))) { + // Bypass libc because it's missing on even relatively new versions. + switch (linux.getErrno(linux.getrandom(buf.ptr, buf.len, 0))) { 0 => return, EINVAL => unreachable, EFAULT => unreachable, diff --git a/std/os/bits/wasi.zig b/std/os/bits/wasi.zig index cf6542a159..a6f96de31a 100644 --- a/std/os/bits/wasi.zig +++ b/std/os/bits/wasi.zig @@ -1,5 +1,4 @@ -// Declarations that are intended to be imported into the POSIX namespace. -// This includes WASI-only APIs. +use @import("../bits.zig"); pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; diff --git a/std/os/bits/windows.zig b/std/os/bits/windows.zig index d3558958ff..4c94853e2a 100644 --- a/std/os/bits/windows.zig +++ b/std/os/bits/windows.zig @@ -1,8 +1,65 @@ -use @import("../windows.zig"); +// The reference for these types and values is Microsoft Windows's ucrt (Universal C RunTime). + +use @import("../windows/bits.zig"); pub const fd_t = HANDLE; pub const pid_t = HANDLE; +pub const sig_atomic_t = c_int; + +/// maximum signal number + 1 +pub const NSIG = 23; + +// Signal types + +/// interrupt +pub const SIGINT = 2; + +/// illegal instruction - invalid function image +pub const SIGILL = 4; + +/// floating point exception +pub const SIGFPE = 8; + +/// segment violation +pub const SIGSEGV = 11; + +/// Software termination signal from kill +pub const SIGTERM = 15; + +/// Ctrl-Break sequence +pub const SIGBREAK = 21; + +/// abnormal termination triggered by abort call +pub const SIGABRT = 22; + +/// SIGABRT compatible with other platforms, same as SIGABRT +pub const SIGABRT_COMPAT = 6; + +// Signal action codes + +/// default signal action +pub const SIG_DFL = 0; + +/// ignore signal +pub const SIG_IGN = 1; + +/// return current value +pub const SIG_GET = 2; + +/// signal gets error +pub const SIG_SGE = 3; + +/// acknowledge +pub const SIG_ACK = 4; + +/// Signal error value (returned by signal call on error) +pub const SIG_ERR = -1; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + pub const EPERM = 1; pub const ENOENT = 2; pub const ESRCH = 3; @@ -89,3 +146,10 @@ pub const ETIME = 137; pub const ETIMEDOUT = 138; pub const ETXTBSY = 139; pub const EWOULDBLOCK = 140; + +// These are workarounds for "use of undeclared identifier" compile errors +// TODO make the compiler even more lazy. don't emit "use of undeclared identifier" errors +// for if branches that aren't taken. +pub const SIGKILL = @compileError("Windows libc does not have this"); +pub const EDQUOT = @compileError("Windows libc does not have this"); +pub const TIOCGWINSZ = @compileError("Windows libc does not have this"); diff --git a/std/os/windows.zig b/std/os/windows.zig index acad139955..ffa647692e 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -6,537 +6,19 @@ const builtin = @import("builtin"); const std = @import("../std.zig"); +const mem = std.mem; const assert = std.debug.assert; +const math = std.math; const maxInt = std.math.maxInt; pub const is_the_target = builtin.os == .windows; - pub const advapi32 = @import("windows/advapi32.zig"); pub const kernel32 = @import("windows/kernel32.zig"); pub const ntdll = @import("windows/ntdll.zig"); pub const ole32 = @import("windows/ole32.zig"); pub const shell32 = @import("windows/shell32.zig"); -pub const ERROR = @import("windows/error.zig"); - -/// The standard input device. Initially, this is the console input buffer, CONIN$. -pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; - -/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; - -/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; - -pub const SHORT = c_short; -pub const BOOL = c_int; -pub const BOOLEAN = BYTE; -pub const BYTE = u8; -pub const CHAR = u8; -pub const DWORD = u32; -pub const FLOAT = f32; -pub const HANDLE = *c_void; -pub const HCRYPTPROV = ULONG_PTR; -pub const HINSTANCE = *@OpaqueType(); -pub const HMODULE = *@OpaqueType(); -pub const FARPROC = *@OpaqueType(); -pub const INT = c_int; -pub const LPBYTE = *BYTE; -pub const LPCH = *CHAR; -pub const LPCSTR = [*]const CHAR; -pub const LPCTSTR = [*]const TCHAR; -pub const LPCVOID = *const c_void; -pub const LPDWORD = *DWORD; -pub const LPSTR = [*]CHAR; -pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR; -pub const LPVOID = *c_void; -pub const LPWSTR = [*]WCHAR; -pub const LPCWSTR = [*]const WCHAR; -pub const PVOID = *c_void; -pub const PWSTR = [*]WCHAR; -pub const SIZE_T = usize; -pub const TCHAR = if (UNICODE) WCHAR else u8; -pub const UINT = c_uint; -pub const ULONG_PTR = usize; -pub const DWORD_PTR = ULONG_PTR; -pub const UNICODE = false; -pub const WCHAR = u16; -pub const WORD = u16; -pub const LARGE_INTEGER = i64; -pub const ULONG = u32; -pub const LONG = i32; -pub const ULONGLONG = u64; -pub const LONGLONG = i64; - -pub const TRUE = 1; -pub const FALSE = 0; - -pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize)); - -pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD)); - -pub const OVERLAPPED = extern struct { - Internal: ULONG_PTR, - InternalHigh: ULONG_PTR, - Offset: DWORD, - OffsetHigh: DWORD, - hEvent: ?HANDLE, -}; -pub const LPOVERLAPPED = *OVERLAPPED; - -pub const MAX_PATH = 260; - -// TODO issue #305 -pub const FILE_INFO_BY_HANDLE_CLASS = u32; -pub const FileBasicInfo = 0; -pub const FileStandardInfo = 1; -pub const FileNameInfo = 2; -pub const FileRenameInfo = 3; -pub const FileDispositionInfo = 4; -pub const FileAllocationInfo = 5; -pub const FileEndOfFileInfo = 6; -pub const FileStreamInfo = 7; -pub const FileCompressionInfo = 8; -pub const FileAttributeTagInfo = 9; -pub const FileIdBothDirectoryInfo = 10; -pub const FileIdBothDirectoryRestartInfo = 11; -pub const FileIoPriorityHintInfo = 12; -pub const FileRemoteProtocolInfo = 13; -pub const FileFullDirectoryInfo = 14; -pub const FileFullDirectoryRestartInfo = 15; -pub const FileStorageInfo = 16; -pub const FileAlignmentInfo = 17; -pub const FileIdInfo = 18; -pub const FileIdExtdDirectoryInfo = 19; -pub const FileIdExtdDirectoryRestartInfo = 20; - -pub const FILE_NAME_INFO = extern struct { - FileNameLength: DWORD, - FileName: [1]WCHAR, -}; - -/// Return the normalized drive name. This is the default. -pub const FILE_NAME_NORMALIZED = 0x0; - -/// Return the opened file name (not normalized). -pub const FILE_NAME_OPENED = 0x8; - -/// Return the path with the drive letter. This is the default. -pub const VOLUME_NAME_DOS = 0x0; - -/// Return the path with a volume GUID path instead of the drive name. -pub const VOLUME_NAME_GUID = 0x1; - -/// Return the path with no drive information. -pub const VOLUME_NAME_NONE = 0x4; - -/// Return the path with the volume device path. -pub const VOLUME_NAME_NT = 0x2; - -pub const SECURITY_ATTRIBUTES = extern struct { - nLength: DWORD, - lpSecurityDescriptor: ?*c_void, - bInheritHandle: BOOL, -}; -pub const PSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; -pub const LPSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; - -pub const GENERIC_READ = 0x80000000; -pub const GENERIC_WRITE = 0x40000000; -pub const GENERIC_EXECUTE = 0x20000000; -pub const GENERIC_ALL = 0x10000000; - -pub const FILE_SHARE_DELETE = 0x00000004; -pub const FILE_SHARE_READ = 0x00000001; -pub const FILE_SHARE_WRITE = 0x00000002; - -pub const CREATE_ALWAYS = 2; -pub const CREATE_NEW = 1; -pub const OPEN_ALWAYS = 4; -pub const OPEN_EXISTING = 3; -pub const TRUNCATE_EXISTING = 5; - -pub const FILE_ATTRIBUTE_ARCHIVE = 0x20; -pub const FILE_ATTRIBUTE_COMPRESSED = 0x800; -pub const FILE_ATTRIBUTE_DEVICE = 0x40; -pub const FILE_ATTRIBUTE_DIRECTORY = 0x10; -pub const FILE_ATTRIBUTE_ENCRYPTED = 0x4000; -pub const FILE_ATTRIBUTE_HIDDEN = 0x2; -pub const FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x8000; -pub const FILE_ATTRIBUTE_NORMAL = 0x80; -pub const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000; -pub const FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x20000; -pub const FILE_ATTRIBUTE_OFFLINE = 0x1000; -pub const FILE_ATTRIBUTE_READONLY = 0x1; -pub const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 0x400000; -pub const FILE_ATTRIBUTE_RECALL_ON_OPEN = 0x40000; -pub const FILE_ATTRIBUTE_REPARSE_POINT = 0x400; -pub const FILE_ATTRIBUTE_SPARSE_FILE = 0x200; -pub const FILE_ATTRIBUTE_SYSTEM = 0x4; -pub const FILE_ATTRIBUTE_TEMPORARY = 0x100; -pub const FILE_ATTRIBUTE_VIRTUAL = 0x10000; - -pub const PROCESS_INFORMATION = extern struct { - hProcess: HANDLE, - hThread: HANDLE, - dwProcessId: DWORD, - dwThreadId: DWORD, -}; - -pub const STARTUPINFOW = extern struct { - cb: DWORD, - lpReserved: ?LPWSTR, - lpDesktop: ?LPWSTR, - lpTitle: ?LPWSTR, - dwX: DWORD, - dwY: DWORD, - dwXSize: DWORD, - dwYSize: DWORD, - dwXCountChars: DWORD, - dwYCountChars: DWORD, - dwFillAttribute: DWORD, - dwFlags: DWORD, - wShowWindow: WORD, - cbReserved2: WORD, - lpReserved2: ?LPBYTE, - hStdInput: ?HANDLE, - hStdOutput: ?HANDLE, - hStdError: ?HANDLE, -}; - -pub const STARTF_FORCEONFEEDBACK = 0x00000040; -pub const STARTF_FORCEOFFFEEDBACK = 0x00000080; -pub const STARTF_PREVENTPINNING = 0x00002000; -pub const STARTF_RUNFULLSCREEN = 0x00000020; -pub const STARTF_TITLEISAPPID = 0x00001000; -pub const STARTF_TITLEISLINKNAME = 0x00000800; -pub const STARTF_UNTRUSTEDSOURCE = 0x00008000; -pub const STARTF_USECOUNTCHARS = 0x00000008; -pub const STARTF_USEFILLATTRIBUTE = 0x00000010; -pub const STARTF_USEHOTKEY = 0x00000200; -pub const STARTF_USEPOSITION = 0x00000004; -pub const STARTF_USESHOWWINDOW = 0x00000001; -pub const STARTF_USESIZE = 0x00000002; -pub const STARTF_USESTDHANDLES = 0x00000100; - -pub const INFINITE = 4294967295; - -pub const WAIT_ABANDONED = 0x00000080; -pub const WAIT_OBJECT_0 = 0x00000000; -pub const WAIT_TIMEOUT = 0x00000102; -pub const WAIT_FAILED = 0xFFFFFFFF; - -pub const HANDLE_FLAG_INHERIT = 0x00000001; -pub const HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002; - -pub const MOVEFILE_COPY_ALLOWED = 2; -pub const MOVEFILE_CREATE_HARDLINK = 16; -pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4; -pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32; -pub const MOVEFILE_REPLACE_EXISTING = 1; -pub const MOVEFILE_WRITE_THROUGH = 8; - -pub const FILE_BEGIN = 0; -pub const FILE_CURRENT = 1; -pub const FILE_END = 2; - -pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000; -pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004; -pub const HEAP_NO_SERIALIZE = 0x00000001; - -// AllocationType values -pub const MEM_COMMIT = 0x1000; -pub const MEM_RESERVE = 0x2000; -pub const MEM_RESET = 0x80000; -pub const MEM_RESET_UNDO = 0x1000000; -pub const MEM_LARGE_PAGES = 0x20000000; -pub const MEM_PHYSICAL = 0x400000; -pub const MEM_TOP_DOWN = 0x100000; -pub const MEM_WRITE_WATCH = 0x200000; - -// Protect values -pub const PAGE_EXECUTE = 0x10; -pub const PAGE_EXECUTE_READ = 0x20; -pub const PAGE_EXECUTE_READWRITE = 0x40; -pub const PAGE_EXECUTE_WRITECOPY = 0x80; -pub const PAGE_NOACCESS = 0x01; -pub const PAGE_READONLY = 0x02; -pub const PAGE_READWRITE = 0x04; -pub const PAGE_WRITECOPY = 0x08; -pub const PAGE_TARGETS_INVALID = 0x40000000; -pub const PAGE_TARGETS_NO_UPDATE = 0x40000000; // Same as PAGE_TARGETS_INVALID -pub const PAGE_GUARD = 0x100; -pub const PAGE_NOCACHE = 0x200; -pub const PAGE_WRITECOMBINE = 0x400; - -// FreeType values -pub const MEM_COALESCE_PLACEHOLDERS = 0x1; -pub const MEM_RESERVE_PLACEHOLDERS = 0x2; -pub const MEM_DECOMMIT = 0x4000; -pub const MEM_RELEASE = 0x8000; - -pub const PTHREAD_START_ROUTINE = extern fn (LPVOID) DWORD; -pub const LPTHREAD_START_ROUTINE = PTHREAD_START_ROUTINE; - -pub const WIN32_FIND_DATAW = extern struct { - dwFileAttributes: DWORD, - ftCreationTime: FILETIME, - ftLastAccessTime: FILETIME, - ftLastWriteTime: FILETIME, - nFileSizeHigh: DWORD, - nFileSizeLow: DWORD, - dwReserved0: DWORD, - dwReserved1: DWORD, - cFileName: [260]u16, - cAlternateFileName: [14]u16, -}; - -pub const FILETIME = extern struct { - dwLowDateTime: DWORD, - dwHighDateTime: DWORD, -}; - -pub const SYSTEM_INFO = extern struct { - anon1: extern union { - dwOemId: DWORD, - anon2: extern struct { - wProcessorArchitecture: WORD, - wReserved: WORD, - }, - }, - dwPageSize: DWORD, - lpMinimumApplicationAddress: LPVOID, - lpMaximumApplicationAddress: LPVOID, - dwActiveProcessorMask: DWORD_PTR, - dwNumberOfProcessors: DWORD, - dwProcessorType: DWORD, - dwAllocationGranularity: DWORD, - wProcessorLevel: WORD, - wProcessorRevision: WORD, -}; - -pub const HRESULT = c_long; - -pub const KNOWNFOLDERID = GUID; -pub const GUID = extern struct { - Data1: c_ulong, - Data2: c_ushort, - Data3: c_ushort, - Data4: [8]u8, - - pub fn parse(str: []const u8) GUID { - var guid: GUID = undefined; - var index: usize = 0; - assert(str[index] == '{'); - index += 1; - - guid.Data1 = std.fmt.parseUnsigned(c_ulong, str[index .. index + 8], 16) catch unreachable; - index += 8; - - assert(str[index] == '-'); - index += 1; - - guid.Data2 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; - index += 4; - - assert(str[index] == '-'); - index += 1; - - guid.Data3 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; - index += 4; - - assert(str[index] == '-'); - index += 1; - - guid.Data4[0] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - guid.Data4[1] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - - assert(str[index] == '-'); - index += 1; - - var i: usize = 2; - while (i < guid.Data4.len) : (i += 1) { - guid.Data4[i] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - } - - assert(str[index] == '}'); - index += 1; - return guid; - } -}; - -pub const FOLDERID_LocalAppData = GUID.parse("{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"); - -pub const KF_FLAG_DEFAULT = 0; -pub const KF_FLAG_NO_APPCONTAINER_REDIRECTION = 65536; -pub const KF_FLAG_CREATE = 32768; -pub const KF_FLAG_DONT_VERIFY = 16384; -pub const KF_FLAG_DONT_UNEXPAND = 8192; -pub const KF_FLAG_NO_ALIAS = 4096; -pub const KF_FLAG_INIT = 2048; -pub const KF_FLAG_DEFAULT_PATH = 1024; -pub const KF_FLAG_NOT_PARENT_RELATIVE = 512; -pub const KF_FLAG_SIMPLE_IDLIST = 256; -pub const KF_FLAG_ALIAS_ONLY = -2147483648; - -pub const S_OK = 0; -pub const E_NOTIMPL = @bitCast(c_long, c_ulong(0x80004001)); -pub const E_NOINTERFACE = @bitCast(c_long, c_ulong(0x80004002)); -pub const E_POINTER = @bitCast(c_long, c_ulong(0x80004003)); -pub const E_ABORT = @bitCast(c_long, c_ulong(0x80004004)); -pub const E_FAIL = @bitCast(c_long, c_ulong(0x80004005)); -pub const E_UNEXPECTED = @bitCast(c_long, c_ulong(0x8000FFFF)); -pub const E_ACCESSDENIED = @bitCast(c_long, c_ulong(0x80070005)); -pub const E_HANDLE = @bitCast(c_long, c_ulong(0x80070006)); -pub const E_OUTOFMEMORY = @bitCast(c_long, c_ulong(0x8007000E)); -pub const E_INVALIDARG = @bitCast(c_long, c_ulong(0x80070057)); - -pub const FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; -pub const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000; -pub const FILE_FLAG_NO_BUFFERING = 0x20000000; -pub const FILE_FLAG_OPEN_NO_RECALL = 0x00100000; -pub const FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000; -pub const FILE_FLAG_OVERLAPPED = 0x40000000; -pub const FILE_FLAG_POSIX_SEMANTICS = 0x0100000; -pub const FILE_FLAG_RANDOM_ACCESS = 0x10000000; -pub const FILE_FLAG_SESSION_AWARE = 0x00800000; -pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; -pub const FILE_FLAG_WRITE_THROUGH = 0x80000000; - -pub const SMALL_RECT = extern struct { - Left: SHORT, - Top: SHORT, - Right: SHORT, - Bottom: SHORT, -}; - -pub const COORD = extern struct { - X: SHORT, - Y: SHORT, -}; - -pub const CREATE_UNICODE_ENVIRONMENT = 1024; - -pub const TLS_OUT_OF_INDEXES = 4294967295; -pub const IMAGE_TLS_DIRECTORY = extern struct { - StartAddressOfRawData: usize, - EndAddressOfRawData: usize, - AddressOfIndex: usize, - AddressOfCallBacks: usize, - SizeOfZeroFill: u32, - Characteristics: u32, -}; -pub const IMAGE_TLS_DIRECTORY64 = IMAGE_TLS_DIRECTORY; -pub const IMAGE_TLS_DIRECTORY32 = IMAGE_TLS_DIRECTORY; - -pub const PIMAGE_TLS_CALLBACK = ?extern fn (PVOID, DWORD, PVOID) void; - -pub const PROV_RSA_FULL = 1; - -pub const REGSAM = ACCESS_MASK; -pub const ACCESS_MASK = DWORD; -pub const PHKEY = *HKEY; -pub const HKEY = *HKEY__; -pub const HKEY__ = extern struct { - unused: c_int, -}; -pub const LSTATUS = LONG; - -pub const FILE_NOTIFY_INFORMATION = extern struct { - NextEntryOffset: DWORD, - Action: DWORD, - FileNameLength: DWORD, - FileName: [1]WCHAR, -}; - -pub const FILE_ACTION_ADDED = 0x00000001; -pub const FILE_ACTION_REMOVED = 0x00000002; -pub const FILE_ACTION_MODIFIED = 0x00000003; -pub const FILE_ACTION_RENAMED_OLD_NAME = 0x00000004; -pub const FILE_ACTION_RENAMED_NEW_NAME = 0x00000005; - -pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn (DWORD, DWORD, *OVERLAPPED) void; - -pub const FILE_LIST_DIRECTORY = 1; - -pub const FILE_NOTIFY_CHANGE_CREATION = 64; -pub const FILE_NOTIFY_CHANGE_SIZE = 8; -pub const FILE_NOTIFY_CHANGE_SECURITY = 256; -pub const FILE_NOTIFY_CHANGE_LAST_ACCESS = 32; -pub const FILE_NOTIFY_CHANGE_LAST_WRITE = 16; -pub const FILE_NOTIFY_CHANGE_DIR_NAME = 2; -pub const FILE_NOTIFY_CHANGE_FILE_NAME = 1; -pub const FILE_NOTIFY_CHANGE_ATTRIBUTES = 4; - -pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { - dwSize: COORD, - dwCursorPosition: COORD, - wAttributes: WORD, - srWindow: SMALL_RECT, - dwMaximumWindowSize: COORD, -}; - -pub const FOREGROUND_BLUE = 1; -pub const FOREGROUND_GREEN = 2; -pub const FOREGROUND_RED = 4; -pub const FOREGROUND_INTENSITY = 8; - -pub const LIST_ENTRY = extern struct { - Flink: *LIST_ENTRY, - Blink: *LIST_ENTRY, -}; - -pub const RTL_CRITICAL_SECTION_DEBUG = extern struct { - Type: WORD, - CreatorBackTraceIndex: WORD, - CriticalSection: *RTL_CRITICAL_SECTION, - ProcessLocksList: LIST_ENTRY, - EntryCount: DWORD, - ContentionCount: DWORD, - Flags: DWORD, - CreatorBackTraceIndexHigh: WORD, - SpareWORD: WORD, -}; - -pub const RTL_CRITICAL_SECTION = extern struct { - DebugInfo: *RTL_CRITICAL_SECTION_DEBUG, - LockCount: LONG, - RecursionCount: LONG, - OwningThread: HANDLE, - LockSemaphore: HANDLE, - SpinCount: ULONG_PTR, -}; - -pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; -pub const INIT_ONCE = RTL_RUN_ONCE; -pub const INIT_ONCE_STATIC_INIT = RTL_RUN_ONCE_INIT; -pub const INIT_ONCE_FN = extern fn (InitOnce: *INIT_ONCE, Parameter: ?*c_void, Context: ?*c_void) BOOL; - -pub const RTL_RUN_ONCE = extern struct { - Ptr: ?*c_void, -}; - -pub const RTL_RUN_ONCE_INIT = RTL_RUN_ONCE{ .Ptr = null }; - -pub const COINIT_APARTMENTTHREADED = COINIT.COINIT_APARTMENTTHREADED; -pub const COINIT_MULTITHREADED = COINIT.COINIT_MULTITHREADED; -pub const COINIT_DISABLE_OLE1DDE = COINIT.COINIT_DISABLE_OLE1DDE; -pub const COINIT_SPEED_OVER_MEMORY = COINIT.COINIT_SPEED_OVER_MEMORY; -pub const COINIT = extern enum { - COINIT_APARTMENTTHREADED = 2, - COINIT_MULTITHREADED = 0, - COINIT_DISABLE_OLE1DDE = 4, - COINIT_SPEED_OVER_MEMORY = 8, -}; - -/// > The maximum path of 32,767 characters is approximate, because the "\\?\" -/// > prefix may be expanded to a longer string by the system at run time, and -/// > this expansion applies to the total length. -/// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation -pub const PATH_MAX_WIDE = 32767; +pub use @import("windows/bits.zig"); pub const CreateFileError = error{ SharingViolation, @@ -765,7 +247,7 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8) ReadFileError!usize { while (index < buffer.len) { const want_read_count = @intCast(DWORD, math.min(DWORD(maxInt(DWORD)), buffer.len - index)); var amt_read: DWORD = undefined; - if (kernel32.ReadFile(fd, buffer.ptr + index, want_read_count, &amt_read, null) == 0) { + if (kernel32.ReadFile(in_hFile, buffer.ptr + index, want_read_count, &amt_read, null) == 0) { switch (kernel32.GetLastError()) { ERROR.OPERATION_ABORTED => continue, ERROR.BROKEN_PIPE => return index, @@ -787,7 +269,7 @@ pub const WriteFileError = error{ /// This function is for blocking file descriptors only. For non-blocking, see /// `WriteFileAsync`. -pub fn WriteFile(in_hFile: HANDLE, bytes: []const u8) WriteFileError!void { +pub fn WriteFile(handle: HANDLE, bytes: []const u8) WriteFileError!void { var bytes_written: DWORD = undefined; // TODO replace this @intCast with a loop that writes all the bytes if (kernel32.WriteFile(handle, bytes.ptr, @intCast(u32, bytes.len), &bytes_written, null) == 0) { @@ -808,7 +290,7 @@ pub const GetCurrentDirectoryError = error{ Unexpected, }; -/// The result is a slice of out_buffer, indexed from 0. +/// The result is a slice of `buffer`, indexed from 0. pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 { var utf16le_buf: [PATH_MAX_WIDE]u16 = undefined; const result = kernel32.GetCurrentDirectoryW(utf16le_buf.len, &utf16le_buf); @@ -821,13 +303,14 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 { const utf16le_slice = utf16le_buf[0..result]; // Trust that Windows gives us valid UTF-16LE. var end_index: usize = 0; - var it = std.unicode.Utf16LeIterator.init(utf16le); + var it = std.unicode.Utf16LeIterator.init(utf16le_slice); while (it.nextCodepoint() catch unreachable) |codepoint| { - if (end_index + std.unicode.utf8CodepointSequenceLength(codepoint) >= out_buffer.len) + const seq_len = std.unicode.utf8CodepointSequenceLength(codepoint) catch unreachable; + if (end_index + seq_len >= buffer.len) return error.NameTooLong; - end_index += utf8Encode(codepoint, out_buffer[end_index..]) catch unreachable; + end_index += std.unicode.utf8Encode(codepoint, buffer[end_index..]) catch unreachable; } - return out_buffer[0..end_index]; + return buffer[0..end_index]; } pub const CreateSymbolicLinkError = error{Unexpected}; @@ -1218,6 +701,10 @@ pub fn QueryPerformanceCounter() u64 { return @bitCast(u64, result); } +pub fn InitOnceExecuteOnce(InitOnce: *INIT_ONCE, InitFn: INIT_ONCE_FN, Parameter: ?*c_void, Context: ?*c_void) void { + assert(kernel32.InitOnceExecuteOnce(InitOnce, InitFn, Parameter, Context) != 0); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/os/windows/advapi32.zig b/std/os/windows/advapi32.zig index 6acb8a2c88..df3220e24c 100644 --- a/std/os/windows/advapi32.zig +++ b/std/os/windows/advapi32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "advapi32" stdcallcc fn RegOpenKeyExW( hKey: HKEY, diff --git a/std/os/windows/bits.zig b/std/os/windows/bits.zig new file mode 100644 index 0000000000..0bf2991903 --- /dev/null +++ b/std/os/windows/bits.zig @@ -0,0 +1,527 @@ +// Platform-dependent types and values that are used along with OS-specific APIs. + +const builtin = @import("builtin"); +const std = @import("../../std.zig"); +const assert = std.debug.assert; +const maxInt = std.math.maxInt; + +pub const ERROR = @import("error.zig"); + +/// The standard input device. Initially, this is the console input buffer, CONIN$. +pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; + +/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; + +/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; + +pub const SHORT = c_short; +pub const BOOL = c_int; +pub const BOOLEAN = BYTE; +pub const BYTE = u8; +pub const CHAR = u8; +pub const DWORD = u32; +pub const FLOAT = f32; +pub const HANDLE = *c_void; +pub const HCRYPTPROV = ULONG_PTR; +pub const HINSTANCE = *@OpaqueType(); +pub const HMODULE = *@OpaqueType(); +pub const FARPROC = *@OpaqueType(); +pub const INT = c_int; +pub const LPBYTE = *BYTE; +pub const LPCH = *CHAR; +pub const LPCSTR = [*]const CHAR; +pub const LPCTSTR = [*]const TCHAR; +pub const LPCVOID = *const c_void; +pub const LPDWORD = *DWORD; +pub const LPSTR = [*]CHAR; +pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR; +pub const LPVOID = *c_void; +pub const LPWSTR = [*]WCHAR; +pub const LPCWSTR = [*]const WCHAR; +pub const PVOID = *c_void; +pub const PWSTR = [*]WCHAR; +pub const SIZE_T = usize; +pub const TCHAR = if (UNICODE) WCHAR else u8; +pub const UINT = c_uint; +pub const ULONG_PTR = usize; +pub const DWORD_PTR = ULONG_PTR; +pub const UNICODE = false; +pub const WCHAR = u16; +pub const WORD = u16; +pub const LARGE_INTEGER = i64; +pub const ULONG = u32; +pub const LONG = i32; +pub const ULONGLONG = u64; +pub const LONGLONG = i64; + +pub const TRUE = 1; +pub const FALSE = 0; + +pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize)); + +pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD)); + +pub const OVERLAPPED = extern struct { + Internal: ULONG_PTR, + InternalHigh: ULONG_PTR, + Offset: DWORD, + OffsetHigh: DWORD, + hEvent: ?HANDLE, +}; +pub const LPOVERLAPPED = *OVERLAPPED; + +pub const MAX_PATH = 260; + +// TODO issue #305 +pub const FILE_INFO_BY_HANDLE_CLASS = u32; +pub const FileBasicInfo = 0; +pub const FileStandardInfo = 1; +pub const FileNameInfo = 2; +pub const FileRenameInfo = 3; +pub const FileDispositionInfo = 4; +pub const FileAllocationInfo = 5; +pub const FileEndOfFileInfo = 6; +pub const FileStreamInfo = 7; +pub const FileCompressionInfo = 8; +pub const FileAttributeTagInfo = 9; +pub const FileIdBothDirectoryInfo = 10; +pub const FileIdBothDirectoryRestartInfo = 11; +pub const FileIoPriorityHintInfo = 12; +pub const FileRemoteProtocolInfo = 13; +pub const FileFullDirectoryInfo = 14; +pub const FileFullDirectoryRestartInfo = 15; +pub const FileStorageInfo = 16; +pub const FileAlignmentInfo = 17; +pub const FileIdInfo = 18; +pub const FileIdExtdDirectoryInfo = 19; +pub const FileIdExtdDirectoryRestartInfo = 20; + +pub const FILE_NAME_INFO = extern struct { + FileNameLength: DWORD, + FileName: [1]WCHAR, +}; + +/// Return the normalized drive name. This is the default. +pub const FILE_NAME_NORMALIZED = 0x0; + +/// Return the opened file name (not normalized). +pub const FILE_NAME_OPENED = 0x8; + +/// Return the path with the drive letter. This is the default. +pub const VOLUME_NAME_DOS = 0x0; + +/// Return the path with a volume GUID path instead of the drive name. +pub const VOLUME_NAME_GUID = 0x1; + +/// Return the path with no drive information. +pub const VOLUME_NAME_NONE = 0x4; + +/// Return the path with the volume device path. +pub const VOLUME_NAME_NT = 0x2; + +pub const SECURITY_ATTRIBUTES = extern struct { + nLength: DWORD, + lpSecurityDescriptor: ?*c_void, + bInheritHandle: BOOL, +}; +pub const PSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; +pub const LPSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; + +pub const GENERIC_READ = 0x80000000; +pub const GENERIC_WRITE = 0x40000000; +pub const GENERIC_EXECUTE = 0x20000000; +pub const GENERIC_ALL = 0x10000000; + +pub const FILE_SHARE_DELETE = 0x00000004; +pub const FILE_SHARE_READ = 0x00000001; +pub const FILE_SHARE_WRITE = 0x00000002; + +pub const CREATE_ALWAYS = 2; +pub const CREATE_NEW = 1; +pub const OPEN_ALWAYS = 4; +pub const OPEN_EXISTING = 3; +pub const TRUNCATE_EXISTING = 5; + +pub const FILE_ATTRIBUTE_ARCHIVE = 0x20; +pub const FILE_ATTRIBUTE_COMPRESSED = 0x800; +pub const FILE_ATTRIBUTE_DEVICE = 0x40; +pub const FILE_ATTRIBUTE_DIRECTORY = 0x10; +pub const FILE_ATTRIBUTE_ENCRYPTED = 0x4000; +pub const FILE_ATTRIBUTE_HIDDEN = 0x2; +pub const FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x8000; +pub const FILE_ATTRIBUTE_NORMAL = 0x80; +pub const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000; +pub const FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x20000; +pub const FILE_ATTRIBUTE_OFFLINE = 0x1000; +pub const FILE_ATTRIBUTE_READONLY = 0x1; +pub const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 0x400000; +pub const FILE_ATTRIBUTE_RECALL_ON_OPEN = 0x40000; +pub const FILE_ATTRIBUTE_REPARSE_POINT = 0x400; +pub const FILE_ATTRIBUTE_SPARSE_FILE = 0x200; +pub const FILE_ATTRIBUTE_SYSTEM = 0x4; +pub const FILE_ATTRIBUTE_TEMPORARY = 0x100; +pub const FILE_ATTRIBUTE_VIRTUAL = 0x10000; + +pub const PROCESS_INFORMATION = extern struct { + hProcess: HANDLE, + hThread: HANDLE, + dwProcessId: DWORD, + dwThreadId: DWORD, +}; + +pub const STARTUPINFOW = extern struct { + cb: DWORD, + lpReserved: ?LPWSTR, + lpDesktop: ?LPWSTR, + lpTitle: ?LPWSTR, + dwX: DWORD, + dwY: DWORD, + dwXSize: DWORD, + dwYSize: DWORD, + dwXCountChars: DWORD, + dwYCountChars: DWORD, + dwFillAttribute: DWORD, + dwFlags: DWORD, + wShowWindow: WORD, + cbReserved2: WORD, + lpReserved2: ?LPBYTE, + hStdInput: ?HANDLE, + hStdOutput: ?HANDLE, + hStdError: ?HANDLE, +}; + +pub const STARTF_FORCEONFEEDBACK = 0x00000040; +pub const STARTF_FORCEOFFFEEDBACK = 0x00000080; +pub const STARTF_PREVENTPINNING = 0x00002000; +pub const STARTF_RUNFULLSCREEN = 0x00000020; +pub const STARTF_TITLEISAPPID = 0x00001000; +pub const STARTF_TITLEISLINKNAME = 0x00000800; +pub const STARTF_UNTRUSTEDSOURCE = 0x00008000; +pub const STARTF_USECOUNTCHARS = 0x00000008; +pub const STARTF_USEFILLATTRIBUTE = 0x00000010; +pub const STARTF_USEHOTKEY = 0x00000200; +pub const STARTF_USEPOSITION = 0x00000004; +pub const STARTF_USESHOWWINDOW = 0x00000001; +pub const STARTF_USESIZE = 0x00000002; +pub const STARTF_USESTDHANDLES = 0x00000100; + +pub const INFINITE = 4294967295; + +pub const WAIT_ABANDONED = 0x00000080; +pub const WAIT_OBJECT_0 = 0x00000000; +pub const WAIT_TIMEOUT = 0x00000102; +pub const WAIT_FAILED = 0xFFFFFFFF; + +pub const HANDLE_FLAG_INHERIT = 0x00000001; +pub const HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002; + +pub const MOVEFILE_COPY_ALLOWED = 2; +pub const MOVEFILE_CREATE_HARDLINK = 16; +pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4; +pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32; +pub const MOVEFILE_REPLACE_EXISTING = 1; +pub const MOVEFILE_WRITE_THROUGH = 8; + +pub const FILE_BEGIN = 0; +pub const FILE_CURRENT = 1; +pub const FILE_END = 2; + +pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000; +pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004; +pub const HEAP_NO_SERIALIZE = 0x00000001; + +// AllocationType values +pub const MEM_COMMIT = 0x1000; +pub const MEM_RESERVE = 0x2000; +pub const MEM_RESET = 0x80000; +pub const MEM_RESET_UNDO = 0x1000000; +pub const MEM_LARGE_PAGES = 0x20000000; +pub const MEM_PHYSICAL = 0x400000; +pub const MEM_TOP_DOWN = 0x100000; +pub const MEM_WRITE_WATCH = 0x200000; + +// Protect values +pub const PAGE_EXECUTE = 0x10; +pub const PAGE_EXECUTE_READ = 0x20; +pub const PAGE_EXECUTE_READWRITE = 0x40; +pub const PAGE_EXECUTE_WRITECOPY = 0x80; +pub const PAGE_NOACCESS = 0x01; +pub const PAGE_READONLY = 0x02; +pub const PAGE_READWRITE = 0x04; +pub const PAGE_WRITECOPY = 0x08; +pub const PAGE_TARGETS_INVALID = 0x40000000; +pub const PAGE_TARGETS_NO_UPDATE = 0x40000000; // Same as PAGE_TARGETS_INVALID +pub const PAGE_GUARD = 0x100; +pub const PAGE_NOCACHE = 0x200; +pub const PAGE_WRITECOMBINE = 0x400; + +// FreeType values +pub const MEM_COALESCE_PLACEHOLDERS = 0x1; +pub const MEM_RESERVE_PLACEHOLDERS = 0x2; +pub const MEM_DECOMMIT = 0x4000; +pub const MEM_RELEASE = 0x8000; + +pub const PTHREAD_START_ROUTINE = extern fn (LPVOID) DWORD; +pub const LPTHREAD_START_ROUTINE = PTHREAD_START_ROUTINE; + +pub const WIN32_FIND_DATAW = extern struct { + dwFileAttributes: DWORD, + ftCreationTime: FILETIME, + ftLastAccessTime: FILETIME, + ftLastWriteTime: FILETIME, + nFileSizeHigh: DWORD, + nFileSizeLow: DWORD, + dwReserved0: DWORD, + dwReserved1: DWORD, + cFileName: [260]u16, + cAlternateFileName: [14]u16, +}; + +pub const FILETIME = extern struct { + dwLowDateTime: DWORD, + dwHighDateTime: DWORD, +}; + +pub const SYSTEM_INFO = extern struct { + anon1: extern union { + dwOemId: DWORD, + anon2: extern struct { + wProcessorArchitecture: WORD, + wReserved: WORD, + }, + }, + dwPageSize: DWORD, + lpMinimumApplicationAddress: LPVOID, + lpMaximumApplicationAddress: LPVOID, + dwActiveProcessorMask: DWORD_PTR, + dwNumberOfProcessors: DWORD, + dwProcessorType: DWORD, + dwAllocationGranularity: DWORD, + wProcessorLevel: WORD, + wProcessorRevision: WORD, +}; + +pub const HRESULT = c_long; + +pub const KNOWNFOLDERID = GUID; +pub const GUID = extern struct { + Data1: c_ulong, + Data2: c_ushort, + Data3: c_ushort, + Data4: [8]u8, + + pub fn parse(str: []const u8) GUID { + var guid: GUID = undefined; + var index: usize = 0; + assert(str[index] == '{'); + index += 1; + + guid.Data1 = std.fmt.parseUnsigned(c_ulong, str[index .. index + 8], 16) catch unreachable; + index += 8; + + assert(str[index] == '-'); + index += 1; + + guid.Data2 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; + index += 4; + + assert(str[index] == '-'); + index += 1; + + guid.Data3 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; + index += 4; + + assert(str[index] == '-'); + index += 1; + + guid.Data4[0] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + guid.Data4[1] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + + assert(str[index] == '-'); + index += 1; + + var i: usize = 2; + while (i < guid.Data4.len) : (i += 1) { + guid.Data4[i] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + } + + assert(str[index] == '}'); + index += 1; + return guid; + } +}; + +pub const FOLDERID_LocalAppData = GUID.parse("{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"); + +pub const KF_FLAG_DEFAULT = 0; +pub const KF_FLAG_NO_APPCONTAINER_REDIRECTION = 65536; +pub const KF_FLAG_CREATE = 32768; +pub const KF_FLAG_DONT_VERIFY = 16384; +pub const KF_FLAG_DONT_UNEXPAND = 8192; +pub const KF_FLAG_NO_ALIAS = 4096; +pub const KF_FLAG_INIT = 2048; +pub const KF_FLAG_DEFAULT_PATH = 1024; +pub const KF_FLAG_NOT_PARENT_RELATIVE = 512; +pub const KF_FLAG_SIMPLE_IDLIST = 256; +pub const KF_FLAG_ALIAS_ONLY = -2147483648; + +pub const S_OK = 0; +pub const E_NOTIMPL = @bitCast(c_long, c_ulong(0x80004001)); +pub const E_NOINTERFACE = @bitCast(c_long, c_ulong(0x80004002)); +pub const E_POINTER = @bitCast(c_long, c_ulong(0x80004003)); +pub const E_ABORT = @bitCast(c_long, c_ulong(0x80004004)); +pub const E_FAIL = @bitCast(c_long, c_ulong(0x80004005)); +pub const E_UNEXPECTED = @bitCast(c_long, c_ulong(0x8000FFFF)); +pub const E_ACCESSDENIED = @bitCast(c_long, c_ulong(0x80070005)); +pub const E_HANDLE = @bitCast(c_long, c_ulong(0x80070006)); +pub const E_OUTOFMEMORY = @bitCast(c_long, c_ulong(0x8007000E)); +pub const E_INVALIDARG = @bitCast(c_long, c_ulong(0x80070057)); + +pub const FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; +pub const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000; +pub const FILE_FLAG_NO_BUFFERING = 0x20000000; +pub const FILE_FLAG_OPEN_NO_RECALL = 0x00100000; +pub const FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000; +pub const FILE_FLAG_OVERLAPPED = 0x40000000; +pub const FILE_FLAG_POSIX_SEMANTICS = 0x0100000; +pub const FILE_FLAG_RANDOM_ACCESS = 0x10000000; +pub const FILE_FLAG_SESSION_AWARE = 0x00800000; +pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; +pub const FILE_FLAG_WRITE_THROUGH = 0x80000000; + +pub const SMALL_RECT = extern struct { + Left: SHORT, + Top: SHORT, + Right: SHORT, + Bottom: SHORT, +}; + +pub const COORD = extern struct { + X: SHORT, + Y: SHORT, +}; + +pub const CREATE_UNICODE_ENVIRONMENT = 1024; + +pub const TLS_OUT_OF_INDEXES = 4294967295; +pub const IMAGE_TLS_DIRECTORY = extern struct { + StartAddressOfRawData: usize, + EndAddressOfRawData: usize, + AddressOfIndex: usize, + AddressOfCallBacks: usize, + SizeOfZeroFill: u32, + Characteristics: u32, +}; +pub const IMAGE_TLS_DIRECTORY64 = IMAGE_TLS_DIRECTORY; +pub const IMAGE_TLS_DIRECTORY32 = IMAGE_TLS_DIRECTORY; + +pub const PIMAGE_TLS_CALLBACK = ?extern fn (PVOID, DWORD, PVOID) void; + +pub const PROV_RSA_FULL = 1; + +pub const REGSAM = ACCESS_MASK; +pub const ACCESS_MASK = DWORD; +pub const PHKEY = *HKEY; +pub const HKEY = *HKEY__; +pub const HKEY__ = extern struct { + unused: c_int, +}; +pub const LSTATUS = LONG; + +pub const FILE_NOTIFY_INFORMATION = extern struct { + NextEntryOffset: DWORD, + Action: DWORD, + FileNameLength: DWORD, + FileName: [1]WCHAR, +}; + +pub const FILE_ACTION_ADDED = 0x00000001; +pub const FILE_ACTION_REMOVED = 0x00000002; +pub const FILE_ACTION_MODIFIED = 0x00000003; +pub const FILE_ACTION_RENAMED_OLD_NAME = 0x00000004; +pub const FILE_ACTION_RENAMED_NEW_NAME = 0x00000005; + +pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn (DWORD, DWORD, *OVERLAPPED) void; + +pub const FILE_LIST_DIRECTORY = 1; + +pub const FILE_NOTIFY_CHANGE_CREATION = 64; +pub const FILE_NOTIFY_CHANGE_SIZE = 8; +pub const FILE_NOTIFY_CHANGE_SECURITY = 256; +pub const FILE_NOTIFY_CHANGE_LAST_ACCESS = 32; +pub const FILE_NOTIFY_CHANGE_LAST_WRITE = 16; +pub const FILE_NOTIFY_CHANGE_DIR_NAME = 2; +pub const FILE_NOTIFY_CHANGE_FILE_NAME = 1; +pub const FILE_NOTIFY_CHANGE_ATTRIBUTES = 4; + +pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { + dwSize: COORD, + dwCursorPosition: COORD, + wAttributes: WORD, + srWindow: SMALL_RECT, + dwMaximumWindowSize: COORD, +}; + +pub const FOREGROUND_BLUE = 1; +pub const FOREGROUND_GREEN = 2; +pub const FOREGROUND_RED = 4; +pub const FOREGROUND_INTENSITY = 8; + +pub const LIST_ENTRY = extern struct { + Flink: *LIST_ENTRY, + Blink: *LIST_ENTRY, +}; + +pub const RTL_CRITICAL_SECTION_DEBUG = extern struct { + Type: WORD, + CreatorBackTraceIndex: WORD, + CriticalSection: *RTL_CRITICAL_SECTION, + ProcessLocksList: LIST_ENTRY, + EntryCount: DWORD, + ContentionCount: DWORD, + Flags: DWORD, + CreatorBackTraceIndexHigh: WORD, + SpareWORD: WORD, +}; + +pub const RTL_CRITICAL_SECTION = extern struct { + DebugInfo: *RTL_CRITICAL_SECTION_DEBUG, + LockCount: LONG, + RecursionCount: LONG, + OwningThread: HANDLE, + LockSemaphore: HANDLE, + SpinCount: ULONG_PTR, +}; + +pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; +pub const INIT_ONCE = RTL_RUN_ONCE; +pub const INIT_ONCE_STATIC_INIT = RTL_RUN_ONCE_INIT; +pub const INIT_ONCE_FN = extern fn (InitOnce: *INIT_ONCE, Parameter: ?*c_void, Context: ?*c_void) BOOL; + +pub const RTL_RUN_ONCE = extern struct { + Ptr: ?*c_void, +}; + +pub const RTL_RUN_ONCE_INIT = RTL_RUN_ONCE{ .Ptr = null }; + +pub const COINIT_APARTMENTTHREADED = COINIT.COINIT_APARTMENTTHREADED; +pub const COINIT_MULTITHREADED = COINIT.COINIT_MULTITHREADED; +pub const COINIT_DISABLE_OLE1DDE = COINIT.COINIT_DISABLE_OLE1DDE; +pub const COINIT_SPEED_OVER_MEMORY = COINIT.COINIT_SPEED_OVER_MEMORY; +pub const COINIT = extern enum { + COINIT_APARTMENTTHREADED = 2, + COINIT_MULTITHREADED = 0, + COINIT_DISABLE_OLE1DDE = 4, + COINIT_SPEED_OVER_MEMORY = 8, +}; + +/// > The maximum path of 32,767 characters is approximate, because the "\\?\" +/// > prefix may be expanded to a longer string by the system at run time, and +/// > this expansion applies to the total length. +/// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation +pub const PATH_MAX_WIDE = 32767; diff --git a/std/os/windows/kernel32.zig b/std/os/windows/kernel32.zig index b14e404c13..2bd1824620 100644 --- a/std/os/windows/kernel32.zig +++ b/std/os/windows/kernel32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVERLAPPED) BOOL; diff --git a/std/os/windows/ntdll.zig b/std/os/windows/ntdll.zig index 1b63c1e7ec..60c03ffc07 100644 --- a/std/os/windows/ntdll.zig +++ b/std/os/windows/ntdll.zig @@ -1,3 +1,3 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "NtDll" stdcallcc fn RtlCaptureStackBackTrace(FramesToSkip: DWORD, FramesToCapture: DWORD, BackTrace: **c_void, BackTraceHash: ?*DWORD) WORD; diff --git a/std/os/windows/ole32.zig b/std/os/windows/ole32.zig index a69137fcab..80f5bebc36 100644 --- a/std/os/windows/ole32.zig +++ b/std/os/windows/ole32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "ole32" stdcallcc fn CoTaskMemFree(pv: LPVOID) void; pub extern "ole32" stdcallcc fn CoUninitialize() void; diff --git a/std/os/windows/shell32.zig b/std/os/windows/shell32.zig index 84accecb70..9f24acc5c4 100644 --- a/std/os/windows/shell32.zig +++ b/std/os/windows/shell32.zig @@ -1,3 +1,3 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "shell32" stdcallcc fn SHGetKnownFolderPath(rfid: *const KNOWNFOLDERID, dwFlags: DWORD, hToken: ?HANDLE, ppszPath: *[*]WCHAR) HRESULT; diff --git a/std/pdb.zig b/std/pdb.zig index 2822d2546e..ffe2120296 100644 --- a/std/pdb.zig +++ b/std/pdb.zig @@ -662,7 +662,7 @@ const MsfStream = struct { } fn seekBy(self: *MsfStream, len: i64) !void { - self.pos += len; + self.pos = @intCast(u64, @intCast(i64, self.pos) + len); if (self.pos >= self.blocks.len * self.block_size) return error.EOF; } diff --git a/std/process.zig b/std/process.zig index b45074d67c..7c5e7fbb07 100644 --- a/std/process.zig +++ b/std/process.zig @@ -144,7 +144,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned windows_buf_len, ) catch |err| switch (err) { error.Unexpected => return error.EnvironmentVariableNotFound, - else => return err, + else => |e| return e, }; if (result > buf.len) { @@ -156,7 +156,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned error.DanglingSurrogateHalf => return error.InvalidUtf8, error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, - else => return err, + else => |e| return e, }; } } else { diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index bf14116edf..45ce23c00f 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -62,7 +62,7 @@ extern fn WinMainCRTStartup() noreturn { if (!builtin.single_threaded) { _ = @import("bootstrap_windows_tls.zig"); } - std.os.windows.ExitProcess(callMain()); + std.os.windows.kernel32.ExitProcess(callMain()); } // TODO https://github.com/ziglang/zig/issues/265 diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index 9c59fccefe..9c8b91c632 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -23,7 +23,7 @@ pub const StaticallyInitializedMutex = switch (builtin.os) { mutex: *StaticallyInitializedMutex, pub fn release(self: Held) void { - windows.LeaveCriticalSection(&self.mutex.lock); + windows.kernel32.LeaveCriticalSection(&self.mutex.lock); } }; @@ -40,20 +40,20 @@ pub const StaticallyInitializedMutex = switch (builtin.os) { Context: ?*c_void, ) windows.BOOL { const lock = @ptrCast(*windows.CRITICAL_SECTION, @alignCast(@alignOf(windows.CRITICAL_SECTION), Parameter)); - windows.InitializeCriticalSection(lock); + windows.kernel32.InitializeCriticalSection(lock); return windows.TRUE; } /// TODO: once https://github.com/ziglang/zig/issues/287 is solved and std.Mutex has a better /// implementation of a runtime initialized mutex, remove this function. pub fn deinit(self: *StaticallyInitializedMutex) void { - assert(windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null) != 0); - windows.DeleteCriticalSection(&self.lock); + windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); + windows.kernel32.DeleteCriticalSection(&self.lock); } pub fn acquire(self: *StaticallyInitializedMutex) Held { - assert(windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null) != 0); - windows.EnterCriticalSection(&self.lock); + windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); + windows.kernel32.EnterCriticalSection(&self.lock); return Held{ .mutex = self }; } }, -- cgit v1.2.3