lightningd: don't abort on incorrect versions, but try to re-exec.

You still shouldn't do this (you could get some transient failures),
but at least you have a decent chance if you reinstall over a running
daemon, instead of getting confusing internal errors if message
formats have changed.

Changelog-Added: lightningd: we now try to restart if subdaemons are upgraded underneath us.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fixes: #4346
This commit is contained in:
Rusty Russell
2021-04-16 14:01:24 +09:30
parent 8714bf903c
commit 32d650f9df
6 changed files with 87 additions and 4 deletions

View File

@@ -211,6 +211,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
ld->listen = true;
ld->autolisten = true;
ld->reconnect = true;
ld->try_reexec = false;
/*~ This is from ccan/timer: it is efficient for the case where timers
* are deleted before expiry (as is common with timeouts) using an
@@ -849,6 +850,8 @@ int main(int argc, char *argv[])
struct rlimit nofile = {1024, 1024};
int sigchld_rfd;
int exit_code = 0;
char **orig_argv;
bool try_reexec;
/*~ Make sure that we limit ourselves to something reasonable. Modesty
* is a virtue. */
@@ -883,6 +886,17 @@ int main(int argc, char *argv[])
ld = new_lightningd(NULL);
ld->state = LD_STATE_RUNNING;
/*~ We store an copy of our arguments before parsing mangles them, so
* we can re-exec if versions of subdaemons change. Note the use of
* notleak() since our leak-detector can't find orig_argv on the
* stack. */
orig_argv = notleak(tal_arr(ld, char *, argc + 1));
for (size_t i = 1; i < argc; i++)
orig_argv[i] = tal_strdup(orig_argv, argv[i]);
/*~ Turn argv[0] into an absolute path (if not already) */
orig_argv[0] = path_join(orig_argv, take(path_cwd(NULL)), argv[0]);
orig_argv[argc] = NULL;
/* Figure out where our daemons are first. */
ld->daemon_dir = find_daemon_dir(ld, argv[0]);
if (!ld->daemon_dir)
@@ -1139,6 +1153,11 @@ int main(int argc, char *argv[])
* ld->payments, so clean that up. */
clean_tmpctx();
/* Gather these before we free ld! */
try_reexec = ld->try_reexec;
if (try_reexec)
tal_steal(NULL, orig_argv);
/* Free this last: other things may clean up timers. */
timers = tal_steal(NULL, ld->timers);
tal_free(ld);
@@ -1156,6 +1175,21 @@ int main(int argc, char *argv[])
tal_free(stop_response);
}
/* Were we supposed to restart ourselves? */
if (try_reexec) {
long max_fd;
/* Give a reasonable chance for the install to finish. */
sleep(5);
/* Close all filedescriptors except stdin/stdout/stderr */
max_fd = sysconf(_SC_OPEN_MAX);
for (int i = STDERR_FILENO+1; i < max_fd; i++)
close(i);
execv(orig_argv[0], orig_argv);
err(1, "Failed to re-exec ourselves after version change");
}
/*~ Farewell. Next stop: hsmd/hsmd.c. */
return exit_code;
}

View File

@@ -275,6 +275,9 @@ struct lightningd {
/* The round-robin list of channels, for use when doing MPP. */
u64 rr_counter;
/* Should we re-exec ourselves instead of just exiting? */
bool try_reexec;
};
/* Turning this on allows a tal allocation to return NULL, rather than aborting.

View File

@@ -412,8 +412,12 @@ static bool handle_version(struct subd *sd, const u8 *msg)
return false;
if (!streq(ver, version())) {
fatal("subdaemon %s version '%s' not '%s'",
sd->name, ver, version());
log_broken(sd->log, "version '%s' not '%s': restarting",
ver, version());
sd->ld->try_reexec = true;
/* Return us to toplevel lightningd.c */
io_break(sd->ld);
return false;
}
return true;
}
@@ -473,7 +477,7 @@ static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd)
goto next;
case WIRE_STATUS_VERSION:
if (!handle_version(sd, sd->msg_in))
goto malformed;
goto close;
goto next;
}