]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
unshare: use the new ul_get{grp,userpw}_str() routines
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Fri, 28 Nov 2025 02:32:41 +0000 (21:32 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 2 Dec 2025 05:45:31 +0000 (00:45 -0500)
This change refactors get_group() and get_user(), so that it
uses the new routines ul_getgrp_str() and ul_getuserpw_str(),
to simplify the code and remove the overkill mem allocations.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
sys-utils/unshare.c

index 11aeae48ed58d0c83144cdddd347635a696f4418..0671ed3e1a8a85a32ce805ac925b5fbde9a92356 100644 (file)
@@ -322,40 +322,24 @@ static pid_t bind_ns_files_from_child(int *fd)
        exit(EXIT_SUCCESS);
 }
 
-static uid_t get_user(const char *s, const char *err)
+static uid_t get_user(const char *s)
 {
        struct passwd *pw;
-       char *buf = NULL;
-       uid_t ret;
 
-       pw = xgetpwnam(s, &buf);
-       if (pw) {
-               ret = pw->pw_uid;
-               free(pw);
-               free(buf);
-       } else {
-               ret = strtoul_or_err(s, err);
-       }
-
-       return ret;
+       pw = ul_getuserpw_str(s);
+       if (!pw)
+               errx(EXIT_FAILURE, _("failed to parse uid '%s'"), s);
+       return pw->pw_uid;
 }
 
-static gid_t get_group(const char *s, const char *err)
+static gid_t get_group(const char *s)
 {
        struct group *gr;
-       char *buf = NULL;
-       gid_t ret;
-
-       gr = xgetgrnam(s, &buf);
-       if (gr) {
-               ret = gr->gr_gid;
-               free(gr);
-               free(buf);
-       } else {
-               ret = strtoul_or_err(s, err);
-       }
 
-       return ret;
+       gr = ul_getgrp_str(s);
+       if (!gr)
+               errx(EXIT_FAILURE, _("failed to parse gid '%s'"), s);
+       return gr->gr_gid;
 }
 
 /**
@@ -970,11 +954,11 @@ int main(int argc, char *argv[])
                        break;
                case OPT_MAPUSER:
                        unshare_flags |= CLONE_NEWUSER;
-                       mapuser = get_user(optarg, _("failed to parse uid"));
+                       mapuser = get_user(optarg);
                        break;
                case OPT_MAPGROUP:
                        unshare_flags |= CLONE_NEWUSER;
-                       mapgroup = get_group(optarg, _("failed to parse gid"));
+                       mapgroup = get_group(optarg);
                        break;
                case 'r':
                        unshare_flags |= CLONE_NEWUSER;