MCPcopy Create free account
hub / github.com/ElementsProject/lightning / handle_init

Function handle_init

plugins/libplugin.c:1023–1110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1021}
1022
1023static struct command_result *handle_init(struct command *cmd,
1024 const char *buf,
1025 const jsmntok_t *params)
1026{
1027 const jsmntok_t *configtok, *opttok, *t;
1028 struct sockaddr_un addr;
1029 size_t i;
1030 char *dir, *network;
1031 struct plugin *p = cmd->plugin;
1032 bool with_rpc = p->rpc_conn != NULL;
1033 const char *err;
1034
1035 configtok = json_get_member(buf, params, "configuration");
1036 err = json_scan(tmpctx, buf, configtok,
1037 "{lightning-dir:%"
1038 ",network:%"
1039 ",feature_set:%"
1040 ",rpc-file:%}",
1041 JSON_SCAN_TAL(tmpctx, json_strdup, &dir),
1042 JSON_SCAN_TAL(tmpctx, json_strdup, &network),
1043 JSON_SCAN_TAL(p, json_to_feature_set, &p->our_features),
1044 JSON_SCAN_TAL(p, json_strdup, &p->rpc_location));
1045 if (err)
1046 plugin_err(p, "cannot scan init params: %s: %.*s",
1047 err, json_tok_full_len(params),
1048 json_tok_full(buf, params));
1049
1050 /* Move into lightning directory: other files are relative */
1051 if (chdir(dir) != 0)
1052 plugin_err(p, "chdir to %s: %s", dir, strerror(errno));
1053
1054 chainparams = chainparams_for_network(network);
1055
1056 /* Only attempt to connect if the plugin has configured the rpc_conn
1057 * already, if that's not the case we were told to run without an RPC
1058 * connection, so don't even log an error. */
1059 /* FIXME: Move this to its own function so we can initialize at a
1060 * later point in time. */
1061 if (p->rpc_conn != NULL) {
1062 p->rpc_conn->fd = socket(AF_UNIX, SOCK_STREAM, 0);
1063 if (strlen(p->rpc_location) + 1 > sizeof(addr.sun_path))
1064 plugin_err(p, "rpc filename '%s' too long",
1065 p->rpc_location);
1066 strcpy(addr.sun_path, p->rpc_location);
1067 addr.sun_family = AF_UNIX;
1068
1069 if (connect(p->rpc_conn->fd, (struct sockaddr *)&addr,
1070 sizeof(addr)) != 0) {
1071 with_rpc = false;
1072 plugin_log(p, LOG_UNUSUAL,
1073 "Could not connect to '%s': %s",
1074 p->rpc_location, strerror(errno));
1075 }
1076
1077 membuf_init(&p->rpc_conn->mb, tal_arr(p, char, READ_CHUNKSIZE),
1078 READ_CHUNKSIZE, membuf_tal_realloc);
1079
1080 }

Callers 1

ld_command_handleFunction · 0.85

Calls 13

chainparams_for_networkFunction · 0.85
tal_freeFunction · 0.85
json_out_objFunction · 0.85
initMethod · 0.80
plugin_errFunction · 0.70
plugin_logFunction · 0.70
command_successFunction · 0.70
json_get_memberFunction · 0.50
json_scanFunction · 0.50
json_tok_full_lenFunction · 0.50
json_tok_fullFunction · 0.50
connectFunction · 0.50

Tested by

no test coverage detected