]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/execute: modernize get_fixed_{user,group}
authorMike Yuan <me@yhndnzj.com>
Sun, 8 Oct 2023 05:44:37 +0000 (13:44 +0800)
committerMike Yuan <me@yhndnzj.com>
Mon, 9 Oct 2023 15:40:58 +0000 (23:40 +0800)
No functional change, preparation for later commit.

src/core/execute.c

index dc7f17f09817e5e1ce7847af5d62be88332fe9ac..04f6bcd703eb8d05ac5264576eeaa324bda7171d 100644 (file)
@@ -989,44 +989,45 @@ restore_stdio:
         return r;
 }
 
-static int get_fixed_user(const ExecContext *c, const char **user,
-                          uid_t *uid, gid_t *gid,
-                          const char **home, const char **shell) {
-        int r;
-        const char *name;
+static int get_fixed_user(
+                const char *username,
+                const char **ret_user,
+                uid_t *ret_uid,
+                gid_t *ret_gid,
+                const char **ret_home,
+                const char **ret_shell) {
 
-        assert(c);
+        int r;
 
-        if (!c->user)
-                return 0;
+        assert(username);
+        assert(ret_user);
 
         /* Note that we don't set $HOME or $SHELL if they are not particularly enlightening anyway
          * (i.e. are "/" or "/bin/nologin"). */
 
-        name = c->user;
-        r = get_user_creds(&name, uid, gid, home, shell, USER_CREDS_CLEAN);
+        r = get_user_creds(&username, ret_uid, ret_gid, ret_home, ret_shell, USER_CREDS_CLEAN);
         if (r < 0)
                 return r;
 
-        *user = name;
+        *ret_user = username;
         return 0;
 }
 
-static int get_fixed_group(const ExecContext *c, const char **group, gid_t *gid) {
-        int r;
-        const char *name;
+static int get_fixed_group(
+                const char *groupname,
+                const char **ret_group,
+                gid_t *ret_gid) {
 
-        assert(c);
+        int r;
 
-        if (!c->group)
-                return 0;
+        assert(groupname);
+        assert(ret_group);
 
-        name = c->group;
-        r = get_group_creds(&name, gid, 0);
+        r = get_group_creds(&groupname, ret_gid, /* flags = */ 0);
         if (r < 0)
                 return r;
 
-        *group = name;
+        *ret_group = groupname;
         return 0;
 }
 
@@ -4153,16 +4154,20 @@ static int exec_child(
                         username = runtime->dynamic_creds->user->name;
 
         } else {
-                r = get_fixed_user(context, &username, &uid, &gid, &home, &shell);
-                if (r < 0) {
-                        *exit_status = EXIT_USER;
-                        return log_unit_error_errno(unit, r, "Failed to determine user credentials: %m");
+                if (context->user) {
+                        r = get_fixed_user(context->user, &username, &uid, &gid, &home, &shell);
+                        if (r < 0) {
+                                *exit_status = EXIT_USER;
+                                return log_unit_error_errno(unit, r, "Failed to determine user credentials: %m");
+                        }
                 }
 
-                r = get_fixed_group(context, &groupname, &gid);
-                if (r < 0) {
-                        *exit_status = EXIT_GROUP;
-                        return log_unit_error_errno(unit, r, "Failed to determine group credentials: %m");
+                if (context->group) {
+                        r = get_fixed_group(context->group, &groupname, &gid);
+                        if (r < 0) {
+                                *exit_status = EXIT_GROUP;
+                                return log_unit_error_errno(unit, r, "Failed to determine group credentials: %m");
+                        }
                 }
         }