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