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/smtpd | |
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/smtpd')
-rw-r--r-- | usr.sbin/smtpd/control.c | 4 | ||||
-rw-r--r-- | usr.sbin/smtpd/enqueue.c | 4 | ||||
-rw-r--r-- | usr.sbin/smtpd/imsg.c | 18 | ||||
-rw-r--r-- | usr.sbin/smtpd/imsg.h | 6 | ||||
-rw-r--r-- | usr.sbin/smtpd/lka.c | 4 | ||||
-rw-r--r-- | usr.sbin/smtpd/mda.c | 6 | ||||
-rw-r--r-- | usr.sbin/smtpd/mta.c | 6 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtp.c | 6 |
8 files changed, 34 insertions, 20 deletions
diff --git a/usr.sbin/smtpd/control.c b/usr.sbin/smtpd/control.c index 76199121e64..9c34d0b546c 100644 --- a/usr.sbin/smtpd/control.c +++ b/usr.sbin/smtpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.34 2009/06/06 04:14:21 pyr Exp $ */ +/* $OpenBSD: control.c,v 1.35 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -794,7 +794,7 @@ control_dispatch_smtp(int sig, short event, void *p) } imsg_compose_event(&c->iev, IMSG_CTL_OK, 0, 0, - imsg_get_fd(ibuf), NULL, 0); + imsg.fd, NULL, 0); break; } default: diff --git a/usr.sbin/smtpd/enqueue.c b/usr.sbin/smtpd/enqueue.c index 3102c1d3bb3..31f1b181804 100644 --- a/usr.sbin/smtpd/enqueue.c +++ b/usr.sbin/smtpd/enqueue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: enqueue.c,v 1.17 2009/06/05 20:43:57 pyr Exp $ */ +/* $OpenBSD: enqueue.c,v 1.18 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2005 Henning Brauer <henning@bulabula.org> @@ -540,7 +540,7 @@ open_connection(void) errx(1, "unexpected imsg reply type"); } - fd = imsg_get_fd(ibuf); + fd = imsg.fd; imsg_free(&imsg); break; diff --git a/usr.sbin/smtpd/imsg.c b/usr.sbin/smtpd/imsg.c index f2e35d7cf61..ecc47ee3b41 100644 --- a/usr.sbin/smtpd/imsg.c +++ b/usr.sbin/smtpd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.8 2009/06/07 00:40:46 eric Exp $ */ +/* $OpenBSD: imsg.c,v 1.9 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); diff --git a/usr.sbin/smtpd/imsg.h b/usr.sbin/smtpd/imsg.h index 52dcace61d3..2b56432d527 100644 --- a/usr.sbin/smtpd/imsg.h +++ b/usr.sbin/smtpd/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.4 2009/06/06 22:11:25 eric Exp $ */ +/* $OpenBSD: imsg.h,v 1.5 2009/06/07 05:56:25 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/smtpd/lka.c b/usr.sbin/smtpd/lka.c index 5542b9937d0..66978b8165d 100644 --- a/usr.sbin/smtpd/lka.c +++ b/usr.sbin/smtpd/lka.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lka.c,v 1.59 2009/06/06 04:14:21 pyr Exp $ */ +/* $OpenBSD: lka.c,v 1.60 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -228,7 +228,7 @@ lka_dispatch_parent(int sig, short event, void *p) lkasession = SPLAY_FIND(lkatree, &env->lka_sessions, &key); if (lkasession == NULL) fatal("lka_dispatch_parent: lka session is gone"); - fd = imsg_get_fd(ibuf); + fd = imsg.fd; --lkasession->pending; if (fd == -1) { diff --git a/usr.sbin/smtpd/mda.c b/usr.sbin/smtpd/mda.c index a9a21135051..d09a44ed93a 100644 --- a/usr.sbin/smtpd/mda.c +++ b/usr.sbin/smtpd/mda.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mda.c,v 1.22 2009/06/06 04:14:21 pyr Exp $ */ +/* $OpenBSD: mda.c,v 1.23 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -111,7 +111,7 @@ mda_dispatch_parent(int sig, short event, void *p) fatalx("mda_dispatch_parent: internal inconsistency."); messagep->status = status; - s->mboxfd = imsg_get_fd(ibuf); + s->mboxfd = imsg.fd; if (s->mboxfd == -1) { mda_remove_message(env, batchp, messagep); break; @@ -146,7 +146,7 @@ mda_dispatch_parent(int sig, short event, void *p) fatalx("mda_dispatch_parent: internal inconsistency."); messagep->status = status; - s->messagefd = imsg_get_fd(ibuf); + s->messagefd = imsg.fd; if (s->messagefd == -1) { if (s->mboxfd != -1) close(s->mboxfd); diff --git a/usr.sbin/smtpd/mta.c b/usr.sbin/smtpd/mta.c index ac5e88fc6e6..6ce27978bed 100644 --- a/usr.sbin/smtpd/mta.c +++ b/usr.sbin/smtpd/mta.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mta.c,v 1.59 2009/06/06 04:14:21 pyr Exp $ */ +/* $OpenBSD: mta.c,v 1.60 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -307,9 +307,9 @@ mta_dispatch_queue(int sig, short event, void *p) IMSG_SIZE_CHECK(batchp); - if ((fd = imsg_get_fd(ibuf)) == -1) { + if ((fd = imsg.fd) == -1) { /* NEEDS_FIX - unsure yet how it must be handled */ - fatalx("mta_dispatch_queue: imsg_get_fd"); + fatalx("mta_dispatch_queue: imsg.fd == -1"); } batchp = batch_by_id(env, batchp->id); diff --git a/usr.sbin/smtpd/smtp.c b/usr.sbin/smtpd/smtp.c index baca27584ee..c8395a6aed4 100644 --- a/usr.sbin/smtpd/smtp.c +++ b/usr.sbin/smtpd/smtp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smtp.c,v 1.57 2009/06/06 04:14:21 pyr Exp $ */ +/* $OpenBSD: smtp.c,v 1.58 2009/06/07 05:56:25 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -164,7 +164,7 @@ smtp_dispatch_parent(int sig, short event, void *p) fatal(NULL); memcpy(l, imsg.data, sizeof(*l)); - if ((l->fd = imsg_get_fd(ibuf)) == -1) + if ((l->fd = imsg.fd) == -1) fatal("cannot get fd"); (void)strlcpy(key.ssl_name, l->ssl_cert_name, @@ -405,7 +405,7 @@ smtp_dispatch_queue(int sig, short event, void *p) IMSG_SIZE_CHECK(ss); - fd = imsg_get_fd(ibuf); + fd = imsg.fd; if ((s = session_lookup(env, ss->id)) == NULL) { close(fd); |