• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23172097450

16 Mar 2026 07:45PM UTC coverage: 72.568% (+0.06%) from 72.513%
23172097450

push

github

keszybz
add-ug-bo-translation

316055 of 435531 relevant lines covered (72.57%)

1182407.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.05
/src/shared/utmp-wtmp.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <sys/utsname.h>
6
#include <utmpx.h>
7

8
#include "log.h"
9
#include "memory-util.h"
10
#include "string-util.h"
11
#include "time-util.h"
12
#include "utmp-wtmp.h"
13

14
static void init_timestamp(struct utmpx *store, usec_t t) {
336✔
15
        assert(store);
336✔
16

17
        if (t <= 0)
336✔
18
                t = now(CLOCK_REALTIME);
213✔
19

20
        store->ut_tv.tv_sec = t / USEC_PER_SEC;
336✔
21
        store->ut_tv.tv_usec = t % USEC_PER_SEC;
336✔
22
}
336✔
23

24
static void init_entry(struct utmpx *store, usec_t t) {
246✔
25
        struct utsname uts = {};
246✔
26

27
        assert(store);
246✔
28

29
        init_timestamp(store, t);
246✔
30

31
        if (uname(&uts) >= 0)
246✔
32
                strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
246✔
33

34
        strncpy(store->ut_line, "~", sizeof(store->ut_line));  /* or ~~ ? */
246✔
35
        strncpy(store->ut_id, "~~", sizeof(store->ut_id));
246✔
36
}
246✔
37

38
static int write_entry_utmp(const struct utmpx *store) {
350✔
39
        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
350✔
40

41
        assert(store);
350✔
42

43
        /* utmp is similar to wtmp, but there is only one entry for
44
         * each entry type resp. user; i.e. basically a key/value
45
         * table. */
46

47
        if (utmpxname(UTMPX_FILE) < 0)
350✔
48
                return -errno;
×
49

50
        utmpx = utxent_start();
350✔
51

52
        if (pututxline(store))
350✔
53
                return 0;
54
        if (errno == ENOENT) {
×
55
                /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
56
                log_debug_errno(errno, "Not writing utmp: %m");
×
57
                return 0;
×
58
        }
59
        return -errno;
×
60
}
61

62
static int write_entry_wtmp(const struct utmpx *store) {
350✔
63
        assert(store);
350✔
64

65
        /* wtmp is a simple append-only file where each entry is
66
         * simply appended to the end; i.e. basically a log. */
67

68
        errno = 0;
350✔
69
        updwtmpx(WTMPX_FILE, store);
350✔
70
        if (errno == ENOENT) {
350✔
71
                /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
72
                log_debug_errno(errno, "Not writing wtmp: %m");
350✔
73
                return 0;
350✔
74
        }
75
        if (errno == EROFS) {
×
76
                log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m");
×
77
                return 0;
×
78
        }
79
        return -errno;
×
80
}
81

82
static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
350✔
83
        int r, s;
350✔
84

85
        r = write_entry_utmp(store_utmp);
350✔
86
        s = write_entry_wtmp(store_wtmp);
350✔
87
        return r < 0 ? r : s;
350✔
88
}
89

90
static int write_entry_both(const struct utmpx *store) {
350✔
91
        return write_utmp_wtmp(store, store);
350✔
92
}
93

94
int utmp_put_shutdown(void) {
123✔
95
        struct utmpx store = {};
123✔
96

97
        init_entry(&store, 0);
123✔
98

99
        store.ut_type = RUN_LVL;
123✔
100
        strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
123✔
101

102
        return write_entry_both(&store);
123✔
103
}
104

105
int utmp_put_reboot(usec_t t) {
123✔
106
        struct utmpx store = {};
123✔
107

108
        init_entry(&store, t);
123✔
109

110
        store.ut_type = BOOT_TIME;
123✔
111
        strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
123✔
112

113
        return write_entry_both(&store);
123✔
114
}
115

116
static void copy_suffix(char *buf, size_t buf_size, const char *src) {
104✔
117
        size_t l;
104✔
118

119
        l = strlen(src);
104✔
120
        if (l < buf_size)
104✔
121
                strncpy(buf, src, buf_size);
×
122
        else
123
                memcpy(buf, src + l - buf_size, buf_size);
104✔
124
}
104✔
125

126
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
90✔
127
        struct utmpx store = {
90✔
128
                .ut_type = INIT_PROCESS,
129
                .ut_pid = pid,
130
                .ut_session = sid,
131
        };
132
        int r;
90✔
133

134
        assert(id);
90✔
135
        assert(ut_type != USER_PROCESS || user);
90✔
136

137
        init_timestamp(&store, 0);
90✔
138

139
        /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
140
        copy_suffix(store.ut_id, sizeof(store.ut_id), id);
90✔
141

142
        if (line)
90✔
143
                strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
88✔
144

145
        r = write_entry_both(&store);
90✔
146
        if (r < 0)
90✔
147
                return r;
90✔
148

149
        if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
90✔
150
                store.ut_type = LOGIN_PROCESS;
7✔
151
                r = write_entry_both(&store);
7✔
152
                if (r < 0)
7✔
153
                        return r;
154
        }
155

156
        if (ut_type == USER_PROCESS) {
90✔
157
                store.ut_type = USER_PROCESS;
7✔
158
                strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
7✔
159
                r = write_entry_both(&store);
7✔
160
                if (r < 0)
7✔
161
                        return r;
×
162
        }
163

164
        return 0;
165
}
166

167
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
14✔
168
        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
14✔
169
        struct utmpx lookup = {
14✔
170
                .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
171
        }, store, store_wtmp, *found;
172

173
        assert(id);
14✔
174

175
        utmpx = utxent_start();
14✔
176

177
        /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
178
        copy_suffix(lookup.ut_id, sizeof(lookup.ut_id), id);
14✔
179

180
        found = getutxid(&lookup);
14✔
181
        if (!found)
14✔
182
                return 0;
183

184
        if (found->ut_pid != pid)
×
185
                return 0;
186

187
        memcpy(&store, found, sizeof(store));
×
188
        store.ut_type = DEAD_PROCESS;
×
189
        store.ut_exit.e_termination = code;
×
190
        store.ut_exit.e_exit = status;
×
191

192
        zero(store.ut_user);
×
193
        zero(store.ut_host);
×
194
        zero(store.ut_tv);
×
195

196
        memcpy(&store_wtmp, &store, sizeof(store_wtmp));
×
197
        /* wtmp wants the current time */
198
        init_timestamp(&store_wtmp, 0);
×
199

200
        return write_utmp_wtmp(&store, &store_wtmp);
×
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc