]>
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"
16 #include "hostname-util.h"
19 #include "memory-util.h"
20 #include "path-util.h"
21 #include "string-util.h"
22 #include "terminal-util.h"
23 #include "time-util.h"
24 #include "user-util.h"
25 #include "utmp-wtmp.h"
27 int utmp_get_runlevel(int *runlevel
, int *previous
) {
28 _cleanup_(utxent_cleanup
) bool utmpx
= false;
29 struct utmpx
*found
, lookup
= { .ut_type
= RUN_LVL
};
34 /* If these values are set in the environment this takes
35 * precedence. Presumably, sysvinit does this to work around a
36 * race condition that would otherwise exist where we'd always
37 * go to disk and hence might read runlevel data that might be
38 * very new and not apply to the current script being executed. */
40 e
= getenv("RUNLEVEL");
49 if (utmpxname(_PATH_UTMPX
) < 0)
52 utmpx
= utxent_start();
54 found
= getutxid(&lookup
);
58 *runlevel
= found
->ut_pid
& 0xFF;
60 *previous
= (found
->ut_pid
>> 8) & 0xFF;
65 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
69 t
= now(CLOCK_REALTIME
);
71 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
72 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
75 static void init_entry(struct utmpx
*store
, usec_t t
) {
76 struct utsname uts
= {};
80 init_timestamp(store
, t
);
83 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
85 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
86 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
89 static int write_entry_utmp(const struct utmpx
*store
) {
90 _cleanup_(utxent_cleanup
) bool utmpx
= false;
94 /* utmp is similar to wtmp, but there is only one entry for
95 * each entry type resp. user; i.e. basically a key/value
98 if (utmpxname(_PATH_UTMPX
) < 0)
101 utmpx
= utxent_start();
103 if (pututxline(store
))
105 if (errno
== ENOENT
) {
106 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
107 log_debug_errno(errno
, "Not writing utmp: %m");
113 static int write_entry_wtmp(const struct utmpx
*store
) {
116 /* wtmp is a simple append-only file where each entry is
117 * simply appended to the end; i.e. basically a log. */
120 updwtmpx(_PATH_WTMPX
, store
);
121 if (errno
== ENOENT
) {
122 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
123 log_debug_errno(errno
, "Not writing wtmp: %m");
126 if (errno
== EROFS
) {
127 log_warning_errno(errno
, "Failed to write wtmp record, ignoring: %m");
133 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
136 r
= write_entry_utmp(store_utmp
);
137 s
= write_entry_wtmp(store_wtmp
);
138 return r
< 0 ? r
: s
;
141 static int write_entry_both(const struct utmpx
*store
) {
142 return write_utmp_wtmp(store
, store
);
145 int utmp_put_shutdown(void) {
146 struct utmpx store
= {};
148 init_entry(&store
, 0);
150 store
.ut_type
= RUN_LVL
;
151 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
153 return write_entry_both(&store
);
156 int utmp_put_reboot(usec_t t
) {
157 struct utmpx store
= {};
159 init_entry(&store
, t
);
161 store
.ut_type
= BOOT_TIME
;
162 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
164 return write_entry_both(&store
);
167 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
172 strncpy(buf
, src
, buf_size
);
174 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
177 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
178 struct utmpx store
= {
179 .ut_type
= INIT_PROCESS
,
187 init_timestamp(&store
, 0);
189 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
190 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
193 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
195 r
= write_entry_both(&store
);
199 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
200 store
.ut_type
= LOGIN_PROCESS
;
201 r
= write_entry_both(&store
);
206 if (ut_type
== USER_PROCESS
) {
207 store
.ut_type
= USER_PROCESS
;
208 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
209 r
= write_entry_both(&store
);
217 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
218 struct utmpx lookup
= {
219 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
220 }, store
, store_wtmp
, *found
;
226 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
227 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
229 found
= getutxid(&lookup
);
233 if (found
->ut_pid
!= pid
)
236 memcpy(&store
, found
, sizeof(store
));
237 store
.ut_type
= DEAD_PROCESS
;
238 store
.ut_exit
.e_termination
= code
;
239 store
.ut_exit
.e_exit
= status
;
245 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
246 /* wtmp wants the current time */
247 init_timestamp(&store_wtmp
, 0);
249 return write_utmp_wtmp(&store
, &store_wtmp
);
252 int utmp_put_runlevel(int runlevel
, int previous
) {
253 struct utmpx store
= {};
256 assert(runlevel
> 0);
259 /* Find the old runlevel automatically */
261 r
= utmp_get_runlevel(&previous
, NULL
);
270 if (previous
== runlevel
)
273 init_entry(&store
, 0);
275 store
.ut_type
= RUN_LVL
;
276 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
277 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
279 return write_entry_both(&store
);
282 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
284 static int write_to_terminal(const char *tty
, const char *message
) {
285 _cleanup_close_
int fd
= -1;
293 fd
= open(tty
, O_WRONLY
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
294 if (fd
< 0 || !isatty(fd
))
298 left
= strlen(message
);
300 end
= now(CLOCK_MONOTONIC
) + TIMEOUT_USEC
;
307 t
= now(CLOCK_MONOTONIC
);
312 k
= fd_wait_for_event(fd
, POLLOUT
, end
- t
);
318 n
= write(fd
, p
, left
);
326 assert((size_t) n
<= left
);
337 const char *username
,
338 const char *origin_tty
,
339 bool (*match_tty
)(const char *tty
, void *userdata
),
342 _cleanup_free_
char *text
= NULL
, *hn
= NULL
, *un
= NULL
, *stdin_tty
= NULL
;
346 hn
= gethostname_malloc();
350 un
= getlogname_malloc();
356 getttyname_harder(STDIN_FILENO
, &stdin_tty
);
357 origin_tty
= stdin_tty
;
362 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
365 origin_tty
? " on " : "", strempty(origin_tty
),
366 FORMAT_TIMESTAMP(now(CLOCK_REALTIME
)),
374 while ((u
= getutxent())) {
375 _cleanup_free_
char *buf
= NULL
;
379 if (u
->ut_type
!= USER_PROCESS
|| u
->ut_user
[0] == 0)
382 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
383 if (path_startswith(u
->ut_line
, "/dev/"))
386 if (asprintf(&buf
, "/dev/%.*s", (int) sizeof(u
->ut_line
), u
->ut_line
) < 0)
392 if (!match_tty
|| match_tty(path
, userdata
)) {
393 q
= write_to_terminal(path
, text
);