2
0
Fork 0
forked from zynk/zynk

Hot reload preserves IRC session (nick, channels, voice). (0.39.0)

This commit is contained in:
Johannes Findeisen 2026-07-21 16:17:43 +02:00
commit 738323ebaf
3 changed files with 43 additions and 51 deletions

78
zynk.c
View file

@ -28,7 +28,7 @@ int asprintf(char **, const char *, ...);
/* ---- constants ---- */
#define VERSION "0.38.3"
#define VERSION "0.39.0"
#define DEFAULT_SERVER "irc.libera.chat"
#define OPENCODE_BIN "/usr/bin/opencode"
#define CLR_RESET "\033[0m"
@ -1194,40 +1194,20 @@ fail:
}
/*
* irc_part_all - Send IRC PART messages to all joined channels.
*
* Iterates the global chans[] array and sends a PART command for each
* non-empty channel name. Used during hot reload so the bot cleanly
* leaves channels before exec'ing a new instance.
*
* Parameters:
* s - The IRC session.
*
* Returns: void.
*/
static void irc_part_all(Session *s) {
for (int i = 0; i < nchans; i++) {
if (chans[i].name[0])
net_send(s, "PART %s\r\n", chans[i].name);
}
}
/*
* reload_do - Execute a hot reload: save state, part channels, fork+exec.
* reload_do - Execute a hot reload: save state, fork-in-place or fork+exec.
*
* Orchestrates the full hot reload sequence: saves TLS session state via
* reload_save(), waits briefly, parts all channels, sends QUIT, then
* fork()s and exec's the same binary. The child inherits the socket fd
* via ZYNK_RESTORE_FD environment variable and the parent exits. Requires
* an active TLS connection to preserve the session across the restart.
* reload_save(), then either forks-in-place (exec_new=0: child inherits the
* live socket and SSL session, continues the event loop) or fork+execs a
* new binary (exec_new=1: sends QUIT, child exec's the same binary).
*
* Parameters:
* s - The IRC session.
* reply_target - Channel or nick to send status messages to.
*
* Returns: void (parent exits, child continues as new instance).
* Returns: void (parent exits, child continues).
*/
static void reload_do(Session *s, const char *reply_target) {
static void reload_do(Session *s, const char *reply_target, int exec_new) {
if (!s->ssl || s->fd < 0) {
irc_msg(s, reply_target, "Cannot reload: not connected with TLS");
return;
@ -1236,15 +1216,16 @@ static void reload_do(Session *s, const char *reply_target) {
irc_msg(s, reply_target, "Failed to save session state");
return;
}
irc_msg(s, reply_target, "Reloading...");
irc_msg(s, reply_target, exec_new ? "Reloading..." : "Reloading (session preserved)...");
struct timespec ts_short = { .tv_sec = 0, .tv_nsec = 100000000 };
nanosleep(&ts_short, NULL);
irc_part_all(s);
int old_fl = fcntl(s->fd, F_GETFL, 0);
fcntl(s->fd, F_SETFL, old_fl & ~O_NONBLOCK);
net_send(s, "QUIT :restarting\r\n");
struct timespec ts_quit = { .tv_sec = 0, .tv_nsec = 500000000 };
nanosleep(&ts_quit, NULL);
if (exec_new) {
int old_fl = fcntl(s->fd, F_GETFL, 0);
fcntl(s->fd, F_SETFL, old_fl & ~O_NONBLOCK);
net_send(s, "QUIT :restarting\r\n");
struct timespec ts_quit = { .tv_sec = 0, .tv_nsec = 500000000 };
nanosleep(&ts_quit, NULL);
}
pid_t pid = fork();
if (pid < 0) {
unlink(ZYNK_RELOAD_FILE);
@ -1252,16 +1233,19 @@ static void reload_do(Session *s, const char *reply_target) {
return;
}
if (pid == 0) {
close(s->fd);
char self[4096];
ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);
if (n > 0) {
self[n] = 0;
char *deleted = strstr(self, " (deleted)");
if (deleted) *deleted = 0;
execl(self, "zynk", NULL);
if (exec_new) {
close(s->fd);
char self[4096];
ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);
if (n > 0) {
self[n] = 0;
char *deleted = strstr(self, " (deleted)");
if (deleted) *deleted = 0;
execl(self, "zynk", NULL);
}
_exit(1);
}
_exit(1);
return;
}
_exit(0);
}
@ -1938,7 +1922,7 @@ static int try_compile_and_restart(Session *s, const char *target) {
log_stamp(); fprintf(stderr, CLR_GREEN "Compilation successful, restarting..." CLR_RESET "\n");
FILE *cf = fopen("/tmp/zynk_code_restart", "w");
if (cf) { fprintf(cf, "%s\n", target); fclose(cf); }
reload_do(s, target);
reload_do(s, target, 1);
return 1;
} else {
char errbuf[512];
@ -2651,7 +2635,7 @@ static int cmd_reload(Session *s, const char *msg, const char *reply_target, con
return 1;
}
if (rate_limit_check(src_nick, reply_target) < 0) return 1;
reload_do(s, reply_target);
reload_do(s, reply_target, 0);
return 1;
}
@ -2671,7 +2655,7 @@ static int cmd_restart(Session *s, const char *msg, const char *reply_target, co
}
if (rate_limit_check(src_nick, reply_target) < 0) return 1;
irc_msg(s, reply_target, "Restarting...");
reload_do(s, reply_target);
reload_do(s, reply_target, 0);
return 1;
}
@ -3318,7 +3302,7 @@ static int cmd_rebuild(Session *s, const char *msg, const char *reply_target, co
log_stamp(); fprintf(stderr, CLR_GREEN "REBUILD successful, restarting..." CLR_RESET "\n");
FILE *cf = fopen("/tmp/zynk_code_restart", "w");
if (cf) { fprintf(cf, "%s\n", reply_target); fclose(cf); }
reload_do(s, reply_target);
reload_do(s, reply_target, 1);
} else {
FILE *f = fopen("/tmp/zynk_rebuild_err", "r");
if (f) { size_t n = fread(errbuf, 1, sizeof errbuf - 1, f); errbuf[n] = 0; fclose(f); char *nl = strchr(errbuf, '\n'); if (nl) *nl = 0; }