mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Danish Khateeb <danishkhateeb03@gmail.com>
To: "Willy Tarreau" <w@1wt.eu>, "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	Danish Khateeb <danishkhateeb03@gmail.com>
Subject: [PATCH 2/2] selftests/nolibc: add a test for errx()
Date: Thu, 24 Sep 2026 07:09:58 -0500	[thread overview]
Message-ID: <20260924120958.23698-3-danishkhateeb03@gmail.com> (raw)
In-Reply-To: <20260924120958.23698-1-danishkhateeb03@gmail.com>

nolibc-test does not call any of the err.h functions, so nothing
noticed that verrx(), and errx() through it, printed their messages
with the wrong arguments, or crashed.

Add a test that calls errx() in a child with stderr redirected to a
pipe, and checks the exit status and the message.

Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 47 ++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 37c11a7fce23..1447c2e39af5 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -32,6 +32,7 @@
 #include <sys/utsname.h>
 #include <sys/wait.h>
 #include <dirent.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <poll.h>
@@ -2223,6 +2224,51 @@ int test_asprintf(void)
 	return 0;
 }
 
+int test_errx(void)
+{
+	char buf[100], expected[100];
+	int pipefd[2], status;
+	ssize_t len;
+	pid_t pid;
+
+	if (pipe(pipefd) == -1)
+		return 1;
+
+	/* flush the printf buffer to avoid child flush it */
+	fflush(stdout);
+	fflush(stderr);
+
+	pid = fork();
+	if (pid == -1) {
+		close(pipefd[0]);
+		close(pipefd[1]);
+		return 2;
+	}
+
+	if (pid == 0) {
+		dup2(pipefd[1], STDERR_FILENO);
+		errx(42, "%d %s", 1234, "foo");
+	}
+
+	close(pipefd[1]);
+	pid = waitpid(pid, &status, 0);
+	len = read(pipefd[0], buf, sizeof(buf) - 1);
+	close(pipefd[0]);
+
+	if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 42)
+		return 3;
+
+	if (len == -1)
+		return 4;
+	buf[len] = 0;
+
+	snprintf(expected, sizeof(expected), "%s: 1234 foo\n", program_invocation_short_name);
+	if (strcmp(buf, expected) != 0)
+		return 5;
+
+	return 0;
+}
+
 static int run_printf(int min, int max)
 {
 	int test;
@@ -2287,6 +2333,7 @@ static int run_printf(int min, int max)
 		CASE_TEST(scanf);        EXPECT_ZR(1, test_scanf()); break;
 		CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
 		CASE_TEST(asprintf);     EXPECT_ZR(1, test_asprintf()); break;
+		CASE_TEST(errx);         EXPECT_ZR(1, test_errx()); break;
 		case __LINE__:
 			return ret; /* must be last */
 		/* note: do not set any defaults so as to permit holes above */
-- 
2.55.0


      parent reply	other threads:[~2026-09-24 12:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 12:09 [PATCH 0/2] tools/nolibc: fix verrx() and errx() Danish Khateeb
2026-09-24 12:09 ` [PATCH 1/2] tools/nolibc: fix verrx() and errx() message formatting Danish Khateeb
2026-09-24 12:09 ` Danish Khateeb [this message]

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

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

  git send-email \
    --in-reply-to=20260924120958.23698-3-danishkhateeb03@gmail.com \
    --to=danishkhateeb03@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=w@1wt.eu \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®