Blame SOURCES/0006-lslogins-return-1-on-lslogins-nonexisting.patch

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