summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig55
1 files changed, 26 insertions, 29 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 7d2ca9b4..3b1cfcd7 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -73,7 +73,7 @@ fn cmsgSpace(n: usize) usize {
return data_off + std.mem.alignForward(usize, n * @sizeOf(c_int), @sizeOf(usize));
}
-pub fn send(fd: c_int, words: []const u8, fds: []const c_int) bool {
+fn send(fd: c_int, words: []const u8, fds: []const c_int) bool {
std.debug.assert(words.len > 0 and fds.len <= max_fds);
var iov = [_]posix.iovec_const{.{ .base = words.ptr, .len = words.len }};
var cbuf: [cmsgSpace(max_fds)]u8 align(@alignOf(libc.cmsghdr)) = undefined;
@@ -93,10 +93,10 @@ pub fn send(fd: c_int, words: []const u8, fds: []const c_int) bool {
}
}
-pub const Got = struct { words: []const u8, fds: [max_fds]c_int = undefined, nfds: usize = 0 };
+const Got = struct { words: []const u8, fds: [max_fds]c_int = undefined, nfds: usize = 0 };
/// The next message, its fds close-on-exec; null at the end of the link.
-pub fn recv(fd: c_int, buf: []u8) ?Got {
+fn recv(fd: c_int, buf: []u8) ?Got {
var iov = [_]posix.iovec{.{ .base = buf.ptr, .len = buf.len }};
var cbuf: [cmsgSpace(max_fds)]u8 align(@alignOf(libc.cmsghdr)) = undefined;
var msg: libc.msghdr = .{ .name = null, .namelen = 0, .iov = &iov, .iovlen = 1, .control = &cbuf, .controllen = cbuf.len, .flags = 0 };
@@ -305,7 +305,7 @@ pub fn kept() bool {
/// The name a session goes by: its 9P socket's (`--9p=`, `--detach=`), else
/// its pid, as ninep_io.start names it.
-pub fn setName(named: []const u8) void {
+fn setName(named: []const u8) void {
const n = if (named.len != 0)
std.fmt.bufPrint(&name_buf, "{s}", .{named[0..@min(named.len, name_buf.len)]}) catch unreachable
else
@@ -693,6 +693,11 @@ const Held = struct {
owner: ?u8 = null,
};
+fn drop(h: *Held) void {
+ _ = libc.close(h.fd);
+ h.* = .{};
+}
+
/// A replica's connection until its hello is whole: read no further than
/// the hello, so what follows it is the session's to read.
const Replica = struct {
@@ -714,12 +719,6 @@ const Daemon = struct {
return true;
}
- fn drop(d: *Daemon, h: *Held) void {
- _ = d;
- _ = libc.close(h.fd);
- h.* = .{};
- }
-
fn hangUp(d: *Daemon, i: u8) void {
const c = &d.conns[i];
const killed = c.killed;
@@ -728,7 +727,7 @@ const Daemon = struct {
// Whatever it still held, it died holding: kept for an Adopt, unless
// it was killed on purpose, when its shells hang up with it.
for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) {
- if (killed) d.drop(h) else h.owner = null;
+ if (killed) drop(h) else h.owner = null;
};
}
@@ -748,7 +747,7 @@ const Daemon = struct {
n += 1;
};
if (named == null) {
- for (&d.held) |*h| if (h.fd >= 0 and h.owner == null) d.drop(h);
+ for (&d.held) |*h| if (h.fd >= 0 and h.owner == null) drop(h);
} else if (n == 0) {
var why: [256]u8 = undefined;
return d.reply(i, std.fmt.bufPrint(&why, "no no session called '{s}'", .{named.?}) catch "no no such session", &.{});
@@ -819,11 +818,11 @@ const Daemon = struct {
}
if (std.mem.eql(u8, verb, "close")) {
const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return;
- for (&d.held) |*h| if (h.fd >= 0 and h.pid == pid and h.owner == i) d.drop(h);
+ for (&d.held) |*h| if (h.fd >= 0 and h.pid == pid and h.owner == i) drop(h);
return;
}
if (std.mem.eql(u8, verb, "quit")) {
- for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) d.drop(h);
+ for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) drop(h);
return;
}
if (std.mem.eql(u8, verb, "upgrade")) {
@@ -854,10 +853,8 @@ const Daemon = struct {
const k = std.meta.stringToEnum(Kind, words.next() orelse "") orelse return d.reply(i, "no bad attach", &.{});
const wanted_name = std.mem.trim(u8, words.rest(), " ");
var why: [512]u8 = undefined;
- const target = d.find(k, wanted_name, &why) catch |err| return d.reply(i, switch (err) {
- // "other <name>": the client joins it through the replica socket.
- error.Said, error.Other => std.mem.sliceTo(&why, 0),
- }, &.{});
+ // "other <name>": the client joins it through the replica socket.
+ const target = d.find(k, wanted_name, &why) orelse return d.reply(i, std.mem.sliceTo(&why, 0), &.{});
const t = &d.conns[target];
if (!send(t.fd, "attach", got.fds[0..got.nfds])) {
d.hangUp(target);
@@ -870,9 +867,9 @@ const Daemon = struct {
}
/// The detached session of kind `k` called `wanted_name`, or the one
- /// there is when it is empty; else why not, in `why` (0-terminated).
- /// One of the other kind is error.Other, `why` "other <its name>".
- fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{ Said, Other }!u8 {
+ /// there is when it is empty; else null, and why not in `why`
+ /// (0-terminated): "other <its name>" for one of the other kind.
+ fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) ?u8 {
@memset(why, 0);
const other: []const u8 = switch (k) {
.tty => "a window: attach it with `pardes-gui --attach",
@@ -882,13 +879,13 @@ const Daemon = struct {
for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind != null and std.mem.eql(u8, s.named(), wanted_name)) {
if (s.kind.? != k) {
_ = std.fmt.bufPrint(why, "other {s}", .{wanted_name}) catch {};
- return error.Other;
+ return null;
}
// Attached already: the session takes another view.
- return @intCast(i);
+ return @as(u8, @intCast(i));
};
_ = std.fmt.bufPrint(why, "no no session called '{s}'", .{wanted_name}) catch {};
- return error.Said;
+ return null;
}
var found: ?u8 = null;
var count: usize = 0;
@@ -920,22 +917,22 @@ const Daemon = struct {
};
if (found == null and sessions == 1) {
_ = std.fmt.bufPrint(why, "other {s}", .{only.?.named()}) catch {};
- return error.Other;
+ return null;
}
}
if (count == 0 and others == 0) {
w.writeAll("no no detached session is running") catch {};
- return error.Said;
+ return null;
}
if (count == 0) {
w.print("no the detached session{s} running {s} {s}=<name>`", .{ if (others == 1) " is" else "s are", if (others == 1) "is" else "are each", other }) catch {};
- return error.Said;
+ return null;
}
w.print("no {d} detached sessions are running; say which with --attach=<name>:", .{count}) catch {};
for (&d.conns) |*s| if (s.fd >= 0 and s.detached and s.kind == k) {
w.print(" {s}", .{s.named()}) catch {};
};
- return error.Said;
+ return null;
}
/// More of a replica's hello; once it is whole, the connection refused
@@ -1189,7 +1186,7 @@ pub fn serve(state: ?[]const u8) noreturn {
}
for (fds[held_at..n], held_of[held_at..n]) |pfd, i| {
if (pfd.revents == 0 or d.held[i].fd != pfd.fd) continue;
- d.drop(&d.held[i]);
+ drop(&d.held[i]);
}
if (d.upgrade_asked) {
d.upgrade_asked = false;