Fix mem leaks in ipcd.
parent
6b8d659319
commit
54fc5aa9e0
|
@ -119,7 +119,10 @@ pub const Context = struct {
|
||||||
// IPC_NETWORK="audio https://example.com/audio ;pong tls://pong.example.com/pong"
|
// 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) {
|
var network_envvar = std.process.getEnvVarOwned(allocator, "IPC_NETWORK") catch |err| switch(err) {
|
||||||
// error{ OutOfMemory, EnvironmentVariableNotFound, InvalidUtf8 } (ErrorSet)
|
// 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; },
|
else => { return err; },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -232,8 +235,15 @@ pub const Context = struct {
|
||||||
// Create a unix socket.
|
// Create a unix socket.
|
||||||
// Store std lib structures in the context.
|
// Store std lib structures in the context.
|
||||||
// TODO: find better error name
|
// TODO: find better error name
|
||||||
pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer {
|
pub fn server_init(self: *Self, service_name: [] const u8) !net.StreamServer {
|
||||||
// print("context server init {s}\n", .{path});
|
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 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);
|
||||||
|
|
|
@ -37,10 +37,8 @@ fn create_service() !void {
|
||||||
var ctx = try ipc.Context.init(allocator);
|
var ctx = try ipc.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?
|
||||||
|
|
||||||
const path = "/tmp/.TEST_USOCK";
|
|
||||||
|
|
||||||
// SERVER SIDE: creating a service.
|
// SERVER SIDE: creating a service.
|
||||||
_ = try ctx.server_init(path);
|
_ = try ctx.server_init("ipcd");
|
||||||
|
|
||||||
// signal handler, to quit when asked
|
// signal handler, to quit when asked
|
||||||
const S = struct {
|
const S = struct {
|
||||||
|
@ -132,16 +130,19 @@ fn create_service() !void {
|
||||||
, allocator
|
, allocator
|
||||||
, "currently not implemented");
|
, "currently not implemented");
|
||||||
try ctx.write(response);
|
try ctx.write(response);
|
||||||
|
response.deinit();
|
||||||
|
m.deinit();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// There is a problem: ipcd was contacted without providing
|
// There is a problem: ipcd was contacted without providing
|
||||||
// a message, meaning there is nothing to do. This should be
|
// a message, meaning there is nothing to do. This should be
|
||||||
// explicitely warned about.
|
// explicitely warned about.
|
||||||
var m = try Message.init(some_event.origin
|
var response = try Message.init(some_event.origin
|
||||||
, Message.Type.ERROR
|
, Message.Type.ERROR
|
||||||
, allocator
|
, allocator
|
||||||
, "lookup message without data");
|
, "lookup message without data");
|
||||||
try ctx.write(m);
|
try ctx.write(response);
|
||||||
|
response.deinit();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Reference in New Issue