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

systemd / systemd / 17253546277

26 Aug 2025 06:24PM UTC coverage: 72.232% (-0.005%) from 72.237%
17253546277

push

github

bluca
shell-completion: support -i option for journalctl

Follow-up for dde54b8a8.

302507 of 418798 relevant lines covered (72.23%)

653328.97 hits per line

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

89.04
/src/basic/pidfd-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/fs.h>
4
#include <linux/magic.h>
5
#include <sys/ioctl.h>
6
#include <threads.h>
7
#include <unistd.h>
8

9
#include "errno-util.h"
10
#include "fd-util.h"
11
#include "fileio.h"
12
#include "mountpoint-util.h"
13
#include "parse-util.h"
14
#include "pidfd-util.h"
15
#include "process-util.h"
16
#include "stat-util.h"
17
#include "stdio-util.h"
18
#include "string-util.h"
19
#include "unaligned.h"
20

21
static thread_local int have_pidfs = -1;
22

23
int pidfd_check_pidfs(int pid_fd) {
37,867✔
24

25
        /* NB: the passed fd *must* be acquired via pidfd_open(), i.e. must be a true pidfd! */
26

27
        if (have_pidfs >= 0)
37,867✔
28
                return have_pidfs;
37,867✔
29

30
        _cleanup_close_ int our_fd = -EBADF;
37,867✔
31
        if (pid_fd < 0) {
17,433✔
32
                our_fd = pidfd_open(getpid_cached(), /* flags = */ 0);
1✔
33
                if (our_fd < 0)
1✔
34
                        return -errno;
×
35

36
                pid_fd = our_fd;
37
        }
38

39
        return (have_pidfs = fd_is_fs_type(pid_fd, PID_FS_MAGIC));
17,433✔
40
}
41

42
int pidfd_get_namespace(int fd, unsigned long ns_type_cmd) {
8,970✔
43
        static bool cached_supported = true;
8,970✔
44

45
        /* Obtain the namespace fd from pidfd directly through ioctl(PIDFD_GET_*_NAMESPACE).
46
         *
47
         * Returns -EOPNOTSUPP if ioctl on pidfds are not supported, -ENOPKG if the requested namespace
48
         * is disabled in kernel. (The errno used are different from what kernel returns via ioctl(),
49
         * see below) */
50

51
        assert(fd >= 0);
8,970✔
52

53
        /* If we know ahead of time that pidfs is unavailable, shortcut things. But otherwise we don't
54
         * call pidfd_check_pidfs() here, which is kinda extraneous and our own cache is required
55
         * anyways (pidfs is introduced in kernel 6.9 while ioctl support there is added in 6.11). */
56
        if (have_pidfs == 0 || !cached_supported)
8,970✔
57
                return -EOPNOTSUPP;
58

59
        int nsfd = ioctl(fd, ns_type_cmd);
3,688✔
60
        if (nsfd < 0) {
3,688✔
61
                /* Kernel returns EOPNOTSUPP if the ns type in question is disabled. Hence we need to look
62
                 * at precise errno instead of generic ERRNO_IS_(IOCTL_)NOT_SUPPORTED. */
63
                if (IN_SET(errno, ENOTTY, EINVAL)) {
3,688✔
64
                        cached_supported = false;
3,688✔
65
                        return -EOPNOTSUPP;
3,688✔
66
                }
67
                if (errno == EOPNOTSUPP) /* Translate to something more recognizable */
×
68
                        return -ENOPKG;
69

70
                return -errno;
×
71
        }
72

73
        return nsfd;
74
}
75

76
static int pidfd_get_info(int fd, struct pidfd_info *info) {
59,065✔
77
        static bool cached_supported = true;
59,065✔
78

79
        assert(fd >= 0);
59,065✔
80
        assert(info);
59,065✔
81

82
        if (have_pidfs == 0 || !cached_supported)
59,065✔
83
                return -EOPNOTSUPP;
84

85
        if (ioctl(fd, PIDFD_GET_INFO, info) < 0) {
44,022✔
86
                if (ERRNO_IS_IOCTL_NOT_SUPPORTED(errno)) {
4,564✔
87
                        cached_supported = false;
3,768✔
88
                        return -EOPNOTSUPP;
3,768✔
89
                }
90

91
                return -errno;
796✔
92
        }
93

94
        return 0;
95
}
96

97
static int pidfd_get_pid_fdinfo(int fd, pid_t *ret) {
17,634✔
98
        char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
17,634✔
99
        _cleanup_free_ char *p = NULL;
17,634✔
100
        int r;
17,634✔
101

102
        assert(fd >= 0);
17,634✔
103

104
        xsprintf(path, "/proc/self/fdinfo/%i", fd);
17,634✔
105

106
        r = get_proc_field(path, "Pid", &p);
17,634✔
107
        if (r == -ENOENT)
17,634✔
108
                return -EBADF;
109
        if (r == -ENODATA) /* not a pidfd? */
17,634✔
110
                return -ENOTTY;
111
        if (r < 0)
17,634✔
112
                return r;
113

114
        if (streq(p, "0"))
17,634✔
115
                return -EREMOTE; /* PID is in foreign PID namespace? */
116
        if (streq(p, "-1"))
17,634✔
117
                return -ESRCH;   /* refers to reaped process? */
118

119
        return parse_pid(p, ret);
16,061✔
120
}
121

122
static int pidfd_get_pid_ioctl(int fd, pid_t *ret) {
56,883✔
123
        struct pidfd_info info = { .mask = PIDFD_INFO_PID };
56,883✔
124
        int r;
56,883✔
125

126
        assert(fd >= 0);
56,883✔
127

128
        r = pidfd_get_info(fd, &info);
56,883✔
129
        if (r < 0)
56,883✔
130
                return r;
56,883✔
131

132
        assert(FLAGS_SET(info.mask, PIDFD_INFO_PID));
38,453✔
133

134
        if (ret)
38,453✔
135
                *ret = info.pid;
38,453✔
136
        return 0;
137
}
138

139
int pidfd_get_pid(int fd, pid_t *ret) {
56,883✔
140
        int r;
56,883✔
141

142
        /* Converts a pidfd into a pid. We try ioctl(PIDFD_GET_INFO) (kernel 6.13+) first,
143
         * /proc/self/fdinfo/ as fallback. Well known errors:
144
         *
145
         *    -EBADF   → fd invalid
146
         *    -ESRCH   → fd valid, but process is already reaped
147
         *
148
         * pidfd_get_pid_fdinfo() might additionally fail for other reasons:
149
         *
150
         *    -ENOSYS  → /proc/ not mounted
151
         *    -ENOTTY  → fd valid, but not a pidfd
152
         *    -EREMOTE → fd valid, but pid is in another namespace we cannot translate to the local one
153
         *               (when using PIDFD_GET_INFO this is indistinguishable from -ESRCH)
154
         */
155

156
        assert(fd >= 0);
56,883✔
157

158
        r = pidfd_get_pid_ioctl(fd, ret);
56,883✔
159
        if (r != -EOPNOTSUPP)
56,883✔
160
                return r;
161

162
        return pidfd_get_pid_fdinfo(fd, ret);
17,634✔
163
}
164

165
int pidfd_verify_pid(int pidfd, pid_t pid) {
39,564✔
166
        pid_t current_pid;
39,564✔
167
        int r;
39,564✔
168

169
        assert(pidfd >= 0);
39,564✔
170
        assert(pid > 0);
39,564✔
171

172
        r = pidfd_get_pid(pidfd, &current_pid);
39,564✔
173
        if (r < 0)
39,564✔
174
                return r;
39,564✔
175

176
        return current_pid != pid ? -ESRCH : 0;
37,195✔
177
}
178

179
int pidfd_get_ppid(int fd, pid_t *ret) {
2,118✔
180
        struct pidfd_info info = { .mask = PIDFD_INFO_PID };
2,118✔
181
        int r;
2,118✔
182

183
        assert(fd >= 0);
2,118✔
184

185
        r = pidfd_get_info(fd, &info);
2,118✔
186
        if (r < 0)
2,118✔
187
                return r;
2,118✔
188

189
        assert(FLAGS_SET(info.mask, PIDFD_INFO_PID));
950✔
190

191
        if (info.ppid == 0) /* See comments in pid_get_ppid() */
950✔
192
                return -EADDRNOTAVAIL;
193

194
        if (ret)
950✔
195
                *ret = info.ppid;
950✔
196
        return 0;
197
}
198

199
int pidfd_get_uid(int fd, uid_t *ret) {
64✔
200
        struct pidfd_info info = { .mask = PIDFD_INFO_CREDS };
64✔
201
        int r;
64✔
202

203
        assert(fd >= 0);
64✔
204

205
        r = pidfd_get_info(fd, &info);
64✔
206
        if (r < 0)
64✔
207
                return r;
64✔
208

209
        assert(FLAGS_SET(info.mask, PIDFD_INFO_CREDS));
55✔
210

211
        if (ret)
55✔
212
                *ret = info.ruid;
55✔
213
        return 0;
214
}
215

216
int pidfd_get_cgroupid(int fd, uint64_t *ret) {
×
217
        struct pidfd_info info = { .mask = PIDFD_INFO_CGROUPID };
×
218
        int r;
×
219

220
        assert(fd >= 0);
×
221

222
        r = pidfd_get_info(fd, &info);
×
223
        if (r < 0)
×
224
                return r;
×
225

226
        assert(FLAGS_SET(info.mask, PIDFD_INFO_CGROUPID));
×
227

228
        if (ret)
×
229
                *ret = info.cgroupid;
×
230
        return 0;
231
}
232

233
int pidfd_get_inode_id_impl(int fd, uint64_t *ret) {
37,866✔
234
        static thread_local bool file_handle_supported = true;
37,866✔
235
        int r;
37,866✔
236

237
        assert(fd >= 0);
37,866✔
238

239
        if (file_handle_supported) {
37,866✔
240
                union {
35,651✔
241
                        struct file_handle file_handle;
242
                        uint8_t space[offsetof(struct file_handle, f_handle) + sizeof(uint64_t)];
243
                } fh = {
35,651✔
244
                        .file_handle.handle_bytes = sizeof(uint64_t),
245
                        .file_handle.handle_type = FILEID_KERNFS,
246
                };
247
                int mnt_id;
35,651✔
248

249
                r = RET_NERRNO(name_to_handle_at(fd, "", &fh.file_handle, &mnt_id, AT_EMPTY_PATH));
35,651✔
250
                if (r >= 0) {
3,975✔
251
                        if (ret)
31,676✔
252
                                /* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */
253
                                *ret = unaligned_read_ne64(fh.file_handle.f_handle);
31,676✔
254
                        return 0;
31,676✔
255
                }
256
                assert(r != -EOVERFLOW);
3,975✔
257
                if (is_name_to_handle_at_fatal_error(r))
3,975✔
258
                        return r;
259

260
                file_handle_supported = false;
3,975✔
261
        }
262

263
#if SIZEOF_INO_T == 8
264
        struct stat st;
6,190✔
265
        if (fstat(fd, &st) < 0)
6,190✔
266
                return -errno;
×
267

268
        if (ret)
6,190✔
269
                *ret = (uint64_t) st.st_ino;
6,189✔
270
        return 0;
271

272
#elif SIZEOF_INO_T == 4
273
        /* On 32-bit systems (where sizeof(ino_t) == 4), the inode id returned by fstat() cannot be used to
274
         * reliably identify the process, nor can we communicate the origin of the id with the clients.
275
         * Hence let's just refuse to acquire pidfdid through fstat() here. All clients shall also insist on
276
         * the 64-bit id from name_to_handle_at(). */
277
        return -EOPNOTSUPP;
278
#else
279
#  error Unsupported ino_t size
280
#endif
281
}
282

283
int pidfd_get_inode_id(int fd, uint64_t *ret) {
37,865✔
284
        int r;
37,865✔
285

286
        assert(fd >= 0);
37,865✔
287

288
        r = pidfd_check_pidfs(fd);
37,865✔
289
        if (r < 0)
37,865✔
290
                return r;
291
        if (r == 0)
37,865✔
292
                return -EOPNOTSUPP;
293

294
        return pidfd_get_inode_id_impl(fd, ret);
37,865✔
295
}
296

297
int pidfd_get_inode_id_self_cached(uint64_t *ret) {
37,521✔
298
        static thread_local uint64_t cached = 0;
37,521✔
299
        static thread_local pid_t initialized = 0; /* < 0: cached error; == 0: invalid; > 0: valid and pid that was current */
37,521✔
300
        int r;
37,521✔
301

302
        assert(ret);
37,521✔
303

304
        if (initialized == getpid_cached()) {
37,521✔
305
                *ret = cached;
5,020✔
306
                return 0;
5,020✔
307
        }
308
        if (initialized < 0)
32,501✔
309
                return initialized;
310

311
        _cleanup_close_ int fd = pidfd_open(getpid_cached(), 0);
70,022✔
312
        if (fd < 0)
32,501✔
313
                return -errno;
×
314

315
        r = pidfd_get_inode_id(fd, &cached);
32,501✔
316
        if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
32,501✔
317
                return (initialized = -EOPNOTSUPP);
×
318
        if (r < 0)
32,501✔
319
                return r;
320

321
        *ret = cached;
32,501✔
322
        initialized = getpid_cached();
32,501✔
323
        return 0;
32,501✔
324
}
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