| 860 | } |
| 861 | |
| 862 | static void wait_and_check_bitcoind(struct plugin *p) |
| 863 | { |
| 864 | int in, from, status, ret; |
| 865 | pid_t child; |
| 866 | const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL); |
| 867 | bool printed = false; |
| 868 | char *output = NULL; |
| 869 | |
| 870 | for (;;) { |
| 871 | tal_free(output); |
| 872 | |
| 873 | child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd)); |
| 874 | |
| 875 | if (bitcoind->rpcpass) |
| 876 | write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass)); |
| 877 | |
| 878 | close(in); |
| 879 | |
| 880 | if (child < 0) { |
| 881 | if (errno == ENOENT) |
| 882 | bitcoind_failure(p, "bitcoin-cli not found. Is bitcoin-cli " |
| 883 | "(part of Bitcoin Core) available in your PATH?"); |
| 884 | plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno)); |
| 885 | } |
| 886 | |
| 887 | output = grab_fd(cmd, from); |
| 888 | |
| 889 | while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR); |
| 890 | if (ret != child) |
| 891 | bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s", |
| 892 | cmd[0], strerror(errno))); |
| 893 | if (!WIFEXITED(status)) |
| 894 | bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i", |
| 895 | cmd[0], WTERMSIG(status))); |
| 896 | |
| 897 | if (WEXITSTATUS(status) == 0) |
| 898 | break; |
| 899 | |
| 900 | /* bitcoin/src/rpc/protocol.h: |
| 901 | * RPC_IN_WARMUP = -28, //!< Client still warming up |
| 902 | */ |
| 903 | if (WEXITSTATUS(status) != 28) { |
| 904 | if (WEXITSTATUS(status) == 1) |
| 905 | bitcoind_failure(p, "Could not connect to bitcoind using" |
| 906 | " bitcoin-cli. Is bitcoind running?"); |
| 907 | bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s", |
| 908 | cmd[0], WEXITSTATUS(status), output)); |
| 909 | } |
| 910 | |
| 911 | if (!printed) { |
| 912 | plugin_log(p, LOG_UNUSUAL, |
| 913 | "Waiting for bitcoind to warm up..."); |
| 914 | printed = true; |
| 915 | } |
| 916 | sleep(1); |
| 917 | } |
| 918 | |
| 919 | parse_getnetworkinfo_result(p, output); |
no test coverage detected