From patchwork Wed Apr 23 15:01:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883664 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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..b7b2b93bc90c9330dec7186d45c18fd3f24373d0 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; } +static __attribute__((unused)) +char *strstr(const char *haystack, const char *needle) +{ + size_t len_haystack, len_needle; + + len_needle = strlen(needle); + if (!len_needle) + return NULL; + + len_haystack = strlen(haystack); + while (len_haystack >= 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/selftests/nolibc/nolibc-test.c index 1ad0db92f0ed47f708363b2e558717fa0e686b8f..3e15a25ccddf5135db1f59bce1eefdff2ffe57f6 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; From patchwork Wed Apr 23 15:01:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883663 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 implementation based on statx(). Signed-off-by: Thomas Weißschuh --- 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/stat.h index 0eaf5496ce233a4d8b5a239eef5ecefe05a39dd6..53c5c8addc829e36b927c3dd39b2a24f3472dcf0 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, struct 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); */ static __attribute__((unused)) @@ -34,12 +37,12 @@ int statx(int fd, const char *path, int flags, unsigned int mask, struct statx * 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; - ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); + ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); if (ret == -1) return ret; @@ -67,6 +70,24 @@ int stat(const char *path, struct stat *buf) return 0; } +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); +} + /* make sure to include all global symbols */ #include "../nolibc.h" From patchwork Wed Apr 23 15:01:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883662 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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/mman.h index ad9d06b6b7919ec76a0652266158366cf639a77a..d1c213c19d7fa55c9db3a9527dd35b6072ca9485 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 flags, int fd, off_t offset) return ret; } +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 flags, void *new_address) +{ + void *ret = sys_mremap(old_address, old_size, new_size, flags, new_address); + + if ((unsigned long)ret >= -4095UL) { + SET_ERRNO(-(long)ret); + ret = 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/selftests/nolibc/nolibc-test.c index b7440a667db6b541a2548bdf5182bee0277100ed..abe0ae794208762f6d91ad81e902fbf77253a1c1 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[] = { @@ -966,14 +966,22 @@ int test_mmap_munmap(void) offset = 0; length = file_size - offset; pa_offset = offset & ~(page_size - 1); + mem_length = length + offset - pa_offset; - mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset); + mem = mmap(NULL, mem_length, PROT_READ, MAP_SHARED, fd, pa_offset); if (mem == MAP_FAILED) { ret = 1; goto end; } - ret = munmap(mem, length + offset - pa_offset); + mem = mremap(mem, mem_length, mem_length * 2, MREMAP_MAYMOVE, 0); + if (mem == MAP_FAILED) { + munmap(mem, mem_length); + ret = 1; + goto end; + } + + ret = munmap(mem, mem_length * 2); end: close(fd); From patchwork Wed Apr 23 15:01:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883661 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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..b22ff1e268b2e4bd788e974d6d8f2e1ef96dfc5f 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -47,6 +47,7 @@ all_files := \ 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..3f329feb379c4c808d2e56fbb2b7a6c4b08a7c0d 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/random.h new file mode 100644 index 0000000000000000000000000000000000000000..b5a904dffbfb5039ca8f9efb9eaf68e7bb1716b5 --- /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ßschuh + */ + +#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/selftests/nolibc/nolibc-test.c index abe0ae794208762f6d91ad81e902fbf77253a1c1..95d08e9ccf5b3be924548100e9621cd47f39e8c2 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; } +int test_getrandom(void) +{ + uint64_t rng = 0; + ssize_t ret; + + ret = getrandom(&rng, sizeof(rng), 0); + if (ret != sizeof(rng)) + return ret; + + if (!rng) { + errno = EINVAL; + return -1; + } + + return 0; +} + int test_getpagesize(void) { int x = 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)); break; CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; From patchwork Wed Apr 23 15:01:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883660 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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..fb330628a59ad8a8b2b01c027b861472888ddee4 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -12,6 +12,98 @@ #include "types.h" #include "sys.h" +#include + +static __inline__ +void __nolibc_timespec_user_to_kernel(const struct timespec *ts, struct __kernel_timespec *kts) +{ + kts->tv_sec = ts->tv_sec; + kts->tv_nsec = ts->tv_nsec; +} + +static __inline__ +void __nolibc_timespec_kernel_to_user(const struct __kernel_timespec *kts, struct timespec *ts) +{ + ts->tv_sec = kts->tv_sec; + ts->tv_nsec = 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 = 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 = 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..08c9fdd1c7991656aabca5dd4dd363da45e00dd8 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 status change */ }; +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/selftests/nolibc/nolibc-test.c index 7090e6b7c37e49afe751835bbff6a7af4f4fcf2a..7eb69971667a196cb0855d156e3e18085851de7d 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_MONOTONIC, &ts)); break; + CASE_TEST(clock_gettime); EXPECT_SYSZR(1, clock_gettime(CLOCK_MONOTONIC, &ts)); break; + CASE_TEST(clock_settime); EXPECT_SYSER(1, clock_settime(CLOCK_MONOTONIC, &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); break; From patchwork Wed Apr 23 15:01:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883659 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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..e3aeb247c74e88af5d769efe64965c5bcbb40611 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -52,6 +52,7 @@ all_files := \ 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..13628261b9cc92d545745acc9ebef541b185f2bd 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..2d61fc76fe9a9ba7571f52ff157a8a4807d7d9c9 --- /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ßschuh + */ + +#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 = my_syscall2(__NR_timerfd_gettime64, fd, &kcurr_value); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_interval, &curr_value->it_interval); + __nolibc_timespec_kernel_to_user(&kcurr_value.it_value, &curr_value->it_value); + 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_value); + __nolibc_timespec_user_to_kernel(&new_value->it_interval, &knew_value.it_interval); + ret = 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_value); + } + 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/selftests/nolibc/nolibc-test.c index 6af55a04e3c6bcea4e09c941778ffd1f3b186690..aed71de4b4f3dd1f183c7fc25e5a5cee466600ed 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; } +int test_timerfd(void) +{ + struct itimerspec timerspec; + int timer, ret; + + timer = timerfd_create(CLOCK_MONOTONIC, 0); + if (timer == -1) + return -1; + + timerspec = (struct itimerspec) { + .it_value.tv_sec = 1000000, + }; + ret = timerfd_settime(timer, 0, &timerspec, NULL); + if (ret) + goto err; + + timerspec = (struct itimerspec) { + .it_value.tv_sec = -1, + .it_value.tv_nsec = -1, + .it_interval.tv_sec = -1, + .it_interval.tv_nsec = -1, + }; + ret = timerfd_gettime(timer, &timerspec); + if (ret) + goto err; + + errno = EINVAL; + ret = -1; + + if (timerspec.it_interval.tv_sec || timerspec.it_interval.tv_nsec) + goto err; + + if (timerspec.it_value.tv_sec > 1000000) + goto err; + + ret = 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()); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); 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; From patchwork Wed Apr 23 15:01:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883658 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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ßschuh --- 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..5fed167f26aa3475eebc7a56bd97820932e97ff9 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -37,6 +37,7 @@ all_files := \ 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..d6722e22f441956e70340dcab004aa73a3125691 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..19108d33c5c6063d41916bbf46ef1f7b1b49595c --- /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ßschuh + */ + +#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/selftests/nolibc/Makefile index 94f3e8be7a68f63ecd639c4f283b3cd10764ce74..37526891af8de338d1d55315d7d6a7179e695cd0 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -106,6 +106,8 @@ DEFCONFIG_sparc64 = sparc64_defconfig DEFCONFIG = $(DEFCONFIG_$(XARCH)) EXTRACONFIG = $(EXTRACONFIG_$(XARCH)) +EXTRACONFIG_arm = -e CONFIG_NAMESPACES +EXTRACONFIG_armthumb = -e CONFIG_NAMESPACES # optional tests to run (default = all) TEST = diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index fd8bab42e75157967658690005bc9142360fc135..ab9c3bcffd9750981d68c6d16245d285ce0657c8 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; } +int test_namespace(void) +{ + int original_ns, new_ns, ret; + ino_t original_ns_ino; + struct stat stat_buf; + + original_ns = open("/proc/self/ns/uts", O_RDONLY); + if (original_ns == -1) + return -1; + + ret = fstat(original_ns, &stat_buf); + if (ret) + goto out; + + original_ns_ino = stat_buf.st_ino; + + ret = unshare(CLONE_NEWUTS); + if (ret) + goto out; + + new_ns = open("/proc/self/ns/uts", O_RDONLY); + if (new_ns == -1) { + ret = new_ns; + goto out; + } + + ret = fstat(new_ns, &stat_buf); + close(new_ns); + if (ret) + goto out; + + if (stat_buf.st_ino == original_ns_ino) { + errno = EINVAL; + ret = -1; + goto out; + } + + ret = setns(original_ns, CLONE_NEWUTS); + if (ret) + goto out; + + new_ns = open("/proc/self/ns/uts", O_RDONLY); + if (new_ns == -1) { + ret = new_ns; + goto out; + } + + ret = fstat(new_ns, &stat_buf); + if (ret) + goto out; + + close(new_ns); + + if (stat_buf.st_ino != original_ns_ino) { + errno = EINVAL; + ret = -1; + goto out; + } + + ret = 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), getpid()); break; CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 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 */ From patchwork Wed Apr 23 15:01:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 883657 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 ignore. Signed-off-by: Thomas Weißschuh --- 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/time.h index 1d326c05ee627b6bdb7d2777cbb11eea385f2407..e949844bf642a4ffd8a95d95752d4b9c37d71ac5 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" +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 = sys_clock_gettime(CLOCK_REALTIME, &tp); + if (!ret && tv) { + tv->tv_sec = tp.tv_sec; + tv->tv_usec = tp.tv_nsec / 1000; + } + + return ret; #endif }