Create the rundir, umask (mkdir + Unix sockets), use fmt.bufPrint.

This commit is contained in:
Philippe Pittoli 2023-02-14 19:16:07 +01:00
parent 2e192aef1b
commit e181f54bb4

View File

@ -54,19 +54,29 @@ pub const Context = struct {
pub fn init(allocator: std.mem.Allocator) !Self { pub fn init(allocator: std.mem.Allocator) !Self {
var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch (err) { var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch (err) {
error.EnvironmentVariableNotFound => blk: { error.EnvironmentVariableNotFound => blk: {
break :blk try allocator.dupeZ(u8, "/tmp/libipc-run/"); break :blk try allocator.dupeZ(u8, "/tmp/.libipc-run/");
}, },
else => { else => {
return err; return err;
}, },
}; };
return Self{ .rundir = rundir, .connections = Connections.init(allocator), .pollfd = PollFD.init(allocator), .tx = Messages.init(allocator), .switchdb = SwitchDB.init(allocator), .allocator = allocator }; // Allow mkdir to create a directory with 0o770 permissions.
} var previous_mask = umask(0o007);
defer _ = umask(previous_mask);
// create a server path for the UNIX socket based on the service name // Create the run directory, where all UNIX sockets will be.
pub fn server_path(self: *Self, service_name: []const u8, writer: anytype) !void { std.os.mkdir(rundir, 0o0770) catch |err| switch(err) {
try writer.print("{s}/{s}", .{ self.rundir, service_name }); error.PathAlreadyExists => {
log.warn("runtime directory ({s}) already exists, (everything is fine, ignoring)", .{rundir});
},
else => {
log.warn("runtime directory ({s}): {any}", .{rundir, err});
return err;
},
};
return Self{ .rundir = rundir, .connections = Connections.init(allocator), .pollfd = PollFD.init(allocator), .tx = Messages.init(allocator), .switchdb = SwitchDB.init(allocator), .allocator = allocator };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
@ -180,12 +190,7 @@ pub const Context = struct {
/// Return the connection FD. /// Return the connection FD.
pub fn connect_service(self: *Self, service_name: []const u8) !i32 { pub fn connect_service(self: *Self, service_name: []const u8) !i32 {
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ self.rundir, service_name });
var writer = fbs.writer();
try self.server_path(service_name, writer);
var path = fbs.getWritten();
return self.connect_(Connection.Type.IPC, path); return self.connect_(Connection.Type.IPC, path);
} }
@ -229,17 +234,13 @@ pub const Context = struct {
// Store std lib structures in the context. // Store std lib structures in the context.
pub fn server_init(self: *Self, service_name: []const u8) !net.StreamServer { pub fn server_init(self: *Self, service_name: []const u8) !net.StreamServer {
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ self.rundir, service_name });
var writer = fbs.writer();
// Allow to create a unix socket with the right permissions. // Allow to create a unix socket with the right permissions.
// Group should include write permissions. // Group should include write permissions.
var previous_mask = umask(0o117); var previous_mask = umask(0o117);
defer _ = umask(previous_mask); defer _ = umask(previous_mask);
try self.server_path(service_name, writer);
var path = fbs.getWritten();
var server = net.StreamServer.init(.{}); var server = net.StreamServer.init(.{});
var socket_addr = try net.Address.initUnix(path); var socket_addr = try net.Address.initUnix(path);
try server.listen(socket_addr); try server.listen(socket_addr);
@ -575,11 +576,8 @@ const CommunicationTestThread = struct {
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ ctx.rundir, "simple-context-test" });
var writer = fbs.writer();
try ctx.server_path("simple-context-test", writer);
var path = fbs.getWritten();
const socket = try net.connectUnixSocket(path); const socket = try net.connectUnixSocket(path);
defer socket.close(); defer socket.close();
_ = try socket.writer().writeAll("Hello world!"); _ = try socket.writer().writeAll("Hello world!");
@ -597,10 +595,7 @@ test "Context - creation, display and memory check" {
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ ctx.rundir, "simple-context-test" });
var writer = fbs.writer();
try ctx.server_path("simple-context-test", writer);
var path = fbs.getWritten();
// SERVER SIDE: creating a service. // SERVER SIDE: creating a service.
var server = ctx.server_init("simple-context-test") catch |err| switch (err) { var server = ctx.server_init("simple-context-test") catch |err| switch (err) {
@ -637,11 +632,8 @@ const ConnectThenSendMessageThread = struct {
var ctx = try Context.init(allocator); var ctx = try Context.init(allocator);
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
var path_buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var path_fbs = std.io.fixedBufferStream(&path_buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ ctx.rundir, "simple-context-test" });
var path_writer = path_fbs.writer();
try ctx.server_path("simple-context-test", path_writer);
var path = path_fbs.getWritten();
// Actual UNIX socket connection. // Actual UNIX socket connection.
const socket = try net.connectUnixSocket(path); const socket = try net.connectUnixSocket(path);
@ -672,10 +664,7 @@ test "Context - creation, echo once" {
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ ctx.rundir, "simple-context-test" });
var writer = fbs.writer();
try ctx.server_path("simple-context-test", writer);
var path = fbs.getWritten();
// SERVER SIDE: creating a service. // SERVER SIDE: creating a service.
var server = ctx.server_init("simple-context-test") catch |err| switch (err) { var server = ctx.server_init("simple-context-test") catch |err| switch (err) {