summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/daemon.zig15
-rw-r--r--src/replica.zig34
2 files changed, 17 insertions, 32 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 18b22f5c..93cc8fa1 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -434,7 +434,9 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
}
}
-fn writeAll(fd: c_int, bytes: []const u8) bool {
+/// `bytes` sent whole on a socket, never raising SIGPIPE: false when the
+/// peer is gone.
+pub fn writeAll(fd: c_int, bytes: []const u8) bool {
var at: usize = 0;
while (at < bytes.len) {
const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal);
@@ -445,10 +447,12 @@ fn writeAll(fd: c_int, bytes: []const u8) bool {
return true;
}
-/// A replica refused by a session that takes none (a window's, so far).
+/// A replica's connection refused in words, and closed: by the daemon at
+/// its hello, or by its session. A fresh connection's buffer is empty, so
+/// this never blocks.
pub fn refuseReplica(fd: c_int, why: []const u8) void {
var out: [600]u8 = undefined;
- if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {}
+ if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(fd, bytes) else |_| {}
_ = libc.close(fd);
}
@@ -972,10 +976,7 @@ const Daemon = struct {
}
fn refuse(r: *Replica, why: []const u8) void {
- var out: [600]u8 = undefined;
- // A fresh connection's buffer is empty: this never blocks.
- if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {}
- _ = libc.close(r.fd);
+ refuseReplica(r.fd, why);
r.* = .{};
}
diff --git a/src/replica.zig b/src/replica.zig
index 16ecc184..4ddb3bb4 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -28,6 +28,8 @@ const tape = @import("tape.zig");
const rollback = @import("rollback.zig");
const walk = @import("walk.zig");
const wire = @import("wire.zig");
+const daemon = @import("daemon.zig");
+const writeAll = daemon.writeAll;
const screen_state = @import("screen_state.zig");
const c_heap = @import("c_heap");
const terminal = pardes.panes.terminal;
@@ -1212,7 +1214,6 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void {
const libc = std.c;
const log = std.log.scoped(.replica);
-const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0;
/// What a client may leave unread, past its join's dump, before it is cut
/// off: minutes of typing, a screenful of a flood's deltas many times over.
const lag_max: usize = 64 << 20;
@@ -1233,17 +1234,6 @@ fn gmtoff() i32 {
return if (localtime_r(&secs, &tm) != null) @intCast(tm.gmtoff) else 0;
}
-fn writeAll(fd: c_int, bytes: []const u8) bool {
- var at: usize = 0;
- while (at < bytes.len) {
- const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal);
- if (n < 0 and libc.errno(n) == .INTR) continue;
- if (n <= 0) return false;
- at += @intCast(n);
- }
- return true;
-}
-
/// `bytes` as dump pieces (1 MiB at most each).
fn writePieces(fd: c_int, bytes: []const u8) bool {
var at: usize = 0;
@@ -1762,7 +1752,7 @@ pub const Peers = struct {
pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed };
/// A connection to join, and the view it joins again for, if it does.
- pub const Joining = @import("daemon.zig").Joining;
+ pub const Joining = daemon.Joining;
pub const Msg = struct { peer: *Peer, msg: []u8 };
pub fn count(ps: *const Peers) usize {
@@ -1837,7 +1827,7 @@ pub const Peers = struct {
for (ps.taken.items) |note| switch (note) {
.join => |j| {
ps.held.append(ps.gpa, j) catch {
- refuse(j.fd, "out of memory");
+ daemon.refuseReplica(j.fd, "out of memory");
continue;
};
ps.joins(core);
@@ -1908,12 +1898,6 @@ pub const Peers = struct {
}
}
- fn refuse(fd: c_int, why: []const u8) void {
- var out: [600]u8 = undefined;
- if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {}
- _ = libc.close(fd);
- }
-
/// The clients waiting to join, joined, once the texts the session
/// never digested have their words (`walk.unhashed`): those are hashed
/// on a thread of their own, pinned meanwhile, and the joins wait a
@@ -1959,11 +1943,11 @@ pub const Peers = struct {
}
const slot = for (&ps.list) |*s| {
if (s.* == null) break s;
- } else return refuse(fd, "that session has as many clients as it takes");
+ } else return daemon.refuseReplica(fd, "that session has as many clients as it takes");
live_peers = ps;
if (!ps.recording) {
// A session recording to a file (PARDES_RECORD) has its tape.
- if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)");
+ if (tape.on != null) return daemon.refuseReplica(fd, "that session records a tape of its own (PARDES_RECORD)");
ps.t = tape.memory(ps.gpa);
ps.t.unit_digests = true;
// Terminals cross as screen state: a unit carries no pty bytes,
@@ -1986,7 +1970,7 @@ pub const Peers = struct {
const k = (if (back) joining.view else core.openView()) orelse {
core.unattended = ps.count() == 0 and ps.alone_unattended;
if (ps.count() == 0) ps.stop(core);
- return refuse(fd, "that session has as many views as it takes");
+ return daemon.refuseReplica(fd, "that session has as many views as it takes");
};
// Its own process draws it.
core.undrawn |= @as(u8, 1) << @intCast(k);
@@ -2003,7 +1987,7 @@ pub const Peers = struct {
ps.unit(0, ps.seq);
if (ps.count() == 0) ps.stop(core);
var why: [96]u8 = undefined;
- return refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped");
+ return daemon.refuseReplica(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped");
};
var whole = bytes.len;
for (blobs.items) |b| {
@@ -2019,7 +2003,7 @@ pub const Peers = struct {
for (blobs.items) |b| pardes.memory.unpin(b.bytes);
blobs.deinit(core.gpa);
core.gpa.free(bytes);
- return refuse(fd, "out of memory");
+ return daemon.refuseReplica(fd, "out of memory");
};
peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa };
ps.next_id +%= 1;