Skip to content

Commit

Permalink
Add option to specify pid file
Browse files Browse the repository at this point in the history
  • Loading branch information
libbitc authored and rustyrussell committed Feb 20, 2018
1 parent bd95aba commit c360cb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ccan/io/io.h>
#include <ccan/noerr/noerr.h>
#include <ccan/pipecmd/pipecmd.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
Expand All @@ -24,6 +25,7 @@
#include <common/version.h>
#include <common/wireaddr.h>
#include <errno.h>
#include <fcntl.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/invoice.h>
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions lightningd/lightningd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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=<file>", opt_set_talstr, opt_show_charp,
&ld->pidfile,
"Specify pid file");

opt_register_arg(
"--channel-update-interval=<s>", opt_set_u32, opt_show_u32,
Expand Down

0 comments on commit c360cb7

Please sign in to comment.