Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: rescan restarts plugin on update #4609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <ccan/array_size/array_size.h>
#include <ccan/ccan/tal/grab_file/grab_file.h>
#include <ccan/crc32c/crc32c.h>
#include <ccan/list/list.h>
#include <ccan/mem/mem.h>
#include <ccan/opt/opt.h>
Expand Down Expand Up @@ -215,28 +217,44 @@ static void destroy_plugin(struct plugin *p)
}
}

static u32 file_checksum(const char* path)
{
char *content = grab_file(tmpctx, path);
if (content == NULL) return 0;
return crc32c(0, content, tal_count(content));
}
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved

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
49 changes: 49 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import re
import signal
import sqlite3
import stat
import subprocess
import time
import unittest
Expand Down Expand Up @@ -2472,3 +2473,51 @@ def test_custom_notification_topics(node_factory):
# The plugin just dist what previously was a fatal mistake (emit
# an unknown notification), make sure we didn't kill it.
assert 'custom_notifications.py' in [p['name'] for p in l1.rpc.listconfigs()['plugins']]


def test_restart_on_update(node_factory):
"""Tests if plugin rescan restarts modified plugins
"""
# we need to write plugin content dynamically
content = """#!/usr/bin/env python3
from pyln.client import Plugin
import time
plugin = Plugin()
@plugin.init()
def init(options, configuration, plugin):
plugin.log("test_restart_on_update %s")
plugin.run()
"""

# get a node that is not started so we can put a plugin in its lightning_dir
n = node_factory.get_node(start=False)
lndir = n.daemon.lightning_dir

# write hello world plugin to lndir/plugins
os.makedirs(os.path.join(lndir, 'plugins'), exist_ok=True)
path = os.path.join(lndir, 'plugins', 'test_restart_on_update.py')
file = open(path, 'w+')
file.write(content % "1")
file.close()
os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)

# now fire up the node and wait for the plugin to print hello
n.daemon.start()
n.daemon.logsearch_start = 0
n.daemon.wait_for_log(r"test_restart_on_update 1")

# a rescan should not yet reload the plugin on the same file
n.rpc.plugin_rescan()
assert not n.daemon.is_in_log(r"Plugin changed, needs restart.")

# modify the file
file = open(path, 'w+')
file.write(content % "2")
file.close()
os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)

# rescan and check
n.rpc.plugin_rescan()
n.daemon.wait_for_log(r"Plugin changed, needs restart.")
n.daemon.wait_for_log(r"test_restart_on_update 2")
n.stop()