commit 228958d476ae1e79cf988989a05ce4031c4ff10c
parent cb13a7eb30a22e4c5bb20f895775e112a5277f7f
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:45:45 -0300
who, last, lastlog, getty: records through session
Diffstat:
7 files changed, 179 insertions(+), 88 deletions(-)
diff --git a/cmd/posix/who.c b/cmd/posix/who.c
@@ -5,30 +5,45 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
-#include <utmp.h>
#include "config.h"
+#include "session.h"
#include "util.h"
+static int mflag, lflag;
+static char *tty;
+
static void
usage(void)
{
eprintf("usage: %s [-ml]\n", argv0);
}
+static int
+show(const struct SessionEntry *e, void *arg)
+{
+ char timebuf[sizeof "yyyy-mm-dd hh:mm"];
+ time_t t;
+
+ (void)arg;
+ if (!*e->user || !*e->line || e->line[0] == '~')
+ return 0;
+ if (mflag != 0 && strcmp(e->line, tty) != 0)
+ return 0;
+ if (!!strcmp(e->user, "LOGIN") == lflag)
+ return 0;
+ t = e->time;
+ strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
+ printf("%-8s %-12s %-16s\n", e->user, e->line, timebuf);
+ return 0;
+}
+
// ?man who: show logged in users
// ?man display a list of users currently logged into the system
int
main(int argc, char *argv[])
{
- struct utmp usr;
- FILE *ufp;
- char timebuf[sizeof "yyyy-mm-dd hh:mm"];
- char line_buf[sizeof(usr.ut_line) + 1];
- char name_buf[sizeof(usr.ut_name) + 1];
- char *tty, *ttmp;
- int mflag = 0, lflag = 0;
- time_t t;
+ char *ttmp;
ARGBEGIN
{
@@ -53,25 +68,7 @@ main(int argc, char *argv[])
if (argc > 0)
usage();
- if (!(ufp = fopen(UTMP_PATH, "r")))
+ if (session_each(UTMP_PATH, show, NULL) < 0)
eprintf("fopen: %s:", UTMP_PATH);
-
- while (fread(&usr, sizeof(usr), 1, ufp) == 1) {
- memcpy(line_buf, usr.ut_line, sizeof(usr.ut_line));
- line_buf[sizeof(usr.ut_line)] = '\0';
- memcpy(name_buf, usr.ut_name, sizeof(usr.ut_name));
- name_buf[sizeof(usr.ut_name)] = '\0';
-
- if (!*name_buf || !*line_buf || line_buf[0] == '~')
- continue;
- if (mflag != 0 && strcmp(line_buf, tty) != 0)
- continue;
- if (!!strcmp(name_buf, "LOGIN") == lflag)
- continue;
- t = usr.ut_time;
- strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
- printf("%-8s %-12s %-16s\n", name_buf, line_buf, timebuf);
- }
- fclose(ufp);
return 0;
}
diff --git a/cmd/pseudo/getty.c b/cmd/pseudo/getty.c
@@ -17,9 +17,9 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <utmp.h>
#include "config.h"
+#include "session.h"
#include "util.h"
#include "wexec.h"
@@ -39,14 +39,10 @@ main(int argc, char *argv[])
{
char term[128], logname[LOGIN_NAME_MAX], c;
char hostname[HOST_NAME_MAX + 1];
- char ut_line_buf[UT_LINESIZE + 1];
- struct utmp usr;
struct sigaction sa;
- FILE *fp;
int fd;
unsigned int i = 0;
ssize_t n;
- long pos;
ARGBEGIN
{
@@ -106,28 +102,7 @@ main(int argc, char *argv[])
sigemptyset(&sa.sa_mask);
sigaction(SIGHUP, &sa, NULL);
- /* clear all utmp entries for this tty */
- fp = fopen(UTMP_PATH, "r+");
- if (fp) {
- do {
- pos = ftell(fp);
- if (fread(&usr, sizeof(usr), 1, fp) != 1)
- break;
- if (usr.ut_line[0] == '\0')
- continue;
- memcpy(ut_line_buf, usr.ut_line, UT_LINESIZE);
- ut_line_buf[UT_LINESIZE] = '\0';
- if (strcmp(ut_line_buf, tty) != 0)
- continue;
- memset(&usr, 0, sizeof(usr));
- fseek(fp, pos, SEEK_SET);
- if (fwrite(&usr, sizeof(usr), 1, fp) != 1)
- break;
- } while (1);
- if (ferror(fp))
- weprintf("%s: I/O error:", UTMP_PATH);
- fclose(fp);
- }
+ session_clear(tty);
if (argc > 2) {
wexecvp_self(argv[2], argv + 2);
diff --git a/cmd/pseudo/last.c b/cmd/pseudo/last.c
@@ -8,10 +8,10 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
-#include <utmp.h>
#include "config.h"
#include "paths.h"
+#include "session.h"
#include "util.h"
static void
@@ -20,17 +20,26 @@ usage(void)
eprintf("usage: %s [user]\n", argv0);
}
+static int
+show(const struct SessionEntry *e, void *arg)
+{
+ const char *user = arg;
+ time_t t;
+
+ if (!e->login || (user && strcmp(user, e->user)))
+ return 0;
+ t = e->time;
+ printf("%-8.8s %-8.8s %-16.16s %s", e->user, e->line, e->host, ctime(&t));
+ return 0;
+}
+
// ?man last: show last logged in users
// ?man arguments: user
// ?man display a list of recent user logins
int
main(int argc, char **argv)
{
- FILE *fp;
- struct utmp ut;
- char *user, *file, *prog;
- char ut_name_buf[UT_NAMESIZE + 1];
- time_t t;
+ char *user = NULL, *file, *prog;
ARGBEGIN
{
@@ -52,20 +61,7 @@ main(int argc, char **argv)
prog = basename(argv0);
file = (!strcmp(prog, "last")) ? WTMP_PATH : BTMP_PATH;
- if ((fp = fopen(file, "r")) == NULL)
+ if (session_each(file, show, user) < 0)
eprintf("fopen %s:", file);
-
- while (fread(&ut, sizeof(ut), 1, fp) == 1) {
- memcpy(ut_name_buf, ut.ut_name, UT_NAMESIZE);
- ut_name_buf[UT_NAMESIZE] = '\0';
- if (ut.ut_type != USER_PROCESS || (user && strcmp(user, ut_name_buf))) {
- continue;
- }
-
- t = ut.ut_time;
- printf("%-8.8s %-8.8s %-16.16s %s", ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t));
- }
- if (fclose(fp))
- eprintf("fclose %s:", file);
return 0;
}
diff --git a/cmd/pseudo/lastlog.c b/cmd/pseudo/lastlog.c
@@ -6,9 +6,9 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include <utmp.h>
#include "paths.h"
+#include "session.h"
#include "text.h"
#include "util.h"
@@ -19,9 +19,8 @@ static FILE *last;
static void
lastlog(char *user)
{
- struct passwd *pwd;
- struct lastlog ll;
- time_t lltime;
+ struct passwd *pwd;
+ struct SessionEntry e;
errno = 0;
if ((pwd = getpwnam(user)) == NULL) {
@@ -32,16 +31,9 @@ lastlog(char *user)
return;
}
- fseek(last, pwd->pw_uid * sizeof(struct lastlog), 0);
- fread(&ll, sizeof(struct lastlog), 1, last);
-
- if (ferror(last))
+ if (session_lastlog(last, pwd->pw_uid, &e) < 0)
eprintf("%s: read error:", ARUU_PATH_LASTLOG);
-
- /* on glibc `ll_time' can be an int32_t with compat32
- * avoid compiler warning when calling ctime() */
- lltime = ll.ll_time;
- printf("%-8.8s %-8.8s %-16.16s %s", user, ll.ll_line, ll.ll_host, ctime(&lltime));
+ printf("%-8.8s %-8.8s %-16.16s %s", user, e.line, e.host, ctime(&e.time));
}
// ?man lastlog: report recent logins
diff --git a/shared/libutil/session_linux.c b/shared/libutil/session_linux.c
@@ -51,4 +51,86 @@ session_record(const char *user)
fclose(fp);
}
}
+
+static void
+entry(struct SessionEntry *e, const struct utmp *u)
+{
+ memset(e, 0, sizeof(*e));
+ memcpy(e->user, u->ut_name, MIN(sizeof(e->user) - 1, sizeof(u->ut_name)));
+ memcpy(e->line, u->ut_line, MIN(sizeof(e->line) - 1, sizeof(u->ut_line)));
+ memcpy(e->host, u->ut_host, MIN(sizeof(e->host) - 1, sizeof(u->ut_host)));
+ e->time = u->ut_time;
+ e->login = u->ut_type == USER_PROCESS;
+}
+
+int
+session_each(const char *path, session_fn *fn, void *arg)
+{
+ struct SessionEntry e;
+ struct utmp u;
+ FILE *fp;
+ int r = 0;
+
+ if (!(fp = fopen(path, "r")))
+ return -1;
+ while (fread(&u, sizeof(u), 1, fp) == 1) {
+ entry(&e, &u);
+ if (fn(&e, arg))
+ break;
+ }
+ if (ferror(fp))
+ r = -1;
+ if (fclose(fp))
+ r = -1;
+ return r;
+}
+
+void
+session_clear(const char *line)
+{
+ struct utmp usr;
+ char ut_line_buf[UT_LINESIZE + 1];
+ FILE *fp;
+ long pos;
+
+ fp = fopen(UTMP_PATH, "r+");
+ if (!fp)
+ return;
+ do {
+ pos = ftell(fp);
+ if (fread(&usr, sizeof(usr), 1, fp) != 1)
+ break;
+ if (usr.ut_line[0] == '\0')
+ continue;
+ memcpy(ut_line_buf, usr.ut_line, UT_LINESIZE);
+ ut_line_buf[UT_LINESIZE] = '\0';
+ if (strcmp(ut_line_buf, line) != 0)
+ continue;
+ memset(&usr, 0, sizeof(usr));
+ fseek(fp, pos, SEEK_SET);
+ if (fwrite(&usr, sizeof(usr), 1, fp) != 1)
+ break;
+ } while (1);
+ if (ferror(fp))
+ weprintf("%s: I/O error:", UTMP_PATH);
+ fclose(fp);
+}
+
+int
+session_lastlog(FILE *fp, uid_t uid, struct SessionEntry *e)
+{
+ struct lastlog ll;
+
+ memset(&ll, 0, sizeof(ll));
+ fseek(fp, uid * sizeof(struct lastlog), 0);
+ fread(&ll, sizeof(struct lastlog), 1, fp);
+ if (ferror(fp))
+ return -1;
+ memset(e, 0, sizeof(*e));
+ memcpy(e->line, ll.ll_line, MIN(sizeof(e->line) - 1, sizeof(ll.ll_line)));
+ memcpy(e->host, ll.ll_host, MIN(sizeof(e->host) - 1, sizeof(ll.ll_host)));
+ /* on glibc ll_time can be an int32_t with compat32 */
+ e->time = ll.ll_time;
+ return 0;
+}
#endif
diff --git a/shared/libutil/session_other.c b/shared/libutil/session_other.c
@@ -1,6 +1,8 @@
/* see LICENSE file for copyright and license details */
#include "../session.h"
+#include <errno.h>
+
/* no login records: who and last have nothing to read */
#if !defined(__linux__) || defined(ARUU_FORCE_OTHER)
void
@@ -8,4 +10,30 @@ session_record(const char *user)
{
(void)user;
}
+
+int
+session_each(const char *path, session_fn *fn, void *arg)
+{
+ (void)path;
+ (void)fn;
+ (void)arg;
+ errno = ENOTSUP;
+ return -1;
+}
+
+void
+session_clear(const char *line)
+{
+ (void)line;
+}
+
+int
+session_lastlog(FILE *fp, uid_t uid, struct SessionEntry *e)
+{
+ (void)fp;
+ (void)uid;
+ (void)e;
+ errno = ENOTSUP;
+ return -1;
+}
#endif
diff --git a/shared/session.h b/shared/session.h
@@ -2,8 +2,29 @@
#ifndef ARUU_SESSION_H
#define ARUU_SESSION_H
+#include <stdio.h>
+#include <sys/types.h>
+#include <time.h>
+
+struct SessionEntry {
+ char user[64];
+ char line[64];
+ char host[257];
+ time_t time;
+ int login; /* a user's session, not one of init's own records */
+};
+
+typedef int session_fn(const struct SessionEntry *e, void *arg);
+
/* a backend per system: the record of a login on the controlling tty
* that who and last read, best effort */
void session_record(const char *user);
+/* fn for every record of a utmp or wtmp file, until it returns non zero.
+ * -1 with errno set when the file cannot be read */
+int session_each(const char *path, session_fn *fn, void *arg);
+/* a getty starting on line forgets every record of it */
+void session_clear(const char *line);
+/* the last login of uid, from a lastlog file the caller opened */
+int session_lastlog(FILE *fp, uid_t uid, struct SessionEntry *e);
#endif