Archived
3
0

Fix mem leaks in ipcd.

This commit is contained in:
Philippe Pittoli 2023-01-03 18:18:16 +01:00
parent 6b8d659319
commit 54fc5aa9e0
2 changed files with 19 additions and 8 deletions

View File

@ -119,7 +119,10 @@ pub const Context = struct {
// IPC_NETWORK="audio https://example.com/audio ;pong tls://pong.example.com/pong"
var network_envvar = std.process.getEnvVarOwned(allocator, "IPC_NETWORK") catch |err| switch(err) {
// error{ OutOfMemory, EnvironmentVariableNotFound, InvalidUtf8 } (ErrorSet)
error.EnvironmentVariableNotFound => { return null; }, // no need to contact IPCd
error.EnvironmentVariableNotFound => {
print("no IPC_NETWORK envvar: IPCd won't be contacted\n", .{});
return null;
}, // no need to contact IPCd
else => { return err; },
};
@ -232,8 +235,15 @@ pub const Context = struct {
// Create a unix socket.
// Store std lib structures in the context.
// TODO: find better error name
pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer {
// print("context server init {s}\n", .{path});
pub fn server_init(self: *Self, service_name: [] const u8) !net.StreamServer {
var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
var writer = fbs.writer();
try self.server_path(service_name, writer);
var path = fbs.getWritten();
print("context server init {s}\n", .{path});
var server = net.StreamServer.init(.{});
var socket_addr = try net.Address.initUnix(path);
try server.listen(socket_addr);

View File

@ -37,10 +37,8 @@ fn create_service() !void {
var ctx = try ipc.Context.init(allocator);
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
const path = "/tmp/.TEST_USOCK";
// SERVER SIDE: creating a service.
_ = try ctx.server_init(path);
_ = try ctx.server_init("ipcd");
// signal handler, to quit when asked
const S = struct {
@ -132,16 +130,19 @@ fn create_service() !void {
, allocator
, "currently not implemented");
try ctx.write(response);
response.deinit();
m.deinit();
}
else {
// There is a problem: ipcd was contacted without providing
// a message, meaning there is nothing to do. This should be
// explicitely warned about.
var m = try Message.init(some_event.origin
var response = try Message.init(some_event.origin
, Message.Type.ERROR
, allocator
, "lookup message without data");
try ctx.write(m);
try ctx.write(response);
response.deinit();
}
},