Following the new Zig std.

This commit is contained in:
Philippe Pittoli 2023-05-03 03:21:30 +02:00
parent 2f369adfef
commit 527049ed3d
4 changed files with 14 additions and 13 deletions

View File

@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void {
// This declares intent for the library to be installed into the standard // This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when // location when the user invokes the "install" step (the default step when
// running `zig build`). // running `zig build`).
static_lib.install(); b.installArtifact(static_lib);
const shared_lib = b.addSharedLibrary(.{ const shared_lib = b.addSharedLibrary(.{
.name = "ipc", .name = "ipc",
@ -43,7 +43,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
shared_lib.linkLibC(); shared_lib.linkLibC();
shared_lib.install(); b.installArtifact(shared_lib);
// Creates a step for unit testing. // Creates a step for unit testing.
const main_tests = b.addTest(.{ const main_tests = b.addTest(.{

View File

@ -3,9 +3,10 @@ all: build
help: help:
@echo "usage: make [build|install|doc|serve-doc]" @echo "usage: make [build|install|doc|serve-doc]"
ZIGOPTS ?=
ZIGOPTIM ?= ReleaseSafe ZIGOPTIM ?= ReleaseSafe
build: build:
zig build -Doptimize=$(ZIGOPTIM) zig build -Doptimize=$(ZIGOPTIM) $(ZIGOPTS)
PREFIX ?= /usr/local PREFIX ?= /usr/local
LIBDIR ?= $(PREFIX)/lib LIBDIR ?= $(PREFIX)/lib

View File

@ -66,12 +66,12 @@ pub const Context = struct {
defer _ = umask(previous_mask); defer _ = umask(previous_mask);
// Create the run directory, where all UNIX sockets will be. // Create the run directory, where all UNIX sockets will be.
std.os.mkdir(rundir, 0o0770) catch |err| switch(err) { std.os.mkdir(rundir, 0o0770) catch |err| switch (err) {
error.PathAlreadyExists => { error.PathAlreadyExists => {
log.warn("runtime directory ({s}) already exists, (everything is fine, ignoring)", .{rundir}); log.warn("runtime directory ({s}) already exists, (everything is fine, ignoring)", .{rundir});
}, },
else => { else => {
log.warn("runtime directory ({s}): {any}", .{rundir, err}); log.warn("runtime directory ({s}): {any}", .{ rundir, err });
return err; return err;
}, },
}; };
@ -218,7 +218,7 @@ pub const Context = struct {
// net.StreamServer // net.StreamServer
var serverfd = self.pollfd.items[server_index].fd; var serverfd = self.pollfd.items[server_index].fd;
var path = self.connections.items[server_index].path orelse return error.ServerWithNoPath; var path = self.connections.items[server_index].path orelse return error.ServerWithNoPath;
var server = net.StreamServer{ .sockfd = serverfd, .kernel_backlog = 100, .reuse_address = false, .listen_address = try net.Address.initUnix(path) }; var server = net.StreamServer{ .sockfd = serverfd, .kernel_backlog = 100, .reuse_address = false, .reuse_port = false, .listen_address = try net.Address.initUnix(path) };
var client = try server.accept(); // net.StreamServer.Connection var client = try server.accept(); // net.StreamServer.Connection
const newfd = client.stream.handle; const newfd = client.stream.handle;
@ -236,15 +236,15 @@ pub const Context = struct {
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var buffer_lock: [1000]u8 = undefined; var buffer_lock: [1000]u8 = undefined;
var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ self.rundir, service_name }); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ self.rundir, service_name });
var lock = try std.fmt.bufPrint(&buffer_lock, "{s}.lock", .{ path }); var lock = try std.fmt.bufPrint(&buffer_lock, "{s}.lock", .{path});
// Create a lock file (and lock it) in order to prevent a race condition. // Create a lock file (and lock it) in order to prevent a race condition.
// While the program is running, the lock is enabled. // While the program is running, the lock is enabled.
// Once the program stops (even if it crashes), the lock is then disabled. // Once the program stops (even if it crashes), the lock is then disabled.
// Quit if the lock is still active. // Quit if the lock is still active.
const lock_opts = .{.lock = .Exclusive, .lock_nonblocking = true}; const lock_opts = .{ .lock = .Exclusive, .lock_nonblocking = true };
_ = std.fs.createFileAbsolute(lock, lock_opts) catch |err| { _ = std.fs.createFileAbsolute(lock, lock_opts) catch |err| {
log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{path, lock, err}); log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{ path, lock, err });
log.err("you may have lauched the service twice.", .{}); log.err("you may have lauched the service twice.", .{});
return err; return err;
}; };
@ -255,7 +255,7 @@ pub const Context = struct {
defer _ = umask(previous_mask); defer _ = umask(previous_mask);
// Remove the old UNIX socket. // Remove the old UNIX socket.
std.os.unlink(path) catch |err| switch(err) { std.os.unlink(path) catch |err| switch (err) {
error.FileNotFound => log.debug("no unlink necessary for {s}", .{path}), error.FileNotFound => log.debug("no unlink necessary for {s}", .{path}),
else => return err, else => return err,
}; };
@ -403,7 +403,7 @@ pub const Context = struct {
// handle messages // handle messages
// => loop over self.pollfd.items // => loop over self.pollfd.items
for (self.pollfd.items) |*fd, i| { for (self.pollfd.items, 0..) |*fd, i| {
// .revents is POLLIN // .revents is POLLIN
if (fd.revents & std.os.linux.POLL.IN > 0) { if (fd.revents & std.os.linux.POLL.IN > 0) {
// SERVER = new connection // SERVER = new connection
@ -470,7 +470,7 @@ pub const Context = struct {
fd.events &= ~@as(i16, std.os.linux.POLL.OUT); fd.events &= ~@as(i16, std.os.linux.POLL.OUT);
var index: usize = undefined; var index: usize = undefined;
for (self.tx.items) |m, index_| { for (self.tx.items, 0..) |m, index_| {
if (m.fd == self.pollfd.items[i].fd) { if (m.fd == self.pollfd.items[i].fd) {
index = index_; index = index_;
break; break;

View File

@ -52,7 +52,7 @@ pub const SwitchDB = struct {
} }
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void { pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
for (self.db.keys()) |k, i| { for (self.db.keys(), 0..) |k, i| {
try fmt.format(out_stream, "({},{})", .{ k, self.db.values()[i].dest }); try fmt.format(out_stream, "({},{})", .{ k, self.db.values()[i].dest });
} }
} }