From 1349f312f5849411e6d8d5306fa7a13a4c9fc1b2 Mon Sep 17 00:00:00 2001 From: eric <> Date: Sun, 7 Jun 2009 05:56:25 +0000 Subject: [PATCH] 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@ --- src/usr.sbin/ntpd/imsg.c | 18 +++++++++++++++--- src/usr.sbin/ntpd/imsg.h | 6 ++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/usr.sbin/ntpd/imsg.c b/src/usr.sbin/ntpd/imsg.c index a70cb77d..ff2f21ba 100644 --- a/src/usr.sbin/ntpd/imsg.c +++ b/src/usr.sbin/ntpd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.15 2009/06/07 00:40:46 eric Exp $ */ +/* $OpenBSD: imsg.c,v 1.16 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -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/src/usr.sbin/ntpd/imsg.h b/src/usr.sbin/ntpd/imsg.h index f70ddb04..427e3e14 100644 --- a/src/usr.sbin/ntpd/imsg.h +++ b/src/usr.sbin/ntpd/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:25 eric Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard @@ -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 *);