summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/client.zig47
-rw-r--r--runtime/daemon.zig16
-rw-r--r--runtime/upgrade.zig26
3 files changed, 62 insertions, 27 deletions
diff --git a/runtime/client.zig b/runtime/client.zig
index 43fdf98d..1d348aea 100644
--- a/runtime/client.zig
+++ b/runtime/client.zig
@@ -95,7 +95,7 @@ pub const Start = struct {
s.o.frames = &s.frames;
// Exec'd at an Upgrade (`execAgain`): for the view it had.
if (init.environ_map.get("PARDES_JOIN_VIEW")) |v| s.o.join_view = std.fmt.parseInt(u8, v, 10) catch null;
- s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false;
+ s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null, false) orelse return false;
return true;
}
@@ -175,7 +175,7 @@ pub fn Loop(comptime Frontend: type) type {
frames: *pardes.FrameStats,
pub fn init(fe: *Frontend, s: *Start) !L {
- var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd }, .fe = fe, .frames = &s.frames };
+ var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd, .far = s.have != null }, .fe = fe, .frames = &s.frames };
try l.link.fromEnv(s.gpa, s.env);
return l;
}
@@ -209,11 +209,23 @@ pub fn Loop(comptime Frontend: type) type {
l.after();
}
if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
- if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close });
- // The link's reader ends with the link, posting as it goes:
- // what it posted is let go until it has (a terminal's queue
- // is bounded: a reader blocked on it would never end).
- _ = libc.shutdown(s.joined.fd, 2);
+ // Its link up and the session's state to take again: its
+ // goodbye and its hello said on it, the session's dump comes
+ // there (`replica.comeBack`); its reader reads on to the welcome,
+ // letting the stream it leaves go. Only a link that is gone
+ // is dialled again.
+ const here = link.backHere() and s.joined.rest.overlong() == null and back: {
+ link.back.store(true, .release);
+ break :back replica.comeBack(s.joined.fd, s.dial.session, s.r.view);
+ };
+ if (!here) {
+ if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close });
+ _ = libc.shutdown(s.joined.fd, 2);
+ }
+ // The link's reader ends with the link (or at the welcome),
+ // posting as it goes: what it posted is let go until it has
+ // (a terminal's queue is bounded: a reader blocked on it
+ // would never end).
while (true) {
const ended = done.load(.acquire);
l.fe.drop();
@@ -221,14 +233,23 @@ pub fn Loop(comptime Frontend: type) type {
std.Thread.yield() catch {};
}
reader.join();
- _ = libc.close(s.joined.fd);
- s.joined.rest.deinit(s.gpa);
+ if (!here) {
+ _ = libc.close(s.joined.fd);
+ s.joined.rest.deinit(s.gpa);
+ }
// Its link cut with no word by a session that is there still
// (an Upgrade's exec): joined again, as when told so.
if (link.gone and link.refused == null and link.ended == null and !link.left and replica.cutListed("the session hung up", s.dial.session, s.have != null))
link.refused = std.heap.c_allocator.dupe(u8, replica.upgrading) catch null;
- // Why it joins again is in its notice until a key.
- if (!link.rejoins()) {
+ // A last word goes first, one read after it said it comes
+ // back too (a Detach in it, the session's end). Why it joins
+ // again is in its notice until a key.
+ const goes = link.ended != null or link.left;
+ if (here and goes) {
+ _ = libc.close(s.joined.fd);
+ s.joined.rest.deinit(s.gpa);
+ }
+ if (goes or !link.rejoins()) {
// What it made since it joined, kept for its next start.
if (s.have) |h| h.keep(s.r.core());
s.r.deinit();
@@ -238,7 +259,7 @@ pub fn Loop(comptime Frontend: type) type {
}
// The session again, the core it leaves held meanwhile: its
// blobs are not sent again.
- s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse {
+ s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r, here) orelse {
if (replica.ended(&s.why)) return .gone;
if (replica.otherBuild(&s.why) and upgrade.replaced()) return .{ .upgrade = s.joined.view };
return .failed;
@@ -272,7 +293,7 @@ pub fn Loop(comptime Frontend: type) type {
/// and its end.
fn read(l: *L, s: *Start, done: *std.atomic.Value(bool)) void {
defer done.store(true, .release);
- replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, l.fe, Frontend.said);
+ replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, &l.link, l.fe, Frontend.said);
l.fe.ended();
}
diff --git a/runtime/daemon.zig b/runtime/daemon.zig
index 595d5107..97941a12 100644
--- a/runtime/daemon.zig
+++ b/runtime/daemon.zig
@@ -241,9 +241,8 @@ extern "c" fn close_range(first: c_uint, last: c_uint, flags: c_int) c_int;
/// twice), on the listener and the lock as fds 3 and 4.
fn spawn(listener: c_int, lock: c_int) bool {
var exe_buf: [4096:0]u8 = undefined;
- const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch return false;
- exe_buf[n] = 0;
- const argv = [_:null]?[*:0]const u8{ &exe_buf, "--daemon", null };
+ const exe = upgrade.startPath(&exe_buf) orelse return false;
+ const argv = [_:null]?[*:0]const u8{ exe, "--daemon", null };
const first = libc.fork();
if (first < 0) return false;
if (first == 0) {
@@ -268,7 +267,7 @@ fn spawn(listener: c_int, lock: c_int) bool {
};
if (null_fd > 4) _ = libc.close(null_fd);
_ = libc.chdir("/");
- _ = libc.execve(&exe_buf, &argv, @ptrCast(libc.environ));
+ _ = libc.execve(exe, &argv, @ptrCast(libc.environ));
libc._exit(127);
}
var status: c_int = 0;
@@ -677,8 +676,13 @@ fn stubWait(session: libc.pid_t, pipe: c_int) noreturn {
if (libc.W.IFEXITED(s)) libc._exit(@intCast(libc.W.EXITSTATUS(s)));
if (libc.W.IFSIGNALED(s)) {
const sig = libc.W.TERMSIG(s);
- const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 };
- posix.sigaction(sig, &default, null);
+ // A KILL's action is its default already and cannot be set
+ // (EINVAL: posix.sigaction's unreachable, the stub aborting with a
+ // trace on the terminal); any other is put back to it first.
+ if (sig != .KILL) {
+ const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 };
+ posix.sigaction(sig, &default, null);
+ }
_ = libc.raise(sig);
}
libc._exit(1);
diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig
index f59117ee..510c50ea 100644
--- a/runtime/upgrade.zig
+++ b/runtime/upgrade.zig
@@ -58,18 +58,28 @@ pub fn remember(args: []const [:0]const u8) void {
};
}
-/// Its path asked of the system (a readlink) and the file there now (a
+/// Its path asked of the system (a readlink) and the file it runs (a
/// stat), once: after the first frame, off a startup's way (the hosts'
-/// loops; nothing is reinstalled within a frame of a start).
+/// loops). A build can be installed at that path before then (a pair's
+/// session, whose first pass comes after its client's first frame; a
+/// loaded machine): Linux then names the path "PATH (deleted)", and the
+/// file at the path is not the one it started from, the one it runs is.
pub fn locate() void {
if (located) return;
located = true;
- if (exe_len == 0) {
- const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0;
- exe_buf[n] = 0;
- exe_len = n;
- }
- if (exe()) |path| started_inode = inodeOf(path);
+ if (exe_len == 0) exe_len = if (startPath(&exe_buf)) |path| path.len else 0;
+ started_inode = if (builtin.os.tag == .linux) inodeOf("/proc/self/exe") else if (exe()) |path| inodeOf(path) else null;
+}
+
+/// The path this process was started from, as the system names it, less
+/// the " (deleted)" Linux adds once the file there was replaced (a
+/// reinstall): the path is the path (what an Upgrade execs, and a daemon
+/// started from here: daemon.zig `spawn`).
+pub fn startPath(buf: *[4096:0]u8) ?[:0]const u8 {
+ const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), buf) catch return null;
+ const len = if (std.mem.endsWith(u8, buf[0..n], " (deleted)")) n - " (deleted)".len else n;
+ buf[len] = 0;
+ return buf[0..len :0];
}
fn inodeOf(path: [:0]const u8) ?u64 {