summaryrefslogtreecommitdiff
path: root/src/su.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2011-11-20 14:08:33 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2011-11-20 14:09:53 -0800
commit71b7ddcdd5c473f9eaf6035b96acc68c14cb379e (patch)
treea4d7544dc4ed5123a2ffaf40105fc8d0049ddf22 /src/su.c
parent8ffc159611db3443282aee42465afc8a1597519c (diff)
downloadcoreutils-71b7ddcdd5c473f9eaf6035b96acc68c14cb379e.tar.xz
port to GNU hosts, where getuid and friends can fail
* src/groups.c (main): * src/install.c (need_copy): * src/su.c (log_su): * src/test.c (unary_operator): * src/whoami.c (main): Don't assume that getuid and friends always succeed. This fixes the same problem that we recently fixed with 'id'.
Diffstat (limited to 'src/su.c')
-rw-r--r--src/su.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/su.c b/src/su.c
index 081ecb2eb..b1ba2a7b7 100644
--- a/src/su.c
+++ b/src/su.c
@@ -173,7 +173,10 @@ log_su (struct passwd const *pw, bool successful)
{
/* getlogin can fail -- usually due to lack of utmp entry.
Resort to getpwuid. */
- struct passwd *pwd = getpwuid (getuid ());
+ errno = 0;
+ uid_t ruid = getuid ();
+ uid_t NO_UID = -1;
+ struct passwd *pwd = (ruid == NO_UID && errno ? NULL : getpwuid (ruid));
old_user = (pwd ? pwd->pw_name : "");
}
tty = ttyname (STDERR_FILENO);