]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/utsname.h>
14 #include "alloc-util.h"
15 #include "errno-util.h"
17 #include "hostname-util.h"
20 #include "memory-util.h"
21 #include "path-util.h"
22 #include "string-util.h"
23 #include "terminal-util.h"
24 #include "time-util.h"
25 #include "user-util.h"
26 #include "utmp-wtmp.h"
28 int utmp_get_runlevel(int *runlevel
, int *previous
) {
29 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
30 struct utmpx
*found
, lookup
= { .ut_type
= RUN_LVL
};
35 /* If these values are set in the environment this takes
36 * precedence. Presumably, sysvinit does this to work around a
37 * race condition that would otherwise exist where we'd always
38 * go to disk and hence might read runlevel data that might be
39 * very new and not apply to the current script being executed. */
41 e
= getenv("RUNLEVEL");
50 if (utmpxname(_PATH_UTMPX
) < 0)
53 utmpx
= utxent_start();
55 found
= getutxid(&lookup
);
59 *runlevel
= found
->ut_pid
& 0xFF;
61 *previous
= (found
->ut_pid
>> 8) & 0xFF;
66 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
70 t
= now(CLOCK_REALTIME
);
72 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
73 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
76 static void init_entry(struct utmpx
*store
, usec_t t
) {
77 struct utsname uts
= {};
81 init_timestamp(store
, t
);
84 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
86 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
87 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
90 static int write_entry_utmp(const struct utmpx
*store
) {
91 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
95 /* utmp is similar to wtmp, but there is only one entry for
96 * each entry type resp. user; i.e. basically a key/value
99 if (utmpxname(_PATH_UTMPX
) < 0)
102 utmpx
= utxent_start();
104 if (pututxline(store
))
106 if (errno
== ENOENT
) {
107 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
108 log_debug_errno(errno
, "Not writing utmp: %m");
114 static int write_entry_wtmp(const struct utmpx
*store
) {
117 /* wtmp is a simple append-only file where each entry is
118 * simply appended to the end; i.e. basically a log. */
121 updwtmpx(_PATH_WTMPX
, store
);
122 if (errno
== ENOENT
) {
123 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
124 log_debug_errno(errno
, "Not writing wtmp: %m");
127 if (errno
== EROFS
) {
128 log_warning_errno(errno
, "Failed to write wtmp record, ignoring: %m");
134 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
137 r
= write_entry_utmp(store_utmp
);
138 s
= write_entry_wtmp(store_wtmp
);
139 return r
< 0 ? r
: s
;
142 static int write_entry_both(const struct utmpx
*store
) {
143 return write_utmp_wtmp(store
, store
);
146 int utmp_put_shutdown(void) {
147 struct utmpx store
= {};
149 init_entry(&store
, 0);
151 store
.ut_type
= RUN_LVL
;
152 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
154 return write_entry_both(&store
);
157 int utmp_put_reboot(usec_t t
) {
158 struct utmpx store
= {};
160 init_entry(&store
, t
);
162 store
.ut_type
= BOOT_TIME
;
163 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
165 return write_entry_both(&store
);
168 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
173 strncpy(buf
, src
, buf_size
);
175 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
178 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
179 struct utmpx store
= {
180 .ut_type
= INIT_PROCESS
,
188 init_timestamp(&store
, 0);
190 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
191 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
194 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
196 r
= write_entry_both(&store
);
200 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
201 store
.ut_type
= LOGIN_PROCESS
;
202 r
= write_entry_both(&store
);
207 if (ut_type
== USER_PROCESS
) {
208 store
.ut_type
= USER_PROCESS
;
209 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
210 r
= write_entry_both(&store
);
218 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
219 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
220 struct utmpx lookup
= {
221 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
222 }, store
, store_wtmp
, *found
;
226 utmpx
= utxent_start();
228 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
229 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
231 found
= getutxid(&lookup
);
235 if (found
->ut_pid
!= pid
)
238 memcpy(&store
, found
, sizeof(store
));
239 store
.ut_type
= DEAD_PROCESS
;
240 store
.ut_exit
.e_termination
= code
;
241 store
.ut_exit
.e_exit
= status
;
247 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
248 /* wtmp wants the current time */
249 init_timestamp(&store_wtmp
, 0);
251 return write_utmp_wtmp(&store
, &store_wtmp
);
254 int utmp_put_runlevel(int runlevel
, int previous
) {
255 struct utmpx store
= {};
258 assert(runlevel
> 0);
261 /* Find the old runlevel automatically */
263 r
= utmp_get_runlevel(&previous
, NULL
);
272 if (previous
== runlevel
)
275 init_entry(&store
, 0);
277 store
.ut_type
= RUN_LVL
;
278 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
279 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
281 return write_entry_both(&store
);
284 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
286 static int write_to_terminal(const char *tty
, const char *message
) {
287 _cleanup_close_
int fd
= -EBADF
;
295 fd
= open(tty
, O_WRONLY
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
302 left
= strlen(message
);
304 end
= usec_add(now(CLOCK_MONOTONIC
), TIMEOUT_USEC
);
311 t
= now(CLOCK_MONOTONIC
);
315 k
= fd_wait_for_event(fd
, POLLOUT
, end
- t
);
317 if (ERRNO_IS_TRANSIENT(k
))
324 n
= write(fd
, p
, left
);
326 if (ERRNO_IS_TRANSIENT(errno
))
332 assert((size_t) n
<= left
);
343 const char *username
,
344 const char *origin_tty
,
345 bool (*match_tty
)(const char *tty
, bool is_local
, void *userdata
),
348 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
349 _cleanup_free_
char *text
= NULL
, *hn
= NULL
, *un
= NULL
, *stdin_tty
= NULL
;
353 hn
= gethostname_malloc();
357 un
= getlogname_malloc();
363 getttyname_harder(STDIN_FILENO
, &stdin_tty
);
364 origin_tty
= stdin_tty
;
369 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
372 origin_tty
? " on " : "", strempty(origin_tty
),
373 FORMAT_TIMESTAMP(now(CLOCK_REALTIME
)),
377 utmpx
= utxent_start();
381 while ((u
= getutxent())) {
382 _cleanup_free_
char *buf
= NULL
;
386 if (u
->ut_type
!= USER_PROCESS
|| u
->ut_user
[0] == 0)
389 /* This access is fine, because strlen("/dev/") < 32 (UT_LINESIZE) */
390 if (path_startswith(u
->ut_line
, "/dev/"))
393 if (asprintf(&buf
, "/dev/%.*s", (int) sizeof(u
->ut_line
), u
->ut_line
) < 0)
398 /* It seems that the address field is always set for remote logins.
399 * For local logins and other local entries, we get [0,0,0,0]. */
400 bool is_local
= memeqzero(u
->ut_addr_v6
, sizeof(u
->ut_addr_v6
));
402 if (!match_tty
|| match_tty(path
, is_local
, userdata
)) {
403 q
= write_to_terminal(path
, text
);