[PATCH 03/10] logger: do not rely only getlogin(3) telling who ran the command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxx>
---
 misc-utils/logger.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 204511d..65e468c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -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) {
-- 
2.0.2

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux