authorgravatar for i@mgxm.meMarcio Giaxa <i@mgxm.me> 2018-12-18 01:32:35-02:00
committergravatar for i@mgxm.meMarcio Giaxa <i@mgxm.me> 2018-12-19 18:42:00-02:00
log11ced4f99d95ec144d9dbb79ead335b571f714fe
tree2ca9671ed971a32728d7897d9d7f564b052978f7
parent6cfcdbde2b902476863cb912190f20ce5e02277c

freebsd: use libc interface instead system calls

Remove all syscalls references * dup2() * chdir() * execve() * fork() * getcwd() * isatty() * readlink() * mkdir() * mmap() * munmap() * read() * rmdir() * symlink() * pread() * write() * pwrite() * rename() * open() * close() * lseek() * exit() * unlink() * waitpid() * nanosleep() * setreuid() * setregid() * raise() * fstat() * pipe() * added pipe2() extern c fn

2 files changed, 57 insertions(+), 59 deletions(-)

std/c/freebsd.zig+2
...@@ -14,6 +14,8 @@ pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlen...@@ -14,6 +14,8 @@ pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlen
14pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;14pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
15pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;15pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
16pub extern "c" fn getdirentries(fd: c_int, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize;16pub extern "c" fn getdirentries(fd: c_int, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize;
17pub extern "c" fn pipe2(arg0: *[2]c_int, arg1: u32) c_int;
18pub extern "c" fn getrandom(buf: [*]u8, count: usize, flags: u32) c_int;
1719
18/// Renamed from `kevent` to `Kevent` to avoid conflict with function name.20/// Renamed from `kevent` to `Kevent` to avoid conflict with function name.
19pub const Kevent = extern struct {21pub const Kevent = extern struct {
std/os/freebsd/index.zig+55-59
...@@ -1,14 +1,11 @@...@@ -1,14 +1,11 @@
1const assert = @import("../debug.zig").assert;
2const builtin = @import("builtin");1const builtin = @import("builtin");
3const arch = switch (builtin.arch) {2
4 builtin.Arch.x86_64 => @import("x86_64.zig"),
5 else => @compileError("unsupported arch"),
6};
7pub use @import("syscall.zig");
8pub use @import("errno.zig");3pub use @import("errno.zig");
94
10const std = @import("../../index.zig");5const std = @import("../../index.zig");
11const c = std.c;6const c = std.c;
7
8const assert = std.debug.assert;
12const maxInt = std.math.maxInt;9const maxInt = std.math.maxInt;
13pub const Kevent = c.Kevent;10pub const Kevent = c.Kevent;
1411
...@@ -546,19 +543,19 @@ pub fn getErrno(r: usize) usize {...@@ -546,19 +543,19 @@ pub fn getErrno(r: usize) usize {
546}543}
547544
548pub fn dup2(old: i32, new: i32) usize {545pub fn dup2(old: i32, new: i32) usize {
549 return arch.syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)));546 return errnoWrap(c.dup2(old, new));
550}547}
551548
552pub fn chdir(path: [*]const u8) usize {549pub fn chdir(path: [*]const u8) usize {
553 return arch.syscall1(SYS_chdir, @ptrToInt(path));550 return errnoWrap(c.chdir(path));
554}551}
555552
556pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize {553pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize {
557 return arch.syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));554 return errnoWrap(c.execve(path, argv, envp));
558}555}
559556
560pub fn fork() usize {557pub fn fork() usize {
561 return arch.syscall0(SYS_fork);558 return errnoWrap(c.fork());
562}559}
563560
564pub fn access(path: [*]const u8, mode: u32) usize {561pub fn access(path: [*]const u8, mode: u32) usize {
...@@ -566,7 +563,7 @@ pub fn access(path: [*]const u8, mode: u32) usize {...@@ -566,7 +563,7 @@ pub fn access(path: [*]const u8, mode: u32) usize {
566}563}
567564
568pub fn getcwd(buf: [*]u8, size: usize) usize {565pub fn getcwd(buf: [*]u8, size: usize) usize {
569 return arch.syscall2(SYS___getcwd, @ptrToInt(buf), size);566 return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
570}567}
571568
572pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {569pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
...@@ -578,40 +575,48 @@ pub fn getdirentries(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize...@@ -578,40 +575,48 @@ pub fn getdirentries(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize
578}575}
579576
580pub fn isatty(fd: i32) bool {577pub fn isatty(fd: i32) bool {
581 var wsz: winsize = undefined;578 return c.isatty(fd) != 0;
582 return arch.syscall3(SYS_ioctl, @bitCast(usize, isize(fd)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
583}579}
584580
585pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {581pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
586 return arch.syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);582 return errnoWrap(c.readlink(path, buf_ptr, buf_len));
587}583}
588584
589pub fn mkdir(path: [*]const u8, mode: u32) usize {585pub fn mkdir(path: [*]const u8, mode: u32) usize {
590 return arch.syscall2(SYS_mkdir, @ptrToInt(path), mode);586 return errnoWrap(c.mkdir(path, mode));
591}587}
592588
593pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {589pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
594 return arch.syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));590 const ptr_result = c.mmap(
591 @ptrCast(*c_void, address),
592 length,
593 @bitCast(c_int, @intCast(c_uint, prot)),
594 @bitCast(c_int, c_uint(flags)),
595 fd,
596 offset,
597 );
598 const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
599 return errnoWrap(isize_result);
595}600}
596601
597pub fn munmap(address: usize, length: usize) usize {602pub fn munmap(address: usize, length: usize) usize {
598 return arch.syscall2(SYS_munmap, address, length);603 return errnoWrap(c.munmap(@intToPtr(*c_void, address), length));
599}604}
600605
601pub fn read(fd: i32, buf: [*]u8, count: usize) usize {606pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize {
602 return arch.syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);607 return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte));
603}608}
604609
605pub fn rmdir(path: [*]const u8) usize {610pub fn rmdir(path: [*]const u8) usize {
606 return arch.syscall1(SYS_rmdir, @ptrToInt(path));611 return errnoWrap(c.rmdir(path));
607}612}
608613
609pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {614pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
610 return arch.syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));615 return errnoWrap(c.symlink(existing, new));
611}616}
612617
613pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {618pub fn pread(fd: i32, buf: [*]u8, nbyte: usize, offset: u64) usize {
614 return arch.syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);619 return errnoWrap(c.pread(fd, @ptrCast(*c_void, buf), nbyte, offset));
615}620}
616621
617pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize {622pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize {
...@@ -622,16 +627,17 @@ pub fn pipe(fd: *[2]i32) usize {...@@ -622,16 +627,17 @@ pub fn pipe(fd: *[2]i32) usize {
622 return pipe2(fd, 0);627 return pipe2(fd, 0);
623}628}
624629
625pub fn pipe2(fd: *[2]i32, flags: usize) usize {630pub fn pipe2(fd: *[2]i32, flags: u32) usize {
626 return arch.syscall2(SYS_pipe2, @ptrToInt(fd), flags);631 comptime assert(i32.bit_count == c_int.bit_count);
632 return errnoWrap(c.pipe2(@ptrCast(*[2]c_int, fd), flags));
627}633}
628634
629pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {635pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
630 return arch.syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);636 return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte));
631}637}
632638
633pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {639pub fn pwrite(fd: i32, buf: [*]const u8, nbyte: usize, offset: u64) usize {
634 return arch.syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);640 return errnoWrap(c.pwrite(fd, @ptrCast(*const c_void, buf), nbyte, offset));
635}641}
636642
637pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize {643pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize {
...@@ -639,11 +645,11 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize)...@@ -639,11 +645,11 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize)
639}645}
640646
641pub fn rename(old: [*]const u8, new: [*]const u8) usize {647pub fn rename(old: [*]const u8, new: [*]const u8) usize {
642 return arch.syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));648 return errnoWrap(c.rename(old, new));
643}649}
644650
645pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {651pub fn open(path: [*]const u8, flags: u32, mode: usize) usize {
646 return arch.syscall3(SYS_open, @ptrToInt(path), flags, perm);652 return errnoWrap(c.open(path, @bitCast(c_int, flags), mode));
647}653}
648654
649pub fn create(path: [*]const u8, perm: usize) usize {655pub fn create(path: [*]const u8, perm: usize) usize {
...@@ -655,20 +661,19 @@ pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {...@@ -655,20 +661,19 @@ pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
655}661}
656662
657pub fn close(fd: i32) usize {663pub fn close(fd: i32) usize {
658 return arch.syscall1(SYS_close, @bitCast(usize, isize(fd)));664 return errnoWrap(c.close(fd));
659}665}
660666
661pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {667pub fn lseek(fd: i32, offset: isize, whence: c_int) usize {
662 return arch.syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), ref_pos);668 return errnoWrap(c.lseek(fd, offset, whence));
663}669}
664670
665pub fn exit(status: i32) noreturn {671pub fn exit(code: i32) noreturn {
666 _ = arch.syscall1(SYS_exit, @bitCast(usize, isize(status)));672 c.exit(code);
667 unreachable;
668}673}
669674
670pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {675pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
671 return arch.syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));676 return errnoWrap(c.getrandom(buf, count, flags));
672}677}
673678
674pub fn kill(pid: i32, sig: i32) usize {679pub fn kill(pid: i32, sig: i32) usize {
...@@ -676,15 +681,16 @@ pub fn kill(pid: i32, sig: i32) usize {...@@ -676,15 +681,16 @@ pub fn kill(pid: i32, sig: i32) usize {
676}681}
677682
678pub fn unlink(path: [*]const u8) usize {683pub fn unlink(path: [*]const u8) usize {
679 return arch.syscall1(SYS_unlink, @ptrToInt(path));684 return errnoWrap(c.unlink(path));
680}685}
681686
682pub fn waitpid(pid: i32, status: *i32, options: i32) usize {687pub fn waitpid(pid: i32, status: *i32, options: u32) usize {
683 return arch.syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);688 comptime assert(i32.bit_count == c_int.bit_count);
689 return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options)));
684}690}
685691
686pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {692pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
687 return arch.syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));693 return errnoWrap(c.nanosleep(req, rem));
688}694}
689695
690pub fn setuid(uid: u32) usize {696pub fn setuid(uid: u32) usize {
...@@ -696,11 +702,11 @@ pub fn setgid(gid: u32) usize {...@@ -696,11 +702,11 @@ pub fn setgid(gid: u32) usize {
696}702}
697703
698pub fn setreuid(ruid: u32, euid: u32) usize {704pub fn setreuid(ruid: u32, euid: u32) usize {
699 return arch.syscall2(SYS_setreuid, ruid, euid);705 return errnoWrap(c.setreuid(ruid, euid));
700}706}
701707
702pub fn setregid(rgid: u32, egid: u32) usize {708pub fn setregid(rgid: u32, egid: u32) usize {
703 return arch.syscall2(SYS_setregid, rgid, egid);709 return errnoWrap(c.setregid(rgid, egid));
704}710}
705711
706const NSIG = 32;712const NSIG = 32;
...@@ -745,26 +751,16 @@ pub const sigset_t = extern struct {...@@ -745,26 +751,16 @@ pub const sigset_t = extern struct {
745};751};
746752
747pub fn raise(sig: i32) usize {753pub fn raise(sig: i32) usize {
748 // TODO have a chat with the freebsd folks and make sure there's no bug in754 return errnoWrap(c.raise(sig));
749 // their libc. musl-libc blocks signals in between these calls because
750 // if a signal handler runs and forks between the gettid and sending the
751 // signal, the parent will get 2 signals, one from itself and one from the child
752 // if the protection does not belong here, then it belongs in abort(),
753 // like it does in freebsd's libc.
754 var id: usize = undefined;
755 const rc = arch.syscall1(SYS_thr_self, @ptrToInt(&id));
756 if (getErrno(rc) != 0) return rc;
757 return arch.syscall2(SYS_thr_kill, id, @bitCast(usize, isize(sig)));
758}755}
759756
760pub const Stat = c.Stat;757pub const Stat = c.Stat;
761pub const dirent = c.dirent;758pub const dirent = c.dirent;
762pub const timespec = c.timespec;759pub const timespec = c.timespec;
763760
764pub fn fstat(fd: i32, stat_buf: *Stat) usize {761pub fn fstat(fd: i32, buf: *c.Stat) usize {
765 return arch.syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));762 return errnoWrap(c.fstat(fd, buf));
766}763}
767
768pub const iovec = extern struct {764pub const iovec = extern struct {
769 iov_base: [*]u8,765 iov_base: [*]u8,
770 iov_len: usize,766 iov_len: usize,