commit 008f34d708d4a63ff6bb13f8c314c15aea9e7112
parent 782022722d0d1c9b1d4ade43b4a579b4d8f34e42
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:36:07 -0300
login: utmp record behind a session backend
Diffstat:
5 files changed, 81 insertions(+), 49 deletions(-)
diff --git a/cmd/xsi/login.c b/cmd/xsi/login.c
@@ -6,6 +6,7 @@
#include "config.h"
#include "passwd.h"
+#include "session.h"
#include "util.h"
#include "wexec.h"
@@ -16,9 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include <termios.h>
-#include <time.h>
#include <unistd.h>
-#include <utmp.h>
static void
usage(void)
@@ -64,51 +63,6 @@ readline(const char *prompt, int echo)
return buf;
}
-/* best effort utmp record for the controlling tty: getty already
- * cleared any stale entry for this line before exec-ing us. only
- * ut_line, ut_name (musl aliases this to ut_user), and ut_time
- * (aliased to ut_tv.tv_sec) exist on both linux and bsd utmp
- * layouts; ut_type/ut_pid do not exist at all on bsd, so those two
- * fields are the one genuinely platform-specific part of this record */
-static void
-write_utmp(const char *user)
-{
- struct utmp usr;
- char *line;
- FILE *fp;
-
- memset(&usr, 0, sizeof(usr));
-#if defined(__linux__)
- usr.ut_type = USER_PROCESS;
- usr.ut_pid = getpid();
-#endif
- line = ttyname(STDIN_FILENO);
- if (line) {
- if (!strncmp(line, "/dev/", 5))
- line += 5;
- strlcpy(usr.ut_line, line, sizeof(usr.ut_line));
- }
- strlcpy(usr.ut_name, user, sizeof(usr.ut_name));
- usr.ut_time = time(NULL);
-
- /* utmp: a live table, so this overwrites/appends the one entry for
- * this session */
- fp = fopen(UTMP_PATH, "r+");
- if (fp) {
- fseek(fp, 0, SEEK_END);
- fwrite(&usr, sizeof(usr), 1, fp);
- fclose(fp);
- }
-
- /* wtmp: an ever-growing append-only log of the same record, same
- * portable field subset, just opened differently */
- fp = fopen(WTMP_PATH, "a");
- if (fp) {
- fwrite(&usr, sizeof(usr), 1, fp);
- fclose(fp);
- }
-}
-
// ?man login: authenticate a user and start a session
// ?man arguments: [-p name] [-f name]
// ?man prompt for a username (unless given) and password, verify against
@@ -182,7 +136,7 @@ main(int argc, char *argv[])
if (setuid(pw->pw_uid) < 0)
eprintf("setuid:");
- write_utmp(pw->pw_name);
+ session_record(pw->pw_name);
if (chdir(pw->pw_dir) < 0)
weprintf("chdir %s:", pw->pw_dir);
diff --git a/ninqu.rules b/ninqu.rules
@@ -49,6 +49,7 @@
"passwd_dragonfly.c" "passwd_freebsd.c" "passwd_linux.c"
"passwd_netbsd.c" "passwd_openbsd.c" "passwd_other.c"
"proc_linux.c" "proc_other.c"
+ "session_linux.c" "session_other.c"
"sysinfo_linux.c" "sysinfo_other.c")
(dep $(MATCH) FEATURES_H)
(after NOFORK_LIST_H)
@@ -80,10 +81,13 @@
"shared/libutil/passwd_other.c"
"shared/libutil/proc_linux.c"
"shared/libutil/proc_other.c"
+ "shared/libutil/session_linux.c"
+ "shared/libutil/session_other.c"
"shared/libutil/sysinfo_linux.c"
"shared/libutil/sysinfo_other.c")
(dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
- "shared/proc.h" "shared/sysinfo.h" "shared/util.h" FEATURES_H)
+ "shared/proc.h" "shared/session.h" "shared/sysinfo.h" "shared/util.h"
+ FEATURES_H)
(after NOFORK_LIST_H)
(out "shared/libutil/$(BASESTEM).o")
(exec $(CC) $(CPPFLAGS_LIBUTIL) -Ishared -Ishared/libutil $(CFLAGS) -c -o $(OUT) $(MATCH)))
diff --git a/shared/libutil/session_linux.c b/shared/libutil/session_linux.c
@@ -0,0 +1,54 @@
+/* see LICENSE file for copyright and license details */
+#ifndef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE
+#endif
+#include "../config.h"
+#include "../session.h"
+#include "../util.h"
+
+#if defined(__linux__) && !defined(ARUU_FORCE_OTHER)
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <utmp.h>
+
+/* getty already cleared any stale entry for this line before exec-ing
+ * login. ut_name is ut_user and ut_time is ut_tv.tv_sec under musl */
+void
+session_record(const char *user)
+{
+ struct utmp usr;
+ char *line;
+ FILE *fp;
+
+ memset(&usr, 0, sizeof(usr));
+ usr.ut_type = USER_PROCESS;
+ usr.ut_pid = getpid();
+ line = ttyname(STDIN_FILENO);
+ if (line) {
+ if (!strncmp(line, "/dev/", 5))
+ line += 5;
+ strlcpy(usr.ut_line, line, sizeof(usr.ut_line));
+ }
+ strlcpy(usr.ut_name, user, sizeof(usr.ut_name));
+ usr.ut_time = time(NULL);
+
+ /* utmp: a live table, so this overwrites/appends the one entry for
+ * this session */
+ fp = fopen(UTMP_PATH, "r+");
+ if (fp) {
+ fseek(fp, 0, SEEK_END);
+ fwrite(&usr, sizeof(usr), 1, fp);
+ fclose(fp);
+ }
+
+ /* wtmp: an ever-growing append-only log of the same record, just
+ * opened differently */
+ fp = fopen(WTMP_PATH, "a");
+ if (fp) {
+ fwrite(&usr, sizeof(usr), 1, fp);
+ fclose(fp);
+ }
+}
+#endif
diff --git a/shared/libutil/session_other.c b/shared/libutil/session_other.c
@@ -0,0 +1,11 @@
+/* see LICENSE file for copyright and license details */
+#include "../session.h"
+
+/* no login records: who and last have nothing to read */
+#if !defined(__linux__) || defined(ARUU_FORCE_OTHER)
+void
+session_record(const char *user)
+{
+ (void)user;
+}
+#endif
diff --git a/shared/session.h b/shared/session.h
@@ -0,0 +1,9 @@
+/* see LICENSE file for copyright and license details */
+#ifndef ARUU_SESSION_H
+#define ARUU_SESSION_H
+
+/* 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);
+
+#endif