Debian Codegraph @

src/login/logind-utmp.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/inotify.h>
4
5 #include "sd-event.h"
6
7 #include "log.h"
8 #include "logind.h"
9 #include "logind-session.h"
10 #include "logind-utmp.h"
11 #include "path-util.h"
12 #include "process-util.h"
13 #include "utmp-wtmp.h"
14
15 int manager_read_utmp(Manager *m) {
16 #if ENABLE_UTMP
17 assert(m);
18
19 if (utmpxname(UTMPX_FILE) < 0)
20 return log_error_errno(errno, "Failed to set utmp path to " UTMPX_FILE ": %m");
21
22 _unused_ _cleanup_(utxent_cleanup) bool utmpx = utxent_start();
23
24 for (;;) {
25 _cleanup_free_ char *t = NULL;
26 struct utmpx *u;
27 const char *c;
28 Session *s;
29
30 errno = 0;
31 u = getutxent();
32 if (!u) {
33 if (errno == ENOENT)
34 log_debug_errno(errno, UTMPX_FILE " does not exist, ignoring.");
35 else if (errno != 0)
36 log_warning_errno(errno, "Failed to read " UTMPX_FILE ", ignoring: %m");
37 return 0;
38 }
39
40 if (u->ut_type != USER_PROCESS)
41 continue;
42
43 if (!pid_is_valid(u->ut_pid))
44 continue;
45
46 t = memdup_suffix0(u->ut_line, sizeof(u->ut_line));
47 if (!t)
48 return log_oom();
49
50 c = path_startswith(t, "/dev/");
51 if (c) {
52 if (free_and_strdup(&t, c) < 0)
53 return log_oom();
54 }
55
56 if (isempty(t))
57 continue;
58
59 if (manager_get_session_by_leader(m, &PIDREF_MAKE_FROM_PID(u->ut_pid), &s) <= 0)
60 continue;
61
62 if (s->type != SESSION_TTY)
63 continue;
64
65 if (s->tty_validity == TTY_FROM_UTMP && !streq_ptr(s->tty, t)) {
66 /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In
67 * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY
68 * information and never acquire it again. */
69
70 s->tty = mfree(s->tty);
71 s->tty_validity = TTY_UTMP_INCONSISTENT;
72 log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s->id);
73 session_save(s);
74 continue;
75 }
76
77 /* Never override what we figured out once */
78 if (s->tty || s->tty_validity >= 0)
79 continue;
80
81 s->tty = TAKE_PTR(t);
82 s->tty_validity = TTY_FROM_UTMP;
83 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s->tty, s->id);
84 session_save(s);
85 }
86
87 #else
88 return 0;
89 #endif
90 }
91
92 #if ENABLE_UTMP
93 static int manager_dispatch_utmp(sd_event_source *s, const struct inotify_event *event, void *userdata) {
94 Manager *m = ASSERT_PTR(userdata);
95
96 /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's
97 * reestablish the watch on whatever there's now. */
98 if ((event->mask & (IN_ATTRIB|IN_DELETE_SELF|IN_MOVE_SELF|IN_Q_OVERFLOW|IN_UNMOUNT)) != 0)
99 manager_connect_utmp(m);
100
101 (void) manager_read_utmp(m);
102 return 0;
103 }
104 #endif
105
106 void manager_connect_utmp(Manager *m) {
107 #if ENABLE_UTMP
108 sd_event_source *s = NULL;
109 int r;
110
111 assert(m);
112
113 /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM
114 * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known
115 * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and
116 * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry.
117 *
118 * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle
119 * detection (which, for tty sessions, relies on the TTY used) */
120
121 r = sd_event_add_inotify(m->event, &s, UTMPX_FILE, IN_MODIFY|IN_MOVE_SELF|IN_DELETE_SELF|IN_ATTRIB, manager_dispatch_utmp, m);
122 if (r < 0)
123 log_full_errno(r == -ENOENT ? LOG_DEBUG: LOG_WARNING, r, "Failed to create inotify watch on " UTMPX_FILE ", ignoring: %m");
124 else {
125 r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE);
126 if (r < 0)
127 log_warning_errno(r, "Failed to adjust utmp event source priority, ignoring: %m");
128
129 (void) sd_event_source_set_description(s, "utmp");
130 }
131
132 sd_event_source_unref(m->utmp_event_source);
133 m->utmp_event_source = s;
134 #endif
135 }
136
137 void manager_reconnect_utmp(Manager *m) {
138 #if ENABLE_UTMP
139 assert(m);
140
141 if (m->utmp_event_source)
142 return;
143
144 manager_connect_utmp(m);
145 #endif
146 }