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 {...@@ -1641,7 +1641,7 @@ pub const Dir = struct {
1641 }1641 }
16421642
1643 while (true) {1643 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);
1645 const err = posix.getErrno(result);1645 const err = posix.getErrno(result);
1646 if (err > 0) {1646 if (err > 0) {
1647 switch (err) {1647 switch (err) {
...@@ -1659,7 +1659,7 @@ pub const Dir = struct {...@@ -1659,7 +1659,7 @@ pub const Dir = struct {
1659 break;1659 break;
1660 }1660 }
1661 }1661 }
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]);
1663 const next_index = self.handle.index + linux_entry.d_reclen;1663 const next_index = self.handle.index + linux_entry.d_reclen;
1664 self.handle.index = next_index;1664 self.handle.index = next_index;
16651665
...@@ -1670,8 +1670,7 @@ pub const Dir = struct {...@@ -1670,8 +1670,7 @@ pub const Dir = struct {
1670 continue :start_over;1670 continue :start_over;
1671 }1671 }
16721672
1673 const type_char = self.handle.buf[next_index - 1];1673 const entry_kind = switch (linux_entry.d_type) {
1674 const entry_kind = switch (type_char) {
1675 posix.DT_BLK => Entry.Kind.BlockDevice,1674 posix.DT_BLK => Entry.Kind.BlockDevice,
1676 posix.DT_CHR => Entry.Kind.CharacterDevice,1675 posix.DT_CHR => Entry.Kind.CharacterDevice,
1677 posix.DT_DIR => Entry.Kind.Directory,1676 posix.DT_DIR => Entry.Kind.Directory,
std/os/linux/index.zig+89-24
...@@ -665,6 +665,13 @@ pub const F_GETOWN_EX = 16;...@@ -665,6 +665,13 @@ pub const F_GETOWN_EX = 16;
665665
666pub const F_GETOWNER_UIDS = 17;666pub 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
668pub fn S_ISREG(m: u32) bool {675pub fn S_ISREG(m: u32) bool {
669 return m & S_IFMT == S_IFREG;676 return m & S_IFMT == S_IFREG;
670}677}
...@@ -738,7 +745,11 @@ pub fn getErrno(r: usize) usize {...@@ -738,7 +745,11 @@ pub fn getErrno(r: usize) usize {
738}745}
739746
740pub fn dup2(old: i32, new: i32) usize {747pub 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);
742}753}
743754
744// TODO https://github.com/ziglang/zig/issues/265755// TODO https://github.com/ziglang/zig/issues/265
...@@ -757,7 +768,7 @@ pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*...@@ -757,7 +768,7 @@ pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*
757}768}
758769
759pub fn fork() usize {770pub fn fork() usize {
760 return syscall0(SYS_fork);771 return clone2(SIGCHLD, 0);
761}772}
762773
763pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?*timespec) usize {774pub 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 {...@@ -772,8 +783,8 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
772 return syscall2(SYS_getcwd, @ptrToInt(buf), size);783 return syscall2(SYS_getcwd, @ptrToInt(buf), size);
773}784}
774785
775pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {786pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
776 return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);787 return syscall3(SYS_getdents64, @intCast(usize, fd), @ptrToInt(dirp), count);
777}788}
778789
779pub fn inotify_init1(flags: u32) usize {790pub fn inotify_init1(flags: u32) usize {
...@@ -795,16 +806,26 @@ pub fn isatty(fd: i32) bool {...@@ -795,16 +806,26 @@ pub fn isatty(fd: i32) bool {
795806
796// TODO https://github.com/ziglang/zig/issues/265807// TODO https://github.com/ziglang/zig/issues/265
797pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {808pub 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);
799}815}
800816
801// TODO https://github.com/ziglang/zig/issues/265817// TODO https://github.com/ziglang/zig/issues/265
802pub fn mkdir(path: [*]const u8, mode: u32) usize {818pub 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);
804}825}
805826
806// TODO https://github.com/ziglang/zig/issues/265827// 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 {
808 return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);829 return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
809}830}
810831
...@@ -840,28 +861,38 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us...@@ -840,28 +861,38 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us
840861
841// TODO https://github.com/ziglang/zig/issues/265862// TODO https://github.com/ziglang/zig/issues/265
842pub fn rmdir(path: [*]const u8) usize {863pub fn rmdir(path: [*]const u8) usize {
843 return syscall1(SYS_rmdir, @ptrToInt(path));864 return unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
844}865}
845866
846// TODO https://github.com/ziglang/zig/issues/265867// TODO https://github.com/ziglang/zig/issues/265
847pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {868pub 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);
849}870}
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
851pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {878pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
852 return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);879 return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
853}880}
854881
855// TODO https://github.com/ziglang/zig/issues/265882// TODO https://github.com/ziglang/zig/issues/265
856pub fn access(path: [*]const u8, mode: u32) usize {883pub 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);
858}889}
859890
860pub fn pipe(fd: *[2]i32) usize {891pub fn pipe(fd: *[2]i32) usize {
861 return pipe2(fd, 0);892 return pipe2(fd, 0);
862}893}
863894
864pub fn pipe2(fd: *[2]i32, flags: usize) usize {895pub fn pipe2(fd: *[2]i32, flags: u32) usize {
865 return syscall2(SYS_pipe2, @ptrToInt(fd), flags);896 return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
866}897}
867898
...@@ -875,12 +906,17 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {...@@ -875,12 +906,17 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
875906
876// TODO https://github.com/ziglang/zig/issues/265907// TODO https://github.com/ziglang/zig/issues/265
877pub fn rename(old: [*]const u8, new: [*]const u8) usize {908pub 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);
879}915}
880916
881// TODO https://github.com/ziglang/zig/issues/265917// TODO https://github.com/ziglang/zig/issues/265
882pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {918pub 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);
884}920}
885921
886// TODO https://github.com/ziglang/zig/issues/265922// TODO https://github.com/ziglang/zig/issues/265
...@@ -889,7 +925,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {...@@ -889,7 +925,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
889}925}
890926
891// TODO https://github.com/ziglang/zig/issues/265927// 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 {
893 return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);929 return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
894}930}
895931
...@@ -899,7 +935,7 @@ pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid:...@@ -899,7 +935,7 @@ pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid:
899}935}
900936
901/// See also `clone` (from the arch-specific include)937/// 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 {
903 return syscall2(SYS_clone, flags, child_stack_ptr);939 return syscall2(SYS_clone, flags, child_stack_ptr);
904}940}
905941
...@@ -917,7 +953,7 @@ pub fn exit(status: i32) noreturn {...@@ -917,7 +953,7 @@ pub fn exit(status: i32) noreturn {
917}953}
918954
919pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {955pub 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);
921}957}
922958
923pub fn kill(pid: i32, sig: i32) usize {959pub fn kill(pid: i32, sig: i32) usize {
...@@ -926,7 +962,12 @@ pub fn kill(pid: i32, sig: i32) usize {...@@ -926,7 +962,12 @@ pub fn kill(pid: i32, sig: i32) usize {
926962
927// TODO https://github.com/ziglang/zig/issues/265963// TODO https://github.com/ziglang/zig/issues/265
928pub fn unlink(path: [*]const u8) usize {964pub 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);
930}971}
931972
932pub fn waitpid(pid: i32, status: *i32, options: i32) usize {973pub 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:...@@ -1230,17 +1271,22 @@ pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags:
1230}1271}
12311272
1232pub fn fstat(fd: i32, stat_buf: *Stat) usize {1273pub 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);
1234}1275}
12351276
1236// TODO https://github.com/ziglang/zig/issues/2651277// TODO https://github.com/ziglang/zig/issues/265
1237pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize {1278pub 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);
1239}1280}
12401281
1241// TODO https://github.com/ziglang/zig/issues/2651282// TODO https://github.com/ziglang/zig/issues/265
1242pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {1283pub 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);
1244}1290}
12451291
1246// TODO https://github.com/ziglang/zig/issues/2651292// 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 {...@@ -1331,7 +1377,18 @@ pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
1331}1377}
13321378
1333pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {1379pub 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 );
1335}1392}
13361393
1337pub fn eventfd(count: u32, flags: u32) usize {1394pub fn eventfd(count: u32, flags: u32) usize {
...@@ -1339,7 +1396,7 @@ pub fn eventfd(count: u32, flags: u32) usize {...@@ -1339,7 +1396,7 @@ pub fn eventfd(count: u32, flags: u32) usize {
1339}1396}
13401397
1341pub fn timerfd_create(clockid: i32, flags: u32) usize {1398pub 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);
1343}1400}
13441401
1345pub const itimerspec = extern struct {1402pub const itimerspec = extern struct {
...@@ -1352,7 +1409,7 @@ pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {...@@ -1352,7 +1409,7 @@ pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
1352}1409}
13531410
1354pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {1411pub 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));
1356}1413}
13571414
1358pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;1415pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
...@@ -1462,7 +1519,7 @@ pub const cap_user_data_t = extern struct {...@@ -1462,7 +1519,7 @@ pub const cap_user_data_t = extern struct {
1462};1519};
14631520
1464pub fn unshare(flags: usize) usize {1521pub fn unshare(flags: usize) usize {
1465 return syscall1(SYS_unshare, @intCast(usize, flags));1522 return syscall1(SYS_unshare, flags);
1466}1523}
14671524
1468pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {1525pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
...@@ -1481,6 +1538,14 @@ pub const inotify_event = extern struct {...@@ -1481,6 +1538,14 @@ pub const inotify_event = extern struct {
1481 //name: [?]u8,1538 //name: [?]u8,
1482};1539};
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
1484test "import" {1549test "import" {
1485 if (builtin.os == builtin.Os.linux) {1550 if (builtin.os == builtin.Os.linux) {
1486 _ = @import("test.zig");1551 _ = @import("test.zig");
std/os/linux/x86_64.zig+2-7
...@@ -266,6 +266,8 @@ pub const SYS_mknodat = 259;...@@ -266,6 +266,8 @@ pub const SYS_mknodat = 259;
266pub const SYS_fchownat = 260;266pub const SYS_fchownat = 260;
267pub const SYS_futimesat = 261;267pub const SYS_futimesat = 261;
268pub const SYS_newfstatat = 262;268pub const SYS_newfstatat = 262;
269// https://github.com/ziglang/zig/issues/1439
270pub const SYS_fstatat = 262;
269pub const SYS_unlinkat = 263;271pub const SYS_unlinkat = 263;
270pub const SYS_renameat = 264;272pub const SYS_renameat = 264;
271pub const SYS_linkat = 265;273pub const SYS_linkat = 265;
...@@ -480,11 +482,4 @@ pub const timezone = extern struct {...@@ -480,11 +482,4 @@ pub const timezone = extern struct {
480 tz_dsttime: i32,482 tz_dsttime: i32,
481};483};
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
490pub const Elf_Symndx = u32;485pub const Elf_Symndx = u32;