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

systemd / systemd / 13424846548

19 Feb 2025 10:09PM UTC coverage: 71.768% (+0.02%) from 71.753%
13424846548

push

github

web-flow
tree-wide: tweaks to mount point inode creation (#36308)

Some love for make_mount_point_inode_from_xyz() and ports PID 1 over to
it for mount units.

Alternative to #36290

58 of 91 new or added lines in 7 files covered. (63.74%)

1322 existing lines in 48 files now uncovered.

293681 of 409206 relevant lines covered (71.77%)

717000.31 hits per line

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

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

3
#include <errno.h>
4
#include <stddef.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <sys/time.h>
8
#include <sys/utsname.h>
9
#include <unistd.h>
10
#include <utmpx.h>
11

12
#include "alloc-util.h"
13
#include "errno-util.h"
14
#include "hostname-util.h"
15
#include "macro.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"
22

23
int utmp_get_runlevel(int *runlevel, int *previous) {
127✔
24
        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
127✔
25
        struct utmpx *found, lookup = { .ut_type = RUN_LVL };
127✔
26
        const char *e;
127✔
27

28
        assert(runlevel);
127✔
29

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. */
35

36
        e = getenv("RUNLEVEL");
127✔
37
        if (!isempty(e)) {
127✔
38
                *runlevel = e[0];
×
39
                if (previous)
×
40
                        *previous = 0;
×
41

42
                return 0;
×
43
        }
44

45
        if (utmpxname(UTMPX_FILE) < 0)
127✔
46
                return -errno;
×
47

48
        utmpx = utxent_start();
127✔
49

50
        found = getutxid(&lookup);
127✔
51
        if (!found)
127✔
52
                return -errno;
7✔
53

54
        *runlevel = found->ut_pid & 0xFF;
120✔
55
        if (previous)
120✔
56
                *previous = (found->ut_pid >> 8) & 0xFF;
×
57

58
        return 0;
59
}
60

61
static void init_timestamp(struct utmpx *store, usec_t t) {
377✔
62
        assert(store);
377✔
63

64
        if (t <= 0)
377✔
65
                t = now(CLOCK_REALTIME);
262✔
66

67
        store->ut_tv.tv_sec = t / USEC_PER_SEC;
377✔
68
        store->ut_tv.tv_usec = t % USEC_PER_SEC;
377✔
69
}
377✔
70

71
static void init_entry(struct utmpx *store, usec_t t) {
231✔
72
        struct utsname uts = {};
231✔
73

74
        assert(store);
231✔
75

76
        init_timestamp(store, t);
231✔
77

78
        if (uname(&uts) >= 0)
231✔
79
                strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
231✔
80

81
        strncpy(store->ut_line, "~", sizeof(store->ut_line));  /* or ~~ ? */
231✔
82
        strncpy(store->ut_id, "~~", sizeof(store->ut_id));
231✔
83
}
231✔
84

85
static int write_entry_utmp(const struct utmpx *store) {
389✔
86
        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
389✔
87

88
        assert(store);
389✔
89

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
92
         * table. */
93

94
        if (utmpxname(UTMPX_FILE) < 0)
389✔
95
                return -errno;
×
96

97
        utmpx = utxent_start();
389✔
98

99
        if (pututxline(store))
389✔
100
                return 0;
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");
×
104
                return 0;
×
105
        }
106
        return -errno;
×
107
}
108

109
static int write_entry_wtmp(const struct utmpx *store) {
389✔
110
        assert(store);
389✔
111

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

115
        errno = 0;
389✔
116
        updwtmpx(_PATH_WTMPX, store);
389✔
117
        if (errno == ENOENT) {
389✔
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");
389✔
120
                return 0;
389✔
121
        }
122
        if (errno == EROFS) {
×
123
                log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m");
×
124
                return 0;
×
125
        }
126
        return -errno;
×
127
}
128

129
static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
389✔
130
        int r, s;
389✔
131

132
        r = write_entry_utmp(store_utmp);
389✔
133
        s = write_entry_wtmp(store_wtmp);
389✔
134
        return r < 0 ? r : s;
389✔
135
}
136

137
static int write_entry_both(const struct utmpx *store) {
389✔
138
        return write_utmp_wtmp(store, store);
389✔
139
}
140

141
int utmp_put_shutdown(void) {
115✔
142
        struct utmpx store = {};
115✔
143

144
        init_entry(&store, 0);
115✔
145

146
        store.ut_type = RUN_LVL;
115✔
147
        strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
115✔
148

149
        return write_entry_both(&store);
115✔
150
}
151

152
int utmp_put_reboot(usec_t t) {
115✔
153
        struct utmpx store = {};
115✔
154

155
        init_entry(&store, t);
115✔
156

157
        store.ut_type = BOOT_TIME;
115✔
158
        strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
115✔
159

160
        return write_entry_both(&store);
115✔
161
}
162

163
static void copy_suffix(char *buf, size_t buf_size, const char *src) {
160✔
164
        size_t l;
160✔
165

166
        l = strlen(src);
160✔
167
        if (l < buf_size)
160✔
168
                strncpy(buf, src, buf_size);
×
169
        else
170
                memcpy(buf, src + l - buf_size, buf_size);
160✔
171
}
160✔
172

173
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
146✔
174
        struct utmpx store = {
146✔
175
                .ut_type = INIT_PROCESS,
176
                .ut_pid = pid,
177
                .ut_session = sid,
178
        };
179
        int r;
146✔
180

181
        assert(id);
146✔
182
        assert(ut_type != USER_PROCESS || user);
146✔
183

184
        init_timestamp(&store, 0);
146✔
185

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);
146✔
188

189
        if (line)
146✔
190
                strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
144✔
191

192
        r = write_entry_both(&store);
146✔
193
        if (r < 0)
146✔
194
                return r;
146✔
195

196
        if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
146✔
197
                store.ut_type = LOGIN_PROCESS;
6✔
198
                r = write_entry_both(&store);
6✔
199
                if (r < 0)
6✔
200
                        return r;
201
        }
202

203
        if (ut_type == USER_PROCESS) {
146✔
204
                store.ut_type = USER_PROCESS;
6✔
205
                strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
6✔
206
                r = write_entry_both(&store);
6✔
207
                if (r < 0)
6✔
208
                        return r;
×
209
        }
210

211
        return 0;
212
}
213

214
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
14✔
215
        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
14✔
216
        struct utmpx lookup = {
14✔
217
                .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
218
        }, store, store_wtmp, *found;
219

220
        assert(id);
14✔
221

222
        utmpx = utxent_start();
14✔
223

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);
14✔
226

227
        found = getutxid(&lookup);
14✔
228
        if (!found)
14✔
229
                return 0;
230

UNCOV
231
        if (found->ut_pid != pid)
×
232
                return 0;
233

UNCOV
234
        memcpy(&store, found, sizeof(store));
×
UNCOV
235
        store.ut_type = DEAD_PROCESS;
×
UNCOV
236
        store.ut_exit.e_termination = code;
×
UNCOV
237
        store.ut_exit.e_exit = status;
×
238

UNCOV
239
        zero(store.ut_user);
×
UNCOV
240
        zero(store.ut_host);
×
UNCOV
241
        zero(store.ut_tv);
×
242

UNCOV
243
        memcpy(&store_wtmp, &store, sizeof(store_wtmp));
×
244
        /* wtmp wants the current time */
UNCOV
245
        init_timestamp(&store_wtmp, 0);
×
246

UNCOV
247
        return write_utmp_wtmp(&store, &store_wtmp);
×
248
}
249

250
int utmp_put_runlevel(int runlevel, int previous) {
1✔
251
        struct utmpx store = {};
1✔
252
        int r;
1✔
253

254
        assert(runlevel > 0);
1✔
255

256
        if (previous <= 0) {
1✔
257
                /* Find the old runlevel automatically */
258

259
                r = utmp_get_runlevel(&previous, NULL);
1✔
260
                if (r < 0) {
1✔
261
                        if (r != -ESRCH)
1✔
262
                                return r;
1✔
263

264
                        previous = 0;
1✔
265
                }
266
        }
267

268
        if (previous == runlevel)
1✔
269
                return 0;
270

271
        init_entry(&store, 0);
1✔
272

273
        store.ut_type = RUN_LVL;
1✔
274
        store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
1✔
275
        strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
1✔
276

277
        return write_entry_both(&store);
1✔
278
}
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