diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 12:14:58 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:56:36 -0300 |
| commit | 4c194047d210ad9cf571c6019731d00d1e3615a4 (patch) | |
| tree | 257b994e471db3011589acd0cfb4a9168f803019 /src/daemon.zig | |
| parent | 60dd9514c8676e79c625948b5e9d2c6d745e8e87 (diff) | |
| download | pardes-4c194047d210ad9cf571c6019731d00d1e3615a4.tar.gz pardes-4c194047d210ad9cf571c6019731d00d1e3615a4.zip | |
idioms #4, string chains are an enum and a switch: main's flags (each exact one a Flag, the prefixed ones as they were), --ps's rows and manage's verb, the daemon's verbs on both ends and its registry's rows, an upgrade record's words, the window driver's mouse kinds and finger names, a frame script's click target and a mount name's reserved words go through std.meta.stringToEnum; with them main asks std.Io for its working directory, its chdir, a directory's access and /proc's comm, not libc
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 283 |
1 files changed, 140 insertions, 143 deletions
diff --git a/src/daemon.zig b/src/daemon.zig index ed558a52..a4e2c2d9 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -418,31 +418,34 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no while (recv(fd, &buf)) |got| { var words = std.mem.tokenizeScalar(u8, got.words, ' '); const verb = words.next() orelse ""; - if (std.mem.eql(u8, verb, "ok")) { - up.store(true, .release); - } else if (std.mem.eql(u8, verb, "attach")) { - deliver(ctx, .{ .attach = .{ - .tty = if (got.nfds >= 2) got.fds[0] else -1, - .stub = if (got.nfds >= 2) got.fds[1] else -1, - } }); - for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); - } else if (std.mem.eql(u8, verb, "replica")) { - const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; - if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); - for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); - } else if (std.mem.eql(u8, verb, "adopted")) { - var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; - const a = ¬e.adopted; - for (got.fds[0..got.nfds]) |f| { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { - _ = libc.close(f); - continue; - }; - a.pids[a.n] = pid; - a.fds[a.n] = f; - a.n += 1; - } - deliver(ctx, note); + if (std.meta.stringToEnum(enum { ok, attach, replica, adopted }, verb)) |v| switch (v) { + .ok => up.store(true, .release), + .attach => { + deliver(ctx, .{ .attach = .{ + .tty = if (got.nfds >= 2) got.fds[0] else -1, + .stub = if (got.nfds >= 2) got.fds[1] else -1, + } }); + for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); + }, + .replica => { + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); + for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); + }, + .adopted => { + var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; + const a = ¬e.adopted; + for (got.fds[0..got.nfds]) |f| { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { + _ = libc.close(f); + continue; + }; + a.pids[a.n] = pid; + a.fds[a.n] = f; + a.n += 1; + } + deliver(ctx, note); + }, } else for (got.fds[0..got.nfds]) |f| { _ = libc.close(f); } @@ -805,97 +808,87 @@ const Daemon = struct { }; const c = &d.conns[i]; var words = std.mem.tokenizeScalar(u8, got.words, ' '); - const verb = words.next() orelse return; - if (std.mem.eql(u8, verb, "hello")) { - const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0; - const k = std.meta.stringToEnum(Kind, words.next() orelse ""); - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const state = words.next() orelse ""; - const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; - const rest = std.mem.trim(u8, words.rest(), " "); - if (v != version or k == null or pid <= 0) { - // Another build's: it runs without a daemon. - d.reply(i, "version " ++ std.fmt.comptimePrint("{d}", .{version}), &.{}); - return if (d.conns[i].fd >= 0) d.hangUp(i); - } - c.kind = k; - c.pid = pid; - c.detached = std.mem.eql(u8, state, "detached"); - c.semantics = semantics; - c.nclients = 0; - c.name_len = @min(rest.len, c.name.len); - @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); - return d.reply(i, "ok", &.{}); - } - if (std.mem.eql(u8, verb, "state")) { - c.detached = std.mem.eql(u8, words.next() orelse "", "detached"); - return; - } - if (std.mem.eql(u8, verb, "clients")) { - c.nclients = 0; - while (words.next()) |w| if (c.nclients < max_clients) { - c.clients[c.nclients] = std.fmt.parseInt(libc.pid_t, w, 10) catch 0; - c.nclients += 1; - }; - return; - } - if (std.mem.eql(u8, verb, "pty")) { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; - if (got.nfds < 1) return; - for (&d.held) |*h| if (h.fd < 0) { - h.* = .{ .fd = got.fds[0], .pid = pid, .owner = i }; - fds_used = 1; - return; - }; - return; - } - 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) drop(h); - return; - } - if (std.mem.eql(u8, verb, "quit")) { - for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) drop(h); - return; - } - if (std.mem.eql(u8, verb, "upgrade")) { - d.upgrade_asked = true; - return; - } - if (std.mem.eql(u8, verb, "list")) return d.list(i); - if (std.mem.eql(u8, verb, "kill")) return d.kill(i, std.mem.trim(u8, words.rest(), " ")); - if (std.mem.eql(u8, verb, "killall")) return d.kill(i, null); - if (std.mem.eql(u8, verb, "adopt")) { - var line: [msg_max]u8 = undefined; - var w: std.Io.Writer = .fixed(&line); - w.writeAll("adopted") catch {}; - var fds: [max_fds]c_int = undefined; - var given: [max_fds]*Held = undefined; - var n: usize = 0; - for (&d.held) |*h| if (h.fd >= 0 and h.owner == null and n < max_fds) { - w.print(" {d}", .{h.pid}) catch break; - fds[n] = h.fd; - given[n] = h; - n += 1; - }; - if (!send(c.fd, w.buffered(), fds[0..n])) return d.hangUp(i); - for (given[0..n]) |h| h.owner = i; - return; - } - if (std.mem.eql(u8, verb, "attach")) { - 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; - // "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); - return d.reply(i, "no that session does not answer", &.{}); - } - t.detached = false; - var line: [32]u8 = undefined; - return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch "ok 0", &.{}); + const Verb = enum { hello, state, clients, pty, close, quit, upgrade, list, kill, killall, adopt, attach }; + switch (std.meta.stringToEnum(Verb, words.next() orelse return) orelse return) { + .hello => { + const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const state = words.next() orelse ""; + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + if (v != version or k == null or pid <= 0) { + // Another build's: it runs without a daemon. + d.reply(i, "version " ++ std.fmt.comptimePrint("{d}", .{version}), &.{}); + return if (d.conns[i].fd >= 0) d.hangUp(i); + } + c.kind = k; + c.pid = pid; + c.detached = std.mem.eql(u8, state, "detached"); + c.semantics = semantics; + c.nclients = 0; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + return d.reply(i, "ok", &.{}); + }, + .state => c.detached = std.mem.eql(u8, words.next() orelse "", "detached"), + .clients => { + c.nclients = 0; + while (words.next()) |w| if (c.nclients < max_clients) { + c.clients[c.nclients] = std.fmt.parseInt(libc.pid_t, w, 10) catch 0; + c.nclients += 1; + }; + }, + .pty => { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; + if (got.nfds < 1) return; + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = got.fds[0], .pid = pid, .owner = i }; + fds_used = 1; + return; + }; + }, + .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) drop(h); + }, + .quit => for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) drop(h), + .upgrade => d.upgrade_asked = true, + .list => d.list(i), + .kill => d.kill(i, std.mem.trim(u8, words.rest(), " ")), + .killall => d.kill(i, null), + .adopt => { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll("adopted") catch unreachable; // the first word of msg_max + var fds: [max_fds]c_int = undefined; + var given: [max_fds]*Held = undefined; + var n: usize = 0; + for (&d.held) |*h| if (h.fd >= 0 and h.owner == null and n < max_fds) { + w.print(" {d}", .{h.pid}) catch break; + fds[n] = h.fd; + given[n] = h; + n += 1; + }; + if (!send(c.fd, w.buffered(), fds[0..n])) return d.hangUp(i); + for (given[0..n]) |h| h.owner = i; + }, + .attach => { + 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; + // "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); + return d.reply(i, "no that session does not answer", &.{}); + } + t.detached = false; + var line: [32]u8 = undefined; + // "ok " and a pid_t's at most 11 digits fit 32. + return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch unreachable, &.{}); + }, } } @@ -1079,33 +1072,37 @@ const Daemon = struct { while (lines.next()) |line| { var words = std.mem.tokenizeScalar(u8, line, ' '); const what = words.next() orelse continue; - if (std.mem.eql(u8, what, "replicas")) { - replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; - if (replicas >= 0) ninep_io.setCloexec(replicas); - } else if (std.mem.eql(u8, what, "c")) { - const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; - const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; - if (i >= max_conns) continue; - const k = std.meta.stringToEnum(Kind, words.next() orelse ""); - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const detached = std.mem.eql(u8, words.next() orelse "", "1"); - const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; - const rest = std.mem.trim(u8, words.rest(), " "); - ninep_io.setCloexec(fd); - const c = &d.conns[i]; - c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; - c.nclients = 0; - c.name_len = @min(rest.len, c.name.len); - @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); - } else if (std.mem.eql(u8, what, "h")) { - const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; - ninep_io.setCloexec(fd); - for (&d.held) |*h| if (h.fd < 0) { - h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; - break; - }; + switch (std.meta.stringToEnum(enum { replicas, c, h }, what) orelse continue) { + .replicas => { + replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; + if (replicas >= 0) ninep_io.setCloexec(replicas); + }, + .c => { + const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + if (i >= max_conns) continue; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const detached = std.mem.eql(u8, words.next() orelse "", "1"); + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + ninep_io.setCloexec(fd); + const c = &d.conns[i]; + c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; + c.nclients = 0; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + }, + .h => { + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; + ninep_io.setCloexec(fd); + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; + break; + }; + }, } } return replicas; |
