summaryrefslogtreecommitdiff
path: root/misc/screen/patches/patch-am
blob: 1bee5a5431b90b37b11aa0472703ff7b44d78756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
$NetBSD: patch-am,v 1.2 2015/02/13 04:44:40 rodent Exp $

Code to handle the login slot in utmp when utmpx is available.
Daemons shipped with NetBSD tend to write to both, while 3rd
party software might write to only one.

--- utmp-netbsd.c.orig	2015-02-13 04:30:05.000000000 +0000
+++ utmp-netbsd.c
@@ -0,0 +1,79 @@
+#include <sys/param.h>
+#if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106050000)
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <ttyent.h>
+#include <unistd.h>
+#include <util.h>
+#include <utmp.h>
+
+static struct utmp saved_utmp;
+static int saved_utmp_ok = 0;
+
+int
+lineslot(line)
+char *line;
+{
+	int slot;
+	struct ttyent *ttyp;
+
+	setttyent();
+	for (slot = 1; (ttyp = getttyent()) != NULL; ++slot)
+		if (!strcmp(ttyp->ty_name, line)) {
+			endttyent();
+			return(slot);
+		}
+	endttyent();
+	return(0);
+}
+
+void
+utmp_login(line)
+char *line;
+{
+	int fd;
+	int tty;
+
+	if (!saved_utmp_ok)
+		return;
+
+	tty = lineslot(line);
+	if (tty > 0 && (fd = open(_PATH_UTMP, O_WRONLY|O_CREAT, 0644)) >= 0) {
+		(void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
+		(void)write(fd, &saved_utmp, sizeof(struct utmp));
+		(void)close(fd);
+	}
+}
+
+void
+utmp_logout(const char *line)
+{
+	int fd;
+	struct utmp ut;
+
+	if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0)
+		return;
+	while (read(fd, &ut, sizeof(ut)) == sizeof(ut)) {
+		if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE))
+			continue;
+		memcpy(&saved_utmp, &ut, sizeof(ut));
+		saved_utmp_ok = 1;
+		memset(ut.ut_name, 0, UT_NAMESIZE);
+		memset(ut.ut_host, 0, UT_HOSTSIZE);
+		(void)time(&ut.ut_time);
+		(void)lseek(fd, -(off_t)sizeof(ut), SEEK_CUR);
+		(void)write(fd, &ut, sizeof(ut));
+	}
+	(void)close(fd);
+}
+
+#endif