mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 22:45:27 +01:00
lightningd: simplify plugin stdin/stdout initialization.
There's no reason to assign the plugin vars inside the callback, so do that outside, and the tal_steal() is redundant (the plugin is already the conn parent). And reduce duplication by making plugin_conn_finish call plugin_kill: just make sure we don't call plugin_conn_finish again if plugin_kill is called externally. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
fe365f930f
commit
0bfa4024b6
1 changed files with 8 additions and 21 deletions
|
@ -212,6 +212,8 @@ void plugin_kill(struct plugin *plugin, const char *msg)
|
||||||
plugin->start_cmd = NULL;
|
plugin->start_cmd = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Don't come back when we free stdout_conn! */
|
||||||
|
io_set_finish(plugin->stdout_conn, NULL, NULL);
|
||||||
tal_free(plugin);
|
tal_free(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -518,22 +520,10 @@ static struct io_plan *plugin_write_json(struct io_conn *conn,
|
||||||
return io_out_wait(conn, plugin, plugin_write_json, plugin);
|
return io_out_wait(conn, plugin, plugin_write_json, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/* This catches the case where their stdout closes (usually they're dead). */
|
||||||
* Finalizer for both stdin and stdout connections.
|
|
||||||
*
|
|
||||||
* Takes care of final cleanup, once the plugin is definitely dead.
|
|
||||||
*/
|
|
||||||
static void plugin_conn_finish(struct io_conn *conn, struct plugin *plugin)
|
static void plugin_conn_finish(struct io_conn *conn, struct plugin *plugin)
|
||||||
{
|
{
|
||||||
struct plugins *plugins = plugin->plugins;
|
plugin_kill(plugin, "Plugin exited before completing handshake.");
|
||||||
plugin->stdout_conn = NULL;
|
|
||||||
if (plugin->start_cmd) {
|
|
||||||
plugin_cmd_killed(plugin->start_cmd, plugin,
|
|
||||||
"Plugin exited before completing handshake.");
|
|
||||||
plugin->start_cmd = NULL;
|
|
||||||
}
|
|
||||||
tal_free(plugin);
|
|
||||||
check_plugins_resolved(plugins);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct io_plan *plugin_stdin_conn_init(struct io_conn *conn,
|
struct io_plan *plugin_stdin_conn_init(struct io_conn *conn,
|
||||||
|
@ -541,18 +531,15 @@ struct io_plan *plugin_stdin_conn_init(struct io_conn *conn,
|
||||||
{
|
{
|
||||||
/* We write to their stdin */
|
/* We write to their stdin */
|
||||||
/* We don't have anything queued yet, wait for notification */
|
/* We don't have anything queued yet, wait for notification */
|
||||||
plugin->stdin_conn = tal_steal(plugin, conn);
|
return io_wait(conn, plugin, plugin_write_json, plugin);
|
||||||
plugin->stdin_conn = conn;
|
|
||||||
return io_wait(plugin->stdin_conn, plugin, plugin_write_json, plugin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct io_plan *plugin_stdout_conn_init(struct io_conn *conn,
|
struct io_plan *plugin_stdout_conn_init(struct io_conn *conn,
|
||||||
struct plugin *plugin)
|
struct plugin *plugin)
|
||||||
{
|
{
|
||||||
/* We read from their stdout */
|
/* We read from their stdout */
|
||||||
plugin->stdout_conn = conn;
|
|
||||||
io_set_finish(conn, plugin_conn_finish, plugin);
|
io_set_finish(conn, plugin_conn_finish, plugin);
|
||||||
return io_read_partial(plugin->stdout_conn, plugin->buffer,
|
return io_read_partial(conn, plugin->buffer,
|
||||||
tal_bytelen(plugin->buffer), &plugin->len_read,
|
tal_bytelen(plugin->buffer), &plugin->len_read,
|
||||||
plugin_read_json, plugin);
|
plugin_read_json, plugin);
|
||||||
}
|
}
|
||||||
|
@ -1260,8 +1247,8 @@ const char *plugin_send_getmanifest(struct plugin *p)
|
||||||
|
|
||||||
/* Create two connections, one read-only on top of p->stdout, and one
|
/* Create two connections, one read-only on top of p->stdout, and one
|
||||||
* write-only on p->stdin */
|
* write-only on p->stdin */
|
||||||
io_new_conn(p, stdout, plugin_stdout_conn_init, p);
|
p->stdout_conn = io_new_conn(p, stdout, plugin_stdout_conn_init, p);
|
||||||
io_new_conn(p, stdin, plugin_stdin_conn_init, p);
|
p->stdin_conn = io_new_conn(p, stdin, plugin_stdin_conn_init, p);
|
||||||
req = jsonrpc_request_start(p, "getmanifest", p->log,
|
req = jsonrpc_request_start(p, "getmanifest", p->log,
|
||||||
plugin_manifest_cb, p);
|
plugin_manifest_cb, p);
|
||||||
jsonrpc_request_end(req);
|
jsonrpc_request_end(req);
|
||||||
|
|
Loading…
Add table
Reference in a new issue