Browse Source

subd: use array of fd pointers, not fds, and use take().

This lets us specify that we want to keep some fds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
d1fcc434c8
  1. 2
      lightningd/gossip_control.c
  2. 2
      lightningd/hsm_control.c
  3. 4
      lightningd/new_connection.c
  4. 18
      lightningd/peer_control.c
  5. 22
      lightningd/subd.c
  6. 2
      lightningd/subd.h

2
lightningd/gossip_control.c

@ -152,7 +152,7 @@ void gossip_init(struct lightningd *ld)
u8 *init; u8 *init;
ld->gossip = new_subd(ld, ld, "lightningd_gossip", NULL, ld->gossip = new_subd(ld, ld, "lightningd_gossip", NULL,
gossip_wire_type_name, gossip_wire_type_name,
gossip_msg, gossip_finished, -1); gossip_msg, gossip_finished, NULL);
if (!ld->gossip) if (!ld->gossip)
err(1, "Could not subdaemon gossip"); err(1, "Could not subdaemon gossip");

2
lightningd/hsm_control.c

@ -92,7 +92,7 @@ void hsm_init(struct lightningd *ld, bool newdir)
ld->hsm = new_subd(ld, ld, "lightningd_hsm", NULL, ld->hsm = new_subd(ld, ld, "lightningd_hsm", NULL,
hsm_wire_type_name, hsm_wire_type_name,
hsm_msg, hsm_finished, -1); hsm_msg, hsm_finished, NULL);
if (!ld->hsm) if (!ld->hsm)
err(1, "Could not subd hsm"); err(1, "Could not subd hsm");

4
lightningd/new_connection.c

@ -181,7 +181,7 @@ static bool got_handshake_hsmfd(struct subd *hsm, const u8 *msg,
"lightningd_handshake", NULL, "lightningd_handshake", NULL,
handshake_wire_type_name, handshake_wire_type_name,
NULL, NULL, NULL, NULL,
fds[0], c->fd, -1); take(&fds[0]), take(&c->fd), NULL);
if (!handshaked) { if (!handshaked) {
log_unusual(ld->log, "Could not subdaemon handshake: %s", log_unusual(ld->log, "Could not subdaemon handshake: %s",
strerror(errno)); strerror(errno));
@ -191,8 +191,6 @@ static bool got_handshake_hsmfd(struct subd *hsm, const u8 *msg,
/* If handshake daemon fails, we just drop connection. */ /* If handshake daemon fails, we just drop connection. */
tal_steal(handshaked, c); tal_steal(handshaked, c);
/* We no longer own fd (closed; handshaked has copy). */
c->fd = -1;
if (c->known_id) { if (c->known_id) {
req = towire_handshake_initiator(c, &ld->dstate.id, req = towire_handshake_initiator(c, &ld->dstate.id,
c->known_id); c->known_id);

18
lightningd/peer_control.c

@ -1502,8 +1502,9 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp,
channel_wire_type_name, channel_wire_type_name,
channel_msg, channel_msg,
peer_owner_finished, peer_owner_finished,
peer->fd, take(&peer->fd),
peer->gossip_client_fd, fds[0], -1); take(&peer->gossip_client_fd),
take(&fds[0]), NULL);
if (!peer->owner) { if (!peer->owner) {
log_unusual(peer->log, "Could not subdaemon channel: %s", log_unusual(peer->log, "Could not subdaemon channel: %s",
strerror(errno)); strerror(errno));
@ -1777,15 +1778,13 @@ void peer_fundee_open(struct peer *peer, const u8 *from_peer)
peer->owner = new_subd(ld, ld, "lightningd_opening", peer, peer->owner = new_subd(ld, ld, "lightningd_opening", peer,
opening_wire_type_name, opening_wire_type_name,
NULL, peer_owner_finished, NULL, peer_owner_finished,
peer->fd, peer->gossip_client_fd, -1); take(&peer->fd), take(&peer->gossip_client_fd),
NULL);
if (!peer->owner) { if (!peer->owner) {
peer_fail(peer, "Failed to subdaemon opening: %s", peer_fail(peer, "Failed to subdaemon opening: %s",
strerror(errno)); strerror(errno));
return; return;
} }
/* We handed off peer fd and gossip fd */
peer->fd = -1;
peer->gossip_client_fd = -1;
/* They will open channel. */ /* They will open channel. */
peer->funder = REMOTE; peer->funder = REMOTE;
@ -1861,7 +1860,8 @@ static bool gossip_peer_released(struct subd *gossip,
"lightningd_opening", fc->peer, "lightningd_opening", fc->peer,
opening_wire_type_name, opening_wire_type_name,
NULL, peer_owner_finished, NULL, peer_owner_finished,
fc->peer->fd, fc->peer->gossip_client_fd, -1); take(&fc->peer->fd),
take(&fc->peer->gossip_client_fd), NULL);
if (!opening) { if (!opening) {
peer_fail(fc->peer, "Failed to subdaemon opening: %s", peer_fail(fc->peer, "Failed to subdaemon opening: %s",
strerror(errno)); strerror(errno));
@ -1869,10 +1869,6 @@ static bool gossip_peer_released(struct subd *gossip,
} }
fc->peer->owner = opening; fc->peer->owner = opening;
/* They took our fds. */
fc->peer->fd = -1;
fc->peer->gossip_client_fd = -1;
/* We will fund channel */ /* We will fund channel */
fc->peer->funder = LOCAL; fc->peer->funder = LOCAL;
channel_config(ld, &fc->peer->our_config, channel_config(ld, &fc->peer->our_config,

22
lightningd/subd.c

@ -124,7 +124,7 @@ static int subd(const char *dir, const char *name, bool debug,
{ {
int childmsg[2], execfail[2]; int childmsg[2], execfail[2];
pid_t childpid; pid_t childpid;
int err, fd; int err, *fd;
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0) if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0)
goto fail; goto fail;
@ -141,7 +141,7 @@ static int subd(const char *dir, const char *name, bool debug,
goto close_execfail_fail; goto close_execfail_fail;
if (childpid == 0) { if (childpid == 0) {
int fdnum = 3; int fdnum = 3, i;
long max; long max;
const char *debug_arg = NULL; const char *debug_arg = NULL;
@ -155,18 +155,18 @@ 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)) != -1) { 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);
for (fd = fdnum; fd < max; fd++) for (i = fdnum; i < max; i++)
close(fd); close(i);
if (debug) if (debug)
debug_arg = "--debugger"; debug_arg = "--debugger";
@ -184,8 +184,12 @@ 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)) != -1) while ((fd = va_arg(ap, int *)) != NULL) {
close(fd); if (taken(fd)) {
close(*fd);
*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)) {

2
lightningd/subd.h

@ -57,7 +57,7 @@ struct subd {
* @msgname: function to get name from messages * @msgname: function to get name from messages
* @msgcb: function to call when non-fatal message received (or NULL) * @msgcb: function to call when non-fatal message received (or NULL)
* @finished: function to call when it's finished (with exit status). * @finished: function to call when it's finished (with exit status).
* @...: the fds to hand as fd 3, 4... terminated with -1. * @...: NULL-terminated list of pointers to fds to hand as fd 3, 4... (can be take, if so, set to -1)
* *
* @msgcb gets called with @fds set to NULL: if it returns a positive number, * @msgcb gets called with @fds set to NULL: if it returns a positive number,
* that many @fds are received before calling again. If it returns -1, the * that many @fds are received before calling again. If it returns -1, the

Loading…
Cancel
Save