From c360cb7b1f6a2d9d1399428433f79b8c1c69245f Mon Sep 17 00:00:00 2001 From: libbitc Date: Mon, 19 Feb 2018 23:00:09 +0000 Subject: [PATCH] Add option to specify pid file Closes #969 --- lightningd/lightningd.c | 32 ++++++++++++++++++++++++++++++++ lightningd/lightningd.h | 3 +++ lightningd/options.c | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index fa1f2365c..1f0b70eed 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +41,8 @@ char *bitcoin_datadir; struct backtrace_state *backtrace_state; +int pid_fd; + static struct lightningd *new_lightningd(const tal_t *ctx) { struct lightningd *ld = tal(ctx, struct lightningd); @@ -70,6 +74,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx) ld->topology = new_topology(ld, ld->log); ld->debug_subdaemon_io = NULL; ld->daemon = false; + ld->pidfile = NULL; return ld; } @@ -250,6 +255,28 @@ static void daemonize_but_keep_dir(void) tal_free(cwd); } +static void pidfile_create(const struct lightningd *ld) +{ + char *pid; + const tal_t *tmpctx = tal_tmpctx(NULL); + + /* Create PID file */ + pid_fd = open(ld->pidfile, O_WRONLY|O_CREAT, 0640); + if (pid_fd < 0) + err(1, "Failed to open PID file"); + + /* Lock PID file */ + if (lockf(pid_fd, F_TLOCK, 0) < 0) + /* Problem locking file */ + err(1, "lightningd already running? Error locking PID file"); + + /* Get current PID and write to PID fie */ + pid = tal_fmt(tmpctx, "%d\n", getpid()); + write_all(pid_fd, pid, strlen(pid)); + + tal_free(tmpctx); +} + int main(int argc, char *argv[]) { struct lightningd *ld; @@ -280,6 +307,9 @@ int main(int argc, char *argv[]) /* Handle options and config; move to .lightningd */ newdir = handle_opts(ld, argc, argv); + /* Create PID file */ + pidfile_create(ld); + /* Ignore SIGPIPE: we look at our write return values*/ signal(SIGPIPE, SIG_IGN); @@ -384,6 +414,8 @@ int main(int argc, char *argv[]) } shutdown_subdaemons(ld); + close(pid_fd); + remove(ld->pidfile); tal_free(ld); opt_free_table(); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 064dc24f7..960d6247c 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -142,6 +142,9 @@ struct lightningd { /* Transaction filter matching what we're interested in */ struct txfilter *owned_txfilter; + /* PID file */ + char *pidfile; + /* May be useful for non-developers debugging in the field */ char *debug_subdaemon_io; diff --git a/lightningd/options.c b/lightningd/options.c index 0bb98e831..857ebb356 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -449,6 +449,10 @@ static void setup_default_config(struct lightningd *ld) ld->config = testnet_config; else ld->config = mainnet_config; + + /* Set default PID file name to be per-network */ + tal_free(ld->pidfile); + ld->pidfile = tal_fmt(ld, "lightningd-%s.pid", get_chainparams(ld)->network_name); } @@ -560,6 +564,9 @@ void register_opts(struct lightningd *ld) opt_register_arg("--bitcoin-rpcconnect", opt_set_talstr, NULL, &ld->topology->bitcoind->rpcconnect, "bitcoind RPC host to connect to"); + opt_register_arg("--pid-file=", opt_set_talstr, opt_show_charp, + &ld->pidfile, + "Specify pid file"); opt_register_arg( "--channel-update-interval=", opt_set_u32, opt_show_u32,