Add option to specify pid file

Closes #969
This commit is contained in:
libbitc 2018-02-19 23:00:09 +00:00 committed by Rusty Russell
parent bd95aba7a5
commit c360cb7b1f
3 changed files with 42 additions and 0 deletions

View File

@ -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>
@ -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>
@ -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();

View File

@ -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;

View File

@ -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=<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,