From 37f5bef63c0db4892d8ffa3c38a04c7998e10f83 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 5 Apr 2010 03:18:40 +0200 Subject: libbb: split update_utmp from login/getty in preparation to use it for telnetd function old new delta update_utent - 339 +339 login_main 1498 1128 -370 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 339/-370) Total: -31 bytes function old new delta update_utmp 246 337 +91 login_main 1128 1140 +12 getty_main 1908 1918 +10 ... update_utent 339 - -339 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 5/6 up/down: 119/-368) Total: -249 bytes Signed-off-by: Denys Vlasenko --- libbb/utmp.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 libbb/utmp.c (limited to 'libbb/utmp.c') diff --git a/libbb/utmp.c b/libbb/utmp.c new file mode 100644 index 000000000..06b939b9d --- /dev/null +++ b/libbb/utmp.c @@ -0,0 +1,92 @@ +/* vi: set sw=4 ts=4: */ +/* + * utmp/wtmp support routines. + * + * Copyright (C) 2010 Denys Vlasenko + * + * Licensed under GPL version 2, see file LICENSE in this tarball for details. + */ +#include "libbb.h" +#include + +static void touch(const char *filename) +{ + if (access(filename, R_OK | W_OK) == -1) + close(open(filename, O_WRONLY | O_CREAT, 0664)); +} + +/* + * Read "man utmp" to make sense out of it. + */ +void FAST_FUNC update_utmp(int new_type, const char *short_tty, const char *username, const char *opt_host) +{ + struct utmp utent; + struct utmp *ut; + pid_t pid; + + touch(_PATH_UTMP); + utmpname(_PATH_UTMP); + setutent(); + + pid = getpid(); + /* Did init/getty/telnetd/sshd/... create an entry for us? + * It should be (new_type-1), but we'd also reuse + * any other potentially stale xxx_PROCESS entry */ + while ((ut = getutent()) != NULL) { + if (ut->ut_pid == pid + // && ut->ut_line[0] + && ut->ut_id[0] /* must have nonzero id */ + && ( ut->ut_type == INIT_PROCESS + || ut->ut_type == LOGIN_PROCESS + || ut->ut_type == USER_PROCESS + || ut->ut_type == DEAD_PROCESS + ) + ) { + utent = *ut; /* struct copy */ + if (ut->ut_type >= new_type) { + /* Stale record. Nuke hostname */ + memset(utent.ut_host, 0, sizeof(utent.ut_host)); + } + /* NB: pututline (see later) searches for matching utent + * using getutid(utent) - we must not change ut_id + * if we want *exactly this* record to be overwritten! + */ + break; + } + } + endutent(); + + if (!ut) { + /* Didn't find anything, create new one */ + memset(&utent, 0, sizeof(utent)); + utent.ut_pid = pid; + /* Invent our own ut_id. ut_id is only 4 chars wide. + * Try to fit something remotely meaningful... */ + if (short_tty[0] == 'p') { + /* if "ptyXXX", map to "pXXX" */ + /* if "pts/XX", map to "p/XX" */ + utent.ut_id[0] = 'p'; + strncpy(utent.ut_id + 1, short_tty + 3, sizeof(utent.ut_id)-1); + } else { + /* assuming it's "ttyXXXX", map to "XXXX" */ + strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id)); + } + } + + utent.ut_type = new_type; + safe_strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line)); + safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); + if (opt_host) + safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); + utent.ut_tv.tv_sec = time(NULL); + + /* Update, or append new one */ + setutent(); + pututline(&utent); + endutent(); + +#if ENABLE_FEATURE_WTMP + touch(bb_path_wtmp_file); + updwtmp(bb_path_wtmp_file, &utent); +#endif +} -- cgit v1.2.3