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;...@@ -158,7 +158,6 @@ pub const SOCK_SEQPACKET = 5;
158pub const SOCK_CLOEXEC = 0x10000000;158pub const SOCK_CLOEXEC = 0x10000000;
159pub const SOCK_NONBLOCK = 0x20000000;159pub const SOCK_NONBLOCK = 0x20000000;
160160
161// TODO: From here
162pub const PROTO_ip = 0o000;161pub const PROTO_ip = 0o000;
163pub const PROTO_icmp = 0o001;162pub const PROTO_icmp = 0o001;
164pub const PROTO_igmp = 0o002;163pub const PROTO_igmp = 0o002;
...@@ -540,7 +539,7 @@ pub fn getErrno(r: usize) usize {...@@ -540,7 +539,7 @@ pub fn getErrno(r: usize) usize {
540}539}
541540
542pub fn dup2(old: i32, new: i32) usize {541pub 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)));
544}543}
545544
546pub fn chdir(path: [*]const u8) usize {545pub fn chdir(path: [*]const u8) usize {
...@@ -560,12 +559,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {...@@ -560,12 +559,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
560}559}
561560
562pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {561pub 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);
564}563}
565564
566pub fn isatty(fd: i32) bool {565pub fn isatty(fd: i32) bool {
567 var wsz: winsize = undefined;566 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;
569}568}
570569
571pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {570pub 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 {...@@ -577,7 +576,7 @@ pub fn mkdir(path: [*]const u8, mode: u32) usize {
577}576}
578577
579pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {578pub 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));
581}580}
582581
583pub fn munmap(address: usize, length: usize) usize {582pub fn munmap(address: usize, length: usize) usize {
...@@ -585,7 +584,7 @@ pub fn munmap(address: usize, length: usize) usize {...@@ -585,7 +584,7 @@ pub fn munmap(address: usize, length: usize) usize {
585}584}
586585
587pub fn read(fd: i32, buf: [*]u8, count: usize) usize {586pub 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);
589}588}
590589
591pub fn rmdir(path: [*]const u8) usize {590pub fn rmdir(path: [*]const u8) usize {
...@@ -597,11 +596,11 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {...@@ -597,11 +596,11 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
597}596}
598597
599pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {598pub 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);
601}600}
602601
603pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: usize) usize {602pub 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);
605}604}
606605
607pub fn pipe(fd: *[2]i32) usize {606pub fn pipe(fd: *[2]i32) usize {
...@@ -613,15 +612,15 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {...@@ -613,15 +612,15 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {
613}612}
614613
615pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {614pub 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);
617}616}
618617
619pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {618pub 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);
621}620}
622621
623pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: usize) usize {622pub 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);
625}624}
626625
627pub fn rename(old: [*]const u8, new: [*]const u8) usize {626pub fn rename(old: [*]const u8, new: [*]const u8) usize {
...@@ -637,15 +636,15 @@ pub fn create(path: [*]const u8, perm: usize) usize {...@@ -637,15 +636,15 @@ pub fn create(path: [*]const u8, perm: usize) usize {
637}636}
638637
639pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {638pub 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);
641}640}
642641
643pub fn close(fd: i32) usize {642pub fn close(fd: i32) usize {
644 return arch.syscall1(SYS_close, @intCast(usize, fd));643 return arch.syscall1(SYS_close, @bitCast(usize, isize(fd)));
645}644}
646645
647pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {646pub 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);
649}648}
650649
651pub fn exit(status: i32) noreturn {650pub fn exit(status: i32) noreturn {
...@@ -654,11 +653,11 @@ pub fn exit(status: i32) noreturn {...@@ -654,11 +653,11 @@ pub fn exit(status: i32) noreturn {
654}653}
655654
656pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {655pub 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));
658}657}
659658
660pub fn kill(pid: i32, sig: i32) usize {659pub 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)));
662}661}
663662
664pub fn unlink(path: [*]const u8) usize {663pub fn unlink(path: [*]const u8) usize {
...@@ -689,66 +688,65 @@ pub fn setregid(rgid: u32, egid: u32) usize {...@@ -689,66 +688,65 @@ pub fn setregid(rgid: u32, egid: u32) usize {
689 return arch.syscall2(SYS_setregid, rgid, egid);688 return arch.syscall2(SYS_setregid, rgid, egid);
690}689}
691690
692pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {691const NSIG = 32;
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}
701692
702const NSIG = 65;693pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize));
703const sigset_t = [128 / @sizeOf(usize)]usize;694pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);
704const all_mask = []usize{maxInt(usize)};695pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);
705const app_mask = []usize{0xfffffffc7fffffff};
706696
707/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.697/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
708pub const Sigaction = struct {698pub const Sigaction = extern struct {
709 // TODO: Adjust to use freebsd struct layout699 /// signal handler
710 handler: extern fn (i32) void,700 __sigaction_u: extern union {
711 mask: sigset_t,701 __sa_handler: extern fn (i32) void,
712 flags: u32,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,
713};710};
714711
715pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize));712pub const _SIG_WORDS = 4;
716pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);713pub const _SIG_MAXSIG = 128;
717pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);
718pub const empty_sigset = []usize{0} ** sigset_t.len;
719714
720pub fn raise(sig: i32) usize {715pub inline fn _SIG_IDX(sig: usize) usize {
721 // TODO implement, see linux equivalent for what we want to try and do716 return sig - 1;
722 return 0;
723}717}
724718pub inline fn _SIG_WORD(sig: usize) usize {
725fn blockAllSignals(set: *sigset_t) void {719 return_SIG_IDX(sig) >> 5;
726 // TODO implement
727}720}
728721pub inline fn _SIG_BIT(sig: usize) usize {
729fn blockAppSignals(set: *sigset_t) void {722 return 1 << (_SIG_IDX(sig) & 31);
730 // TODO implement
731}723}
732724pub inline fn _SIG_VALID(sig: usize) usize {
733fn restoreSignals(set: *sigset_t) void {725 return sig <= _SIG_MAXSIG and sig > 0;
734 // TODO implement
735}726}
736727
737pub fn sigaddset(set: *sigset_t, sig: u6) void {728pub const sigset_t = extern struct {
738 const s = sig - 1;729 __bits: [_SIG_WORDS]u32,
739 (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));730};
740}
741731
742pub fn sigismember(set: *const sigset_t, sig: u6) bool {732pub fn raise(sig: i32) usize {
743 const s = sig - 1;733 // TODO have a chat with the freebsd folks and make sure there's no bug in
744 return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;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)));
745}743}
746744
747pub const Stat = arch.Stat;745pub const Stat = arch.Stat;
748pub const timespec = arch.timespec;746pub const timespec = arch.timespec;
749747
750pub fn fstat(fd: i32, stat_buf: *Stat) usize {748pub 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));
752}750}
753751
754pub const iovec = extern struct {752pub const iovec = extern struct {
...@@ -761,10 +759,12 @@ pub const iovec_const = extern struct {...@@ -761,10 +759,12 @@ pub const iovec_const = extern struct {
761 iov_len: usize,759 iov_len: usize,
762};760};
763761
762// TODO avoid libc dependency
764pub fn kqueue() usize {763pub fn kqueue() usize {
765 return errnoWrap(c.kqueue());764 return errnoWrap(c.kqueue());
766}765}
767766
767// TODO avoid libc dependency
768pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize {768pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize {
769 return errnoWrap(c.kevent(769 return errnoWrap(c.kevent(
770 kq,770 kq,
...@@ -776,18 +776,23 @@ pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout:...@@ -776,18 +776,23 @@ pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout:
776 ));776 ));
777}777}
778778
779// TODO avoid libc dependency
779pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {780pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {
780 return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen));781 return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen));
781}782}
782783
784// TODO avoid libc dependency
783pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {785pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize {
784 return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen));786 return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen));
785}787}
786788
789// TODO avoid libc dependency
787pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize {790pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize {
788 return errnoWrap(c.sysctlnametomib(name, wibp, sizep));791 return errnoWrap(c.sysctlnametomib(name, wibp, sizep));
789}792}
790793
794// TODO avoid libc dependency
795
791/// Takes the return value from a syscall and formats it back in the way796/// Takes the return value from a syscall and formats it back in the way
792/// that the kernel represents it to libc. Errno was a mistake, let's make797/// that the kernel represents it to libc. Errno was a mistake, let's make
793/// it go away forever.798/// it go away forever.