]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include <sys/utsname.h>
20 #include "alloc-util.h"
22 #include "hostname-util.h"
24 #include "path-util.h"
25 #include "string-util.h"
26 #include "terminal-util.h"
27 #include "time-util.h"
28 #include "user-util.h"
30 #include "utmp-wtmp.h"
32 int utmp_get_runlevel(int *runlevel
, int *previous
) {
33 struct utmpx
*found
, lookup
= { .ut_type
= RUN_LVL
};
39 /* If these values are set in the environment this takes
40 * precedence. Presumably, sysvinit does this to work around a
41 * race condition that would otherwise exist where we'd always
42 * go to disk and hence might read runlevel data that might be
43 * very new and does not apply to the current script being
46 e
= getenv("RUNLEVEL");
51 /* $PREVLEVEL seems to be an Upstart thing */
53 e
= getenv("PREVLEVEL");
63 if (utmpxname(_PATH_UTMPX
) < 0)
68 found
= getutxid(&lookup
);
74 a
= found
->ut_pid
& 0xFF;
75 b
= (found
->ut_pid
>> 8) & 0xFF;
89 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
93 t
= now(CLOCK_REALTIME
);
95 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
96 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
99 static void init_entry(struct utmpx
*store
, usec_t t
) {
100 struct utsname uts
= {};
104 init_timestamp(store
, t
);
106 if (uname(&uts
) >= 0)
107 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
109 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
110 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
113 static int write_entry_utmp(const struct utmpx
*store
) {
118 /* utmp is similar to wtmp, but there is only one entry for
119 * each entry type resp. user; i.e. basically a key/value
122 if (utmpxname(_PATH_UTMPX
) < 0)
127 if (!pututxline(store
))
137 static int write_entry_wtmp(const struct utmpx
*store
) {
140 /* wtmp is a simple append-only file where each entry is
141 simply appended to the end; i.e. basically a log. */
144 updwtmpx(_PATH_WTMPX
, store
);
148 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
151 r
= write_entry_utmp(store_utmp
);
152 s
= write_entry_wtmp(store_wtmp
);
157 /* If utmp/wtmp have been disabled, that's a good thing, hence
158 * ignore the errors */
165 static int write_entry_both(const struct utmpx
*store
) {
166 return write_utmp_wtmp(store
, store
);
169 int utmp_put_shutdown(void) {
170 struct utmpx store
= {};
172 init_entry(&store
, 0);
174 store
.ut_type
= RUN_LVL
;
175 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
177 return write_entry_both(&store
);
180 int utmp_put_reboot(usec_t t
) {
181 struct utmpx store
= {};
183 init_entry(&store
, t
);
185 store
.ut_type
= BOOT_TIME
;
186 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
188 return write_entry_both(&store
);
191 _pure_
static const char *sanitize_id(const char *id
) {
197 if (l
<= sizeof(((struct utmpx
*) NULL
)->ut_id
))
200 return id
+ l
- sizeof(((struct utmpx
*) NULL
)->ut_id
);
203 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
204 struct utmpx store
= {
205 .ut_type
= INIT_PROCESS
,
213 init_timestamp(&store
, 0);
215 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
216 strncpy(store
.ut_id
, sanitize_id(id
), sizeof(store
.ut_id
));
219 strncpy(store
.ut_line
, basename(line
), sizeof(store
.ut_line
));
221 r
= write_entry_both(&store
);
225 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
226 store
.ut_type
= LOGIN_PROCESS
;
227 r
= write_entry_both(&store
);
232 if (ut_type
== USER_PROCESS
) {
233 store
.ut_type
= USER_PROCESS
;
234 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
235 r
= write_entry_both(&store
);
243 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
244 struct utmpx lookup
= {
245 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
246 }, store
, store_wtmp
, *found
;
252 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
253 strncpy(lookup
.ut_id
, sanitize_id(id
), sizeof(lookup
.ut_id
));
255 found
= getutxid(&lookup
);
259 if (found
->ut_pid
!= pid
)
262 memcpy(&store
, found
, sizeof(store
));
263 store
.ut_type
= DEAD_PROCESS
;
264 store
.ut_exit
.e_termination
= code
;
265 store
.ut_exit
.e_exit
= status
;
271 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
272 /* wtmp wants the current time */
273 init_timestamp(&store_wtmp
, 0);
275 return write_utmp_wtmp(&store
, &store_wtmp
);
279 int utmp_put_runlevel(int runlevel
, int previous
) {
280 struct utmpx store
= {};
283 assert(runlevel
> 0);
286 /* Find the old runlevel automatically */
288 r
= utmp_get_runlevel(&previous
, NULL
);
297 if (previous
== runlevel
)
300 init_entry(&store
, 0);
302 store
.ut_type
= RUN_LVL
;
303 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
304 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
306 return write_entry_both(&store
);
309 #define TIMEOUT_MSEC 50
311 static int write_to_terminal(const char *tty
, const char *message
) {
312 _cleanup_close_
int fd
= -1;
320 fd
= open(tty
, O_WRONLY
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
321 if (fd
< 0 || !isatty(fd
))
325 left
= strlen(message
);
327 end
= now(CLOCK_MONOTONIC
) + TIMEOUT_MSEC
*USEC_PER_MSEC
;
331 struct pollfd pollfd
= {
338 t
= now(CLOCK_MONOTONIC
);
343 k
= poll(&pollfd
, 1, (end
- t
) / USEC_PER_MSEC
);
350 n
= write(fd
, p
, left
);
358 assert((size_t) n
<= left
);
369 const char *username
,
370 const char *origin_tty
,
371 bool (*match_tty
)(const char *tty
, void *userdata
),
374 _cleanup_free_
char *text
= NULL
, *hn
= NULL
, *un
= NULL
, *stdin_tty
= NULL
;
375 char date
[FORMAT_TIMESTAMP_MAX
];
379 hn
= gethostname_malloc();
383 un
= getlogname_malloc();
389 getttyname_harder(STDIN_FILENO
, &stdin_tty
);
390 origin_tty
= stdin_tty
;
395 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
398 origin_tty
? " on " : "", strempty(origin_tty
),
399 format_timestamp(date
, sizeof(date
), now(CLOCK_REALTIME
)),
407 while ((u
= getutxent())) {
408 _cleanup_free_
char *buf
= NULL
;
412 if (u
->ut_type
!= USER_PROCESS
|| u
->ut_user
[0] == 0)
415 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
416 if (path_startswith(u
->ut_line
, "/dev/"))
419 if (asprintf(&buf
, "/dev/%.*s", (int) sizeof(u
->ut_line
), u
->ut_line
) < 0)
425 if (!match_tty
|| match_tty(path
, userdata
)) {
426 q
= write_to_terminal(path
, text
);