From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0610928469B; Wed, 23 Apr 2025 15:01:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420502; cv=none; b=C/MkMgZEinLwT2oXWn+dTqOjba3xIxLBQrtTsUo4O8iZC7X89BR3eKhH37lTcZOTPL/E9ZevK7s1B65t4sNv4ewX/H2Pw/9wGvI3roYrOjKe2K+UlEZEHuCdAd2XcgW273OmQLyA0m8ZFCZoFPUaFkrGA1uEK1KAj1aMPJrx/2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420502; c=relaxed/simple; bh=jX6My3jcrxavMP5EesEhuX0EJNp3o2ftH7MFb8jfw3g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rJQiKYtcrRnz9vmFQEA1NeznYfOWQlly7VW+Fl3cubHgleNAooTbO2+VPcT+ODPnVAkNi6V0KYJjZJGkWPPkRTmUxVJGkL7+NT2aMcPeI+JrXal6bpu6NkzSEJ/FYEkJGWzoi1k36zhQTpV5XAYgmTKYP0SECgRj2S+wpKajz+E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bD2OSUXq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=otOppsYY; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bD2OSUXq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="otOppsYY" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HYPtxUj1uqJvzEWy6ALyMeU9sbBmbB2jbaGAAmmgheY=; b=bD2OSUXqcGq5XNElXb5siEgtCK/VPvjKDe/8oXg6AMPttkVAQbxr1TkU6Ffn90BGYMfWtw lJEhZgLmWo4HUtH+ipCVJGl4tHhWcKr9+g4ELvi8QLUa47kEU7X6aD3tlVplbX+BbDOHxa JYII3cOEi9Uxp5KONA/XEnCGtLdDWmarVVuZESaNI5p9us1/5xo2RtPc/3W5QOnwq8Ohjb GHYcwXVnDN0rI4tAdlbA/5yfYsQM+9jpvfCevN4Qj8QkkxzuPlfEc0K2PBd6tVMdm5qyOZ HSiych4xQbOQ5QpAxy9r+nH4ZxvistiONvlVmYYtH28+hmdfyC2/MDova172RA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HYPtxUj1uqJvzEWy6ALyMeU9sbBmbB2jbaGAAmmgheY=; b=otOppsYYEUbPfIY2g7WlTXN73NsSn207jWqBGXHg9zOGNBqYRGz2XYcJPAMIrUJeBzAetD exDqQ+LdJL+QToCA== Date: Wed, 23 Apr 2025 17:01:31 +0200 Subject: [PATCH 01/15] tools/nolibc: add strstr() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-1-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=2403; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=jX6My3jcrxavMP5EesEhuX0EJNp3o2ftH7MFb8jfw3g=; b=ioqG/7hgeKH6ZECd/6WrFY0NZRK82KCFNKZ5B1pnkO53UfP3/W0GtZFAmNplOAuFv+08AsxPH Dh9F1dzQ8uQD+0lmbCuEypmvv6qAiJaWwz95GgQz02ZWTPnA1ST9eYY X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/string.h | 20 ++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 3 +++ 2 files changed, 23 insertions(+) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index f0d335f0e467ec870066811289dfd11e46e60a92..b7b2b93bc90c9330dec7186d45c= 18fd3f24373d0 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -289,6 +289,26 @@ char *strrchr(const char *s, int c) return (char *)ret; } =20 +static __attribute__((unused)) +char *strstr(const char *haystack, const char *needle) +{ + size_t len_haystack, len_needle; + + len_needle =3D strlen(needle); + if (!len_needle) + return NULL; + + len_haystack =3D strlen(haystack); + while (len_haystack >=3D len_needle) { + if (!memcmp(haystack, needle, len_needle)) + return (char *)haystack; + haystack++; + len_haystack--; + } + + return NULL; +} + static __attribute__((unused)) int tolower(int c) { diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 1ad0db92f0ed47f708363b2e558717fa0e686b8f..3e15a25ccddf5135db1f59bce1e= efdff2ffe57f6 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1211,6 +1211,9 @@ int run_stdlib(int min, int max) CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "= bar", 2), buf, 3, "b"); break; CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "= bar", 3), buf, 3, "ba"); break; CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "= bar", 4), buf, 3, "bar"); break; + CASE_TEST(strstr_foobar_foo); EXPECT_STREQ(1, strstr("foobar", "foo"), = "foobar"); break; + CASE_TEST(strstr_foobar_bar); EXPECT_STREQ(1, strstr("foobar", "bar"), = "bar"); break; + CASE_TEST(strstr_foobar_baz); EXPECT_PTREQ(1, strstr("foobar", "baz"), = NULL); break; CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20",= 4), 0); break; CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60",= 4), 0); break; CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20",= 4), 0); break; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F57228134C; Wed, 23 Apr 2025 15:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; cv=none; b=FkI228dBZvSCHcPPRryfMALu6OBBCqIElyB7OYAFOgcX5Q2p/bkdjFC0ikXscbfk8gHto+gUga+K3l5lmSZ9Gu5cpW32j3sbh+22jL33HHQ4cKxq1h/VHD24AggbV+xZJH5XML1j2/pBxyH3+zjDP/EoKsmynjMDcKItw6kJg5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; c=relaxed/simple; bh=Np0oBwLMhPpLv5pP1PIcbr1JsW8qIWPf7vPBJHuBFys=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OBjBx285Wo4Vf2CEk3Yx2+z+qmMJSUvyjwQqZot+tYRqr6f2AkdPggC9Ku9eaTCTEO3jVf89Y9T4FmkAhYcKIH3hij8dQLhL9ZErdbyNC16SqQkQMLXArx7MX2Xv9IlPUAD7VQuTJ3Rw+G8ODtUtecR+2mEcZ1qSni/svRSHx/Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jbtB/kQ9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=B1MKPmVt; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jbtB/kQ9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="B1MKPmVt" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gWxX6o0dhF/CJHuGOM1VGqf2IcTvjrF7WP0MZxIu7Cw=; b=jbtB/kQ9uWgE8OJyqblfpQdCc566e7cWcybH7ZhhxNcn3Xkq7BQq1EIMqpB0PwCuW4I/pi KYnjjRFF84Tepn5tMw0lmaVKuxnuLI+7Oi+omFxOU7yFCIrxA8TxxHVCDqql75FB0LWPWy MPkeIG6zqbKjhlCJFo201inSPQOzSZ/sCsjpTGJKt6L8C7Xzozbfl8V7I7jYm8Z0ji7zaC fNFd/5n+TiQ1vZyB+n8hYLGylh9YTRqFKQzNLoNhIaGQpm6qvv3Ey5QJ0xtFi1pgRKaFvX Q0avVghWwIc/2ZCuR8D5Di78vKt/TCBdLk07veyYBC2KyEBvrMnDPMujUtt+Gw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gWxX6o0dhF/CJHuGOM1VGqf2IcTvjrF7WP0MZxIu7Cw=; b=B1MKPmVtDKLHYiGBPYwysN0lO2LGd3UVh1pxW3QZg/029dnm8wT+lwirUnDX6dlwggKIxQ mNN6L+sxRKnFUMCg== Date: Wed, 23 Apr 2025 17:01:32 +0200 Subject: [PATCH 02/15] tools/nolibc: add %m printf format Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-2-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=2389; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=Np0oBwLMhPpLv5pP1PIcbr1JsW8qIWPf7vPBJHuBFys=; b=ussC6c5aR9ND9nLzJ/CGUwKIQnZCxYOMAiRj2GOVyUniMrJp24lVNVClu071tTSARr7UmcRlp UngxaWScOpIDZxeSJmH1hBMNgWBJO3V5PSRfIW72tOjliHCuWu3grYw X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= The %m format can be used to format the current errno. It is non-standard but supported by other commonly used libcs like glibc and musl, so applications do rely on them. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/stdio.h | 7 +++++++ tools/testing/selftests/nolibc/nolibc-test.c | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index fb0417477759ee6c9663e84807c1d1067e735dec..02426682c34bcd82d9556036c6c= 3e73a6a5a3b4d 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -17,6 +17,8 @@ #include "string.h" #include "compiler.h" =20 +static const char *strerror(int errnum); + #ifndef EOF #define EOF (-1) #endif @@ -289,6 +291,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t st= ate, size_t n, const char if (!outstr) outstr=3D"(null)"; } +#ifndef NOLIBC_IGNORE_ERRNO + else if (c =3D=3D 'm') { + outstr =3D strerror(errno); + } +#endif /* NOLIBC_IGNORE_ERRNO */ else if (c =3D=3D '%') { /* queue it verbatim */ continue; diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 3e15a25ccddf5135db1f59bce1eefdff2ffe57f6..b7440a667db6b541a2548bdf518= 2bee0277100ed 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1393,6 +1393,23 @@ static int test_scanf(void) return 0; } =20 +int test_strerror(void) +{ + char buf[100]; + ssize_t ret; + + memset(buf, 'A', sizeof(buf)); + + errno =3D EINVAL; + ret =3D snprintf(buf, sizeof(buf), "%m"); + if (is_nolibc) { + if (ret < 6 || memcmp(buf, "errno=3D", 6)) + return 1; + } + + return 0; +} + static int run_printf(int min, int max) { int test; @@ -1421,6 +1438,7 @@ static int run_printf(int min, int max) CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); b= reak; CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%2= 5d", 1); break; CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; + CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break; case __LINE__: return ret; /* must be last */ /* note: do not set any defaults so as to permit holes above */ --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F50727CB12; Wed, 23 Apr 2025 15:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; cv=none; b=hAYlzRrTGcBRrH7gkX6xwH3O9Z3FqtpXsuL7p5oIRJrhI4dwPOZk4OmcQPH3tMW+8W2Ot6cjSrkjcn5qAI7FMqOpBTR8RrCLsPKEVeJyVdS3NFJlY/iJUNDiXu9phQBJgueg3l428WBXQQKmycX+hLzb6CbbFK9zvMJDyayRYVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; c=relaxed/simple; bh=fVoLFuGSUSVnAazZi4qvsdowRwhDO8H618HmxSKX/6A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FQoerVO95ncxWC/VdN7MqMlDHw/RpB7PWJ3Sgo8Y4xE2OzKckQNhpjOJ2NlC5Ig7t6ACL9S36zuQKnYa0VL4JO6KvUhhHJJ1EFnA9EXVpSptFwIhvDokpAKgc3qgp6gVQsAKpNObTMGapQAQbyZARQvHPG2AeCQo8uUx1q0vafw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Erf9YT9h; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0xZNGsLx; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Erf9YT9h"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0xZNGsLx" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420500; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18Iz/Bdikt1dQavoAudy/VOxjZ22ac5YRQQjF3CfKfg=; b=Erf9YT9hH8Wc8KCfGxy9tOhyxctIqoCx6lSvW9rkK86pp8431yPEPlF1a+0TJ5OHAxv5AQ o/ukqqj1aF8+fKedqbpOotSkTUgSEt6u74AtDhwaiWyjlzMO40ctWIWrLnvZPtknKMY8sI zHmFgO02Qk6sWFL3bFcqAH3Em/F/BKzRU6gQlj20iGPylWq+PudEIiluysunyeAtZQI1O3 ZhIYqNG9NBJDb10ItZNIvZAnHBe1yASjZq64nbBBZ6wTRuu0bD7BWyQw0IheHuy+YORC9o m80qEJKjhJ3cKQDjuyiGsnWaksih+nW0OZi9o+WyYGStOWHgYAbX8rey8bd9tw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420500; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18Iz/Bdikt1dQavoAudy/VOxjZ22ac5YRQQjF3CfKfg=; b=0xZNGsLxTpGphEZ7vcHb+Zl68twFgwGFsVZ8VSy1DyiqxmZwZkVCQ7/MBGi4FnO5zGNV63 egW+qm/KdZ5SH9Cw== Date: Wed, 23 Apr 2025 17:01:33 +0200 Subject: [PATCH 03/15] tools/nolibc: add more stat() variants Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-3-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=2043; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=fVoLFuGSUSVnAazZi4qvsdowRwhDO8H618HmxSKX/6A=; b=cgGrOPnhG6fND0X5FFTnFL2p//uNE0yDgWprFYXinT7QAj6i+5pQJjbLqHBdWorotMUUEqbcy woEHHrMsy4fC1VGQQrwmncqb/nlgeC3ZE4iGJEJ1aHqJpkKY70CukNN X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= Add fstat(), fstatat() and lstat(). All of them use the existing implementa= tion based on statx(). Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/sys/stat.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/include/nolibc/sys/stat.h b/tools/include/nolibc/sys/sta= t.h index 0eaf5496ce233a4d8b5a239eef5ecefe05a39dd6..53c5c8addc829e36b927c3dd39b= 2a24f3472dcf0 100644 --- a/tools/include/nolibc/sys/stat.h +++ b/tools/include/nolibc/sys/stat.h @@ -14,6 +14,9 @@ /* * int statx(int fd, const char *path, int flags, unsigned int mask, struc= t statx *buf); * int stat(const char *path, struct stat *buf); + * int fstatat(int fd, const char *path, struct stat *buf, int flag); + * int fstat(int fildes, struct stat *buf); + * int lstat(const char *path, struct stat *buf); */ =20 static __attribute__((unused)) @@ -34,12 +37,12 @@ int statx(int fd, const char *path, int flags, unsigned= int mask, struct statx * =20 =20 static __attribute__((unused)) -int stat(const char *path, struct stat *buf) +int fstatat(int fd, const char *path, struct stat *buf, int flag) { struct statx statx; long ret; =20 - ret =3D __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_S= TATS, &statx)); + ret =3D __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_= STATS, &statx)); if (ret =3D=3D -1) return ret; =20 @@ -67,6 +70,24 @@ int stat(const char *path, struct stat *buf) return 0; } =20 +static __attribute__((unused)) +int stat(const char *path, struct stat *buf) +{ + return fstatat(AT_FDCWD, path, buf, 0); +} + +static __attribute__((unused)) +int fstat(int fildes, struct stat *buf) +{ + return fstatat(fildes, "", buf, AT_EMPTY_PATH); +} + +static __attribute__((unused)) +int lstat(const char *path, struct stat *buf) +{ + return fstatat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); +} + =20 /* make sure to include all global symbols */ #include "../nolibc.h" --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9854328469F; Wed, 23 Apr 2025 15:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; cv=none; b=VcHsAwBulpky7KDrE0GBMErTHsr7ZAtfurO+5mVjI3tgihwcIxJ+ym7HMl/4qcrdpJU99+g3aqjvmHtI8kFEEIEild+a1RzYlTxXde48QDvErYBqgUo5JEv/Y8CUkmQpVyjhy8h/Q8I29V4tHlayR08gwlaBaUkmZSP4mRqQp18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420504; c=relaxed/simple; bh=jBXf5Mtp9KvxjCbpxdmGGb3Wzx4EvSPLG4sESOoybeQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d2a4aB0KW9buE+4pTz0yMnmD8w28kC0dBKZzm+YggMjoxpMlGUMb/Q1mOedaA4sNgAnhtpdIlLt2Rr16FjnbLdTGzBQ3YYh/HLeB7Q6VGBoq6Z+6NESJxCtMsVmkmUBcapfIjLniLPvCUIANZzYxS4TUor8OyziShiioneUYvMI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rIDQAI1U; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=H9Ed+i7p; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rIDQAI1U"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="H9Ed+i7p" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wWLbPEK5LvxBlDZi/5mdWIWYYff5AO7fk+5OpUiSiZU=; b=rIDQAI1UceFgts2Y98B5e3CKEXXFh4jm4guFodHltQEbrKdig3eVnbky1zBfKSDSWlh3XV pT5gslxCSJEAUAEenP4DOJOMvGZXmG/x4yKBdoFOb7a0jMxU5fAZBn6aqt/SU49/osgr2f JKCvs9YP3ypifU127eRRgLyWyFDkabnYfV+nZajDaBA9VwOYX3LLNNEvEafp5YCjX2VyIL iteZrtU1Sv9KiajVeX26oXNEaL7QZNWD7a53+wm4Gj4MUXoinHwz0fv7/U2BAZUdTIuOW3 pDvv6Viy2/1Ye7K6rnzwe6PHZdRFmYdhvhYh37niOeVL2LxalJgM6sV71CWFhQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wWLbPEK5LvxBlDZi/5mdWIWYYff5AO7fk+5OpUiSiZU=; b=H9Ed+i7pqlTJ2LhJMINn2JxdAy0H16Gvi0ejMS23Fs4XzX4BKR4oOTwrfutTExRyS2OppQ yCj99Su1RhUFECBQ== Date: Wed, 23 Apr 2025 17:01:34 +0200 Subject: [PATCH 04/15] tools/nolibc: add mremap() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-4-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=2662; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=jBXf5Mtp9KvxjCbpxdmGGb3Wzx4EvSPLG4sESOoybeQ=; b=GXyXOFUZo3z60zm1V1Zq5wlUG8H2zppRlAY55njuNEIeJpeEVus5VyN8+cNfz99tr9bLO1mHJ ojmTCKeAYjgBXMEgRYVQYk9SLnp0w2vgFjNTT+ymDXkh9+y5Q/zIOO6 X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/sys/mman.h | 19 +++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 14 +++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tools/include/nolibc/sys/mman.h b/tools/include/nolibc/sys/mma= n.h index ad9d06b6b7919ec76a0652266158366cf639a77a..d1c213c19d7fa55c9db3a9527dd= 35b6072ca9485 100644 --- a/tools/include/nolibc/sys/mman.h +++ b/tools/include/nolibc/sys/mman.h @@ -45,6 +45,25 @@ void *mmap(void *addr, size_t length, int prot, int flag= s, int fd, off_t offset) return ret; } =20 +static __attribute__((unused)) +void *sys_mremap(void *old_address, size_t old_size, size_t new_size, int = flags, void *new_address) +{ + return (void *)my_syscall5(__NR_mremap, old_address, old_size, + new_size, flags, new_address); +} + +static __attribute__((unused)) +void *mremap(void *old_address, size_t old_size, size_t new_size, int flag= s, void *new_address) +{ + void *ret =3D sys_mremap(old_address, old_size, new_size, flags, new_addr= ess); + + if ((unsigned long)ret >=3D -4095UL) { + SET_ERRNO(-(long)ret); + ret =3D MAP_FAILED; + } + return ret; +} + static __attribute__((unused)) int sys_munmap(void *addr, size_t length) { diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index b7440a667db6b541a2548bdf5182bee0277100ed..abe0ae794208762f6d91ad81e90= 2fbf77253a1c1 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -926,7 +926,7 @@ int test_mmap_munmap(void) { int ret, fd, i, page_size; void *mem; - size_t file_size, length; + size_t file_size, length, mem_length; off_t offset, pa_offset; struct stat stat_buf; const char * const files[] =3D { @@ -966,14 +966,22 @@ int test_mmap_munmap(void) offset =3D 0; length =3D file_size - offset; pa_offset =3D offset & ~(page_size - 1); + mem_length =3D length + offset - pa_offset; =20 - mem =3D mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd= , pa_offset); + mem =3D mmap(NULL, mem_length, PROT_READ, MAP_SHARED, fd, pa_offset); if (mem =3D=3D MAP_FAILED) { ret =3D 1; goto end; } =20 - ret =3D munmap(mem, length + offset - pa_offset); + mem =3D mremap(mem, mem_length, mem_length * 2, MREMAP_MAYMOVE, 0); + if (mem =3D=3D MAP_FAILED) { + munmap(mem, mem_length); + ret =3D 1; + goto end; + } + + ret =3D munmap(mem, mem_length * 2); =20 end: close(fd); --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A0D02857EB; Wed, 23 Apr 2025 15:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420506; cv=none; b=CI/UNGRDwm+wqYJ3lI4TN3pXctCGD+lkTAhmazRD2oTDRCMWaSv8wWvHf/AmFfKUiqCYt5h9IYM7/RR3ONyHVnMGmYGOLBPqS3UOC3Q9i772mgPxhBrHqF8VKHfcKyHVTjoTLBc3nUGX9z4VdY5oNOTPFIqAbLTQqalg66ZxgCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420506; c=relaxed/simple; bh=3nwM0JHeNoU0v891mo4FQH2ynggcOB+InZcti6LaMCs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KaZt8R84lu7FKeWTeZj2/2V3KF9a602J5FC2iupb0UomUch4Cyg1mh72CdQgLvlkvFEvRj6RbA+BdstNgipGrIDWF2F+Ne0dJvC+u0hXYYU9XdXcOKAJd44FSVZXrXHpHKd+tWLLbMq0PpFSDrIprEv82vN8blPCGEmSm0Su3Uk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kF7MEDhP; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=b5BJvqys; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kF7MEDhP"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="b5BJvqys" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RlgB8Ut5fBCGPHTtZnYDvvE4LxvOqgGbRbKmjz3WVBI=; b=kF7MEDhPnDV2JB4y+nHurIJqtj4J/mVhzN/aCUMc2YHdOtO9tSkG+xfGvhldx83W7Nxk5w 32KGxcH0DaAXY4ru+jTTMX2WhLC6Ry1TCVapQW7m19HAB4Xjx1xCCfJ9ZOOMgv3ILyPYo4 11sfiFP6647BKeRK+fl8kCYhoxXcI598Fqt4DeRUWVp4++6suiCvcN8hIzzM4SXHTMLupi U57U889lBc+PQUGxA52FX+VdIbfh+SQacXGESn9kgXqMg12lZ2eQ7RrBn8ScmAUos1vQfd t3dMocEFqavKsMC9lSFZ6x9Rx6lVKWFAaJ+C8198uvfoENCISfWTHwZdapaHdA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RlgB8Ut5fBCGPHTtZnYDvvE4LxvOqgGbRbKmjz3WVBI=; b=b5BJvqysUA+2cqCvGL+GfL/ddkiEXuvRtcjsurvh/kzUZTUucraEQcsQ+ecuQ2PJ0CP3tU mxuW2eBFhWqVoyDQ== Date: Wed, 23 Apr 2025 17:01:35 +0200 Subject: [PATCH 05/15] tools/nolibc: add getrandom() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-5-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=3922; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=3nwM0JHeNoU0v891mo4FQH2ynggcOB+InZcti6LaMCs=; b=fIrdYrOUHNVpNztAKovT/6sdqK6zjRK+7sqQby8f5sL16nf3uB9bHgbb8HW7nwcEP4jjOme7K 68gO3SgJh/YAPjwAhfMfuNzn/BmHAD/D9q/sGlYhMK571VM5SZ0JuUX X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/sys/random.h | 32 ++++++++++++++++++++++++= ++++ tools/testing/selftests/nolibc/nolibc-test.c | 19 +++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index e05862cd08051685112f067d6eb45716613dd43c..b22ff1e268b2e4bd788e974d6d8= f2e1ef96dfc5f 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -47,6 +47,7 @@ all_files :=3D \ sys.h \ sys/auxv.h \ sys/mman.h \ + sys/random.h \ sys/stat.h \ sys/syscall.h \ sys/time.h \ diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index d1b949e094eeb7cc0fe875deeafa4c972ecf35b2..3f329feb379c4c808d2e56fbb2b= 7a6c4b08a7c0d 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -98,6 +98,7 @@ #include "sys.h" #include "sys/auxv.h" #include "sys/mman.h" +#include "sys/random.h" #include "sys/stat.h" #include "sys/syscall.h" #include "sys/time.h" diff --git a/tools/include/nolibc/sys/random.h b/tools/include/nolibc/sys/r= andom.h new file mode 100644 index 0000000000000000000000000000000000000000..b5a904dffbfb5039ca8f9efb9ea= f68e7bb1716b5 --- /dev/null +++ b/tools/include/nolibc/sys/random.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * random definitions for NOLIBC + * Copyright (C) 2025 Thomas Wei=C3=9Fschuh + */ + +#ifndef _NOLIBC_SYS_RANDOM_H +#define _NOLIBC_SYS_RANDOM_H + +#include "../arch.h" +#include "../sys.h" + +/* + * ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); + */ + +static __attribute__((unused)) +ssize_t sys_getrandom(void *buf, size_t buflen, unsigned int flags) +{ + return my_syscall3(__NR_getrandom, buf, buflen, flags); +} + +static __attribute__((unused)) +ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) +{ + return __sysret(sys_getrandom(buf, buflen, flags)); +} + +/* make sure to include all global symbols */ +#include "../nolibc.h" + +#endif /* _NOLIBC_SYS_RANDOM_H */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index abe0ae794208762f6d91ad81e902fbf77253a1c1..95d08e9ccf5b3be924548100e96= 21cd47f39e8c2 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -807,6 +808,23 @@ static int test_dirent(void) return 0; } =20 +int test_getrandom(void) +{ + uint64_t rng =3D 0; + ssize_t ret; + + ret =3D getrandom(&rng, sizeof(rng), 0); + if (ret !=3D sizeof(rng)) + return ret; + + if (!rng) { + errno =3D EINVAL; + return -1; + } + + return 0; +} + int test_getpagesize(void) { int x =3D getpagesize(); @@ -1124,6 +1142,7 @@ int run_syscall(int min, int max) CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1);= break; CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null= "), -1, ENOTDIR); break; CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break; + CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break; CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); = break; CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); b= reak; CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28DCD285412; Wed, 23 Apr 2025 15:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420505; cv=none; b=MrMRuHmOGjvYOiSg59v0z1UE6EmaZZbLBr0krXJxrfR1rcu4FHBHbgFx59sgPGhiu3DSnh0LhKuE/JeY9ZxtdZlh0smAXlF/Tz4zDg3iQxydudVJ3mJLxC02NcDVaeilfMfJpxEMs1wPM5hfPGm1v29hbxeWxnR/04WGpzVfxZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420505; c=relaxed/simple; bh=QfQ9GtmoHqSeMw2lyf9jJI9CMPdDtCVSWd27rKlz+Xo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RY2Lf2vPnOoqAP75RBUcUubuuvFvrH78351C91pbM7UyGBbrYXAer1FhdcwiFscaRHv6M71TN4Z7N2bucPogiIwsLYWLTqfE+0MxdgVcnybSpF2dHAAPdn23G+j/3bj8tmZXxpJeIRQ4DuiqCSFNxnFfXSexQPcp/IkBXxr1veI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0eqsa60k; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=yesxX99x; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0eqsa60k"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="yesxX99x" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/8oVfZJdfrHYqPrR5e8gdRZt2wr7Trjz1vMDOcsGB5A=; b=0eqsa60ksROqE0A9zwtTZhW30qJ0E/zcXur12kRm+ZE6smO2TufA6hkwzjeJIOdRCAF3qO S5AFgOFmxuxIRPMEmz2h+LQvC1nPIeSVyT94310ywK9qf223sIsfs9aDS0jIy5iMxTgxf0 PVL5OEfZ9YmeGF8qEv0fCQFarv5rAjFmoXDWuGqyB5blVtVCw6HQQqFoam/wJ5jq3ybHoH BH4mhS3EyEbkPQTl3gF8hTXrEzP+08OV4cJWhGJfLS5QfxSWsU8o23TZWfJCrTJdmupQUx ahZChMSbIN4TmF21qTf3bomRJeytdFy82o2fooUU5fCkx6OeNInUIvd5T990tA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/8oVfZJdfrHYqPrR5e8gdRZt2wr7Trjz1vMDOcsGB5A=; b=yesxX99xUgTUaOxIuEuhNzR192JpkaMNkMAUoXaK2fkbsi5D7PxSfVgCJOTRJRD7HKNqAQ q+ZnftM/VjXwZgCQ== Date: Wed, 23 Apr 2025 17:01:36 +0200 Subject: [PATCH 06/15] tools/nolibc: add abs() and friends Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-6-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=3810; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=QfQ9GtmoHqSeMw2lyf9jJI9CMPdDtCVSWd27rKlz+Xo=; b=MRZljW58twBXWkoNS/TKUojIU7T/JIsIXFCxZ+jtRg7m9xSTcGEKdxuWdMjsYmux4OZwwmlmI IB6Qlx6iFDCCGgU5MObdpwaa7QI/IhYrWEfnRVetj+yp2AX9xgftjxu X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/math.h | 31 ++++++++++++++++++++++++= ++++ tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/stdlib.h | 18 ++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 5 files changed, 53 insertions(+) diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index b22ff1e268b2e4bd788e974d6d8f2e1ef96dfc5f..3a82e036a2c1762da5c3916ff9f= 4e158fb6106ff 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -35,6 +35,7 @@ all_files :=3D \ fcntl.h \ getopt.h \ limits.h \ + math.h \ nolibc.h \ signal.h \ stackprotector.h \ diff --git a/tools/include/nolibc/math.h b/tools/include/nolibc/math.h new file mode 100644 index 0000000000000000000000000000000000000000..4906d3eb7c1aed0c402d3ab3952= 6dd1c93b5df2a --- /dev/null +++ b/tools/include/nolibc/math.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * math definitions for NOLIBC + * Copyright (C) 2025 Thomas Wei=C3=9Fschuh + */ + +#ifndef _NOLIBC_SYS_MATH_H +#define _NOLIBC_SYS_MATH_H + +static __inline__ +double fabs(double x) +{ + return x >=3D 0 ? x : -x; +} + +static __inline__ +float fabsf(float x) +{ + return x >=3D 0 ? x : -x; +} + +static __inline__ +long double fabsl(long double x) +{ + return x >=3D 0 ? x : -x; +} + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#endif /* _NOLIBC_SYS_MATH_H */ diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 3f329feb379c4c808d2e56fbb2b7a6c4b08a7c0d..2207f3d3c5cf92b96150149050f= 0bde0ed7e7147 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -115,6 +115,7 @@ #include "dirent.h" #include "fcntl.h" #include "getopt.h" +#include "math.h" =20 /* Used by programs to avoid std includes */ #define NOLIBC diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 69cf1d4418f1c1f59192aee40ea3a5b684d2709a..c8c44859aee20e2b9eb55ddae48= 8f1f70b710fbb 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -29,6 +29,24 @@ static __attribute__((unused)) char itoa_buffer[21]; * As much as possible, please keep functions alphabetically sorted. */ =20 +static __inline__ +int abs(int j) +{ + return j >=3D 0 ? j : -j; +} + +static __inline__ +long labs(long j) +{ + return j >=3D 0 ? j : -j; +} + +static __inline__ +long long llabs(long long j) +{ + return j >=3D 0 ? j : -j; +} + /* must be exported, as it's used by libgcc for various divide functions */ void abort(void); __attribute__((weak,unused,noreturn,section(".text.nolibc_abort"))) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 95d08e9ccf5b3be924548100e9621cd47f39e8c2..b6724c314766b0cf34d7cae5cac= 28a9102ebe66e 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1315,6 +1315,8 @@ int run_stdlib(int min, int max) CASE_TEST(tolower_noop); EXPECT_EQ(1, tolower('a'), 'a'); bre= ak; CASE_TEST(toupper); EXPECT_EQ(1, toupper('a'), 'A'); bre= ak; CASE_TEST(toupper_noop); EXPECT_EQ(1, toupper('A'), 'A'); bre= ak; + CASE_TEST(abs); EXPECT_EQ(1, abs(-10), 10); break; + CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10); break; =20 case __LINE__: return ret; /* must be last */ --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68284288CBC; Wed, 23 Apr 2025 15:01:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; cv=none; b=ZJY3FqjSQ7B5JWTTx3TWuPhpdda14eU9Nyd3kuIa/yYkF8Z3vsd2PXiC6j05zMane7gbjXvnS/WuehQORRXx4fYcwtM+agu55zpoLXlnHJzDxs+MrzlvjZYhQJkG65Vr+SRYk2jqvHndUzzsWKb4c+mTq6NQR/WCyeER/es0emQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; c=relaxed/simple; bh=CTwSi83UyUElkeC0d9YnXD36OkjwZYrnlRYHpExmrVo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uKWI5yxHSYhY06zknGBsum7xjKIc52NIksep18yfsG/lDk4zApBHDS2Z0IrVrhMjhYN7H4QBvHhZoZYhHgDS78KCnbUBszAH0S/Fx3cWVs+35/GSapM0bGK8Yl2t8GyeH88ZfkJrEQAGzMh4xg/YOIK4btLP3HBSwlI1DIPjcdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dN6Eza9Y; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mTKZ6gOJ; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dN6Eza9Y"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mTKZ6gOJ" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G3GrxsqYvKvy8k2dAEo8dPbUQENIK7ngV+AfA0g2P8g=; b=dN6Eza9YY6IMTtum6GsKhZGWXJBThpD42XrOna8P9CiIsgsSHnvhoHn0vNagO7TM4rm0nQ Vf63Z1lGRWFNfV1/RiW1IRYoga7E7oCKKyEe4i3fow3SC/0zLTtYZsYqlPSFNflDSt6Za1 BZT1PApYk703hiFc2P1/ma/TReI387bDL4ZqrRmyKcrYRxFASJUG/LxraKznsJuSoxbtmC ugeOtlJZ9zZthfGdAXxGkBmpCSaTyrDCkU4EH6zzjchRrO6QzrEGd3B5ryrd6K59GzKwRT 8qXvq+ds/IpxswEpIR98OlirhWW+TdvjJIQPorqY/8WTyArn1z8fNXsnG737xw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G3GrxsqYvKvy8k2dAEo8dPbUQENIK7ngV+AfA0g2P8g=; b=mTKZ6gOJuB2Tn1mHcUDVglWFIXp1XLNv3Uoi5ea6Wo9S7m1x0mjWLZlCdjYHVhIG49qhYz GT54ZsQ7AGydvrBw== Date: Wed, 23 Apr 2025 17:01:37 +0200 Subject: [PATCH 07/15] tools/nolibc: add support for access() and faccessat() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-7-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=2263; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=CTwSi83UyUElkeC0d9YnXD36OkjwZYrnlRYHpExmrVo=; b=UqGH1aGCfEzS6/oLJxkgMGaZH2L5tCyM0AvFdWoA450JYNOGve96Q0D1JfdCUsQrI4H+GKm0i vYksaYRzOfBAQRys26cAv7RH4CP9PPs4vihTZWSFkIRihT1sKo5uG+l X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/unistd.h | 28 ++++++++++++++++++++++++= ++++ tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 2 files changed, 30 insertions(+) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index ac7d53d986cd11ac84dd0a17e5a7055c779b2b10..a18e318399880dec12284ea86cb= 91cf46cfc4aaa 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -17,6 +17,34 @@ #define STDOUT_FILENO 1 #define STDERR_FILENO 2 =20 +#define F_OK 0 +#define X_OK 1 +#define W_OK 2 +#define R_OK 4 + +/* + * int access(const char *path, int amode); + * int faccessat(int fd, const char *path, int amode, int flag); + */ + +static __attribute__((unused)) +int sys_faccessat(int fd, const char *path, int amode, int flag) +{ + return my_syscall4(__NR_faccessat, fd, path, amode, flag); +} + +static __attribute__((unused)) +int faccessat(int fd, const char *path, int amode, int flag) +{ + return __sysret(sys_faccessat(fd, path, amode, flag)); +} + +static __attribute__((unused)) +int access(const char *path, int amode) +{ + return faccessat(AT_FDCWD, path, amode, 0); +} + =20 static __attribute__((unused)) int msleep(unsigned int msecs) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index b6724c314766b0cf34d7cae5cac28a9102ebe66e..7090e6b7c37e49afe751835bbff= 6a7af4f4fcf2a 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1109,6 +1109,8 @@ int run_syscall(int min, int max) * test numbers. */ switch (test + __LINE__ + 1) { + CASE_TEST(access); EXPECT_SYSZR(proc, access("/proc/self", R_= OK)); break; + CASE_TEST(access_bad); EXPECT_SYSER(proc, access("/proc/self", W_= OK), -1, EPERM); break; CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); br= eak; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6833F288CBD; Wed, 23 Apr 2025 15:01:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; cv=none; b=GfXasYF6fr7A6uLcwG9rH1seFNJCTYa/YVUm7EAqKv6D+pPpUNtl6pNTrIZTRSI3Hxudgt85huIgznMaiIr1F87W70ZZatXgPG7ImLw/raa+dqx2dmyuG/Y29hiJDVLnu4/4upwLZdFaLXzI3aMzSIAgjpMAKzArVLSX12PaF28= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; c=relaxed/simple; bh=iitfFCLJTE0y+kJ/Mc8P1qvaU4SXXc5zObH3qT9X+1I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E0r6Bd7I/YneJSBXmJtH2l6U2dkjhWUSJeYUE7Db35zw89ZHrpB4+Y4stRD0X+tfNb98n0O09iFjX/oM3xtV33xbM/YD/nR2PyeFEV/hv5lSscAliayhW4wM8OfyRwqCJAqqAvYn/d0a9I6C2dMtnWQDSrnDEcSSTi+FhNkFepE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=XaAEu4V9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZFI1Jg3f; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="XaAEu4V9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZFI1Jg3f" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420503; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IPrhwNYWJPbK9qX437sTXCnY+0U3HzpnaSqOnRkeTh0=; b=XaAEu4V9YZgTQu4a4CHSovZGUaQKhLGD28HWasyqMXdNQS2eqE5A/NyUYq5o9PGeDHiI55 KeR9/1UxqYaOgZTRvcdNDTuNNJ7T2HfF4Xzs/BDpOC9JbnBu1BRK0GM+vP8l2195eezkz4 V37y3iSCgDR6CgTq/mzwdiRHcB8QdNtsn7/xs6hsVqReE5gkS8ulIZ+5AfOsBRStypMTwP nEfVHPksruujrY1aKXBXLy1ve6c4zh6mYrxrDaH5imMKoLSEbLxRmqmVtxy4gofBPgHEKv 5M8QC68Ecq1NRmoBuZ70aV2BA6NqkskSHgbEEDvAWP1aKEGj7fzdPojVrUUfhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420503; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IPrhwNYWJPbK9qX437sTXCnY+0U3HzpnaSqOnRkeTh0=; b=ZFI1Jg3fXWbAI3jUE4sWDct3frbg/ea31yGvPpBJ5wZcCAlav/6faKTBQRaIQTp5+loN9e WPIZTIK28vCkhUAQ== Date: Wed, 23 Apr 2025 17:01:38 +0200 Subject: [PATCH 08/15] tools/nolibc: add clock_getres(), clock_gettime() and clock_settime() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-8-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=5397; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=iitfFCLJTE0y+kJ/Mc8P1qvaU4SXXc5zObH3qT9X+1I=; b=MAWotSyw/VzUjbGFFEAHc7Y1soQZuO763wr4at2EbYXMmbeYCZuK9HtD8AUSPElH/XWemgaV2 S1QL8fNnYr2C1/fgRTVNs1AmBTDD8kYiM18jixHWd90nsfhTuxcBay6 X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/time.h | 92 ++++++++++++++++++++++++= ++++ tools/include/nolibc/types.h | 2 + tools/testing/selftests/nolibc/nolibc-test.c | 5 ++ 3 files changed, 99 insertions(+) diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index 84655361b9ad2ce141f9af2759f2b128ae8a83a3..fb330628a59ad8a8b2b01c027b8= 61472888ddee4 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -12,6 +12,98 @@ #include "types.h" #include "sys.h" =20 +#include + +static __inline__ +void __nolibc_timespec_user_to_kernel(const struct timespec *ts, struct __= kernel_timespec *kts) +{ + kts->tv_sec =3D ts->tv_sec; + kts->tv_nsec =3D ts->tv_nsec; +} + +static __inline__ +void __nolibc_timespec_kernel_to_user(const struct __kernel_timespec *kts,= struct timespec *ts) +{ + ts->tv_sec =3D kts->tv_sec; + ts->tv_nsec =3D kts->tv_nsec; +} + +/* + * int clock_getres(clockid_t clockid, struct timespec *res); + * int clock_gettime(clockid_t clockid, struct timespec *tp); + * int clock_settime(clockid_t clockid, const struct timespec *tp); + */ + +static __attribute__((unused)) +int sys_clock_getres(clockid_t clockid, struct timespec *res) +{ +#if defined(__NR_clock_getres) + return my_syscall2(__NR_clock_getres, clockid, res); +#elif defined(__NR_clock_getres_time64) + struct __kernel_timespec kres; + int ret; + + ret =3D my_syscall2(__NR_clock_getres_time64, clockid, &kres); + if (res) + __nolibc_timespec_kernel_to_user(&kres, res); + return ret; +#else + return __nolibc_enosys(__func__, clockid, res); +#endif +} + +static __attribute__((unused)) +int clock_getres(clockid_t clockid, struct timespec *res) +{ + return __sysret(sys_clock_getres(clockid, res)); +} + +static __attribute__((unused)) +int sys_clock_gettime(clockid_t clockid, struct timespec *tp) +{ +#if defined(__NR_clock_gettime) + return my_syscall2(__NR_clock_gettime, clockid, tp); +#elif defined(__NR_clock_gettime64) + struct __kernel_timespec ktp; + int ret; + + ret =3D my_syscall2(__NR_clock_gettime64, clockid, &ktp); + if (tp) + __nolibc_timespec_kernel_to_user(&ktp, tp); + return ret; +#else + return __nolibc_enosys(__func__, clockid, tp); +#endif +} + +static __attribute__((unused)) +int clock_gettime(clockid_t clockid, struct timespec *tp) +{ + return __sysret(sys_clock_gettime(clockid, tp)); +} + +static __attribute__((unused)) +int sys_clock_settime(clockid_t clockid, struct timespec *tp) +{ +#if defined(__NR_clock_settime) + return my_syscall2(__NR_clock_settime, clockid, tp); +#elif defined(__NR_clock_settime64) + struct __kernel_timespec ktp; + + __nolibc_timespec_user_to_kernel(tp, &ktp); + return my_syscall2(__NR_clock_settime64, clockid, &ktp); +#else + return __nolibc_enosys(__func__, clockid, tp); +#endif +} + +static __attribute__((unused)) +int clock_settime(clockid_t clockid, struct timespec *tp) +{ + return __sysret(sys_clock_settime(clockid, tp)); +} + + static __attribute__((unused)) time_t time(time_t *tptr) { diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index 32d0929c633bbae29263375c461a0c82baf7f869..08c9fdd1c7991656aabca5dd4dd= 363da45e00dd8 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -198,6 +198,8 @@ struct stat { union { time_t st_ctime; struct timespec st_ctim; }; /* time of last stat= us change */ }; =20 +typedef __kernel_clockid_t clockid_t; + /* WARNING, it only deals with the 4096 first majors and 256 first minors = */ #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor)= & 0xff))) #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff)) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 7090e6b7c37e49afe751835bbff6a7af4f4fcf2a..7eb69971667a196cb0855d156e3= e18085851de7d 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -1078,6 +1079,7 @@ int run_syscall(int min, int max) { struct timeval tv; struct timezone tz; + struct timespec ts; struct stat stat_buf; int euid0; int proc; @@ -1111,6 +1113,9 @@ int run_syscall(int min, int max) switch (test + __LINE__ + 1) { CASE_TEST(access); EXPECT_SYSZR(proc, access("/proc/self", R_= OK)); break; CASE_TEST(access_bad); EXPECT_SYSER(proc, access("/proc/self", W_= OK), -1, EPERM); break; + CASE_TEST(clock_getres); EXPECT_SYSZR(1, clock_getres(CLOCK_MONOTON= IC, &ts)); break; + CASE_TEST(clock_gettime); EXPECT_SYSZR(1, clock_gettime(CLOCK_MONOTO= NIC, &ts)); break; + CASE_TEST(clock_settime); EXPECT_SYSER(1, clock_settime(CLOCK_MONOTO= NIC, &ts), -1, EINVAL); break; CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); br= eak; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD72C289359; Wed, 23 Apr 2025 15:01:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; cv=none; b=cCeIravX1Tato2Wbavgc8hXpfhKqGLaRattUEMROmw9dErmMeqixenQ6JgJ0OFmp8Dn/rloMsi6oXbpWwp8ST6SEkat8uKS71E9k5BVLPKNL7B3hCPNjxcpVhdCEfrqdtrLrT3iXLIfg3Dn23b5OYTPHzpFyb4TmCXDROLqcqv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420507; c=relaxed/simple; bh=AYXbn4NX9XeIVkNZl12qqF/W5pwE5oN1B2T0jDTP9Hw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oMaa7dHalAAgdtRAXc4xwvlXyi12CopoFg8totRBaCfnvXMiOu3yXJNlr0AAv5DTa3sXO0CwaHtf/IOEsv6QP+wgDrEXb8buzwV7fqgPPyocdg6eKQ3SujDOOdw52L1ulL9fPuEkAUtdCuC4mHGOoCiPl4MUhp7wd231L4TdEZ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=oaTU8fkf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=J+J0MAUb; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oaTU8fkf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="J+J0MAUb" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420504; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eQbT0qvKJrdNXKvpl0h1tlyZlalWotnaZqnnxZBEhvw=; b=oaTU8fkfdKvQeCRwt4/T3DajDImT697wENohpSawCYAuNOOA5OCaxmHA0thRRA0pyBFZWR ueTrPPesd5c1wrB4pumFfkc3sgaqV5HLdKk1sSpylm+SlEQ8/jdyxF9hR8jBymJEv/XVWx hpMp72rspqbXZc1oSDJa7iJaFObz/ZncjLbKzHvDXPfScM5K9Oeke/oZkVfq93dtKataNr WtfWq4Exi1809BMJZ3N7p/+/F51yuCfkXxRKfZCGvra2hOMhcOM+Es7CC9LNKjsOsgKlVE 6+AudiH+IwoCXn825Xx6xjYjN8rqAY1E0X4ouFaqvZAlBfOcCnqJjCREe5+UYw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420504; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eQbT0qvKJrdNXKvpl0h1tlyZlalWotnaZqnnxZBEhvw=; b=J+J0MAUbRh3We3LoQxXrAxXM3chzxK/U9ueSNtFqSVglRkdKnLx9AHyb2zHzkf5U8KhvEj htPPNpkHPBiLmLCg== Date: Wed, 23 Apr 2025 17:01:39 +0200 Subject: [PATCH 09/15] tools/nolibc: add timer functions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-9-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=6398; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=AYXbn4NX9XeIVkNZl12qqF/W5pwE5oN1B2T0jDTP9Hw=; b=PWbTs4g65BX5Wbuv4a9vLyGN51Op/RIxrC3fQ+K9BjoGnpHjaHZmue/VpAaxGWCDFQkzPc0Bc ls1fvkcZv90CFVsPLZCt9VDAplCsnYjvqpv3CUg+s8KCM6nrxdGNnd0 X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/time.h | 86 ++++++++++++++++++++++++= ++++ tools/include/nolibc/types.h | 1 + tools/testing/selftests/nolibc/nolibc-test.c | 51 +++++++++++++++++ 3 files changed, 138 insertions(+) diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index fb330628a59ad8a8b2b01c027b861472888ddee4..28a1549adb14e2087fa8fbdb7e9= c35e1c3f22c2a 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -12,6 +12,7 @@ #include "types.h" #include "sys.h" =20 +#include #include =20 static __inline__ @@ -117,6 +118,91 @@ time_t time(time_t *tptr) return tv.tv_sec; } =20 + +/* + * int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *time= rid); + * int timer_gettime(timer_t timerid, struct itimerspec *curr_value); + * int timer_settime(timer_t timerid, int flags, const struct itimerspec *= new_value, struct itimerspec *old_value); + */ + +static __attribute__((unused)) +int sys_timer_create(clockid_t clockid, struct sigevent *evp, timer_t *tim= erid) +{ + return my_syscall3(__NR_timer_create, clockid, evp, timerid); +} + +static __attribute__((unused)) +int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid) +{ + return __sysret(sys_timer_create(clockid, evp, timerid)); +} + +static __attribute__((unused)) +int sys_timer_delete(timer_t timerid) +{ + return my_syscall1(__NR_timer_delete, timerid); +} + +static __attribute__((unused)) +int timer_delete(timer_t timerid) +{ + return __sysret(sys_timer_delete(timerid)); +} + +static __attribute__((unused)) +int sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value) +{ +#if defined(__NR_timer_gettime) + return my_syscall2(__NR_timer_gettime, timerid, curr_value); +#elif defined(__NR_timer_gettime64) + struct __kernel_itimerspec kcurr_value; + int ret; + + ret =3D my_syscall2(__NR_timer_gettime64, timerid, &kcurr_value); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_interval, &curr_value->i= t_interval); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_value, &curr_value->it_v= alue); + return ret; +#else + return __nolibc_enosys(__func__, timerid, curr_value); +#endif +} + +static __attribute__((unused)) +int timer_gettime(timer_t timerid, struct itimerspec *curr_value) +{ + return __sysret(sys_timer_gettime(timerid, curr_value)); +} + +static __attribute__((unused)) +int sys_timer_settime(timer_t timerid, int flags, + const struct itimerspec *new_value, struct itimerspec *old_value) +{ +#if defined(__NR_timer_settime) + return my_syscall4(__NR_timer_settime, timerid, flags, new_value, old_val= ue); +#elif defined(__NR_timer_settime64) + struct __kernel_itimerspec knew_value, kold_value; + int ret; + + __nolibc_timespec_user_to_kernel(&new_value->it_value, &knew_value.it_val= ue); + __nolibc_timespec_user_to_kernel(&new_value->it_interval, &knew_value.it_= interval); + ret =3D my_syscall4(__NR_timer_settime64, timerid, flags, &knew_value, &k= old_value); + if (old_value) { + __nolibc_timespec_kernel_to_user(&kold_value.it_interval, &old_value->it= _interval); + __nolibc_timespec_kernel_to_user(&kold_value.it_value, &old_value->it_va= lue); + } + return ret; +#else + return __nolibc_enosys(__func__, timerid, flags, new_value, old_value); +#endif +} + +static __attribute__((unused)) +int timer_settime(timer_t timerid, int flags, + const struct itimerspec *new_value, struct itimerspec *old_value) +{ + return __sysret(sys_timer_settime(timerid, flags, new_value, old_value)); +} + /* make sure to include all global symbols */ #include "nolibc.h" =20 diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index 08c9fdd1c7991656aabca5dd4dd363da45e00dd8..3528782670bca23fc9eb0323eb5= af3100a037855 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -199,6 +199,7 @@ struct stat { }; =20 typedef __kernel_clockid_t clockid_t; +typedef int timer_t; =20 /* WARNING, it only deals with the 4096 first majors and 256 first minors = */ #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor)= & 0xff))) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 7eb69971667a196cb0855d156e3e18085851de7d..6af55a04e3c6bcea4e09c941778= ffd1f3b186690 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -902,6 +902,56 @@ int test_stat_timestamps(void) return 0; } =20 +int test_timer(void) +{ + struct itimerspec timerspec; + struct sigevent evp; + timer_t timer; + int ret; + + evp.sigev_notify =3D SIGEV_NONE; + + ret =3D timer_create(CLOCK_MONOTONIC, &evp, &timer); + if (ret) + return ret; + + timerspec =3D (struct itimerspec) { + .it_value.tv_sec =3D 1000000, + }; + ret =3D timer_settime(timer, 0, &timerspec, NULL); + if (ret) + goto err; + + timerspec =3D (struct itimerspec) { + .it_value.tv_sec =3D -1, + .it_value.tv_nsec =3D -1, + .it_interval.tv_sec =3D -1, + .it_interval.tv_nsec =3D -1, + }; + ret =3D timer_gettime(timer, &timerspec); + if (ret) + goto err; + + errno =3D EINVAL; + ret =3D -1; + + if (timerspec.it_interval.tv_sec || timerspec.it_interval.tv_nsec) + goto err; + + if (timerspec.it_value.tv_sec > 1000000) + goto err; + + ret =3D timer_delete(timer); + if (ret) + return ret; + + return 0; + +err: + timer_delete(timer); + return ret; +} + int test_uname(void) { struct utsname buf; @@ -1183,6 +1233,7 @@ int run_syscall(int min, int max) CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1,= EFAULT); break; CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); b= reak; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEX= IST); break; + CASE_TEST(timer); EXPECT_SYSZR(1, test_timer()); break; CASE_TEST(uname); EXPECT_SYSZR(proc, test_uname()); break; CASE_TEST(uname_fault); EXPECT_SYSER(1, uname(NULL), -1, EFAULT); = break; CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); = break; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C996289368; Wed, 23 Apr 2025 15:01:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420508; cv=none; b=UqQC/Ko7wjk6GaemT7sDwSyJj1M9nbKGkDRH0jnVLF+xAfEZxJBmRoepYWZKK9Dfl+Gpra8zcnSiY+jnXl+qGHeGPopIG4owVxeCtnp/1C5QyCn07ElojxqxU8K7timzys8d8bwnENV2Rkpwj9r1LxtB3imnP8islayZxsY6J8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420508; c=relaxed/simple; bh=mL7BMOt2Dm2hWpLZpunY/sGwlAtb+TWJVi/FfVODcRY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gQMhZmNkGUY3tBBlqWMXPiHEMJIliZ0TWLTqYBW85eshkTU2NbVDmfAA+Up7hHS4KmfwXDtHBK6ttvaxq9rczC8zP8Vin0nJQtMHOQJN7lvheAN+4Sq1WIJshl0mPRPIndo0QzWDhTD4sTzyHO78XLOcN5v1hA0znikPvpX/0rU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kFsIgXmA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ySb6Q6SL; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kFsIgXmA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ySb6Q6SL" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420504; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+h5AdN/9Qv0rdqSTBWh0yx7GylopoI7RA2AVXxDG3K0=; b=kFsIgXmADoiLQb5rfePzJtQAw4pHN6yTbzn+UOSqjOqmy70yDCA7FxoAVrVaLI6uZnua9t rlo1sQvfHxzIqD6X6s4Fsj7Ehn6tduf4XEybBYN+MxZJvd9dhYUoZ/UmjB4H4/gVrRzdIA 7E35c6l9PB1T6PRruUnT3hl7WdmqmDgVDV6CWWFSkjIrUZ+koqQF1g162TsjPKNgzOtNmc jpjdCmNjQSHlBhtvCBO9+cXtKNHNX62Yb4EwadUHb2sapTSwM3t9eDH4HwGizvJaSoM1BJ a/rqdg2taI9JIBBCz/5rXnMR/dx2mkTYHpWnl9M4ds6iz3JPz7YU3SZbpSMn8g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420504; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+h5AdN/9Qv0rdqSTBWh0yx7GylopoI7RA2AVXxDG3K0=; b=ySb6Q6SLvwn9crlZw+Sk/y4PRZBpAe+ymiBildeQ91hLDDGkg3XUWu3p0ZGqDVmZkgSao1 GSPp8mgYIl9MdWBg== Date: Wed, 23 Apr 2025 17:01:40 +0200 Subject: [PATCH 10/15] tools/nolibc: add timerfd functionality Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-10-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=6373; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=mL7BMOt2Dm2hWpLZpunY/sGwlAtb+TWJVi/FfVODcRY=; b=7ksIL9Rwv+gVsw6d3gqEua/DlLtOrJHnFw6ATfnUWo8nlzHbLYOFaZ940WncZkH1BVWpC5zdu v3lwPArQTAVDxXWXwk0feZMRRpvCp9eu7E18LeUDa0M+Rzh3SH1dBaG X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/sys/timerfd.h | 87 ++++++++++++++++++++++++= ++++ tools/testing/selftests/nolibc/nolibc-test.c | 48 +++++++++++++++ 4 files changed, 137 insertions(+) diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index 3a82e036a2c1762da5c3916ff9f4e158fb6106ff..e3aeb247c74e88af5d769efe649= 65c5bcbb40611 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -52,6 +52,7 @@ all_files :=3D \ sys/stat.h \ sys/syscall.h \ sys/time.h \ + sys/timerfd.h \ sys/types.h \ sys/wait.h \ time.h \ diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 2207f3d3c5cf92b96150149050f0bde0ed7e7147..13628261b9cc92d545745acc9eb= ef541b185f2bd 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -102,6 +102,7 @@ #include "sys/stat.h" #include "sys/syscall.h" #include "sys/time.h" +#include "sys/timerfd.h" #include "sys/wait.h" #include "ctype.h" #include "elf.h" diff --git a/tools/include/nolibc/sys/timerfd.h b/tools/include/nolibc/sys/= timerfd.h new file mode 100644 index 0000000000000000000000000000000000000000..2d61fc76fe9a9ba7571f52ff157= a8a4807d7d9c9 --- /dev/null +++ b/tools/include/nolibc/sys/timerfd.h @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * timerfd definitions for NOLIBC + * Copyright (C) 2025 Thomas Wei=C3=9Fschuh + */ + +#ifndef _NOLIBC_SYS_TIMERFD_H +#define _NOLIBC_SYS_TIMERFD_H + +#include "../sys.h" +#include "../time.h" + +#include + + +static __attribute__((unused)) +int sys_timerfd_create(int clockid, int flags) +{ + return my_syscall2(__NR_timerfd_create, clockid, flags); +} + +static __attribute__((unused)) +int timerfd_create(int clockid, int flags) +{ + return __sysret(sys_timerfd_create(clockid, flags)); +} + + +static __attribute__((unused)) +int sys_timerfd_gettime(int fd, struct itimerspec *curr_value) +{ +#if defined(__NR_timerfd_gettime) + return my_syscall2(__NR_timerfd_gettime, fd, curr_value); +#elif defined(__NR_timerfd_gettime64) + struct __kernel_itimerspec kcurr_value; + int ret; + + ret =3D my_syscall2(__NR_timerfd_gettime64, fd, &kcurr_value); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_interval, &curr_value->i= t_interval); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_value, &curr_value->it_v= alue); + return ret; +#else + return __nolibc_enosys(__func__, fd, curr_value); +#endif +} + +static __attribute__((unused)) +int timerfd_gettime(int fd, struct itimerspec *curr_value) +{ + return __sysret(sys_timerfd_gettime(fd, curr_value)); +} + + +static __attribute__((unused)) +int sys_timerfd_settime(int fd, int flags, + const struct itimerspec *new_value, struct itimerspec *old_value) +{ +#if defined(__NR_timerfd_settime) + return my_syscall4(__NR_timerfd_settime, fd, flags, new_value, old_value); +#elif defined(__NR_timerfd_settime64) + struct __kernel_itimerspec knew_value, kold_value; + int ret; + + __nolibc_timespec_user_to_kernel(&new_value->it_value, &knew_value.it_val= ue); + __nolibc_timespec_user_to_kernel(&new_value->it_interval, &knew_value.it_= interval); + ret =3D my_syscall4(__NR_timerfd_settime64, fd, flags, &knew_value, &kold= _value); + if (old_value) { + __nolibc_timespec_kernel_to_user(&kold_value.it_interval, &old_value->it= _interval); + __nolibc_timespec_kernel_to_user(&kold_value.it_value, &old_value->it_va= lue); + } + return ret; +#else + return __nolibc_enosys(__func__, fd, flags, new_value, old_value); +#endif +} + +static __attribute__((unused)) +int timerfd_settime(int fd, int flags, + const struct itimerspec *new_value, struct itimerspec *old_value) +{ + return __sysret(sys_timerfd_settime(fd, flags, new_value, old_value)); +} + +/* make sure to include all global symbols */ +#include "../nolibc.h" + +#endif /* _NOLIBC_SYS_TIMERFD_H */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 6af55a04e3c6bcea4e09c941778ffd1f3b186690..aed71de4b4f3dd1f183c7fc25e5= a5cee466600ed 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -952,6 +953,52 @@ int test_timer(void) return ret; } =20 +int test_timerfd(void) +{ + struct itimerspec timerspec; + int timer, ret; + + timer =3D timerfd_create(CLOCK_MONOTONIC, 0); + if (timer =3D=3D -1) + return -1; + + timerspec =3D (struct itimerspec) { + .it_value.tv_sec =3D 1000000, + }; + ret =3D timerfd_settime(timer, 0, &timerspec, NULL); + if (ret) + goto err; + + timerspec =3D (struct itimerspec) { + .it_value.tv_sec =3D -1, + .it_value.tv_nsec =3D -1, + .it_interval.tv_sec =3D -1, + .it_interval.tv_nsec =3D -1, + }; + ret =3D timerfd_gettime(timer, &timerspec); + if (ret) + goto err; + + errno =3D EINVAL; + ret =3D -1; + + if (timerspec.it_interval.tv_sec || timerspec.it_interval.tv_nsec) + goto err; + + if (timerspec.it_value.tv_sec > 1000000) + goto err; + + ret =3D close(timer); + if (ret) + return ret; + + return 0; + +err: + close(timer); + return ret; +} + int test_uname(void) { struct utsname buf; @@ -1234,6 +1281,7 @@ int run_syscall(int min, int max) CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); b= reak; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEX= IST); break; CASE_TEST(timer); EXPECT_SYSZR(1, test_timer()); break; + CASE_TEST(timerfd); EXPECT_SYSZR(1, test_timerfd()); break; CASE_TEST(uname); EXPECT_SYSZR(proc, test_uname()); break; CASE_TEST(uname_fault); EXPECT_SYSER(1, uname(NULL), -1, EFAULT); = break; CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); = break; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE7DA28469B; Wed, 23 Apr 2025 15:01:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420508; cv=none; b=lYw49MPbIwiNhfynNMM23cfBpQuGbhb1z2KZp6xpDbiLTURIOhq+bnBi3B+GPdtXBXvpHjGLY/gPSMtbXPavaURna7NqkaYtk8yNASKcfCRU01izyaOkkaeZqhTlJQnb1W4LS77S9yOwl+rywa3XJnc1wqtNUcNMPc8pNzA0SvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420508; c=relaxed/simple; bh=vxoT92of/rlazXwaV9YXlg1WYXN/+U03gMMmCD+41y8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=C64vwQ1TIk8v+MOYqH2qUjacqrwa8uTthZSTDZjbMu4QjClqcawh/46WD06oOg4j1xmuNG/LeNgm94Eh6L1pjt962Zxa42F6UWkgc3sI0W3qArFKyTDMsFqu+AvbuKlGEWvPaYy1Y8ovtlDHzGajwsXdZOharLzGJpz5Nd4JHm0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=f9VTdJx9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=aPiHloiK; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="f9VTdJx9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="aPiHloiK" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZKJi5obWY5ETMlVCxwTzt0WlZX9v+939EY9af8zla/8=; b=f9VTdJx9wewfT1biawm7CSSNtiIXwWVlKbF3na10U54NbCBV+u8kI+Rmny2iwA3Kdl0uWP 5cMfIzxUx9jNkUFOAUFcWP9tEXGjeW8qfaUm0BdsHXpKhLx0W4Aju4VmvsnSf/cDShdUU+ fYwnGzNQAiyppNkuuaLjOJG3JL2cIRPNchBeoVlF6vIjtmwdkyKna+7iUJc4XXi3wkGduL IG5t+RP6IqqJHoQgmz+v4gFhBI+0xM9HKkwLY8z9s12hfQdBN08adHOHKY1hCQCPZQNiB1 RsqPoCeh+KX2aedqbsohmqy4XNzjKeGCTX+skNF4XxE4cODU97XqmCAWm9lv4A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZKJi5obWY5ETMlVCxwTzt0WlZX9v+939EY9af8zla/8=; b=aPiHloiKog+oNTX5cL0sR44zKc7UgtstArQr8TJgYBhfAgzVbU0lpmvwn9y83TfuIBc/MF x0rABOUTWaNxp/CA== Date: Wed, 23 Apr 2025 17:01:41 +0200 Subject: [PATCH 11/15] tools/nolibc: add difftime() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-11-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=1567; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=vxoT92of/rlazXwaV9YXlg1WYXN/+U03gMMmCD+41y8=; b=0JCWS5hFZRx4Av49TwqS3Jo6OqH6tDeA4kYh2G+y46p1j2JB3a1TNbKQgh20tVRfYF+yLAVV8 S/S/CDpfMx2Da3xrBIQq+JQ7ZILMdWXGysklEkDP2wl7IY1eQdIV9lC X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/time.h | 7 +++++++ tools/testing/selftests/nolibc/nolibc-test.c | 1 + 2 files changed, 8 insertions(+) diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index 28a1549adb14e2087fa8fbdb7e9c35e1c3f22c2a..760133c574ece97165e3bba5616= a387deaf07aff 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -105,6 +105,13 @@ int clock_settime(clockid_t clockid, struct timespec *= tp) } =20 =20 +static __inline__ +double difftime(time_t time1, time_t time2) +{ + return time1 - time2; +} + + static __attribute__((unused)) time_t time(time_t *tptr) { diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index aed71de4b4f3dd1f183c7fc25e5a5cee466600ed..fd8bab42e75157967658690005b= c9142360fc135 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1423,6 +1423,7 @@ int run_stdlib(int min, int max) CASE_TEST(toupper_noop); EXPECT_EQ(1, toupper('A'), 'A'); bre= ak; CASE_TEST(abs); EXPECT_EQ(1, abs(-10), 10); break; CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10); break; + CASE_TEST(difftime); EXPECT_EQ(1, difftime(200., 100.), 1= 00.); break; =20 case __LINE__: return ret; /* must be last */ --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 117BB28A1FB; Wed, 23 Apr 2025 15:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420509; cv=none; b=OG5GA6SIT+N1LzN2LnqHXmY/np/ckIAqwc7OHzfpyywSuFUWDgeICYcb1DaVHamtpZ/q6hVQDln+SiRlhNogidYFSHA/npvaMh+cvZe08nPzCwBOuZZ8FdF3yaGySX25eJqxZkxLyjetJRtiOHV+CkRr1kwqhl89/p/mxTRza3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420509; c=relaxed/simple; bh=FNILqJREiWU391TWm5ngVTlKSf5kcHUg4Mk9H+r2gLg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=a1SpXXG6bBQUVtRcyFVELV3AHcaqCZTf3NoFTSWq+M6eHTcQwyYZ3Y8q2b9aRms9NpjswluVNug7xqDPsUp5eBoRKKmjkvRBRk6S4taCHWDkfOZ2T3hSFHdREhquDomQA8edpJWZVB4x5s/GDYuifkKlscAe3tb7dC+Ih2aL5hs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3rxtct5T; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Nup0TkJh; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3rxtct5T"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Nup0TkJh" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qj5rd4/9gTpDNF7r8PvlINanGlwadZfZJ4R7QvPABYE=; b=3rxtct5TcgZB3Z3xfm4luWIOQkis65uATg8LF+oVatrVaS8HNxkTkiq3fppQE/6C4Nv5vu Oa7z3Wz1SDwcZ5DsumWEaNFszPr84dv9wT4pH0A+wi8q71M8V7aNMmm9Bzd+lvjQxnaWbp Z3WdbErfLSwO0S0vtwkjx+cFWH7IbZm6ojuaWDoumDT6kTELi2h2+DSzPZxr5he9Dv0iyZ /6iLaq3Ei7AguP4LI7RTsFqC9ND9ZopRx5YydeuYJzQSrfq1ROZ/8aY2P69XOT6FfDxh4e 0W0ym59Tiqu+ZsY4VILbSxlYNsiAqXur3jN+WYpjnp8dNzdBRZslqGVQIS/YHA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qj5rd4/9gTpDNF7r8PvlINanGlwadZfZJ4R7QvPABYE=; b=Nup0TkJhnvbzuvNBnPJiFNsc3Qn1vWABvjKlKxZHEtDrh66nT9zMdYQsMWvq8zHRsRmo9K 49UAFhXASzUrF5BQ== Date: Wed, 23 Apr 2025 17:01:42 +0200 Subject: [PATCH 12/15] tools/nolibc: add namespace functionality Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-12-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=5288; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=FNILqJREiWU391TWm5ngVTlKSf5kcHUg4Mk9H+r2gLg=; b=oQgZiX49EZdYEU3Mi3ktQ5Sso6l6EV1vv5HVvPYVEyUbPpAP3W7KH9yncurUfZYe1l+duxEC2 5oggR6ywSiHBOm4bORvGfRa8nrVgUazF+u9lSzPqeRIkPoAmJ/b1Qt6 X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/sched.h | 50 +++++++++++++++++++++ tools/testing/selftests/nolibc/Makefile | 2 + tools/testing/selftests/nolibc/nolibc-test.c | 67 ++++++++++++++++++++++++= ++++ 5 files changed, 121 insertions(+) diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index e3aeb247c74e88af5d769efe64965c5bcbb40611..5fed167f26aa3475eebc7a56bd9= 7820932e97ff9 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -37,6 +37,7 @@ all_files :=3D \ limits.h \ math.h \ nolibc.h \ + sched.h \ signal.h \ stackprotector.h \ std.h \ diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 13628261b9cc92d545745acc9ebef541b185f2bd..d6722e22f441956e70340dcab00= 4aa73a3125691 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -106,6 +106,7 @@ #include "sys/wait.h" #include "ctype.h" #include "elf.h" +#include "sched.h" #include "signal.h" #include "unistd.h" #include "stdio.h" diff --git a/tools/include/nolibc/sched.h b/tools/include/nolibc/sched.h new file mode 100644 index 0000000000000000000000000000000000000000..19108d33c5c6063d41916bbf46e= f1f7b1b49595c --- /dev/null +++ b/tools/include/nolibc/sched.h @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * sched function definitions for NOLIBC + * Copyright (C) 2025 Thomas Wei=C3=9Fschuh + */ + +#ifndef _NOLIBC_SCHED_H +#define _NOLIBC_SCHED_H + +#include "sys.h" + +#include + +/* + * int setns(int fd, int nstype); + */ + +static __attribute__((unused)) +int sys_setns(int fd, int nstype) +{ + return my_syscall2(__NR_setns, fd, nstype); +} + +static __attribute__((unused)) +int setns(int fd, int nstype) +{ + return __sysret(sys_setns(fd, nstype)); +} + + +/* + * int unshare(int flags); + */ + +static __attribute__((unused)) +int sys_unshare(int flags) +{ + return my_syscall1(__NR_unshare, flags); +} + +static __attribute__((unused)) +int unshare(int flags) +{ + return __sysret(sys_unshare(flags)); +} + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#endif /* _NOLIBC_SCHED_H */ diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selfte= sts/nolibc/Makefile index 94f3e8be7a68f63ecd639c4f283b3cd10764ce74..37526891af8de338d1d55315d7d= 6a7179e695cd0 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -106,6 +106,8 @@ DEFCONFIG_sparc64 =3D sparc64_defconfig DEFCONFIG =3D $(DEFCONFIG_$(XARCH)) =20 EXTRACONFIG =3D $(EXTRACONFIG_$(XARCH)) +EXTRACONFIG_arm =3D -e CONFIG_NAMESPACES +EXTRACONFIG_armthumb =3D -e CONFIG_NAMESPACES =20 # optional tests to run (default =3D all) TEST =3D diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index fd8bab42e75157967658690005bc9142360fc135..ab9c3bcffd9750981d68c6d1624= 5d285ce0657c8 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1169,6 +1169,72 @@ int test_openat(void) return 0; } =20 +int test_namespace(void) +{ + int original_ns, new_ns, ret; + ino_t original_ns_ino; + struct stat stat_buf; + + original_ns =3D open("/proc/self/ns/uts", O_RDONLY); + if (original_ns =3D=3D -1) + return -1; + + ret =3D fstat(original_ns, &stat_buf); + if (ret) + goto out; + + original_ns_ino =3D stat_buf.st_ino; + + ret =3D unshare(CLONE_NEWUTS); + if (ret) + goto out; + + new_ns =3D open("/proc/self/ns/uts", O_RDONLY); + if (new_ns =3D=3D -1) { + ret =3D new_ns; + goto out; + } + + ret =3D fstat(new_ns, &stat_buf); + close(new_ns); + if (ret) + goto out; + + if (stat_buf.st_ino =3D=3D original_ns_ino) { + errno =3D EINVAL; + ret =3D -1; + goto out; + } + + ret =3D setns(original_ns, CLONE_NEWUTS); + if (ret) + goto out; + + new_ns =3D open("/proc/self/ns/uts", O_RDONLY); + if (new_ns =3D=3D -1) { + ret =3D new_ns; + goto out; + } + + ret =3D fstat(new_ns, &stat_buf); + if (ret) + goto out; + + close(new_ns); + + if (stat_buf.st_ino !=3D original_ns_ino) { + errno =3D EINVAL; + ret =3D -1; + goto out; + } + + ret =3D 0; + +out: + close(original_ns); + return ret; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -1293,6 +1359,7 @@ int run_syscall(int min, int max) CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break; CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getp= id()); break; CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NUL= L, 0, 0, NULL), -1, EFAULT); break; + CASE_TEST(namespace); EXPECT_SYSZR(euid0 && proc, test_namespace= ()); break; case __LINE__: return ret; /* must be last */ /* note: do not set any defaults so as to permit holes above */ --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE9EC28134C; Wed, 23 Apr 2025 15:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420509; cv=none; b=Zj0IKO0GeSIgIFkV6EZuN5MZmLhUKiwDeNZi5+KfGRswzl41ZjeCjH0VpH/8VQ/oHmda3uE3tl990zeGsVQt7DYhfyHMdxnecv1+wezukYmOGeSdCTVhCT8lUB0ljII/4Nm6YlLebIKGKie0MWSQAjFuCOZIyAVtomC4ksQ9mvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420509; c=relaxed/simple; bh=iyOqOWRt84OTzJZitLtaFwLAoRvMX+MnJAIWfcOuDmU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=m3gS/Vs/RugxKoDvFFg+CFFUYhZsKfeCerZhzA3d1EYKbf1WH9LQ0wnBzPQDjuzV/t02L5loN9+SaLX0cJfi2WGm4MDCTa2NsQU4Ubsx5MG1YSiEdhGFztpijxVguE0AaKPp5xZZWNsyvFy6XQJ49Grkji0AHiRPtu5h/W7zK0I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=j5e0cxjM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=eAp/itsm; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="j5e0cxjM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="eAp/itsm" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420506; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cLR/z7YIqr5I7FIwQeuOg1fHdrCyaNJ8I9tSX8q3nFc=; b=j5e0cxjM81SuyAJbPdhF4ypyPUlWYpteIDjTuFYMFW/zVbyQHj+pt7qKlRLLl4dhaVrpvz 6NWlRR3wblMhKQEXKb2ykdjBXPMf1K6G0KuRFYd3w5pwyohPVnSvZ5H18NwP0vxqFT59uH eB0s7+hCcsPvVPGpbC2SmWGsJC2HtRcQ/pbBVQ4OJUJNLH6p3g0DF3x+Bj+F1albte8fem iu8wZEeMNga1J67ePjvxlXs/m20toWKxItUblKvt17wy1nmaiwWtkET/kHB/jPqCdUf4zR sIWU5sDqEnf51eruQwC0orGqH9X7xIxDetXDDfsW/bi++s/tolm9VU0gAn5Qqg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420506; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cLR/z7YIqr5I7FIwQeuOg1fHdrCyaNJ8I9tSX8q3nFc=; b=eAp/itsmXsv97IACjC0wVeBsSSHpld1NMqf4RovJ9/mUxHfAqeU0pcdz2JDKvwmPM1/Nun tyJKLU9qsugLXzAg== Date: Wed, 23 Apr 2025 17:01:43 +0200 Subject: [PATCH 13/15] tools/nolibc: add fopen() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-13-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=3216; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=iyOqOWRt84OTzJZitLtaFwLAoRvMX+MnJAIWfcOuDmU=; b=gnt6p0DkYCDpq0sC9fE9l3NG/heviYXE4qQmSHd8tYSkecwhF1AxRXCadYkIujBsDc0Kx9YEw c7tVhxgPeiQAUBaWXLpAg0nCkh++UP3ahKEYujCl7/3ytmkQDjKz7kC X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= This is used in various selftests and will be handy when integrating those with nolibc. Only the standard POSIX modes are supported. No extensions nor the (noop) "b" from ISO C are accepted. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/stdio.h | 27 ++++++++++++++++++++++++= +++ tools/testing/selftests/nolibc/nolibc-test.c | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 02426682c34bcd82d9556036c6c3e73a6a5a3b4d..86a1132abcd4e5e5b0f821cfe97= 4aca8a6d4966f 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -10,6 +10,7 @@ #include "std.h" #include "arch.h" #include "errno.h" +#include "fcntl.h" #include "types.h" #include "sys.h" #include "stdarg.h" @@ -52,6 +53,32 @@ FILE *fdopen(int fd, const char *mode __attribute__((unu= sed))) return (FILE*)(intptr_t)~fd; } =20 +static __attribute__((unused)) +FILE *fopen(const char *pathname, const char *mode) +{ + int flags, fd; + + if (!strcmp(mode, "r")) + flags =3D O_RDONLY; + else if (!strcmp(mode, "w")) + flags =3D O_WRONLY | O_CREAT | O_TRUNC; + else if (!strcmp(mode, "a")) + flags =3D O_WRONLY | O_CREAT | O_APPEND; + else if (!strcmp(mode, "r+")) + flags =3D O_RDWR; + else if (!strcmp(mode, "w+")) + flags =3D O_RDWR | O_CREAT | O_TRUNC; + else if (!strcmp(mode, "a+")) + flags =3D O_RDWR | O_CREAT | O_APPEND; + else { + SET_ERRNO(EINVAL); + return NULL; + } + + fd =3D open(pathname, flags, 0666); + return fdopen(fd, mode); +} + /* provides the fd of stream. */ static __attribute__((unused)) int fileno(FILE *stream) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index ab9c3bcffd9750981d68c6d16245d285ce0657c8..f576edc3725b42ce086d6d83d98= 9f38d3fab6eb8 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -856,6 +856,29 @@ int test_getpagesize(void) return !c; } =20 +int test_file_stream(void) +{ + FILE *f; + int r; + + f =3D fopen("/dev/null", "r"); + if (!f) + return -1; + + errno =3D 0; + r =3D fwrite("foo", 1, 3, f); + if (r !=3D 0 || errno !=3D EBADF) { + fclose(f); + return -1; + } + + r =3D fclose(f); + if (r =3D=3D EOF) + return -1; + + return 0; +} + int test_fork(void) { int status; @@ -1308,6 +1331,7 @@ int run_syscall(int min, int max) CASE_TEST(dup3_0); tmp =3D dup3(0, 100, 0); EXPECT_SYSNE(1, = tmp, -1); close(tmp); break; CASE_TEST(dup3_m1); tmp =3D dup3(-1, 100, 0); EXPECT_SYSER(1, = tmp, -1, EBADF); if (tmp !=3D -1) close(tmp); break; CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0]= =3D "/", [1] =3D NULL }, NULL), -1, EACCES); break; + CASE_TEST(file_stream); EXPECT_SYSZR(1, test_file_stream()); break; CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break; CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1);= break; CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null= "), -1, ENOTDIR); break; --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 704D128B50B; Wed, 23 Apr 2025 15:01:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420510; cv=none; b=SNLx/p8Lot52tu2aDUOEGL3JV+ogMlx2faqNXfTxAxEey2fAQqavUnDtkEGyW5Qan0p6q/Ex+bpR27BQKNRRuvJzZUDFpvxmstdid/gZCk06QVZitwxH68UBDBWvYr00vhvIWxmB3Pa3I6GYdhj2edj1WSx2OX0D9pey/24bDlE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420510; c=relaxed/simple; bh=9H7RvcZBbPXMLyb8mShjwm8voZBm+v2g1s9nr5SlohY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VNudxdocjTXmNUqbKK4VeVA1IRu/NKaTBFvxM/DaHANQEJKjqsNR/LTqTRI9Z18YtDA4GWB6zdF9glYHHQpOqoJLtSnhE4mczY7n56zjAvC+IE9pU6I/c4/2YwukzxStmXss7RUTYyIG2gQMm0S1lwWPjFzJFRIim7u4IUI0YrE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LACa2Hac; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uh0cm7Yr; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LACa2Hac"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uh0cm7Yr" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420506; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8LJq/1nYP1KELL7mieiK61zxyQg8oMgPrLAC0XbiP+U=; b=LACa2HacTg7b77RB4Hjmw+H0sYfMhOg8++SGo5kl66M3s36tmRaIoc1oyG85P5xlBEqDb8 zS1yCCLb+Oi/BvnhnTKsB7xPOlGmP0k62u0TjbwuHeiJ+EP9CDrRT2z6t3h1DB32v9nJNv xnIKmygAmH0cRubyPwi1uKq4hmxa1WpCDASEzuINCepmFl9sG42x5IriWB5yiXp8SElHlw LJTbzU2A6K3/Wk4tDQvDd8Bh78uJ5P9yAs7Fj5Jy2rGYNdDgm4IfLgQi2QhRwcXghD0OvS PXHijcMRtWkXCJZftJO8Besccc76TsPrlpN5NxFyhbzEHENP4EpnptAPQoaU+A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420506; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8LJq/1nYP1KELL7mieiK61zxyQg8oMgPrLAC0XbiP+U=; b=uh0cm7YrVbka2k6daoLX7zp5xGP3XNyYICMIV5SPr2oG7kM7ual2hwnehqb/Jq+kkm0ASN sIMBJ05MINnQyfAQ== Date: Wed, 23 Apr 2025 17:01:44 +0200 Subject: [PATCH 14/15] tools/nolibc: implement fall back to sys_clock_gettime() in gettimeofday() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-14-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=1427; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=9H7RvcZBbPXMLyb8mShjwm8voZBm+v2g1s9nr5SlohY=; b=L0hARfzpKl6uaBdYMbEWUUcsXXRJExqRq/vNnj/2y97EkeP8dop9x00sMP/8+Q2c3cbMxPgN7 cJdVZYpdIFVAsFkB8kPYglBrM2OsKfoIyHjRylK1ElkVsr6iXJT9pDP X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= Newer architectures (like riscv32) do not implement sys_gettimeofday(). In those cases fall back to sys_clock_gettime(). While that does not support the timezone argument of sys_gettimeofday(), specifying this argument invokes undefined behaviour, so it's safe to ignor= e. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/sys/time.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/sys/time.h b/tools/include/nolibc/sys/tim= e.h index 1d326c05ee627b6bdb7d2777cbb11eea385f2407..e949844bf642a4ffd8a95d95752= d4b9c37d71ac5 100644 --- a/tools/include/nolibc/sys/time.h +++ b/tools/include/nolibc/sys/time.h @@ -10,6 +10,8 @@ #include "../arch.h" #include "../sys.h" =20 +static int sys_clock_gettime(clockid_t clockid, struct timespec *tp); + /* * int gettimeofday(struct timeval *tv, struct timezone *tz); */ @@ -20,7 +22,18 @@ int sys_gettimeofday(struct timeval *tv, struct timezone= *tz) #ifdef __NR_gettimeofday return my_syscall2(__NR_gettimeofday, tv, tz); #else - return __nolibc_enosys(__func__, tv, tz); + (void) tz; /* Non-NULL tz is undefined behaviour */ + + struct timespec tp; + int ret; + + ret =3D sys_clock_gettime(CLOCK_REALTIME, &tp); + if (!ret && tv) { + tv->tv_sec =3D tp.tv_sec; + tv->tv_usec =3D tp.tv_nsec / 1000; + } + + return ret; #endif } =20 --=20 2.49.0 From nobody Fri Dec 19 02:50:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B13DE28B51A; Wed, 23 Apr 2025 15:01:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420510; cv=none; b=Uz3Y3lXzUYZJPXP9L170pNqdoPldec5uKF90iMMkrT9vibXv/3XkRprPMKnawXCDk0Gxuzv6AxbbGgOq77DszwqwjMEc5Twf/qo/bs63jGuj1qBK2VWcqFFiOryDQUmIrIeHt59UuzdGYa2Vi1X9YOoGIOlagod8KnP48Y/VdTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745420510; c=relaxed/simple; bh=st5xfWnHhMqzFYyPpI3LZRF3yasUPhWHSGvOc/RLkFs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=STLyh23DqQkuM5rRDJiBMQFw9IB7oPtRnWdnJnXiml7YDZKCeNeUdJGLjoMLG7lvL6J1l9jnCxR4oNWdifUrV8PvkF/sGR6aucS4+6d0hgWX2CtSOTKjWC4y2oF//h7/b3XxdQiAiZBCMn1ocweHL9cKvrJNrPf1iIU3q/Rc0pw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fF1wNh8n; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wENAKAFU; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fF1wNh8n"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wENAKAFU" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745420507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qfk0SOkNJJSXcGWn4KkI774z2CZgjpLKyw1BvZF8y/I=; b=fF1wNh8nHwvm4JTS30FiOp26wa6UlCv1ExCXGr88OGBN3gO3IXzuJ+L3pOcwepejWcVJc9 8qBuiAaz/kZbA7BDYKO+0aOPIGT4BGOF+CLlc33Bw2TV+UNweekSnng/Hyt78xhCgkyGRO wpFw6nDixEfVFkUkJIJzJWdtHDlicmWL7iyQg3bZSBR+DTL6mwJ8cVsg6603r29hZmAxEi 34Idl9/NJfSkgQ+6bX9cmYfxV0oDwYQc2YoflcfNJKhYFuav/bz3ECAPuT56grAfugWqTA joO1vNNtKeJZ8YVvsZZDlXBrfrKomcZzfgXaJkLxw+099Y2LFTIm559rK/JE3A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745420507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qfk0SOkNJJSXcGWn4KkI774z2CZgjpLKyw1BvZF8y/I=; b=wENAKAFUc0NKbwd/rcakPQOxOTCsmSxMpFoOM6BNQG7LB+KWDzg2DLA28bVBoFQdxhLG/w 1E5eSHdtQNfDPFAA== Date: Wed, 23 Apr 2025 17:01:45 +0200 Subject: [PATCH 15/15] tools/nolibc: implement wait() in terms of waitpid() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250423-nolibc-misc-v1-15-a925bf40297b@linutronix.de> References: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> In-Reply-To: <20250423-nolibc-misc-v1-0-a925bf40297b@linutronix.de> To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1745420497; l=1274; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=st5xfWnHhMqzFYyPpI3LZRF3yasUPhWHSGvOc/RLkFs=; b=jN8k7wucSHbEul0z0ct8HmdWRbjItqd9lvsQYOw1TgeSC136qeYbE0rLkoSc9JxwuI1hJ+Qny vnVmTMJQoqIBvazDrtj2OqjhU1yyDI/NNBt7qdzX7eQ1tZcl54gvPkv X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= Newer architectures like riscv 32-bit are missing sys_wait4(). Make use of the fact that wait(&status) is defined to be equivalent to waitpid(-1, status, 0) to implment it on all architectures. Signed-off-by: Thomas Wei=C3=9Fschuh --- tools/include/nolibc/sys/wait.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/include/nolibc/sys/wait.h b/tools/include/nolibc/sys/wai= t.h index 9a68e6a6b1df8f938225007eb0de0574257ccf00..9783632a80bc20e0175a8842e7a= 7aea27defeb27 100644 --- a/tools/include/nolibc/sys/wait.h +++ b/tools/include/nolibc/sys/wait.h @@ -28,12 +28,6 @@ pid_t sys_wait4(pid_t pid, int *status, int options, str= uct rusage *rusage) #endif } =20 -static __attribute__((unused)) -pid_t wait(int *status) -{ - return __sysret(sys_wait4(-1, status, 0, NULL)); -} - static __attribute__((unused)) pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) { @@ -110,6 +104,12 @@ pid_t waitpid(pid_t pid, int *status, int options) return info.si_pid; } =20 +static __attribute__((unused)) +pid_t wait(int *status) +{ + return waitpid(-1, status, 0); +} + =20 /* make sure to include all global symbols */ #include "../nolibc.h" --=20 2.49.0