telnetd: write LOGIN/DEAD_PROCESS utmp records. Closes bug 1363

function                                             old     new   delta
write_new_utmp                                         -     253    +253
skip_dev_pfx                                           -      30     +30
handle_sigchld                                        42      72     +30
telnetd_main                                        1650    1673     +23
make_new_session                                     415     438     +23
...
login_main                                          1140    1148      +8
update_utmp                                          337     313     -24
write_wtmp                                           220     154     -66
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 11/6 up/down: 406/-115)         Total: ~291 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/networking/telnetd.c b/networking/telnetd.c
index cab94f0..b3e66eb 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -20,18 +20,22 @@
  * Vladimir Oleynik <dzo@simtreas.ru> 2001
  * Set process group corrections, initial busybox port
  */
-
 #define DEBUG 0
 
 #include "libbb.h"
 #include <syslog.h>
 
 #if DEBUG
-#define TELCMDS
-#define TELOPTS
+# define TELCMDS
+# define TELOPTS
 #endif
 #include <arpa/telnet.h>
 
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h> /* LOGIN_PROCESS */
+#endif
+
+
 struct tsession {
 	struct tsession *next;
 	pid_t shell_pid;
@@ -319,7 +323,11 @@
 	xopen(tty_name, O_RDWR); /* becomes our ctty */
 	xdup2(0, 1);
 	xdup2(0, 2);
-	tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
+	pid = getpid();
+	tcsetpgrp(0, pid); /* switch this tty's process group to us */
+
+//TODO: fetch remote addr via getpeername (see ftpd.c)
+	write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", /*hostname:*/ NULL);
 
 	/* The pseudo-terminal allocated to the client is configured to operate
 	 * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
@@ -358,12 +366,13 @@
 static void
 free_session(struct tsession *ts)
 {
-	struct tsession *t = G.sessions;
+	struct tsession *t;
 
 	if (option_mask32 & OPT_INETD)
 		exit(EXIT_SUCCESS);
 
 	/* Unlink this telnet session from the session list */
+	t = G.sessions;
 	if (t == ts)
 		G.sessions = ts->next;
 	else {
@@ -414,6 +423,7 @@
 {
 	pid_t pid;
 	struct tsession *ts;
+	int save_errno = errno;
 
 	/* Looping: more than one child may have exited */
 	while (1) {
@@ -424,11 +434,22 @@
 		while (ts) {
 			if (ts->shell_pid == pid) {
 				ts->shell_pid = -1;
+// man utmp:
+// When init(8) finds that a process has exited, it locates its utmp entry
+// by ut_pid, sets ut_type to DEAD_PROCESS, and clears ut_user, ut_host
+// and ut_time with null bytes.
+// [same applies to other processes which maintain utmp entries, like telnetd]
+//
+// We do not bother actually clearing fields:
+// it might be interesting to know who was logged in and from where
+				update_utmp(pid, DEAD_PROCESS, /*tty_name:*/ NULL, /*username:*/ NULL, /*hostname:*/ NULL);
 				break;
 			}
 			ts = ts->next;
 		}
 	}
+
+	errno = save_errno;
 }
 
 int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -689,6 +710,8 @@
 		ts = next;
 		continue;
  kill_session:
+		if (ts->shell_pid > 0)
+			update_utmp(ts->shell_pid, DEAD_PROCESS, /*tty_name:*/ NULL, /*username:*/ NULL, /*hostname:*/ NULL);
 		free_session(ts);
 		ts = next;
 	}