From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v9 1/2] lib: New library function tst_get_free_uid
Date: Tue, 7 Jul 2026 02:29:41 +0000 [thread overview]
Message-ID: <20260707022959.24088-2-wegao@suse.com> (raw)
In-Reply-To: <20260707022959.24088-1-wegao@suse.com>
Add tst_get_free_uid() to dynamically find unused UIDs for tests.
Signed-off-by: Wei Gao <wegao@suse.com>
---
include/tst_uid.h | 7 +++++--
lib/tst_uid.c | 26 ++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/include/tst_uid.h b/include/tst_uid.h
index 2237ddcbf..394cb3edc 100644
--- a/include/tst_uid.h
+++ b/include/tst_uid.h
@@ -8,10 +8,13 @@
#include <sys/types.h>
/*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/gid. The skip argument can be used to ignore e.g. the main
* group of a specific user in case it's not listed in the group file. If you
- * do not need to skip any specific gid, simply set it to 0.
+ * do not need to skip any specific id, simply set it to 0.
*/
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#define tst_get_free_uid(skip) tst_get_free_uid_(__FILE__, __LINE__, (skip))
+
gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip);
#define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
diff --git a/lib/tst_uid.c b/lib/tst_uid.c
index af4ef8cf7..deff17085 100644
--- a/lib/tst_uid.c
+++ b/lib/tst_uid.c
@@ -5,6 +5,7 @@
#include <sys/types.h>
#include <grp.h>
+#include <pwd.h>
#include <errno.h>
#define TST_NO_DEFAULT_MAIN
@@ -12,6 +13,31 @@
#include "tst_uid.h"
#define MAX_GID 32767
+#define MAX_UID 32767
+
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip)
+{
+ uid_t ret;
+
+ errno = 0;
+
+ for (ret = 1; ret < MAX_UID; ret++) {
+ if (ret == skip || getpwuid(ret))
+ continue;
+
+ if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+ tst_res_(file, lineno, TINFO | TERRNO,
+ "Found unused UID %d", (int)ret);
+ return ret;
+ }
+
+ tst_brk_(file, lineno, TBROK|TERRNO, "User ID lookup failed");
+ return (uid_t)-1;
+ }
+
+ tst_brk_(file, lineno, TBROK, "No free user ID found");
+ return (uid_t)-1;
+}
gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
{
--
2.54.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-07-07 2:30 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-09 11:20 [LTP] [PATCH v1] open15: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2023-12-08 15:34 ` Cyril Hrubis
2023-12-27 13:05 ` [LTP] [PATCH v2] " Wei Gao via ltp
2024-02-26 13:37 ` Cyril Hrubis
2024-06-03 12:55 ` [LTP] [PATCH v3] " Wei Gao via ltp
2025-02-21 10:01 ` Andrea Cervesato via ltp
2025-03-19 14:23 ` [LTP] [PATCH v4] open16: " Wei Gao via ltp
2025-07-11 12:04 ` Cyril Hrubis
2025-07-23 15:46 ` [LTP] [PATCH v5] " Wei Gao via ltp
2026-02-18 13:21 ` Andrea Cervesato via ltp
2026-04-15 6:07 ` [LTP] [PATCH v6 0/2] " Wei Gao via ltp
2026-04-15 6:07 ` [LTP] [PATCH v6 1/2] lib: New library function tst_get_free_uid Wei Gao via ltp
2026-04-15 7:54 ` [LTP] " linuxtestproject.agent
2026-04-15 6:07 ` [LTP] [PATCH v6 2/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-04-15 10:37 ` [LTP] [PATCH v6 0/2] " Wei Gao via ltp
2026-04-15 10:37 ` [LTP] [PATCH v7 1/2] lib: New library function tst_get_free_uid Wei Gao via ltp
2026-04-15 11:08 ` [LTP] " linuxtestproject.agent
2026-04-16 0:55 ` [LTP] [PATCH v8 0/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-04-16 0:55 ` [LTP] [PATCH v8 1/2] lib: New library function tst_get_free_uid Wei Gao via ltp
2026-04-16 2:43 ` [LTP] " linuxtestproject.agent
2026-04-16 4:15 ` Wei Gao via ltp
2026-04-16 0:55 ` [LTP] [PATCH v8 2/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-05-06 13:16 ` Andrea Cervesato via ltp
2026-07-07 2:29 ` [LTP] [PATCH v9 0/2] " Wei Gao via ltp
2026-07-07 2:29 ` Wei Gao via ltp [this message]
2026-07-07 3:34 ` [LTP] lib: New library function tst_get_free_uid linuxtestproject.agent
2026-07-07 2:29 ` [LTP] [PATCH v9 2/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-04-15 10:37 ` [LTP] [PATCH v7 " Wei Gao via ltp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260707022959.24088-2-wegao@suse.com \
--to=ltp@lists.linux.it \
--cc=wegao@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox