]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
8a69e9692ba4d37917a54519b266fdf49b4ded8d
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <sys/utsname.h>
9 #include "memory-util.h"
10 #include "string-util.h"
11 #include "time-util.h"
12 #include "utmp-wtmp.h"
14 int utmp_get_runlevel(int *runlevel
, int *previous
) {
15 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
16 struct utmpx
*found
, lookup
= { .ut_type
= RUN_LVL
};
21 /* If these values are set in the environment this takes
22 * precedence. Presumably, sysvinit does this to work around a
23 * race condition that would otherwise exist where we'd always
24 * go to disk and hence might read runlevel data that might be
25 * very new and not apply to the current script being executed. */
27 e
= getenv("RUNLEVEL");
36 if (utmpxname(UTMPX_FILE
) < 0)
39 utmpx
= utxent_start();
41 found
= getutxid(&lookup
);
45 *runlevel
= found
->ut_pid
& 0xFF;
47 *previous
= (found
->ut_pid
>> 8) & 0xFF;
52 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
56 t
= now(CLOCK_REALTIME
);
58 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
59 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
62 static void init_entry(struct utmpx
*store
, usec_t t
) {
63 struct utsname uts
= {};
67 init_timestamp(store
, t
);
70 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
72 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
73 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
76 static int write_entry_utmp(const struct utmpx
*store
) {
77 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
81 /* utmp is similar to wtmp, but there is only one entry for
82 * each entry type resp. user; i.e. basically a key/value
85 if (utmpxname(UTMPX_FILE
) < 0)
88 utmpx
= utxent_start();
90 if (pututxline(store
))
92 if (errno
== ENOENT
) {
93 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
94 log_debug_errno(errno
, "Not writing utmp: %m");
100 static int write_entry_wtmp(const struct utmpx
*store
) {
103 /* wtmp is a simple append-only file where each entry is
104 * simply appended to the end; i.e. basically a log. */
107 updwtmpx(WTMPX_FILE
, store
);
108 if (errno
== ENOENT
) {
109 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
110 log_debug_errno(errno
, "Not writing wtmp: %m");
113 if (errno
== EROFS
) {
114 log_warning_errno(errno
, "Failed to write wtmp record, ignoring: %m");
120 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
123 r
= write_entry_utmp(store_utmp
);
124 s
= write_entry_wtmp(store_wtmp
);
125 return r
< 0 ? r
: s
;
128 static int write_entry_both(const struct utmpx
*store
) {
129 return write_utmp_wtmp(store
, store
);
132 int utmp_put_shutdown(void) {
133 struct utmpx store
= {};
135 init_entry(&store
, 0);
137 store
.ut_type
= RUN_LVL
;
138 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
140 return write_entry_both(&store
);
143 int utmp_put_reboot(usec_t t
) {
144 struct utmpx store
= {};
146 init_entry(&store
, t
);
148 store
.ut_type
= BOOT_TIME
;
149 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
151 return write_entry_both(&store
);
154 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
159 strncpy(buf
, src
, buf_size
);
161 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
164 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
165 struct utmpx store
= {
166 .ut_type
= INIT_PROCESS
,
173 assert(ut_type
!= USER_PROCESS
|| user
);
175 init_timestamp(&store
, 0);
177 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
178 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
181 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
183 r
= write_entry_both(&store
);
187 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
188 store
.ut_type
= LOGIN_PROCESS
;
189 r
= write_entry_both(&store
);
194 if (ut_type
== USER_PROCESS
) {
195 store
.ut_type
= USER_PROCESS
;
196 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
197 r
= write_entry_both(&store
);
205 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
206 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
207 struct utmpx lookup
= {
208 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
209 }, store
, store_wtmp
, *found
;
213 utmpx
= utxent_start();
215 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
216 copy_suffix(lookup
.ut_id
, sizeof(lookup
.ut_id
), id
);
218 found
= getutxid(&lookup
);
222 if (found
->ut_pid
!= pid
)
225 memcpy(&store
, found
, sizeof(store
));
226 store
.ut_type
= DEAD_PROCESS
;
227 store
.ut_exit
.e_termination
= code
;
228 store
.ut_exit
.e_exit
= status
;
234 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
235 /* wtmp wants the current time */
236 init_timestamp(&store_wtmp
, 0);
238 return write_utmp_wtmp(&store
, &store_wtmp
);
241 int utmp_put_runlevel(int runlevel
, int previous
) {
242 struct utmpx store
= {};
245 assert(runlevel
> 0);
248 /* Find the old runlevel automatically */
250 r
= utmp_get_runlevel(&previous
, NULL
);
259 if (previous
== runlevel
)
262 init_entry(&store
, 0);
264 store
.ut_type
= RUN_LVL
;
265 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
266 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
268 return write_entry_both(&store
);