summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig59
1 files changed, 18 insertions, 41 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 3b1cfcd7..18b22f5c 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -1119,42 +1119,24 @@ pub fn serve(state: ?[]const u8) noreturn {
var d: Daemon = .{};
const given = if (state) |text| d.restore(text) else -1;
const replicas = if (given >= 0) given else listenReplicas();
- const slots = 2 + max_conns + max_replicas + max_held;
- var fds: [slots]libc.pollfd = undefined;
- var conn_of: [slots]u8 = undefined;
- var held_of: [slots]u16 = undefined;
- var replica_of: [slots]u8 = undefined;
+ // Every slot in its own place, an empty one's fd -1: poll passes over
+ // a negative fd.
+ var fds: [2 + max_conns + max_replicas + max_held]libc.pollfd = undefined;
+ const conns = fds[2..][0..max_conns];
+ const held = fds[2 + max_conns ..][0..max_held];
+ const reps = fds[2 + max_conns + max_held ..][0..max_replicas];
var buf: [msg_max]u8 = undefined;
while (true) {
- var n: usize = 0;
- fds[n] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 };
- n += 1;
- // -1 when there is none: poll skips a negative fd.
- fds[n] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 };
- n += 1;
- const conns_at = n;
- for (&d.conns, 0..) |*c, i| if (c.fd >= 0) {
- fds[n] = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 };
- conn_of[n] = @intCast(i);
- n += 1;
- };
- const replicas_at = n;
- for (&d.replicas, 0..) |*r, i| if (r.fd >= 0) {
- fds[n] = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 };
- replica_of[n] = @intCast(i);
- n += 1;
- };
- const held_at = n;
+ fds[0] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 };
+ fds[1] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 };
+ for (conns, &d.conns) |*pfd, *c| pfd.* = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 };
+ for (reps, &d.replicas) |*pfd, *r| pfd.* = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 };
// No events asked of a master: its hangup (every slave closed, the
// shell and all it ran gone) is reported anyway, and its output is
// the session's to read, never this one's.
- for (&d.held, 0..) |*h, i| if (h.fd >= 0) {
- fds[n] = .{ .fd = h.fd, .events = 0, .revents = 0 };
- held_of[n] = @intCast(i);
- n += 1;
- };
+ for (held, &d.held) |*pfd, *h| pfd.* = .{ .fd = h.fd, .events = 0, .revents = 0 };
const idle = d.idle();
- const ready = libc.poll(&fds, @intCast(n), if (idle) linger_ms else -1);
+ const ready = libc.poll(&fds, fds.len, if (idle) linger_ms else -1);
if (ready < 0) {
if (libc.errno(ready) == .INTR) continue;
break;
@@ -1172,22 +1154,17 @@ pub fn serve(state: ?[]const u8) noreturn {
}
if (fds[0].revents & libc.POLL.IN != 0) d.accept(listener);
if (fds[1].revents & libc.POLL.IN != 0) d.acceptReplica(replicas);
- for (fds[replicas_at..held_at], replica_of[replicas_at..held_at]) |pfd, i| {
- if (pfd.revents == 0 or d.replicas[i].fd != pfd.fd) continue;
- d.hear(&d.replicas[i]);
- }
- for (fds[conns_at..replicas_at], conn_of[conns_at..replicas_at]) |pfd, i| {
+ // A slot an earlier handler emptied or filled again is passed over.
+ for (reps, &d.replicas) |pfd, *r| if (pfd.revents != 0 and r.fd == pfd.fd) d.hear(r);
+ for (conns, 0..) |pfd, i| {
if (pfd.revents == 0 or d.conns[i].fd != pfd.fd) continue;
const got = recv(pfd.fd, &buf) orelse {
- d.hangUp(i);
+ d.hangUp(@intCast(i));
continue;
};
- d.take(i, got);
- }
- for (fds[held_at..n], held_of[held_at..n]) |pfd, i| {
- if (pfd.revents == 0 or d.held[i].fd != pfd.fd) continue;
- drop(&d.held[i]);
+ d.take(@intCast(i), got);
}
+ for (held, &d.held) |pfd, *h| if (pfd.revents != 0 and h.fd == pfd.fd) drop(h);
if (d.upgrade_asked) {
d.upgrade_asked = false;
d.reexec(replicas);