Execute bitcoin-cli with pre-built command and optional stdin args. * Returns result with output and exit status. */
| 158 | /* Execute bitcoin-cli with pre-built command and optional stdin args. |
| 159 | * Returns result with output and exit status. */ |
| 160 | static struct bcli_result * |
| 161 | execute_bitcoin_cli(const tal_t *ctx, |
| 162 | struct plugin *plugin, |
| 163 | const char **cmd, |
| 164 | const char **stdinargs) |
| 165 | { |
| 166 | int in, from, status; |
| 167 | pid_t child; |
| 168 | struct bcli_result *res; |
| 169 | |
| 170 | child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd)); |
| 171 | if (child < 0) |
| 172 | plugin_err(plugin, "%s exec failed: %s", cmd[0], strerror(errno)); |
| 173 | |
| 174 | /* Send rpcpass via stdin if configured */ |
| 175 | if (bitcoind->rpcpass) { |
| 176 | write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass)); |
| 177 | write_all(in, "\n", 1); |
| 178 | } |
| 179 | /* Send any additional stdin args */ |
| 180 | if (stdinargs) { |
| 181 | for (size_t i = 0; i < tal_count(stdinargs); i++) { |
| 182 | write_all(in, stdinargs[i], strlen(stdinargs[i])); |
| 183 | write_all(in, "\n", 1); |
| 184 | } |
| 185 | } |
| 186 | close(in); |
| 187 | |
| 188 | /* Read all output until EOF */ |
| 189 | res = tal(ctx, struct bcli_result); |
| 190 | res->output = grab_fd_str(res, from); |
| 191 | res->output_len = strlen(res->output); |
| 192 | res->args = args_string(res, cmd, stdinargs); |
| 193 | close(from); |
| 194 | |
| 195 | /* Wait for child to exit */ |
| 196 | while (waitpid(child, &status, 0) < 0) { |
| 197 | if (errno == EINTR) |
| 198 | continue; |
| 199 | plugin_err(plugin, "waitpid(%s) failed: %s", |
| 200 | res->args, strerror(errno)); |
| 201 | } |
| 202 | |
| 203 | if (!WIFEXITED(status)) |
| 204 | plugin_err(plugin, "%s died with signal %i", |
| 205 | res->args, WTERMSIG(status)); |
| 206 | |
| 207 | res->exitstatus = WEXITSTATUS(status); |
| 208 | |
| 209 | return res; |
| 210 | } |
| 211 | |
| 212 | /* Synchronous execution of bitcoin-cli. |
| 213 | * Returns result with output and exit status. */ |
no test coverage detected