summaryrefslogtreecommitdiffstats
path: root/login-utils/last.c
diff options
context:
space:
mode:
authorSami Kerola2014-04-24 00:32:11 +0200
committerSami Kerola2014-05-04 12:42:03 +0200
commit0e5218acd9ee73b0042489addf202a9b3cf799ed (patch)
treec6ce4b019bf046fb5f38db33854433cdff7f5a67 /login-utils/last.c
parentdmesg: move get_boot_time() to lib/timeutils (diff)
downloadkernel-qcow2-util-linux-0e5218acd9ee73b0042489addf202a9b3cf799ed.tar.gz
kernel-qcow2-util-linux-0e5218acd9ee73b0042489addf202a9b3cf799ed.tar.xz
kernel-qcow2-util-linux-0e5218acd9ee73b0042489addf202a9b3cf799ed.zip
last: fix is_phantom() detection
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>
Diffstat (limited to 'login-utils/last.c')
-rw-r--r--login-utils/last.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/login-utils/last.c b/login-utils/last.c
index 5122f8e4b..ed1058652 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]);
}