authorgravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2018-08-29 17:36:18-07:00
committergravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2018-09-08 03:52:28+00:00
log4a8c992ef1e550d969ef1b26a460327fa188451c
tree03f53becbaca68d559337d7c350f9823a9e5dc7e
parentd956d30167665ae935ab123b4183f9aead79fa33

os: use less syscalls

these don't exist on new platforms (such as arm64) also switch from the deprecated dirent to dirent64

3 files changed, 94 insertions(+), 35 deletions(-)

std/os/index.zig+3-4
......@@ -1641,7 +1641,7 @@ pub const Dir = struct {
16411641 }
16421642
16431643 while (true) {
1644 const result = posix.getdents(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len);
1644 const result = posix.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len);
16451645 const err = posix.getErrno(result);
16461646 if (err > 0) {
16471647 switch (err) {
......@@ -1659,7 +1659,7 @@ pub const Dir = struct {
16591659 break;
16601660 }
16611661 }
1662 const linux_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]);
1662 const linux_entry = @ptrCast(*align(1) posix.dirent64, &self.handle.buf[self.handle.index]);
16631663 const next_index = self.handle.index + linux_entry.d_reclen;
16641664 self.handle.index = next_index;
16651665
......@@ -1670,8 +1670,7 @@ pub const Dir = struct {
16701670 continue :start_over;
16711671 }
16721672
1673 const type_char = self.handle.buf[next_index - 1];
1674 const entry_kind = switch (type_char) {
1673 const entry_kind = switch (linux_entry.d_type) {
16751674 posix.DT_BLK => Entry.Kind.BlockDevice,
16761675 posix.DT_CHR => Entry.Kind.CharacterDevice,
16771676 posix.DT_DIR => Entry.Kind.Directory,
std/os/linux/index.zig+89-24
......@@ -665,6 +665,13 @@ pub const F_GETOWN_EX = 16;
665665
666666pub const F_GETOWNER_UIDS = 17;
667667
668pub const AT_FDCWD = -100;
669pub const AT_SYMLINK_NOFOLLOW = 0x100;
670pub const AT_REMOVEDIR = 0x200;
671pub const AT_SYMLINK_FOLLOW = 0x400;
672pub const AT_NO_AUTOMOUNT = 0x800;
673pub const AT_EMPTY_PATH = 0x1000;
674
668675pub fn S_ISREG(m: u32) bool {
669676 return m & S_IFMT == S_IFREG;
670677}
......@@ -738,7 +745,11 @@ pub fn getErrno(r: usize) usize {
738745}
739746
740747pub fn dup2(old: i32, new: i32) usize {
741 return syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
748 return dup3(old, new, 0);
749}
750
751pub fn dup3(old: i32, new: i32, flags: u32) usize {
752 return syscall3(SYS_dup3, @intCast(usize, old), @intCast(usize, new), flags);
742753}
743754
744755// TODO https://github.com/ziglang/zig/issues/265
......@@ -757,7 +768,7 @@ pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*
757768}
758769
759770pub fn fork() usize {
760 return syscall0(SYS_fork);
771 return clone2(SIGCHLD, 0);
761772}
762773
763774pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?*timespec) usize {
......@@ -772,8 +783,8 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
772783 return syscall2(SYS_getcwd, @ptrToInt(buf), size);
773784}
774785
775pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
776 return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
786pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
787 return syscall3(SYS_getdents64, @intCast(usize, fd), @ptrToInt(dirp), count);
777788}
778789
779790pub fn inotify_init1(flags: u32) usize {
......@@ -795,16 +806,26 @@ pub fn isatty(fd: i32) bool {
795806
796807// TODO https://github.com/ziglang/zig/issues/265
797808pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
798 return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
809 return readlinkat(AT_FDCWD, path, buf_ptr, buf_len);
810}
811
812// TODO https://github.com/ziglang/zig/issues/265
813pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
814 return syscall4(SYS_readlinkat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
799815}
800816
801817// TODO https://github.com/ziglang/zig/issues/265
802818pub fn mkdir(path: [*]const u8, mode: u32) usize {
803 return syscall2(SYS_mkdir, @ptrToInt(path), mode);
819 return mkdirat(AT_FDCWD, path, mode);
820}
821
822// TODO https://github.com/ziglang/zig/issues/265
823pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize {
824 return syscall3(SYS_mkdirat, @intCast(usize, dirfd), @ptrToInt(path), mode);
804825}
805826
806827// TODO https://github.com/ziglang/zig/issues/265
807pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: usize, data: usize) usize {
828pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize {
808829 return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
809830}
810831
......@@ -840,28 +861,38 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us
840861
841862// TODO https://github.com/ziglang/zig/issues/265
842863pub fn rmdir(path: [*]const u8) usize {
843 return syscall1(SYS_rmdir, @ptrToInt(path));
864 return unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
844865}
845866
846867// TODO https://github.com/ziglang/zig/issues/265
847868pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
848 return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
869 return symlinkat(existing, AT_FDCWD, new);
849870}
850871
872// TODO https://github.com/ziglang/zig/issues/265
873pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
874 return syscall3(SYS_symlinkat, @ptrToInt(existing), @intCast(usize, newfd), @ptrToInt(newpath));
875}
876
877// TODO https://github.com/ziglang/zig/issues/265
851878pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
852879 return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
853880}
854881
855882// TODO https://github.com/ziglang/zig/issues/265
856883pub fn access(path: [*]const u8, mode: u32) usize {
857 return syscall2(SYS_access, @ptrToInt(path), mode);
884 return faccessat(AT_FDCWD, path, mode);
885}
886
887pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize {
888 return syscall3(SYS_faccessat, @intCast(usize, dirfd), @ptrToInt(path), mode);
858889}
859890
860891pub fn pipe(fd: *[2]i32) usize {
861892 return pipe2(fd, 0);
862893}
863894
864pub fn pipe2(fd: *[2]i32, flags: usize) usize {
895pub fn pipe2(fd: *[2]i32, flags: u32) usize {
865896 return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
866897}
867898
......@@ -875,12 +906,17 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
875906
876907// TODO https://github.com/ziglang/zig/issues/265
877908pub fn rename(old: [*]const u8, new: [*]const u8) usize {
878 return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));
909 return renameat2(AT_FDCWD, old, AT_FDCWD, new, 0);
910}
911
912// TODO https://github.com/ziglang/zig/issues/265
913pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize {
914 return syscall5(SYS_renameat2, @intCast(usize, oldfd), @ptrToInt(oldpath), @intCast(usize, newfd), @ptrToInt(newpath), flags);
879915}
880916
881917// TODO https://github.com/ziglang/zig/issues/265
882918pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {
883 return syscall3(SYS_open, @ptrToInt(path), flags, perm);
919 return openat(AT_FDCWD, path, flags, perm);
884920}
885921
886922// TODO https://github.com/ziglang/zig/issues/265
......@@ -889,7 +925,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
889925}
890926
891927// TODO https://github.com/ziglang/zig/issues/265
892pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
928pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize {
893929 return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
894930}
895931
......@@ -899,7 +935,7 @@ pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid:
899935}
900936
901937/// See also `clone` (from the arch-specific include)
902pub fn clone2(flags: usize, child_stack_ptr: usize) usize {
938pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
903939 return syscall2(SYS_clone, flags, child_stack_ptr);
904940}
905941
......@@ -917,7 +953,7 @@ pub fn exit(status: i32) noreturn {
917953}
918954
919955pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
920 return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
956 return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags);
921957}
922958
923959pub fn kill(pid: i32, sig: i32) usize {
......@@ -926,7 +962,12 @@ pub fn kill(pid: i32, sig: i32) usize {
926962
927963// TODO https://github.com/ziglang/zig/issues/265
928964pub fn unlink(path: [*]const u8) usize {
929 return syscall1(SYS_unlink, @ptrToInt(path));
965 return unlinkat(AT_FDCWD, path, 0);
966}
967
968// TODO https://github.com/ziglang/zig/issues/265
969pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
970 return syscall3(SYS_unlinkat, @intCast(usize, dirfd), @ptrToInt(path), flags);
930971}
931972
932973pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
......@@ -1230,17 +1271,22 @@ pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags:
12301271}
12311272
12321273pub fn fstat(fd: i32, stat_buf: *Stat) usize {
1233 return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
1274 return fstatat(fd, c"", stat_buf, AT_EMPTY_PATH);
12341275}
12351276
12361277// TODO https://github.com/ziglang/zig/issues/265
12371278pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize {
1238 return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf));
1279 return fstatat(AT_FDCWD, pathname, statbuf, AT_NO_AUTOMOUNT);
12391280}
12401281
12411282// TODO https://github.com/ziglang/zig/issues/265
12421283pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {
1243 return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf));
1284 return fstatat(AF_FDCWD, pathname, statbuf, AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT);
1285}
1286
1287// TODO https://github.com/ziglang/zig/issues/265
1288pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize {
1289 return syscall4(SYS_fstatat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(stat_buf), flags);
12441290}
12451291
12461292// TODO https://github.com/ziglang/zig/issues/265
......@@ -1331,7 +1377,18 @@ pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
13311377}
13321378
13331379pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
1334 return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
1380 return epoll_pwait(epoll_fd, events, maxevents, timeout, null);
1381}
1382
1383pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
1384 return syscall6(SYS_epoll_pwait,
1385 @intCast(usize, epoll_fd),
1386 @ptrToInt(events),
1387 @intCast(usize, maxevents),
1388 @intCast(usize, timeout),
1389 @ptrToInt(sigmask),
1390 @sizeOf(sigset_t)
1391 );
13351392}
13361393
13371394pub fn eventfd(count: u32, flags: u32) usize {
......@@ -1339,7 +1396,7 @@ pub fn eventfd(count: u32, flags: u32) usize {
13391396}
13401397
13411398pub fn timerfd_create(clockid: i32, flags: u32) usize {
1342 return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
1399 return syscall2(SYS_timerfd_create, @intCast(usize, clockid), flags);
13431400}
13441401
13451402pub const itimerspec = extern struct {
......@@ -1352,7 +1409,7 @@ pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
13521409}
13531410
13541411pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
1355 return syscall4(SYS_timerfd_settime, @intCast(usize, fd), @intCast(usize, flags), @ptrToInt(new_value), @ptrToInt(old_value));
1412 return syscall4(SYS_timerfd_settime, @intCast(usize, fd), flags, @ptrToInt(new_value), @ptrToInt(old_value));
13561413}
13571414
13581415pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
......@@ -1462,7 +1519,7 @@ pub const cap_user_data_t = extern struct {
14621519};
14631520
14641521pub fn unshare(flags: usize) usize {
1465 return syscall1(SYS_unshare, @intCast(usize, flags));
1522 return syscall1(SYS_unshare, flags);
14661523}
14671524
14681525pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
......@@ -1481,6 +1538,14 @@ pub const inotify_event = extern struct {
14811538 //name: [?]u8,
14821539};
14831540
1541pub const dirent64 = extern struct {
1542 d_ino: u64,
1543 d_off: u64,
1544 d_reclen: u16,
1545 d_type: u8,
1546 d_name: u8, // field address is the address of first byte of name https://github.com/ziglang/zig/issues/173
1547};
1548
14841549test "import" {
14851550 if (builtin.os == builtin.Os.linux) {
14861551 _ = @import("test.zig");
std/os/linux/x86_64.zig+2-7
......@@ -266,6 +266,8 @@ pub const SYS_mknodat = 259;
266266pub const SYS_fchownat = 260;
267267pub const SYS_futimesat = 261;
268268pub const SYS_newfstatat = 262;
269// https://github.com/ziglang/zig/issues/1439
270pub const SYS_fstatat = 262;
269271pub const SYS_unlinkat = 263;
270272pub const SYS_renameat = 264;
271273pub const SYS_linkat = 265;
......@@ -480,11 +482,4 @@ pub const timezone = extern struct {
480482 tz_dsttime: i32,
481483};
482484
483pub const dirent = extern struct {
484 d_ino: usize,
485 d_off: usize,
486 d_reclen: u16,
487 d_name: u8, // field address is the address of first byte of name
488};
489
490485pub const Elf_Symndx = u32;