Skip to content

Commit c65b248

Browse files
author
Jan Philipp Hafer
committed
std.os: spring-cleanup and specify organization
- Alphabetically sort things, where reasonable. - Document, that **only** non-portable posix things belong into posix.zig * If there is a portable abstraction, do not offer one in posix.zig * Reason: Prevent useless abstractions and needless strong coupling. - Move posix-only functions into posix.zig, which have either incompatible or more extensive execution semantics than their counterparts and can be grouped into * File permission system * Process management * Memory management * IPC * Signaling Work on #6600.
1 parent 4ea2f44 commit c65b248

20 files changed

+990
-934
lines changed

lib/std/Thread.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ const LinuxThreadImpl = struct {
946946
// map all memory needed without read/write permissions
947947
// to avoid committing the whole region right away
948948
// anonymous mapping ensures file descriptor limits are not exceeded
949-
const mapped = os.mmap(
949+
const mapped = os.posix.mmap(
950950
null,
951951
map_bytes,
952952
os.PROT.NONE,
@@ -962,7 +962,7 @@ const LinuxThreadImpl = struct {
962962
else => |e| return e,
963963
};
964964
assert(mapped.len >= map_bytes);
965-
errdefer os.munmap(mapped);
965+
errdefer os.posix.munmap(mapped);
966966

967967
// map everything but the guard page as read/write
968968
os.mprotect(
@@ -1035,7 +1035,7 @@ const LinuxThreadImpl = struct {
10351035
}
10361036

10371037
fn join(self: Impl) void {
1038-
defer os.munmap(self.thread.mapped);
1038+
defer os.posix.munmap(self.thread.mapped);
10391039

10401040
var spin: u8 = 10;
10411041
while (true) {

lib/std/child_process.zig

+19-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const unicode = std.unicode;
55
const io = std.io;
66
const fs = std.fs;
77
const os = std.os;
8+
const posix = os.posix;
89
const process = std.process;
910
const File = std.fs.File;
1011
const windows = os.windows;
@@ -70,7 +71,7 @@ pub const ChildProcess = struct {
7071
/// Darwin-only. Start child process in suspended state as if SIGSTOP was sent.
7172
start_suspended: bool = false,
7273

73-
pub const Arg0Expand = os.Arg0Expand;
74+
pub const Arg0Expand = os.posix.Arg0Expand;
7475

7576
pub const SpawnError = error{
7677
OutOfMemory,
@@ -86,8 +87,8 @@ pub const ChildProcess = struct {
8687
/// Windows-only. `cwd` was provided, but the path did not exist when spawning the child process.
8788
CurrentWorkingDirectoryUnlinked,
8889
} ||
89-
os.ExecveError ||
90-
os.SetIdError ||
90+
os.posix.ExecveError ||
91+
os.posix.SetIdError ||
9192
os.ChangeCurDirError ||
9293
windows.CreateProcessError ||
9394
windows.WaitForSingleObjectError;
@@ -179,7 +180,7 @@ pub const ChildProcess = struct {
179180
self.cleanupStreams();
180181
return term;
181182
}
182-
try os.kill(self.id, os.SIG.TERM);
183+
try posix.kill(self.pid, os.SIG.TERM);
183184
try self.waitUnwrapped();
184185
return self.term.?;
185186
}
@@ -309,7 +310,7 @@ pub const ChildProcess = struct {
309310
return term;
310311
}
311312

312-
try self.waitUnwrapped();
313+
try self.waitUnwrappedPosix();
313314
return self.term.?;
314315
}
315316

@@ -331,8 +332,8 @@ pub const ChildProcess = struct {
331332
return result;
332333
}
333334

334-
fn waitUnwrapped(self: *ChildProcess) !void {
335-
const res: os.WaitPidResult = os.waitpid(self.id, 0);
335+
fn waitUnwrappedPosix(self: *ChildProcess) !void {
336+
const res: os.posix.WaitPidResult = os.posix.waitpid(self.id, 0);
336337
const status = res.status;
337338
self.cleanupStreams();
338339
self.handleWaitResult(status);
@@ -410,17 +411,17 @@ pub const ChildProcess = struct {
410411

411412
fn spawnPosix(self: *ChildProcess) SpawnError!void {
412413
const pipe_flags = if (io.is_async) os.O.NONBLOCK else 0;
413-
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try os.pipe2(pipe_flags) else undefined;
414+
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try os.posix.pipe2(pipe_flags) else undefined;
414415
errdefer if (self.stdin_behavior == StdIo.Pipe) {
415416
destroyPipe(stdin_pipe);
416417
};
417418

418-
const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try os.pipe2(pipe_flags) else undefined;
419+
const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try os.posix.pipe2(pipe_flags) else undefined;
419420
errdefer if (self.stdout_behavior == StdIo.Pipe) {
420421
destroyPipe(stdout_pipe);
421422
};
422423

423-
const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try os.pipe2(pipe_flags) else undefined;
424+
const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try os.posix.pipe2(pipe_flags) else undefined;
424425
errdefer if (self.stderr_behavior == StdIo.Pipe) {
425426
destroyPipe(stderr_pipe);
426427
};
@@ -485,12 +486,12 @@ pub const ChildProcess = struct {
485486
// end with eventfd
486487
break :blk [2]os.fd_t{ fd, fd };
487488
} else {
488-
break :blk try os.pipe2(os.O.CLOEXEC);
489+
break :blk try os.posix.pipe2(os.O.CLOEXEC);
489490
}
490491
};
491492
errdefer destroyPipe(err_pipe);
492493

493-
const pid_result = try os.fork();
494+
const pid_result = try os.posix.fork();
494495
if (pid_result == 0) {
495496
// we are the child
496497
setUpChildIo(self.stdin_behavior, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
@@ -517,16 +518,16 @@ pub const ChildProcess = struct {
517518
}
518519

519520
if (self.gid) |gid| {
520-
os.setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err);
521+
os.posix.setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err);
521522
}
522523

523524
if (self.uid) |uid| {
524-
os.setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
525+
os.posix.setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
525526
}
526527

527528
const err = switch (self.expand_arg0) {
528-
.expand => os.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
529-
.no_expand => os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
529+
.expand => os.posix.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
530+
.no_expand => os.posix.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp),
530531
};
531532
forkChildErrReport(err_pipe[1], err);
532533
}
@@ -832,10 +833,10 @@ pub const ChildProcess = struct {
832833

833834
fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void {
834835
switch (stdio) {
835-
.Pipe => try os.dup2(pipe_fd, std_fileno),
836+
.Pipe => try os.posix.dup2(pipe_fd, std_fileno),
836837
.Close => os.close(std_fileno),
837838
.Inherit => {},
838-
.Ignore => try os.dup2(dev_null_fd, std_fileno),
839+
.Ignore => try os.posix.dup2(dev_null_fd, std_fileno),
839840
}
840841
}
841842
};

lib/std/crypto/tlcsprng.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
7575
if (want_fork_safety and maybe_have_wipe_on_fork or is_haiku) {
7676
// Allocate a per-process page, madvise operates with page
7777
// granularity.
78-
wipe_mem = os.mmap(
78+
wipe_mem = os.posix.mmap(
7979
null,
8080
@sizeOf(Context),
8181
os.PROT.READ | os.PROT.WRITE,
@@ -111,11 +111,11 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
111111
// Qemu user-mode emulation ignores any valid/invalid madvise
112112
// hint and returns success. Check if this is the case by
113113
// passing bogus parameters, we expect EINVAL as result.
114-
if (os.madvise(wipe_mem.ptr, 0, 0xffffffff)) |_| {
114+
if (os.posix.madvise(wipe_mem.ptr, 0, 0xffffffff)) |_| {
115115
break :wof;
116116
} else |_| {}
117117

118-
if (os.madvise(wipe_mem.ptr, wipe_mem.len, os.MADV.WIPEONFORK)) |_| {
118+
if (os.posix.madvise(wipe_mem.ptr, wipe_mem.len, os.MADV.WIPEONFORK)) |_| {
119119
return initAndFill(buffer);
120120
} else |_| {}
121121
}

lib/std/debug.zig

+10-10
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ pub const StackIterator = struct {
482482

483483
if (native_os != .windows) {
484484
if (native_os != .wasi) {
485-
os.msync(aligned_memory, os.MSF.ASYNC) catch |err| {
485+
os.posix.msync(aligned_memory, os.MSF.ASYNC) catch |err| {
486486
switch (err) {
487-
os.MSyncError.UnmappedMemory => {
487+
os.posix.MSyncError.UnmappedMemory => {
488488
return false;
489489
},
490490
else => unreachable,
@@ -1222,15 +1222,15 @@ fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
12221222
defer file.close();
12231223

12241224
const file_len = math.cast(usize, try file.getEndPos()) orelse math.maxInt(usize);
1225-
const mapped_mem = try os.mmap(
1225+
const mapped_mem = try os.posix.mmap(
12261226
null,
12271227
file_len,
12281228
os.PROT.READ,
12291229
os.MAP.SHARED,
12301230
file.handle,
12311231
0,
12321232
);
1233-
errdefer os.munmap(mapped_mem);
1233+
errdefer os.posix.munmap(mapped_mem);
12341234

12351235
return mapped_mem;
12361236
}
@@ -1491,7 +1491,7 @@ pub const ModuleDebugInfo = switch (native_os) {
14911491
}
14921492
self.ofiles.deinit();
14931493
allocator.free(self.symbols);
1494-
os.munmap(self.mapped_memory);
1494+
os.posix.munmap(self.mapped_memory);
14951495
}
14961496

14971497
fn loadOFile(self: *@This(), allocator: mem.Allocator, o_file_path: []const u8) !OFileInfo {
@@ -1798,7 +1798,7 @@ pub const ModuleDebugInfo = switch (native_os) {
17981798

17991799
fn deinit(self: *@This(), allocator: mem.Allocator) void {
18001800
self.dwarf.deinit(allocator);
1801-
os.munmap(self.mapped_memory);
1801+
os.posix.munmap(self.mapped_memory);
18021802
}
18031803

18041804
pub fn getSymbolAtAddress(self: *@This(), allocator: mem.Allocator, address: usize) !SymbolInfo {
@@ -1880,10 +1880,10 @@ pub fn maybeEnableSegfaultHandler() void {
18801880
var windows_segfault_handle: ?windows.HANDLE = null;
18811881

18821882
pub fn updateSegfaultHandler(act: ?*const os.Sigaction) error{OperationNotSupported}!void {
1883-
try os.sigaction(os.SIG.SEGV, act, null);
1884-
try os.sigaction(os.SIG.ILL, act, null);
1885-
try os.sigaction(os.SIG.BUS, act, null);
1886-
try os.sigaction(os.SIG.FPE, act, null);
1883+
try os.posix.sigaction(os.SIG.SEGV, act, null);
1884+
try os.posix.sigaction(os.SIG.ILL, act, null);
1885+
try os.posix.sigaction(os.SIG.BUS, act, null);
1886+
try os.posix.sigaction(os.SIG.FPE, act, null);
18871887
}
18881888

18891889
/// Attaches a global SIGSEGV handler which calls @panic("segmentation fault");

lib/std/dynamic_library.zig

+7-7
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ pub const ElfDynLib = struct {
123123

124124
// This one is to read the ELF info. We do more mmapping later
125125
// corresponding to the actual LOAD sections.
126-
const file_bytes = try os.mmap(
126+
const file_bytes = try os.posix.mmap(
127127
null,
128128
mem.alignForward(size, mem.page_size),
129129
os.PROT.READ,
130130
os.MAP.PRIVATE,
131131
fd,
132132
0,
133133
);
134-
defer os.munmap(file_bytes);
134+
defer os.posix.munmap(file_bytes);
135135

136136
const eh = @ptrCast(*elf.Ehdr, file_bytes.ptr);
137137
if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
@@ -161,15 +161,15 @@ pub const ElfDynLib = struct {
161161
const dynv = maybe_dynv orelse return error.MissingDynamicLinkingInformation;
162162

163163
// Reserve the entire range (with no permissions) so that we can do MAP.FIXED below.
164-
const all_loaded_mem = try os.mmap(
164+
const all_loaded_mem = try os.posix.mmap(
165165
null,
166166
virt_addr_end,
167167
os.PROT.NONE,
168168
os.MAP.PRIVATE | os.MAP.ANONYMOUS,
169169
-1,
170170
0,
171171
);
172-
errdefer os.munmap(all_loaded_mem);
172+
errdefer os.posix.munmap(all_loaded_mem);
173173

174174
const base = @ptrToInt(all_loaded_mem.ptr);
175175

@@ -193,7 +193,7 @@ pub const ElfDynLib = struct {
193193
const prot = elfToMmapProt(ph.p_flags);
194194
if ((ph.p_flags & elf.PF_W) == 0) {
195195
// If it does not need write access, it can be mapped from the fd.
196-
_ = try os.mmap(
196+
_ = try os.posix.mmap(
197197
ptr,
198198
extended_memsz,
199199
prot,
@@ -202,7 +202,7 @@ pub const ElfDynLib = struct {
202202
ph.p_offset - extra_bytes,
203203
);
204204
} else {
205-
const sect_mem = try os.mmap(
205+
const sect_mem = try os.posix.mmap(
206206
ptr,
207207
extended_memsz,
208208
prot,
@@ -256,7 +256,7 @@ pub const ElfDynLib = struct {
256256

257257
/// Trusts the file
258258
pub fn close(self: *ElfDynLib) void {
259-
os.munmap(self.memory);
259+
os.posix.munmap(self.memory);
260260
self.* = undefined;
261261
}
262262

lib/std/fs.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ pub const Dir = struct {
11941194
}
11951195

11961196
if (has_flock_open_flags and flags.lock_nonblocking) {
1197-
var fl_flags = os.fcntl(fd, os.F.GETFL, 0) catch |err| switch (err) {
1197+
var fl_flags = os.posix.fcntl(fd, os.F.GETFL, 0) catch |err| switch (err) {
11981198
error.FileBusy => unreachable,
11991199
error.Locked => unreachable,
12001200
error.PermissionDenied => unreachable,
@@ -1203,7 +1203,7 @@ pub const Dir = struct {
12031203
else => |e| return e,
12041204
};
12051205
fl_flags &= ~@as(usize, os.O.NONBLOCK);
1206-
_ = os.fcntl(fd, os.F.SETFL, fl_flags) catch |err| switch (err) {
1206+
_ = os.posix.fcntl(fd, os.F.SETFL, fl_flags) catch |err| switch (err) {
12071207
error.FileBusy => unreachable,
12081208
error.Locked => unreachable,
12091209
error.PermissionDenied => unreachable,
@@ -1350,7 +1350,7 @@ pub const Dir = struct {
13501350
}
13511351

13521352
if (has_flock_open_flags and flags.lock_nonblocking) {
1353-
var fl_flags = os.fcntl(fd, os.F.GETFL, 0) catch |err| switch (err) {
1353+
var fl_flags = os.posix.fcntl(fd, os.F.GETFL, 0) catch |err| switch (err) {
13541354
error.FileBusy => unreachable,
13551355
error.Locked => unreachable,
13561356
error.PermissionDenied => unreachable,
@@ -1359,7 +1359,7 @@ pub const Dir = struct {
13591359
else => |e| return e,
13601360
};
13611361
fl_flags &= ~@as(usize, os.O.NONBLOCK);
1362-
_ = os.fcntl(fd, os.F.SETFL, fl_flags) catch |err| switch (err) {
1362+
_ = os.posix.fcntl(fd, os.F.SETFL, fl_flags) catch |err| switch (err) {
13631363
error.FileBusy => unreachable,
13641364
error.Locked => unreachable,
13651365
error.PermissionDenied => unreachable,

lib/std/fs/file.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -399,25 +399,25 @@ pub const File = struct {
399399
return Stat.fromSystem(st);
400400
}
401401

402-
pub const ChmodError = std.os.FChmodError;
402+
pub const ChmodError = std.os.posix.FChmodError;
403403

404404
/// Changes the mode of the file.
405405
/// The process must have the correct privileges in order to do this
406406
/// successfully, or must have the effective user ID matching the owner
407407
/// of the file.
408408
pub fn chmod(self: File, new_mode: Mode) ChmodError!void {
409-
try os.fchmod(self.handle, new_mode);
409+
try os.posix.fchmod(self.handle, new_mode);
410410
}
411411

412-
pub const ChownError = std.os.FChownError;
412+
pub const ChownError = std.os.posix.FChownError;
413413

414414
/// Changes the owner and group of the file.
415415
/// The process must have the correct privileges in order to do this
416416
/// successfully. The group may be changed by the owner of the file to
417417
/// any group of which the owner is a member. If the owner or group is
418418
/// specified as `null`, the ID is not changed.
419419
pub fn chown(self: File, owner: ?Uid, group: ?Gid) ChownError!void {
420-
try os.fchown(self.handle, owner, group);
420+
try os.posix.fchown(self.handle, owner, group);
421421
}
422422

423423
/// Cross-platform representation of permissions on a file.
@@ -899,7 +899,7 @@ pub const File = struct {
899899
};
900900
}
901901

902-
pub const UpdateTimesError = os.FutimensError || windows.SetFileTimeError;
902+
pub const UpdateTimesError = os.posix.FutimensError || windows.SetFileTimeError;
903903

904904
/// The underlying file system may have a different granularity than nanoseconds,
905905
/// and therefore this function cannot guarantee any precision will be stored.
@@ -928,7 +928,7 @@ pub const File = struct {
928928
.tv_nsec = math.cast(isize, @mod(mtime, std.time.ns_per_s)) orelse maxInt(isize),
929929
},
930930
};
931-
try os.futimens(self.handle, &times);
931+
try os.posix.futimens(self.handle, &times);
932932
}
933933

934934
/// Reads all the bytes from the current position to the end of the file.

0 commit comments

Comments
 (0)