From nobody Fri Dec 19 02:50:46 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 216231DE2D8; Mon, 28 Apr 2025 12:40:10 +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=1745844012; cv=none; b=o+/3wYdf52YczU8iwSeztTv8f93MmrOve2vQ7gKHvpbt2hygwOu8MNl3DMlfktpGBu563McYijNqnEZfNzzafva2JC2WZe9Vm1w5SIMpQuSocpGTDciBu1REFw9JWGSmB6jiu8c78p+XzLDmwTd/8r745kIWfIPZltJ16p+KTJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844012; c=relaxed/simple; bh=Xp8uvOhEOR7fzrNm6CzmO37fl3vDnAQUpFelxCvuGQc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nlmYgcfvHFxSrs00vhhszyQWiUUCjVCBvnTEm9yvk5RB57bCKo3ShFujdJZSH4lsE6E/H+F0KU0tTxkDIHdM+TwMtKM4D2QD3/8/WDuQY6FqsplCtSZDMeIjlxB1b78e5OHQcFK/RjPKjxCqf6RVlpvRjb9tYGUeNirHsWe9UZA= 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=Dmb9pnfS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ShyGXnKj; 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="Dmb9pnfS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ShyGXnKj" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844009; 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=olD03ixj73yylxhDK+TZykboubChwemnqnc4p5noK1c=; b=Dmb9pnfSPqi2SbQ4JR91LuqLzn1zFd2u5fQI17mbpWZm55uRv17Gtpw8bXPhht9U/L4NmV ocZOhyKEbD0ZJhgde0RMd5UWY8beU1Ay64WhTCElimJblxuRouWvSTGticaW8EHUcpCqZF 34ktDF/7396bFE6JiwQA/j49yeipSrerBxRiT8qF9bhub8MdOr5adTOkOxZNmkrBOf+CHY oVVZ2qkNVvV8x3f9d5BhQDn42WMoLStkVgTFacU9GZvBdC2m+bjYex0vJnxxeSWpltNP70 MHbfJQ4i+FCtGS6jJyE6Uo0w3ztFVwvix/Vv7ikq3hoRnso+6VsL95OcG2FE+A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844009; 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=olD03ixj73yylxhDK+TZykboubChwemnqnc4p5noK1c=; b=ShyGXnKjP2Bzu8ASp8pB0930Vq26JbsvKq8NFgpXpPTknHRABrHc7iSfU/cefbb7jNY3CQ zPIbrqrs5rTLe+Ag== Date: Mon, 28 Apr 2025 14:40:02 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-1-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=2439; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=Xp8uvOhEOR7fzrNm6CzmO37fl3vDnAQUpFelxCvuGQc=; b=j7/wUUWFWKiTQahLuqPUfMNRG/hoTvJCh1SqjZHGN4A4q301fmwUNt9byBWLOI7zrIZEjDM1g X6xrJzhYFcuA+HRCDwHCLJT3IyT7yK476D0jRKhZVMBm1cMWt+vugQL 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 Acked-by: Willy Tarreau --- 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 febfd69789666e2445b82423a5f4df5e18a1c774..163a17e7dd38b48ba8d9218e107= 33b3600710477 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -292,6 +292,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:46 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 66C7D269806; Mon, 28 Apr 2025 12:40:11 +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=1745844012; cv=none; b=bP4ZvSqQLFRM4kb9WnXI/8mIKylMxcIEC9dN1tzt05wKKgPTrQ5uDS5FA9VvM92HL/DQn+UPBL3QG5jD2zX8UM2q9+5IPuP3WycjrScgkwg50ihqF9B8IhmaW8g7JSxTZCe3dfvtSpcCWyE76i2kd1BeNqgsZdeW1zwvBwQAtyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844012; c=relaxed/simple; bh=Vq4SFsSgvfFXkCLM/1TQB88Lq9bzobGeGU1hpdpPaFA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UoYWSsz8XbFGfEFpdZzGjY6kZq9jgV7tRPdDW+rmYN4HCE0mFy4ndBH6TJT1eU1zQyK1qDFqZwL0Y1O5yTTy/omu3TYcZuwUthETXftNGpCCqnSOXqzpHMYWQmhWB7KtiUBI/G4KMSrps8EUStOpPBUZNYFXLb0kH8ZyG/17rRo= 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=4WMwG53Q; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6LzyQtiB; 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="4WMwG53Q"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6LzyQtiB" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844009; 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=PEFTUn4BS38AAuPwQQHUG5575AdUi7Dnv0iae5/KCck=; b=4WMwG53QcdWARNCImm+LjGRvsameEd0JAI8BGQWeamxRJ+4Sx9nzBu1c8Mhyg3RLnH6d1M VcuHl64i77IeWWZsFJg0MhmImcgOOY9rNyyW83Q0c9Sk2FY5R9MIkOEFURq5LNO+o5ym9s QPAWaFVMkCV38iPoDjjv90e92Rtu1mW8Wmn2onILIn6CQuF54GTG4env3+Ne5KwDncJc5N bTIe3v6gG9y3P90UzEyEB/FMzWOd1wOnXrapyQ6bOQh1AfmTUvaFp0KfmizRwy+EqH3Gee jidtnK3/Q0Bl7CiXQ+Bofv6lQrijnbLrXr52SICONn4cO61tUSVgna3qdNLlPg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844009; 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=PEFTUn4BS38AAuPwQQHUG5575AdUi7Dnv0iae5/KCck=; b=6LzyQtiBwO2xchRtfZOJHLZjutdwbX8amfd4RIBCnRMqAjahWYnu/3rdeCP+KmoI5FpVmM v6SYsC6rU2kT10Dg== Date: Mon, 28 Apr 2025 14:40:03 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-2-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=2425; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=Vq4SFsSgvfFXkCLM/1TQB88Lq9bzobGeGU1hpdpPaFA=; b=PuC5di+9lSy4fE6RdSoe/5UBiHNOw/jzW0kLP8zmbSHFnUOW/ZkWzpLlsjj6th+T8RQXq9V9d 5ExH1qhSP14AlJLpbtSYU7Fn1jeT8Uba/sxya5w1/n98S4iXabLnmT4 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 Acked-by: Willy Tarreau --- 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 8fa98abab2127c2cd175201d2f50d71b78d03559..df5717d591826c8468e3473581a= c0d6e76629aa3 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -20,6 +20,8 @@ #include "string.h" #include "compiler.h" =20 +static const char *strerror(int errnum); + #ifndef EOF #define EOF (-1) #endif @@ -292,6 +294,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:46 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 E5EAE268FFF; Mon, 28 Apr 2025 12:40:11 +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=1745844013; cv=none; b=JKNV18syUevZzcDaYEQwGDNEj0dfxhctdaPNU4idHFSU4x5rtd5/xnTniPZzVtWCEp45rqpYHAXpT4FjYoRDwhG6ut7YF+z3ksPj5juI2Npcx5BmU7z6Mddtc7huUXObvRkotozaG5sRQ7abffCB23U/Z2ZRReo6sko9VlNW2WI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844013; c=relaxed/simple; bh=UPpcbSd0G1pS5+bG0ky3u+DXim5LqFVkhaH6YpvhkxI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XC1GOzSGzYD/q0LS1QQ5btIY1DeiHpPw4XuNwJVGUkeSwtsg8VHu3/ke2CBm2dy8wq9Z5qQQGijCjOOYJXEVShJJHQ7fPSMtpl5cHNQCWKno8rmtiLXoSvnoyFZ5M7LiEk5BrYefOEbeq5a7wzZWI7FxwKQYn2KAkTYk8vddJyg= 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=FMv0EaxQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RJXUnjCQ; 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="FMv0EaxQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RJXUnjCQ" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844010; 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=ALdbIXVv5ve0GoNFjKFhCmLKhoEWDD+m7TIEQsPXqyo=; b=FMv0EaxQMvWr5osWE/pqxt6toCXq/dpwNK2aVeE6cQUJkyNyuXhAWx6wxNO30Fdqg4tx9r rcM1NziOULLebYJ+SvHkJvS76/T2c1ZudkTs/pOJsCQtC0LG1Vng9zD+ZIaVLOYB6ztrNa OdUi+8cXXbEfggzWsdJJr+kLpD0nLBpvHd2MvAcBryzGilPW68c5VvOkzY9oyuSkEJ06Kb UdTT05uuyn51d7PrYZyzg4i1QHqhY0Vsq7SXE114pUuqb6cN3PfV+D5XWhm+UmTzLX1lb6 LUxPEP9ug8/SYSwMWL6E1eOYFrF+e9NYWSnrItaeaivj1V3Hlje2Op9e8/mWPQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844010; 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=ALdbIXVv5ve0GoNFjKFhCmLKhoEWDD+m7TIEQsPXqyo=; b=RJXUnjCQvaGEK3KmjORw9AccyaaQ5ToLlSh0VSIwrDdU7lhOWia72z+yoARFtbHz0oUu1m 6cY5zzneT1KM1HBw== Date: Mon, 28 Apr 2025 14:40:04 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-3-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=2037; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=UPpcbSd0G1pS5+bG0ky3u+DXim5LqFVkhaH6YpvhkxI=; b=PzkSXe3FRqaVx/4B73/MKKn6hZ/+CVnulZKhi2OUVdOSDEVTneCN1b8Ttp022hJvxHH6AnibS 3yZ0laP0a00A00rjfY3G60aVZis85m1REbkei3JJ2i3aMEi6Ddm6H6h 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 Acked-by: Willy Tarreau --- 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 987c8bb52502f1f758005b0afdfb2e39fc8ea1c6..8b4d80e3ea0360e3e6280f6c616= 6e87f793ddee4 100644 --- a/tools/include/nolibc/sys/stat.h +++ b/tools/include/nolibc/sys/stat.h @@ -17,6 +17,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)) @@ -37,12 +40,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 @@ -70,4 +73,22 @@ 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); +} + #endif /* _NOLIBC_SYS_STAT_H */ --=20 2.49.0 From nobody Fri Dec 19 02:50:46 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 707B726980C; Mon, 28 Apr 2025 12:40:12 +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=1745844014; cv=none; b=KFmamXfQy3qvkesi1gQ3I+GWjsceI/WrQaMCAL2KOktVxwuPevr3suNdYgjYmE1PKqrHMODY+do0ERPOrK6KN3KCVO1jx/Zfno1EpZxfrIhbaOyZFXf1rKCv3dQlirMzim148iIoU7ThvG1E7qhp4Pu4Wb0bmYZkoN3Xlr9lO6A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844014; c=relaxed/simple; bh=x7vlVk9lwh5FEmDyAto9R37oXEH+y51IIvotdJ4TcKw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BW304drDgR2Q/Vza0g6Mjlf427h0fkONj1pfGlsBJYCDyTyMJEThFU67sCR+mIbjGvVAxrH0BiI1wUzezJlkxTyz5T+TeEwwNzKxS2WDFokKy/Ky62o6kpWr9d3kbTIZUoKsvUipZl9vlT1Psx6VXXSt5wsDVlg5JRWGUNvtBIU= 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=gIwg4Tsu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lcFmL2Cm; 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="gIwg4Tsu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lcFmL2Cm" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844010; 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=QbPWjHhlJoETnAz824XzmvX5jyZFPtPy6g/2Y6azr8U=; b=gIwg4TsuLI74B/q4WScfk1HlnLV7sSeJzpII3Vrv8ZY3wNX1HePkzU9VQZw+YCdix1hXNK rEFsSgH6WV5xzACdB1XhvOoLz3EOjII1hkwtrRn0+grFetrFxgUDomjY12kUXzHewIQdfl S14nrMhlgW+BBYUcsJ/rTOAR8mCIwSwsciEt0aGJb9wzfjcoOIoAZ745Wu+rv6dbpWPUC6 u9jnCYrCROz4oC2cAJp5MEDASKb2sJw7ujOhSfUyvY0m+yPSZPZeUO3G6FtfLua05ZhUBD KRDHjmsqGf57G/PiQqoXWfP6bmKWN22oWExKaEkMwWg5k+azXaJ+9yzAUjT+EA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844010; 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=QbPWjHhlJoETnAz824XzmvX5jyZFPtPy6g/2Y6azr8U=; b=lcFmL2Cmeme180KmjzZH8dso5SwlCBOV0SRr6gwoudEdeU0wjSGgWlgRNt2ttKzNEtaCkA nfBoleFlK2FQoiAg== Date: Mon, 28 Apr 2025 14:40:05 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-4-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=2698; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=x7vlVk9lwh5FEmDyAto9R37oXEH+y51IIvotdJ4TcKw=; b=2+mGdTIJWB7jJfMqgtCXuSBYTlP2bzo4FbtOCG6JqBZehw+Fsa0ydAIjWxTsgXafLmHUA1E1X Ida/KUW9TD7Cc9H38GhkfFa9JM9cKFUKWe3dQxv8i39yXdaJw6Gu8Gi 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 Acked-by: Willy Tarreau --- 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 41c7bf45e427b96efec949364890e9c2b9c226a5..5228751b458c8d8a1bdfb500662= 3d016a48155fb 100644 --- a/tools/include/nolibc/sys/mman.h +++ b/tools/include/nolibc/sys/mman.h @@ -48,6 +48,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:46 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 470B126B09F; Mon, 28 Apr 2025 12:40:14 +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=1745844016; cv=none; b=nM/wCu2yRgpjyilQpBuaK1skuBu3a9jW1KhI/iUk+8QeMBQvpFgyXLsfQfvx8j0/2667aRoLchcJq6pCPorOd9ZcK98IqJ6ObLk2roLlRrxD+wErphUfdCUIKwZi7G8wF04EHVr4dr5zQrzW51PrUoB24E3okKHTkSCTv3nHVzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844016; c=relaxed/simple; bh=DzFtg3UDqBFeItEsUzo9Ww6n9EDXJhOOJUAQyvCiWL0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BCeio32V+HJT3xha3EgMsbjZOmn5KGoUaeqpmKeuepPQU3LcEg3Sg4tLeOGeLn1PVJRNfkUoGaDioJHLFB01FOrR9PX3BAB0sG2+XHD/OR2fXsvUOwnYAhROT/6KsRie0UOjSqBpZLqbrROMlecuTuaM+a19R6hajaS88zTLMFs= 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=q8B4CDMZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bB3L6gSJ; 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="q8B4CDMZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bB3L6gSJ" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844011; 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=N+nyq/bw7RYw7/i1fLMy0Z8JgRIiodFsEkUIDTIZCgU=; b=q8B4CDMZcYAc3rYuNJXxMI3+0PWICpzgZIGiXCz6YmpzT31nIs1CytcoI6+gt0DWEssRM1 1BWuhOmB1pX1FhxRupw2uikPtQ7IxRrEWJvV6AcsjQFgVdWItaz4WrkjeB0FsjU1snQp0q 1DT4rnc7dmOOTetPjz1alwX35fEtbrxEcX7W79SqSGkGBuHvO03gwG6BA8GPTbRbBk7zJU OHDEAmwnnC4ODBjJo93Gh7KkRt1FDGS7CTwt+YgeoVKUWz6uvGd4u7a/znkResUiODCewe NYmeVjdo8CauUuoODcmYV1eG0NP8EX1jOKfdk+1CXjzUKzZld7+lKypEoFinnA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844011; 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=N+nyq/bw7RYw7/i1fLMy0Z8JgRIiodFsEkUIDTIZCgU=; b=bB3L6gSJ/BBTWuyGvLGaOGIhnpvlUz59x3Ti6kwKnyTVYOipXM5Its/Bb/a2tFf3GR8C5S Xn88QGzaR77toeDA== Date: Mon, 28 Apr 2025 14:40:06 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-5-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=4087; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=DzFtg3UDqBFeItEsUzo9Ww6n9EDXJhOOJUAQyvCiWL0=; b=drp+gnpMUFga268e6kUY4Sjn8nSqU/lBPNW1lOgtTLZY+1FIsw/KeoW9uW54CoFoPqBX35Xly Tp4quOyDrx8DEJvtyDo+AfTRx1HvVagGLV04AID8S+h5UfYAIGcSTjU 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 Acked-by: Willy Tarreau --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/sys/random.h | 34 ++++++++++++++++++++++++= ++++ tools/testing/selftests/nolibc/nolibc-test.c | 22 ++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index 41ef18872a7e9435a5efec9c0b32b9e29fcd4ce8..4bbe3b7fa2805f356d9a5cc1e4a= 8ee55b05af746 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..8d9749f1c84572ab07c6dc70f33= 1583063266eaf --- /dev/null +++ b/tools/include/nolibc/sys/random.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * random definitions for NOLIBC + * Copyright (C) 2025 Thomas Wei=C3=9Fschuh + */ + +/* make sure to include all global symbols */ +#include "../nolibc.h" + +#ifndef _NOLIBC_SYS_RANDOM_H +#define _NOLIBC_SYS_RANDOM_H + +#include "../arch.h" +#include "../sys.h" + +#include + +/* + * 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)); +} + +#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..df1cb3e62564f1fa78c55740f7c= 2403b8390168f 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,26 @@ 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), GRND_NONBLOCK); + if (ret =3D=3D -1 && errno =3D=3D EAGAIN) + return 0; /* No entropy available yet */ + + 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 +1145,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:46 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 487C926B0A7; Mon, 28 Apr 2025 12:40:14 +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=1745844016; cv=none; b=XtqIefnLft76DyGvYzKb8XR51dc1CWj+nWC9EIk3Kr+LmeBjuoIe/+tyRIZe24Wl0PxFEXPgU+Bek3iivbqBAECsXyQYr9RcZL3H+StWydk1NzQ8fJzlGmZWlVwvC0IOgqC58shN2vgH/IidAT3JwNolh7FHViO2yKiwIRbmK2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844016; c=relaxed/simple; bh=GgB68c8Kv1yFST4RZVyzlgzYluq4bR/jdOFo+kxIPbo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bxolQBMnBjVAUd4kPwQeST6SMwQuR3cYSLIMbQC8V9jvVasiwEE9FLXJWt6wRw90ZFdJqmRgVJuBkYnbgLTjcar587/6MWQdrAgPsOZpJJ3u1gIpcr21YRws6Z6poJj3LEsUqbhl6omU9OWEiUpnKUE/Sn7HTl4sCTCPYxJEZaM= 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=iLh73l/r; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CClS0+NJ; 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="iLh73l/r"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CClS0+NJ" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844011; 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=VcSKazjeGyETBw+7Vy3nf4kOTNfP5Y5fqGj9lMJNEgg=; b=iLh73l/rQV8k6n55xL3tZmxMRsJikStmVx4dq7Z4j0ZPp4vF172Qkw5LTC50vjBqf0mSn+ SQHpwrwnhm9UlU4FnuLDqG/Euj5/LPmCc5AR5rZ2zP7c9lmbnJ/hOOR7fChpNN5qZwuD0S jqyPzQLKKeSAVrg36gbfij2//SlTC2cftVAwGYjSJUA9Gy3UYeIk+n0BCGuMbUXo4I3cIR PzN5CYscMJyUZ9MKoX2xTJ3qpJVqTXuFMoccSjsBR1qeHocLQrAQ8qNG89MB0Aq16BA5Xj zIC08fzXw/t9JKYZXBK35VId1cl99JwBDvbYOMheGxCmDpn/BbhNgyDL51zDyQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844011; 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=VcSKazjeGyETBw+7Vy3nf4kOTNfP5Y5fqGj9lMJNEgg=; b=CClS0+NJB9oR9t6rUgQswE6jfggx963QcRa0vmpcfDGK7WlE80nG3og3WUd1bTjqHXE6xk r1ouu/eyrEFA2bCA== Date: Mon, 28 Apr 2025 14:40:07 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-6-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=3846; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=GgB68c8Kv1yFST4RZVyzlgzYluq4bR/jdOFo+kxIPbo=; b=k0R3n4y1NO3iWIMDM0YCMUXSPtuyqcq27cOTOXaF4ScxgTOIH6+okkzkOF2QLuhPJJwEp3D/n 0I4eQO3ODq7AEYGh7uYVy97Id6FzPMm7VyNuC0NmORE91uQ31jXQCxd 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 Acked-by: Willy Tarreau --- 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 4bbe3b7fa2805f356d9a5cc1e4a8ee55b05af746..552e44f5a48211f81c68068b9b9= 9c1dd6e44e9d8 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..9df823ddd41297436015333d665= 61f810f101d06 --- /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 + */ + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#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; +} + +#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 4790298f985c2b0db91ec777140201b7ec869346..5fd99a480f821210212f08d814a= 4c09c2c804db8 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -32,6 +32,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 df1cb3e62564f1fa78c55740f7c2403b8390168f..14a27bc6c83e4fda595b10dc29c= f56b63904272a 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1318,6 +1318,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:46 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 5B38A26B2A5; Mon, 28 Apr 2025 12:40:14 +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=1745844016; cv=none; b=tl1d7kq8dZ5k55D+GKS4hbOlAqgLqbi1ybJW7OUEI/U7RTQTprgbjAspa5K9QdgK3Nh71nw9+Okx5+fxk7MK7ITtfpQzm+hSXtG8/4tf+GX7geL+K52IE9wsuMZoDpRydnOQ/rrSTGNXcjxAXiDKuAOKFjCehjbN/CGGz86Z7Ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844016; c=relaxed/simple; bh=2XfhIYrUpwPiS3xNMEeUtDZt+yUx0Is6L7LMEnS4xiI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FAaD4ZPx3G9W8COm760duzLy/9p9PNmNBGeisseNYgTL9QN8mK0/wMKYe4em6+jt5tYzhYaK195RADpUbLSNIrHvWMxr0fILDgE9VpVJk+pXCL/c6ZQpJLblAn1JEV51h5dezzxdRv80nna71MFt6Xdm4tgkNVRsgj4y6jGqbDM= 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=xkS7MhE9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cTq+Brhn; 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="xkS7MhE9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cTq+Brhn" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844012; 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=ZKovy5nOL4qPV4KCSZjTnwgma6hkKTxr26X+/tQlD6c=; b=xkS7MhE9vXG6V3ZzGaeAIEGqggxQP6Ii+aw3DcwndvQkO5WBYa7YXBim1wh8vkfK7Po27w 8fKSpx3nwbk3y5LYrOYc0CCggRgpOl0absLeohbBRV4r4IJJNiVDg61vIGUl7vdqySVW5y Gm4mzugcc3+7OEx6EMYEt6YG4OfDxi1+CPCNr15f1xnIwYxsi4bFl7HGCno4lProSo2tqE xMEbXiREIp0QTOiBO2h+kIZEMN57c6A2rcKb5VxIPoq/g+WxlfLOoOnynU3Eap0w4PV87G xD/ePGeA+S18G1pG16RsZWxqR5B/n5Zb0Fp8qAfU6wj4bv3Ydsr7WZjiTwcpfg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844012; 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=ZKovy5nOL4qPV4KCSZjTnwgma6hkKTxr26X+/tQlD6c=; b=cTq+Brhnh/O4B9RxZfiPxS2B3Jhv8/1GuSwGh9ZDIrpl4VHHuBV0RJxJnNYBzG2IPBKZe2 mQOQg4eSrfKY2KBQ== Date: Mon, 28 Apr 2025 14:40:08 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-7-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=2299; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=2XfhIYrUpwPiS3xNMEeUtDZt+yUx0Is6L7LMEnS4xiI=; b=R/X5VEjcDnfZETbZ9ZKyE6N3oDm78sxoUudDSbHg8TvpDPuyZVfG7wbrp3cHq3mFCkwWR1VFN eWDq11G/7rpCuDpyrBUvmSkAWLGMn6VlJ9HjOBOtn7CwHZBSPO09gwT 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 Acked-by: Willy Tarreau --- 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 ed253305fdbafd040ca5820207d8c53c6ea9f9fc..25bfc7732ec7e72a60f445588a2= 33f72c008f7ef 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -20,6 +20,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 14a27bc6c83e4fda595b10dc29cf56b63904272a..10db118b8b111ec17f713d57240= f193c7b18c70f 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1112,6 +1112,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:46 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 753CF26B2B2; Mon, 28 Apr 2025 12:40:14 +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=1745844016; cv=none; b=SkaCgoieH3vWvI89/yvksVcxFG1gqJ1UCvk0kYlAKMeZh78ibdP1Lgq9rgdOZEHkXhJ15O7PBHBzEklDF3RNBBFMg/gbkmEM2IyJKRU1BAlFCQnqoxLhq9q5PZXDhAyRHE8x/5uCdVBi7OPbVZu2NVba7aTK9JAEoFcwJGulJaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844016; c=relaxed/simple; bh=jKHlhI6seUzGayl/WwMKF7slaauY7Kha0rOhKN4hhUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OxXMXLxSwXwcDC1TxYm5q9DoXIK5mnei9JEXE4eGWV0KaUtx7mzE+yU6rM7cXxucSnLnrcLOtmiXg1TX9JDbLphCw/duGepsXM1qdjPSKZsD5U6dWGxYjUQ3EW8XSvJha72AhYpFGUTID0vGbJTttqW9ue1lOgRPGGMXaTNv1mY= 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=STan5fOm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4KUMdBLW; 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="STan5fOm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4KUMdBLW" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844013; 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=QJY1+QnNRvenV8jVIlAdmBrCNcOSvyEzDjb7yrCveqk=; b=STan5fOmfwSgFxwl0wQzWsoiGXQBHLi/kfz9XCRgrMX+mSEYajInjaAAQsfhiGQ62R/9Vr I/eVJNQF0fz148bFulweQixFtHTDW7PK1wrfpjBG9NcmXgSAln7NyoITYl8o7gP0N4PQXM eFpTQ97yL7kBvIlPcfsMotsAj32+GBZ/1bPObnANnOFfWZjS1jI5cdGMdPJXgkXSkh+Bkz bHTm3oz+5YSrZRjYHcji3EirkAsQDL2txCsVhXHHPwzXothpQrNfEVmpNjYkHYkBH1knJ7 LxyGCP42XUXNTNnjA0DjELyQBXSiibIz4hUBaeRSQBWPyb6nms3joveE9e+ddg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844013; 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=QJY1+QnNRvenV8jVIlAdmBrCNcOSvyEzDjb7yrCveqk=; b=4KUMdBLWhIZ14RzkaEUcamr7WcUsO9/yNDKJiBVwcYi9n7yoUgzD2tjVhOWBEXmn5I2N2q nMSMv+VY2e3cAHDA== Date: Mon, 28 Apr 2025 14:40:09 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-8-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=5433; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=jKHlhI6seUzGayl/WwMKF7slaauY7Kha0rOhKN4hhUw=; b=1W4a9hlCjoJBmbnvzVr2VhZu95BOacur60q0nz3/vifeerJJUbiknxrIUyHCKMzUmsNrS8PN9 PBidkH5apqkB8NhOnAKQ2wGJ/6nVv+oHBO6+5e3ywbVfoSM3HbbTJDU 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 Acked-by: Willy Tarreau --- 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 9502f9aaf62110ec8ee113fcf08dd47de40e924c..7e0b7eac0b7c31910a48ebb9755= d5004e5da1a69 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -15,6 +15,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 fe97953d16572db3e4c18cdc8921c6a991d64f94..8eb48d98667f5fe65bb8a974f61= 3fd1fd17ae11e 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -201,6 +201,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 10db118b8b111ec17f713d57240f193c7b18c70f..9b8aa41a547b1735014c5a7541e= 426f4e08c7f90 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 @@ -1081,6 +1082,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; @@ -1114,6 +1116,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:46 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 1FB2526B960; Mon, 28 Apr 2025 12:40:14 +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=1745844017; cv=none; b=ALYf5FxCjyuPhAPoyKRcghoThXkPKAuOfqRnYV3yLAktpk7g+UgeEeSYh8ZxnlmlA3F0anPE3igPae4pfwFSc9MTbVDsmQByE4vHTFDFK5GluMNvx8UOfOi8OlXF156amN67qxWsxiTmxgE/A3zavAmz7QuMWoSyPoU+Or11HyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844017; c=relaxed/simple; bh=r7MpfiNnGdfXFcxKitAEOsdH4MgCqhIf1FBUF/XO6/Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gUjtF1rNRqUHz4YbN2+iiBqQoUh89mVMESIM8wioBXOvB5d6kJWSR0W1tWsGG/rWW+7YP60PQgnnFk/Xu1NkIj8UMGfmjwZs7L0LhARQYGwss7d+mXOKqq8moJc4zt1vKG9C5YExAqS5WRegEIRselX+6zuHLjOZk/IM7WIDJJ4= 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=dttleIPn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qiGd4pjF; 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="dttleIPn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qiGd4pjF" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844013; 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=TN3r7fgOPxBE2HHLeJFIdrF/MUglLa7bwqqPumSJmTM=; b=dttleIPnkspJoZ6pmr9fsZW8rwyRPfF6a75r06ZyWhUjOolYfe5LiiEw2PTXo2tKGY+I1r q72kNZ88ly+MagUBGNeRnb0j5IS0CgMZqIsNtgGEvWWucBVVBHXt/yk1JSYsiJUBd+2UKF zoJMKIwCT6jTN1jY9J+CV84cNH/gckRYAenNQcIjbdbp2TLHVjG+tlAO1pHXy0j62kcAmf UW9mySovty6SQ7HDpcscNxhQMe5b7vjKnHWK1iMzgVCT/8S7Rh01ovVFN245t1w+FNiYxN SSkerfiOw+wSUCZTSZKSnyG5gSdgRi3DeqG9sbtpq5N5Ek7tAg7ciDtY12Aabg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844013; 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=TN3r7fgOPxBE2HHLeJFIdrF/MUglLa7bwqqPumSJmTM=; b=qiGd4pjFK7MtvNxMQMxxp12u/o3u5KSPi/0hvgxEDed/YTqxbHTOCi70CaUBF+7fW1izZr zayEgeyTwA4F89CA== Date: Mon, 28 Apr 2025 14:40:10 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-9-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=6391; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=r7MpfiNnGdfXFcxKitAEOsdH4MgCqhIf1FBUF/XO6/Y=; b=QP9nlx4NsXKJLj2GiDQoEcbUKApPz/Oe5hgr31WfcZ2R6ifFG74nb3Ws3BRhQUspjHBsoO4qq 5LER7GTxX/cB/cFX/cduibAiLYurw7iPUz0YAf04W324eqFgkZ+DKuE 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 Acked-by: Willy Tarreau --- 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 7e0b7eac0b7c31910a48ebb9755d5004e5da1a69..48f602b661b300a0bfbac5a2313= 15fc95727c04e 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -15,6 +15,7 @@ #include "types.h" #include "sys.h" =20 +#include #include =20 static __inline__ @@ -120,4 +121,89 @@ 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)); +} + #endif /* _NOLIBC_TIME_H */ diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index 8eb48d98667f5fe65bb8a974f613fd1fd17ae11e..2b02d4531427ca071d6e55abac5= 260a0b0226c40 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -202,6 +202,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 9b8aa41a547b1735014c5a7541e426f4e08c7f90..7530b442941f5538cd1d54974e7= c8aa435513653 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -905,6 +905,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; @@ -1186,6 +1236,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:46 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 899D126C384; Mon, 28 Apr 2025 12:40:15 +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=1745844017; cv=none; b=SOLlA3qCqaSv8YyOlGzXP6J9U0+uiZPCU/aUb8gNV78s5jyzkQbkDanLWlFk90leI+ms9ugmnigy0z4R8hAx2EdxsdNf6/KIb1tXT34RU5SLs+HAR9LF8i6pfKockvGKvy/tPxx+irLhoPje8Ojpm/Cmwm8aaCM6sBJY5/ap7qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844017; c=relaxed/simple; bh=p8Qfp5jbr7ezRzEZ5mBSvKLM+B+sEy8yhzPaaJe2Xa8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AHMELqY0ZQqJC1Qv+AXX64NIr3RaaB7K8VqThE1+jids2++FXmJy8UBmzPRsdR+GRRmEx8uGKFeRoKrKeOxl1XSfaT8hBq/w8OTdD0j/8JvTeZC4nwiotZrE/Q79TN96ZVf77Nyo+AMdCC6hHh6aT37lQFZWX5XZioqfHINaHm4= 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=KeMRdeDx; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ikgXC93c; 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="KeMRdeDx"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ikgXC93c" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844014; 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=/PbjA91xQab2W4IEoeudlT3ft1JpgJ5knx/HDjiMC8s=; b=KeMRdeDxH5Z+1nLXGHsaTgYTWN1e0IOxcw9Xel4QCFSvcbT1520de9U/1EL2qnzflGRs50 GI5pzZdnAuXjsmCWCVKbSJFjwBVXGE9FaV/Mudce//DiBchvE0rkyDEYg2esNQOa94ZDym WiFOoT8fl6DIrdu31A7RBBhTfcCF+CxklmOgjdhQcnRU9DQVRNIKQTFxf9wdQIVWoKn9Ca 6Jc84r3yzjnGLHy2urq8jwiKQOTGfe9zUe/e8TGafwpimTv6QxtmaJQssP21Lkjkns84/i m0z+VVrF6bOYFzcxaIbCn3BFGzMl+6Y5lgCLXz6z2A6RMntItooiLbtOBsPgzQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844014; 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=/PbjA91xQab2W4IEoeudlT3ft1JpgJ5knx/HDjiMC8s=; b=ikgXC93cqak0X7ueXB4JaGe0EbSbtoeovMEaiz6rqalRY6OnS6EguXqDD2crOlReMO6fH7 Texa8tNS+4Hl15BA== Date: Mon, 28 Apr 2025 14:40:11 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-10-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=6409; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=p8Qfp5jbr7ezRzEZ5mBSvKLM+B+sEy8yhzPaaJe2Xa8=; b=J1yqlEnNYPKNo0mrKz1V3O7h2geZdm3QbuatE+LnXS8dBB6cf1TBqMPYLVtMMsyvGTp5sBOEe BN2bXuR0lKhAT6J/1yYVPVmZirEWrfEptJY5tBIeSIhp1/RxGIKxvcP 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 Acked-by: Willy Tarreau --- 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 552e44f5a48211f81c68068b9b99c1dd6e44e9d8..d54646387872d3b0d0158a35c41= 22f4713c435ce 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..4375d546ba58f849b3ba48e7b22= 1e6ec3c15fcf6 --- /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 + */ + +/* make sure to include all global symbols */ +#include "../nolibc.h" + +#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)); +} + +#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 7530b442941f5538cd1d54974e7c8aa435513653..d73125c41096fd208b7b8719e61= efa2da9865673 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 @@ -955,6 +956,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; @@ -1237,6 +1284,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:46 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 3F6F726D4F3; Mon, 28 Apr 2025 12:40:15 +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=1745844018; cv=none; b=CReh8ez1fpuvVbX+X+8S66Kl1G6SUhbjF2KGV5uW12GT2aZXztekbI6+dV8XJRwX56XglOZjLFOTvpVqqD5hYxC1l+OaEcscG7skqlFAcvwhzigeMK/uh1D9QpoHr4ZTrCl67LGvBGAQQgLcVIiAI22VAnj7pE6bFO64tl4qhJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844018; c=relaxed/simple; bh=TSTWz+qX8Q+FXXbscSdm+0zq+oKFJszOahA9pWCFjRc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=vE3NpXi7eOzmFsZKakwaSFm7Z9ZiWqW3kukBGAUyCKKKayshWf2Zt8lkoVbPHmbpobpD/qR8OEUJOOVHCDzLZxqmWIHQICpxI3jViEfJXbN9o9DCGRUZKUZAN+1mi0TXvYXdIXjwePbJ6ICO4x3AhzDSjxb4+uSWmDdJn5UD4/8= 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=fChPIF4I; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GQC7BTt9; 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="fChPIF4I"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GQC7BTt9" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844014; 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=/M4ZkI+BDRIkqDPmauaPsLvEjDOe/PrNi79VD9UYdC8=; b=fChPIF4IKQVqMgMriTg58aAa7W1USbuqhpVrJfdt+7j/NNtg1BjufcyxKTIIhZ3CvwwCtb GCSZSDZ6eU37KCnKwrJvjG47hB+SpWnNXLBjlgYpMaVjEgevPBOYiwVw6clJETwe8g8J+O lOF0YqGuUrSalkvlcJ4YOi8qbKJvn0Db0zJvPG0AUhPN2ZD02V+dg42P0Qp5QOx1GUOrXg fKLSUEEda9bH/7vNyzjvWdUF12s3KNtvN4x3yfZ1yubdHNhpxQWkaFSNQVkp6G4P6YezFF pI/o0L94Jken9T4DLTiyHuKr3VCzpUerI6p8wU/3Wk5OBDKKuHWQjYy1V986jA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844014; 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=/M4ZkI+BDRIkqDPmauaPsLvEjDOe/PrNi79VD9UYdC8=; b=GQC7BTt9AR823hiZg67sb3sexIsyv13KHIZweb1qRbFgA3IfOd+T2A/v4KwFnhY/cptMCZ Z2L+fy8b8jJfElAQ== Date: Mon, 28 Apr 2025 14:40:12 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-11-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=1897; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=TSTWz+qX8Q+FXXbscSdm+0zq+oKFJszOahA9pWCFjRc=; b=rrb7NY0/ZIzXbP4KBbbjj8MzQYalESyvLQu9QATKWTc4Re64kUA8vodDo0+Fin/U5c+28v9Ym DmvAUoDiahdAIi5lMT6K71aMr35y1t2stqMPbmPitu/WQADkbd0BGqN 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 Acked-by: Willy Tarreau --- tools/include/nolibc/time.h | 7 +++++++ tools/testing/selftests/nolibc/nolibc-test.c | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index 48f602b661b300a0bfbac5a231315fc95727c04e..fc387940d51f389d4233bd57125= 88dced31ae6e5 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -108,6 +108,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 d73125c41096fd208b7b8719e61efa2da9865673..665d7631fbc6e9ecae469a4b143= 440ec0a43d4aa 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1304,6 +1304,17 @@ int run_syscall(int min, int max) return ret; } =20 +int test_difftime(void) +{ + if (difftime(200., 100.) !=3D 100.) + return 1; + + if (difftime(100., 200.) !=3D -100.) + return 1; + + return 0; +} + int run_stdlib(int min, int max) { int test; @@ -1426,6 +1437,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_ZR(1, test_difftime()); break; =20 case __LINE__: return ret; /* must be last */ --=20 2.49.0 From nobody Fri Dec 19 02:50:46 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 5B54E262FD9; Mon, 28 Apr 2025 12:40:16 +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=1745844018; cv=none; b=UqOCEhgAK9XXftXYnGbgazDR8x7iaZSjGFw7TUrZuOdoP+zA3q9xvTaV2PdRNuUpHJZYbVI2+qUzzaeYPScmNgDAH6iJsv177adBelHTT7O3251OjioJkIjfux0hL6p4VLSFnI+aj851x4JwGWRQ0PgVIwZ2Nf/uTnnh6nwnkyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844018; c=relaxed/simple; bh=WeAGTV8OHPTjzwgW5Egi8+ev06i97jWnUOaoMHGLjlI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=X2/b9qT/O+r6wAhs1BIuoQEpqgqeargVGCZJz60W775KkSTZuFQY0xkynyYz55ttbiaNHqiTmAOHdtXFRpTwgJVFCJugUAxZ92XgYDQteGF1ky4330+LTOTWIfN6rOdvCMKfEKvOBXT6D2AWi43jzN+yUR3VKIVoK7eLrZVzCdc= 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=3LBVdia/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ErMAX1nb; 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="3LBVdia/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ErMAX1nb" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844015; 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=1W3Lr5AUZYiINJIhhoTZvtHq8tC9SgHLl8tJzDEjFCk=; b=3LBVdia/77zU7LcKjFXtipuCPnGOuLprEtH+W1UK+RScX6Jkk+G2QvCfcjjk/z2OEHpD+A rNoBRY9nuETsYnuUP470fpeUkH3nKRJYPeUuHJMqVsRWOHkWxhAC+g05b95fTmRfLaca/Y lbUs7p1kNjCaAwmwSLcjNld30dwobPJEGL7Y+BLzvLxKx/OYEDZMGzunSpTYyJFOa8OFpZ 4OT8Ov37mpO7KTJ4GBw0g61iWDM/3lIoC7QMCCweP61EsC2o4s5Nh9gJDDknQ7UQJta7yp w+xSTLuxPrKNnA4x9bVLnQ32a7sRxWLZF9p/lMb1eBeDU9FUbJ3aWwPN5OUZAQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844015; 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=1W3Lr5AUZYiINJIhhoTZvtHq8tC9SgHLl8tJzDEjFCk=; b=ErMAX1nbPPRBtGD+ZvWtOYvL/DGElGYtCPbnl3WC3pg0sVv7Bz9TuhAI+Vxoeh2CqeW8Xp QiS/Iaad1nGQLVCQ== Date: Mon, 28 Apr 2025 14:40:13 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-12-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=5562; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=WeAGTV8OHPTjzwgW5Egi8+ev06i97jWnUOaoMHGLjlI=; b=BC195df7JZ9wXBc5jXhYZA6p59GNibkX5bah29hCq4cHWDH5BchDVBHy7ZQQXtUZZwLFecd9J E+SKxz2dvEtD9/9P14vFlFaW2ASCUpJN+7ou/d3QnPMWD5uXD19Fv8E 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. Not all configurations support namespaces, so skip the tests where necessary. Also if the tests are running without privileges. Enable the namespace configuration for those architectures where it is not enabled by default. Signed-off-by: Thomas Wei=C3=9Fschuh Acked-by: Willy Tarreau --- 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 d54646387872d3b0d0158a35c4122f4713c435ce..48f89567c03fb3cbcbe1d20accb= 9d293056d4142 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..32221562c1663f6d1d78fab82bb= 6e8d05ad68da9 --- /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 + */ + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#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)); +} + +#endif /* _NOLIBC_SCHED_H */ diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selfte= sts/nolibc/Makefile index 2671383045dbb51682fd62ac8c8df9c317157317..94176ffe46463548cc9bc787933= b6cefa83d6502 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -109,6 +109,8 @@ DEFCONFIG =3D $(DEFCONFIG_$(XARCH)) =20 EXTRACONFIG_m68k =3D -e CONFIG_BLK_DEV_INITRD 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 665d7631fbc6e9ecae469a4b143440ec0a43d4aa..d1157319b5d715dbc94ff9203b1= d2a2e932aabcf 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1172,6 +1172,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. */ @@ -1296,6 +1362,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:46 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 1B3AD26F478; Mon, 28 Apr 2025 12:40:16 +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=1745844019; cv=none; b=LIYSKsU0Xrg09lbafUGo8bkyAYuUCBMO3E+T4LzykCgVbkBLpwA5uk9mTRBOPRaNW0JqKnmkWch0QrNv5vS745XaQiqCBVbh5MMOLwwNlkMCU2Z+aAqV/8gQVQzfk496YO58CBSKqvN8+R3AcixFQbM+1ohXBqTMb4cWTu3GeZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844019; c=relaxed/simple; bh=aEBBgnNmmm0oiSb4EWtCJKDiJ+chaLjNWUeDK6AsQpQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T/yJ6NeZnZ4/UWPSX/s/Mc37uloAgpXMEV52lfvgRPt7z5bH2uQ4aeHIeJVXVw5xcaQVFXiY3TdxQdDuugR7CO1UECYqznHGVcinMpxDusp16i3dZbpfueRM9fVfn34WU0gbblx6lrz8Ue4thQwlLeXCsn+GVlhE4yqEGfe0cnI= 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=TKCVKwdt; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=etbcRAP5; 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="TKCVKwdt"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="etbcRAP5" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844015; 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=5fPPjFV4RDhqoeNYAqhvlDYRxfhJhfDARBxfUjI2goc=; b=TKCVKwdtniUW58Qi22M5qeDi/5bV9vlLCivi2Hj0Oo4ry4xAaye9EUcSsRGViMcrKJKANg +rawG+NZ5EUT8gXC4vYkibgP+uuz9n75EZzh5/uFRrL5NcX33oEl0HXYGV50iMlHYEotiE QqVyKHTjSxu5mmh9HHkUbKCv4g/3Qp1GwMVO7pXW5TFhDdraonHOZxASvdjLE0XJ++RryG V0CiYGwOfZzYTi8wIAQmNqIGL+n+EbjaPwheYTQ8cFm2SKHsme3k+VMMLfqO9NlUGy4tk+ ds6yPQm/EU6pPBV8x123SImWDqX7RbZOb+ZhM1m85Pp+zU6es31F7ziWfxDKZA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844015; 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=5fPPjFV4RDhqoeNYAqhvlDYRxfhJhfDARBxfUjI2goc=; b=etbcRAP5oZxqUp4Dp0xXV70itDcjFF+jJ3uPXga1OAsM0gjwSZrzlalq2RKdSqiGN7jdbF 6Czj3zLjJPkosqBg== Date: Mon, 28 Apr 2025 14:40:14 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-13-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=3191; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=aEBBgnNmmm0oiSb4EWtCJKDiJ+chaLjNWUeDK6AsQpQ=; b=9g9Cx13ipSPIi5V1VBh7clhuvR0pcjkf/qwgM29Tm8PNBTpW32jfpxuVMkOyvxSaN8A5CJaez sdkfJBNZo/FCijDEIllRwxtAEbzjxnE/cxQATn2S0LvOuLkS7qcJxrC 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 Acked-by: Willy Tarreau --- tools/include/nolibc/stdio.h | 20 ++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index df5717d591826c8468e3473581ac0d6e76629aa3..61b7252ab6d04f8758367f52656= 87146326456a3 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -13,6 +13,7 @@ #include "std.h" #include "arch.h" #include "errno.h" +#include "fcntl.h" #include "types.h" #include "sys.h" #include "stdarg.h" @@ -55,6 +56,25 @@ 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; + + switch (*mode) { + case 'r': flags =3D O_RDONLY; break; + case 'w': flags =3D O_WRONLY | O_CREAT | O_TRUNC; break; + case 'a': flags =3D O_WRONLY | O_CREAT | O_APPEND; break; + default : SET_ERRNO(EINVAL); return NULL; + } + + if (mode[1] =3D=3D '+') + flags =3D (flags & ~(O_RDONLY|O_WRONLY)) | O_RDWR; + + 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 d1157319b5d715dbc94ff9203b1d2a2e932aabcf..0391c7d01380ea2f20d1d07497e= a1964bcb6a9f4 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -859,6 +859,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; @@ -1311,6 +1334,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:46 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 C310826FA77; Mon, 28 Apr 2025 12:40:17 +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=1745844019; cv=none; b=ZRTTjtN5otir5JHS4QIvvCkbwx0P8gGSHdxO1GUJrB/6FZNBfNQ7uJ0FIg7xtgFaWxjsCm+Sj8SO8X160xsjTUi/ABhL42caaK+mNxRb3w96gIhO1JvuHK1ZesX4NhmaTWwHT56krDnNoXWgdXXRwA+2rsp0I0bLeILtoOry4Dw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844019; c=relaxed/simple; bh=uR1uMvXkwy1U5+i9SR6QfCR7GUaSyCdxeSM6QC/XKuI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MXGil2CRxqDFPf17TcMHoilDosMhMIUQSXu9n1+xxBraEMykt/i5z/ZvS26NIuA+hzbNV9YExgvSpsUJaH4feEbdP77cOniHuI/lughWXV16SjoNvEF5hhHKjsO9U7KW7cdcbhnbyU9Xq6BbO2gI9tzCYO9WoGQ7nkuTnuSStpA= 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=IAcLD5+S; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=XwKjZS7C; 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="IAcLD5+S"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="XwKjZS7C" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844016; 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=3v9eG5lyR1SK4v03tlBXazUzZEzaRroYU5b0iRkhlHw=; b=IAcLD5+S6eZkQCbPl8qMeB1FvhwdYq3Fi5Alodl9KhQvVEShZ+4lSrUtZGA6LrmAMaIEYj UHXVJeWSBWcDXbxbePjp7FlWAMDGclDZzknLLVPoiQGwoAPw5dPtnO/wyC3BzZcEcE8dFN f6aKPtFvZ80bO7xpMB+gPkY3kso3qEDvrti+wqaBDBz3NyF77OZ+6HsDP2OPCHv7NQ1EMK BsXlphvpvb84iw691rpFMCtuesl/sXhwRIqD9gUVvcoo6yBg3r55DI+TdIJOOfMl5+WtbO jFnokUl/8O2uQIfT1rGDa3J5A3EPnml+jP9kIBzmu9TzWMC3kmgQeGyGOG80SQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844016; 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=3v9eG5lyR1SK4v03tlBXazUzZEzaRroYU5b0iRkhlHw=; b=XwKjZS7CnuewFlLx5T3J//xJ8PMWgPbk1Lu9I49cR8+WX2nNpARew/EQeKPUA6wa95ZWRs ZVgsASIFmiHKRCBQ== Date: Mon, 28 Apr 2025 14:40:15 +0200 Subject: [PATCH v2 14/15] tools/nolibc: 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: <20250428-nolibc-misc-v2-14-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=1463; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=uR1uMvXkwy1U5+i9SR6QfCR7GUaSyCdxeSM6QC/XKuI=; b=LeCfvd2ShVjqzQbWqsTNhYtwd85Lan7zthLtgUXXABK2wSuCCbELnNAtMTVPFJz0Ao7im5IBB UkJNRxJ86xjAo40zrYoxa6N5nYgJsgDfjanibZAfnTAoUXRaDkB7qYd 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 Acked-by: Willy Tarreau --- 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 785961c52fa3b6606ee30c19a4a1c11e86ccacc0..33782a19aae92907ccc636a656f= c0f27b838dd61 100644 --- a/tools/include/nolibc/sys/time.h +++ b/tools/include/nolibc/sys/time.h @@ -13,6 +13,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); */ @@ -23,7 +25,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:46 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 CA49F26FA78; Mon, 28 Apr 2025 12:40:17 +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=1745844019; cv=none; b=J4HMlUDxEYXCZ13KQbG7Yjit1KuMeKq6t/crQnwAas54xrcITqyWMligK31nRZWg2bVotVE0wlbSXifbkiedt4p30CexViLR2OF01AWfE3V3cRod7Ib8IYXPk4o4XwgoHE87mc6dDzrubeHsh4skbyMp+m+O3GppJ/qZPpLGVLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745844019; c=relaxed/simple; bh=HQLcfa8CaI5ExDZqkRSeOEeJwuLKsNYi1GOJJ28A+8k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Q20DSRWMuG4HZMCKLs41dTDQGHLOR3U/0qkKrw/sfZXwnnOX9aft1aRpzMCDmBEVt1hG09NnA44/45cJqyonh2nkzZDkXH/gbkTppDCvCeuJrA3SwnopFaLfrMaf2XeYdJIk8pNs71RAk4mzONhhx1P42znly+SHvTV+qON5cwA= 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=TDZbsVYD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UD1wW/mG; 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="TDZbsVYD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UD1wW/mG" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1745844016; 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=F/pw8wmkd0xKcovIdkDVLZ9oevdZmMFufDwKnizO/wc=; b=TDZbsVYDviz8q+3+gRWPWagV90ztMeLDLom8jk5F3RmlRHLWwxjt4uYOtWMuJrs0t/NP50 VNJb0pLRtI7edvyJEV4XxP84eissUfGnWAcuQ0rYw+s8xKdg/kL3sr7ZLNwaIOUY5ACiY5 VqKp3zAuWxtVPjpE397ITycHDv7ll/QXc7ANQW2LqCmm6Q1ir/nzsTnVduxLUPRP7rOwJy OB2XnZ2WV0Ru0wXv9ImQUQyCFyOv30XwjLOHH/9giIDu8nCFicw3BiF1binGZvikAzRVAD psH4HncwrEJ/TWa+R+BF6UbQBlbZ73ZRCRZrJpZXF3AVvo5+E6G474UAS5oCIw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1745844016; 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=F/pw8wmkd0xKcovIdkDVLZ9oevdZmMFufDwKnizO/wc=; b=UD1wW/mGQrDhfY1TKM5SshCLjY1HXUbma+nTJqYOBYNXEEvPo01edTscho8FCGKLEHOsuT ZiajE+r1WqyzT3CQ== Date: Mon, 28 Apr 2025 14:40:16 +0200 Subject: [PATCH v2 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: <20250428-nolibc-misc-v2-15-3c043eeab06c@linutronix.de> References: <20250428-nolibc-misc-v2-0-3c043eeab06c@linutronix.de> In-Reply-To: <20250428-nolibc-misc-v2-0-3c043eeab06c@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=1745844007; l=1268; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=HQLcfa8CaI5ExDZqkRSeOEeJwuLKsNYi1GOJJ28A+8k=; b=r3v1ZxwU6rznEjDY9nXpXALVJJQXvujCbkZBw2Oqf8gwunfOEffDHSL7EhlXLv/yBpyTsT7y2 g10hNVhfQZsCiBZy3vGa1obxCRVZRL7XDn4XL3CT7c3q0GKRu7XShC5 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 Acked-by: Willy Tarreau --- 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 f27be86ad5e480f3817b4500ac42fae445cdfc27..4d44e3da0ba814d00368027d893= e2eb1155b86f3 100644 --- a/tools/include/nolibc/sys/wait.h +++ b/tools/include/nolibc/sys/wait.h @@ -31,12 +31,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) { @@ -113,4 +107,10 @@ 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); +} + #endif /* _NOLIBC_SYS_WAIT_H */ --=20 2.49.0