From 131d33646a15e9f05fcbe327082aaa0fa5e17984 Mon Sep 17 00:00:00 2001 From: Claudio Jeker Date: Thu, 21 Nov 2024 13:01:08 +0000 Subject: Make struct msgbuf opaque. Introduce msgbuf_new() and msgbuf_free() for that. This does not yet fix the imsgbuf_init() function which can now error. OK tb@ --- lib/libutil/Symbols.map | 3 ++- lib/libutil/imsg-buffer.c | 25 ++++++++++++++++++++++--- lib/libutil/imsg.c | 21 ++++++++++++--------- lib/libutil/imsg.h | 14 ++++++-------- 4 files changed, 42 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/libutil/Symbols.map b/lib/libutil/Symbols.map index 04ff2b37168..55d01cbba0e 100644 --- a/lib/libutil/Symbols.map +++ b/lib/libutil/Symbols.map @@ -93,7 +93,8 @@ logout; logwtmp; msgbuf_clear; - msgbuf_init; + msgbuf_free; + msgbuf_new; msgbuf_queuelen; msgbuf_write; ober_add_bitstring; diff --git a/lib/libutil/imsg-buffer.c b/lib/libutil/imsg-buffer.c index b6db734d587..d0d5bbf5b6b 100644 --- a/lib/libutil/imsg-buffer.c +++ b/lib/libutil/imsg-buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg-buffer.c,v 1.26 2024/11/21 13:00:14 claudio Exp $ */ +/* $OpenBSD: imsg-buffer.c,v 1.27 2024/11/21 13:01:07 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker @@ -32,6 +32,11 @@ #include "imsg.h" +struct msgbuf { + TAILQ_HEAD(, ibuf) bufs; + uint32_t queued; +}; + static void msgbuf_enqueue(struct msgbuf *, struct ibuf *); static void msgbuf_dequeue(struct msgbuf *, struct ibuf *); static void msgbuf_drain(struct msgbuf *, size_t); @@ -546,11 +551,25 @@ ibuf_fd_set(struct ibuf *buf, int fd) buf->fd = fd; } -void -msgbuf_init(struct msgbuf *msgbuf) +struct msgbuf * +msgbuf_new(void) { + struct msgbuf *msgbuf; + + if ((msgbuf = calloc(1, sizeof(*msgbuf))) == NULL) + return (NULL); msgbuf->queued = 0; TAILQ_INIT(&msgbuf->bufs); + + return msgbuf; +} + +void +msgbuf_free(struct msgbuf *msgbuf) +{ + if (msgbuf != NULL) + msgbuf_clear(msgbuf); + free(msgbuf); } uint32_t diff --git a/lib/libutil/imsg.c b/lib/libutil/imsg.c index a1991eecc83..4e5e9041d83 100644 --- a/lib/libutil/imsg.c +++ b/lib/libutil/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.34 2024/11/21 13:00:14 claudio Exp $ */ +/* $OpenBSD: imsg.c,v 1.35 2024/11/21 13:01:07 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker @@ -39,7 +39,7 @@ static int imsg_dequeue_fd(struct imsgbuf *); void imsgbuf_init(struct imsgbuf *imsgbuf, int fd) { - msgbuf_init(&imsgbuf->w); + imsgbuf->w = msgbuf_new(); memset(&imsgbuf->r, 0, sizeof(imsgbuf->r)); imsgbuf->fd = fd; imsgbuf->pid = getpid(); @@ -137,15 +137,16 @@ fail: int imsgbuf_write(struct imsgbuf *imsgbuf) { - return msgbuf_write(imsgbuf->fd, &imsgbuf->w); + return msgbuf_write(imsgbuf->fd, imsgbuf->w); } int imsgbuf_flush(struct imsgbuf *imsgbuf) { - while (imsgbuf->w.queued) + while (imsgbuf_queuelen(imsgbuf) > 0) { if (imsgbuf_write(imsgbuf) == -1) return (-1); + } return (0); } @@ -154,7 +155,9 @@ imsgbuf_clear(struct imsgbuf *imsgbuf) { int fd; - msgbuf_clear(&imsgbuf->w); + msgbuf_clear(imsgbuf->w); + msgbuf_free(imsgbuf->w); + imsgbuf->w = NULL; while ((fd = imsg_dequeue_fd(imsgbuf)) != -1) close(fd); } @@ -162,7 +165,7 @@ imsgbuf_clear(struct imsgbuf *imsgbuf) uint32_t imsgbuf_queuelen(struct imsgbuf *imsgbuf) { - return msgbuf_queuelen(&imsgbuf->w); + return msgbuf_queuelen(imsgbuf->w); } ssize_t @@ -342,8 +345,8 @@ imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1) goto fail; - ibuf_close(&imsgbuf->w, hdrbuf); - ibuf_close(&imsgbuf->w, buf); + ibuf_close(imsgbuf->w, hdrbuf); + ibuf_close(imsgbuf->w, buf); return (1); fail: @@ -433,7 +436,7 @@ imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg) hdr->flags |= IMSGF_HASFD; hdr->len = ibuf_size(msg); - ibuf_close(&imsgbuf->w, msg); + ibuf_close(imsgbuf->w, msg); } void diff --git a/lib/libutil/imsg.h b/lib/libutil/imsg.h index e38689b7add..995c3666206 100644 --- a/lib/libutil/imsg.h +++ b/lib/libutil/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.16 2024/11/21 13:00:14 claudio Exp $ */ +/* $OpenBSD: imsg.h,v 1.17 2024/11/21 13:01:07 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker @@ -38,22 +38,19 @@ struct ibuf { int fd; }; -struct msgbuf { - TAILQ_HEAD(, ibuf) bufs; - uint32_t queued; -}; - struct ibuf_read { unsigned char buf[IBUF_READ_SIZE]; unsigned char *rptr; size_t wpos; }; +struct msgbuf; struct imsg_fd; + struct imsgbuf { TAILQ_HEAD(, imsg_fd) fds; struct ibuf_read r; - struct msgbuf w; + struct msgbuf *w; int fd; pid_t pid; }; @@ -122,7 +119,8 @@ void ibuf_free(struct ibuf *); int ibuf_fd_avail(struct ibuf *); int ibuf_fd_get(struct ibuf *); void ibuf_fd_set(struct ibuf *, int); -void msgbuf_init(struct msgbuf *); +struct msgbuf *msgbuf_new(void); +void msgbuf_free(struct msgbuf *); void msgbuf_clear(struct msgbuf *); uint32_t msgbuf_queuelen(struct msgbuf *); int ibuf_write(int, struct msgbuf *); -- cgit v1.2.3