summaryrefslogtreecommitdiff
path: root/runtime/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/main.zig')
-rw-r--r--runtime/main.zig38
1 files changed, 21 insertions, 17 deletions
diff --git a/runtime/main.zig b/runtime/main.zig
index aa35e687..5cdf5ee0 100644
--- a/runtime/main.zig
+++ b/runtime/main.zig
@@ -1583,17 +1583,17 @@ fn nativeMain(std_init: std.process.Init) !void {
daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, rec.state orelse if (detached) .detached else .attached);
} else if (paired) |fd| {
// A window's pair's session: its first client on `fd`, the daemon
- // its own to dial (and to start, when none runs).
+ // its own to dial (and to start, when none runs), listed as the
+ // window's (P49: only a window's pair execs one with `--paired`).
opts.pair = fd;
term = .detached;
detached = true;
- if (daemon.wanted()) _ = daemon.register(null, kind, opts.ninep_name, .pair);
+ if (daemon.wanted()) _ = daemon.register(null, .gui, opts.ninep_name, .pair);
} else if (detach == null and std.c.getenv("PARDES_PAIR") != null) {
// The last step's way, behind its variable until it is the default:
// this terminal or window a client of a session forked off for it
// (`pair`).
- pair(init, &opts, args);
- term = .detached;
+ term = if (pair(init, &opts, args)) |kept| .{ .pair = kept } else .detached;
} else if (daemon.wanted()) {
if (detach != null or pardes.platform == .gui) {
// A session started detached is kept from birth: a pair's word.
@@ -1756,9 +1756,11 @@ fn pardesBeside(buf: *[4104]u8) ?[:0]const u8 {
/// effects (runtime/session.zig). A window's child execs the `pardes`
/// beside it (`--paired=FD` first, its arguments after), which dials the
/// daemon itself: a window's session loop is woken through SDL (gui.zig
-/// `sdlWake`), a terminal's needs none. Returns in a terminal's session;
-/// the client exits. No fork to be had: returns, and this process is the
-/// session, with no terminal: said by the caller's run. A window with no
+/// `sdlWake`), a terminal's needs none. Returns in a terminal's session,
+/// with the terminal its client draws on, cooked as it is now (P48: put
+/// back if that client dies with no word); the client exits. No fork to
+/// be had: returns, and this process is the session, with no terminal:
+/// said by the caller's run. A window with no
/// `pardes` beside it returns at once, its own session as before.
/// A terminal's launch with no terminal to draw on (a detached pty, a
/// daemon): said, not an error trace, and exit 1.
@@ -1767,23 +1769,22 @@ fn noTerminal(io: std.Io) noreturn {
std.process.exit(1);
}
-fn pair(init: std.process.Init, opts: *pardes.Options, args: []const [:0]const u8) void {
+fn pair(init: std.process.Init, opts: *pardes.Options, args: []const [:0]const u8) ?@import("dev").tui.Kept {
var path_buf: [4104]u8 = undefined;
var fd_buf: [32]u8 = undefined;
var argv: [64:null]?[*:0]const u8 = @splat(null);
if (pardes.platform == .gui) {
- const path = pardesBeside(&path_buf) orelse return;
- if (args.len >= argv.len - 1 or std.c.access(path, std.c.X_OK) != 0) return;
+ const path = pardesBeside(&path_buf) orelse return null;
+ if (args.len >= argv.len - 1 or std.c.access(path, std.c.X_OK) != 0) return null;
argv[0] = path.ptr;
for (args[1..], 2..) |a, k| argv[k] = a.ptr;
}
// Its client draws on /dev/tty: none to be had, said as the own path
- // says it, before a session is forked for nobody.
- if (pardes.platform == .tui) {
- const tty = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0));
- if (tty < 0) noTerminal(init.io);
- _ = std.c.close(tty);
- }
+ // says it, before a session is forked for nobody. The session keeps it,
+ // cooked as it is now. std.posix's termios: glibc's is a bigger struct.
+ const tty: c_int = if (pardes.platform == .tui) std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0)) else -1;
+ if (pardes.platform == .tui and tty < 0) noTerminal(init.io);
+ const cooked = if (tty >= 0) std.posix.tcgetattr(tty) catch null else null;
const conn: ?c_int = if (pardes.platform == .tui and daemon.wanted()) daemon.connect(true) else null;
var fds: [2]c_int = undefined;
if (std.c.socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0)
@@ -1815,8 +1816,11 @@ fn pair(init: std.process.Init, opts: *pardes.Options, args: []const [:0]const u
// The daemon is the client's to start; the hello is this one's.
daemon.startPending(false);
if (conn) |c| _ = daemon.register(c, .tty, opts.ninep_name, .pair);
- return;
+ if (cooked) |t| return .{ .fd = tty, .termios = t };
+ if (tty >= 0) _ = std.c.close(tty);
+ return null;
}
+ if (tty >= 0) _ = std.c.close(tty);
_ = std.c.close(fds[0]);
if (conn) |c| _ = std.c.close(c);
daemon.startPending(true);