Skip to content

Commit

Permalink
std.os.linux -> std.posix.system
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed May 13, 2024
1 parent b5cf9ac commit 933a75e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 92 deletions.
11 changes: 5 additions & 6 deletions src/boot.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const os = std.os;
const posix = std.posix;
const E = std.os.linux.E;
const system = std.posix.system;

const linux_headers = @import("linux_headers");

Expand Down Expand Up @@ -146,7 +145,7 @@ pub fn kexecLoad(
defer allocator.free(cmdline);
const cmdline_len = cmdline.len + 1;

const rc = os.linux.syscall5(
const rc = system.syscall5(
.kexec_file_load,
linux_fd,
@as(usize, @bitCast(@as(isize, initrd_fd))),
Expand Down Expand Up @@ -325,12 +324,12 @@ pub const Autoboot = struct {
}

pub fn register(self: *@This(), epoll_fd: posix.fd_t) !void {
var ready_event = os.linux.epoll_event{
var ready_event = system.epoll_event{
.data = .{ .fd = self.ready_fd },
// we will only be ready to boot once
.events = os.linux.EPOLL.IN | os.linux.EPOLL.ONESHOT,
.events = system.EPOLL.IN | system.EPOLL.ONESHOT,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, self.ready_fd, &ready_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, self.ready_fd, &ready_event);
}

pub fn start(self: *@This()) !void {
Expand Down
14 changes: 7 additions & 7 deletions src/boot/bls.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const os = std.os;
const posix = std.posix;
const system = std.posix.system;

const BootDevice = @import("../boot.zig").BootDevice;
const BootEntry = @import("../boot.zig").BootEntry;
Expand Down Expand Up @@ -248,13 +248,13 @@ pub const BootLoaderSpec = struct {

const mountpoint = try mountpoint_dir.realpathAlloc(allocator, partition_filename);

switch (posix.errno(os.linux.mount(
switch (posix.errno(system.mount(
try allocator.dupeZ(u8, try dev_disk_alias.realpathAlloc(allocator, partition_filename)),
try allocator.dupeZ(u8, mountpoint),
switch (fstype) {
.Vfat => "vfat",
},
os.linux.MS.NOSUID | os.linux.MS.NODEV | os.linux.MS.NOEXEC,
system.MS.NOSUID | system.MS.NODEV | system.MS.NOEXEC,
0,
))) {
.SUCCESS => {},
Expand Down Expand Up @@ -418,18 +418,18 @@ pub const BootLoaderSpec = struct {

for (self.external_mounts) |mount| {
std.log.info("unmounted disk \"{s}\"", .{mount.disk_name});
_ = os.linux.umount2(
_ = system.umount2(
try self.arena.allocator().dupeZ(u8, try mount.dir.realpath(".", &buf)),
os.linux.MNT.DETACH,
system.MNT.DETACH,
);
mount.dir.close();
}

for (self.internal_mounts) |mount| {
std.log.info("unmounted disk \"{s}\"", .{mount.disk_name});
_ = os.linux.umount2(
_ = system.umount2(
try self.arena.allocator().dupeZ(u8, try mount.dir.realpath(".", &buf)),
os.linux.MNT.DETACH,
system.MNT.DETACH,
);
mount.dir.close();
}
Expand Down
41 changes: 21 additions & 20 deletions src/client.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const std = @import("std");
const os = std.os;
const posix = std.posix;
const process = std.process;
const system = std.posix.system;

const linux_headers = @import("linux_headers");

const ClientMsg = @import("./message.zig").ClientMsg;
const ServerMsg = @import("./message.zig").ServerMsg;
const kernelLogs = @import("./system.zig").kernelLogs;
const readMessage = @import("./message.zig").readMessage;
const setupTty = @import("./system.zig").setupTty;
const tmp = @import("./tmp.zig");
const writeMessage = @import("./message.zig").writeMessage;
const system = @import("./system.zig");
const xmodem_recv = @import("./xmodem.zig").xmodem_recv;
const tmp = @import("./tmp.zig");

pub const Client = struct {
waiting_for_response: bool = false,
Expand Down Expand Up @@ -77,31 +78,31 @@ pub const Client = struct {
const epoll_fd = try posix.epoll_create1(linux_headers.EPOLL_CLOEXEC);
defer posix.close(epoll_fd);

var stdin_event = os.linux.epoll_event{
var stdin_event = system.epoll_event{
.data = .{ .fd = posix.STDIN_FILENO },
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, posix.STDIN_FILENO, &stdin_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, posix.STDIN_FILENO, &stdin_event);

var server_event = os.linux.epoll_event{
var server_event = system.epoll_event{
.data = .{ .fd = self.stream.handle },
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, self.stream.handle, &server_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, self.stream.handle, &server_event);

const inotify_fd = try posix.inotify_init1(0);
const logs_watch_fd = try posix.inotify_add_watch(inotify_fd, "/run/log", os.linux.IN.MODIFY);
const logs_watch_fd = try posix.inotify_add_watch(inotify_fd, "/run/log", system.IN.MODIFY);
defer posix.close(inotify_fd);
var inotify_event = os.linux.epoll_event{
var inotify_event = system.epoll_event{
.data = .{ .fd = inotify_fd },
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, inotify_fd, &inotify_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, inotify_fd, &inotify_event);

// main event loop
while (true) {
const max_events = 8; // arbitrary
var events = [_]os.linux.epoll_event{undefined} ** max_events;
var events = [_]system.epoll_event{undefined} ** max_events;

const n_events = posix.epoll_wait(epoll_fd, &events, -1);

Expand All @@ -113,7 +114,7 @@ pub const Client = struct {
// the client will no longer need to passively watch logs, so
// we remove the inotify watcher.
if (event.data.fd != inotify_fd and self.watching_logs) {
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_DEL, inotify_fd, null);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_DEL, inotify_fd, null);
posix.inotify_rm_watch(inotify_fd, logs_watch_fd);
self.watching_logs = false;
}
Expand All @@ -136,7 +137,7 @@ pub const Client = struct {
// actually use the data since we only have one file
// registered. If we don't do this, we will continue to
// get epoll notifications for this fd.
var buf: [@sizeOf(os.linux.inotify_event)]u8 = undefined;
var buf: [@sizeOf(system.inotify_event)]u8 = undefined;
_ = posix.read(inotify_fd, &buf) catch {};
try self.printLogs(.{ .from_start = false });
}
Expand Down Expand Up @@ -593,7 +594,7 @@ pub const Command = struct {

fn run(c: *Command, args: *ArgsIterator) !?ClientMsg {
const filter = if (args.next()) |filter_str| try std.fmt.parseInt(u8, filter_str, 10) else 6;
const kernel_logs = try system.kernelLogs(c.allocator, filter);
const kernel_logs = try kernelLogs(c.allocator, filter);
defer c.allocator.free(kernel_logs);
try c.shell_instance.writeAllAndFlush(kernel_logs);

Expand Down Expand Up @@ -639,9 +640,9 @@ pub const Command = struct {
fn run(c: *Command, args: *ArgsIterator) !?ClientMsg {
var tmp_dir = try tmp.tmpDir(.{});

errdefer system.setupTty(posix.STDIN_FILENO, .user_input) catch {};
errdefer setupTty(posix.STDIN_FILENO, .user_input) catch {};

try system.setupTty(posix.STDIN_FILENO, .file_transfer_recv);
try setupTty(posix.STDIN_FILENO, .file_transfer_recv);

const kernel_bytes = try xmodem_recv(c.allocator, posix.STDIN_FILENO);
// Free up the kernel bytes since this is large.
Expand Down Expand Up @@ -679,7 +680,7 @@ pub const Command = struct {
defer params_file.close();
try params_file.writeAll(kernel_params);

system.setupTty(posix.STDIN_FILENO, .user_input) catch {};
setupTty(posix.STDIN_FILENO, .user_input) catch {};

return .{ .data = .{ .Boot = .{
.Dir = try tmp_dir.dir.realpathAlloc(c.allocator, "."),
Expand Down
26 changes: 12 additions & 14 deletions src/device.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const std = @import("std");
const os = std.os;
const posix = std.posix;
const path = std.fs.path;
const linux = os.linux;
const system = posix.system;

const linux_headers = @import("linux_headers");
Expand Down Expand Up @@ -167,21 +165,21 @@ pub const DeviceWatcher = struct {
}

pub fn register(self: *@This(), epoll_fd: posix.fd_t) !void {
var device_event = os.linux.epoll_event{
var device_event = system.epoll_event{
.data = .{ .fd = self.nl_fd },
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, self.nl_fd, &device_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, self.nl_fd, &device_event);

var timer_event = os.linux.epoll_event{
var timer_event = system.epoll_event{
.data = .{ .fd = self.settle_fd },
.events = os.linux.EPOLL.IN | os.linux.EPOLL.ONESHOT,
.events = system.EPOLL.IN | system.EPOLL.ONESHOT,
};
try posix.epoll_ctl(epoll_fd, os.linux.EPOLL.CTL_ADD, self.settle_fd, &timer_event);
try posix.epoll_ctl(epoll_fd, system.EPOLL.CTL_ADD, self.settle_fd, &timer_event);
}

pub fn start_settle_timer(self: *@This()) !void {
const timerspec = os.linux.itimerspec{
const timerspec = system.itimerspec{
// oneshot
.it_interval = .{ .tv_sec = 0, .tv_nsec = 0 },
// consider settled after 2 seconds without any new events
Expand Down Expand Up @@ -448,10 +446,10 @@ pub const DeviceWatcher = struct {
};

test "device mode" {
try std.testing.expectEqual(@as(u32, linux.S.IFCHR), special(null));
try std.testing.expectEqual(@as(u32, linux.S.IFCHR), special("foo"));
try std.testing.expectEqual(@as(u32, linux.S.IFBLK), special("disk"));
try std.testing.expectEqual(@as(u32, linux.S.IFBLK), special("partition"));
try std.testing.expectEqual(@as(u32, system.S.IFCHR), special(null));
try std.testing.expectEqual(@as(u32, system.S.IFCHR), special("foo"));
try std.testing.expectEqual(@as(u32, system.S.IFBLK), special("disk"));
try std.testing.expectEqual(@as(u32, system.S.IFBLK), special("partition"));
}

test "uevent file content parsing" {
Expand Down Expand Up @@ -610,7 +608,7 @@ pub fn findActiveConsoles(allocator: std.mem.Allocator) ![]posix.fd_t {
fn serialDeviceIsConnected(fd: posix.fd_t) bool {
var serial: c_int = 0;

if (os.linux.ioctl(fd, linux_headers.TIOCMGET, @intFromPtr(&serial)) != 0) {
if (system.ioctl(fd, linux_headers.TIOCMGET, @intFromPtr(&serial)) != 0) {
return false;
}

Expand Down
4 changes: 0 additions & 4 deletions src/log.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const std = @import("std");
const os = std.os;
const posix = std.posix;
const O = std.os.O;
const S = std.os.S;

var log_file: ?std.fs.File = null;

Expand Down
18 changes: 8 additions & 10 deletions src/server.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const std = @import("std");
const builtin = @import("builtin");
const os = std.os;
const posix = std.posix;
const system = std.posix.system;

const system = @import("./system.zig");
const BootEntry = @import("./boot.zig").BootEntry;
const kexecLoad = @import("./boot.zig").kexecLoad;
const kexecLoadFromDir = @import("./boot.zig").kexecLoadFromDir;
const ClientMsg = @import("./message.zig").ClientMsg;
const ServerMsg = @import("./message.zig").ServerMsg;
const kexecLoad = @import("./boot.zig").kexecLoad;
const kexecLoadFromDir = @import("./boot.zig").kexecLoadFromDir;
const readMessage = @import("./message.zig").readMessage;
const writeMessage = @import("./message.zig").writeMessage;

Expand All @@ -34,10 +32,10 @@ pub const Server = struct {
pub fn register_self(self: *@This(), epoll_fd: posix.fd_t) !void {
try posix.epoll_ctl(
epoll_fd,
os.linux.EPOLL.CTL_ADD,
system.EPOLL.CTL_ADD,
self.inner.stream.handle,
@constCast(&.{
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
.data = .{ .fd = self.inner.stream.handle },
}),
);
Expand All @@ -48,10 +46,10 @@ pub const Server = struct {

try posix.epoll_ctl(
epoll_fd,
os.linux.EPOLL.CTL_ADD,
system.EPOLL.CTL_ADD,
client_stream.handle,
@constCast(&.{
.events = os.linux.EPOLL.IN,
.events = system.EPOLL.IN,
.data = .{ .fd = client_stream.handle },
}),
);
Expand All @@ -63,7 +61,7 @@ pub const Server = struct {
}
}

pub fn handle_new_event(self: *@This(), event: os.linux.epoll_event) !?posix.RebootCommand {
pub fn handle_new_event(self: *@This(), event: system.epoll_event) !?posix.RebootCommand {
const client = b: {
for (self.clients.items) |client| {
if (event.data.fd == client.handle) {
Expand Down
23 changes: 10 additions & 13 deletions src/system.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const fs = std.fs;
const os = std.os;
const posix = std.posix;
const system = std.posix.system;
const linux = std.os.linux;

const linux_headers = @import("linux_headers");

Expand All @@ -17,7 +14,7 @@ fn mountPseudoFs(
fstype: [*:0]const u8,
flags: u32,
) MountError!void {
const rc = linux.mount("", path, fstype, flags, 0);
const rc = system.mount("", path, fstype, flags, 0);

switch (posix.errno(rc)) {
.SUCCESS => {},
Expand All @@ -29,18 +26,18 @@ fn mountPseudoFs(
/// Does initial system setup and mounts basic psuedo-filesystems.
pub fn setupSystem() !void {
try fs.makeDirAbsolute("/proc");
try mountPseudoFs("/proc", "proc", linux.MS.NOSUID | linux.MS.NODEV | linux.MS.NOEXEC);
try mountPseudoFs("/proc", "proc", system.MS.NOSUID | system.MS.NODEV | system.MS.NOEXEC);

try fs.makeDirAbsolute("/sys");
try mountPseudoFs("/sys", "sysfs", linux.MS.NOSUID | linux.MS.NODEV | linux.MS.NOEXEC | linux.MS.RELATIME);
try mountPseudoFs("/sys/kernel/security", "securityfs", linux.MS.NOSUID | linux.MS.NODEV | linux.MS.NOEXEC | linux.MS.RELATIME);
try mountPseudoFs("/sys/kernel/debug", "debugfs", linux.MS.NOSUID | linux.MS.NODEV | linux.MS.NOEXEC | linux.MS.RELATIME);
try mountPseudoFs("/sys", "sysfs", system.MS.NOSUID | system.MS.NODEV | system.MS.NOEXEC | system.MS.RELATIME);
try mountPseudoFs("/sys/kernel/security", "securityfs", system.MS.NOSUID | system.MS.NODEV | system.MS.NOEXEC | system.MS.RELATIME);
try mountPseudoFs("/sys/kernel/debug", "debugfs", system.MS.NOSUID | system.MS.NODEV | system.MS.NOEXEC | system.MS.RELATIME);

// we use CONFIG_DEVTMPFS, so we don't need to create /dev
try mountPseudoFs("/dev", "devtmpfs", linux.MS.SILENT | linux.MS.NOSUID | linux.MS.NOEXEC);
try mountPseudoFs("/dev", "devtmpfs", system.MS.SILENT | system.MS.NOSUID | system.MS.NOEXEC);

try fs.makeDirAbsolute("/run");
try mountPseudoFs("/run", "tmpfs", linux.MS.NOSUID | linux.MS.NODEV);
try mountPseudoFs("/run", "tmpfs", system.MS.NOSUID | system.MS.NODEV);

try fs.makeDirAbsolute("/mnt");

Expand Down Expand Up @@ -182,12 +179,12 @@ const SYSLOG_ACTION_UNREAD = 9;
/// Read kernel logs (AKA syslog/dmesg). Caller is responsible for returned
/// slice.
pub fn kernelLogs(allocator: std.mem.Allocator, filter: u8) ![]const u8 {
const bytes_available = linux.syscall3(linux.SYS.syslog, SYSLOG_ACTION_UNREAD, 0, 0);
const bytes_available = system.syscall3(system.SYS.syslog, SYSLOG_ACTION_UNREAD, 0, 0);
const buf = try allocator.alloc(u8, bytes_available);
defer allocator.free(buf);

switch (posix.errno(linux.syscall3(
linux.SYS.syslog,
switch (posix.errno(system.syscall3(
system.SYS.syslog,
SYSLOG_ACTION_READ_ALL,
@intFromPtr(buf.ptr),
buf.len,
Expand Down
Loading

0 comments on commit 933a75e

Please sign in to comment.