| author | |
| committer | |
| log | 36eb8dec98c743f369a2badf6a56599c736a45ae |
| tree | b6af4445b3d486aaf63378f40d2ca1de032c04f6 |
| parent | e7e168727e3c024869de9d114a7dd49d2a80de4d |
- fstat
- inotify_init1
- inotify_add_watch, inotify_add_watchZ
- inotify_rm_watch
- sysctlbynameZ5 files changed, 40 insertions(+), 143 deletions(-)
lib/std/Thread.zig+9-6| ... | ... | @@ -809,12 +809,15 @@ const PosixThreadImpl = struct { |
| 809 | 809 | else => { |
| 810 | 810 | var count: c_int = undefined; |
| 811 | 811 | var count_len: usize = @sizeOf(c_int); |
| 812 | const name = if (comptime target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu"; | |
| 813 | posix.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) { | |
| 814 | error.UnknownName => unreachable, | |
| 815 | else => |e| return e, | |
| 816 | }; | |
| 817 | return @as(usize, @intCast(count)); | |
| 812 | const name = comptime if (target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu"; | |
| 813 | switch (posix.errno(posix.system.sysctlbyname(name, &count, &count_len, null, 0))) { | |
| 814 | .SUCCESS => return @intCast(count), | |
| 815 | .FAULT => unreachable, | |
| 816 | .PERM => return error.PermissionDenied, | |
| 817 | .NOMEM => return error.SystemResources, | |
| 818 | .NOENT => unreachable, | |
| 819 | else => |err| return posix.unexpectedErrno(err), | |
| 820 | } | |
| 818 | 821 | }, |
| 819 | 822 | } |
| 820 | 823 | } |
lib/std/posix.zig-113| ... | ... | @@ -677,89 +677,6 @@ pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock |
| 677 | 677 | } |
| 678 | 678 | } |
| 679 | 679 | |
| 680 | pub const FStatError = std.Io.File.StatError; | |
| 681 | ||
| 682 | /// Return information about a file descriptor. | |
| 683 | pub fn fstat(fd: fd_t) FStatError!Stat { | |
| 684 | if (native_os == .wasi and !builtin.link_libc) { | |
| 685 | @compileError("unsupported OS"); | |
| 686 | } | |
| 687 | ||
| 688 | var stat = mem.zeroes(Stat); | |
| 689 | switch (errno(system.fstat(fd, &stat))) { | |
| 690 | .SUCCESS => return stat, | |
| 691 | .INVAL => unreachable, | |
| 692 | .BADF => unreachable, // Always a race condition. | |
| 693 | .NOMEM => return error.SystemResources, | |
| 694 | .ACCES => return error.AccessDenied, | |
| 695 | else => |err| return unexpectedErrno(err), | |
| 696 | } | |
| 697 | } | |
| 698 | ||
| 699 | pub const INotifyInitError = error{ | |
| 700 | ProcessFdQuotaExceeded, | |
| 701 | SystemFdQuotaExceeded, | |
| 702 | SystemResources, | |
| 703 | } || UnexpectedError; | |
| 704 | ||
| 705 | /// initialize an inotify instance | |
| 706 | pub fn inotify_init1(flags: u32) INotifyInitError!i32 { | |
| 707 | const rc = system.inotify_init1(flags); | |
| 708 | switch (errno(rc)) { | |
| 709 | .SUCCESS => return @intCast(rc), | |
| 710 | .INVAL => unreachable, | |
| 711 | .MFILE => return error.ProcessFdQuotaExceeded, | |
| 712 | .NFILE => return error.SystemFdQuotaExceeded, | |
| 713 | .NOMEM => return error.SystemResources, | |
| 714 | else => |err| return unexpectedErrno(err), | |
| 715 | } | |
| 716 | } | |
| 717 | ||
| 718 | pub const INotifyAddWatchError = error{ | |
| 719 | AccessDenied, | |
| 720 | NameTooLong, | |
| 721 | FileNotFound, | |
| 722 | SystemResources, | |
| 723 | UserResourceLimitReached, | |
| 724 | NotDir, | |
| 725 | WatchAlreadyExists, | |
| 726 | } || UnexpectedError; | |
| 727 | ||
| 728 | /// add a watch to an initialized inotify instance | |
| 729 | pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INotifyAddWatchError!i32 { | |
| 730 | const pathname_c = try toPosixPath(pathname); | |
| 731 | return inotify_add_watchZ(inotify_fd, &pathname_c, mask); | |
| 732 | } | |
| 733 | ||
| 734 | /// Same as `inotify_add_watch` except pathname is null-terminated. | |
| 735 | pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 { | |
| 736 | const rc = system.inotify_add_watch(inotify_fd, pathname, mask); | |
| 737 | switch (errno(rc)) { | |
| 738 | .SUCCESS => return @intCast(rc), | |
| 739 | .ACCES => return error.AccessDenied, | |
| 740 | .BADF => unreachable, | |
| 741 | .FAULT => unreachable, | |
| 742 | .INVAL => unreachable, | |
| 743 | .NAMETOOLONG => return error.NameTooLong, | |
| 744 | .NOENT => return error.FileNotFound, | |
| 745 | .NOMEM => return error.SystemResources, | |
| 746 | .NOSPC => return error.UserResourceLimitReached, | |
| 747 | .NOTDIR => return error.NotDir, | |
| 748 | .EXIST => return error.WatchAlreadyExists, | |
| 749 | else => |err| return unexpectedErrno(err), | |
| 750 | } | |
| 751 | } | |
| 752 | ||
| 753 | /// remove an existing watch from an inotify instance | |
| 754 | pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void { | |
| 755 | switch (errno(system.inotify_rm_watch(inotify_fd, wd))) { | |
| 756 | .SUCCESS => return, | |
| 757 | .BADF => unreachable, | |
| 758 | .INVAL => unreachable, | |
| 759 | else => unreachable, | |
| 760 | } | |
| 761 | } | |
| 762 | ||
| 763 | 680 | pub const FanotifyInitError = error{ |
| 764 | 681 | ProcessFdQuotaExceeded, |
| 765 | 682 | SystemFdQuotaExceeded, |
| ... | ... | @@ -996,36 +913,6 @@ pub fn sysctl( |
| 996 | 913 | } |
| 997 | 914 | } |
| 998 | 915 | |
| 999 | pub const SysCtlByNameError = error{ | |
| 1000 | PermissionDenied, | |
| 1001 | SystemResources, | |
| 1002 | UnknownName, | |
| 1003 | } || UnexpectedError; | |
| 1004 | ||
| 1005 | pub fn sysctlbynameZ( | |
| 1006 | name: [*:0]const u8, | |
| 1007 | oldp: ?*anyopaque, | |
| 1008 | oldlenp: ?*usize, | |
| 1009 | newp: ?*anyopaque, | |
| 1010 | newlen: usize, | |
| 1011 | ) SysCtlByNameError!void { | |
| 1012 | if (native_os == .wasi) { | |
| 1013 | @compileError("sysctl not supported on WASI"); | |
| 1014 | } | |
| 1015 | if (native_os == .haiku) { | |
| 1016 | @compileError("sysctl not supported on Haiku"); | |
| 1017 | } | |
| 1018 | ||
| 1019 | switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) { | |
| 1020 | .SUCCESS => return, | |
| 1021 | .FAULT => unreachable, | |
| 1022 | .PERM => return error.PermissionDenied, | |
| 1023 | .NOMEM => return error.SystemResources, | |
| 1024 | .NOENT => return error.UnknownName, | |
| 1025 | else => |err| return unexpectedErrno(err), | |
| 1026 | } | |
| 1027 | } | |
| 1028 | ||
| 1029 | 916 | pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { |
| 1030 | 917 | switch (errno(system.gettimeofday(tv, tz))) { |
| 1031 | 918 | .SUCCESS => return, |
lib/std/process.zig+14-12| ... | ... | @@ -556,26 +556,28 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 { |
| 556 | 556 | const name = if (native_os == .netbsd) "hw.physmem64" else "hw.physmem"; |
| 557 | 557 | var physmem: c_ulong = undefined; |
| 558 | 558 | var len: usize = @sizeOf(c_ulong); |
| 559 | posix.sysctlbynameZ(name, &physmem, &len, null, 0) catch |err| switch (err) { | |
| 560 | error.PermissionDenied => unreachable, // only when setting values, | |
| 561 | error.SystemResources => unreachable, // memory already on the stack | |
| 562 | error.UnknownName => unreachable, | |
| 559 | switch (posix.errno(posix.system.sysctlbyname(name, &physmem, &len, null, 0))) { | |
| 560 | .SUCCESS => return @intCast(physmem), | |
| 561 | .FAULT => unreachable, | |
| 562 | .PERM => unreachable, // only when setting values | |
| 563 | .NOMEM => unreachable, // memory already on the stack | |
| 564 | .NOENT => unreachable, | |
| 563 | 565 | else => return error.UnknownTotalSystemMemory, |
| 564 | }; | |
| 565 | return @intCast(physmem); | |
| 566 | } | |
| 566 | 567 | }, |
| 567 | 568 | // whole Darwin family |
| 568 | 569 | .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => { |
| 569 | 570 | // "hw.memsize" returns uint64_t |
| 570 | 571 | var physmem: u64 = undefined; |
| 571 | 572 | var len: usize = @sizeOf(u64); |
| 572 | posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) { | |
| 573 | error.PermissionDenied => unreachable, // only when setting values, | |
| 574 | error.SystemResources => unreachable, // memory already on the stack | |
| 575 | error.UnknownName => unreachable, // constant, known good value | |
| 573 | switch (posix.errno(posix.system.sysctlbyname("hw.memsize", &physmem, &len, null, 0))) { | |
| 574 | .SUCCESS => return physmem, | |
| 575 | .FAULT => unreachable, | |
| 576 | .PERM => unreachable, // only when setting values | |
| 577 | .NOMEM => unreachable, // memory already on the stack | |
| 578 | .NOENT => unreachable, // constant, known good value | |
| 576 | 579 | else => return error.UnknownTotalSystemMemory, |
| 577 | }; | |
| 578 | return physmem; | |
| 580 | } | |
| 579 | 581 | }, |
| 580 | 582 | .openbsd => { |
| 581 | 583 | const mib: [2]c_int = [_]c_int{ |
lib/std/zig/system.zig+8-6| ... | ... | @@ -260,12 +260,14 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target { |
| 260 | 260 | var value: u32 = undefined; |
| 261 | 261 | var len: usize = @sizeOf(@TypeOf(value)); |
| 262 | 262 | |
| 263 | posix.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) { | |
| 264 | error.PermissionDenied => unreachable, // only when setting values, | |
| 265 | error.SystemResources => unreachable, // memory already on the stack | |
| 266 | error.UnknownName => unreachable, // constant, known good value | |
| 267 | error.Unexpected => return error.OSVersionDetectionFail, | |
| 268 | }; | |
| 263 | switch (posix.errno(posix.system.sysctlbyname(key, &value, &len, null, 0))) { | |
| 264 | .SUCCESS => {}, | |
| 265 | .FAULT => unreachable, | |
| 266 | .PERM => unreachable, // only when setting values, | |
| 267 | .NOMEM => unreachable, // memory already on the stack | |
| 268 | .NOENT => unreachable, // constant, known good value | |
| 269 | else => return error.OSVersionDetectionFail, | |
| 270 | } | |
| 269 | 271 | |
| 270 | 272 | switch (builtin.target.os.tag) { |
| 271 | 273 | .freebsd => { |
lib/std/zig/system/darwin/macos.zig+9-6| ... | ... | @@ -2,6 +2,7 @@ const builtin = @import("builtin"); |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | 4 | const Io = std.Io; |
| 5 | const posix = std.posix; | |
| 5 | 6 | const assert = std.debug.assert; |
| 6 | 7 | const mem = std.mem; |
| 7 | 8 | const testing = std.testing; |
| ... | ... | @@ -399,12 +400,14 @@ test "detect" { |
| 399 | 400 | pub fn detectNativeCpuAndFeatures() ?Target.Cpu { |
| 400 | 401 | var cpu_family: std.c.CPUFAMILY = undefined; |
| 401 | 402 | var len: usize = @sizeOf(std.c.CPUFAMILY); |
| 402 | std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch |err| switch (err) { | |
| 403 | error.PermissionDenied => unreachable, // only when setting values, | |
| 404 | error.SystemResources => unreachable, // memory already on the stack | |
| 405 | error.UnknownName => unreachable, // constant, known good value | |
| 406 | error.Unexpected => unreachable, // EFAULT: stack should be safe, EISDIR/ENOTDIR: constant, known good value | |
| 407 | }; | |
| 403 | switch (posix.errno(posix.system.sysctlbyname("hw.cpufamily", &cpu_family, &len, null, 0))) { | |
| 404 | .SUCCESS => {}, | |
| 405 | .FAULT => unreachable, // segmentation fault | |
| 406 | .PERM => unreachable, // only when setting values, | |
| 407 | .NOMEM => unreachable, // memory already on the stack | |
| 408 | .NOENT => unreachable, // constant, known good value | |
| 409 | else => unreachable, | |
| 410 | } | |
| 408 | 411 | |
| 409 | 412 | const current_arch = builtin.cpu.arch; |
| 410 | 413 | switch (current_arch) { |