mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
lightningd/lightningd: start HSM at initialization time.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
015eb072b8
commit
542e6844f7
@ -16,6 +16,7 @@ LIGHTNINGD_OLD_SRC := \
|
||||
daemon/opt_time.c \
|
||||
daemon/pseudorand.c \
|
||||
daemon/routing.c \
|
||||
daemon/timeout.c \
|
||||
daemon/watch.c
|
||||
LIGHTNINGD_OLD_OBJS := $(LIGHTNINGD_OLD_SRC:.c=.o)
|
||||
LIGHTNINGD_OLD_HEADERS := $(LIGHTNINGD_OLD_SRC:.c=.h)
|
||||
@ -27,6 +28,7 @@ LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o)
|
||||
LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h)
|
||||
|
||||
LIGHTNINGD_SRC := \
|
||||
lightningd/hsm_control.c \
|
||||
lightningd/lightningd.c \
|
||||
lightningd/subdaemon.c
|
||||
|
||||
@ -35,10 +37,12 @@ LIGHTNINGD_OBJS := $(LIGHTNINGD_SRC:.c=.o)
|
||||
LIGHTNINGD_JSMN_OBJS := daemon/jsmn.o
|
||||
LIGHTNINGD_JSMN_HEADERS := daemon/jsmn/jsmn.h
|
||||
|
||||
LIGHTNINGD_HEADERS := lightningd/lightningd.h \
|
||||
LIGHTNINGD_HEADERS := \
|
||||
lightningd/hsm_control.h \
|
||||
lightningd/lightningd.h \
|
||||
lightningd/subdaemon.h
|
||||
|
||||
$(LIGHTNINGD_OBJS) $(LIGHTNINGD_LIB_OBJS): $(LIGHTNINGD_HEADERS) $(LIGHTNINGD_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(CCAN_HEADERS) $(DAEMON_HEADERS) $(LIBBASE58_HEADERS)
|
||||
$(LIGHTNINGD_OBJS) $(LIGHTNINGD_LIB_OBJS): $(LIGHTNINGD_HEADERS) $(LIGHTNINGD_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(CCAN_HEADERS) $(DAEMON_HEADERS) $(LIGHTNINGD_HSM_CONTROL_HEADERS) $(LIBBASE58_HEADERS)
|
||||
|
||||
include lightningd/hsm/Makefile
|
||||
|
||||
@ -53,7 +57,7 @@ check-whitespace: $(LIGHTNINGD_SRC:%=check-whitespace/%) $(LIGHTNINGD_HEADERS:%=
|
||||
check-lightningd-makefile:
|
||||
@if [ "`ls lightningd/*.h | grep -v lightningd/gen | tr '\012' ' '`" != "`echo $(LIGHTNINGD_HEADERS) ''`" ]; then echo LIGHTNINGD_HEADERS incorrect; exit 1; fi
|
||||
|
||||
lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_OLD_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_JSMN_OBJS) $(CORE_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a
|
||||
lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_OLD_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_JSMN_OBJS) $(CORE_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(LIGHTNINGD_HSM_CONTROL_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a
|
||||
|
||||
clean: lightningd-clean
|
||||
|
||||
|
@ -12,8 +12,10 @@ LIGHTNINGD_HSM_CLIENT_SRC := lightningd/hsm/client.c lightningd/hsm/gen_hsm_clie
|
||||
LIGHTNINGD_HSM_CLIENT_OBJS := $(LIGHTNINGD_HSM_CLIENT_SRC:.c=.o)
|
||||
|
||||
# Control daemon uses this:
|
||||
LIGHTNINGD_HSM_CONTROL_HEADERS := lightningd/hsm/gen_hsm_control_wire.h
|
||||
LIGHTNINGD_HSM_CONTROL_SRC := lightningd/hsm/gen_hsm_control_wire.c
|
||||
LIGHTNINGD_HSM_CONTROL_HEADERS := lightningd/hsm/gen_hsm_control_wire.h \
|
||||
lightningd/hsm/gen_hsm_status_wire.h
|
||||
LIGHTNINGD_HSM_CONTROL_SRC := lightningd/hsm/gen_hsm_control_wire.c \
|
||||
lightningd/hsm/gen_hsm_status_wire.c
|
||||
LIGHTNINGD_HSM_CONTROL_OBJS := $(LIGHTNINGD_HSM_CONTROL_SRC:.c=.o)
|
||||
|
||||
# lightningd/hsm needs these:
|
||||
|
55
lightningd/hsm_control.c
Normal file
55
lightningd/hsm_control.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include "hsm_control.h"
|
||||
#include "lightningd.h"
|
||||
#include "subdaemon.h"
|
||||
#include <ccan/err/err.h>
|
||||
#include <ccan/io/io.h>
|
||||
#include <ccan/take/take.h>
|
||||
#include <daemon/log.h>
|
||||
#include <lightningd/hsm/gen_hsm_control_wire.h>
|
||||
#include <lightningd/hsm/gen_hsm_status_wire.h>
|
||||
|
||||
static void hsm_init_done(struct subdaemon *hsm, const u8 *msg,
|
||||
struct lightningd *ld)
|
||||
{
|
||||
if (!fromwire_hsmctl_init_response(msg, NULL, &ld->dstate.id))
|
||||
errx(1, "HSM did not give init response");
|
||||
|
||||
log_info_struct(ld->log, "Our ID: %s", struct pubkey, &ld->dstate.id);
|
||||
io_break(ld->hsm);
|
||||
}
|
||||
|
||||
static void hsm_finished(struct subdaemon *hsm, int status)
|
||||
{
|
||||
if (WIFEXITED(status))
|
||||
errx(1, "HSM failed (exit status %i), exiting.",
|
||||
WEXITSTATUS(status));
|
||||
errx(1, "HSM failed (signal %u), exiting.", WTERMSIG(status));
|
||||
}
|
||||
|
||||
void hsm_init(struct lightningd *ld, bool newdir)
|
||||
{
|
||||
bool create;
|
||||
|
||||
ld->hsm = new_subdaemon(ld, ld, "lightningd_hsm",
|
||||
hsm_status_wire_type_name,
|
||||
hsm_control_wire_type_name,
|
||||
hsm_finished, -1);
|
||||
if (!ld->hsm)
|
||||
err(1, "Could not subdaemon hsm");
|
||||
|
||||
if (newdir)
|
||||
create = true;
|
||||
else
|
||||
create = (access("hsm_secret", F_OK) != 0);
|
||||
|
||||
if (create)
|
||||
subdaemon_req(ld->hsm, take(towire_hsmctl_init_new(ld->hsm)),
|
||||
-1, NULL, hsm_init_done, ld);
|
||||
else
|
||||
subdaemon_req(ld->hsm, take(towire_hsmctl_init_load(ld->hsm)),
|
||||
-1, NULL, hsm_init_done, ld);
|
||||
|
||||
if (io_loop(NULL, NULL) != ld->hsm)
|
||||
errx(1, "Unexpected io exit during HSM startup");
|
||||
}
|
||||
|
9
lightningd/hsm_control.h
Normal file
9
lightningd/hsm_control.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef LIGHTNING_LIGHTNINGD_HSM_CONTROL_H
|
||||
#define LIGHTNING_LIGHTNINGD_HSM_CONTROL_H
|
||||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
struct lightningd;
|
||||
|
||||
void hsm_init(struct lightningd *ld, bool newdir);
|
||||
#endif /* LIGHTNING_LIGHTNINGD_HSM_CONTROL_H */
|
@ -1,6 +1,9 @@
|
||||
#include "hsm_control.h"
|
||||
#include "lightningd.h"
|
||||
#include "subdaemon.h"
|
||||
#include <ccan/array_size/array_size.h>
|
||||
#include <ccan/err/err.h>
|
||||
#include <ccan/io/io.h>
|
||||
#include <ccan/pipecmd/pipecmd.h>
|
||||
#include <ccan/take/take.h>
|
||||
#include <ccan/tal/grab_file/grab_file.h>
|
||||
@ -11,6 +14,7 @@
|
||||
#include <daemon/log.h>
|
||||
#include <daemon/options.h>
|
||||
#include <daemon/routing.h>
|
||||
#include <daemon/timeout.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
@ -137,6 +141,7 @@ static const char *find_my_path(const tal_t *ctx, const char *argv0)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct lightningd *ld = new_lightningd(NULL);
|
||||
bool newdir;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@ -147,7 +152,7 @@ int main(int argc, char *argv[])
|
||||
ld->daemon_dir = find_my_path(ld, argv[0]);
|
||||
|
||||
/* Handle options and config; move to .lightningd */
|
||||
handle_opts(&ld->dstate, argc, argv);
|
||||
newdir = handle_opts(&ld->dstate, argc, argv);
|
||||
|
||||
/* Activate crash log now we're in the right place. */
|
||||
crashlog_activate(ld->log);
|
||||
@ -161,9 +166,10 @@ int main(int argc, char *argv[])
|
||||
/* Mark ourselves live. */
|
||||
log_info(ld->log, "Hello world from %s!", version());
|
||||
|
||||
#if 0
|
||||
/* Set up HSM. */
|
||||
hsm_init(dstate, &dstate->id);
|
||||
hsm_init(ld, newdir);
|
||||
|
||||
#if 0
|
||||
|
||||
/* Initialize block topology. */
|
||||
setup_topology(dstate);
|
||||
@ -176,19 +182,19 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* Ready for connections from peers. */
|
||||
setup_listeners(dstate);
|
||||
#endif
|
||||
|
||||
for (;;) {
|
||||
struct timer *expired;
|
||||
void *v = io_loop(&dstate->timers, &expired);
|
||||
void *v = io_loop(&ld->dstate.timers, &expired);
|
||||
|
||||
/* We use io_break(dstate) to shut down. */
|
||||
if (v == dstate)
|
||||
if (v == ld)
|
||||
break;
|
||||
|
||||
if (expired)
|
||||
timer_expired(dstate, expired);
|
||||
timer_expired(&ld->dstate, expired);
|
||||
}
|
||||
#endif
|
||||
|
||||
tal_free(ld);
|
||||
opt_free_table();
|
||||
|
@ -20,5 +20,8 @@ struct lightningd {
|
||||
|
||||
/* Log for general stuff. */
|
||||
struct log *log;
|
||||
|
||||
/* Bearer of all my secrets. */
|
||||
struct subdaemon *hsm;
|
||||
};
|
||||
#endif /* LIGHTNING_LIGHTNINGD_LIGHTNINGD_H */
|
||||
|
Loading…
Reference in New Issue
Block a user