authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-19 11:35:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-19 11:35:22-07:00
log01337e209366bf7eed9a8aca0b80be0e8c9c04f5
tree82d84ded03cf82eca3662acc1b16b100964937b4
parent7157189143e83dedb29d25553d2841ff2576dc5c

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

14 files changed, 27 insertions(+), 88 deletions(-)

lib/std/c.zig+17-14
......@@ -4,9 +4,6 @@ const c = @This();
44const maxInt = std.math.maxInt;
55const assert = std.debug.assert;
66const page_size = std.mem.page_size;
7const iovec = std.posix.iovec;
8const iovec_const = std.posix.iovec_const;
9const winsize = std.posix.winsize;
107const native_abi = builtin.abi;
118const native_arch = builtin.cpu.arch;
129const native_os = builtin.os.tag;
......@@ -23,6 +20,14 @@ const dragonfly = @import("c/dragonfly.zig");
2320const haiku = @import("c/haiku.zig");
2421const openbsd = @import("c/openbsd.zig");
2522
23// These constants are shared among all operating systems even when not linking
24// libc.
25
26pub const iovec = std.posix.iovec;
27pub const iovec_const = std.posix.iovec_const;
28pub const LOCK = std.posix.LOCK;
29pub const winsize = std.posix.winsize;
30
2631/// The value of the link editor defined symbol _MH_EXECUTE_SYM is the address
2732/// of the mach header in a Mach-O executable file type. It does not appear in
2833/// 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) {
13301335 },
13311336 else => void,
13321337};
1333pub const LOCK = switch (native_os) {
1334 .linux => linux.LOCK,
1335 .emscripten => emscripten.LOCK,
1336 else => struct {
1337 pub const SH = 1;
1338 pub const EX = 2;
1339 pub const NB = 4;
1340 pub const UN = 8;
1341 },
1342};
13431338pub const MADV = switch (native_os) {
13441339 .linux => linux.MADV,
13451340 .emscripten => emscripten.MADV,
......@@ -9032,6 +9027,11 @@ pub const sf_hdtr = switch (native_os) {
90329027 else => void,
90339028};
90349029
9030pub const flock = switch (native_os) {
9031 .windows, .wasi => {},
9032 else => private.flock,
9033};
9034
90359035pub extern "c" var environ: [*:null]?[*:0]u8;
90369036
90379037pub 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
91169116pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
91179117pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
91189118pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int;
9119pub extern "c" fn flock(fd: fd_t, operation: c_int) c_int;
91209119pub extern "c" fn ioctl(fd: fd_t, request: c_int, ...) c_int;
91219120pub extern "c" fn uname(buf: *utsname) c_int;
91229121
......@@ -9396,6 +9395,9 @@ pub extern "c" fn pthread_getthreadid_np() c_int;
93969395pub extern "c" fn pthread_set_name_np(thread: pthread_t, name: [*:0]const u8) void;
93979396pub extern "c" fn pthread_get_name_np(thread: pthread_t, name: [*:0]u8, len: usize) void;
93989397
9398// OS-specific bits. These are protected from being used on the wrong OS by
9399// comptime assertions inside each OS-specific file.
9400
93999401pub const AF_SUN = solaris.AF_SUN;
94009402pub const AT_SUN = solaris.AT_SUN;
94019403pub const FILE_EVENT = solaris.FILE_EVENT;
......@@ -9675,6 +9677,7 @@ const private = struct {
96759677 extern "c" fn clock_getres(clk_id: clockid_t, tp: *timespec) c_int;
96769678 extern "c" fn clock_gettime(clk_id: clockid_t, tp: *timespec) c_int;
96779679 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;
9680 extern "c" fn flock(fd: fd_t, operation: c_int) c_int;
96789681 extern "c" fn fork() c_int;
96799682 extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
96809683 extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, buf: *Stat, flag: u32) c_int;
lib/std/fs/Dir.zig+3-2
......@@ -881,7 +881,7 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File
881881 const fd = try posix.openatZ(self.fd, sub_path, os_flags, 0);
882882 errdefer posix.close(fd);
883883
884 if (!has_flock_open_flags and flags.lock != .none) {
884 if (have_flock and !has_flock_open_flags and flags.lock != .none) {
885885 // TODO: integrate async I/O
886886 const lock_nonblocking: i32 = if (flags.lock_nonblocking) posix.LOCK.NB else 0;
887887 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
10291029 const fd = try posix.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
10301030 errdefer posix.close(fd);
10311031
1032 if (!has_flock_open_flags and flags.lock != .none) {
1032 if (have_flock and !has_flock_open_flags and flags.lock != .none) {
10331033 // TODO: integrate async I/O
10341034 const lock_nonblocking: i32 = if (flags.lock_nonblocking) posix.LOCK.NB else 0;
10351035 try posix.flock(fd, switch (flags.lock) {
......@@ -2702,3 +2702,4 @@ const Allocator = std.mem.Allocator;
27022702const assert = std.debug.assert;
27032703const windows = std.os.windows;
27042704const native_os = builtin.os.tag;
2705const have_flock = @TypeOf(posix.system.flock) != void;
lib/std/os/linux.zig-1
......@@ -69,7 +69,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
6969pub const F = arch_bits.F;
7070pub const Flock = arch_bits.Flock;
7171pub const HWCAP = arch_bits.HWCAP;
72pub const LOCK = arch_bits.LOCK;
7372pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
7473pub const REG = arch_bits.REG;
7574pub const SC = arch_bits.SC;
lib/std/os/linux/arm-eabi.zig-7
......@@ -167,13 +167,6 @@ pub const F = struct {
167167 pub const GETOWNER_UIDS = 17;
168168};
169169
170pub const LOCK = struct {
171 pub const SH = 1;
172 pub const EX = 2;
173 pub const UN = 8;
174 pub const NB = 4;
175};
176
177170pub const VDSO = struct {
178171 pub const CGT_SYM = "__vdso_clock_gettime";
179172 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/arm64.zig-7
......@@ -149,13 +149,6 @@ pub const F = struct {
149149 pub const GETOWNER_UIDS = 17;
150150};
151151
152pub const LOCK = struct {
153 pub const SH = 1;
154 pub const EX = 2;
155 pub const UN = 8;
156 pub const NB = 4;
157};
158
159152pub const VDSO = struct {
160153 pub const CGT_SYM = "__kernel_clock_gettime";
161154 pub const CGT_VER = "LINUX_2.6.39";
lib/std/os/linux/mips.zig-7
......@@ -239,13 +239,6 @@ pub const F = struct {
239239 pub const GETOWNER_UIDS = 17;
240240};
241241
242pub const LOCK = struct {
243 pub const SH = 1;
244 pub const EX = 2;
245 pub const UN = 8;
246 pub const NB = 4;
247};
248
249242pub const MMAP2_UNIT = 4096;
250243
251244pub const VDSO = struct {
lib/std/os/linux/mips64.zig-7
......@@ -224,13 +224,6 @@ pub const F = struct {
224224 pub const GETOWNER_UIDS = 17;
225225};
226226
227pub const LOCK = struct {
228 pub const SH = 1;
229 pub const EX = 2;
230 pub const UN = 8;
231 pub const NB = 4;
232};
233
234227pub const MMAP2_UNIT = 4096;
235228
236229pub const VDSO = struct {
lib/std/os/linux/powerpc.zig-7
......@@ -168,13 +168,6 @@ pub const F = struct {
168168 pub const UNLCK = 2;
169169};
170170
171pub const LOCK = struct {
172 pub const SH = 1;
173 pub const EX = 2;
174 pub const UN = 8;
175 pub const NB = 4;
176};
177
178171pub const VDSO = struct {
179172 pub const CGT_SYM = "__kernel_clock_gettime";
180173 pub const CGT_VER = "LINUX_2.6.15";
lib/std/os/linux/powerpc64.zig-7
......@@ -168,13 +168,6 @@ pub const F = struct {
168168 pub const GETOWNER_UIDS = 17;
169169};
170170
171pub const LOCK = struct {
172 pub const SH = 1;
173 pub const EX = 2;
174 pub const UN = 8;
175 pub const NB = 4;
176};
177
178171pub const VDSO = struct {
179172 pub const CGT_SYM = "__kernel_clock_gettime";
180173 pub const CGT_VER = "LINUX_2.6.15";
lib/std/os/linux/riscv64.zig-7
......@@ -134,13 +134,6 @@ pub const F = struct {
134134 pub const GETOWNER_UIDS = 17;
135135};
136136
137pub const LOCK = struct {
138 pub const SH = 1;
139 pub const EX = 2;
140 pub const UN = 8;
141 pub const NB = 4;
142};
143
144137pub const blksize_t = i32;
145138pub const nlink_t = u32;
146139pub const time_t = isize;
lib/std/os/linux/sparc64.zig-7
......@@ -218,13 +218,6 @@ pub const F = struct {
218218 pub const GETOWNER_UIDS = 17;
219219};
220220
221pub const LOCK = struct {
222 pub const SH = 1;
223 pub const EX = 2;
224 pub const NB = 4;
225 pub const UN = 8;
226};
227
228221pub const VDSO = struct {
229222 pub const CGT_SYM = "__vdso_clock_gettime";
230223 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/x86.zig-7
......@@ -181,13 +181,6 @@ pub const F = struct {
181181 pub const UNLCK = 2;
182182};
183183
184pub const LOCK = struct {
185 pub const SH = 1;
186 pub const EX = 2;
187 pub const NB = 4;
188 pub const UN = 8;
189};
190
191184pub const MMAP2_UNIT = 4096;
192185
193186pub const VDSO = struct {
lib/std/os/linux/x86_64.zig-7
......@@ -195,13 +195,6 @@ pub const REG = struct {
195195 pub const CR2 = 22;
196196};
197197
198pub const LOCK = struct {
199 pub const SH = 1;
200 pub const EX = 2;
201 pub const NB = 4;
202 pub const UN = 8;
203};
204
205198pub const Flock = extern struct {
206199 type: i16,
207200 whence: i16,
lib/std/posix.zig+7-1
......@@ -70,7 +70,6 @@ pub const IOV_MAX = system.IOV_MAX;
7070pub const IPPROTO = system.IPPROTO;
7171pub const KERN = system.KERN;
7272pub const Kevent = system.Kevent;
73pub const LOCK = system.LOCK;
7473pub const MADV = system.MADV;
7574pub const MAP = system.MAP;
7675pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
......@@ -202,6 +201,13 @@ pub const winsize = extern struct {
202201 ypixel: u16,
203202};
204203
204pub const LOCK = struct {
205 pub const SH = 1;
206 pub const EX = 2;
207 pub const NB = 4;
208 pub const UN = 8;
209};
210
205211pub const LOG = struct {
206212 /// system is unusable
207213 pub const EMERG = 0;