summaryrefslogtreecommitdiff
path: root/usr.sbin/ypldap/imsg.c
diff options
context:
space:
mode:
authorEric Faurot <eric@cvs.openbsd.org>2009-06-07 05:56:26 +0000
committerEric Faurot <eric@cvs.openbsd.org>2009-06-07 05:56:26 +0000
commit703b56a3f22756eac55e0bd7b17d1538de6c8000 (patch)
tree40f35f3c5bceaefa5fe710539018fdc22722309a /usr.sbin/ypldap/imsg.c
parenta09824e75d2192dc4f1a42afe21653e1f153c241 (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/ypldap/imsg.c')
-rw-r--r--usr.sbin/ypldap/imsg.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/usr.sbin/ypldap/imsg.c b/usr.sbin/ypldap/imsg.c
index 6b158a676cf..921ea801c8f 100644
--- a/usr.sbin/ypldap/imsg.c
+++ b/usr.sbin/ypldap/imsg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg.c,v 1.6 2009/06/07 00:40:46 eric Exp $ */
+/* $OpenBSD: imsg.c,v 1.7 2009/06/07 05:56:25 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);