aboutsummaryrefslogtreecommitdiff
path: root/net-p2p/c-lightning/files/lightningd.in
blob: ffe735e76b27ce3c145d86e84bc819b6022da0a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
# $FreeBSD$

# PROVIDE: lightningd
# REQUIRE: LOGIN bitcoind cleanvar
# KEYWORD: shutdown

# Add the following to %%LOCALBASE%%/etc/rc.conf.d/lightningd to influence
# the behavior of this script (default values are listed):
#
# lightningd_enable="NO"  # change to "YES" to enable
# lightningd_user="%%U%%"
# lightningd_group="%%G%%"
# lightningd_base_dir="%%LIGHTNINGD_BASE_DIR%%"
# lightningd_network="bitcoin"
# lightningd_conf="%%PREFIX%%/etc/lightningd-${lightningd_network}.conf"
# lightningd_extra_args=""  # See lightningd --help

. /etc/rc.subr

name="lightningd"
rcvar=lightningd_enable

load_rc_config ${name}

: ${lightningd_enable:="NO"}
: ${lightningd_user:="%%U%%"}
: ${lightningd_group:="%%G%%"}
: ${lightningd_base_dir:="%%LIGHTNINGD_BASE_DIR%%"}
: ${lightningd_network:="bitcoin"}
: ${lightningd_conf:="%%PREFIX%%/etc/${name}-${lightningd_network}.conf"}

start_precmd="lightningd_start_precmd"
pidfile="${lightningd_base_dir}/lightningd-${lightningd_network}.pid"
command="%%PREFIX%%/bin/lightningd"
command_args=""
command_args="${command_args} --lightning-dir=${lightningd_base_dir}"
command_args="${command_args} --network=${lightningd_network}"
command_args="${command_args} --daemon"
# service(8) would execute us with LOCALBASE stripped out from PATH,
# thus specify the full path to bitcoin-cli.
command_args="${command_args} --bitcoin-cli=%%LOCALBASE%%/bin/bitcoin-cli"
# bitcoin-cli(1) tries to create its "datadir" (by default ${HOME}/.bitcoin)
# if it does not exist. Provide something that already exists to avoid the
# creation of unnecessary empty directories.
command_args="${command_args} --bitcoin-datadir=${lightningd_base_dir}"

if [ -e "${lightningd_conf}" ] ; then
	command_args="${command_args} --conf=${lightningd_conf}"
fi

command_args="${command_args} ${lightningd_extra_args}"

check_bitcoind_ready()
{
    bitcoin-cli \
        -rpcconnect=${bitcoin_addr}${bitcoin_port:+":"}${bitcoin_port} \
        -rpcuser=${bitcoin_user} \
        -stdinrpcpass \
        echo itworks <<PASS
${bitcoin_pass}
PASS
}

lightningd_start_precmd()
{
    # Wait for bitcoind to be fully operational. lightningd would quit (refuse
    # to start) if it can't talk to bitcoind via its RPC.
    bitcoin_addr="`grep ^bitcoin-rpcconnect= "${lightningd_conf}" |cut -f 2- -d =`"
    bitcoin_port="`grep ^bitcoin-rpcport= "${lightningd_conf}" |cut -f 2- -d =`"
    bitcoin_user="`grep ^bitcoin-rpcuser= "${lightningd_conf}" |cut -f 2- -d =`"
    bitcoin_pass="`grep ^bitcoin-rpcpassword= "${lightningd_conf}" |cut -f 2- -d =`"

    i=20
    while : ; do
        if [ $i -eq 0 ] ; then
            # Show errors from last attempt.
            if ! check_bitcoind_ready ; then
                echo "Failed: bitcoind did not start serving RPC, starting lightningd anyway"
            fi
            break
        else
            if check_bitcoind_ready > /dev/null 2>&1; then
                break
            fi
        fi
        echo "Waiting for bitcoind to start serving RPC, lightningd cannot start without it $i"
        sleep 1
        i=$((i - 1))
    done
}

run_rc_command "$1"