libbb: Enable support for !CONFIG_MULTIUSER
[oweals/busybox.git] / libbb / change_identity.c
index 619db09a85e211e2c543e72d08cbdd3d66cbe431..d48d863264081521c974f70dc5c039c17293303e 100644 (file)
 /* Become the user and group(s) specified by PW.  */
 void FAST_FUNC change_identity(const struct passwd *pw)
 {
-       if (initgroups(pw->pw_name, pw->pw_gid) == -1)
-               bb_perror_msg_and_die("can't set groups");
+       int res;
+
+       res = initgroups(pw->pw_name, pw->pw_gid);
        endgrent(); /* helps to close a fd used internally by libc */
+
+       if (res != 0) {
+               /*
+                * If initgroups() fails because a system call is unimplemented
+                * then we are running on a Linux kernel compiled without multiuser
+                * support (CONFIG_MULTIUSER is not defined).
+                *
+                * If we are running without multiuser support *and* the target uid
+                * already matches the current uid then we can skip the change of
+                * identity.
+                */
+               if (errno == ENOSYS && pw->pw_uid == getuid()) {
+                       return;
+               }
+
+               bb_perror_msg_and_die("can't set groups");
+       }
+
        xsetgid(pw->pw_gid);
        xsetuid(pw->pw_uid);
 }