diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-11-21 12:49:59 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-11-21 12:49:59 +0000 |
commit | 9fba74e48ba9ca6c311c192d8323f3bc0e202863 (patch) | |
tree | 807a5904a416e2391e625824a9c26c39672c952f | |
parent | 2bf4f07177486bda8a275b0c6676a016788d8e54 (diff) |
Simplify imsg_write, msgbuf_write and ibuf_write return codes.
Return 0 on success or when a temporary error happened (EAGAIN, ENOBUFS).
Return -1 on error and set errno otherwise.
Kill the old 0 return for EOF. This is not how write operations work.
OK tb@
-rw-r--r-- | lib/libutil/ibuf_add.3 | 38 | ||||
-rw-r--r-- | lib/libutil/imsg-buffer.c | 32 | ||||
-rw-r--r-- | lib/libutil/imsg.c | 4 | ||||
-rw-r--r-- | lib/libutil/imsg_init.3 | 38 |
4 files changed, 69 insertions, 43 deletions
diff --git a/lib/libutil/ibuf_add.3 b/lib/libutil/ibuf_add.3 index e8486aa7324..5445c9a972a 100644 --- a/lib/libutil/ibuf_add.3 +++ b/lib/libutil/ibuf_add.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ibuf_add.3,v 1.2 2024/11/21 12:42:14 claudio Exp $ +.\" $OpenBSD: ibuf_add.3,v 1.3 2024/11/21 12:49:58 claudio Exp $ .\" .\" Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> .\" Copyright (c) 2010 Nicholas Marriott <nicm@openbsd.org> @@ -469,11 +469,19 @@ routine transmits as many pending buffers as possible from .Fa msgbuf using .Xr writev 2 . -It returns 1 if it succeeds, \-1 on error and 0 when no buffers were -pending or an EOF condition on the socket is detected. -Temporary resource shortages are returned with errno -.Er EAGAIN -and require the application to retry again in the future. +It returns 0 if it succeeds, -1 on error and the global variable +.Va errno +is set to indicate the error. +The errors +.Er EINTR , +.Er EAGAIN , +and +.Er ENOBUFS +are treated as follows. +.Er EINTR +will automatically retry the write operation while the other errors are +ignored with a 0 return. +The application will then retry the operation at a later stage. .Pp The .Fn msgbuf_init @@ -500,11 +508,19 @@ routine calls .Xr sendmsg 2 to transmit buffers queued in .Fa msgbuf . -It returns 1 if it succeeds, \-1 on error, and 0 when the queue was empty -or an EOF condition on the socket is detected. -Temporary resource shortages are returned with errno -.Er EAGAIN -and require the application to retry again in the future. +It returns 0 if it succeeds, -1 on error and the global variable +.Va errno +is set to indicate the error. +The errors +.Er EINTR , +.Er EAGAIN , +and +.Er ENOBUFS +are treated as follows. +.Er EINTR +will automatically retry the write operation while the other errors are +ignored with a 0 return. +The application will then retry the operation at a later stage. .Sh SEE ALSO .Xr socketpair 2 , .Xr imsg_init 3 , diff --git a/lib/libutil/imsg-buffer.c b/lib/libutil/imsg-buffer.c index 0a824c63bd8..537e52d6f99 100644 --- a/lib/libutil/imsg-buffer.c +++ b/lib/libutil/imsg-buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg-buffer.c,v 1.22 2024/11/21 12:44:06 claudio Exp $ */ +/* $OpenBSD: imsg-buffer.c,v 1.23 2024/11/21 12:49:58 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> @@ -569,24 +569,21 @@ ibuf_write(struct msgbuf *msgbuf) iov[i].iov_len = ibuf_size(buf); i++; } + if (i == 0) + return (0); /* nothing queued */ again: if ((n = writev(msgbuf->fd, iov, i)) == -1) { if (errno == EINTR) goto again; - if (errno == ENOBUFS) - errno = EAGAIN; + if (errno == EAGAIN || errno == ENOBUFS) + /* lets retry later again */ + return (0); return (-1); } - if (n == 0) { /* connection closed */ - errno = 0; - return (0); - } - msgbuf_drain(msgbuf, n); - - return (1); + return (0); } void @@ -653,6 +650,9 @@ msgbuf_write(struct msgbuf *msgbuf) buf0 = buf; } + if (i == 0) + return (0); /* nothing queued */ + msg.msg_iov = iov; msg.msg_iovlen = i; @@ -670,16 +670,12 @@ again: if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) { if (errno == EINTR) goto again; - if (errno == ENOBUFS) - errno = EAGAIN; + if (errno == EAGAIN || errno == ENOBUFS) + /* lets retry later again */ + return (0); return (-1); } - if (n == 0) { /* connection closed */ - errno = 0; - return (0); - } - /* * assumption: fd got sent if sendmsg sent anything * this works because fds are passed one at a time @@ -691,7 +687,7 @@ again: msgbuf_drain(msgbuf, n); - return (1); + return (0); } uint32_t diff --git a/lib/libutil/imsg.c b/lib/libutil/imsg.c index db43273f1c0..8ee07a2dfaf 100644 --- a/lib/libutil/imsg.c +++ b/lib/libutil/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.27 2024/11/21 12:49:14 claudio Exp $ */ +/* $OpenBSD: imsg.c,v 1.28 2024/11/21 12:49:58 claudio Exp $ */ /* * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> @@ -429,7 +429,7 @@ int imsg_flush(struct imsgbuf *imsgbuf) { while (imsgbuf->w.queued) - if (imsg_write(imsgbuf) <= 0 && errno != EAGAIN) + if (imsg_write(imsgbuf) == -1) return (-1); return (0); } diff --git a/lib/libutil/imsg_init.3 b/lib/libutil/imsg_init.3 index 652e7be189e..96cfd71bbc5 100644 --- a/lib/libutil/imsg_init.3 +++ b/lib/libutil/imsg_init.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: imsg_init.3,v 1.35 2024/11/21 12:49:14 claudio Exp $ +.\" $OpenBSD: imsg_init.3,v 1.36 2024/11/21 12:49:58 claudio Exp $ .\" .\" Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org> .\" Copyright (c) 2010 Nicholas Marriott <nicm@openbsd.org> @@ -21,6 +21,9 @@ .Sh NAME .Nm imsg_init , .Nm imsg_read , +.Nm imsg_write , +.Nm imsg_flush , +.Nm imsg_clear .Nm imsg_get , .Nm imsg_get_ibuf , .Nm imsg_get_data , @@ -36,9 +39,7 @@ .Nm imsg_add , .Nm imsg_close , .Nm imsg_forward , -.Nm imsg_free , -.Nm imsg_flush , -.Nm imsg_clear +.Nm imsg_free .Nd IPC messaging functions .Sh SYNOPSIS .In sys/queue.h @@ -235,7 +236,9 @@ more than once per message. calls .Fn imsg_write in a loop until all imsgs in the output buffer are sent. -It returns 0 if it succeeds, \-1 otherwise. +It returns 0 if it succeeds, \-1 otherwise and the global variable +.Va errno +is set to indicate the error. .Pp The .Fn imsg_read @@ -253,8 +256,19 @@ and renders it suitable only for passing to .Pp .Fn imsg_write writes out queued messages. -It returns 1 if it succeeds, -1 on error, and 0 when the queue -was empty or an EOF condition on the socket is detected. +It returns 0 if it succeeds, -1 on error and the global variable +.Va errno +is set to indicate the error. +The errors +.Er EINTR , +.Er EAGAIN , +and +.Er ENOBUFS +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. .Fn imsg_get fills in an individual imsg pending on .Fa imsgbuf @@ -377,11 +391,11 @@ library is used to monitor the socket file descriptor. When the socket is ready for writing, queued messages are transmitted with .Fn imsg_write : .Bd -literal -offset indent - if ((n = imsg_write(&imsgbuf-\*(Gtw)) == -1 && errno != EAGAIN) { - /* handle write failure */ - } - if (n == 0) { - /* handle closed connection */ + if (imsg_write(imsgbuf) == -1) { + if (errno == EPIPE) + /* handle closed connection */ + else + /* handle write failure */ } .Ed .Pp |