From: Sami Kerola Date: Sun, 22 Jun 2014 22:29:36 +0000 (+0100) Subject: logger: do not rely only getlogin(3) telling who ran the command X-Git-Tag: v2.26-rc1~576^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=019b97024fde3f07eaf541eef990762483369a11;p=thirdparty%2Futil-linux.git logger: do not rely only getlogin(3) telling who ran the command The getlogin(3) is known not to always work, and when that happens it is reasonable to try determine user of name by looking process owner and passwd information. Reference: http://man7.org/linux/man-pages/man3/getlogin.3.html Signed-off-by: Sami Kerola --- diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 0c6dbbdd20..5204cef55f 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -50,6 +50,7 @@ #include #include #include +#include #include "all-io.h" #include "c.h" @@ -257,6 +258,16 @@ static int journald_entry(FILE *fp) } #endif +static char *xgetlogin() +{ + char *cp; + struct passwd *pw; + + if (!(cp = getlogin()) || !*cp) + cp = (pw = getpwuid(geteuid()))? pw->pw_name : ""; + return cp; +} + static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg) { char buf[1000], pid[30], *cp, *tp; @@ -269,11 +280,8 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg) pid[0] = 0; if (tag) cp = tag; - else { - cp = getlogin(); - if (!cp) - cp = ""; - } + else + cp = xgetlogin(); time(&now); tp = ctime(&now) + 4; @@ -438,7 +446,7 @@ int main(int argc, char **argv) else if (usock) LogSock = unix_socket(usock, socket_type); else - openlog(tag ? tag : getlogin(), logflags, 0); + openlog(tag ? tag : xgetlogin(), logflags, 0); /* log input line if appropriate */ if (argc > 0) {