linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
To: "Shuah Khan" <shuah@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Willy Tarreau" <w@1wt.eu>,
	"Thomas Weißschuh" <linux@weissschuh.net>
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Subject: [PATCH v2 21/32] tools/nolibc: implement waitpid() in terms of waitid()
Date: Mon, 07 Apr 2025 08:52:44 +0200	[thread overview]
Message-ID: <20250407-nolibc-kselftest-harness-v2-21-f8812f76e930@linutronix.de> (raw)
In-Reply-To: <20250407-nolibc-kselftest-harness-v2-0-f8812f76e930@linutronix.de>

The old wait4() syscall used by waitpid() before is not available everywhere.
Switch to the waitid() syscall which is the new replacement.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
---
 tools/include/nolibc/sys.h | 71 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 59 insertions(+), 12 deletions(-)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 5c71a2f65c7bd93f6f516a54f7e63244466fad47..e39eefb2b82cc97eccfc030005fe0321632f12c4 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -1267,6 +1267,7 @@ int unlink(const char *path)
  * pid_t wait(int *status);
  * pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
  * pid_t waitpid(pid_t pid, int *status, int options);
+ * int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
  */
 
 static __attribute__((unused))
@@ -1291,18 +1292,6 @@ pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 	return __sysret(sys_wait4(pid, status, options, rusage));
 }
 
-
-static __attribute__((unused))
-pid_t waitpid(pid_t pid, int *status, int options)
-{
-	return __sysret(sys_wait4(pid, status, options, NULL));
-}
-
-
-/*
- * int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
- */
-
 static __attribute__((unused))
 int sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage)
 {
@@ -1316,6 +1305,64 @@ int waitid(int which, pid_t pid, siginfo_t *infop, int options)
 }
 
 
+static __attribute__((unused))
+pid_t waitpid(pid_t pid, int *status, int options)
+{
+	int idtype, ret;
+	siginfo_t info;
+	pid_t id;
+
+	if (pid == INT_MIN) {
+		SET_ERRNO(ESRCH);
+		return -1;
+	} else if (pid < -1) {
+		idtype = P_PGID;
+		id = -pid;
+	} else if (pid == -1) {
+		idtype = P_ALL;
+		id = 0;
+	} else if (pid == 0) {
+		idtype = P_PGID;
+		id = 0;
+	} else {
+		idtype = P_PID;
+		id = pid;
+	}
+
+	options |= WEXITED;
+
+	ret = waitid(idtype, id, &info, options);
+	if (ret)
+		return ret;
+
+	switch (info.si_code) {
+	case 0:
+		*status = 0;
+		break;
+	case CLD_EXITED:
+		*status = (info.si_status & 0xff) << 8;
+		break;
+	case CLD_KILLED:
+		*status = info.si_status & 0x7f;
+		break;
+	case CLD_DUMPED:
+		*status = (info.si_status & 0x7f) | 0x80;
+		break;
+	case CLD_STOPPED:
+	case CLD_TRAPPED:
+		*status = (info.si_status << 8) + 0x7f;
+		break;
+	case CLD_CONTINUED:
+		*status = 0xffff;
+		break;
+	default:
+		return -1;
+	}
+
+	return info.si_pid;
+}
+
+
 /*
  * ssize_t write(int fd, const void *buf, size_t count);
  */

-- 
2.49.0


  parent reply	other threads:[~2025-04-07  6:53 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-07  6:52 [PATCH v2 00/32] kselftest harness and nolibc compatibility Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 01/32] selftests: harness: Add harness selftest Thomas Weißschuh
2025-04-10 15:19   ` Shuah Khan
2025-04-07  6:52 ` [PATCH v2 02/32] selftests: harness: Use C89 comment style Thomas Weißschuh
2025-04-10 15:33   ` Shuah Khan
2025-04-07  6:52 ` [PATCH v2 03/32] selftests: harness: Ignore unused variant argument warning Thomas Weißschuh
2025-04-10 15:33   ` Shuah Khan
2025-04-07  6:52 ` [PATCH v2 04/32] selftests: harness: Mark functions without prototypes static Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 05/32] selftests: harness: Remove inline qualifier for wrappers Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 06/32] selftests: harness: Remove dependency on libatomic Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 07/32] selftests: harness: Implement test timeouts through pidfd Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 08/32] selftests: harness: Don't set setup_completed for fixtureless tests Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 09/32] selftests: harness: Always provide "self" and "variant" Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 10/32] selftests: harness: Move teardown conditional into test metadata Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 11/32] selftests: harness: Add teardown callback to " Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 12/32] selftests: harness: Stop using setjmp()/longjmp() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 13/32] selftests: harness: Guard includes on nolibc Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 14/32] tools/nolibc: handle intmax_t/uintmax_t in printf Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 15/32] tools/nolibc: use intmax definitions from compiler Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 16/32] tools/nolibc: use pselect6_time64 if available Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 17/32] tools/nolibc: use ppoll_time64 " Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 18/32] tools/nolibc: add tolower() and toupper() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 19/32] tools/nolibc: add _exit() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 20/32] tools/nolibc: add setpgrp() Thomas Weißschuh
2025-04-07  6:52 ` Thomas Weißschuh [this message]
2025-04-07  6:52 ` [PATCH v2 22/32] Revert "selftests/nolibc: use waitid() over waitpid()" Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 23/32] tools/nolibc: add dprintf() and vdprintf() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 24/32] tools/nolibc: add getopt() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 25/32] tools/nolibc: allow different write callbacks in printf Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 26/32] tools/nolibc: allow limiting of printf destination size Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 27/32] tools/nolibc: add snprintf() and friends Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 28/32] selftests/nolibc: use snprintf() for printf tests Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 29/32] selftests/nolibc: rename vfprintf test suite Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 30/32] selftests/nolibc: add test for snprintf() truncation Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 31/32] tools/nolibc: implement width padding in printf() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 32/32] HACK: selftests/nolibc: demonstrate usage of the kselftest harness Thomas Weißschuh
2025-04-10 15:19 ` [PATCH v2 00/32] kselftest harness and nolibc compatibility Shuah Khan
2025-04-10 20:01   ` Shuah Khan

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=20250407-nolibc-kselftest-harness-v2-21-f8812f76e930@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.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;
as well as URLs for NNTP newsgroup(s).