cbc4cf
From 0aa9097f9ecee3688b8659d7f7414f5fb26e147a Mon Sep 17 00:00:00 2001
cbc4cf
From: Karel Zak <kzak@redhat.com>
cbc4cf
Date: Mon, 13 Aug 2018 14:16:28 +0200
cbc4cf
Subject: [PATCH 6/6] lslogins: return 1 on "lslogins nonexisting"
cbc4cf
cbc4cf
The default behavior for -l and -g is to silently ignore unknown login
cbc4cf
names, but this is very confusing when you explicitly specify just one
cbc4cf
login name.
cbc4cf
cbc4cf
Note that the current implementation also prints empty "Last log" for
cbc4cf
nonexisting user. It seems ugly.
cbc4cf
cbc4cf
 # lslogins nonexisting
cbc4cf
cbc4cf
 Last logs:
cbc4cf
cbc4cf
new version:
cbc4cf
cbc4cf
 # lslogins nonexisting
cbc4cf
 lt-lslogins: cannot found 'nonexisting'
cbc4cf
 # echo $?
cbc4cf
 1
cbc4cf
cbc4cf
The -l and -g behaviour has not been changed.
cbc4cf
cbc4cf
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1614967
cbc4cf
Signed-off-by: Karel Zak <kzak@redhat.com>
cbc4cf
---
cbc4cf
 login-utils/lslogins.1 |  3 ++-
cbc4cf
 login-utils/lslogins.c | 16 +++++++++++++---
cbc4cf
 2 files changed, 15 insertions(+), 4 deletions(-)
cbc4cf
cbc4cf
diff --git a/login-utils/lslogins.1 b/login-utils/lslogins.1
cbc4cf
index effd42790..c831739d9 100644
cbc4cf
--- a/login-utils/lslogins.1
cbc4cf
+++ b/login-utils/lslogins.1
cbc4cf
@@ -18,7 +18,8 @@ and output the desired data.
cbc4cf
 The optional argument \fIusername\fR forces
cbc4cf
 .BR lslogins
cbc4cf
 to print all available details about the specified user only. In this case the
cbc4cf
-output format is different than in case of \fB\-l\fR or \fB\-g\fR.
cbc4cf
+output format is different than in case of \fB\-l\fR or \fB\-g\fR and unknown
cbc4cf
+is \fIusername\fR reported as an error.
cbc4cf
 
cbc4cf
 .PP
cbc4cf
 The default action is to list info about all the users in the system.
cbc4cf
diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c
cbc4cf
index 501778e54..6f804aa35 100644
cbc4cf
--- a/login-utils/lslogins.c
cbc4cf
+++ b/login-utils/lslogins.c
cbc4cf
@@ -266,6 +266,7 @@ struct lslogins_control {
cbc4cf
 	const char *journal_path;
cbc4cf
 
cbc4cf
 	unsigned int selinux_enabled : 1,
cbc4cf
+		     fail_on_unknown : 1,		/* fail if user does not exist */
cbc4cf
 		     ulist_on : 1,
cbc4cf
 		     noheadings : 1,
cbc4cf
 		     notrunc : 1;
cbc4cf
@@ -653,6 +654,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
cbc4cf
 	uid_t uid;
cbc4cf
 	errno = 0;
cbc4cf
 
cbc4cf
+	errno = 0;
cbc4cf
 	pwd = username ? getpwnam(username) : getpwent();
cbc4cf
 	if (!pwd)
cbc4cf
 		return NULL;
cbc4cf
@@ -675,6 +677,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
cbc4cf
 		return NULL;
cbc4cf
 	}
cbc4cf
 
cbc4cf
+	errno = 0;
cbc4cf
 	grp = getgrgid(pwd->pw_gid);
cbc4cf
 	if (!grp)
cbc4cf
 		return NULL;
cbc4cf
@@ -965,10 +968,16 @@ static int create_usertree(struct lslogins_control *ctl)
cbc4cf
 
cbc4cf
 	if (ctl->ulist_on) {
cbc4cf
 		for (n = 0; n < ctl->ulsiz; n++) {
cbc4cf
-			if (get_user(ctl, &user, ctl->ulist[n]))
cbc4cf
+			int rc = get_user(ctl, &user, ctl->ulist[n]);
cbc4cf
+
cbc4cf
+			if (ctl->fail_on_unknown && !user) {
cbc4cf
+				warnx(_("cannot found '%s'"), ctl->ulist[n]);
cbc4cf
+				return -1;
cbc4cf
+			}
cbc4cf
+			if (rc || !user)
cbc4cf
 				continue;
cbc4cf
-			if (user) /* otherwise an invalid user name has probably been given */
cbc4cf
-				tsearch(user, &ctl->usertree, cmp_uid);
cbc4cf
+
cbc4cf
+			tsearch(user, &ctl->usertree, cmp_uid);
cbc4cf
 		}
cbc4cf
 	} else {
cbc4cf
 		while ((user = get_next_user(ctl)))
cbc4cf
@@ -1518,6 +1527,7 @@ int main(int argc, char *argv[])
cbc4cf
 			errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
cbc4cf
 		logins = argv[optind];
cbc4cf
 		outmode = OUT_PRETTY;
cbc4cf
+		ctl->fail_on_unknown = 1;
cbc4cf
 	} else if (argc != optind)
cbc4cf
 		errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
cbc4cf
 
cbc4cf
-- 
cbc4cf
2.14.4
cbc4cf