summaryrefslogtreecommitdiff
path: root/system/sysvinit/utmpx.patch
blob: e839a0834bce1e17313d13133e36b6556b067609 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
diff -ur a/src/dowall.c b/src/dowall.c
--- a/src/dowall.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/dowall.c	2022-05-04 08:18:29.040000000 +0000
@@ -31,7 +31,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <utmp.h>
+#include <utmpx.h>
 #include <pwd.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -160,7 +160,7 @@
 {
 	FILE			*tp;
 	struct sigaction	sa;
-	struct utmp		*utmp;
+	struct utmpx		*utmp;
 	time_t			t;
 	char			term[UT_LINESIZE+ strlen(_PATH_DEV) + 1];
 	char			line[256];
@@ -217,9 +217,9 @@
 	sigemptyset(&sa.sa_mask);
 	sigaction(SIGALRM, &sa, NULL);
 
-	setutent();
+	setutxent();
 
-	while ((utmp = getutent()) != NULL) {
+	while ((utmp = getutxent()) != NULL) {
 		if(utmp->ut_type != USER_PROCESS ||
 		   utmp->ut_user[0] == 0) continue;
 		if (strncmp(utmp->ut_line, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
@@ -253,7 +253,7 @@
 		if (fd >= 0) close(fd);
 		if (tp != NULL) fclose(tp);
 	}
-	endutent();
+	endutxent();
 
 	exit(0);
 }
diff -ur a/src/halt.c b/src/halt.c
--- a/src/halt.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/halt.c	2022-05-04 08:18:29.030000000 +0000
@@ -49,7 +49,7 @@
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <stdlib.h>
-#include <utmp.h>
+#include <utmpx.h>
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
@@ -106,7 +106,7 @@
  */
 int get_runlevel(void)
 {
-	struct utmp *ut;
+	struct utmpx *ut;
 	char *r;
         int runlevel, status;
 
@@ -134,8 +134,8 @@
 	/*
 	 *	Find runlevel in utmp.
 	 */
-	setutent();
-	while ((ut = getutent()) != NULL) {
+	setutxent();
+	while ((ut = getutxent()) != NULL) {
 #if RUNLVL_PICKY
 		/*
 		 *	Only accept value if it's from after boottime.
@@ -147,7 +147,7 @@
 			return (ut->ut_pid & 255);
 #endif
 	}
-	endutent();
+	endutxent();
 
         /* Did not find utmp entry, try to read from log file */
         status = Read_Runlevel_Log(&runlevel);
diff -ur a/src/init.c b/src/init.c
--- a/src/init.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/init.c	2022-05-04 08:22:24.310000000 +0000
@@ -53,11 +53,7 @@
 #include <string.h>
 #include <signal.h>
 #include <termios.h>
-#ifdef __FreeBSD__
 #include <utmpx.h>
-#else
-#include <utmp.h>
-#endif
 #include <ctype.h>
 #include <stdarg.h>
 #include <sys/ttydefaults.h>
@@ -156,7 +152,7 @@
 int sleep_time = WAIT_BETWEEN_SIGNALS;    /* Sleep time between TERM and KILL */
 char *argv0;			/* First arguments; show up in ps listing */
 int maxproclen;			/* Maximal length of argv[0] with \0 */
-struct utmp utproto;		/* Only used for sizeof(utproto.ut_id) */
+struct utmpx utproto;		/* Only used for sizeof(utproto.ut_id) */
 char *console_dev;		/* Console device. */
 int pipe_fd = -1;		/* /run/initctl */
 int did_boot = 0;		/* Did we already do BOOT* stuff? */
@@ -2332,12 +2328,6 @@
 static
 void redo_utmp_wtmp(void)
 {
-	struct stat ustat;
-	const int ret = stat(UTMP_FILE, &ustat);
-
-	if ((ret < 0) || (ustat.st_size == 0))
-		wrote_utmp_rlevel = wrote_utmp_reboot = 0;
-
 	if ((wrote_wtmp_reboot == 0) || (wrote_utmp_reboot == 0))
 		write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
 
@@ -2879,8 +2869,6 @@
   console_init();
 
   if (!reload) {
-	int fd;
-
   	/* Close whatever files are open, and reset the console. */
 	close(0);
 	close(1);
@@ -2894,13 +2882,6 @@
   	setenv("PATH", PATH_DEFAULT, 1 /* Overwrite */);
 
   	/*
-	 *	Initialize /var/run/utmp (only works if /var is on
-	 *	root and mounted rw)
-	 */
-	if ((fd = open(UTMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
-		close(fd);
-
-  	/*
 	 *	Say hello to the world
 	 */
   	initlog(L_CO, bootmsg, "booting");
diff -ur a/src/runlevel.c b/src/runlevel.c
--- a/src/runlevel.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/runlevel.c	2022-05-04 08:21:34.870000000 +0000
@@ -22,7 +22,7 @@
  */
 
 #include <stdio.h>
-#include <utmp.h>
+#include <utmpx.h>
 #include <time.h>
 #include <stdlib.h>
 #include "runlevellog.h"
@@ -31,23 +31,23 @@
 int argc;
 char **argv;
 {
-  struct utmp *ut;
+  struct utmpx *ut;
   char prev;
   int status, runlevel;
 
-  if (argc > 1) utmpname(argv[1]);
+  if (argc > 1) utmpxname(argv[1]);
 
-  setutent();
-  while ((ut = getutent()) != NULL) {
+  setutxent();
+  while ((ut = getutxent()) != NULL) {
 	if (ut->ut_type == RUN_LVL) {
 		prev = ut->ut_pid / 256;
 		if (prev == 0) prev = 'N';
 		printf("%c %c\n", prev, ut->ut_pid % 256);
-		endutent();
+		endutxent();
 		exit(0);
 	}
   }
-  endutent();
+  endutxent();
   
   status = Read_Runlevel_Log(&runlevel);
   if (status)
diff -ur a/src/shutdown.c b/src/shutdown.c
--- a/src/shutdown.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/shutdown.c	2022-05-04 08:22:47.300000000 +0000
@@ -54,11 +54,7 @@
 #include <signal.h>
 #include <fcntl.h>
 #include <stdarg.h>
-#ifdef __FreeBSD__
 #include <utmpx.h>
-#else
-#include <utmp.h>
-#endif
 #include <syslog.h>
 #include "paths.h"
 #include "reboot.h"
@@ -355,6 +351,9 @@
 	for(i = 3; i < 20; i++) close(i);
 	close(255);
 
+	/* Record the fact that we're going down */
+	write_wtmp("shutdown", "~~", 0, RUN_LVL, "~~");
+
 	/* First idle init. */
 	if (kill(1, SIGTSTP) < 0) {
 		fprintf(stderr, "shutdown: can't idle init: %s.\r\n", strerror(errno));
@@ -381,9 +380,6 @@
 	/* Give init the chance to collect zombies. */
         /* sleep(1); */
 
-	/* Record the fact that we're going down */
-	write_wtmp("shutdown", "~~", 0, RUN_LVL, "~~");
-
 	/* This is for those who have quota installed. */
 #if defined(ACCTON_OFF)
 # if (ACCTON_OFF > 1) && (_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500))
@@ -514,7 +510,7 @@
 	struct sigaction	sa;
 	struct tm		*lt;
 	struct stat		st;
-	struct utmp		*ut;
+	struct utmpx		*ut;
 	time_t			t, target_time;
 	char			*halttype;
 	char			*downusers[32];
@@ -634,7 +630,7 @@
 		fclose(fp);
 
 		/* Now walk through /var/run/utmp to find logged in users. */
-		while(!user_ok && (ut = getutent()) != NULL) {
+		while(!user_ok && (ut = getutxent()) != NULL) {
 
 			/* See if this is a user process on a VC. */
 			if (ut->ut_type != USER_PROCESS) continue;
@@ -660,7 +656,7 @@
 					break;
 				}
 		}
-		endutent();
+		endutxent();
 
 		/* See if user was allowed. */
 		if (!user_ok) {
diff -ur a/src/utmp.c b/src/utmp.c
--- a/src/utmp.c	2022-05-04 08:18:19.100000000 +0000
+++ b/src/utmp.c	2022-05-04 08:22:04.640000000 +0000
@@ -32,11 +32,7 @@
 #include <time.h>
 #include <fcntl.h>
 #include <string.h>
-#ifdef __FreeBSD__
 #include <utmpx.h>
-#else
-#include <utmp.h>
-#endif
 
 #include "init.h"
 #include "initreq.h"
@@ -50,7 +46,11 @@
 #    define HAVE_UPDWTMP 1
 #  endif
 #else
-#  define HAVE_UPDWTMP 0
+#  define HAVE_UPDWTMPX 1
+#endif
+
+#ifndef WTMP_FILE
+#  define WTMP_FILE "/var/run/wtmp"
 #endif
 
 
@@ -64,24 +64,10 @@
 int type,			/* TYPE of entry */
 char *line)			/* Which line is this */
 {
-	int fd;
-	struct utmp utmp;
+	struct utmpx utmp;
 	struct utsname uname_buf;
 	struct timeval tv;
 	
-	/*
-	 *	Can't do much if WTMP_FILE is not present or not writable.
-	 */
-	if (access(WTMP_FILE, W_OK) < 0)
-		return;
-
-	/*
-	 *	Try to open the wtmp file. Note that we even try
-	 *	this if we have updwtmp() so we can see if the
-	 *	wtmp file is accessible.
-	 */
-	if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND)) < 0) return;
-
 #ifdef INIT_MAIN
 	/*
 	 *	Note if we are going to write a boot record.
@@ -119,13 +105,9 @@
 	 *	Zero the fields and enter new fields.
 	 */
 	memset(&utmp, 0, sizeof(utmp));
-#if defined(__GLIBC__)
 	gettimeofday(&tv, NULL);
 	utmp.ut_tv.tv_sec = tv.tv_sec;
 	utmp.ut_tv.tv_usec = tv.tv_usec;
-#else
-	time(&utmp.ut_time);
-#endif
 	utmp.ut_pid  = pid;
 	utmp.ut_type = type;
 	strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
@@ -136,12 +118,7 @@
         if (uname(&uname_buf) == 0)
 		strncpy(utmp.ut_host, uname_buf.release, sizeof(utmp.ut_host));
 
-#if HAVE_UPDWTMP
-	updwtmp(WTMP_FILE, &utmp);
-#else
-	write(fd, (char *)&utmp, sizeof(utmp));
-#endif
-	close(fd);
+	updwtmpx(WTMP_FILE, &utmp);
 }
 
 /*
@@ -156,17 +133,11 @@
 char *line,			/* LINE if used. */
 char *oldline)			/* Line of old utmp entry. */
 {
-	struct utmp utmp;
-	struct utmp tmp;
-	struct utmp *utmptr;
+	struct utmpx utmp;
+	struct utmpx tmp;
+	struct utmpx *utmptr;
 	struct timeval tv;
 
-	/*
-	 *	Can't do much if UTMP_FILE is not present or not writable.
-	 */
-	if (access(UTMP_FILE, W_OK) < 0)
-		return;
-
 #ifdef INIT_MAIN
 	/*
 	 *	Note if we are going to write a boot record.
@@ -207,13 +178,9 @@
 	utmp.ut_type = type;
 	utmp.ut_pid = pid;
 	strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
-#if defined(__GLIBC__)
 	gettimeofday(&tv, NULL);
 	utmp.ut_tv.tv_sec = tv.tv_sec;
 	utmp.ut_tv.tv_usec = tv.tv_usec;
-#else
-	time(&utmp.ut_time);
-#endif
 	strncpy(utmp.ut_user, user, UT_NAMESIZE);
 	if (line) strncpy(utmp.ut_line, line, UT_LINESIZE);
 	
@@ -225,9 +192,9 @@
 		/*
 		 *	Find existing entry for the tty line.
 		 */
-		setutent();
+		setutxent();
 		tmp = utmp;
-		if ((utmptr = getutid(&tmp)) != NULL) {
+		if ((utmptr = getutxid(&tmp)) != NULL) {
 			strncpy(utmp.ut_line, utmptr->ut_line, UT_LINESIZE);
 			if (oldline)
 				strncpy(oldline, utmptr->ut_line, UT_LINESIZE);
@@ -237,9 +204,9 @@
 	/*
 	 *	Update existing utmp file.
 	 */
-	setutent();
-	pututline(&utmp);
-	endutent();
+	setutxent();
+	pututxline(&utmp);
+	endutxent();
 }
 
 /*