Skip to content

Commit 5172438

Browse files
author
Jan Philipp Hafer
committed
move over other posix functions from std.os
1 parent c19c049 commit 5172438

File tree

2 files changed

+251
-235
lines changed

2 files changed

+251
-235
lines changed

lib/std/os.zig

Lines changed: 7 additions & 233 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ pub fn abort() noreturn {
441441
raise(SIG.ABRT) catch {};
442442

443443
// Disable all signal handlers.
444-
sigprocmask(SIG.BLOCK, &linux.all_mask, null);
444+
posix.sigprocmask(SIG.BLOCK, &linux.all_mask, null);
445445

446446
// Only one thread may proceed to the rest of abort().
447447
if (!builtin.single_threaded) {
@@ -457,14 +457,14 @@ pub fn abort() noreturn {
457457
.mask = empty_sigset,
458458
.flags = 0,
459459
};
460-
sigaction(SIG.ABRT, &sigact, null) catch |err| switch (err) {
460+
posix.sigaction(SIG.ABRT, &sigact, null) catch |err| switch (err) {
461461
error.OperationNotSupported => unreachable,
462462
};
463463

464464
_ = linux.tkill(linux.gettid(), SIG.ABRT);
465465

466466
const sigabrtmask: linux.sigset_t = [_]u32{0} ** 31 ++ [_]u32{1 << (SIG.ABRT - 1)};
467-
sigprocmask(SIG.UNBLOCK, &sigabrtmask, null);
467+
posix.sigprocmask(SIG.UNBLOCK, &sigabrtmask, null);
468468

469469
// Beyond this point should be unreachable.
470470
@intToPtr(*allowzero volatile u8, 0).* = 0;
@@ -503,13 +503,13 @@ pub fn raise(sig: u8) RaiseError!void {
503503
if (builtin.os.tag == .linux) {
504504
var set: sigset_t = undefined;
505505
// block application signals
506-
sigprocmask(SIG.BLOCK, &linux.app_mask, &set);
506+
posix.sigprocmask(SIG.BLOCK, &linux.app_mask, &set);
507507

508508
const tid = linux.gettid();
509509
const rc = linux.tkill(tid, sig);
510510

511511
// restore signal mask
512-
sigprocmask(SIG.SETMASK, &set, null);
512+
posix.sigprocmask(SIG.SETMASK, &set, null);
513513

514514
switch (errno(rc)) {
515515
.SUCCESS => return,
@@ -4922,99 +4922,6 @@ pub fn unexpectedErrno(err: E) UnexpectedError {
49224922
return error.Unexpected;
49234923
}
49244924

4925-
pub const SigaltstackError = error{
4926-
/// The supplied stack size was less than MINSIGSTKSZ.
4927-
SizeTooSmall,
4928-
4929-
/// Attempted to change the signal stack while it was active.
4930-
PermissionDenied,
4931-
} || UnexpectedError;
4932-
4933-
pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
4934-
switch (errno(system.sigaltstack(ss, old_ss))) {
4935-
.SUCCESS => return,
4936-
.FAULT => unreachable,
4937-
.INVAL => unreachable,
4938-
.NOMEM => return error.SizeTooSmall,
4939-
.PERM => return error.PermissionDenied,
4940-
else => |err| return unexpectedErrno(err),
4941-
}
4942-
}
4943-
4944-
/// Examine and change a signal action.
4945-
pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) error{OperationNotSupported}!void {
4946-
switch (errno(system.sigaction(sig, act, oact))) {
4947-
.SUCCESS => return,
4948-
.INVAL, .NOSYS => return error.OperationNotSupported,
4949-
else => unreachable,
4950-
}
4951-
}
4952-
4953-
/// Sets the thread signal mask.
4954-
pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) void {
4955-
switch (errno(system.sigprocmask(flags, set, oldset))) {
4956-
.SUCCESS => return,
4957-
.FAULT => unreachable,
4958-
.INVAL => unreachable,
4959-
else => unreachable,
4960-
}
4961-
}
4962-
4963-
pub const FutimensError = error{
4964-
/// times is NULL, or both tv_nsec values are UTIME_NOW, and either:
4965-
/// * the effective user ID of the caller does not match the owner
4966-
/// of the file, the caller does not have write access to the
4967-
/// file, and the caller is not privileged (Linux: does not have
4968-
/// either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability);
4969-
/// or,
4970-
/// * the file is marked immutable (see chattr(1)).
4971-
AccessDenied,
4972-
4973-
/// The caller attempted to change one or both timestamps to a value
4974-
/// other than the current time, or to change one of the timestamps
4975-
/// to the current time while leaving the other timestamp unchanged,
4976-
/// (i.e., times is not NULL, neither tv_nsec field is UTIME_NOW,
4977-
/// and neither tv_nsec field is UTIME_OMIT) and either:
4978-
/// * the caller's effective user ID does not match the owner of
4979-
/// file, and the caller is not privileged (Linux: does not have
4980-
/// the CAP_FOWNER capability); or,
4981-
/// * the file is marked append-only or immutable (see chattr(1)).
4982-
PermissionDenied,
4983-
4984-
ReadOnlyFileSystem,
4985-
} || UnexpectedError;
4986-
4987-
pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
4988-
if (builtin.os.tag == .wasi and !builtin.link_libc) {
4989-
// TODO WASI encodes `wasi.fstflags` to signify magic values
4990-
// similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore
4991-
// this here, but we should really handle it somehow.
4992-
const atim = times[0].toTimestamp();
4993-
const mtim = times[1].toTimestamp();
4994-
switch (wasi.fd_filestat_set_times(fd, atim, mtim, wasi.FILESTAT_SET_ATIM | wasi.FILESTAT_SET_MTIM)) {
4995-
.SUCCESS => return,
4996-
.ACCES => return error.AccessDenied,
4997-
.PERM => return error.PermissionDenied,
4998-
.BADF => unreachable, // always a race condition
4999-
.FAULT => unreachable,
5000-
.INVAL => unreachable,
5001-
.ROFS => return error.ReadOnlyFileSystem,
5002-
else => |err| return unexpectedErrno(err),
5003-
}
5004-
}
5005-
5006-
switch (errno(system.futimens(fd, times))) {
5007-
.SUCCESS => return,
5008-
.ACCES => return error.AccessDenied,
5009-
.PERM => return error.PermissionDenied,
5010-
.BADF => unreachable, // always a race condition
5011-
.FAULT => unreachable,
5012-
.INVAL => unreachable,
5013-
.ROFS => return error.ReadOnlyFileSystem,
5014-
else => |err| return unexpectedErrno(err),
5015-
}
5016-
}
5017-
50184925
pub const GetHostNameError = error{PermissionDenied} || UnexpectedError;
50194926

50204927
pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
@@ -5028,7 +4935,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
50284935
}
50294936
}
50304937
if (builtin.os.tag == .linux) {
5031-
const uts = uname();
4938+
const uts = posix.uname();
50324939
const hostname = mem.sliceTo(&uts.nodename, 0);
50334940
mem.copy(u8, name_buffer, hostname);
50344941
return name_buffer[0..hostname.len];
@@ -5037,15 +4944,6 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
50374944
@compileError("TODO implement gethostname for this OS");
50384945
}
50394946

5040-
pub fn uname() utsname {
5041-
var uts: utsname = undefined;
5042-
switch (errno(system.uname(&uts))) {
5043-
.SUCCESS => return uts,
5044-
.FAULT => unreachable,
5045-
else => unreachable,
5046-
}
5047-
}
5048-
50494947
pub fn res_mkquery(
50504948
op: u4,
50514949
dname: []const u8,
@@ -6111,48 +6009,6 @@ pub fn memfd_create(name: []const u8, flags: u32) !fd_t {
61116009
return memfd_createZ(&name_t, flags);
61126010
}
61136011

6114-
pub fn getrusage(who: i32) rusage {
6115-
var result: rusage = undefined;
6116-
const rc = system.getrusage(who, &result);
6117-
switch (errno(rc)) {
6118-
.SUCCESS => return result,
6119-
.INVAL => unreachable,
6120-
.FAULT => unreachable,
6121-
else => unreachable,
6122-
}
6123-
}
6124-
6125-
pub const TermiosGetError = error{NotATerminal} || UnexpectedError;
6126-
6127-
pub fn tcgetattr(handle: fd_t) TermiosGetError!termios {
6128-
while (true) {
6129-
var term: termios = undefined;
6130-
switch (errno(system.tcgetattr(handle, &term))) {
6131-
.SUCCESS => return term,
6132-
.INTR => continue,
6133-
.BADF => unreachable,
6134-
.NOTTY => return error.NotATerminal,
6135-
else => |err| return unexpectedErrno(err),
6136-
}
6137-
}
6138-
}
6139-
6140-
pub const TermiosSetError = TermiosGetError || error{ProcessOrphaned};
6141-
6142-
pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) TermiosSetError!void {
6143-
while (true) {
6144-
switch (errno(system.tcsetattr(handle, optional_action, &termios_p))) {
6145-
.SUCCESS => return,
6146-
.BADF => unreachable,
6147-
.INTR => continue,
6148-
.INVAL => unreachable,
6149-
.NOTTY => return error.NotATerminal,
6150-
.IO => return error.ProcessOrphaned,
6151-
else => |err| return unexpectedErrno(err),
6152-
}
6153-
}
6154-
}
6155-
61566012
pub const IoCtl_SIOCGIFINDEX_Error = error{
61576013
FileSystem,
61586014
InterfaceNotFound,
@@ -6299,88 +6155,6 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
62996155
}
63006156
}
63016157

6302-
pub const GetrlimitError = UnexpectedError;
6303-
6304-
pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
6305-
const getrlimit_sym = if (builtin.os.tag == .linux and builtin.link_libc)
6306-
system.getrlimit64
6307-
else
6308-
system.getrlimit;
6309-
6310-
var limits: rlimit = undefined;
6311-
switch (errno(getrlimit_sym(resource, &limits))) {
6312-
.SUCCESS => return limits,
6313-
.FAULT => unreachable, // bogus pointer
6314-
.INVAL => unreachable,
6315-
else => |err| return unexpectedErrno(err),
6316-
}
6317-
}
6318-
6319-
pub const SetrlimitError = error{ PermissionDenied, LimitTooBig } || UnexpectedError;
6320-
6321-
pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void {
6322-
const setrlimit_sym = if (builtin.os.tag == .linux and builtin.link_libc)
6323-
system.setrlimit64
6324-
else
6325-
system.setrlimit;
6326-
6327-
switch (errno(setrlimit_sym(resource, &limits))) {
6328-
.SUCCESS => return,
6329-
.FAULT => unreachable, // bogus pointer
6330-
.INVAL => return error.LimitTooBig, // this could also mean "invalid resource", but that would be unreachable
6331-
.PERM => return error.PermissionDenied,
6332-
else => |err| return unexpectedErrno(err),
6333-
}
6334-
}
6335-
6336-
pub const MadviseError = error{
6337-
/// advice is MADV.REMOVE, but the specified address range is not a shared writable mapping.
6338-
AccessDenied,
6339-
/// advice is MADV.HWPOISON, but the caller does not have the CAP_SYS_ADMIN capability.
6340-
PermissionDenied,
6341-
/// A kernel resource was temporarily unavailable.
6342-
SystemResources,
6343-
/// One of the following:
6344-
/// * addr is not page-aligned or length is negative
6345-
/// * advice is not valid
6346-
/// * advice is MADV.DONTNEED or MADV.REMOVE and the specified address range
6347-
/// includes locked, Huge TLB pages, or VM_PFNMAP pages.
6348-
/// * advice is MADV.MERGEABLE or MADV.UNMERGEABLE, but the kernel was not
6349-
/// configured with CONFIG_KSM.
6350-
/// * advice is MADV.FREE or MADV.WIPEONFORK but the specified address range
6351-
/// includes file, Huge TLB, MAP.SHARED, or VM_PFNMAP ranges.
6352-
InvalidSyscall,
6353-
/// (for MADV.WILLNEED) Paging in this area would exceed the process's
6354-
/// maximum resident set size.
6355-
WouldExceedMaximumResidentSetSize,
6356-
/// One of the following:
6357-
/// * (for MADV.WILLNEED) Not enough memory: paging in failed.
6358-
/// * Addresses in the specified range are not currently mapped, or
6359-
/// are outside the address space of the process.
6360-
OutOfMemory,
6361-
/// The madvise syscall is not available on this version and configuration
6362-
/// of the Linux kernel.
6363-
MadviseUnavailable,
6364-
/// The operating system returned an undocumented error code.
6365-
Unexpected,
6366-
};
6367-
6368-
/// Give advice about use of memory.
6369-
/// This syscall is optional and is sometimes configured to be disabled.
6370-
pub fn madvise(ptr: [*]align(mem.page_size) u8, length: usize, advice: u32) MadviseError!void {
6371-
switch (errno(system.madvise(ptr, length, advice))) {
6372-
.SUCCESS => return,
6373-
.ACCES => return error.AccessDenied,
6374-
.AGAIN => return error.SystemResources,
6375-
.BADF => unreachable, // The map exists, but the area maps something that isn't a file.
6376-
.INVAL => return error.InvalidSyscall,
6377-
.IO => return error.WouldExceedMaximumResidentSetSize,
6378-
.NOMEM => return error.OutOfMemory,
6379-
.NOSYS => return error.MadviseUnavailable,
6380-
else => |err| return unexpectedErrno(err),
6381-
}
6382-
}
6383-
63846158
pub const PerfEventOpenError = error{
63856159
/// Returned if the perf_event_attr size value is too small (smaller
63866160
/// than PERF_ATTR_SIZE_VER0), too big (larger than the page size),
@@ -6527,7 +6301,7 @@ pub fn maybeIgnoreSigpipe() void {
65276301
.mask = empty_sigset,
65286302
.flags = 0,
65296303
};
6530-
sigaction(SIG.PIPE, &act, null) catch |err|
6304+
posix.sigaction(SIG.PIPE, &act, null) catch |err|
65316305
std.debug.panic("failed to install noop SIGPIPE handler with '{s}'", .{@errorName(err)});
65326306
}
65336307
}

0 commit comments

Comments
 (0)