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

systemd / systemd / 18810271929

25 Oct 2025 04:50PM UTC coverage: 72.26%. Remained the same
18810271929

push

github

YHNdnzj
core/exec-invoke: relax restriction for process name length

Previously, we limit the length of process name by 8.
This relax the restriction then at least process comm or
program_invocation_name contains the untrucated process name.

Closes #38367.

24 of 28 new or added lines in 3 files covered. (85.71%)

486 existing lines in 52 files now uncovered.

304829 of 421852 relevant lines covered (72.26%)

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

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

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

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

27
        assert(store);
234✔
28

29
        init_timestamp(store, t);
234✔
30

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

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

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

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

50
        utmpx = utxent_start();
418✔
51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

142
        if (line)
170✔
143
                strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
168✔
144

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

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