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
1414pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
1515pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
1616pub 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
1820/// Renamed from `kevent` to `Kevent` to avoid conflict with function name.
1921pub const Kevent = extern struct {
std/os/freebsd/index.zig+55-59
......@@ -1,14 +1,11 @@
1const assert = @import("../debug.zig").assert;
21const builtin = @import("builtin");
3const arch = switch (builtin.arch) {
4 builtin.Arch.x86_64 => @import("x86_64.zig"),
5 else => @compileError("unsupported arch"),
6};
7pub use @import("syscall.zig");
2
83pub use @import("errno.zig");
94
105const std = @import("../../index.zig");
116const c = std.c;
7
8const assert = std.debug.assert;
129const maxInt = std.math.maxInt;
1310pub const Kevent = c.Kevent;
1411
......@@ -546,19 +543,19 @@ pub fn getErrno(r: usize) usize {
546543}
547544
548545pub 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));
550547}
551548
552549pub fn chdir(path: [*]const u8) usize {
553 return arch.syscall1(SYS_chdir, @ptrToInt(path));
550 return errnoWrap(c.chdir(path));
554551}
555552
556553pub 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));
558555}
559556
560557pub fn fork() usize {
561 return arch.syscall0(SYS_fork);
558 return errnoWrap(c.fork());
562559}
563560
564561pub fn access(path: [*]const u8, mode: u32) usize {
......@@ -566,7 +563,7 @@ pub fn access(path: [*]const u8, mode: u32) usize {
566563}
567564
568565pub 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;
570567}
571568
572569pub 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
578575}
579576
580577pub fn isatty(fd: i32) bool {
581 var wsz: winsize = undefined;
582 return arch.syscall3(SYS_ioctl, @bitCast(usize, isize(fd)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
578 return c.isatty(fd) != 0;
583579}
584580
585581pub 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));
587583}
588584
589585pub fn mkdir(path: [*]const u8, mode: u32) usize {
590 return arch.syscall2(SYS_mkdir, @ptrToInt(path), mode);
586 return errnoWrap(c.mkdir(path, mode));
591587}
592588
593pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {
594 return arch.syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
589pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
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);
595600}
596601
597602pub 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));
599604}
600605
601pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
602 return arch.syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
606pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize {
607 return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte));
603608}
604609
605610pub fn rmdir(path: [*]const u8) usize {
606 return arch.syscall1(SYS_rmdir, @ptrToInt(path));
611 return errnoWrap(c.rmdir(path));
607612}
608613
609614pub 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));
611616}
612617
613pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
614 return arch.syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
618pub fn pread(fd: i32, buf: [*]u8, nbyte: usize, offset: u64) usize {
619 return errnoWrap(c.pread(fd, @ptrCast(*c_void, buf), nbyte, offset));
615620}
616621
617622pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize {
......@@ -622,16 +627,17 @@ pub fn pipe(fd: *[2]i32) usize {
622627 return pipe2(fd, 0);
623628}
624629
625pub fn pipe2(fd: *[2]i32, flags: usize) usize {
626 return arch.syscall2(SYS_pipe2, @ptrToInt(fd), flags);
630pub fn pipe2(fd: *[2]i32, flags: u32) usize {
631 comptime assert(i32.bit_count == c_int.bit_count);
632 return errnoWrap(c.pipe2(@ptrCast(*[2]c_int, fd), flags));
627633}
628634
629pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
630 return arch.syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
635pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
636 return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte));
631637}
632638
633pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
634 return arch.syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
639pub fn pwrite(fd: i32, buf: [*]const u8, nbyte: usize, offset: u64) usize {
640 return errnoWrap(c.pwrite(fd, @ptrCast(*const c_void, buf), nbyte, offset));
635641}
636642
637643pub 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)
639645}
640646
641647pub 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));
643649}
644650
645pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {
646 return arch.syscall3(SYS_open, @ptrToInt(path), flags, perm);
651pub fn open(path: [*]const u8, flags: u32, mode: usize) usize {
652 return errnoWrap(c.open(path, @bitCast(c_int, flags), mode));
647653}
648654
649655pub 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 {
655661}
656662
657663pub fn close(fd: i32) usize {
658 return arch.syscall1(SYS_close, @bitCast(usize, isize(fd)));
664 return errnoWrap(c.close(fd));
659665}
660666
661pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
662 return arch.syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), ref_pos);
667pub fn lseek(fd: i32, offset: isize, whence: c_int) usize {
668 return errnoWrap(c.lseek(fd, offset, whence));
663669}
664670
665pub fn exit(status: i32) noreturn {
666 _ = arch.syscall1(SYS_exit, @bitCast(usize, isize(status)));
667 unreachable;
671pub fn exit(code: i32) noreturn {
672 c.exit(code);
668673}
669674
670675pub 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));
672677}
673678
674679pub fn kill(pid: i32, sig: i32) usize {
......@@ -676,15 +681,16 @@ pub fn kill(pid: i32, sig: i32) usize {
676681}
677682
678683pub fn unlink(path: [*]const u8) usize {
679 return arch.syscall1(SYS_unlink, @ptrToInt(path));
684 return errnoWrap(c.unlink(path));
680685}
681686
682pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
683 return arch.syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
687pub fn waitpid(pid: i32, status: *i32, options: u32) usize {
688 comptime assert(i32.bit_count == c_int.bit_count);
689 return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options)));
684690}
685691
686692pub 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));
688694}
689695
690696pub fn setuid(uid: u32) usize {
......@@ -696,11 +702,11 @@ pub fn setgid(gid: u32) usize {
696702}
697703
698704pub fn setreuid(ruid: u32, euid: u32) usize {
699 return arch.syscall2(SYS_setreuid, ruid, euid);
705 return errnoWrap(c.setreuid(ruid, euid));
700706}
701707
702708pub fn setregid(rgid: u32, egid: u32) usize {
703 return arch.syscall2(SYS_setregid, rgid, egid);
709 return errnoWrap(c.setregid(rgid, egid));
704710}
705711
706712const NSIG = 32;
......@@ -745,26 +751,16 @@ pub const sigset_t = extern struct {
745751};
746752
747753pub fn raise(sig: i32) usize {
748 // TODO have a chat with the freebsd folks and make sure there's no bug in
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)));
754 return errnoWrap(c.raise(sig));
758755}
759756
760757pub const Stat = c.Stat;
761758pub const dirent = c.dirent;
762759pub const timespec = c.timespec;
763760
764pub fn fstat(fd: i32, stat_buf: *Stat) usize {
765 return arch.syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));
761pub fn fstat(fd: i32, buf: *c.Stat) usize {
762 return errnoWrap(c.fstat(fd, buf));
766763}
767
768764pub const iovec = extern struct {
769765 iov_base: [*]u8,
770766 iov_len: usize,