Skip to content

Commit

Permalink
build: update to Zig 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ifreund committed May 2, 2024
1 parent 84c6dbf commit 5b3f13d
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ git submodule update --init
To compile waylock first ensure that you have the following dependencies
installed:

- [zig](https://ziglang.org/download/) 0.11.0
- [zig](https://ziglang.org/download/) 0.12.0
- wayland
- wayland-protocols
- xkbcommon
Expand Down
17 changes: 8 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn build(b: *Build) !void {
if (mem.endsWith(u8, version, "-dev")) {
var ret: u8 = undefined;

const git_describe_long = b.execAllowFail(
const git_describe_long = b.runAllowFail(
&.{ "git", "-C", b.build_root.path orelse ".", "describe", "--long" },
&ret,
.Inherit,
Expand Down Expand Up @@ -88,32 +88,31 @@ pub fn build(b: *Build) !void {
scanner.generate("wp_viewporter", 1);
scanner.generate("wp_single_pixel_buffer_manager_v1", 1);

const wayland = b.createModule(.{ .source_file = scanner.result });
const xkbcommon = b.createModule(
.{ .source_file = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" } },
);
const wayland = b.createModule(.{ .root_source_file = scanner.result });
const xkbcommon = b.createModule(.{ .root_source_file = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" } });

const waylock = b.addExecutable(.{
.name = "waylock",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
waylock.addOptions("build_options", options);
waylock.root_module.addOptions("build_options", options);

waylock.linkLibC();
waylock.linkSystemLibrary("pam");

waylock.addModule("wayland", wayland);
waylock.root_module.addImport("wayland", wayland);
waylock.linkSystemLibrary("wayland-client");

waylock.addModule("xkbcommon", xkbcommon);
waylock.root_module.addImport("xkbcommon", xkbcommon);
waylock.linkSystemLibrary("xkbcommon");

// TODO: remove when zig issue #131 is implemented
scanner.addCSource(waylock);

waylock.strip = strip;
waylock.root_module.strip = strip;
waylock.pie = pie;

b.installArtifact(waylock);
}
2 changes: 1 addition & 1 deletion deps/zig-wayland
Submodule zig-wayland updated from 65475b to fe0463
2 changes: 1 addition & 1 deletion deps/zig-xkbcommon
Submodule zig-xkbcommon updated from 7b188d to 85a796
42 changes: 21 additions & 21 deletions src/Lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const builtin = @import("builtin");
const assert = std.debug.assert;
const log = std.log;
const mem = std.mem;
const os = std.os;
const posix = std.posix;

const wayland = @import("wayland");
const wl = wayland.client.wl;
Expand All @@ -30,7 +30,7 @@ pub const Color = enum {

pub const Options = struct {
fork_on_lock: bool,
ready_fd: ?os.fd_t = null,
ready_fd: ?posix.fd_t = null,
ignore_empty_password: bool,
init_color: u24 = 0x002b36,
input_color: u24 = 0x6c71c4,
Expand Down Expand Up @@ -61,10 +61,10 @@ state: enum {
color: Color = .init,

fork_on_lock: bool,
ready_fd: ?os.fd_t,
ready_fd: ?posix.fd_t,
ignore_empty_password: bool,

pollfds: [2]os.pollfd,
pollfds: [2]posix.pollfd,

display: *wl.Display,
compositor: ?*wl.Compositor = null,
Expand Down Expand Up @@ -104,12 +104,12 @@ pub fn run(options: Options) void {

lock.pollfds[poll_wayland] = .{
.fd = lock.display.getFd(),
.events = os.POLL.IN,
.events = posix.POLL.IN,
.revents = 0,
};
lock.pollfds[poll_auth] = .{
.fd = lock.auth_connection.read_fd,
.events = os.POLL.IN,
.events = posix.POLL.IN,
.revents = 0,
};

Expand Down Expand Up @@ -160,11 +160,11 @@ pub fn run(options: Options) void {
while (lock.state != .exiting) {
lock.flush_wayland_and_prepare_read();

_ = os.poll(&lock.pollfds, -1) catch |err| {
_ = posix.poll(&lock.pollfds, -1) catch |err| {
fatal("poll() failed: {s}", .{@errorName(err)});
};

if (lock.pollfds[poll_wayland].revents & os.POLL.IN != 0) {
if (lock.pollfds[poll_wayland].revents & posix.POLL.IN != 0) {
const errno = lock.display.readEvents();
if (errno != .SUCCESS) {
fatal("error reading wayland events: {s}", .{@tagName(errno)});
Expand All @@ -173,7 +173,7 @@ pub fn run(options: Options) void {
lock.display.cancelRead();
}

if (lock.pollfds[poll_auth].revents & os.POLL.IN != 0) {
if (lock.pollfds[poll_auth].revents & posix.POLL.IN != 0) {
const byte = lock.auth_connection.reader().readByte() catch |err| {
fatal("failed to read response from child authentication process: {s}", .{@errorName(err)});
};
Expand Down Expand Up @@ -231,12 +231,12 @@ fn flush_wayland_and_prepare_read(lock: *Lock) void {
},
.AGAIN => {
// The socket buffer is full, so wait for it to become writable again.
var wayland_out = [_]os.pollfd{.{
var wayland_out = [_]posix.pollfd{.{
.fd = lock.display.getFd(),
.events = os.POLL.OUT,
.events = posix.POLL.OUT,
.revents = 0,
}};
_ = os.poll(&wayland_out, -1) catch |err| {
_ = posix.poll(&wayland_out, -1) catch |err| {
fatal("poll() failed: {s}", .{@errorName(err)});
};
// No need to check for POLLHUP/POLLERR here, just fall
Expand Down Expand Up @@ -365,7 +365,7 @@ fn session_lock_listener(_: *ext.SessionLockV1, event: ext.SessionLockV1.Event,
const file = std.fs.File{ .handle = ready_fd };
file.writeAll("\n") catch |err| {
log.err("failed to send readiness notification: {s}", .{@errorName(err)});
os.exit(1);
posix.exit(1);
};
file.close();
lock.ready_fd = null;
Expand All @@ -381,11 +381,11 @@ fn session_lock_listener(_: *ext.SessionLockV1, event: ext.SessionLockV1.Event,
.locking => {
log.err("the wayland compositor has denied our attempt to lock the session, " ++
"is another ext-session-lock client already running?", .{});
os.exit(1);
posix.exit(1);
},
.locked => {
log.info("the wayland compositor has unlocked the session, exiting", .{});
os.exit(0);
posix.exit(0);
},
.exiting => unreachable,
}
Expand All @@ -409,7 +409,7 @@ pub fn submit_password(lock: *Lock) void {
fn send_password_to_auth(lock: *Lock) !void {
defer lock.password.clear();
const writer = lock.auth_connection.writer();
try writer.writeIntNative(u32, @as(u32, @intCast(lock.password.buffer.len)));
try writer.writeInt(u32, @as(u32, @intCast(lock.password.buffer.len)), builtin.cpu.arch.endian());
try writer.writeAll(lock.password.buffer);
}

Expand All @@ -426,7 +426,7 @@ pub fn set_color(lock: *Lock, color: Color) void {

fn fatal(comptime format: []const u8, args: anytype) noreturn {
log.err(format, args);
os.exit(1);
posix.exit(1);
}

fn fatal_oom() noreturn {
Expand Down Expand Up @@ -455,24 +455,24 @@ fn create_buffers(
}

// TODO: Upstream this to the Zig standard library
extern fn setsid() os.pid_t;
extern fn setsid() posix.pid_t;

fn fork_to_background() void {
const pid = os.fork() catch |err| fatal("fork failed: {s}", .{@errorName(err)});
const pid = posix.fork() catch |err| fatal("fork failed: {s}", .{@errorName(err)});
if (pid == 0) {
// This can't fail as we are the child of a fork() and therefore not
// a process group leader.
assert(setsid() != -1);
// Ensure the working directory is on the root filesystem to avoid potentially
// blocking some other filesystem from being unmounted.
os.chdirZ("/") catch |err| {
posix.chdirZ("/") catch |err| {
// While this is a nice thing to do, it is not critical to the locking functionality
// and it is better to allow potentially unlocking the session rather than aborting
// and leaving the session locked if this fails.
log.warn("failed to change working directory to / on fork: {s}", .{@errorName(err)});
};
} else {
// Terminate the parent process with a clean exit code.
os.exit(0);
posix.exit(0);
}
}
2 changes: 1 addition & 1 deletion src/Output.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn destroy(output: *Output) void {
if (output.lock_surface) |lock_surface| lock_surface.destroy();
if (output.surface) |surface| surface.destroy();

const node = @fieldParentPtr(std.SinglyLinkedList(Output).Node, "data", output);
const node: *std.SinglyLinkedList(Output).Node = @fieldParentPtr("data", output);
output.lock.outputs.remove(node);
gpa.destroy(node);
}
Expand Down
12 changes: 6 additions & 6 deletions src/PasswordBuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const builtin = @import("builtin");
const assert = std.debug.assert;
const log = std.log;
const mem = std.mem;
const os = std.os;
const posix = std.posix;

const auth = @import("auth.zig");

Expand All @@ -18,7 +18,7 @@ pub fn init() PasswordBuffer {
var password: PasswordBuffer = .{
.buffer = gpa.alignedAlloc(u8, mem.page_size, size_max) catch {
log.err("failed to allocate password buffer", .{});
os.exit(1);
posix.exit(1);
},
};

Expand Down Expand Up @@ -74,26 +74,26 @@ pub fn clear(password: *PasswordBuffer) void {
fn prevent_swapping(buffer: []align(mem.page_size) const u8) void {
var attempts: usize = 0;
while (attempts < 10) : (attempts += 1) {
const errno = os.errno(mlock(buffer.ptr, buffer.len));
const errno = posix.errno(mlock(buffer.ptr, buffer.len));
switch (errno) {
.SUCCESS => return,
.AGAIN => continue,
else => {
log.err("mlock() on password buffer failed: E{s}", .{@tagName(errno)});
os.exit(1);
posix.exit(1);
},
}
}
log.err("mlock() on password buffer failed: EAGAIN after 10 attempts", .{});
os.exit(1);
posix.exit(1);
}

fn prevent_dumping_best_effort(buffer: []align(mem.page_size) u8) void {
if (builtin.target.os.tag != .linux) return;

var attempts: usize = 0;
while (attempts < 10) : (attempts += 1) {
const errno = os.errno(os.system.madvise(buffer.ptr, buffer.len, os.MADV.DONTDUMP));
const errno = posix.errno(std.os.linux.madvise(buffer.ptr, buffer.len, posix.MADV.DONTDUMP));
switch (errno) {
.SUCCESS => return,
.AGAIN => continue,
Expand Down
10 changes: 5 additions & 5 deletions src/Seat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Seat = @This();

const std = @import("std");
const log = std.log;
const os = std.os;
const posix = std.posix;

const wayland = @import("wayland");
const wl = wayland.client.wl;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn destroy(seat: *Seat) void {
if (seat.wl_keyboard) |wl_keyboard| wl_keyboard.release();
if (seat.xkb_state) |xkb_state| xkb_state.unref();

const node = @fieldParentPtr(std.SinglyLinkedList(Seat).Node, "data", seat);
const node: *std.SinglyLinkedList(Seat).Node = @fieldParentPtr("data", seat);
seat.lock.seats.remove(node);
gpa.destroy(node);
}
Expand Down Expand Up @@ -92,18 +92,18 @@ fn keyboard_listener(_: *wl.Keyboard, event: wl.Keyboard.Event, seat: *Seat) voi
// only care about press events, not release.
},
.keymap => |ev| {
defer os.close(ev.fd);
defer posix.close(ev.fd);

if (ev.format != .xkb_v1) {
log.err("unsupported keymap format {d}", .{@intFromEnum(ev.format)});
return;
}

const keymap_string = os.mmap(null, ev.size, os.PROT.READ, os.MAP.PRIVATE, ev.fd, 0) catch |err| {
const keymap_string = posix.mmap(null, ev.size, posix.PROT.READ, .{ .TYPE = .PRIVATE }, ev.fd, 0) catch |err| {
log.err("failed to mmap() keymap fd: {s}", .{@errorName(err)});
return;
};
defer os.munmap(keymap_string);
defer posix.munmap(keymap_string);

const keymap = xkb.Keymap.newFromBuffer(
seat.lock.xkb_context,
Expand Down
Loading

0 comments on commit 5b3f13d

Please sign in to comment.