]>
Commit | Line | Data |
---|---|---|
e537352b LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
e537352b LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
e537352b | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
e537352b LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
e537352b | 20 | #include <errno.h> |
07630cea LP |
21 | #include <fcntl.h> |
22 | #include <poll.h> | |
a8fbdf54 TA |
23 | #include <stddef.h> |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
e537352b | 26 | #include <string.h> |
a8fbdf54 | 27 | #include <sys/time.h> |
e537352b | 28 | #include <sys/utsname.h> |
ef2f1067 | 29 | #include <unistd.h> |
07630cea | 30 | #include <utmpx.h> |
e537352b | 31 | |
b5efdb8a | 32 | #include "alloc-util.h" |
3ffd4af2 | 33 | #include "fd-util.h" |
07630cea | 34 | #include "hostname-util.h" |
e537352b | 35 | #include "macro.h" |
9eb977db | 36 | #include "path-util.h" |
07630cea | 37 | #include "string-util.h" |
288a74cc | 38 | #include "terminal-util.h" |
a8fbdf54 | 39 | #include "time-util.h" |
b1d4f8e1 | 40 | #include "user-util.h" |
a8fbdf54 | 41 | #include "util.h" |
958b66ea | 42 | #include "utmp-wtmp.h" |
e537352b LP |
43 | |
44 | int utmp_get_runlevel(int *runlevel, int *previous) { | |
b92bea5d | 45 | struct utmpx *found, lookup = { .ut_type = RUN_LVL }; |
e537352b LP |
46 | int r; |
47 | const char *e; | |
48 | ||
49 | assert(runlevel); | |
50 | ||
51 | /* If these values are set in the environment this takes | |
52 | * precedence. Presumably, sysvinit does this to work around a | |
53 | * race condition that would otherwise exist where we'd always | |
54 | * go to disk and hence might read runlevel data that might be | |
55 | * very new and does not apply to the current script being | |
56 | * executed. */ | |
57 | ||
e7fb33ff LP |
58 | e = getenv("RUNLEVEL"); |
59 | if (e && e[0] > 0) { | |
e537352b LP |
60 | *runlevel = e[0]; |
61 | ||
62 | if (previous) { | |
63 | /* $PREVLEVEL seems to be an Upstart thing */ | |
64 | ||
e7fb33ff LP |
65 | e = getenv("PREVLEVEL"); |
66 | if (e && e[0] > 0) | |
e537352b LP |
67 | *previous = e[0]; |
68 | else | |
69 | *previous = 0; | |
70 | } | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
75 | if (utmpxname(_PATH_UTMPX) < 0) | |
76 | return -errno; | |
77 | ||
78 | setutxent(); | |
79 | ||
e7fb33ff LP |
80 | found = getutxid(&lookup); |
81 | if (!found) | |
e537352b LP |
82 | r = -errno; |
83 | else { | |
84 | int a, b; | |
85 | ||
86 | a = found->ut_pid & 0xFF; | |
87 | b = (found->ut_pid >> 8) & 0xFF; | |
88 | ||
4dd1de72 ZJS |
89 | *runlevel = a; |
90 | if (previous) | |
91 | *previous = b; | |
92 | ||
93 | r = 0; | |
e537352b LP |
94 | } |
95 | ||
96 | endutxent(); | |
97 | ||
98 | return r; | |
99 | } | |
100 | ||
169c1bda | 101 | static void init_timestamp(struct utmpx *store, usec_t t) { |
e537352b LP |
102 | assert(store); |
103 | ||
871d7de4 LP |
104 | if (t <= 0) |
105 | t = now(CLOCK_REALTIME); | |
e537352b | 106 | |
871d7de4 LP |
107 | store->ut_tv.tv_sec = t / USEC_PER_SEC; |
108 | store->ut_tv.tv_usec = t % USEC_PER_SEC; | |
169c1bda LP |
109 | } |
110 | ||
111 | static void init_entry(struct utmpx *store, usec_t t) { | |
b92bea5d | 112 | struct utsname uts = {}; |
169c1bda LP |
113 | |
114 | assert(store); | |
115 | ||
116 | init_timestamp(store, t); | |
117 | ||
e537352b LP |
118 | if (uname(&uts) >= 0) |
119 | strncpy(store->ut_host, uts.release, sizeof(store->ut_host)); | |
120 | ||
121 | strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */ | |
122 | strncpy(store->ut_id, "~~", sizeof(store->ut_id)); | |
123 | } | |
124 | ||
125 | static int write_entry_utmp(const struct utmpx *store) { | |
126 | int r; | |
127 | ||
128 | assert(store); | |
129 | ||
130 | /* utmp is similar to wtmp, but there is only one entry for | |
131 | * each entry type resp. user; i.e. basically a key/value | |
132 | * table. */ | |
133 | ||
134 | if (utmpxname(_PATH_UTMPX) < 0) | |
135 | return -errno; | |
136 | ||
137 | setutxent(); | |
138 | ||
139 | if (!pututxline(store)) | |
140 | r = -errno; | |
141 | else | |
142 | r = 0; | |
143 | ||
144 | endutxent(); | |
145 | ||
146 | return r; | |
147 | } | |
148 | ||
149 | static int write_entry_wtmp(const struct utmpx *store) { | |
150 | assert(store); | |
151 | ||
152 | /* wtmp is a simple append-only file where each entry is | |
863f3ce0 | 153 | simply appended to the end; i.e. basically a log. */ |
e537352b LP |
154 | |
155 | errno = 0; | |
156 | updwtmpx(_PATH_WTMPX, store); | |
157 | return -errno; | |
158 | } | |
159 | ||
4743137a | 160 | static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) { |
e537352b LP |
161 | int r, s; |
162 | ||
4743137a MS |
163 | r = write_entry_utmp(store_utmp); |
164 | s = write_entry_wtmp(store_wtmp); | |
e537352b LP |
165 | |
166 | if (r >= 0) | |
167 | r = s; | |
168 | ||
169 | /* If utmp/wtmp have been disabled, that's a good thing, hence | |
170 | * ignore the errors */ | |
171 | if (r == -ENOENT) | |
172 | r = 0; | |
173 | ||
174 | return r; | |
175 | } | |
176 | ||
4743137a MS |
177 | static int write_entry_both(const struct utmpx *store) { |
178 | return write_utmp_wtmp(store, store); | |
179 | } | |
180 | ||
0ad26e09 | 181 | int utmp_put_shutdown(void) { |
863f3ce0 | 182 | struct utmpx store = {}; |
e537352b | 183 | |
0ad26e09 | 184 | init_entry(&store, 0); |
e537352b LP |
185 | |
186 | store.ut_type = RUN_LVL; | |
187 | strncpy(store.ut_user, "shutdown", sizeof(store.ut_user)); | |
188 | ||
189 | return write_entry_both(&store); | |
190 | } | |
191 | ||
871d7de4 | 192 | int utmp_put_reboot(usec_t t) { |
863f3ce0 | 193 | struct utmpx store = {}; |
e537352b | 194 | |
871d7de4 | 195 | init_entry(&store, t); |
e537352b LP |
196 | |
197 | store.ut_type = BOOT_TIME; | |
55e39f40 | 198 | strncpy(store.ut_user, "reboot", sizeof(store.ut_user)); |
e537352b LP |
199 | |
200 | return write_entry_both(&store); | |
201 | } | |
202 | ||
44a6b1b6 | 203 | _pure_ static const char *sanitize_id(const char *id) { |
169c1bda LP |
204 | size_t l; |
205 | ||
206 | assert(id); | |
207 | l = strlen(id); | |
208 | ||
209 | if (l <= sizeof(((struct utmpx*) NULL)->ut_id)) | |
210 | return id; | |
211 | ||
212 | return id + l - sizeof(((struct utmpx*) NULL)->ut_id); | |
213 | } | |
214 | ||
023a4f67 | 215 | int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) { |
863f3ce0 TG |
216 | struct utmpx store = { |
217 | .ut_type = INIT_PROCESS, | |
218 | .ut_pid = pid, | |
219 | .ut_session = sid, | |
220 | }; | |
023a4f67 | 221 | int r; |
169c1bda LP |
222 | |
223 | assert(id); | |
224 | ||
0ad26e09 | 225 | init_timestamp(&store, 0); |
169c1bda | 226 | |
863f3ce0 | 227 | /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */ |
169c1bda LP |
228 | strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id)); |
229 | ||
230 | if (line) | |
2b6bf07d | 231 | strncpy(store.ut_line, basename(line), sizeof(store.ut_line)); |
169c1bda | 232 | |
023a4f67 LP |
233 | r = write_entry_both(&store); |
234 | if (r < 0) | |
235 | return r; | |
236 | ||
237 | if (ut_type == LOGIN_PROCESS || ut_type == USER_PROCESS) { | |
238 | store.ut_type = LOGIN_PROCESS; | |
239 | r = write_entry_both(&store); | |
240 | if (r < 0) | |
241 | return r; | |
242 | } | |
243 | ||
244 | if (ut_type == USER_PROCESS) { | |
245 | store.ut_type = USER_PROCESS; | |
246 | strncpy(store.ut_user, user, sizeof(store.ut_user)-1); | |
247 | r = write_entry_both(&store); | |
248 | if (r < 0) | |
249 | return r; | |
250 | } | |
251 | ||
252 | return 0; | |
169c1bda LP |
253 | } |
254 | ||
255 | int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) { | |
863f3ce0 TG |
256 | struct utmpx lookup = { |
257 | .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */ | |
258 | }, store, store_wtmp, *found; | |
169c1bda LP |
259 | |
260 | assert(id); | |
261 | ||
262 | setutxent(); | |
263 | ||
863f3ce0 | 264 | /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */ |
169c1bda LP |
265 | strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id)); |
266 | ||
e7fb33ff LP |
267 | found = getutxid(&lookup); |
268 | if (!found) | |
169c1bda LP |
269 | return 0; |
270 | ||
271 | if (found->ut_pid != pid) | |
272 | return 0; | |
273 | ||
fa4ad7ce | 274 | memcpy(&store, found, sizeof(store)); |
169c1bda LP |
275 | store.ut_type = DEAD_PROCESS; |
276 | store.ut_exit.e_termination = code; | |
277 | store.ut_exit.e_exit = status; | |
278 | ||
279 | zero(store.ut_user); | |
280 | zero(store.ut_host); | |
281 | zero(store.ut_tv); | |
282 | ||
4743137a MS |
283 | memcpy(&store_wtmp, &store, sizeof(store_wtmp)); |
284 | /* wtmp wants the current time */ | |
285 | init_timestamp(&store_wtmp, 0); | |
286 | ||
287 | return write_utmp_wtmp(&store, &store_wtmp); | |
169c1bda LP |
288 | } |
289 | ||
290 | ||
0ad26e09 | 291 | int utmp_put_runlevel(int runlevel, int previous) { |
863f3ce0 | 292 | struct utmpx store = {}; |
e537352b LP |
293 | int r; |
294 | ||
295 | assert(runlevel > 0); | |
296 | ||
297 | if (previous <= 0) { | |
298 | /* Find the old runlevel automatically */ | |
299 | ||
e7fb33ff LP |
300 | r = utmp_get_runlevel(&previous, NULL); |
301 | if (r < 0) { | |
d7fc909d LP |
302 | if (r != -ESRCH) |
303 | return r; | |
304 | ||
305 | previous = 0; | |
306 | } | |
e537352b LP |
307 | } |
308 | ||
4927fcae LP |
309 | if (previous == runlevel) |
310 | return 0; | |
311 | ||
0ad26e09 | 312 | init_entry(&store, 0); |
e537352b LP |
313 | |
314 | store.ut_type = RUN_LVL; | |
315 | store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8); | |
316 | strncpy(store.ut_user, "runlevel", sizeof(store.ut_user)); | |
317 | ||
318 | return write_entry_both(&store); | |
319 | } | |
ef2f1067 LP |
320 | |
321 | #define TIMEOUT_MSEC 50 | |
322 | ||
323 | static int write_to_terminal(const char *tty, const char *message) { | |
7fd1b19b | 324 | _cleanup_close_ int fd = -1; |
ef2f1067 LP |
325 | const char *p; |
326 | size_t left; | |
327 | usec_t end; | |
328 | ||
329 | assert(tty); | |
330 | assert(message); | |
331 | ||
e62d8c39 ZJS |
332 | fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC); |
333 | if (fd < 0 || !isatty(fd)) | |
ef2f1067 LP |
334 | return -errno; |
335 | ||
ef2f1067 LP |
336 | p = message; |
337 | left = strlen(message); | |
338 | ||
339 | end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC; | |
340 | ||
341 | while (left > 0) { | |
342 | ssize_t n; | |
b92bea5d ZJS |
343 | struct pollfd pollfd = { |
344 | .fd = fd, | |
345 | .events = POLLOUT, | |
346 | }; | |
ef2f1067 LP |
347 | usec_t t; |
348 | int k; | |
349 | ||
350 | t = now(CLOCK_MONOTONIC); | |
351 | ||
e62d8c39 ZJS |
352 | if (t >= end) |
353 | return -ETIME; | |
ef2f1067 | 354 | |
e62d8c39 ZJS |
355 | k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC); |
356 | if (k < 0) | |
ef2f1067 LP |
357 | return -errno; |
358 | ||
e62d8c39 ZJS |
359 | if (k == 0) |
360 | return -ETIME; | |
ef2f1067 | 361 | |
e62d8c39 ZJS |
362 | n = write(fd, p, left); |
363 | if (n < 0) { | |
ef2f1067 LP |
364 | if (errno == EAGAIN) |
365 | continue; | |
366 | ||
e62d8c39 | 367 | return -errno; |
ef2f1067 LP |
368 | } |
369 | ||
370 | assert((size_t) n <= left); | |
371 | ||
372 | p += n; | |
373 | left -= n; | |
374 | } | |
375 | ||
e62d8c39 | 376 | return 0; |
ef2f1067 LP |
377 | } |
378 | ||
99f710dd DM |
379 | int utmp_wall( |
380 | const char *message, | |
381 | const char *username, | |
382 | const char *origin_tty, | |
383 | bool (*match_tty)(const char *tty, void *userdata), | |
384 | void *userdata) { | |
385 | ||
386 | _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL; | |
11620592 | 387 | char date[FORMAT_TIMESTAMP_MAX]; |
e7fb33ff | 388 | struct utmpx *u; |
ef2f1067 | 389 | int r; |
ef2f1067 | 390 | |
e7fb33ff | 391 | hn = gethostname_malloc(); |
9003d9b0 | 392 | if (!hn) |
e7fb33ff | 393 | return -ENOMEM; |
9003d9b0 ST |
394 | if (!username) { |
395 | un = getlogname_malloc(); | |
396 | if (!un) | |
397 | return -ENOMEM; | |
398 | } | |
ef2f1067 | 399 | |
99f710dd DM |
400 | if (!origin_tty) { |
401 | getttyname_harder(STDIN_FILENO, &stdin_tty); | |
402 | origin_tty = stdin_tty; | |
403 | } | |
ef2f1067 LP |
404 | |
405 | if (asprintf(&text, | |
406 | "\a\r\n" | |
11620592 | 407 | "Broadcast message from %s@%s%s%s (%s):\r\n\r\n" |
ef2f1067 | 408 | "%s\r\n\r\n", |
9003d9b0 | 409 | un ?: username, hn, |
99f710dd | 410 | origin_tty ? " on " : "", strempty(origin_tty), |
11620592 | 411 | format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)), |
e7fb33ff LP |
412 | message) < 0) |
413 | return -ENOMEM; | |
ef2f1067 LP |
414 | |
415 | setutxent(); | |
416 | ||
417 | r = 0; | |
418 | ||
419 | while ((u = getutxent())) { | |
e7fb33ff | 420 | _cleanup_free_ char *buf = NULL; |
ef2f1067 | 421 | const char *path; |
e7fb33ff | 422 | int q; |
ef2f1067 LP |
423 | |
424 | if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0) | |
425 | continue; | |
426 | ||
26888876 | 427 | /* this access is fine, because strlen("/dev/") << 32 (UT_LINESIZE) */ |
ef2f1067 LP |
428 | if (path_startswith(u->ut_line, "/dev/")) |
429 | path = u->ut_line; | |
430 | else { | |
e7fb33ff LP |
431 | if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0) |
432 | return -ENOMEM; | |
ef2f1067 LP |
433 | |
434 | path = buf; | |
435 | } | |
436 | ||
99f710dd | 437 | if (!match_tty || match_tty(path, userdata)) { |
e7fb33ff LP |
438 | q = write_to_terminal(path, text); |
439 | if (q < 0) | |
7af53310 | 440 | r = q; |
e7fb33ff | 441 | } |
ef2f1067 LP |
442 | } |
443 | ||
ef2f1067 LP |
444 | return r; |
445 | } |