user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] xap_helper: avoid strerror(3) inside signal handler
Date: Mon, 27 Nov 2023 21:54:38 +0000	[thread overview]
Message-ID: <20231127215439.91487-2-e@80x24.org> (raw)
In-Reply-To: <20231127215439.91487-1-e@80x24.org>

It's not async-signal-safe and the glibc implementation uses
malloc via asnprintf.  Practically it's not a problem unless the
kernel OOMs and the write(2) fails to the self-pipe.
---
 lib/PublicInbox/xap_helper.h | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/xap_helper.h b/lib/PublicInbox/xap_helper.h
index b6b517d5..1d8437c9 100644
--- a/lib/PublicInbox/xap_helper.h
+++ b/lib/PublicInbox/xap_helper.h
@@ -980,7 +980,8 @@ static void sigp(int sig) // parent signal handler
 {
 	static const char eagain[] = "signals coming in too fast";
 	static const char bad_sig[] = "BUG: bad sig\n";
-	static const char write_err[] = "BUG: sigp write: ";
+	static const char write_errno[] = "BUG: sigp write (errno)";
+	static const char write_zero[] = "BUG: sigp write wrote zero bytes";
 	char c = 0;
 
 	switch (sig) {
@@ -992,23 +993,17 @@ static void sigp(int sig) // parent signal handler
 		_exit(EXIT_FAILURE);
 	}
 	ssize_t w = write(pipefds[1], &c, 1);
-	if (w == sizeof(c)) return;
-	int e = 0;
-	if (w < 0) {
-		e = errno;
-		if (e == EAGAIN) {
-			write(STDERR_FILENO, eagain, sizeof(eagain) - 1);
-			return;
-		}
+	if (w > 0) return;
+	if (w < 0 && errno == EAGAIN) {
+		write(STDERR_FILENO, eagain, sizeof(eagain) - 1);
+		return;
+	} else if (w == 0) {
+		write(STDERR_FILENO, write_zero, sizeof(write_zero) - 1);
+	} else {
+		// strerror isn't technically async-signal-safe, and
+		// strerrordesc_np+strerrorname_np isn't portable
+		write(STDERR_FILENO, write_errno, sizeof(write_errno) - 1);
 	}
-	struct iovec iov[3];
-	iov[0].iov_base = (void *)write_err;
-	iov[0].iov_len = sizeof(write_err) - 1;
-	iov[1].iov_base = (void *)(e ? strerror(e) : "zero write");
-	iov[1].iov_len = strlen((const char *)iov[1].iov_base);
-	iov[2].iov_base = (void *)"\n";
-	iov[2].iov_len = 1;
-	(void)writev(STDERR_FILENO, iov, MY_ARRAY_SIZE(iov));
 	_exit(EXIT_FAILURE);
 }
 

  reply	other threads:[~2023-11-27 21:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-27 21:54 [PATCH 0/2] xap_helper C++ fixes Eric Wong
2023-11-27 21:54 ` Eric Wong [this message]
2023-11-27 21:54 ` [PATCH 2/2] xap_helper.h: avoid some off_t vs size_t problems Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231127215439.91487-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).