server: use fork/execve for startup command
std.ChildProcess leaks an fd currently and is more complex than what we need anyways.
This commit is contained in:
parent
3e17f12cbb
commit
54b09c4ae9
1 changed files with 12 additions and 11 deletions
|
@ -35,7 +35,7 @@ const usage: []const u8 =
|
||||||
var server: Server = undefined;
|
var server: Server = undefined;
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
var startup_command: ?[]const u8 = null;
|
var startup_command: ?[*:0]const u8 = null;
|
||||||
{
|
{
|
||||||
var it = std.process.args();
|
var it = std.process.args();
|
||||||
// Skip our name
|
// Skip our name
|
||||||
|
@ -47,7 +47,7 @@ pub fn main() anyerror!void {
|
||||||
std.os.exit(0);
|
std.os.exit(0);
|
||||||
} else if (std.mem.eql(u8, arg, "-c")) {
|
} else if (std.mem.eql(u8, arg, "-c")) {
|
||||||
if (it.nextPosix()) |command| {
|
if (it.nextPosix()) |command| {
|
||||||
startup_command = command;
|
startup_command = @ptrCast([*:0]const u8, command.ptr);
|
||||||
} else {
|
} else {
|
||||||
printErrorExit("Error: flag '-c' requires exactly one argument", .{});
|
printErrorExit("Error: flag '-c' requires exactly one argument", .{});
|
||||||
}
|
}
|
||||||
|
@ -85,16 +85,17 @@ pub fn main() anyerror!void {
|
||||||
|
|
||||||
try server.start();
|
try server.start();
|
||||||
|
|
||||||
const startup_process = if (startup_command) |cmd| blk: {
|
const child_pid = if (startup_command) |cmd| blk: {
|
||||||
const child_args = [_][]const u8{ "/bin/sh", "-c", cmd };
|
const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", cmd, null };
|
||||||
const child = try std.ChildProcess.init(&child_args, util.gpa);
|
const pid = try std.os.fork();
|
||||||
try std.ChildProcess.spawn(child);
|
if (pid == 0) {
|
||||||
break :blk child;
|
if (std.os.system.sigprocmask(std.os.SIG_SETMASK, &std.os.empty_sigset, null) < 0) unreachable;
|
||||||
|
std.os.execveZ("/bin/sh", &child_args, std.c.environ) catch c._exit(1);
|
||||||
|
}
|
||||||
|
break :blk pid;
|
||||||
} else null;
|
} else null;
|
||||||
defer if (startup_process) |child| {
|
defer if (child_pid) |pid|
|
||||||
_ = child.kill() catch |e| log.err(.server, "failed to terminate startup process: {}", .{e});
|
std.os.kill(pid, std.os.SIGTERM) catch |e| log.err(.server, "failed to kill startup process: {}", .{e});
|
||||||
child.deinit();
|
|
||||||
};
|
|
||||||
|
|
||||||
log.info(.server, "running...", .{});
|
log.info(.server, "running...", .{});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue