diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-11-21 12:54:53 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-11-21 12:54:53 +0000 |
commit | 47ba2539ec89c842e3972f962f7bf14c238ea60c (patch) | |
tree | 323e51cfa9d137385e61aa9c5272c957435912da | |
parent | 2c347f26a71390d97a0b58ae937f8601a38166e4 (diff) |
Adjust imsgbuf_read to return an int with either 1, 0 or -1.
Handle EAGAIN by a simple return 1 (same for the fd check).
This way the caller will poll again and then retry later.
OK tb@
-rw-r--r-- | lib/libutil/imsg.c | 23 | ||||
-rw-r--r-- | lib/libutil/imsg.h | 4 | ||||
-rw-r--r-- | lib/libutil/imsg_init.3 | 26 |
3 files changed, 34 insertions, 19 deletions
diff --git a/lib/libutil/imsg.c b/lib/libutil/imsg.c index fbe001e4341..f9497a47ec0 100644 --- a/lib/libutil/imsg.c +++ b/lib/libutil/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.30 2024/11/21 12:54:10 claudio Exp $ */ +/* $OpenBSD: imsg.c,v 1.31 2024/11/21 12:54:52 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> @@ -49,7 +49,7 @@ imsgbuf_init(struct imsgbuf *imsgbuf, int fd) TAILQ_INIT(&imsgbuf->fds); } -ssize_t +int imsgbuf_read(struct imsgbuf *imsgbuf) { struct msghdr msg; @@ -59,7 +59,7 @@ imsgbuf_read(struct imsgbuf *imsgbuf) char buf[CMSG_SPACE(sizeof(int) * 1)]; } cmsgbuf; struct iovec iov; - ssize_t n = -1; + ssize_t n; int fd; struct imsg_fd *ifd; @@ -80,17 +80,25 @@ again: if (getdtablecount() + imsg_fd_overhead + (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int)) >= getdtablesize()) { - errno = EAGAIN; free(ifd); - return (-1); + return (1); } if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) { if (errno == EINTR) goto again; + if (errno == EAGAIN) { + free(ifd); + return (1); + } goto fail; } + if (n == 0) { /* connection closed */ + free(ifd); + return (0); + } + imsgbuf->r.wpos += n; for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; @@ -121,9 +129,12 @@ again: /* we do not handle other ctl data level */ } + free(ifd); + return (1); + fail: free(ifd); - return (n); + return (-1); } int diff --git a/lib/libutil/imsg.h b/lib/libutil/imsg.h index 6e9ec6eaf47..56701b18c0c 100644 --- a/lib/libutil/imsg.h +++ b/lib/libutil/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.13 2024/11/21 12:54:10 claudio Exp $ */ +/* $OpenBSD: imsg.h,v 1.14 2024/11/21 12:54:52 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> @@ -132,7 +132,7 @@ int msgbuf_write(struct msgbuf *); /* imsg.c */ void imsgbuf_init(struct imsgbuf *, int); -ssize_t imsgbuf_read(struct imsgbuf *); +int imsgbuf_read(struct imsgbuf *); int imsgbuf_write(struct imsgbuf *); int imsgbuf_flush(struct imsgbuf *); void imsgbuf_clear(struct imsgbuf *); diff --git a/lib/libutil/imsg_init.3 b/lib/libutil/imsg_init.3 index 59a35e9941b..a2fa656b53a 100644 --- a/lib/libutil/imsg_init.3 +++ b/lib/libutil/imsg_init.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: imsg_init.3,v 1.38 2024/11/21 12:54:10 claudio Exp $ +.\" $OpenBSD: imsg_init.3,v 1.39 2024/11/21 12:54:52 claudio Exp $ .\" .\" Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> .\" Copyright (c) 2010 Nicholas Marriott <nicm@openbsd.org> @@ -87,7 +87,7 @@ .Fn imsgbuf_init "struct imsgbuf *imsgbuf" "int fd" .Ft uint32_t .Fn imsgbuf_queuelen "struct imsgbuf *imsgbuf" -.Ft ssize_t +.Ft int .Fn imsgbuf_read "struct imsgbuf *imsgbuf" .Ft int .Fn imsgbuf_write "struct imsgbuf *imsgbuf" @@ -127,13 +127,18 @@ routine reads pending data with .Xr recvmsg 2 and queues it as individual messages on .Fa imsgbuf . -It returns the number of bytes read on success, or \-1 on error. -A return value of \-1 from -.Fn imsgbuf_read -invalidates -.Fa imsgbuf , -and renders it suitable only for passing to -.Fn imsgbuf_clear . +It returns 1 on success, 0 if the connection is closed, or \-1 on error +and the global variable +.Va errno +is set to indicate the error. +The errors +.Er EINTR +and +.Er EAGAIN +are treated as follows. +.Er EINTR +will automatically retry the read operation while the other errors are +ignored with a 1 return. .Pp .Fn imsgbuf_write writes out queued messages. @@ -149,7 +154,6 @@ are treated as follows. .Er EINTR will automatically retry the write operation while the other errors are ignored with a 0 return. -The caller will then retry the operation at a later stage. .Pp .Fn imsgbuf_flush calls @@ -401,7 +405,7 @@ dispatch_imsg(struct imsgbuf *imsgbuf) ssize_t n; int idata; - if ((n = imsgbuf_read(imsgbuf)) == -1 && errno != EAGAIN) { + if ((n = imsgbuf_read(imsgbuf)) == -1) { /* handle read error */ } if (n == 0) { |