| ... | ... | @@ -158,7 +158,6 @@ pub const SOCK_SEQPACKET = 5; |
| 158 | 158 | pub const SOCK_CLOEXEC = 0x10000000; |
| 159 | 159 | pub const SOCK_NONBLOCK = 0x20000000; |
| 160 | 160 | |
| 161 | | // TODO: From here |
| 162 | 161 | pub const PROTO_ip = 0o000; |
| 163 | 162 | pub const PROTO_icmp = 0o001; |
| 164 | 163 | pub const PROTO_igmp = 0o002; |
| ... | ... | @@ -540,7 +539,7 @@ pub fn getErrno(r: usize) usize { |
| 540 | 539 | } |
| 541 | 540 | |
| 542 | 541 | pub 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 | } |
| 545 | 544 | |
| 546 | 545 | pub fn chdir(path: [*]const u8) usize { |
| ... | ... | @@ -560,12 +559,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { |
| 560 | 559 | } |
| 561 | 560 | |
| 562 | 561 | pub 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 | } |
| 565 | 564 | |
| 566 | 565 | pub fn isatty(fd: i32) bool { |
| 567 | 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 | } |
| 570 | 569 | |
| 571 | 570 | pub 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 | 576 | } |
| 578 | 577 | |
| 579 | 578 | pub 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 | } |
| 582 | 581 | |
| 583 | 582 | pub fn munmap(address: usize, length: usize) usize { |
| ... | ... | @@ -585,7 +584,7 @@ pub fn munmap(address: usize, length: usize) usize { |
| 585 | 584 | } |
| 586 | 585 | |
| 587 | 586 | pub 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 | } |
| 590 | 589 | |
| 591 | 590 | pub fn rmdir(path: [*]const u8) usize { |
| ... | ... | @@ -597,11 +596,11 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { |
| 597 | 596 | } |
| 598 | 597 | |
| 599 | 598 | pub 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 | } |
| 602 | 601 | |
| 603 | 602 | pub 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 | } |
| 606 | 605 | |
| 607 | 606 | pub fn pipe(fd: *[2]i32) usize { |
| ... | ... | @@ -613,15 +612,15 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize { |
| 613 | 612 | } |
| 614 | 613 | |
| 615 | 614 | pub 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 | } |
| 618 | 617 | |
| 619 | 618 | pub 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 | } |
| 622 | 621 | |
| 623 | 622 | pub 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 | } |
| 626 | 625 | |
| 627 | 626 | pub fn rename(old: [*]const u8, new: [*]const u8) usize { |
| ... | ... | @@ -637,15 +636,15 @@ pub fn create(path: [*]const u8, perm: usize) usize { |
| 637 | 636 | } |
| 638 | 637 | |
| 639 | 638 | pub 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 | } |
| 642 | 641 | |
| 643 | 642 | pub 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 | } |
| 646 | 645 | |
| 647 | 646 | pub 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 | } |
| 650 | 649 | |
| 651 | 650 | pub fn exit(status: i32) noreturn { |
| ... | ... | @@ -654,11 +653,11 @@ pub fn exit(status: i32) noreturn { |
| 654 | 653 | } |
| 655 | 654 | |
| 656 | 655 | pub 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 | } |
| 659 | 658 | |
| 660 | 659 | pub 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 | } |
| 663 | 662 | |
| 664 | 663 | pub fn unlink(path: [*]const u8) usize { |
| ... | ... | @@ -689,66 +688,65 @@ pub fn setregid(rgid: u32, egid: u32) usize { |
| 689 | 688 | return arch.syscall2(SYS_setregid, rgid, egid); |
| 690 | 689 | } |
| 691 | 690 | |
| 692 | | pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { |
| 693 | | // TODO: Implement |
| 694 | | return 0; |
| 695 | | } |
| 696 | | |
| 697 | | pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { |
| 698 | | // TODO: Implement |
| 699 | | return 0; |
| 700 | | } |
| 691 | const NSIG = 32; |
| 701 | 692 | |
| 702 | | const NSIG = 65; |
| 703 | | const sigset_t = [128 / @sizeOf(usize)]usize; |
| 704 | | const all_mask = []usize{maxInt(usize)}; |
| 705 | | const app_mask = []usize{0xfffffffc7fffffff}; |
| 693 | pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); |
| 694 | pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); |
| 695 | pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); |
| 706 | 696 | |
| 707 | 697 | /// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. |
| 708 | | pub const Sigaction = struct { |
| 709 | | // TODO: Adjust to use freebsd struct layout |
| 710 | | handler: extern fn (i32) void, |
| 711 | | mask: sigset_t, |
| 712 | | flags: u32, |
| 698 | pub 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, |
| 713 | 710 | }; |
| 714 | 711 | |
| 715 | | pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); |
| 716 | | pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); |
| 717 | | pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); |
| 718 | | pub const empty_sigset = []usize{0} ** sigset_t.len; |
| 712 | pub const _SIG_WORDS = 4; |
| 713 | pub const _SIG_MAXSIG = 128; |
| 719 | 714 | |
| 720 | | pub fn raise(sig: i32) usize { |
| 721 | | // TODO implement, see linux equivalent for what we want to try and do |
| 722 | | return 0; |
| 715 | pub inline fn _SIG_IDX(sig: usize) usize { |
| 716 | return sig - 1; |
| 723 | 717 | } |
| 724 | | |
| 725 | | fn blockAllSignals(set: *sigset_t) void { |
| 726 | | // TODO implement |
| 718 | pub inline fn _SIG_WORD(sig: usize) usize { |
| 719 | return_SIG_IDX(sig) >> 5; |
| 727 | 720 | } |
| 728 | | |
| 729 | | fn blockAppSignals(set: *sigset_t) void { |
| 730 | | // TODO implement |
| 721 | pub inline fn _SIG_BIT(sig: usize) usize { |
| 722 | return 1 << (_SIG_IDX(sig) & 31); |
| 731 | 723 | } |
| 732 | | |
| 733 | | fn restoreSignals(set: *sigset_t) void { |
| 734 | | // TODO implement |
| 724 | pub inline fn _SIG_VALID(sig: usize) usize { |
| 725 | return sig <= _SIG_MAXSIG and sig > 0; |
| 735 | 726 | } |
| 736 | 727 | |
| 737 | | pub 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 | | } |
| 728 | pub const sigset_t = extern struct { |
| 729 | __bits: [_SIG_WORDS]u32, |
| 730 | }; |
| 741 | 731 | |
| 742 | | pub 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; |
| 732 | pub 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))); |
| 745 | 743 | } |
| 746 | 744 | |
| 747 | 745 | pub const Stat = arch.Stat; |
| 748 | 746 | pub const timespec = arch.timespec; |
| 749 | 747 | |
| 750 | 748 | pub 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 | } |
| 753 | 751 | |
| 754 | 752 | pub const iovec = extern struct { |
| ... | ... | @@ -761,10 +759,12 @@ pub const iovec_const = extern struct { |
| 761 | 759 | iov_len: usize, |
| 762 | 760 | }; |
| 763 | 761 | |
| 762 | // TODO avoid libc dependency |
| 764 | 763 | pub fn kqueue() usize { |
| 765 | 764 | return errnoWrap(c.kqueue()); |
| 766 | 765 | } |
| 767 | 766 | |
| 767 | // TODO avoid libc dependency |
| 768 | 768 | pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec) usize { |
| 769 | 769 | return errnoWrap(c.kevent( |
| 770 | 770 | kq, |
| ... | ... | @@ -776,18 +776,23 @@ pub fn kevent(kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: |
| 776 | 776 | )); |
| 777 | 777 | } |
| 778 | 778 | |
| 779 | // TODO avoid libc dependency |
| 779 | 780 | pub fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { |
| 780 | 781 | return errnoWrap(c.sysctl(name, namelen, oldp, oldlenp, newp, newlen)); |
| 781 | 782 | } |
| 782 | 783 | |
| 784 | // TODO avoid libc dependency |
| 783 | 785 | pub fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) usize { |
| 784 | 786 | return errnoWrap(c.sysctlbyname(name, oldp, oldlenp, newp, newlen)); |
| 785 | 787 | } |
| 786 | 788 | |
| 789 | // TODO avoid libc dependency |
| 787 | 790 | pub fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) usize { |
| 788 | 791 | return errnoWrap(c.sysctlnametomib(name, wibp, sizep)); |
| 789 | 792 | } |
| 790 | 793 | |
| 794 | // TODO avoid libc dependency |
| 795 | |
| 791 | 796 | /// Takes the return value from a syscall and formats it back in the way |
| 792 | 797 | /// that the kernel represents it to libc. Errno was a mistake, let's make |
| 793 | 798 | /// it go away forever. |