diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2009-06-07 05:56:26 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2009-06-07 05:56:26 +0000 |
commit | 703b56a3f22756eac55e0bd7b17d1538de6c8000 (patch) | |
tree | 40f35f3c5bceaefa5fe710539018fdc22722309a /usr.sbin/bgpd | |
parent | a09824e75d2192dc4f1a42afe21653e1f153c241 (diff) |
Change the way fds passed over a socket are retreived on the receiving side.
Currently the receiver fetches an imsg via imsg_get() and if he expects
an fd, he then calls imsg_get_fd() to fetch the next fd queued on the
imsgbuf from which the imsg came.
This changes hides the fd queueing mechanism to the API user. When closing
an imsg with an fd, the message is flagged so that the receiving end knows
it must dequeue the fd in imsg_get() and return it with the imsg structure.
This way there is no (less) possible screw up from imsg_get_fd() not being
called directly after imsg_get() by the user. The retreived imsg is
self-contained.
ok pyr@, "I like that" henning@
Diffstat (limited to 'usr.sbin/bgpd')
-rw-r--r-- | usr.sbin/bgpd/imsg.c | 18 | ||||
-rw-r--r-- | usr.sbin/bgpd/imsg.h | 6 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde.c | 4 | ||||
-rw-r--r-- | usr.sbin/bgpd/session.c | 6 |
4 files changed, 24 insertions, 10 deletions
diff --git a/usr.sbin/bgpd/imsg.c b/usr.sbin/bgpd/imsg.c index 4239b154560..373f75965e7 100644 --- a/usr.sbin/bgpd/imsg.c +++ b/usr.sbin/bgpd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.45 2009/06/07 00:40:46 eric Exp $ */ +/* $OpenBSD: imsg.c,v 1.46 2009/06/07 05:56:24 eric Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -28,6 +28,8 @@ #include "imsg.h" +int imsg_get_fd(struct imsgbuf *); + void imsg_init(struct imsgbuf *ibuf, int fd) { @@ -113,9 +115,14 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) imsg->hdr.pid = ntohl(imsg->hdr.pid); datalen = imsg->hdr.len - IMSG_HEADER_SIZE; ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; - if ((imsg->data = malloc(datalen)) == NULL) { + if ((imsg->data = malloc(datalen)) == NULL) return (-1); - } + + if (imsg->hdr.flags & IMSGF_HASFD) + imsg->fd = imsg_get_fd(ibuf); + else + imsg->fd = -1; + memcpy(imsg->data, ibuf->r.rptr, datalen); if (imsg->hdr.len < av) { @@ -216,6 +223,11 @@ imsg_close(struct imsgbuf *ibuf, struct buf *msg) struct imsg_hdr *hdr; hdr = (struct imsg_hdr *)msg->buf; + + hdr->flags &= ~IMSGF_HASFD; + if (msg->fd != -1) + hdr->flags |= IMSGF_HASFD; + hdr->type = htonl(hdr->type); hdr->len = htons(msg->wpos); hdr->flags = htons(hdr->flags); diff --git a/usr.sbin/bgpd/imsg.h b/usr.sbin/bgpd/imsg.h index f70ddb04a3b..8fa2e4f2da0 100644 --- a/usr.sbin/bgpd/imsg.h +++ b/usr.sbin/bgpd/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.2 2009/06/06 22:11:25 eric Exp $ */ +/* $OpenBSD: imsg.h,v 1.3 2009/06/07 05:56:24 eric Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -59,6 +59,8 @@ struct imsgbuf { pid_t pid; }; +#define IMSGF_HASFD 1 + struct imsg_hdr { u_int32_t type; u_int16_t len; @@ -69,6 +71,7 @@ struct imsg_hdr { struct imsg { struct imsg_hdr hdr; + int fd; void *data; }; @@ -101,6 +104,5 @@ struct buf *imsg_create(struct imsgbuf *, u_int32_t, u_int32_t, pid_t, int imsg_add(struct buf *, void *, u_int16_t); void imsg_close(struct imsgbuf *, struct buf *); void imsg_free(struct imsg *); -int imsg_get_fd(struct imsgbuf *); int imsg_flush(struct imsgbuf *); void imsg_clear(struct imsgbuf *); diff --git a/usr.sbin/bgpd/rde.c b/usr.sbin/bgpd/rde.c index 2140cfffd65..f9282c3e07a 100644 --- a/usr.sbin/bgpd/rde.c +++ b/usr.sbin/bgpd/rde.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.c,v 1.261 2009/06/07 00:30:23 claudio Exp $ */ +/* $OpenBSD: rde.c,v 1.262 2009/06/07 05:56:24 eric Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -721,7 +721,7 @@ rde_dispatch_imsg_parent(struct imsgbuf *ibuf) break; } memcpy(&xmrt, imsg.data, sizeof(xmrt)); - if ((fd = imsg_get_fd(ibuf)) == -1) + if ((fd = imsg.fd) == -1) log_warnx("expected to receive fd for mrt dump " "but didn't receive any"); else if (xmrt.type == MRT_TABLE_DUMP || diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index c8bd5e36c2d..4f7f787272c 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.292 2009/06/06 06:33:15 eric Exp $ */ +/* $OpenBSD: session.c,v 1.293 2009/06/07 05:56:24 eric Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org> @@ -2310,7 +2310,7 @@ session_dispatch_imsg(struct imsgbuf *ibuf, int idx, u_int *listener_cnt) fatalx("king bula sez: " "expected REINIT"); - if ((nla->fd = imsg_get_fd(ibuf)) == -1) + if ((nla->fd = imsg.fd) == -1) log_warnx("expected to receive fd for " "%s but didn't receive any", log_sockaddr((struct sockaddr *) @@ -2421,7 +2421,7 @@ session_dispatch_imsg(struct imsgbuf *ibuf, int idx, u_int *listener_cnt) } memcpy(&xmrt, imsg.data, sizeof(struct mrt)); - if ((xmrt.wbuf.fd = imsg_get_fd(ibuf)) == -1) + if ((xmrt.wbuf.fd = imsg.fd) == -1) log_warnx("expected to receive fd for mrt dump " "but didn't receive any"); |