Skip to content

Commit 01337e2

Browse files
committed
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
1 parent 7157189 commit 01337e2

14 files changed

+27
-88
lines changed

lib/std/c.zig

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ const c = @This();
44
const maxInt = std.math.maxInt;
55
const assert = std.debug.assert;
66
const page_size = std.mem.page_size;
7-
const iovec = std.posix.iovec;
8-
const iovec_const = std.posix.iovec_const;
9-
const winsize = std.posix.winsize;
107
const native_abi = builtin.abi;
118
const native_arch = builtin.cpu.arch;
129
const native_os = builtin.os.tag;
@@ -23,6 +20,14 @@ const dragonfly = @import("c/dragonfly.zig");
2320
const haiku = @import("c/haiku.zig");
2421
const openbsd = @import("c/openbsd.zig");
2522

23+
// These constants are shared among all operating systems even when not linking
24+
// libc.
25+
26+
pub const iovec = std.posix.iovec;
27+
pub const iovec_const = std.posix.iovec_const;
28+
pub const LOCK = std.posix.LOCK;
29+
pub 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
};
1333-
pub 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-
};
13431338
pub 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

9030+
pub const flock = switch (native_os) {
9031+
.windows, .wasi => {},
9032+
else => private.flock,
9033+
};
9034+
90359035
pub extern "c" var environ: [*:null]?[*:0]u8;
90369036

90379037
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
91169116
pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
91179117
pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
91189118
pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int;
9119-
pub extern "c" fn flock(fd: fd_t, operation: c_int) c_int;
91209119
pub extern "c" fn ioctl(fd: fd_t, request: c_int, ...) c_int;
91219120
pub extern "c" fn uname(buf: *utsname) c_int;
91229121

@@ -9396,6 +9395,9 @@ pub extern "c" fn pthread_getthreadid_np() c_int;
93969395
pub extern "c" fn pthread_set_name_np(thread: pthread_t, name: [*:0]const u8) void;
93979396
pub 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+
93999401
pub const AF_SUN = solaris.AF_SUN;
94009402
pub const AT_SUN = solaris.AT_SUN;
94019403
pub 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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
27022702
const assert = std.debug.assert;
27032703
const windows = std.os.windows;
27042704
const native_os = builtin.os.tag;
2705+
const have_flock = @TypeOf(posix.system.flock) != void;

lib/std/os/linux.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
6969
pub const F = arch_bits.F;
7070
pub const Flock = arch_bits.Flock;
7171
pub const HWCAP = arch_bits.HWCAP;
72-
pub const LOCK = arch_bits.LOCK;
7372
pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
7473
pub const REG = arch_bits.REG;
7574
pub const SC = arch_bits.SC;

lib/std/os/linux/arm-eabi.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,6 @@ pub const F = struct {
167167
pub const GETOWNER_UIDS = 17;
168168
};
169169

170-
pub 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-
177170
pub 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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,6 @@ pub const F = struct {
149149
pub const GETOWNER_UIDS = 17;
150150
};
151151

152-
pub 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-
159152
pub 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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,6 @@ pub const F = struct {
239239
pub const GETOWNER_UIDS = 17;
240240
};
241241

242-
pub 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-
249242
pub const MMAP2_UNIT = 4096;
250243

251244
pub const VDSO = struct {

lib/std/os/linux/mips64.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,6 @@ pub const F = struct {
224224
pub const GETOWNER_UIDS = 17;
225225
};
226226

227-
pub 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-
234227
pub const MMAP2_UNIT = 4096;
235228

236229
pub const VDSO = struct {

lib/std/os/linux/powerpc.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ pub const F = struct {
168168
pub const UNLCK = 2;
169169
};
170170

171-
pub 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-
178171
pub 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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ pub const F = struct {
168168
pub const GETOWNER_UIDS = 17;
169169
};
170170

171-
pub 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-
178171
pub 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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ pub const F = struct {
134134
pub const GETOWNER_UIDS = 17;
135135
};
136136

137-
pub 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-
144137
pub const blksize_t = i32;
145138
pub const nlink_t = u32;
146139
pub const time_t = isize;

lib/std/os/linux/sparc64.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,6 @@ pub const F = struct {
218218
pub const GETOWNER_UIDS = 17;
219219
};
220220

221-
pub 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-
228221
pub 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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ pub const F = struct {
181181
pub const UNLCK = 2;
182182
};
183183

184-
pub 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-
191184
pub const MMAP2_UNIT = 4096;
192185

193186
pub const VDSO = struct {

lib/std/os/linux/x86_64.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,6 @@ pub const REG = struct {
195195
pub const CR2 = 22;
196196
};
197197

198-
pub 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-
205198
pub const Flock = extern struct {
206199
type: i16,
207200
whence: i16,

lib/std/posix.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub const IOV_MAX = system.IOV_MAX;
7070
pub const IPPROTO = system.IPPROTO;
7171
pub const KERN = system.KERN;
7272
pub const Kevent = system.Kevent;
73-
pub const LOCK = system.LOCK;
7473
pub const MADV = system.MADV;
7574
pub const MAP = system.MAP;
7675
pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
@@ -202,6 +201,13 @@ pub const winsize = extern struct {
202201
ypixel: u16,
203202
};
204203

204+
pub 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+
205211
pub const LOG = struct {
206212
/// system is unusable
207213
pub const EMERG = 0;

0 commit comments

Comments
 (0)