Browse Source

lightningd: implement --daemon.

Includes closing off stdout and stderr.  We don't do it directly in the
arg parser, as we want to interact normally (eg with other errors) before
we turn off stdout/stderr.

Fixes: #986
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
eb17d6af71
  1. 25
      lightningd/lightningd.c
  2. 3
      lightningd/lightningd.h
  3. 3
      lightningd/options.c

25
lightningd/lightningd.c

@ -7,6 +7,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/daemonize/daemonize.h>
#include <ccan/err/err.h>
#include <ccan/io/fdpass/fdpass.h>
#include <ccan/io/io.h>
@ -22,6 +23,7 @@
#include <common/utils.h>
#include <common/version.h>
#include <common/wireaddr.h>
#include <errno.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/invoice.h>
@ -68,6 +70,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx,
timers_init(&ld->timers, time_mono());
ld->topology = new_topology(ld, ld->log);
ld->debug_subdaemon_io = NULL;
ld->daemon = false;
return ld;
}
@ -230,6 +233,24 @@ static void init_txfilter(struct wallet *w, struct txfilter *filter)
}
}
static void daemonize_but_keep_dir(void)
{
/* daemonize moves us into /, but we want to be here */
const char *cwd = path_cwd(NULL);
if (!cwd)
fatal("Could not get current directory: %s", strerror(errno));
if (!daemonize())
fatal("Could not become a daemon: %s", strerror(errno));
/* Move back: important, since lightning dir may be relative! */
if (chdir(cwd) != 0)
fatal("Could not return to directory %s: %s",
cwd, strerror(errno));
tal_free(cwd);
}
int main(int argc, char *argv[])
{
struct log_book *log_book;
@ -331,6 +352,10 @@ int main(int argc, char *argv[])
/* Create RPC socket (if any) */
setup_jsonrpc(ld, ld->rpc_filename);
/* Now we're about to start, become daemon if desired. */
if (ld->daemon)
daemonize_but_keep_dir();
/* Mark ourselves live. */
log_info(ld->log, "Server started with public key %s, alias %s (color #%s) and lightningd %s",
type_to_string(ltmp, struct pubkey, &ld->id),

3
lightningd/lightningd.h

@ -77,6 +77,9 @@ struct lightningd {
/* The directory to find all the subdaemons. */
const char *daemon_dir;
/* Are we told to run in the background. */
bool daemon;
/* Our config dir, and rpc file */
char *config_dir;
char *rpc_filename;

3
lightningd/options.c

@ -7,6 +7,7 @@
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/memleak.h>
@ -211,6 +212,8 @@ static char *opt_set_fee_rates(const char *arg, struct chain_topology *topo)
static void config_register_opts(struct lightningd *ld)
{
opt_register_noarg("--daemon", opt_set_bool, &ld->daemon,
"Run in the background, suppress stdout/stderr");
opt_register_arg("--ignore-fee-limits", opt_set_bool_arg, opt_show_bool,
&ld->config.ignore_fee_limits,
"(DANGEROUS) allow peer to set any feerate");

Loading…
Cancel
Save