From f73922b858e0c5e094ceb9777b977dc2caafffb5 Mon Sep 17 00:00:00 2001 From: claudio Date: Mon, 21 Apr 2014 17:41:52 +0000 Subject: [PATCH] Redo most of the control message handling. Switch it back to use SOCK_SEQPACKET and provide functions to build the control messages that can also be used by iscsictl. --- usr.sbin/iscsid/control.c | 47 ++++++------------------------------ usr.sbin/iscsid/iscsid.h | 12 +++++++-- usr.sbin/iscsid/util.c | 51 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/usr.sbin/iscsid/control.c b/usr.sbin/iscsid/control.c index c352b1261e7..47743fc326f 100644 --- a/usr.sbin/iscsid/control.c +++ b/usr.sbin/iscsid/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.7 2014/04/20 18:17:12 claudio Exp $ */ +/* $OpenBSD: control.c,v 1.8 2014/04/21 17:41:52 claudio Exp $ */ /* * Copyright (c) 2010 Claudio Jeker @@ -67,7 +67,7 @@ control_init(char *path) return -1; } - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + if ((fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) == -1) { log_warn("control_init: socket"); return -1; } @@ -242,8 +242,6 @@ control_dispatch(int fd, short event, void *bula) } if (event & EV_WRITE) { if ((pdu = TAILQ_FIRST(&c->channel)) != NULL) { - TAILQ_REMOVE(&c->channel, pdu, entry); - for (niov = 0; niov < PDU_MAXIOV; niov++) { iov[niov].iov_base = pdu->iov[niov].iov_base; iov[niov].iov_len = pdu->iov[niov].iov_len; @@ -251,13 +249,16 @@ control_dispatch(int fd, short event, void *bula) bzero(&msg, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = niov; - if (sendmsg(fd, &msg, 0) == -1 && - !(errno == EAGAIN || errno == ENOBUFS)) { + if (sendmsg(fd, &msg, 0) == -1) { + if (errno == EAGAIN || errno == ENOBUFS) + goto requeue; control_close(c); return; } + TAILQ_REMOVE(&c->channel, pdu, entry); } } +requeue: if (!TAILQ_EMPTY(&c->channel)) flags |= EV_WRITE; @@ -314,7 +315,7 @@ fail: return p; } -int +void control_queue(void *ch, struct pdu *pdu) { struct control *c = ch; @@ -324,36 +325,4 @@ control_queue(void *ch, struct pdu *pdu) event_del(&c->ev); event_set(&c->ev, c->fd, EV_READ|EV_WRITE, control_dispatch, c); event_add(&c->ev, NULL); - - return 0; -} - -int -control_compose(void *ch, u_int16_t type, void *buf, size_t len) -{ - struct pdu *pdu; - struct ctrlmsghdr *cmh; - void *ptr; - - if (PDU_LEN(len) > CONTROL_READ_SIZE - PDU_LEN(sizeof(*cmh))) - return -1; - if ((pdu = pdu_new()) == NULL) - return -1; - if ((cmh = pdu_alloc(sizeof(*cmh))) == NULL) - goto fail; - bzero(cmh, sizeof(*cmh)); - cmh->type = type; - cmh->len[0] = len; - pdu_addbuf(pdu, cmh, sizeof(*cmh), 0); - if (len > 0) { - if ((ptr = pdu_alloc(len)) == NULL) - goto fail; - memcpy(ptr, buf, len); - pdu_addbuf(pdu, ptr, len, 1); - } - - return control_queue(ch, pdu); -fail: - pdu_free(pdu); - return -1; } diff --git a/usr.sbin/iscsid/iscsid.h b/usr.sbin/iscsid/iscsid.h index a530ada3f83..e47d622e1e8 100644 --- a/usr.sbin/iscsid/iscsid.h +++ b/usr.sbin/iscsid/iscsid.h @@ -1,4 +1,4 @@ -/* $OpenBSD: iscsid.h,v 1.12 2014/04/21 09:48:31 claudio Exp $ */ +/* $OpenBSD: iscsid.h,v 1.13 2014/04/21 17:41:52 claudio Exp $ */ /* * Copyright (c) 2009 Claudio Jeker @@ -46,6 +46,13 @@ struct ctrlmsghdr { u_int16_t len[3]; }; +struct ctrldata { + void *buf; + size_t len; +}; + +#define CTRLARGV(x...) ((struct ctrldata []){ x }) + /* Control message types */ #define CTRL_SUCCESS 1 #define CTRL_FAILURE 2 @@ -311,8 +318,9 @@ char *default_initiator_name(void); int control_init(char *); void control_cleanup(char *); void control_event_init(void); -int control_queue(void *, struct pdu *); +void control_queue(void *, struct pdu *); int control_compose(void *, u_int16_t, void *, size_t); +int control_build(void *, u_int16_t, int, struct ctrldata *); struct session *session_find(struct initiator *, char *); struct session *session_new(struct initiator *, u_int8_t); diff --git a/usr.sbin/iscsid/util.c b/usr.sbin/iscsid/util.c index 0592fc76f3e..ccebaab1eb8 100644 --- a/usr.sbin/iscsid/util.c +++ b/usr.sbin/iscsid/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.3 2014/04/19 18:31:33 claudio Exp $ */ +/* $OpenBSD: util.c,v 1.4 2014/04/21 17:41:52 claudio Exp $ */ /* * Copyright (c) 2009 Claudio Jeker @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include +#include #include #include #include @@ -141,3 +142,51 @@ log_sockaddr(void *arg) snprintf(buf, sizeof(buf), "[%s]:%s", host, port); return buf; } + +int +control_compose(void *ch, u_int16_t type, void *buf, size_t len) +{ + return control_build(ch, type, 1, CTRLARGV({ buf, len })); +} + +int +control_build(void *ch, u_int16_t type, int argc, struct ctrldata *argv) +{ + struct pdu *pdu; + struct ctrlmsghdr *cmh; + size_t size = 0; + int i; + + if (argc > (int)nitems(cmh->len)) + return -1; + + for (i = 0; i < argc; i++) + size += argv[i].len; + if (PDU_LEN(size) > CONTROL_READ_SIZE - PDU_LEN(sizeof(*cmh))) + return -1; + + if ((pdu = pdu_new()) == NULL) + return -1; + if ((cmh = pdu_alloc(sizeof(*cmh))) == NULL) + goto fail; + bzero(cmh, sizeof(*cmh)); + cmh->type = type; + pdu_addbuf(pdu, cmh, sizeof(*cmh), 0); + + for (i = 0; i < argc; i++) + if (argv[i].len > 0) { + void *ptr; + + cmh->len[i] = argv[i].len; + if ((ptr = pdu_alloc(argv[i].len)) == NULL) + goto fail; + memcpy(ptr, argv[i].buf, argv[i].len); + pdu_addbuf(pdu, ptr, argv[i].len, i + 1); + } + + control_queue(ch, pdu); + return 0; +fail: + pdu_free(pdu); + return -1; +} -- 2.20.1