Brian May <brian(a)microcomaustralia.com.au> wrote:
In my rewrite come across a sticky problem: how do I
reliably and
efficiently allocate unique uidNumber for new users and gidNumber of
new groups?
The current solution, as used by my predecessor, is to list every user
or group in the system, sort or scan though the list looking for the
highest id, add 1, and use that.
Unfortunately, this seems to be lacking in efficiency (specially if
there are a lot of users) and relying on the hope that two users will
never be created at the same time. Race conditions could occur.
If you're mainly worried that your tool could end up creating two users at
once, you could use locking to prevent it, I suppose.
If you're concerned that someone might run useradd/groupadd at the same time
as your tool is operating, I don't see how this could be prevented easily
other than opening and locking /etc/passwd or /etc/group.
Is there a good reason not simply to spawn useradd/groupadd and let them
allocate the ids?
Is it safe to assume that uids/gids appear in ascending order in /etc/passwd
and /etc/group? In general, probably not, especially as an administrator might
edit those files, so that's out.