2017-03-10 11:48:43 +01:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_SUBD_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_SUBD_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/list/list.h>
|
|
|
|
#include <ccan/short_types/short_types.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
#include <lightningd/msg_queue.h>
|
|
|
|
|
|
|
|
struct io_conn;
|
|
|
|
|
|
|
|
/* By convention, replies are requests + 100 */
|
|
|
|
#define SUBD_REPLY_OFFSET 100
|
2017-06-24 08:25:53 +02:00
|
|
|
/* And reply failures are requests + 200 */
|
|
|
|
#define SUBD_REPLYFAIL_OFFSET 200
|
2017-03-10 11:48:43 +01:00
|
|
|
|
|
|
|
/* One of our subds. */
|
|
|
|
struct subd {
|
|
|
|
/* Name, like John, or "lightningd_hsm" */
|
|
|
|
const char *name;
|
|
|
|
/* The Big Cheese. */
|
|
|
|
struct lightningd *ld;
|
|
|
|
/* pid, for waiting for status when it dies. */
|
|
|
|
int pid;
|
|
|
|
/* Connection. */
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
|
|
|
/* If we are associated with a single peer, this points to it. */
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
/* For logging */
|
|
|
|
struct log *log;
|
|
|
|
|
|
|
|
/* Callback when non-reply message comes in. */
|
2017-04-01 12:58:30 +02:00
|
|
|
int (*msgcb)(struct subd *, const u8 *, const int *);
|
2017-03-10 11:48:43 +01:00
|
|
|
const char *(*msgname)(int msgtype);
|
|
|
|
void (*finished)(struct subd *sd, int status);
|
|
|
|
|
|
|
|
/* Buffer for input. */
|
|
|
|
u8 *msg_in;
|
2017-03-19 21:31:35 +01:00
|
|
|
|
|
|
|
/* While we're reading fds in. */
|
|
|
|
size_t num_fds_in_read;
|
|
|
|
int *fds_in;
|
2017-03-10 11:48:43 +01:00
|
|
|
|
|
|
|
/* Messages queue up here. */
|
|
|
|
struct msg_queue outq;
|
|
|
|
|
|
|
|
/* Callbacks for replies. */
|
|
|
|
struct list_head reqs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* new_subd - create a new subdaemon.
|
|
|
|
* @ctx: context to allocate from
|
|
|
|
* @ld: global state
|
|
|
|
* @name: basename of daemon
|
2017-05-22 13:25:01 +02:00
|
|
|
* @peer: peer to associate (if any).
|
2017-03-10 11:48:43 +01:00
|
|
|
* @msgname: function to get name from messages
|
|
|
|
* @msgcb: function to call when non-fatal message received (or NULL)
|
|
|
|
* @finished: function to call when it's finished (with exit status).
|
2017-05-25 04:16:03 +02:00
|
|
|
* @...: NULL-terminated list of pointers to fds to hand as fd 3, 4...
|
|
|
|
* (can be take, if so, set to -1)
|
2017-03-10 11:48:43 +01:00
|
|
|
*
|
2017-03-19 21:31:35 +01:00
|
|
|
* @msgcb gets called with @fds set to NULL: if it returns a positive number,
|
2017-04-01 12:58:30 +02:00
|
|
|
* that many @fds are received before calling again. If it returns -1, the
|
|
|
|
* subdaemon is shutdown.
|
2017-03-10 11:48:43 +01:00
|
|
|
*/
|
|
|
|
struct subd *new_subd(const tal_t *ctx,
|
|
|
|
struct lightningd *ld,
|
|
|
|
const char *name,
|
|
|
|
struct peer *peer,
|
|
|
|
const char *(*msgname)(int msgtype),
|
2017-04-01 12:58:30 +02:00
|
|
|
int (*msgcb)(struct subd *, const u8 *, const int *fds),
|
2017-03-10 11:48:43 +01:00
|
|
|
void (*finished)(struct subd *, int), ...);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* subd_send_msg - queue a message to the subdaemon.
|
|
|
|
* @sd: subdaemon to request
|
|
|
|
* @msg_out: message (can be take)
|
|
|
|
*/
|
|
|
|
void subd_send_msg(struct subd *sd, const u8 *msg_out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* subd_send_msg - queue a file descriptor to pass to the subdaemon.
|
|
|
|
* @sd: subdaemon to request
|
|
|
|
* @fd: the file descriptor (closed after passing).
|
|
|
|
*/
|
|
|
|
void subd_send_fd(struct subd *sd, int fd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* subd_req - queue a request to the subdaemon.
|
2017-04-01 12:58:30 +02:00
|
|
|
* @ctx: lifetime for the callback: if this is freed, don't call replycb.
|
2017-03-10 11:48:43 +01:00
|
|
|
* @sd: subdaemon to request
|
|
|
|
* @msg_out: request message (can be take)
|
|
|
|
* @fd_out: if >=0 fd to pass at the end of the message (closed after)
|
2017-06-24 08:25:53 +02:00
|
|
|
* @num_fds_in: how many fds to read in to hand to @replycb if it's a reply.
|
2017-03-10 11:48:43 +01:00
|
|
|
* @replycb: callback when reply comes in, returns false to shutdown daemon.
|
|
|
|
* @replycb_data: final arg to hand to @replycb
|
|
|
|
*
|
|
|
|
* @replycb cannot free @sd, so it returns false to remove it.
|
2017-06-24 08:25:53 +02:00
|
|
|
* Note that @replycb is called for replies of type @msg_out + SUBD_REPLY_OFFSET
|
|
|
|
* with @num_fds_in fds, or type @msg_out + SUBD_REPLYFAIL_OFFSET with no fds.
|
2017-03-10 11:48:43 +01:00
|
|
|
*/
|
2017-04-01 12:58:30 +02:00
|
|
|
#define subd_req(ctx, sd, msg_out, fd_out, num_fds_in, replycb, replycb_data) \
|
|
|
|
subd_req_((ctx), (sd), (msg_out), (fd_out), (num_fds_in), \
|
2017-03-19 21:31:30 +01:00
|
|
|
typesafe_cb_preargs(bool, void *, \
|
|
|
|
(replycb), (replycb_data), \
|
|
|
|
struct subd *, \
|
|
|
|
const u8 *, const int *), \
|
2017-03-10 11:48:43 +01:00
|
|
|
(replycb_data))
|
2017-04-01 12:58:30 +02:00
|
|
|
void subd_req_(const tal_t *ctx,
|
|
|
|
struct subd *sd,
|
2017-03-19 21:31:30 +01:00
|
|
|
const u8 *msg_out,
|
|
|
|
int fd_out, size_t num_fds_in,
|
|
|
|
bool (*replycb)(struct subd *, const u8 *, const int *, void *),
|
|
|
|
void *replycb_data);
|
2017-03-10 11:48:43 +01:00
|
|
|
|
2017-04-12 08:52:32 +02:00
|
|
|
/**
|
|
|
|
* subd_shutdown - try to politely shut down a subdaemon.
|
|
|
|
* @subd: subd to shutdown.
|
|
|
|
* @seconds: maximum seconds to wait for it to exit.
|
|
|
|
*
|
|
|
|
* This closes the fd to the subdaemon, and gives it a little while to exit.
|
|
|
|
* The @finished callback will never be called.
|
|
|
|
*/
|
|
|
|
void subd_shutdown(struct subd *subd, unsigned int seconds);
|
|
|
|
|
2017-03-10 11:48:43 +01:00
|
|
|
char *opt_subd_debug(const char *optarg, struct lightningd *ld);
|
2017-05-24 12:10:16 +02:00
|
|
|
char *opt_subd_dev_disconnect(const char *optarg, struct lightningd *ld);
|
2017-03-10 11:48:43 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_SUBD_H */
|