authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-13 00:33:13-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-13 00:33:13-05:00
log7417f2e4b3a9c21249bac38688ef453c6b86acfa
treef527511e25c127d2f4a5329be2b4aa255dbcdf02
parentd770333827549e5dcbb727c7f434eaac9dd433d2
signaturelock-open Commit is signed but in an unrecognized format.

freebsd: fix issues with syscalls


1 files changed, 63 insertions(+), 58 deletions(-)

std/os/freebsd/index.zig+63-58
......@@ -158,7 +158,6 @@ pub const SOCK_SEQPACKET = 5;
158158pub const SOCK_CLOEXEC = 0x10000000;
159159pub const SOCK_NONBLOCK = 0x20000000;
160160
161// TODO: From here
162161pub const PROTO_ip = 0o000;
163162pub const PROTO_icmp = 0o001;
164163pub const PROTO_igmp = 0o002;
......@@ -540,7 +539,7 @@ pub fn getErrno(r: usize) usize {
540539}
541540
542541pub fn dup2(old: i32, new: i32) usize {
543 return arch.syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
542 return arch.syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)));
544543}
545544
546545pub fn chdir(path: [*]const u8) usize {
......@@ -560,12 +559,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
560559}
561560
562561pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
563 return arch.syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
562 return arch.syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
564563}
565564
566565pub fn isatty(fd: i32) bool {
567566 var wsz: winsize = undefined;
568 return arch.syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
567 return arch.syscall3(SYS_ioctl, @bitCast(usize, isize(fd)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
569568}
570569
571570pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
......@@ -577,7 +576,7 @@ pub fn mkdir(path: [*]const u8, mode: u32) usize {
577576}
578577
579578pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {
580 return arch.syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
579 return arch.syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
581580}
582581
583582pub fn munmap(address: usize, length: usize) usize {
......@@ -585,7 +584,7 @@ pub fn munmap(address: usize, length: usize) usize {
585584}
586585
587586pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
588 return arch.syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count);
587 return arch.syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
589588}
590589
591590pub fn rmdir(path: [*]const u8) usize {
......@@ -597,11 +596,11 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
597596}
598597
599598pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
600 return arch.syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
599 return arch.syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
601600}
602601
603602pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize {
604 return arch.syscall4(SYS_preadv, @intCast(usize, fd), @ptrToInt(iov), count, offset);
603 return arch.syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
605604}
606605
607606pub fn pipe(fd: *[2]i32) usize {
......@@ -613,15 +612,15 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {
613612}
614613
615614pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
616 return arch.syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count);
615 return arch.syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
617616}
618617
619618pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
620 return arch.syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset);
619 return arch.syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
621620}
622621
623622pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize {
624 return arch.syscall4(SYS_pwritev, @intCast(usize, fd), @ptrToInt(iov), count, offset);
623 return arch.syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
625624}
626625
627626pub fn rename(old: [*]const u8, new: [*]const u8) usize {
......@@ -637,15 +636,15 @@ pub fn create(path: [*]const u8, perm: usize) usize {
637636}
638637
639638pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
640 return arch.syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
639 return arch.syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode);
641640}
642641
643642pub fn close(fd: i32) usize {
644 return arch.syscall1(SYS_close, @intCast(usize, fd));
643 return arch.syscall1(SYS_close, @bitCast(usize, isize(fd)));
645644}
646645
647646pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
648 return arch.syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos);
647 return arch.syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), ref_pos);
649648}
650649
651650pub fn exit(status: i32) noreturn {
......@@ -654,11 +653,11 @@ pub fn exit(status: i32) noreturn {
654653}
655654
656655pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
657 return arch.syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
656 return arch.syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));
658657}
659658
660659pub fn kill(pid: i32, sig: i32) usize {
661 return arch.syscall2(SYS_kill, @bitCast(usize, @intCast(isize, pid)), @intCast(usize, sig));
660 return arch.syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig)));
662661}
663662
664663pub fn unlink(path: [*]const u8) usize {
......@@ -689,66 +688,65 @@ pub fn setregid(rgid: u32, egid: u32) usize {
689688 return arch.syscall2(SYS_setregid, rgid, egid);
690689}
691690
692pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
693 // TODO: Implement
694 return 0;
695}
696
697pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
698 // TODO: Implement
699 return 0;
700}
691const NSIG = 32;
701692
702const NSIG = 65;
703const sigset_t = [128 / @sizeOf(usize)]usize;
704const all_mask = []usize{maxInt(usize)};
705const app_mask = []usize{0xfffffffc7fffffff};
693pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize));
694pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);
695pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);
706696
707697/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
708pub const Sigaction = struct {
709 // TODO: Adjust to use freebsd struct layout
710 handler: extern fn (i32) void,
711 mask: sigset_t,
712 flags: u32,
698pub const Sigaction = extern struct {
699 /// signal handler
700 __sigaction_u: extern union {
701 __sa_handler: extern fn (i32) void,
702 __sa_sigaction: extern fn (i32, *__siginfo, usize) void,
703 },
704
705 /// see signal options
706 sa_flags: u32,
707
708 /// signal mask to apply
709 sa_mask: sigset_t,
713710};
714711
715pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize));
716pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);
717pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);
718pub const empty_sigset = []usize{0} ** sigset_t.len;
712pub const _SIG_WORDS = 4;
713pub const _SIG_MAXSIG = 128;
719714
720pub fn raise(sig: i32) usize {
721 // TODO implement, see linux equivalent for what we want to try and do
722 return 0;
715pub inline fn _SIG_IDX(sig: usize) usize {
716 return sig - 1;
723717}
724
725fn blockAllSignals(set: *sigset_t) void {
726 // TODO implement
718pub inline fn _SIG_WORD(sig: usize) usize {
719 return_SIG_IDX(sig) >> 5;
727720}
728
729fn blockAppSignals(set: *sigset_t) void {
730 // TODO implement
721pub inline fn _SIG_BIT(sig: usize) usize {
722 return 1 << (_SIG_IDX(sig) & 31);
731723}
732
733fn restoreSignals(set: *sigset_t) void {
734 // TODO implement
724pub inline fn _SIG_VALID(sig: usize) usize {
725 return sig <= _SIG_MAXSIG and sig > 0;
735726}
736727
737pub fn sigaddset(set: *sigset_t, sig: u6) void {
738 const s = sig - 1;
739 (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
740}
728pub const sigset_t = extern struct {
729 __bits: [_SIG_WORDS]u32,
730};
741731
742pub fn sigismember(set: *const sigset_t, sig: u6) bool {
743 const s = sig - 1;
744 return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
732pub fn raise(sig: i32) usize {
733 // TODO have a chat with the freebsd folks and make sure there's no bug in
734 // their libc. musl-libc blocks signals in between these calls because
735 // if a signal handler runs and forks between the gettid and sending the
736 // signal, the parent will get 2 signals, one from itself and one from the child
737 // if the protection does not belong here, then it belongs in abort(),
738 // like it does in freebsd's libc.
739 var id: usize = undefined;
740 const rc = arch.syscall1(SYS_thr_self, @ptrToInt(&id));
741 if (getErrno(rc) != 0) return rc;
742 return arch.syscall2(SYS_thr_kill, id, @bitCast(usize, isize(sig)));
745743}
746744
747745pub const Stat = arch.Stat;
748746pub const timespec = arch.timespec;
749747
750748pub fn fstat(fd: i32, stat_buf: *Stat) usize {
751 return arch.syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
749 return arch.syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));
752750}
753751
754752pub const iovec = extern struct {
......@@ -761,10 +759,12 @@ pub const iovec_const = extern struct {
761759 iov_len: usize,
762760};
763761
762// TODO avoid libc dependency
764763pub fn kqueue() usize {
765764 return errnoWrap(c.kqueue());
766765}
767766
767// TODO avoid libc dependency
768768pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize {
769769 return errnoWrap(c.kevent(
770770 kq,
......@@ -776,18 +776,23 @@ pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout:
776776 ));
777777}
778778
779// TODO avoid libc dependency
779780pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {
780781 return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen));
781782}
782783
784// TODO avoid libc dependency
783785pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {
784786 return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen));
785787}
786788
789// TODO avoid libc dependency
787790pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize {
788791 return errnoWrap(c.sysctlnametomib(name, wibp, sizep));
789792}
790793
794// TODO avoid libc dependency
795
791796/// Takes the return value from a syscall and formats it back in the way
792797/// that the kernel represents it to libc. Errno was a mistake, let's make
793798/// it go away forever.