Skip to content

Commit

Permalink
plugin: rescan restarts plugin on update
Browse files Browse the repository at this point in the history
This adds a `u32 checksum` field to the plugin struct that is used
to identify if a plugin is outdated and needs to be restarted on `rescan`.

Note: Only affects non-important plugins.

Changelog-Added: Plugin: Restart plugin on `rescan` when binary was changed.
  • Loading branch information
m-schmoock committed Jun 17, 2021
1 parent 40544b7 commit 0cb7b95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
33 changes: 30 additions & 3 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ccan/array_size/array_size.h>
#include <ccan/crc32c/crc32c.h>
#include <ccan/list/list.h>
#include <ccan/mem/mem.h>
#include <ccan/opt/opt.h>
Expand All @@ -15,6 +16,7 @@
#include <lightningd/plugin_control.h>
#include <lightningd/plugin_hook.h>
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

Expand Down Expand Up @@ -215,28 +217,53 @@ static void destroy_plugin(struct plugin *p)
}
}

static u32 file_checksum(const char* path)
{
FILE *fp;
char buf[1024];
size_t nread;
u32 checksum = 0;

fp = fopen(path, "r");
if (fp == NULL)
return 0;
while ((nread = fread(buf, 1, sizeof(buf), fp)) > 0)
checksum = crc32c(checksum, &buf, nread);
fclose(fp);
return checksum;
}

struct plugin *plugin_register(struct plugins *plugins, const char* path TAKES,
struct command *start_cmd, bool important,
const char *parambuf STEALS,
const jsmntok_t *params STEALS)
{
struct plugin *p, *p_temp;
u32 chksum;

/* Don't register an already registered plugin */
list_for_each(&plugins->plugins, p_temp, list) {
if (streq(path, p_temp->cmd)) {
if (taken(path))
tal_free(path);
/* If added as "important", upgrade to "important". */
/* If added as "important", upgrade to "important". */
if (important)
p_temp->important = true;
/* stop and restart plugin on different checksum */
chksum = file_checksum(path);
if (p_temp->checksum != chksum && !p_temp->important) {
plugin_kill(p_temp, LOG_INFORM,
"Plugin changed, needs restart.");
break;
}
if (taken(path))
tal_free(path);
return NULL;
}
}

p = tal(plugins, struct plugin);
p->plugins = plugins;
p->cmd = tal_strdup(p, path);
p->checksum = file_checksum(p->cmd);
p->shortname = path_basename(p, p->cmd);
p->start_cmd = start_cmd;

Expand Down
1 change: 1 addition & 0 deletions lightningd/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct plugin {

pid_t pid;
char *cmd;
u32 checksum;
struct io_conn *stdin_conn, *stdout_conn;
struct plugins *plugins;
const char **plugin_path;
Expand Down

0 comments on commit 0cb7b95

Please sign in to comment.