"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: remove errno from bufio API
To:
gameoftrees@openbsd.org
Date:
Tue, 16 Apr 2024 12:58:06 +0200

Download raw body.

Thread
On Tue, Apr 16, 2024 at 12:51:10PM +0200, Stefan Sperling wrote:
> There is no need for bufio to be using errno as part of the API.
> Since errno really belongs to libc I don't feel comfortable with
> this, even though I understand it is supposed to emulate libc's
> read/write interfaces.
> 
> This diff switches from errno to the -ERRCODE linux-ism simply
> because this is the easiest way I found to get rid of errno at
> the API level.
> 
> We could also switch to got_error but that would make it harder
> to reuse bufio code elsewhere.
> 
> ok?

Had one little error where bufio_handshake() was returning
EINVAL instead of -EINVAL. Not a big deal but it is probably
better to stay consistent.

-----------------------------------------------
commit cf32c3de8e52f8a1b0c7b91abe3ccdae2024c868 (bufio-errno)
from: Stefan Sperling <stsp@stsp.name>
date: Tue Apr 16 10:56:16 2024 UTC
 
 avoid clobbering errno in bufio API
 
diff 7457011ad491370bd5ae4cbd56c4065715659fc0 cf32c3de8e52f8a1b0c7b91abe3ccdae2024c868
commit - 7457011ad491370bd5ae4cbd56c4065715659fc0
commit + cf32c3de8e52f8a1b0c7b91abe3ccdae2024c868
blob - 9c8a90e392c27e03eb1dd32a88414aa62a355012
blob + 18e8b03b5187cb01a6be50c468e281e3daa7edcf
--- gotd/libexec/got-notify-http/got-notify-http.c
+++ gotd/libexec/got-notify-http/got-notify-http.c
@@ -772,7 +772,7 @@ main(int argc, char **argv)
 	const char	*host = NULL, *port = NULL, *path = NULL;
 	char		*auth, *line, *spc;
 	size_t		 len;
-	ssize_t		 r;
+	ssize_t		 r, w;
 	off_t		 paylen;
 	int		 tls = 0;
 	int		 response_code = 0, done = 0;
@@ -913,12 +913,13 @@ main(int argc, char **argv)
 			errx(1, "timeout");
 
 		if (bio.wbuf.len > 0 && (pfd.revents & POLLOUT)) {
-			if (bufio_write(&bio) == -1 && errno != EAGAIN)
+			w = bufio_write(&bio);
+			if (w < 0 && w != -EAGAIN)
 				errx(1, "bufio_write: %s", bufio_io_err(&bio));
 		}
 		if (pfd.revents & POLLIN) {
 			r = bufio_read(&bio);
-			if (r == -1 && errno != EAGAIN)
+			if (r < 0 && r != -EAGAIN)
 				errx(1, "bufio_read: %s", bufio_io_err(&bio));
 			if (r == 0)
 				errx(1, "unexpected EOF");
blob - 8c41efaa709011edca44b1445296029d36c0644f
blob + c494d02cf40b165980995dafdeffcca562ece5ee
--- lib/bufio.c
+++ lib/bufio.c
@@ -159,13 +159,11 @@ bufio_close(struct bufio *bio)
 	case 0:
 		return 0;
 	case TLS_WANT_POLLIN:
-		errno = EAGAIN;
 		bio->wantev = BUFIO_WANT_READ;
-		return (-1);
+		return (-EAGAIN);
 	case TLS_WANT_POLLOUT:
-		errno = EAGAIN;
 		bio->wantev = BUFIO_WANT_WRITE;
-		return (-1);
+		return (-EAGAIN);
 	default:
 		return (-1);
 	}
@@ -241,22 +239,18 @@ bufio_ev(struct bufio *bio)
 int
 bufio_handshake(struct bufio *bio)
 {
-	if (bio->ctx == NULL) {
-		errno = EINVAL;
-		return (-1);
-	}
+	if (bio->ctx == NULL)
+		return (-EINVAL);
 
 	switch (tls_handshake(bio->ctx)) {
 	case 0:
 		return (0);
 	case TLS_WANT_POLLIN:
-		errno = EAGAIN;
 		bio->wantev = BUFIO_WANT_READ;
-		return (-1);
+		return (-EAGAIN);
 	case TLS_WANT_POLLOUT:
-		errno = EAGAIN;
 		bio->wantev = BUFIO_WANT_WRITE;
-		return (-1);
+		return (-EAGAIN);
 	default:
 		return (-1);
 	}
@@ -279,16 +273,13 @@ bufio_read(struct bufio *bio)
 		    rbuf->cap - rbuf->len);
 		switch (r) {
 		case TLS_WANT_POLLIN:
-			errno = EAGAIN;
 			bio->wantev = BUFIO_WANT_READ;
-			return (-1);
+			return (-EAGAIN);
 		case TLS_WANT_POLLOUT:
-			errno = EAGAIN;
 			bio->wantev = BUFIO_WANT_WRITE;
-			return (-1);
+			return (-EAGAIN);
 		case -1:
 			bio->wantev = 0;
-			errno = 0;
 			return (-1);
 		default:
 			bio->wantev = 0;
@@ -325,13 +316,11 @@ bufio_write(struct bufio *bio)
 	if (bio->ctx) {
 		switch (w = tls_write(bio->ctx, wbuf->buf, wbuf->len)) {
 		case TLS_WANT_POLLIN:
-			errno = EAGAIN;
 			bio->wantev = BUFIO_WANT_READ;
-			return (-1);
+			return (-EAGAIN);
 		case TLS_WANT_POLLOUT:
-			errno = EAGAIN;
 			bio->wantev = BUFIO_WANT_WRITE;
-			return (-1);
+			return (-EAGAIN);
 		case -1:
 			return (-1);
 		default:
blob - 53f97cf1eb90762e361151b23bfb6223d09c5302
blob + 938cea419f777b16583fa9434f4940ea5a951469
--- libexec/got-fetch-http/got-fetch-http.c
+++ libexec/got-fetch-http/got-fetch-http.c
@@ -55,9 +55,9 @@ bufio_getdelim_sync(struct bufio *bio, const char *nl,
 
 	do {
 		r = bufio_read(bio);
-		if (r == -1 && errno != EAGAIN)
+		if (r < 0 && r != -EAGAIN)
 			errx(1, "bufio_read: %s", bufio_io_err(bio));
-	} while (r == -1 && errno == EAGAIN);
+	} while (r == -EAGAIN);
 	return buf_getdelim(&bio->rbuf, nl, len);
 }
 
@@ -68,9 +68,9 @@ bufio_drain_sync(struct bufio *bio, void *d, size_t le
 
 	do {
 		r = bufio_read(bio);
-		if (r == -1 && errno != EAGAIN)
+		if (r < 0 && r != -EAGAIN)
 			errx(1, "bufio_read: %s", bufio_io_err(bio));
-	} while (r == -1 && errno == EAGAIN);
+	} while (r == -EAGAIN);
 	return bufio_drain(bio, d, len);
 }
 
@@ -81,9 +81,9 @@ bufio_close_sync(struct bufio *bio)
 
 	do {
 		r = bufio_close(bio);
-		if (r == -1 && errno == EAGAIN)
+		if (r < 0 && r != -EAGAIN)
 			errx(1, "bufio_read: %s", bufio_io_err(bio));
-	} while (r == -1 && errno == EAGAIN);
+	} while (r == -EAGAIN);
 }
 
 static long long
@@ -195,7 +195,7 @@ http_open(struct bufio *bio, int https, const char *me
 
 	do {
 		r = bufio_write(bio);
-		if (r == -1 && errno != EAGAIN)
+		if (r < 0 && r != -EAGAIN)
 			errx(1, "bufio_read: %s", bufio_io_err(bio));
 	} while (bio->wbuf.len != 0);
 
@@ -345,7 +345,7 @@ http_chunk(struct bufio *bio, const void *buf, size_t 
 
 	do {
 		r = bufio_write(bio);
-		if (r == -1 && errno != EAGAIN)
+		if (r < 0 && r != -EAGAIN)
 			errx(1, "bufio_read: %s", bufio_io_err(bio));
 	} while (bio->wbuf.len != 0);