]>
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 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 does not apply to the current script being
41 e
= getenv("RUNLEVEL");
46 /* $PREVLEVEL seems to be an Upstart thing */
48 e
= getenv("PREVLEVEL");
58 if (utmpxname(_PATH_UTMPX
) < 0)
63 found
= getutxid(&lookup
);
69 a
= found
->ut_pid
& 0xFF;
70 b
= (found
->ut_pid
>> 8) & 0xFF;
84 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
88 t
= now(CLOCK_REALTIME
);
90 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
91 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
94 static void init_entry(struct utmpx
*store
, usec_t t
) {
95 struct utsname uts
= {};
99 init_timestamp(store
, t
);
101 if (uname(&uts
) >= 0)
102 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
104 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
105 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
108 static int write_entry_utmp(const struct utmpx
*store
) {
113 /* utmp is similar to wtmp, but there is only one entry for
114 * each entry type resp. user; i.e. basically a key/value
117 if (utmpxname(_PATH_UTMPX
) < 0)
122 if (!pututxline(store
))
132 static int write_entry_wtmp(const struct utmpx
*store
) {
135 /* wtmp is a simple append-only file where each entry is
136 simply appended to the end; i.e. basically a log. */
139 updwtmpx(_PATH_WTMPX
, store
);
143 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
146 r
= write_entry_utmp(store_utmp
);
147 s
= write_entry_wtmp(store_wtmp
);
152 /* If utmp/wtmp have been disabled, that's a good thing, hence
153 * ignore the errors */
160 static int write_entry_both(const struct utmpx
*store
) {
161 return write_utmp_wtmp(store
, store
);
164 int utmp_put_shutdown(void) {
165 struct utmpx store
= {};
167 init_entry(&store
, 0);
169 store
.ut_type
= RUN_LVL
;
170 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
172 return write_entry_both(&store
);
175 int utmp_put_reboot(usec_t t
) {
176 struct utmpx store
= {};
178 init_entry(&store
, t
);
180 store
.ut_type
= BOOT_TIME
;
181 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
183 return write_entry_both(&store
);
186 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
191 strncpy(buf
, src
, buf_size
);
193 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
196 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
197 struct utmpx store
= {
198 .ut_type
= INIT_PROCESS
,
206 init_timestamp(&store
, 0);
208 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
209 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
212 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
214 r
= write_entry_both(&store
);
218 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
219 store
.ut_type
= LOGIN_PROCESS
;
220 r
= write_entry_both(&store
);
225 if (ut_type
== USER_PROCESS
) {
226 store
.ut_type
= USER_PROCESS
;
227 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
228 r
= write_entry_both(&store
);
236 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
237 struct utmpx lookup
= {
238 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
239 }, store
, store_wtmp
, *found
;
245 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
246 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
248 found
= getutxid(&lookup
);
252 if (found
->ut_pid
!= pid
)
255 memcpy(&store
, found
, sizeof(store
));
256 store
.ut_type
= DEAD_PROCESS
;
257 store
.ut_exit
.e_termination
= code
;
258 store
.ut_exit
.e_exit
= status
;
264 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
265 /* wtmp wants the current time */
266 init_timestamp(&store_wtmp
, 0);
268 return write_utmp_wtmp(&store
, &store_wtmp
);
271 int utmp_put_runlevel(int runlevel
, int previous
) {
272 struct utmpx store
= {};
275 assert(runlevel
> 0);
278 /* Find the old runlevel automatically */
280 r
= utmp_get_runlevel(&previous
, NULL
);
289 if (previous
== runlevel
)
292 init_entry(&store
, 0);
294 store
.ut_type
= RUN_LVL
;
295 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
296 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
298 return write_entry_both(&store
);
301 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
303 static int write_to_terminal(const char *tty
, const char *message
) {
304 _cleanup_close_
int fd
= -1;
312 fd
= open(tty
, O_WRONLY
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
313 if (fd
< 0 || !isatty(fd
))
317 left
= strlen(message
);
319 end
= now(CLOCK_MONOTONIC
) + TIMEOUT_USEC
;
326 t
= now(CLOCK_MONOTONIC
);
331 k
= fd_wait_for_event(fd
, POLLOUT
, end
- t
);
337 n
= write(fd
, p
, left
);
345 assert((size_t) n
<= left
);
356 const char *username
,
357 const char *origin_tty
,
358 bool (*match_tty
)(const char *tty
, void *userdata
),
361 _cleanup_free_
char *text
= NULL
, *hn
= NULL
, *un
= NULL
, *stdin_tty
= NULL
;
362 char date
[FORMAT_TIMESTAMP_MAX
];
366 hn
= gethostname_malloc();
370 un
= getlogname_malloc();
376 getttyname_harder(STDIN_FILENO
, &stdin_tty
);
377 origin_tty
= stdin_tty
;
382 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
385 origin_tty
? " on " : "", strempty(origin_tty
),
386 format_timestamp(date
, sizeof(date
), now(CLOCK_REALTIME
)),
394 while ((u
= getutxent())) {
395 _cleanup_free_
char *buf
= NULL
;
399 if (u
->ut_type
!= USER_PROCESS
|| u
->ut_user
[0] == 0)
402 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
403 if (path_startswith(u
->ut_line
, "/dev/"))
406 if (asprintf(&buf
, "/dev/%.*s", (int) sizeof(u
->ut_line
), u
->ut_line
) < 0)
412 if (!match_tty
|| match_tty(path
, userdata
)) {
413 q
= write_to_terminal(path
, text
);