Debian Codegraph @

lib/readutmp.c

raw
1 /* GNU's read utmp module.
2
3 Copyright (C) 1992-2001, 2003-2006, 2009-2026 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by jla; revised by djm */
19
20 #include <config.h>
21
22 #include "readutmp.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33
34 #if defined __linux__ || defined __ANDROID__
35 # include <sys/sysinfo.h>
36 # include <time.h>
37 #endif
38 #if READUTMP_USE_SYSTEMD
39 # include <dirent.h>
40 # include <systemd/sd-login.h>
41 #endif
42
43 #if HAVE_SYS_SYSCTL_H && !(defined __GLIBC__ && defined __linux__) && !defined __minix
44 # if HAVE_SYS_PARAM_H
45 # include <sys/param.h>
46 # endif
47 # include <sys/sysctl.h>
48 #endif
49
50 #if HAVE_OS_H
51 # include <OS.h>
52 #endif
53
54 #if defined _WIN32 && ! defined __CYGWIN__
55 # define WIN32_LEAN_AND_MEAN
56 # include <windows.h>
57 # include <sys/time.h>
58 #endif
59
60 #include "stat-time.h"
61 #include "xalloc.h"
62
63 /* Each of the FILE streams in this file is only used in a single thread. */
64 #include "unlocked-io.h"
65
66 /* Some helper functions. */
67 #include "boot-time-aux.h"
68
69 /* The following macros describe the 'struct UTMP_STRUCT_NAME',
70 *not* 'struct gl_utmp'. */
71 #undef UT_USER
72 #undef UT_TIME_MEMBER
73 #undef UT_PID
74 #undef UT_TYPE_EQ
75 #undef UT_TYPE_NOT_DEFINED
76 #undef UT_EXIT_E_TERMINATION
77 #undef UT_EXIT_E_EXIT
78
79 /* Accessor macro for the member named ut_user or ut_name. */
80 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_NAME \
81 : HAVE_UTMP_H && HAVE_STRUCT_UTMP_UT_NAME)
82 # define UT_USER(UT) ((UT)->ut_name)
83 #else
84 # define UT_USER(UT) ((UT)->ut_user)
85 #endif
86
87 /* Accessor macro for the member of type time_t (or 'unsigned int'). */
88 #if HAVE_UTMPX_H || (HAVE_UTMP_H && HAVE_STRUCT_UTMP_UT_TV)
89 # define UT_TIME_MEMBER(UT) ((UT)->ut_tv.tv_sec)
90 #else
91 # define UT_TIME_MEMBER(UT) ((UT)->ut_time)
92 #endif
93
94 /* Accessor macro for the member named ut_pid. */
95 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_PID : HAVE_STRUCT_UTMP_UT_PID)
96 # define UT_PID(UT) ((UT)->ut_pid)
97 #else
98 # define UT_PID(UT) 0
99 #endif
100
101 /* Accessor macros for the member named ut_type. */
102 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
103 # define UT_TYPE_EQ(UT, V) ((UT)->ut_type == (V))
104 # define UT_TYPE_NOT_DEFINED 0
105 #else
106 # define UT_TYPE_EQ(UT, V) 0
107 # define UT_TYPE_NOT_DEFINED 1
108 #endif
109
110 #if HAVE_UTMPX_H
111 # if HAVE_STRUCT_UTMPX_UT_EXIT_E_TERMINATION
112 # define UT_EXIT_E_TERMINATION(UT) ((UT)->ut_exit.e_termination)
113 # else
114 # define UT_EXIT_E_TERMINATION(UT) 0
115 # endif
116 #elif HAVE_UTMP_H
117 # if HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION
118 # define UT_EXIT_E_TERMINATION(UT) ((UT)->ut_exit.e_termination)
119 # else
120 # define UT_EXIT_E_TERMINATION(UT) 0
121 # endif
122 #endif
123
124 #if HAVE_UTMPX_H
125 # if HAVE_STRUCT_UTMPX_UT_EXIT_E_EXIT
126 # define UT_EXIT_E_EXIT(UT) ((UT)->ut_exit.e_exit)
127 # else
128 # define UT_EXIT_E_EXIT(UT) 0
129 # endif
130 #elif HAVE_UTMP_H
131 # if HAVE_STRUCT_UTMP_UT_EXIT_E_EXIT
132 # define UT_EXIT_E_EXIT(UT) ((UT)->ut_exit.e_exit)
133 # else
134 # define UT_EXIT_E_EXIT(UT) 0
135 # endif
136 #endif
137
138 /* Size of the UT_USER (ut) member. */
139 #define UT_USER_SIZE sizeof UT_USER ((struct UTMP_STRUCT_NAME *) 0)
140 /* Size of the ut->ut_id member. */
141 #define UT_ID_SIZE sizeof (((struct UTMP_STRUCT_NAME *) 0)->ut_id)
142 /* Size of the ut->ut_line member. */
143 #define UT_LINE_SIZE sizeof (((struct UTMP_STRUCT_NAME *) 0)->ut_line)
144 /* Size of the ut->ut_host member. */
145 #define UT_HOST_SIZE sizeof (((struct UTMP_STRUCT_NAME *) 0)->ut_host)
146
147 #if _GL_GNUC_PREREQ (8, 0)
148 # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
149 #endif
150
151 /* Copy UT->ut_user into storage obtained from malloc. Then remove any
152 trailing spaces from the copy, NUL terminate it, and return the copy. */
153
154 char *
155 extract_trimmed_name (const STRUCT_UTMP *ut)
156 {
157 char const *name = ut->ut_user;
158 idx_t len = strlen (name);
159 char const *p;
160 for (p = name + len; name < p && p[-1] == ' '; p--)
161 continue;
162 return ximemdup0 (name, p - name);
163 }
164
165 #if READ_UTMP_SUPPORTED
166
167 /* Is the utmp entry UT desired by the user who asked for OPTIONS? */
168
169 static bool
170 desirable_utmp_entry (STRUCT_UTMP const *ut, int options)
171 {
172 # if defined __OpenBSD__ && !HAVE_UTMPX_H
173 /* Eliminate entirely empty entries. */
174 if (ut->ut_ts.tv_sec == 0 && ut->ut_user[0] == '\0'
175 && ut->ut_line[0] == '\0' && ut->ut_host[0] == '\0')
176 return false;
177 # endif
178
179 bool boot_time = UT_TYPE_BOOT_TIME (ut);
180 if ((options & READ_UTMP_BOOT_TIME) && !boot_time)
181 return false;
182 if ((options & READ_UTMP_NO_BOOT_TIME) && boot_time)
183 return false;
184
185 bool user_proc = IS_USER_PROCESS (ut);
186 if ((options & READ_UTMP_USER_PROCESS) && !user_proc)
187 return false;
188 # if !(defined __CYGWIN__ || defined _WIN32)
189 if ((options & READ_UTMP_CHECK_PIDS)
190 && user_proc
191 && 0 < UT_PID (ut)
192 && (kill (UT_PID (ut), 0) < 0 && errno == ESRCH))
193 return false;
194 # endif
195
196 return true;
197 }
198
199 /* A memory allocation for an in-progress read_utmp. */
200
201 struct utmp_alloc
202 {
203 /* A pointer to a possibly-empty array of utmp entries,
204 followed by a possibly-empty sequence of unused bytes,
205 followed by a possibly-empty sequence of string bytes.
206 UTMP is either null or allocated by malloc. */
207 struct gl_utmp *utmp;
208
209 /* The number of utmp entries. */
210 idx_t filled;
211
212 /* The string byte sequence length. Strings are null-terminated. */
213 idx_t string_bytes;
214
215 /* The total number of bytes allocated. This equals
216 FILLED * sizeof *UTMP + [size of free area] + STRING_BYTES. */
217 idx_t alloc_bytes;
218 };
219
220 /* Use the memory allocation A, and if the read_utmp options OPTIONS
221 permit it, add a new entry with the given USER, etc. Grow A as
222 needed, reporting an error and exit on memory allocation failure.
223 Return the resulting memory allocation. */
224
225 static struct utmp_alloc
226 add_utmp (struct utmp_alloc a, int options,
227 char const *user, idx_t user_len,
228 char const *id, idx_t id_len,
229 char const *line, idx_t line_len,
230 char const *host, idx_t host_len,
231 pid_t pid, short type, struct timespec ts, long session,
232 int termination, int exit)
233 {
234 int entry_bytes = sizeof (struct gl_utmp);
235 idx_t avail = a.alloc_bytes - (entry_bytes * a.filled + a.string_bytes);
236 idx_t needed_string_bytes =
237 (user_len + 1) + (id_len + 1) + (line_len + 1) + (host_len + 1);
238 idx_t needed = entry_bytes + needed_string_bytes;
239 if (avail < needed)
240 {
241 idx_t old_string_offset = a.alloc_bytes - a.string_bytes;
242 void *new = xpalloc (a.utmp, &a.alloc_bytes, needed - avail, -1, 1);
243 idx_t new_string_offset = a.alloc_bytes - a.string_bytes;
244 a.utmp = new;
245 char *q = new;
246 memmove (q + new_string_offset, q + old_string_offset, a.string_bytes);
247 }
248 struct gl_utmp *ut = &a.utmp[a.filled];
249 char *stringlim = (char *) a.utmp + a.alloc_bytes;
250 char *p = stringlim - a.string_bytes;
251 *--p = '\0'; /* NUL-terminate ut->ut_user */
252 ut->ut_user = p = memcpy (p - user_len, user, user_len);
253 *--p = '\0'; /* NUL-terminate ut->ut_id */
254 ut->ut_id = p = memcpy (p - id_len, id, id_len);
255 *--p = '\0'; /* NUL-terminate ut->ut_line */
256 ut->ut_line = p = memcpy (p - line_len, line, line_len);
257 *--p = '\0'; /* NUL-terminate ut->ut_host */
258 ut->ut_host = memcpy (p - host_len, host, host_len);
259 ut->ut_ts = ts;
260 ut->ut_pid = pid;
261 ut->ut_session = session;
262 ut->ut_type = type;
263 ut->ut_exit.e_termination = termination;
264 ut->ut_exit.e_exit = exit;
265 if (desirable_utmp_entry (ut, options))
266 {
267 /* Now that UT has been checked, relocate its string slots to be
268 relative to the end of the allocated storage, so that these
269 slots survive realloc. The slots will be relocated back just
270 before read_utmp returns. */
271 ut->ut_user = (char *) (intptr_t) (ut->ut_user - stringlim);
272 ut->ut_id = (char *) (intptr_t) (ut->ut_id - stringlim);
273 ut->ut_line = (char *) (intptr_t) (ut->ut_line - stringlim);
274 ut->ut_host = (char *) (intptr_t) (ut->ut_host - stringlim);
275 a.filled++;
276 a.string_bytes += needed_string_bytes;
277 }
278 return a;
279 }
280
281 /* Relocate the string pointers in A back to their natural position. */
282 static struct utmp_alloc
283 finish_utmp (struct utmp_alloc a)
284 {
285 char *stringlim = (char *) a.utmp + a.alloc_bytes;
286
287 for (idx_t i = 0; i < a.filled; i++)
288 {
289 a.utmp[i].ut_user = (intptr_t) a.utmp[i].ut_user + stringlim;
290 a.utmp[i].ut_id = (intptr_t) a.utmp[i].ut_id + stringlim;
291 a.utmp[i].ut_line = (intptr_t) a.utmp[i].ut_line + stringlim;
292 a.utmp[i].ut_host = (intptr_t) a.utmp[i].ut_host + stringlim;
293 }
294
295 return a;
296 }
297
298 /* Determine whether A already contains an entry of type BOOT_TIME. */
299 _GL_ATTRIBUTE_MAYBE_UNUSED
300 static bool
301 have_boot_time (struct utmp_alloc a)
302 {
303 for (idx_t i = 0; i < a.filled; i++)
304 {
305 struct gl_utmp *ut = &a.utmp[i];
306 if (UT_TYPE_BOOT_TIME (ut))
307 return true;
308 }
309 return false;
310 }
311
312 #if !HAVE_UTMPX_H && HAVE_UTMP_H && defined UTMP_NAME_FUNCTION
313 # if !HAVE_DECL_ENDUTENT /* Android */
314 void endutent (void);
315 # endif
316 #endif
317
318 static int
319 read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf,
320 int options)
321 {
322 if ((options & READ_UTMP_BOOT_TIME) != 0
323 && (options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) != 0)
324 {
325 /* No entries can match the given options. */
326 *n_entries = 0;
327 *utmp_buf = NULL;
328 return 0;
329 }
330
331 struct utmp_alloc a = { NULL, 0, 0, 0 };
332
333 # if READUTMP_USE_SYSTEMD || HAVE_UTMPX_H || HAVE_UTMP_H
334
335 # if defined UTMP_NAME_FUNCTION /* glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, Solaris, Cygwin, Android */
336
337 /* Ignore the return value for now.
338 Solaris' utmpname returns 1 upon success -- which is contrary
339 to what the GNU libc version does. In addition, older GNU libc
340 versions are actually void. */
341 UTMP_NAME_FUNCTION ((char *) file);
342
343 SET_UTMP_ENT ();
344
345 # if (defined __linux__ && !defined __ANDROID__) || defined __minix
346 bool file_is_utmp = streq (file, UTMP_FILE);
347 /* Timestamp of the "runlevel" entry, if any. */
348 struct timespec runlevel_ts = {0};
349 # endif
350
351 void const *entry;
352
353 while ((entry = GET_UTMP_ENT ()) != NULL)
354 {
355 struct UTMP_STRUCT_NAME const *ut = (struct UTMP_STRUCT_NAME const *) entry;
356
357 struct timespec ts =
358 #if (HAVE_UTMPX_H ? 1 : HAVE_STRUCT_UTMP_UT_TV)
359 { .tv_sec = ut->ut_tv.tv_sec, .tv_nsec = ut->ut_tv.tv_usec * 1000 };
360 #else
361 { .tv_sec = ut->ut_time, .tv_nsec = 0 };
362 #endif
363
364 a = add_utmp (a, options,
365 UT_USER (ut), strnlen (UT_USER (ut), UT_USER_SIZE),
366 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_ID : HAVE_STRUCT_UTMP_UT_ID)
367 ut->ut_id, strnlen (ut->ut_id, UT_ID_SIZE),
368 #else
369 "", 0,
370 #endif
371 ut->ut_line, strnlen (ut->ut_line, UT_LINE_SIZE),
372 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_HOST : HAVE_STRUCT_UTMP_UT_HOST)
373 ut->ut_host, strnlen (ut->ut_host, UT_HOST_SIZE),
374 #else
375 "", 0,
376 #endif
377 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_PID : HAVE_STRUCT_UTMP_UT_PID)
378 ut->ut_pid,
379 #else
380 0,
381 #endif
382 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
383 ut->ut_type,
384 #else
385 0,
386 #endif
387 ts,
388 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_SESSION : HAVE_STRUCT_UTMP_UT_SESSION)
389 ut->ut_session,
390 #else
391 0,
392 #endif
393 UT_EXIT_E_TERMINATION (ut), UT_EXIT_E_EXIT (ut)
394 );
395 # if defined __linux__ && !defined __ANDROID__
396 if (file_is_utmp
397 && memeq (UT_USER (ut), "runlevel", strlen ("runlevel") + 1)
398 && memeq (ut->ut_line, "~", strlen ("~") + 1))
399 runlevel_ts = ts;
400 # endif
401 # if defined __minix
402 if (file_is_utmp
403 && UT_USER (ut)[0] == '\0'
404 && memeq (ut->ut_line, "run-level ", strlen ("run-level ")))
405 runlevel_ts = ts;
406 # endif
407 }
408
409 END_UTMP_ENT ();
410
411 # if defined __linux__ && !defined __ANDROID__
412 /* On Alpine Linux, UTMP_FILE is not filled. It is always empty.
413 So, fake a BOOT_TIME entry, by getting the time stamp of a file that
414 gets touched only during the boot process.
415
416 On Raspbian, which runs on hardware without a real-time clock, during boot,
417 1. the clock gets set to 1970-01-01 00:00:00,
418 2. an entry gets written into /var/run/utmp, with ut_type = BOOT_TIME,
419 ut_user = "reboot", ut_line = "~", time = 1970-01-01 00:00:05 or so,
420 3. the clock gets set to a correct value through NTP,
421 4. an entry gets written into /var/run/utmp, with
422 ut_user = "runlevel", ut_line = "~", time = correct value.
423 In this case, copy the time from the "runlevel" entry to the "reboot"
424 entry. */
425 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
426 && file_is_utmp)
427 {
428 for (idx_t i = 0; i < a.filled; i++)
429 {
430 struct gl_utmp *ut = &a.utmp[i];
431 if (UT_TYPE_BOOT_TIME (ut))
432 {
433 /* Workaround for Raspbian: */
434 if (ut->ut_ts.tv_sec <= 60 && runlevel_ts.tv_sec != 0)
435 ut->ut_ts = runlevel_ts;
436 break;
437 }
438 }
439 if (!have_boot_time (a))
440 {
441 /* Workaround for Alpine Linux: */
442 struct timespec boot_time;
443 if (get_linux_boot_time_fallback (&boot_time) >= 0)
444 a = add_utmp (a, options,
445 "reboot", strlen ("reboot"),
446 "", 0,
447 "~", strlen ("~"),
448 "", 0,
449 0, BOOT_TIME, boot_time, 0, 0, 0);
450 }
451 }
452 # endif
453
454 # if defined __ANDROID__
455 /* On Android, there is no /var, and normal processes don't have access
456 to system files. Therefore use the kernel's uptime counter, although
457 it produces wrong values after the date has been bumped in the running
458 system. */
459 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
460 && streq (file, UTMP_FILE)
461 && !have_boot_time (a))
462 {
463 struct timespec boot_time;
464 if (get_android_boot_time (&boot_time) >= 0)
465 a = add_utmp (a, options,
466 "reboot", strlen ("reboot"),
467 "", 0,
468 "", 0,
469 "", 0,
470 0, BOOT_TIME, boot_time, 0, 0, 0);
471 }
472 # endif
473
474 # if defined __minix
475 /* On Minix, during boot,
476 1. an entry gets written into /var/run/utmp, with ut_type = BOOT_TIME,
477 ut_user = "", ut_line = "system boot", time = 1970-01-01 00:00:00,
478 2. an entry gets written into /var/run/utmp, with
479 ut_user = "", ut_line = "run-level m", time = correct value.
480 In this case, copy the time from the "run-level m" entry to the
481 "system boot" entry. */
482 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
483 && file_is_utmp)
484 {
485 for (idx_t i = 0; i < a.filled; i++)
486 {
487 struct gl_utmp *ut = &a.utmp[i];
488 if (UT_TYPE_BOOT_TIME (ut))
489 {
490 if (ut->ut_ts.tv_sec <= 60 && runlevel_ts.tv_sec != 0)
491 ut->ut_ts = runlevel_ts;
492 break;
493 }
494 }
495 }
496 # endif
497
498 # else /* old FreeBSD, OpenBSD, HP-UX, Haiku */
499
500 FILE *f = fopen (file, "re");
501
502 if (f != NULL)
503 {
504 for (;;)
505 {
506 struct UTMP_STRUCT_NAME ut;
507
508 if (fread (&ut, sizeof ut, 1, f) == 0)
509 break;
510 a = add_utmp (a, options,
511 UT_USER (&ut), strnlen (UT_USER (&ut), UT_USER_SIZE),
512 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_ID : HAVE_STRUCT_UTMP_UT_ID)
513 ut.ut_id, strnlen (ut.ut_id, UT_ID_SIZE),
514 #else
515 "", 0,
516 #endif
517 ut.ut_line, strnlen (ut.ut_line, UT_LINE_SIZE),
518 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_HOST : HAVE_STRUCT_UTMP_UT_HOST)
519 ut.ut_host, strnlen (ut.ut_host, UT_HOST_SIZE),
520 #else
521 "", 0,
522 #endif
523 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_PID : HAVE_STRUCT_UTMP_UT_PID)
524 ut.ut_pid,
525 #else
526 0,
527 #endif
528 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
529 ut.ut_type,
530 #else
531 0,
532 #endif
533 #if (HAVE_UTMPX_H ? 1 : HAVE_STRUCT_UTMP_UT_TV)
534 (struct timespec) { .tv_sec = ut.ut_tv.tv_sec, .tv_nsec = ut.ut_tv.tv_usec * 1000 },
535 #else
536 (struct timespec) { .tv_sec = ut.ut_time, .tv_nsec = 0 },
537 #endif
538 #if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_SESSION : HAVE_STRUCT_UTMP_UT_SESSION)
539 ut.ut_session,
540 #else
541 0,
542 #endif
543 UT_EXIT_E_TERMINATION (&ut), UT_EXIT_E_EXIT (&ut)
544 );
545 }
546
547 int saved_errno = ferror (f) ? errno : 0;
548 if (fclose (f) != 0)
549 saved_errno = errno;
550 if (saved_errno != 0)
551 {
552 free (a.utmp);
553 errno = saved_errno;
554 return -1;
555 }
556 }
557 else
558 {
559 if (!streq (file, UTMP_FILE))
560 {
561 int saved_errno = errno;
562 free (a.utmp);
563 errno = saved_errno;
564 return -1;
565 }
566 }
567
568 # if defined __OpenBSD__
569 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
570 && streq (file, UTMP_FILE)
571 && !have_boot_time (a))
572 {
573 struct timespec boot_time;
574 if (get_openbsd_boot_time (&boot_time) >= 0)
575 a = add_utmp (a, options,
576 "reboot", strlen ("reboot"),
577 "", 0,
578 "", 0,
579 "", 0,
580 0, BOOT_TIME, boot_time, 0, 0, 0);
581 }
582 # endif
583
584 # endif
585
586 # if defined __linux__ && !defined __ANDROID__
587 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
588 && streq (file, UTMP_FILE)
589 && !have_boot_time (a))
590 {
591 struct timespec boot_time;
592 if (get_linux_boot_time_final_fallback (&boot_time) >= 0)
593 a = add_utmp (a, options,
594 "reboot", strlen ("reboot"),
595 "", 0,
596 "~", strlen ("~"),
597 "", 0,
598 0, BOOT_TIME, boot_time, 0, 0, 0);
599 }
600
601 # endif
602
603 # if HAVE_SYS_SYSCTL_H && HAVE_SYSCTL \
604 && defined CTL_KERN && defined KERN_BOOTTIME \
605 && !defined __minix
606 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
607 && streq (file, UTMP_FILE)
608 && !have_boot_time (a))
609 {
610 struct timespec boot_time;
611 if (get_bsd_boot_time_final_fallback (&boot_time) >= 0)
612 a = add_utmp (a, options,
613 "reboot", strlen ("reboot"),
614 "", 0,
615 "", 0,
616 "", 0,
617 0, BOOT_TIME, boot_time, 0, 0, 0);
618 }
619 # endif
620
621 # if defined __HAIKU__
622 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
623 && streq (file, UTMP_FILE)
624 && !have_boot_time (a))
625 {
626 struct timespec boot_time;
627 if (get_haiku_boot_time (&boot_time) >= 0)
628 a = add_utmp (a, options,
629 "reboot", strlen ("reboot"),
630 "", 0,
631 "", 0,
632 "", 0,
633 0, BOOT_TIME, boot_time, 0, 0, 0);
634 }
635 # endif
636
637 # if HAVE_OS_H /* BeOS, Haiku */
638 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
639 && streq (file, UTMP_FILE)
640 && !have_boot_time (a))
641 {
642 struct timespec boot_time;
643 if (get_haiku_boot_time_final_fallback (&boot_time) >= 0)
644 a = add_utmp (a, options,
645 "reboot", strlen ("reboot"),
646 "", 0,
647 "", 0,
648 "", 0,
649 0, BOOT_TIME, boot_time, 0, 0, 0);
650 }
651 # endif
652
653 # endif
654
655 # if defined __CYGWIN__ || defined _WIN32
656 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
657 && streq (file, UTMP_FILE)
658 && !have_boot_time (a))
659 {
660 struct timespec boot_time;
661 if (get_windows_boot_time (&boot_time) >= 0)
662 a = add_utmp (a, options,
663 "reboot", strlen ("reboot"),
664 "", 0,
665 "", 0,
666 "", 0,
667 0, BOOT_TIME, boot_time, 0, 0, 0);
668 }
669 # endif
670
671 # if defined _WIN32 && ! defined __CYGWIN__
672 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
673 && streq (file, UTMP_FILE)
674 && !have_boot_time (a))
675 {
676 struct timespec boot_time;
677 if (get_windows_boot_time_fallback (&boot_time) >= 0)
678 a = add_utmp (a, options,
679 "reboot", strlen ("reboot"),
680 "", 0,
681 "", 0,
682 "", 0,
683 0, BOOT_TIME, boot_time, 0, 0, 0);
684 }
685 # endif
686
687 a = finish_utmp (a);
688
689 *n_entries = a.filled;
690 *utmp_buf = a.utmp;
691
692 return 0;
693 }
694
695 # if READUTMP_USE_SYSTEMD
696 /* Use systemd and Linux /proc and kernel APIs. */
697
698 static struct timespec
699 get_boot_time_uncached (void)
700 {
701 /* Try to find the boot time in the /var/run/utmp file. */
702 {
703 idx_t n_entries = 0;
704 STRUCT_UTMP *utmp = NULL;
705 read_utmp_from_file (UTMP_FILE, &n_entries, &utmp, READ_UTMP_BOOT_TIME);
706 if (n_entries > 0)
707 {
708 struct timespec result = utmp[0].ut_ts;
709 free (utmp);
710 return result;
711 }
712 free (utmp);
713 }
714
715 /* We shouldn't get here. */
716 return (struct timespec) {0};
717 }
718
719 static struct timespec
720 get_boot_time (void)
721 {
722 static bool volatile cached;
723 static struct timespec volatile boot_time;
724
725 if (!cached)
726 {
727 boot_time = get_boot_time_uncached ();
728 cached = true;
729 }
730 return boot_time;
731 }
732
733 /* Guess the pty name that was opened for the given user right after
734 the given time AT. */
735 static char *
736 guess_pty_name (uid_t uid, const struct timespec at)
737 {
738 /* Traverse the entries of the /dev/pts/ directory, looking for devices
739 which are owned by UID and whose ctime is shortly after AT. */
740 DIR *dirp = opendir ("/dev/pts");
741 if (dirp != NULL)
742 {
743 /* Buffer containing /dev/pts/N. */
744 char name_buf[9 + 10 + 1];
745 memcpy (name_buf, "/dev/pts/", 9);
746
747 char best_name[9 + 10 + 1];
748 struct timespec best_time = { .tv_sec = 0, .tv_nsec = 0 };
749
750 for (;;)
751 {
752 struct dirent *dp = readdir (dirp);
753 if (dp == NULL)
754 break;
755 if (dp->d_name[0] != '.' && strlen (dp->d_name) <= 10)
756 {
757 /* Compose the absolute file name /dev/pts/N. */
758 strcpy (name_buf + 9, dp->d_name);
759
760 /* Find its owner and ctime. */
761 struct stat st;
762 if (stat (name_buf, &st) >= 0
763 && st.st_uid == uid
764 && (st.st_ctim.tv_sec > at.tv_sec
765 || (st.st_ctim.tv_sec == at.tv_sec
766 && st.st_ctim.tv_nsec >= at.tv_nsec)))
767 {
768 /* This entry has the owner UID and a ctime >= AT. */
769 /* Is this entry the best one so far? */
770 if ((best_time.tv_sec == 0 && best_time.tv_nsec == 0)
771 || (st.st_ctim.tv_sec < best_time.tv_sec
772 || (st.st_ctim.tv_sec == best_time.tv_sec
773 && st.st_ctim.tv_nsec < best_time.tv_nsec)))
774 {
775 strcpy (best_name, name_buf);
776 best_time = st.st_ctim;
777 }
778 }
779 }
780 }
781
782 closedir (dirp);
783
784 /* Did we find an entry owned by ID, and is it at most 5 seconds
785 after AT? */
786 if (!(best_time.tv_sec == 0 && best_time.tv_nsec == 0)
787 && (best_time.tv_sec < at.tv_sec + 5
788 || (best_time.tv_sec == at.tv_sec + 5
789 && best_time.tv_nsec <= at.tv_nsec)))
790 return xstrdup (best_name + 5);
791 }
792
793 return NULL;
794 }
795
796 static int
797 read_utmp_from_systemd (idx_t *n_entries, STRUCT_UTMP **utmp_buf, int options)
798 {
799 /* Fill entries, simulating what a utmp file would contain. */
800 struct utmp_alloc a = {0};
801
802 /* Synthesize a BOOT_TIME entry. */
803 if (!(options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)))
804 a = add_utmp (a, options,
805 "reboot", strlen ("reboot"),
806 "", 0,
807 "~", strlen ("~"),
808 "", 0,
809 0, BOOT_TIME, get_boot_time (), 0, 0, 0);
810
811 /* Synthesize USER_PROCESS entries. */
812 if (!(options & READ_UTMP_BOOT_TIME))
813 {
814 char **sessions;
815 int num_sessions = sd_get_sessions (&sessions);
816 if (num_sessions >= 0 && sessions != NULL)
817 {
818 for (char **session_ptr = sessions; *session_ptr != NULL; session_ptr++)
819 {
820 char *session = *session_ptr;
821
822 uint64_t start_usec;
823 if (sd_session_get_start_time (session, &start_usec) < 0)
824 start_usec = 0;
825 struct timespec start_ts;
826 start_ts.tv_sec = start_usec / 1000000;
827 start_ts.tv_nsec = start_usec % 1000000 * 1000;
828
829 char *seat;
830 if (sd_session_get_seat (session, &seat) < 0)
831 seat = NULL;
832
833 char missing[] = "";
834
835 char *type = NULL;
836 char *tty;
837 if (sd_session_get_tty (session, &tty) < 0)
838 {
839 tty = NULL;
840 /* Try harder to get a sensible value for the tty. */
841 if (sd_session_get_type (session, &type) < 0)
842 type = missing;
843 if (streq (type, "tty"))
844 {
845 char *service;
846 if (sd_session_get_service (session, &service) < 0)
847 service = NULL;
848
849 uid_t uid;
850 char *pty = (sd_session_get_uid (session, &uid) < 0 ? NULL
851 : guess_pty_name (uid, start_ts));
852
853 if (service != NULL && pty != NULL)
854 {
855 tty = xmalloc (strlen (service) + 1 + strlen (pty) + 1);
856 stpcpy (stpcpy (stpcpy (tty, service), " "), pty);
857 free (pty);
858 free (service);
859 }
860 else if (service != NULL)
861 tty = service;
862 else if (pty != NULL)
863 tty = pty;
864 }
865 else if (streq (type, "web"))
866 {
867 char *service;
868 if (sd_session_get_service (session, &service) < 0)
869 service = NULL;
870
871 tty = service;
872 }
873 }
874
875 /* Create up to two USER_PROCESS or LOGIN_PROCESS entries:
876 one for the seat, one for the tty. */
877 if (seat != NULL || tty != NULL)
878 {
879 char *user;
880 if (sd_session_get_username (session, &user) < 0)
881 user = missing;
882
883 pid_t leader_pid;
884 if (sd_session_get_leader (session, &leader_pid) < 0)
885 leader_pid = 0;
886
887 char *clasz;
888 if (sd_session_get_class (session, &clasz) < 0)
889 clasz = missing;
890 short ctype = (strncmp (clasz, "user", 4) == 0
891 ? USER_PROCESS : LOGIN_PROCESS);
892
893 char *host;
894 char *remote_host;
895 if (sd_session_get_remote_host (session, &remote_host) < 0)
896 {
897 host = missing;
898 /* For backward compatibility, put the X11 display into the
899 host field. */
900 if (!type && sd_session_get_type (session, &type) < 0)
901 type = missing;
902 if (streq (type, "x11"))
903 {
904 char *display;
905 if (sd_session_get_display (session, &display) < 0)
906 display = NULL;
907 /* Workaround: gdm "forgets" to pass the display to
908 systemd, thus display may be NULL here. */
909 if (display != NULL)
910 host = display;
911 }
912 }
913 else
914 {
915 char *remote_user;
916 if (sd_session_get_remote_user (session, &remote_user) < 0)
917 host = remote_host;
918 else
919 {
920 host = xmalloc (strlen (remote_user) + 1
921 + strlen (remote_host) + 1);
922 stpcpy (stpcpy (stpcpy (host, remote_user), "@"),
923 remote_host);
924 free (remote_user);
925 free (remote_host);
926 }
927 }
928
929 if (seat != NULL)
930 a = add_utmp (a, options,
931 user, strlen (user),
932 session, strlen (session),
933 seat, strlen (seat),
934 host, strlen (host),
935 leader_pid /* the best we have */,
936 ctype, start_ts, leader_pid, 0, 0);
937 if (tty != NULL)
938 a = add_utmp (a, options,
939 user, strlen (user),
940 session, strlen (session),
941 tty, strlen (tty),
942 host, strlen (host),
943 leader_pid /* the best we have */,
944 ctype, start_ts, leader_pid, 0, 0);
945
946 if (host != missing)
947 free (host);
948 if (clasz != missing)
949 free (clasz);
950 if (user != missing)
951 free (user);
952 }
953
954 if (type != missing)
955 free (type);
956 free (tty);
957 free (seat);
958 free (session);
959 }
960 free (sessions);
961 }
962 }
963
964 a = finish_utmp (a);
965
966 *n_entries = a.filled;
967 *utmp_buf = a.utmp;
968
969 return 0;
970 }
971
972 # endif
973
974 int
975 read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf,
976 int options)
977 {
978 # if READUTMP_USE_SYSTEMD
979 if (streq (file, UTMP_FILE))
980 /* Imitate reading UTMP_FILE, using systemd and Linux APIs. */
981 return read_utmp_from_systemd (n_entries, utmp_buf, options);
982 # endif
983
984 return read_utmp_from_file (file, n_entries, utmp_buf, options);
985 }
986
987 #else /* dummy fallback */
988
989 int
990 read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf,
991 int options)
992 {
993 errno = ENOSYS;
994 return -1;
995 }
996
997 #endif