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

systemd / systemd / 17507162131

05 Sep 2025 01:14PM UTC coverage: 72.251% (+0.05%) from 72.2%
17507162131

push

github

DaanDeMeyer
test: use assertion macroses

An assertion macros helps to debug failing tests as it reports
expression, arguments and errno (if applicable)

13 of 13 new or added lines in 2 files covered. (100.0%)

975 existing lines in 41 files now uncovered.

302629 of 418860 relevant lines covered (72.25%)

1092693.24 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) {
414✔
15
        assert(store);
414✔
16

17
        if (t <= 0)
414✔
18
                t = now(CLOCK_REALTIME);
287✔
19

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

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

27
        assert(store);
254✔
28

29
        init_timestamp(store, t);
254✔
30

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

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

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

41
        assert(store);
428✔
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)
428✔
48
                return -errno;
×
49

50
        utmpx = utxent_start();
428✔
51

52
        if (pututxline(store))
428✔
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) {
428✔
63
        assert(store);
428✔
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;
428✔
69
        updwtmpx(WTMPX_FILE, store);
428✔
70
        if (errno == ENOENT) {
428✔
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");
428✔
73
                return 0;
428✔
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) {
428✔
83
        int r, s;
428✔
84

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

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

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

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

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

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

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

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

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

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

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

119
        l = strlen(src);
174✔
120
        if (l < buf_size)
174✔
121
                strncpy(buf, src, buf_size);
×
122
        else
123
                memcpy(buf, src + l - buf_size, buf_size);
174✔
124
}
174✔
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) {
160✔
127
        struct utmpx store = {
160✔
128
                .ut_type = INIT_PROCESS,
129
                .ut_pid = pid,
130
                .ut_session = sid,
131
        };
132
        int r;
160✔
133

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

137
        init_timestamp(&store, 0);
160✔
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);
160✔
141

142
        if (line)
160✔
143
                strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
158✔
144

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

149
        if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
160✔
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) {
160✔
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

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

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

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

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

UNCOV
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