diff --git a/man/wtmpdb.8.xml b/man/wtmpdb.8.xml
index 1cd07ca..3a019d9 100644
--- a/man/wtmpdb.8.xml
+++ b/man/wtmpdb.8.xml
@@ -124,6 +124,16 @@
+
+
+
+
+
+
+ Display only sessions that are still open, i.e. no end of session has been recorded since last boot.
+
+
+
TIME
diff --git a/src/wtmpdb.c b/src/wtmpdb.c
index 4bd4671..50e268a 100644
--- a/src/wtmpdb.c
+++ b/src/wtmpdb.c
@@ -69,7 +69,7 @@ static char *wtmpdb_path = NULL;
#define LAST_TIMESTAMP_LEN 32
static uint64_t wtmp_start = UINT64_MAX;
-static int after_reboot = 0;
+static uint64_t after_reboot = 0;
/* options for last */
static int hostlast = 0;
@@ -80,6 +80,7 @@ static int iflag = 0;
static int jflag = 0;
static int wflag = 0;
static int xflag = 0;
+static int open_sessions = 0; /* show open sessions, only */
static const int name_len = 8; /* LAST_LOGIN_LEN */
static int login_fmt = TIMEFMT_SHORT;
static int login_len = 16; /* 16 = short, 24 = full */
@@ -451,6 +452,15 @@ print_entry (void *unused __attribute__((__unused__)),
|| (endptr == argv[3]) || (*endptr != '\0'))
fprintf (stderr, "Invalid numeric time entry for 'login': '%s'\n",
argv[3]);
+ if (login_t < wtmp_start)
+ wtmp_start = login_t;
+
+ /** Can't do it earlier because there is no contract that this function
+ * gets only invoked on lists sorted by Login time. Side effect is, that this
+ * way "wtmpdb begins ..." doesn't lie (as maxentries option does).
+ */
+ if (open_sessions && after_reboot > login_t)
+ return 0;
if (argv[4])
{
@@ -461,9 +471,6 @@ print_entry (void *unused __attribute__((__unused__)),
argv[4]);
}
- if (login_t < wtmp_start)
- wtmp_start = login_t;
-
int swap = type == xflag && BOOT_TIME && logout_t != 0;
if ((since && since > from_usec(swap ? logout_t : login_t)) ||
@@ -494,6 +501,8 @@ print_entry (void *unused __attribute__((__unused__)),
if (logout_t != 0)
{
+ if (open_sessions)
+ return 0;
logout_t = strtoull(argv[4], &endptr, 10);
if ((errno == ERANGE && logout_t == ULLONG_MAX)
|| (endptr == argv[4]) || (*endptr != '\0'))
@@ -511,7 +520,7 @@ print_entry (void *unused __attribute__((__unused__)),
}
else /* login but no logout */
{
- if (after_reboot)
+ if (after_reboot > login_t)
{
snprintf (times.logout, sizeof (times.logout), "crash");
times.length[0] = '\0';
@@ -555,7 +564,7 @@ print_entry (void *unused __attribute__((__unused__)),
if (type == BOOT_TIME)
{
tty = "system boot";
- after_reboot = 1;
+ after_reboot = login_t;
}
char *print_service = NULL;
@@ -658,6 +667,7 @@ usage (int retval)
fputs (" -i, --ip Translate hostnames to IP addresses\n", output);
fputs (" -j, --json Generate JSON output\n", output);
fputs (" -n, --limit N, -N Display only first N entries\n", output);
+ fputs (" -o, --open Display open sessions, only.\n", output);
fputs (" -p, --present TIME Display who was present at TIME\n", output);
fputs (" -R, --nohostname Don't display hostname\n", output);
fputs (" -S, --service Display PAM service used to login\n", output);
@@ -775,6 +785,7 @@ main_last (int argc, char **argv)
{"fulltimes", no_argument, NULL, 'F'},
{"ip", no_argument, NULL, 'i'},
{"limit", required_argument, NULL, 'n'},
+ {"open", no_argument, NULL, 'o'},
{"present", required_argument, NULL, 'p'},
{"nohostname", no_argument, NULL, 'R'},
{"service", no_argument, NULL, 'S'},
@@ -789,7 +800,7 @@ main_last (int argc, char **argv)
char *error = NULL;
int c;
- while ((c = getopt_long (argc, argv, "0123456789adf:Fijn:p:RSs:t:wx",
+ while ((c = getopt_long (argc, argv, "0123456789adf:Fijn:op:RSs:t:wx",
longopts, NULL)) != -1)
{
switch (c)
@@ -830,6 +841,9 @@ main_last (int argc, char **argv)
case 'n':
maxentries = strtoul (optarg, NULL, 10);
break;
+ case 'o':
+ open_sessions = 1;
+ break;
case 'p':
if (parse_time (optarg, &present) < 0)
{