]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: do not rely only getlogin(3) telling who ran the command
authorSami Kerola <kerolasa@iki.fi>
Sun, 22 Jun 2014 22:29:36 +0000 (23:29 +0100)
committerSami Kerola <kerolasa@iki.fi>
Mon, 28 Jul 2014 20:15:16 +0000 (21:15 +0100)
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 <kerolasa@iki.fi>
misc-utils/logger.c

index 0c6dbbdd208f13e1340c6afc9c44cb1aa704e515..5204cef55f26990c8364a1487c4a12cb869bc7ad 100644 (file)
@@ -50,6 +50,7 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <getopt.h>
+#include <pwd.h>
 
 #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 : "<someone>";
+       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 = "<someone>";
-               }
+               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) {