public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 03/12] last: fix is_phantom() detection
Date: Sun, 27 Apr 2014 21:05:29 +0100	[thread overview]
Message-ID: <1398629138-31718-4-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1398629138-31718-1-git-send-email-kerolasa@iki.fi>

The /proc/<pid>/loginuid is not always available, and when so a running
session should not be determined to be gone.  This is a regression from
commit mentioned in reference.

Sessions that have started before previous system boot, and did not log
out meanwhile, will be marked as gone.  It is fair to say that these
sessions are most likely result of a wtmp corruption.

Reference: 404fa3f93c00c7e130f5a0ec963b2dc6a3743986
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/last.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/login-utils/last.c b/login-utils/last.c
index 5122f8e..ed10586 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -94,6 +94,7 @@ struct last_control {
 	unsigned int altc;	/* Number of alternative files */
 	unsigned int alti;	/* Index number of the alternative file */
 
+	struct timeval boot_time; /* system boot time */
 	time_t since;		/* at what time to start displaying the file */
 	time_t until;		/* at what time to stop displaying the file */
 	time_t present;		/* who where present at time_t */
@@ -573,19 +574,24 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-static int is_phantom(struct utmp *ut)
+static int is_phantom(const struct last_control *ctl, struct utmp *ut)
 {
 	struct passwd *pw;
 	char path[32];
 	FILE *f;
-	unsigned int loginuid, ret = 0;
+	unsigned int loginuid;
+	int ret = 0;
 
+	if (ut->UL_UT_TIME < ctl->boot_time.tv_sec)
+		return 1;
 	pw = getpwnam(ut->ut_name);
 	if (!pw)
 		return 1;
 	sprintf(path, "/proc/%u/loginuid", ut->ut_pid);
-	if (!(f = fopen(path, "r")))
+	if (access(path, R_OK) == 0 && !(f = fopen(path, "r")))
 		return 1;
+	else
+		return ret;
 	if (fscanf(f, "%u", &loginuid) != 1)
 		ret = 1;
 	fclose(f);
@@ -787,7 +793,7 @@ static void process_wtmp_file(const struct last_control *ctl)
 				if (!lastboot) {
 					c = R_NOW;
 					/* Is process still alive? */
-					if (is_phantom(&ut))
+					if (is_phantom(ctl, &ut))
 						c = R_PHANTOM;
 				} else
 					c = whydown;
@@ -980,6 +986,7 @@ int main(int argc, char **argv)
 	}
 
 	for (; ctl.alti < ctl.altc; ctl.alti++) {
+		get_boot_time(&ctl.boot_time);
 		process_wtmp_file(&ctl);
 		free(ctl.altv[ctl.alti]);
 	}
-- 
1.9.2


  parent reply	other threads:[~2014-04-27 20:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-27 20:05 [PATCH 00/12] pull: mostly swap command clarifications Sami Kerola
2014-04-27 20:05 ` [PATCH 01/12] include/xalloc: ensure arithmetics overflow cannot happen Sami Kerola
2014-04-27 20:45   ` Bernhard Voelker
2014-04-28  6:52   ` Karel Zak
2014-04-28  8:42     ` Sami Kerola
2014-04-27 20:05 ` [PATCH 02/12] dmesg: move get_boot_time() to lib/timeutils Sami Kerola
2014-05-06 10:00   ` Ruediger Meier
2014-05-06 11:20     ` Karel Zak
2014-05-06 13:42       ` Ruediger Meier
2014-05-06 14:40         ` Karel Zak
2014-05-06 16:03           ` Ruediger Meier
2014-05-07  9:52             ` Karel Zak
2014-05-07 14:04               ` Ruediger Meier
2014-04-27 20:05 ` Sami Kerola [this message]
2014-04-27 20:05 ` [PATCH 04/12] include/c.h: add macro to print definitions as string Sami Kerola
2014-04-27 20:05 ` [PATCH 05/12] mkswap, swaplabel: move version number to header Sami Kerola
2014-04-27 20:05 ` [PATCH 06/12] mkswap: remove legacy swap structure Sami Kerola
2014-04-27 20:05 ` [PATCH 07/12] include/swapheader.h: ensure type sizes Sami Kerola
2014-04-27 20:05 ` [PATCH 08/12] swapon: swaps with legacy version label are not supported Sami Kerola
2014-04-28  8:37   ` Benno Schulenberg
2014-04-28  8:44     ` Sami Kerola
2014-04-28  9:00       ` Karel Zak
2014-04-27 20:05 ` [PATCH 09/12] swapon, swapheader, mkswap: move swap signature to header Sami Kerola
2014-04-27 20:05 ` [PATCH 10/12] libsmartcols: remove ununsed assignment Sami Kerola
2014-04-28  9:02   ` Karel Zak
2014-04-27 20:05 ` [PATCH 11/12] lib/timeutils: fix memory leak Sami Kerola
2014-04-27 20:05 ` [PATCH 12/12] lib/pager: use names when referring to standard file descriptors Sami Kerola
2014-04-29 21:05 ` [PATCH 00/12] pull: mostly swap command clarifications Sami Kerola
2014-04-29 21:46   ` Bernhard Voelker
2014-05-06  8:36 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1398629138-31718-4-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox