]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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");
45 /* $PREVLEVEL seems to be an Upstart thing */
47 e
= getenv("PREVLEVEL");
57 if (utmpxname(_PATH_UTMPX
) < 0)
60 utmpx
= utxent_start();
62 found
= getutxid(&lookup
);
66 *runlevel
= found
->ut_pid
& 0xFF;
68 *previous
= (found
->ut_pid
>> 8) & 0xFF;
73 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
77 t
= now(CLOCK_REALTIME
);
79 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
80 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
83 static void init_entry(struct utmpx
*store
, usec_t t
) {
84 struct utsname uts
= {};
88 init_timestamp(store
, t
);
91 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
93 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
94 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
97 static int write_entry_utmp(const struct utmpx
*store
) {
98 _cleanup_(utxent_cleanup
) bool utmpx
= false;
102 /* utmp is similar to wtmp, but there is only one entry for
103 * each entry type resp. user; i.e. basically a key/value
106 if (utmpxname(_PATH_UTMPX
) < 0)
109 utmpx
= utxent_start();
111 if (pututxline(store
))
113 if (errno
== ENOENT
) {
114 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
115 log_debug_errno(errno
, "Not writing utmp: %m");
121 static int write_entry_wtmp(const struct utmpx
*store
) {
124 /* wtmp is a simple append-only file where each entry is
125 * simply appended to the end; i.e. basically a log. */
128 updwtmpx(_PATH_WTMPX
, store
);
129 if (errno
== ENOENT
) {
130 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
131 log_debug_errno(errno
, "Not writing wtmp: %m");
134 if (errno
== EROFS
) {
135 log_warning_errno(errno
, "Failed to write wtmp record, ignoring: %m");
141 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
144 r
= write_entry_utmp(store_utmp
);
145 s
= write_entry_wtmp(store_wtmp
);
146 return r
< 0 ? r
: s
;
149 static int write_entry_both(const struct utmpx
*store
) {
150 return write_utmp_wtmp(store
, store
);
153 int utmp_put_shutdown(void) {
154 struct utmpx store
= {};
156 init_entry(&store
, 0);
158 store
.ut_type
= RUN_LVL
;
159 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
161 return write_entry_both(&store
);
164 int utmp_put_reboot(usec_t t
) {
165 struct utmpx store
= {};
167 init_entry(&store
, t
);
169 store
.ut_type
= BOOT_TIME
;
170 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
172 return write_entry_both(&store
);
175 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
180 strncpy(buf
, src
, buf_size
);
182 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
185 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
186 struct utmpx store
= {
187 .ut_type
= INIT_PROCESS
,
195 init_timestamp(&store
, 0);
197 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
198 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
201 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
203 r
= write_entry_both(&store
);
207 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
208 store
.ut_type
= LOGIN_PROCESS
;
209 r
= write_entry_both(&store
);
214 if (ut_type
== USER_PROCESS
) {
215 store
.ut_type
= USER_PROCESS
;
216 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
217 r
= write_entry_both(&store
);
225 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
226 struct utmpx lookup
= {
227 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
228 }, store
, store_wtmp
, *found
;
234 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
235 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
237 found
= getutxid(&lookup
);
241 if (found
->ut_pid
!= pid
)
244 memcpy(&store
, found
, sizeof(store
));
245 store
.ut_type
= DEAD_PROCESS
;
246 store
.ut_exit
.e_termination
= code
;
247 store
.ut_exit
.e_exit
= status
;
253 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
254 /* wtmp wants the current time */
255 init_timestamp(&store_wtmp
, 0);
257 return write_utmp_wtmp(&store
, &store_wtmp
);
260 int utmp_put_runlevel(int runlevel
, int previous
) {
261 struct utmpx store
= {};
264 assert(runlevel
> 0);
267 /* Find the old runlevel automatically */
269 r
= utmp_get_runlevel(&previous
, NULL
);
278 if (previous
== runlevel
)
281 init_entry(&store
, 0);
283 store
.ut_type
= RUN_LVL
;
284 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
285 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
287 return write_entry_both(&store
);
290 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
292 static int write_to_terminal(const char *tty
, const char *message
) {
293 _cleanup_close_
int fd
= -1;
301 fd
= open(tty
, O_WRONLY
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
302 if (fd
< 0 || !isatty(fd
))
306 left
= strlen(message
);
308 end
= now(CLOCK_MONOTONIC
) + TIMEOUT_USEC
;
315 t
= now(CLOCK_MONOTONIC
);
320 k
= fd_wait_for_event(fd
, POLLOUT
, end
- t
);
326 n
= write(fd
, p
, left
);
334 assert((size_t) n
<= left
);
345 const char *username
,
346 const char *origin_tty
,
347 bool (*match_tty
)(const char *tty
, void *userdata
),
350 _cleanup_free_
char *text
= NULL
, *hn
= NULL
, *un
= NULL
, *stdin_tty
= NULL
;
351 char date
[FORMAT_TIMESTAMP_MAX
];
355 hn
= gethostname_malloc();
359 un
= getlogname_malloc();
365 getttyname_harder(STDIN_FILENO
, &stdin_tty
);
366 origin_tty
= stdin_tty
;
371 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
374 origin_tty
? " on " : "", strempty(origin_tty
),
375 format_timestamp(date
, sizeof(date
), now(CLOCK_REALTIME
)),
383 while ((u
= getutxent())) {
384 _cleanup_free_
char *buf
= NULL
;
388 if (u
->ut_type
!= USER_PROCESS
|| u
->ut_user
[0] == 0)
391 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
392 if (path_startswith(u
->ut_line
, "/dev/"))
395 if (asprintf(&buf
, "/dev/%.*s", (int) sizeof(u
->ut_line
), u
->ut_line
) < 0)
401 if (!match_tty
|| match_tty(path
, userdata
)) {
402 q
= write_to_terminal(path
, text
);