summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 0f770514..ac9e6eb4 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -530,21 +530,32 @@ fn hupOnStubDeath(on: bool) void {
pub fn tellStubDetached() void {
if (stub < 0) return;
hupOnStubDeath(false);
+ tellDetached(stub);
+ stub = -1;
+}
+
+/// The stub on `fd` told its terminal is detached (one of a session's
+/// several, views.zig), and the pipe let go.
+pub fn tellDetached(fd: c_int) void {
+ if (fd < 0) return;
var buf: [160]u8 = undefined;
const said = std.fmt.bufPrint(&buf, "d{s}", .{name()}) catch "d";
- _ = libc.write(stub, said.ptr, said.len);
- _ = libc.close(stub);
- stub = -1;
+ _ = libc.write(fd, said.ptr, said.len);
+ _ = libc.close(fd);
}
/// The session's end, `code` its exit status: the stub is told (and goes).
pub fn tellStubExit(code: u8) void {
- if (stub < 0) return;
+ tellExit(stub, code);
+ stub = -1;
+}
+
+pub fn tellExit(fd: c_int, code: u8) void {
+ if (fd < 0) return;
var buf: [8]u8 = undefined;
const said = std.fmt.bufPrint(&buf, "x{d}", .{code}) catch "x1";
- _ = libc.write(stub, said.ptr, said.len);
- _ = libc.close(stub);
- stub = -1;
+ _ = libc.write(fd, said.ptr, said.len);
+ _ = libc.close(fd);
}
/// A terminal refused (an attach that came too late): its stub is told why.
@@ -742,10 +753,7 @@ const Daemon = struct {
_ = std.fmt.bufPrint(why, "no '{s}' is {s}={s}`", .{ wanted_name, other, wanted_name }) catch {};
return error.Said;
}
- if (!s.detached) {
- _ = std.fmt.bufPrint(why, "no '{s}' is attached already, to another terminal or window", .{wanted_name}) catch {};
- return error.Said;
- }
+ // Attached already: the session takes another view.
return @intCast(i);
};
_ = std.fmt.bufPrint(why, "no no session called '{s}'", .{wanted_name}) catch {};
@@ -764,6 +772,15 @@ const Daemon = struct {
found = @intCast(i);
};
if (count == 1) return found.?;
+ // None detached: the one session there is, as another view of it.
+ if (count == 0) {
+ for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind == k) {
+ count += 1;
+ found = @intCast(i);
+ };
+ if (count == 1) return found.?;
+ count = 0;
+ }
if (count == 0 and others == 0) {
w.writeAll("no no detached session is running") catch {};
return error.Said;