Browse Source

subd: expose raw API for getting a single fd to a subdaemon.

We're going to use this for the HSM.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
6e59f85666
  1. 36
      lightningd/subd.c
  2. 9
      lightningd/subd.h

36
lightningd/subd.c

@ -126,12 +126,13 @@ static struct subd_req *get_req(struct subd *sd, int reply_type)
} }
/* We use sockets, not pipes, because fds are bidir. */ /* We use sockets, not pipes, because fds are bidir. */
static int subd(const char *dir, const char *name, bool debug, static int subd(const char *dir, const char *name, const char *debug_subdaemon,
int *msgfd, int dev_disconnect_fd, va_list ap) int *msgfd, int dev_disconnect_fd, va_list *ap)
{ {
int childmsg[2], execfail[2]; int childmsg[2], execfail[2];
pid_t childpid; pid_t childpid;
int err, *fd; int err, *fd;
bool debug = debug_subdaemon && strends(name, debug_subdaemon);
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0) if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0)
goto fail; goto fail;
@ -169,13 +170,15 @@ static int subd(const char *dir, const char *name, bool debug,
} }
/* Dup any extra fds up first. */ /* Dup any extra fds up first. */
while ((fd = va_arg(ap, int *)) != NULL) { if (ap) {
while ((fd = va_arg(*ap, int *)) != NULL) {
/* If this were stdin, dup2 closed! */ /* If this were stdin, dup2 closed! */
assert(*fd != STDIN_FILENO); assert(*fd != STDIN_FILENO);
if (!move_fd(*fd, fdnum)) if (!move_fd(*fd, fdnum))
goto child_errno_fail; goto child_errno_fail;
fdnum++; fdnum++;
} }
}
/* Make (fairly!) sure all other fds are closed. */ /* Make (fairly!) sure all other fds are closed. */
max = sysconf(_SC_OPEN_MAX); max = sysconf(_SC_OPEN_MAX);
@ -201,12 +204,14 @@ static int subd(const char *dir, const char *name, bool debug,
close(childmsg[1]); close(childmsg[1]);
close(execfail[1]); close(execfail[1]);
while ((fd = va_arg(ap, int *)) != NULL) { if (ap) {
while ((fd = va_arg(*ap, int *)) != NULL) {
if (taken(fd)) { if (taken(fd)) {
close(*fd); close(*fd);
*fd = -1; *fd = -1;
} }
} }
}
/* Child will close this without writing on successful exec. */ /* Child will close this without writing on successful exec. */
if (read(execfail[0], &err, sizeof(err)) == sizeof(err)) { if (read(execfail[0], &err, sizeof(err)) == sizeof(err)) {
@ -229,6 +234,22 @@ fail:
return -1; return -1;
} }
int subd_raw(struct lightningd *ld, const char *name)
{
pid_t pid;
int msg_fd;
pid = subd(ld->daemon_dir, name, ld->dev_debug_subdaemon,
&msg_fd, ld->dev_disconnect_fd, NULL);
if (pid == (pid_t)-1) {
log_unusual(ld->log, "subd %s failed: %s",
name, strerror(errno));
return -1;
}
return msg_fd;
}
static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd); static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd);
static struct io_plan *sd_msg_reply(struct io_conn *conn, struct subd *sd, static struct io_plan *sd_msg_reply(struct io_conn *conn, struct subd *sd,
@ -394,13 +415,10 @@ struct subd *new_subd(const tal_t *ctx,
va_list ap; va_list ap;
struct subd *sd = tal(ctx, struct subd); struct subd *sd = tal(ctx, struct subd);
int msg_fd; int msg_fd;
bool debug;
debug = ld->dev_debug_subdaemon
&& strends(name, ld->dev_debug_subdaemon);
va_start(ap, finished); va_start(ap, finished);
sd->pid = subd(ld->daemon_dir, name, debug, &msg_fd, sd->pid = subd(ld->daemon_dir, name, ld->dev_debug_subdaemon,
ld->dev_disconnect_fd, ap); &msg_fd, ld->dev_disconnect_fd, &ap);
va_end(ap); va_end(ap);
if (sd->pid == (pid_t)-1) { if (sd->pid == (pid_t)-1) {
log_unusual(ld->log, "subd %s failed: %s", log_unusual(ld->log, "subd %s failed: %s",

9
lightningd/subd.h

@ -74,6 +74,13 @@ struct subd *new_subd(const tal_t *ctx,
int (*msgcb)(struct subd *, const u8 *, const int *fds), int (*msgcb)(struct subd *, const u8 *, const int *fds),
void (*finished)(struct subd *, int), ...); void (*finished)(struct subd *, int), ...);
/**
* subd_raw - raw interface to get a subdaemon on an fd (for HSM)
* @ld: global state
* @name: basename of daemon
*/
int subd_raw(struct lightningd *ld, const char *name);
/** /**
* subd_send_msg - queue a message to the subdaemon. * subd_send_msg - queue a message to the subdaemon.
* @sd: subdaemon to request * @sd: subdaemon to request
@ -82,7 +89,7 @@ struct subd *new_subd(const tal_t *ctx,
void subd_send_msg(struct subd *sd, const u8 *msg_out); void subd_send_msg(struct subd *sd, const u8 *msg_out);
/** /**
* subd_send_msg - queue a file descriptor to pass to the subdaemon. * subd_send_fd - queue a file descriptor to pass to the subdaemon.
* @sd: subdaemon to request * @sd: subdaemon to request
* @fd: the file descriptor (closed after passing). * @fd: the file descriptor (closed after passing).
*/ */

Loading…
Cancel
Save