authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-09 22:04:05-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-14 10:12:46-06:00
logf66a607607c20841ed59a1fe11486333f8621426
tree1914649ce4cc7f5691fec0602fa7b46d3414aee8
parente1868029e9c861fa6b1382ba52052264fc5f9c31

Define Flock for all posix systems


11 files changed, 117 insertions(+), 20 deletions(-)

lib/std/fs.zig+8-7
...@@ -681,13 +681,14 @@ pub const Dir = struct {...@@ -681,13 +681,14 @@ pub const Dir = struct {
681 var locked = false;681 var locked = false;
682 if (flags.lock) {682 if (flags.lock) {
683 // TODO: integrate async I/O683 // TODO: integrate async I/O
684 try os.fcntlFlockBlocking(fd, &os.flock{684 // mem.zeroes is used here because flock's structure can vary across architectures and systems
685 .lock_type = if (flags.write) os.F_WRLCK else os.F_RDLCK,685 var flock = mem.zeroes(os.Flock);
686 .whence = os.SEEK_SET,686 flock.l_type = if (flags.write) os.F_WRLCK else os.F_RDLCK;
687 .start = 0,687 flock.l_whence = os.SEEK_SET;
688 .len = 0,688 flock.l_start = 0;
689 .pid = 0,689 flock.l_len = 0;
690 });690 flock.l_pid = 0;
691 try os.fcntlFlockBlocking(fd, &flock);
691 locked = true;692 locked = true;
692 }693 }
693694
lib/std/os.zig+1-1
...@@ -1141,7 +1141,7 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)...@@ -1141,7 +1141,7 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
1141}1141}
11421142
1143/// Attempts to get lock the file, blocking if the file is locked.1143/// Attempts to get lock the file, blocking if the file is locked.
1144pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const flock) OpenError!void {1144pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const Flock) OpenError!void {
1145 const rc = system.fcntl(fd, F_SETLKW, flock_p);1145 const rc = system.fcntl(fd, F_SETLKW, flock_p);
1146 if (rc < 0) {1146 if (rc < 0) {
1147 std.debug.panic("fcntl error: {}\n", .{rc});1147 std.debug.panic("fcntl error: {}\n", .{rc});
lib/std/os/bits/darwin.zig+8
...@@ -55,6 +55,14 @@ pub const mach_timebase_info_data = extern struct {...@@ -55,6 +55,14 @@ pub const mach_timebase_info_data = extern struct {
55pub const off_t = i64;55pub const off_t = i64;
56pub const ino_t = u64;56pub const ino_t = u64;
5757
58pub const Flock = extern struct {
59 l_start: off_t,
60 l_len: off_t,
61 l_pid: pid_t,
62 l_type: i16,
63 l_whence: i16,
64};
65
58/// Renamed to Stat to not conflict with the stat function.66/// Renamed to Stat to not conflict with the stat function.
59/// atime, mtime, and ctime have functions to return `timespec`,67/// atime, mtime, and ctime have functions to return `timespec`,
60/// because although this is a POSIX API, the layout and names of68/// because although this is a POSIX API, the layout and names of
lib/std/os/bits/freebsd.zig+14
...@@ -51,6 +51,16 @@ pub const dl_phdr_info = extern struct {...@@ -51,6 +51,16 @@ pub const dl_phdr_info = extern struct {
51 dlpi_phnum: u16,51 dlpi_phnum: u16,
52};52};
5353
54pub const Flock = extern struct {
55 l_start: off_t,
56 l_len: off_t,
57 l_pid: pid_t,
58 l_type: i16,
59 l_whence: i16,
60 l_sysid: i32,
61 __unused: [4]u8,
62};
63
54pub const msghdr = extern struct {64pub const msghdr = extern struct {
55 /// optional address65 /// optional address
56 msg_name: ?*sockaddr,66 msg_name: ?*sockaddr,
...@@ -350,6 +360,10 @@ pub const F_GETLK = 5;...@@ -350,6 +360,10 @@ pub const F_GETLK = 5;
350pub const F_SETLK = 6;360pub const F_SETLK = 6;
351pub const F_SETLKW = 7;361pub const F_SETLKW = 7;
352362
363pub const F_RDLCK = 1;
364pub const F_WRLCK = 3;
365pub const F_UNLCK = 2;
366
353pub const F_SETOWN_EX = 15;367pub const F_SETOWN_EX = 15;
354pub const F_GETOWN_EX = 16;368pub const F_GETOWN_EX = 16;
355369
lib/std/os/bits/linux/arm-eabi.zig+15
...@@ -8,6 +8,7 @@ const stack_t = linux.stack_t;...@@ -8,6 +8,7 @@ const stack_t = linux.stack_t;
8const sigset_t = linux.sigset_t;8const sigset_t = linux.sigset_t;
9const uid_t = linux.uid_t;9const uid_t = linux.uid_t;
10const gid_t = linux.gid_t;10const gid_t = linux.gid_t;
11const pid_t = linux.pid_t;
1112
12pub const SYS_restart_syscall = 0;13pub const SYS_restart_syscall = 0;
13pub const SYS_exit = 1;14pub const SYS_exit = 1;
...@@ -446,6 +447,10 @@ pub const F_GETLK = 12;...@@ -446,6 +447,10 @@ pub const F_GETLK = 12;
446pub const F_SETLK = 13;447pub const F_SETLK = 13;
447pub const F_SETLKW = 14;448pub const F_SETLKW = 14;
448449
450pub const F_RDLCK = 0;
451pub const F_WRLCK = 1;
452pub const F_UNLCK = 2;
453
449pub const F_SETOWN_EX = 15;454pub const F_SETOWN_EX = 15;
450pub const F_GETOWN_EX = 16;455pub const F_GETOWN_EX = 16;
451456
...@@ -493,6 +498,16 @@ pub const HWCAP_IDIV = HWCAP_IDIVA | HWCAP_IDIVT;...@@ -493,6 +498,16 @@ pub const HWCAP_IDIV = HWCAP_IDIVA | HWCAP_IDIVT;
493pub const HWCAP_LPAE = 1 << 20;498pub const HWCAP_LPAE = 1 << 20;
494pub const HWCAP_EVTSTRM = 1 << 21;499pub const HWCAP_EVTSTRM = 1 << 21;
495500
501pub const Flock = extern struct {
502 l_type: i16,
503 l_whence: i16,
504 __pad0: [4]u8,
505 l_start: off_t,
506 l_len: off_t,
507 l_pid: pid_t,
508 __unused: [4]u8,
509};
510
496pub const msghdr = extern struct {511pub const msghdr = extern struct {
497 msg_name: ?*sockaddr,512 msg_name: ?*sockaddr,
498 msg_namelen: socklen_t,513 msg_namelen: socklen_t,
lib/std/os/bits/linux/arm64.zig+14
...@@ -8,6 +8,7 @@ const iovec = linux.iovec;...@@ -8,6 +8,7 @@ const iovec = linux.iovec;
8const iovec_const = linux.iovec_const;8const iovec_const = linux.iovec_const;
9const uid_t = linux.uid_t;9const uid_t = linux.uid_t;
10const gid_t = linux.gid_t;10const gid_t = linux.gid_t;
11const pid_t = linux.pid_t;
11const stack_t = linux.stack_t;12const stack_t = linux.stack_t;
12const sigset_t = linux.sigset_t;13const sigset_t = linux.sigset_t;
1314
...@@ -339,6 +340,10 @@ pub const F_GETLK = 5;...@@ -339,6 +340,10 @@ pub const F_GETLK = 5;
339pub const F_SETLK = 6;340pub const F_SETLK = 6;
340pub const F_SETLKW = 7;341pub const F_SETLKW = 7;
341342
343pub const F_RDLCK = 0;
344pub const F_WRLCK = 1;
345pub const F_UNLCK = 2;
346
342pub const F_SETOWN_EX = 15;347pub const F_SETOWN_EX = 15;
343pub const F_GETOWN_EX = 16;348pub const F_GETOWN_EX = 16;
344349
...@@ -362,6 +367,15 @@ pub const MAP_NORESERVE = 0x4000;...@@ -362,6 +367,15 @@ pub const MAP_NORESERVE = 0x4000;
362pub const VDSO_CGT_SYM = "__kernel_clock_gettime";367pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
363pub const VDSO_CGT_VER = "LINUX_2.6.39";368pub const VDSO_CGT_VER = "LINUX_2.6.39";
364369
370pub const Flock = extern struct {
371 l_type: i16,
372 l_whence: i16,
373 l_start: off_t,
374 l_len: off_t,
375 l_pid: pid_t,
376 __unused: [4]u8,
377};
378
365pub const msghdr = extern struct {379pub const msghdr = extern struct {
366 msg_name: ?*sockaddr,380 msg_name: ?*sockaddr,
367 msg_namelen: socklen_t,381 msg_namelen: socklen_t,
lib/std/os/bits/linux/i386.zig+10-6
...@@ -472,6 +472,10 @@ pub const F_GETLK = 12;...@@ -472,6 +472,10 @@ pub const F_GETLK = 12;
472pub const F_SETLK = 13;472pub const F_SETLK = 13;
473pub const F_SETLKW = 14;473pub const F_SETLKW = 14;
474474
475pub const F_RDLCK = 0;
476pub const F_WRLCK = 1;
477pub const F_UNLCK = 2;
478
475pub const F_SETOWN_EX = 15;479pub const F_SETOWN_EX = 15;
476pub const F_GETOWN_EX = 16;480pub const F_GETOWN_EX = 16;
477481
...@@ -489,12 +493,12 @@ pub const MMAP2_UNIT = 4096;...@@ -489,12 +493,12 @@ pub const MMAP2_UNIT = 4096;
489pub const VDSO_CGT_SYM = "__vdso_clock_gettime";493pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
490pub const VDSO_CGT_VER = "LINUX_2.6";494pub const VDSO_CGT_VER = "LINUX_2.6";
491495
492pub const flock = extern struct {496pub const Flock = extern struct {
493 lock_type: i16,497 l_type: i16,
494 whence: i16,498 l_whence: i16,
495 start: off_t,499 l_start: off_t,
496 len: off_t,500 l_len: off_t,
497 pid: pid_t,501 l_pid: pid_t,
498};502};
499503
500pub const msghdr = extern struct {504pub const msghdr = extern struct {
lib/std/os/bits/linux/mipsel.zig+15
...@@ -5,6 +5,7 @@ const iovec = linux.iovec;...@@ -5,6 +5,7 @@ const iovec = linux.iovec;
5const iovec_const = linux.iovec_const;5const iovec_const = linux.iovec_const;
6const uid_t = linux.uid_t;6const uid_t = linux.uid_t;
7const gid_t = linux.gid_t;7const gid_t = linux.gid_t;
8const pid_t = linux.pid_t;
89
9pub const SYS_Linux = 4000;10pub const SYS_Linux = 4000;
10pub const SYS_syscall = (SYS_Linux + 0);11pub const SYS_syscall = (SYS_Linux + 0);
...@@ -412,6 +413,10 @@ pub const F_GETLK = 33;...@@ -412,6 +413,10 @@ pub const F_GETLK = 33;
412pub const F_SETLK = 34;413pub const F_SETLK = 34;
413pub const F_SETLKW = 35;414pub const F_SETLKW = 35;
414415
416pub const F_RDLCK = 0;
417pub const F_WRLCK = 1;
418pub const F_UNLCK = 2;
419
415pub const F_SETOWN_EX = 15;420pub const F_SETOWN_EX = 15;
416pub const F_GETOWN_EX = 16;421pub const F_GETOWN_EX = 16;
417422
...@@ -457,6 +462,16 @@ pub const SO_RCVBUFFORCE = 33;...@@ -457,6 +462,16 @@ pub const SO_RCVBUFFORCE = 33;
457pub const VDSO_CGT_SYM = "__kernel_clock_gettime";462pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
458pub const VDSO_CGT_VER = "LINUX_2.6.39";463pub const VDSO_CGT_VER = "LINUX_2.6.39";
459464
465pub const Flock = extern struct {
466 l_type: i16,
467 l_whence: i16,
468 __pad0: [4]u8,
469 l_start: off_t,
470 l_len: off_t,
471 l_pid: pid_t,
472 __unused: [4]u8,
473};
474
460pub const blksize_t = i32;475pub const blksize_t = i32;
461pub const nlink_t = u32;476pub const nlink_t = u32;
462pub const time_t = isize;477pub const time_t = isize;
lib/std/os/bits/linux/riscv64.zig+14
...@@ -2,6 +2,7 @@...@@ -2,6 +2,7 @@
2const std = @import("../../../std.zig");2const std = @import("../../../std.zig");
3const uid_t = std.os.linux.uid_t;3const uid_t = std.os.linux.uid_t;
4const gid_t = std.os.linux.gid_t;4const gid_t = std.os.linux.gid_t;
5const pid_t = std.os.linux.pid_t;
56
6pub const SYS_io_setup = 0;7pub const SYS_io_setup = 0;
7pub const SYS_io_destroy = 1;8pub const SYS_io_destroy = 1;
...@@ -332,6 +333,10 @@ pub const F_GETOWN = 9;...@@ -332,6 +333,10 @@ pub const F_GETOWN = 9;
332pub const F_SETSIG = 10;333pub const F_SETSIG = 10;
333pub const F_GETSIG = 11;334pub const F_GETSIG = 11;
334335
336pub const F_RDLCK = 0;
337pub const F_WRLCK = 1;
338pub const F_UNLCK = 2;
339
335pub const F_SETOWN_EX = 15;340pub const F_SETOWN_EX = 15;
336pub const F_GETOWN_EX = 16;341pub const F_GETOWN_EX = 16;
337342
...@@ -350,6 +355,15 @@ pub const timespec = extern struct {...@@ -350,6 +355,15 @@ pub const timespec = extern struct {
350 tv_nsec: isize,355 tv_nsec: isize,
351};356};
352357
358pub const Flock = extern struct {
359 l_type: i16,
360 l_whence: i16,
361 l_start: off_t,
362 l_len: off_t,
363 l_pid: pid_t,
364 __unused: [4]u8,
365};
366
353/// Renamed to Stat to not conflict with the stat function.367/// Renamed to Stat to not conflict with the stat function.
354/// atime, mtime, and ctime have functions to return `timespec`,368/// atime, mtime, and ctime have functions to return `timespec`,
355/// because although this is a POSIX API, the layout and names of369/// because although this is a POSIX API, the layout and names of
lib/std/os/bits/linux/x86_64.zig+6-6
...@@ -460,12 +460,12 @@ pub const F_RDLCK = 0;...@@ -460,12 +460,12 @@ pub const F_RDLCK = 0;
460pub const F_WRLCK = 1;460pub const F_WRLCK = 1;
461pub const F_UNLCK = 2;461pub const F_UNLCK = 2;
462462
463pub const flock = extern struct {463pub const Flock = extern struct {
464 lock_type: i16,464 l_type: i16,
465 whence: i16,465 l_whence: i16,
466 start: off_t,466 l_start: off_t,
467 len: off_t,467 l_len: off_t,
468 pid: pid_t,468 l_pid: pid_t,
469};469};
470470
471pub const msghdr = extern struct {471pub const msghdr = extern struct {
lib/std/os/bits/netbsd.zig+12
...@@ -22,6 +22,14 @@ pub const dl_phdr_info = extern struct {...@@ -22,6 +22,14 @@ pub const dl_phdr_info = extern struct {
22 dlpi_phnum: u16,22 dlpi_phnum: u16,
23};23};
2424
25pub const Flock = extern struct {
26 start: off_t,
27 len: off_t,
28 pid: pid_t,
29 lock_type: i16,
30 whence: i16,
31};
32
25pub const msghdr = extern struct {33pub const msghdr = extern struct {
26 /// optional address34 /// optional address
27 msg_name: ?*sockaddr,35 msg_name: ?*sockaddr,
...@@ -312,6 +320,10 @@ pub const F_GETLK = 7;...@@ -312,6 +320,10 @@ pub const F_GETLK = 7;
312pub const F_SETLK = 8;320pub const F_SETLK = 8;
313pub const F_SETLKW = 9;321pub const F_SETLKW = 9;
314322
323pub const F_RDLCK = 1;
324pub const F_WRLCK = 3;
325pub const F_UNLCK = 2;
326
315pub const SEEK_SET = 0;327pub const SEEK_SET = 0;
316pub const SEEK_CUR = 1;328pub const SEEK_CUR = 1;
317pub const SEEK_END = 2;329pub const SEEK_END = 2;