]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
test: drop --tpm2-public-key= from TEST-70
[thirdparty/systemd.git] / src / shared / utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/time.h>
8 #include <sys/utsname.h>
9 #include <unistd.h>
10 #include <utmpx.h>
11
12 #include "alloc-util.h"
13 #include "errno-util.h"
14 #include "hostname-util.h"
15 #include "macro.h"
16 #include "memory-util.h"
17 #include "path-util.h"
18 #include "string-util.h"
19 #include "time-util.h"
20 #include "user-util.h"
21 #include "utmp-wtmp.h"
22
23 int utmp_get_runlevel(int *runlevel, int *previous) {
24 _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
25 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
26 const char *e;
27
28 assert(runlevel);
29
30 /* If these values are set in the environment this takes
31 * precedence. Presumably, sysvinit does this to work around a
32 * race condition that would otherwise exist where we'd always
33 * go to disk and hence might read runlevel data that might be
34 * very new and not apply to the current script being executed. */
35
36 e = getenv("RUNLEVEL");
37 if (!isempty(e)) {
38 *runlevel = e[0];
39 if (previous)
40 *previous = 0;
41
42 return 0;
43 }
44
45 if (utmpxname(_PATH_UTMPX) < 0)
46 return -errno;
47
48 utmpx = utxent_start();
49
50 found = getutxid(&lookup);
51 if (!found)
52 return -errno;
53
54 *runlevel = found->ut_pid & 0xFF;
55 if (previous)
56 *previous = (found->ut_pid >> 8) & 0xFF;
57
58 return 0;
59 }
60
61 static void init_timestamp(struct utmpx *store, usec_t t) {
62 assert(store);
63
64 if (t <= 0)
65 t = now(CLOCK_REALTIME);
66
67 store->ut_tv.tv_sec = t / USEC_PER_SEC;
68 store->ut_tv.tv_usec = t % USEC_PER_SEC;
69 }
70
71 static void init_entry(struct utmpx *store, usec_t t) {
72 struct utsname uts = {};
73
74 assert(store);
75
76 init_timestamp(store, t);
77
78 if (uname(&uts) >= 0)
79 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
80
81 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
82 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
83 }
84
85 static int write_entry_utmp(const struct utmpx *store) {
86 _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
87
88 assert(store);
89
90 /* utmp is similar to wtmp, but there is only one entry for
91 * each entry type resp. user; i.e. basically a key/value
92 * table. */
93
94 if (utmpxname(_PATH_UTMPX) < 0)
95 return -errno;
96
97 utmpx = utxent_start();
98
99 if (pututxline(store))
100 return 0;
101 if (errno == ENOENT) {
102 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
103 log_debug_errno(errno, "Not writing utmp: %m");
104 return 0;
105 }
106 return -errno;
107 }
108
109 static int write_entry_wtmp(const struct utmpx *store) {
110 assert(store);
111
112 /* wtmp is a simple append-only file where each entry is
113 * simply appended to the end; i.e. basically a log. */
114
115 errno = 0;
116 updwtmpx(_PATH_WTMPX, store);
117 if (errno == ENOENT) {
118 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
119 log_debug_errno(errno, "Not writing wtmp: %m");
120 return 0;
121 }
122 if (errno == EROFS) {
123 log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m");
124 return 0;
125 }
126 return -errno;
127 }
128
129 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
130 int r, s;
131
132 r = write_entry_utmp(store_utmp);
133 s = write_entry_wtmp(store_wtmp);
134 return r < 0 ? r : s;
135 }
136
137 static int write_entry_both(const struct utmpx *store) {
138 return write_utmp_wtmp(store, store);
139 }
140
141 int utmp_put_shutdown(void) {
142 struct utmpx store = {};
143
144 init_entry(&store, 0);
145
146 store.ut_type = RUN_LVL;
147 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
148
149 return write_entry_both(&store);
150 }
151
152 int utmp_put_reboot(usec_t t) {
153 struct utmpx store = {};
154
155 init_entry(&store, t);
156
157 store.ut_type = BOOT_TIME;
158 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
159
160 return write_entry_both(&store);
161 }
162
163 static void copy_suffix(char *buf, size_t buf_size, const char *src) {
164 size_t l;
165
166 l = strlen(src);
167 if (l < buf_size)
168 strncpy(buf, src, buf_size);
169 else
170 memcpy(buf, src + l - buf_size, buf_size);
171 }
172
173 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
174 struct utmpx store = {
175 .ut_type = INIT_PROCESS,
176 .ut_pid = pid,
177 .ut_session = sid,
178 };
179 int r;
180
181 assert(id);
182 assert(ut_type != USER_PROCESS || user);
183
184 init_timestamp(&store, 0);
185
186 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
187 copy_suffix(store.ut_id, sizeof(store.ut_id), id);
188
189 if (line)
190 strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
191
192 r = write_entry_both(&store);
193 if (r < 0)
194 return r;
195
196 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
197 store.ut_type = LOGIN_PROCESS;
198 r = write_entry_both(&store);
199 if (r < 0)
200 return r;
201 }
202
203 if (ut_type == USER_PROCESS) {
204 store.ut_type = USER_PROCESS;
205 strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
206 r = write_entry_both(&store);
207 if (r < 0)
208 return r;
209 }
210
211 return 0;
212 }
213
214 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
215 _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
216 struct utmpx lookup = {
217 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
218 }, store, store_wtmp, *found;
219
220 assert(id);
221
222 utmpx = utxent_start();
223
224 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
225 copy_suffix(lookup.ut_id, sizeof(lookup.ut_id), id);
226
227 found = getutxid(&lookup);
228 if (!found)
229 return 0;
230
231 if (found->ut_pid != pid)
232 return 0;
233
234 memcpy(&store, found, sizeof(store));
235 store.ut_type = DEAD_PROCESS;
236 store.ut_exit.e_termination = code;
237 store.ut_exit.e_exit = status;
238
239 zero(store.ut_user);
240 zero(store.ut_host);
241 zero(store.ut_tv);
242
243 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
244 /* wtmp wants the current time */
245 init_timestamp(&store_wtmp, 0);
246
247 return write_utmp_wtmp(&store, &store_wtmp);
248 }
249
250 int utmp_put_runlevel(int runlevel, int previous) {
251 struct utmpx store = {};
252 int r;
253
254 assert(runlevel > 0);
255
256 if (previous <= 0) {
257 /* Find the old runlevel automatically */
258
259 r = utmp_get_runlevel(&previous, NULL);
260 if (r < 0) {
261 if (r != -ESRCH)
262 return r;
263
264 previous = 0;
265 }
266 }
267
268 if (previous == runlevel)
269 return 0;
270
271 init_entry(&store, 0);
272
273 store.ut_type = RUN_LVL;
274 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
275 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
276
277 return write_entry_both(&store);
278 }