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 {
681681 var locked = false;
682682 if (flags.lock) {
683683 // TODO: integrate async I/O
684 try os.fcntlFlockBlocking(fd, &os.flock{
685 .lock_type = if (flags.write) os.F_WRLCK else os.F_RDLCK,
686 .whence = os.SEEK_SET,
687 .start = 0,
688 .len = 0,
689 .pid = 0,
690 });
684 // mem.zeroes is used here because flock's structure can vary across architectures and systems
685 var flock = mem.zeroes(os.Flock);
686 flock.l_type = if (flags.write) os.F_WRLCK else os.F_RDLCK;
687 flock.l_whence = os.SEEK_SET;
688 flock.l_start = 0;
689 flock.l_len = 0;
690 flock.l_pid = 0;
691 try os.fcntlFlockBlocking(fd, &flock);
691692 locked = true;
692693 }
693694
lib/std/os.zig+1-1
......@@ -1141,7 +1141,7 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
11411141}
11421142
11431143/// 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 {
11451145 const rc = system.fcntl(fd, F_SETLKW, flock_p);
11461146 if (rc < 0) {
11471147 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 {
5555pub const off_t = i64;
5656pub 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
5866/// Renamed to Stat to not conflict with the stat function.
5967/// atime, mtime, and ctime have functions to return `timespec`,
6068/// 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 {
5151 dlpi_phnum: u16,
5252};
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
5464pub const msghdr = extern struct {
5565 /// optional address
5666 msg_name: ?*sockaddr,
......@@ -350,6 +360,10 @@ pub const F_GETLK = 5;
350360pub const F_SETLK = 6;
351361pub const F_SETLKW = 7;
352362
363pub const F_RDLCK = 1;
364pub const F_WRLCK = 3;
365pub const F_UNLCK = 2;
366
353367pub const F_SETOWN_EX = 15;
354368pub 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;
88const sigset_t = linux.sigset_t;
99const uid_t = linux.uid_t;
1010const gid_t = linux.gid_t;
11const pid_t = linux.pid_t;
1112
1213pub const SYS_restart_syscall = 0;
1314pub const SYS_exit = 1;
......@@ -446,6 +447,10 @@ pub const F_GETLK = 12;
446447pub const F_SETLK = 13;
447448pub const F_SETLKW = 14;
448449
450pub const F_RDLCK = 0;
451pub const F_WRLCK = 1;
452pub const F_UNLCK = 2;
453
449454pub const F_SETOWN_EX = 15;
450455pub const F_GETOWN_EX = 16;
451456
......@@ -493,6 +498,16 @@ pub const HWCAP_IDIV = HWCAP_IDIVA | HWCAP_IDIVT;
493498pub const HWCAP_LPAE = 1 << 20;
494499pub 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
496511pub const msghdr = extern struct {
497512 msg_name: ?*sockaddr,
498513 msg_namelen: socklen_t,
lib/std/os/bits/linux/arm64.zig+14
......@@ -8,6 +8,7 @@ const iovec = linux.iovec;
88const iovec_const = linux.iovec_const;
99const uid_t = linux.uid_t;
1010const gid_t = linux.gid_t;
11const pid_t = linux.pid_t;
1112const stack_t = linux.stack_t;
1213const sigset_t = linux.sigset_t;
1314
......@@ -339,6 +340,10 @@ pub const F_GETLK = 5;
339340pub const F_SETLK = 6;
340341pub const F_SETLKW = 7;
341342
343pub const F_RDLCK = 0;
344pub const F_WRLCK = 1;
345pub const F_UNLCK = 2;
346
342347pub const F_SETOWN_EX = 15;
343348pub const F_GETOWN_EX = 16;
344349
......@@ -362,6 +367,15 @@ pub const MAP_NORESERVE = 0x4000;
362367pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
363368pub 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
365379pub const msghdr = extern struct {
366380 msg_name: ?*sockaddr,
367381 msg_namelen: socklen_t,
lib/std/os/bits/linux/i386.zig+10-6
......@@ -472,6 +472,10 @@ pub const F_GETLK = 12;
472472pub const F_SETLK = 13;
473473pub const F_SETLKW = 14;
474474
475pub const F_RDLCK = 0;
476pub const F_WRLCK = 1;
477pub const F_UNLCK = 2;
478
475479pub const F_SETOWN_EX = 15;
476480pub const F_GETOWN_EX = 16;
477481
......@@ -489,12 +493,12 @@ pub const MMAP2_UNIT = 4096;
489493pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
490494pub const VDSO_CGT_VER = "LINUX_2.6";
491495
492pub const flock = extern struct {
493 lock_type: i16,
494 whence: i16,
495 start: off_t,
496 len: off_t,
497 pid: pid_t,
496pub const Flock = extern struct {
497 l_type: i16,
498 l_whence: i16,
499 l_start: off_t,
500 l_len: off_t,
501 l_pid: pid_t,
498502};
499503
500504pub const msghdr = extern struct {
lib/std/os/bits/linux/mipsel.zig+15
......@@ -5,6 +5,7 @@ const iovec = linux.iovec;
55const iovec_const = linux.iovec_const;
66const uid_t = linux.uid_t;
77const gid_t = linux.gid_t;
8const pid_t = linux.pid_t;
89
910pub const SYS_Linux = 4000;
1011pub const SYS_syscall = (SYS_Linux + 0);
......@@ -412,6 +413,10 @@ pub const F_GETLK = 33;
412413pub const F_SETLK = 34;
413414pub const F_SETLKW = 35;
414415
416pub const F_RDLCK = 0;
417pub const F_WRLCK = 1;
418pub const F_UNLCK = 2;
419
415420pub const F_SETOWN_EX = 15;
416421pub const F_GETOWN_EX = 16;
417422
......@@ -457,6 +462,16 @@ pub const SO_RCVBUFFORCE = 33;
457462pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
458463pub 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
460475pub const blksize_t = i32;
461476pub const nlink_t = u32;
462477pub const time_t = isize;
lib/std/os/bits/linux/riscv64.zig+14
......@@ -2,6 +2,7 @@
22const std = @import("../../../std.zig");
33const uid_t = std.os.linux.uid_t;
44const gid_t = std.os.linux.gid_t;
5const pid_t = std.os.linux.pid_t;
56
67pub const SYS_io_setup = 0;
78pub const SYS_io_destroy = 1;
......@@ -332,6 +333,10 @@ pub const F_GETOWN = 9;
332333pub const F_SETSIG = 10;
333334pub const F_GETSIG = 11;
334335
336pub const F_RDLCK = 0;
337pub const F_WRLCK = 1;
338pub const F_UNLCK = 2;
339
335340pub const F_SETOWN_EX = 15;
336341pub const F_GETOWN_EX = 16;
337342
......@@ -350,6 +355,15 @@ pub const timespec = extern struct {
350355 tv_nsec: isize,
351356};
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
353367/// Renamed to Stat to not conflict with the stat function.
354368/// atime, mtime, and ctime have functions to return `timespec`,
355369/// 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;
460460pub const F_WRLCK = 1;
461461pub const F_UNLCK = 2;
462462
463pub const flock = extern struct {
464 lock_type: i16,
465 whence: i16,
466 start: off_t,
467 len: off_t,
468 pid: pid_t,
463pub const Flock = extern struct {
464 l_type: i16,
465 l_whence: i16,
466 l_start: off_t,
467 l_len: off_t,
468 l_pid: pid_t,
469469};
470470
471471pub const msghdr = extern struct {
lib/std/os/bits/netbsd.zig+12
......@@ -22,6 +22,14 @@ pub const dl_phdr_info = extern struct {
2222 dlpi_phnum: u16,
2323};
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
2533pub const msghdr = extern struct {
2634 /// optional address
2735 msg_name: ?*sockaddr,
......@@ -312,6 +320,10 @@ pub const F_GETLK = 7;
312320pub const F_SETLK = 8;
313321pub const F_SETLKW = 9;
314322
323pub const F_RDLCK = 1;
324pub const F_WRLCK = 3;
325pub const F_UNLCK = 2;
326
315327pub const SEEK_SET = 0;
316328pub const SEEK_CUR = 1;
317329pub const SEEK_END = 2;