]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include <sys/utsname.h>
12 #include "alloc-util.h"
13 #include "errno-util.h"
14 #include "hostname-util.h"
16 #include "memory-util.h"
17 #include "path-util.h"
18 #include "string-util.h"
19 #include "time-util.h"
20 #include "user-util.h"
21 #include "utmp-wtmp.h"
23 int utmp_get_runlevel(int *runlevel
, int *previous
) {
24 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
25 struct utmpx
*found
, lookup
= { .ut_type
= RUN_LVL
};
30 /* If these values are set in the environment this takes
31 * precedence. Presumably, sysvinit does this to work around a
32 * race condition that would otherwise exist where we'd always
33 * go to disk and hence might read runlevel data that might be
34 * very new and not apply to the current script being executed. */
36 e
= getenv("RUNLEVEL");
45 if (utmpxname(_PATH_UTMPX
) < 0)
48 utmpx
= utxent_start();
50 found
= getutxid(&lookup
);
54 *runlevel
= found
->ut_pid
& 0xFF;
56 *previous
= (found
->ut_pid
>> 8) & 0xFF;
61 static void init_timestamp(struct utmpx
*store
, usec_t t
) {
65 t
= now(CLOCK_REALTIME
);
67 store
->ut_tv
.tv_sec
= t
/ USEC_PER_SEC
;
68 store
->ut_tv
.tv_usec
= t
% USEC_PER_SEC
;
71 static void init_entry(struct utmpx
*store
, usec_t t
) {
72 struct utsname uts
= {};
76 init_timestamp(store
, t
);
79 strncpy(store
->ut_host
, uts
.release
, sizeof(store
->ut_host
));
81 strncpy(store
->ut_line
, "~", sizeof(store
->ut_line
)); /* or ~~ ? */
82 strncpy(store
->ut_id
, "~~", sizeof(store
->ut_id
));
85 static int write_entry_utmp(const struct utmpx
*store
) {
86 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
90 /* utmp is similar to wtmp, but there is only one entry for
91 * each entry type resp. user; i.e. basically a key/value
94 if (utmpxname(_PATH_UTMPX
) < 0)
97 utmpx
= utxent_start();
99 if (pututxline(store
))
101 if (errno
== ENOENT
) {
102 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
103 log_debug_errno(errno
, "Not writing utmp: %m");
109 static int write_entry_wtmp(const struct utmpx
*store
) {
112 /* wtmp is a simple append-only file where each entry is
113 * simply appended to the end; i.e. basically a log. */
116 updwtmpx(_PATH_WTMPX
, store
);
117 if (errno
== ENOENT
) {
118 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
119 log_debug_errno(errno
, "Not writing wtmp: %m");
122 if (errno
== EROFS
) {
123 log_warning_errno(errno
, "Failed to write wtmp record, ignoring: %m");
129 static int write_utmp_wtmp(const struct utmpx
*store_utmp
, const struct utmpx
*store_wtmp
) {
132 r
= write_entry_utmp(store_utmp
);
133 s
= write_entry_wtmp(store_wtmp
);
134 return r
< 0 ? r
: s
;
137 static int write_entry_both(const struct utmpx
*store
) {
138 return write_utmp_wtmp(store
, store
);
141 int utmp_put_shutdown(void) {
142 struct utmpx store
= {};
144 init_entry(&store
, 0);
146 store
.ut_type
= RUN_LVL
;
147 strncpy(store
.ut_user
, "shutdown", sizeof(store
.ut_user
));
149 return write_entry_both(&store
);
152 int utmp_put_reboot(usec_t t
) {
153 struct utmpx store
= {};
155 init_entry(&store
, t
);
157 store
.ut_type
= BOOT_TIME
;
158 strncpy(store
.ut_user
, "reboot", sizeof(store
.ut_user
));
160 return write_entry_both(&store
);
163 static void copy_suffix(char *buf
, size_t buf_size
, const char *src
) {
168 strncpy(buf
, src
, buf_size
);
170 memcpy(buf
, src
+ l
- buf_size
, buf_size
);
173 int utmp_put_init_process(const char *id
, pid_t pid
, pid_t sid
, const char *line
, int ut_type
, const char *user
) {
174 struct utmpx store
= {
175 .ut_type
= INIT_PROCESS
,
182 assert(ut_type
!= USER_PROCESS
|| user
);
184 init_timestamp(&store
, 0);
186 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
187 copy_suffix(store
.ut_id
, sizeof(store
.ut_id
), id
);
190 strncpy_exact(store
.ut_line
, line
, sizeof(store
.ut_line
));
192 r
= write_entry_both(&store
);
196 if (IN_SET(ut_type
, LOGIN_PROCESS
, USER_PROCESS
)) {
197 store
.ut_type
= LOGIN_PROCESS
;
198 r
= write_entry_both(&store
);
203 if (ut_type
== USER_PROCESS
) {
204 store
.ut_type
= USER_PROCESS
;
205 strncpy(store
.ut_user
, user
, sizeof(store
.ut_user
)-1);
206 r
= write_entry_both(&store
);
214 int utmp_put_dead_process(const char *id
, pid_t pid
, int code
, int status
) {
215 _unused_
_cleanup_(utxent_cleanup
) bool utmpx
= false;
216 struct utmpx lookup
= {
217 .ut_type
= INIT_PROCESS
/* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
218 }, store
, store_wtmp
, *found
;
222 utmpx
= utxent_start();
224 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
225 copy_suffix(lookup
.ut_id
, sizeof(lookup
.ut_id
), id
);
227 found
= getutxid(&lookup
);
231 if (found
->ut_pid
!= pid
)
234 memcpy(&store
, found
, sizeof(store
));
235 store
.ut_type
= DEAD_PROCESS
;
236 store
.ut_exit
.e_termination
= code
;
237 store
.ut_exit
.e_exit
= status
;
243 memcpy(&store_wtmp
, &store
, sizeof(store_wtmp
));
244 /* wtmp wants the current time */
245 init_timestamp(&store_wtmp
, 0);
247 return write_utmp_wtmp(&store
, &store_wtmp
);
250 int utmp_put_runlevel(int runlevel
, int previous
) {
251 struct utmpx store
= {};
254 assert(runlevel
> 0);
257 /* Find the old runlevel automatically */
259 r
= utmp_get_runlevel(&previous
, NULL
);
268 if (previous
== runlevel
)
271 init_entry(&store
, 0);
273 store
.ut_type
= RUN_LVL
;
274 store
.ut_pid
= (runlevel
& 0xFF) | ((previous
& 0xFF) << 8);
275 strncpy(store
.ut_user
, "runlevel", sizeof(store
.ut_user
));
277 return write_entry_both(&store
);