From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 576863EFFA1 for ; Thu, 2 Jul 2026 08:51:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782982282; cv=none; b=c68tnmFNx3RXQirNnkg9ofOIZmUfE8Y3+mv+mohnBQZLo4oifigcuy5i4rfgdc4n1EM0qyuflkjw6bj8IUm0w4N9IvC/ce8DWV5BwEfkBAvZNka2bPoCLcVJmKsBwLUC9p8cbx1TLchTMeKlGPOLQy/Vm3CKbvS3Z45gMLFYZUs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782982282; c=relaxed/simple; bh=9z/Qx3Jc2qcBjt90F7N/TPGPlzJVi2WnI64HggnFLWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sj96Skl+inPm/DTFg8CGzpSazl2cPOXgQ9cpBjdIb+/N+ctCqm6DXTeY6yzM6uOxI4MaS+Jq8NLhrg/ySBpQmxbi8vclzLHIX93U7xYUcvwptrF/YigDs/teRGRAIkkDntsfLCOLtlViRx7RJ4KgPALnEYdWeUKVKq2nItnz0Fo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp; spf=pass smtp.mailfrom=0x0f.com; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b=Qn52LH2x; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=0x0f.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b="Qn52LH2x" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8423f236418so959478b3a.1 for ; Thu, 02 Jul 2026 01:51:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thingy.jp; s=google; t=1782982279; x=1783587079; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XCH8sFhVDc53jjHRToVZbYCacY4nX5P0fSDfz0uaTN4=; b=Qn52LH2xDe8ZGL5dN61QGzf6dsWka1hHRr41MgsNWMdfj0mGV1zLecAi4kHfJ3V+Hd 2xmo3TOGNhUKgbYY0xMzF2HpqgnZGUmtlI0n64ZOcRYRnhDyO2Y1EoTs23TEV1Yagf6M MbBOc8dALBUSCun4LLJe7rNrFAFNFZxU8nofQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782982279; x=1783587079; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=XCH8sFhVDc53jjHRToVZbYCacY4nX5P0fSDfz0uaTN4=; b=o9uemgMV+GqrjjZWL7UK2dwj1vPzXDoq1A5JEf1s12YGEthbd0iCFt3jxIHItyClfp Sbcab5tBtUfbUjVvsZc2/s1eXTWatWI9yXMWJvtFYha8eNhAMZSxOK5BOyjcG1kAL4tm YJIvEKX+Oy2iFD/RHwB3Gr1smi15oQH8E6hBew/vl2SL1YtyIRpFPEYAsGnN/YOUoT9y wWAukwSAidRNAJebAsYtYFruZWoLzJXDrM+QnflmjCLAkSVzrMxuGhHD9OM2I6S050Fa VuqYrmzJFDlu2xfpA/aG9fEa4FO7fc3UWY6yt4Aq9yz5GEW16ibQXlA4r4feXh4zafbx j+HQ== X-Gm-Message-State: AOJu0Yw8lT4qldjlvwZkHQBwLM4d+b4Hqv4d2lq9zJkEzlA+Bo2YLkWk 5gMCbvBKF19w0/d/PlsK6HEV94nCadFsb57+Kvwe+a3RhaUDu8Y0CRtY/Wx+SuLY9Vw= X-Gm-Gg: AfdE7ckaB5dW65yZNzCIgrL4RFhCA/WvWdNaWCfFJtcB44VFsmKNfqfDW/isYndIoO7 MCILtbaZR34qdYYRbCkW6M2YyrJQ2XhzIXt0BuViwt3WKRMhNDxPZhOFJIAcy9+GqPs8THIBfJP fd2fwn4ny3iYgEx5cDv8klxtkWZifM2Xi822uczroB387WIpEIjC3roc5kqb1ehuiNtLNOLVfgw WKjC6yrz+PQLdEeb/UvRySLol+rv14JIH2PMDzkuLkTC9F5x+gg0BKjYVV6qRSuEsI7CV5N3IEe 6+hxSzROQsYggLsl11h3c+NLs2zMvxU8ANHYco5pWOwvXGbL8g2nuWUQZUSSx8MbmI/OQHirBF3 ErcRzsE2x7c+jIJ89co9P0rrp9cpPu49DMphebgNk6jBZcOrEBtufHtHRXvEwFamRmc57hhlEFk bAjiv2Jv/C0gC3kpltWW9LryGoaw== X-Received: by 2002:a05:6a00:b46:b0:847:7f7a:dd18 with SMTP id d2e1a72fcca58-847c4ffbc09mr4351605b3a.3.1782982279236; Thu, 02 Jul 2026 01:51:19 -0700 (PDT) Received: from kinako.work.home.arpa ([2400:4162:2428:2ffe:a973:53e4:1a28:8545]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-c9e8f4cd4b1sm944649a12.12.2026.07.02.01.51.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 01:51:18 -0700 (PDT) From: Daniel Palmer To: w@1wt.eu, linux@weissschuh.net Cc: linux-kernel@vger.kernel.org, Daniel Palmer Subject: [PATCH v3 3/3] selftests/nolibc: Add test for getcwd() and readlink() Date: Thu, 2 Jul 2026 17:51:01 +0900 Message-ID: <20260702085101.3304547-4-daniel@thingy.jp> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260702085101.3304547-1-daniel@thingy.jp> References: <20260702085101.3304547-1-daniel@thingy.jp> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a test that uses both getcwd() and readlink() so that both are exercised. First the happy path is tested by fetching what should be the same string via getcwd() and readlink() and checking they match. Then a few different combinations of bad parameters are passed to getcwd() to make sure it returns NULL and sets errno in those cases. Signed-off-by: Daniel Palmer --- tools/testing/selftests/nolibc/nolibc-test.c | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index c1c1ce43a047..ad3f6e0751be 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -854,6 +854,58 @@ static int test_dirent(void) return 0; } +static int test_getcwd(void) +{ + char cwd_syscall[PATH_MAX]; + char cwd_proc[PATH_MAX]; + ssize_t len; + + /* Read where the link /proc/self/cwd points */ + len = readlink("/proc/self/cwd", cwd_proc, sizeof(cwd_proc) - 1); + if (len <= 0) + return __LINE__; + + /* Terminate the string from readlink() */ + cwd_proc[len] = '\0'; + + /* Get the cwd via syscall */ + if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL) + return __LINE__; + + /* Fail if they aren't the same */ + if (strcmp(cwd_proc, cwd_syscall) != 0) + return __LINE__; + + /* Try getcwd() with NULL for the buffer, + * should return NULL and an error in errno. + * Other libc's allow this by allocating a buffer + * internally. + */ + if (is_nolibc) { + errno = 0; + if (getcwd(NULL, 0) != NULL || !errno) + return __LINE__; + } + + /* Try getcwd() with a buffer but make the size 0, + * should return NULL and an error in errno. + */ + errno = 0; + if (getcwd(cwd_syscall, 0) != NULL || !errno) + return __LINE__; + + /* Try getcwd() with a buffer but make the size 1, + * should return NULL and an error in errno because + * the string written to the buffer is terminated + * so you need at least 2 bytes even for "/". + */ + errno = 0; + if (getcwd(cwd_syscall, 1) != NULL || !errno) + return __LINE__; + + return 0; +} + int test_getrandom(void) { uint64_t rng = 0; @@ -1555,6 +1607,7 @@ int run_syscall(int min, int max) CASE_TEST(clock_getres); EXPECT_SYSZR(1, clock_getres(CLOCK_MONOTONIC, &ts)); break; CASE_TEST(clock_gettime); EXPECT_SYSZR(1, clock_gettime(CLOCK_MONOTONIC, &ts)); break; CASE_TEST(clock_settime); EXPECT_SYSER(1, clock_settime(CLOCK_MONOTONIC, &ts), -1, EINVAL); break; + CASE_TEST(getcwd); EXPECT_SYSZR(proc, test_getcwd()); break; CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break; -- 2.53.0