Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions man/wtmpdb.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-o, --open</option>
</term>
<listitem>
<para>
Display only sessions that are still open, i.e. no end of session has been recorded since last boot.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-p, --present</option> <replaceable>TIME</replaceable>
Expand Down
28 changes: 21 additions & 7 deletions src/wtmpdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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])
{
Expand All @@ -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)) ||
Expand Down Expand Up @@ -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'))
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'},
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down