From 01337e209366bf7eed9a8aca0b80be0e8c9c04f5 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 19 Jul 2024 11:35:22 -0700 Subject: [PATCH] fix regression of flock being called on wasi targets * common symbols are now public from std.c even if they live in std.posix * LOCK is now one of the common symbols since it is the same on 100% of operating systems. * flock is now void value on wasi and windows * std.fs.Dir now uses flock being void as feature detection, avoiding trying to call it on wasi and windows --- lib/std/c.zig | 31 +++++++++++++++++-------------- lib/std/fs/Dir.zig | 5 +++-- lib/std/os/linux.zig | 1 - lib/std/os/linux/arm-eabi.zig | 7 ------- lib/std/os/linux/arm64.zig | 7 ------- lib/std/os/linux/mips.zig | 7 ------- lib/std/os/linux/mips64.zig | 7 ------- lib/std/os/linux/powerpc.zig | 7 ------- lib/std/os/linux/powerpc64.zig | 7 ------- lib/std/os/linux/riscv64.zig | 7 ------- lib/std/os/linux/sparc64.zig | 7 ------- lib/std/os/linux/x86.zig | 7 ------- lib/std/os/linux/x86_64.zig | 7 ------- lib/std/posix.zig | 8 +++++++- 14 files changed, 27 insertions(+), 88 deletions(-) diff --git a/lib/std/c.zig b/lib/std/c.zig index c051303d33..cd743911f7 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -4,9 +4,6 @@ const c = @This(); const maxInt = std.math.maxInt; const assert = std.debug.assert; const page_size = std.mem.page_size; -const iovec = std.posix.iovec; -const iovec_const = std.posix.iovec_const; -const winsize = std.posix.winsize; const native_abi = builtin.abi; const native_arch = builtin.cpu.arch; const native_os = builtin.os.tag; @@ -23,6 +20,14 @@ const dragonfly = @import("c/dragonfly.zig"); const haiku = @import("c/haiku.zig"); const openbsd = @import("c/openbsd.zig"); +// These constants are shared among all operating systems even when not linking +// libc. + +pub const iovec = std.posix.iovec; +pub const iovec_const = std.posix.iovec_const; +pub const LOCK = std.posix.LOCK; +pub const winsize = std.posix.winsize; + /// The value of the link editor defined symbol _MH_EXECUTE_SYM is the address /// of the mach header in a Mach-O executable file type. It does not appear in /// any file type other than a MH_EXECUTE file type. The type of the symbol is @@ -1330,16 +1335,6 @@ pub const KERN = switch (native_os) { }, else => void, }; -pub const LOCK = switch (native_os) { - .linux => linux.LOCK, - .emscripten => emscripten.LOCK, - else => struct { - pub const SH = 1; - pub const EX = 2; - pub const NB = 4; - pub const UN = 8; - }, -}; pub const MADV = switch (native_os) { .linux => linux.MADV, .emscripten => emscripten.MADV, @@ -9032,6 +9027,11 @@ pub const sf_hdtr = switch (native_os) { else => void, }; +pub const flock = switch (native_os) { + .windows, .wasi => {}, + else => private.flock, +}; + pub extern "c" var environ: [*:null]?[*:0]u8; pub extern "c" fn fopen(noalias filename: [*:0]const u8, noalias modes: [*:0]const u8) ?*FILE; @@ -9116,7 +9116,6 @@ pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*u pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int; pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int; pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int; -pub extern "c" fn flock(fd: fd_t, operation: c_int) c_int; pub extern "c" fn ioctl(fd: fd_t, request: c_int, ...) c_int; pub extern "c" fn uname(buf: *utsname) c_int; @@ -9396,6 +9395,9 @@ pub extern "c" fn pthread_getthreadid_np() c_int; pub extern "c" fn pthread_set_name_np(thread: pthread_t, name: [*:0]const u8) void; pub extern "c" fn pthread_get_name_np(thread: pthread_t, name: [*:0]u8, len: usize) void; +// OS-specific bits. These are protected from being used on the wrong OS by +// comptime assertions inside each OS-specific file. + pub const AF_SUN = solaris.AF_SUN; pub const AT_SUN = solaris.AT_SUN; pub const FILE_EVENT = solaris.FILE_EVENT; @@ -9675,6 +9677,7 @@ const private = struct { extern "c" fn clock_getres(clk_id: clockid_t, tp: *timespec) c_int; extern "c" fn clock_gettime(clk_id: clockid_t, tp: *timespec) c_int; extern "c" fn copy_file_range(fd_in: fd_t, off_in: ?*i64, fd_out: fd_t, off_out: ?*i64, len: usize, flags: c_uint) isize; + extern "c" fn flock(fd: fd_t, operation: c_int) c_int; extern "c" fn fork() c_int; extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int; extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, buf: *Stat, flag: u32) c_int; diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig index 137936b09f..bf0db90a73 100644 --- a/lib/std/fs/Dir.zig +++ b/lib/std/fs/Dir.zig @@ -881,7 +881,7 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File const fd = try posix.openatZ(self.fd, sub_path, os_flags, 0); errdefer posix.close(fd); - if (!has_flock_open_flags and flags.lock != .none) { + if (have_flock and !has_flock_open_flags and flags.lock != .none) { // TODO: integrate async I/O const lock_nonblocking: i32 = if (flags.lock_nonblocking) posix.LOCK.NB else 0; try posix.flock(fd, switch (flags.lock) { @@ -1029,7 +1029,7 @@ pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags const fd = try posix.openatZ(self.fd, sub_path_c, os_flags, flags.mode); errdefer posix.close(fd); - if (!has_flock_open_flags and flags.lock != .none) { + if (have_flock and !has_flock_open_flags and flags.lock != .none) { // TODO: integrate async I/O const lock_nonblocking: i32 = if (flags.lock_nonblocking) posix.LOCK.NB else 0; try posix.flock(fd, switch (flags.lock) { @@ -2702,3 +2702,4 @@ const Allocator = std.mem.Allocator; const assert = std.debug.assert; const windows = std.os.windows; const native_os = builtin.os.tag; +const have_flock = @TypeOf(posix.system.flock) != void; diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 7d7106102c..6b1789f916 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -69,7 +69,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx; pub const F = arch_bits.F; pub const Flock = arch_bits.Flock; pub const HWCAP = arch_bits.HWCAP; -pub const LOCK = arch_bits.LOCK; pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT; pub const REG = arch_bits.REG; pub const SC = arch_bits.SC; diff --git a/lib/std/os/linux/arm-eabi.zig b/lib/std/os/linux/arm-eabi.zig index 59923e619f..d969608d2f 100644 --- a/lib/std/os/linux/arm-eabi.zig +++ b/lib/std/os/linux/arm-eabi.zig @@ -167,13 +167,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_2.6"; diff --git a/lib/std/os/linux/arm64.zig b/lib/std/os/linux/arm64.zig index 0daa33c52f..2c5b21db70 100644 --- a/lib/std/os/linux/arm64.zig +++ b/lib/std/os/linux/arm64.zig @@ -149,13 +149,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const VDSO = struct { pub const CGT_SYM = "__kernel_clock_gettime"; pub const CGT_VER = "LINUX_2.6.39"; diff --git a/lib/std/os/linux/mips.zig b/lib/std/os/linux/mips.zig index 1b83194933..abf047e838 100644 --- a/lib/std/os/linux/mips.zig +++ b/lib/std/os/linux/mips.zig @@ -239,13 +239,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const MMAP2_UNIT = 4096; pub const VDSO = struct { diff --git a/lib/std/os/linux/mips64.zig b/lib/std/os/linux/mips64.zig index 43b1e1ad95..9be0f41c4f 100644 --- a/lib/std/os/linux/mips64.zig +++ b/lib/std/os/linux/mips64.zig @@ -224,13 +224,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const MMAP2_UNIT = 4096; pub const VDSO = struct { diff --git a/lib/std/os/linux/powerpc.zig b/lib/std/os/linux/powerpc.zig index 6b7181600d..034520f68b 100644 --- a/lib/std/os/linux/powerpc.zig +++ b/lib/std/os/linux/powerpc.zig @@ -168,13 +168,6 @@ pub const F = struct { pub const UNLCK = 2; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const VDSO = struct { pub const CGT_SYM = "__kernel_clock_gettime"; pub const CGT_VER = "LINUX_2.6.15"; diff --git a/lib/std/os/linux/powerpc64.zig b/lib/std/os/linux/powerpc64.zig index 733c7305cb..cdc117d9bf 100644 --- a/lib/std/os/linux/powerpc64.zig +++ b/lib/std/os/linux/powerpc64.zig @@ -168,13 +168,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const VDSO = struct { pub const CGT_SYM = "__kernel_clock_gettime"; pub const CGT_VER = "LINUX_2.6.15"; diff --git a/lib/std/os/linux/riscv64.zig b/lib/std/os/linux/riscv64.zig index 7ab814fa71..6c590348f4 100644 --- a/lib/std/os/linux/riscv64.zig +++ b/lib/std/os/linux/riscv64.zig @@ -134,13 +134,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const UN = 8; - pub const NB = 4; -}; - pub const blksize_t = i32; pub const nlink_t = u32; pub const time_t = isize; diff --git a/lib/std/os/linux/sparc64.zig b/lib/std/os/linux/sparc64.zig index ac2c6d6b83..5dff773ca4 100644 --- a/lib/std/os/linux/sparc64.zig +++ b/lib/std/os/linux/sparc64.zig @@ -218,13 +218,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const NB = 4; - pub const UN = 8; -}; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_2.6"; diff --git a/lib/std/os/linux/x86.zig b/lib/std/os/linux/x86.zig index 57f5b53aa0..53d6717384 100644 --- a/lib/std/os/linux/x86.zig +++ b/lib/std/os/linux/x86.zig @@ -181,13 +181,6 @@ pub const F = struct { pub const UNLCK = 2; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const NB = 4; - pub const UN = 8; -}; - pub const MMAP2_UNIT = 4096; pub const VDSO = struct { diff --git a/lib/std/os/linux/x86_64.zig b/lib/std/os/linux/x86_64.zig index d9022d4c5a..f613e8cb0a 100644 --- a/lib/std/os/linux/x86_64.zig +++ b/lib/std/os/linux/x86_64.zig @@ -195,13 +195,6 @@ pub const REG = struct { pub const CR2 = 22; }; -pub const LOCK = struct { - pub const SH = 1; - pub const EX = 2; - pub const NB = 4; - pub const UN = 8; -}; - pub const Flock = extern struct { type: i16, whence: i16, diff --git a/lib/std/posix.zig b/lib/std/posix.zig index a2d02a7772..facb4b8cd6 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -70,7 +70,6 @@ pub const IOV_MAX = system.IOV_MAX; pub const IPPROTO = system.IPPROTO; pub const KERN = system.KERN; pub const Kevent = system.Kevent; -pub const LOCK = system.LOCK; pub const MADV = system.MADV; pub const MAP = system.MAP; pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN; @@ -202,6 +201,13 @@ pub const winsize = extern struct { ypixel: u16, }; +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const NB = 4; + pub const UN = 8; +}; + pub const LOG = struct { /// system is unusable pub const EMERG = 0;