From: Petr Vorel <pvorel@suse.cz>
To: Wei Gao <wegao@suse.com>,
ltp@lists.linux.it, Andrea Cervesato <andrea.cervesato@suse.com>,
Martin Doucha <martin.doucha@suse.com>
Subject: Re: [LTP] [PATCH v16 1/2] lib: New library function tst_get_free_uid
Date: Thu, 22 Oct 2026 17:32:49 +0200 [thread overview]
Message-ID: <20261022153249.GB405225@pevik> (raw)
In-Reply-To: <20261022152542.GA405225@pevik>
> Hi Wei,
> > +uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
> > +
> > +/**
> > + * tst_get_free_uid() - Find a UID not assigned to any user.
> > + * @skip: UID value to skip (pass 0 to skip none).
> > + *
> > + * Scans the password database for the first unused UID starting
> > + * from 1, skipping @skip. Calls tst_brk(TBROK) if no free UID
> > + * is found or a lookup error occurs.
> > + *
> > + * Return: An unused uid_t value.
> > + */
> > +#define tst_get_free_uid(skip) tst_get_free_uid_(__FILE__, __LINE__, (skip))
> > +
> > /*
> > * Find unassigned 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
> > diff --git a/lib/tst_uid.c b/lib/tst_uid.c
> > index b0b087362..47c267bc4 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,33 @@
> > #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;
> > +
> > + for (ret = 1; ret < MAX_UID; ret++) {
> > + if (ret == skip)
> > + continue;
> > +
> > + errno = 0;
> > + if (getpwuid(ret))
> > + continue;
> If you don't mind I'd prefer to use the same approach which is on
> tst_get_free_gid_(), e.g. set errno first:
> errno = 0;
> for (ret = 1; ret < MAX_UID; ret++) {
> if (ret == skip || getpwuid(ret))
> continue;
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> Other than that LGTM.
> Cc Martin in case he wants to add his ack before I merge.
> Kind regards,
> Petr
> > +
> > + if (errno == 0 || errno == ENOENT || errno == ESRCH) {
> > + tst_res_(file, lineno, TINFO,
Actually also here TERRNO probably makes sense.
I suggest to merge with following diff:
+++ lib/tst_uid.c
@@ -19,16 +19,14 @@ uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip)
{
uid_t ret;
- for (ret = 1; ret < MAX_UID; ret++) {
- if (ret == skip)
- continue;
+ errno = 0;
- errno = 0;
- if (getpwuid(ret))
+ 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,
+ tst_res_(file, lineno, TINFO | TERRNO,
"Found unused UID %d", (int)ret);
return ret;
}
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-10-22 15:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 2:28 [LTP] [PATCH v16 0/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-10-22 2:28 ` [LTP] [PATCH v16 1/2] lib: New library function tst_get_free_uid Wei Gao via ltp
2026-10-22 7:12 ` [LTP] " linuxtestproject.agent
2026-10-22 15:25 ` [LTP] [PATCH v16 1/2] " Petr Vorel
2026-10-22 15:32 ` Petr Vorel [this message]
2026-10-22 2:28 ` [LTP] [PATCH v16 2/2] open16: allow restricted O_CREAT of FIFOs and regular files Wei Gao via ltp
2026-10-22 15:44 ` Petr Vorel
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=20261022153249.GB405225@pevik \
--to=pvorel@suse.cz \
--cc=andrea.cervesato@suse.com \
--cc=ltp@lists.linux.it \
--cc=martin.doucha@suse.com \
--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