user-util: add get{pw,gr}{uid,gid,name}_malloc() helpers

These are wrappers around getpwuid_r() and friends, and will allocate the
right-sized buffer for this call.

We so far had multiple implementations of a buffer allocation loop
around getpwuid_r() and friends, and they all suck in some way. Let's
clean this up and add a common implementation, and use it everywhere.

Also, be more careful with error numbers, in particular systematically
turn ENOENT into ENOSRCH (the former is what is returned if /etc/passwd
is absent, which we want to consider identical to user not existing,
which is ENOSRCH). We so far did this at some invocations, but not all.

There are some invocations of getpwuid() left in the codebase. We really
should fix those too, and have a single unified implementation of the
logic, but those are not as trivial to convert, so left for another
time.
This commit is contained in:
Lennart Poettering
2024-01-22 17:55:07 +01:00
parent 829854afa5
commit 75673cd8ae
10 changed files with 311 additions and 255 deletions
+4 -5
View File
@@ -1079,12 +1079,11 @@ static int process_root_account(int rfd) {
return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
if (arg_copy_root_shell && k == 0) {
struct passwd *p;
_cleanup_free_ struct passwd *p = NULL;
errno = 0;
p = getpwnam("root");
if (!p)
return log_error_errno(errno_or_else(EIO), "Failed to find passwd entry for root: %m");
r = getpwnam_malloc("root", &p);
if (r < 0)
return log_error_errno(r, "Failed to find passwd entry for root: %m");
r = free_and_strdup(&arg_root_shell, p->pw_shell);
if (r < 0)