execute the child command

This commit is contained in:
Mitchell Hashimoto
2022-04-24 14:33:25 -07:00
parent c4600d584f
commit 9cc19b0553
3 changed files with 81 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ const c = switch (builtin.os.tag) {
@cInclude("util.h"); // openpty()
}),
else => @cImport({
@cInclude("sys/ioctl.h"); // ioctl and constants
@cInclude("pty.h");
}),
};
@@ -27,6 +28,8 @@ const winsize = extern struct {
ws_ypixel: u16,
};
pub extern "c" fn setsid() std.c.pid_t;
/// The file descriptors for the master and slave side of the pty.
master: fd_t,
slave: fd_t,
@@ -73,6 +76,23 @@ pub fn setSize(self: Pty, size: winsize) !void {
return error.IoctlFailed;
}
/// This should be called prior to exec in the forked child process
/// in order to setup the tty properly.
pub fn childPreExec(self: Pty) !void {
// Create a new process group
if (setsid() < 0) return error.ProcessGroupFailed;
// Set controlling terminal
if (std.os.linux.ioctl(self.slave, c.TIOCSCTTY, 0) < 0)
return error.SetControllingTerminalFailed;
// Can close master/slave pair now
std.os.close(self.slave);
std.os.close(self.master);
// TODO: reset signals
}
test {
var ws: winsize = .{
.ws_row = 50,