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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

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

3
#include <stdlib.h>
4
#include <unistd.h>
5

6
#include "alloc-util.h"
7
#include "errno-util.h"
8
#include "fd-util.h"
9
#include "fileio.h"
10
#include "fs-util.h"
11
#include "log.h"
12
#include "path-util.h"
13
#include "random-util.h"
14
#include "string-util.h"
15
#include "sync-util.h"
16
#include "tmpfile-util.h"
17
#include "umask-util.h"
18

19
static int fopen_temporary_internal(int dir_fd, const char *path, FILE **ret_file) {
183,222✔
20
        _cleanup_fclose_ FILE *f = NULL;
183,222✔
21
        _cleanup_close_ int fd = -EBADF;
183,222✔
22
        int r;
183,222✔
23

24
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
183,222✔
25
        assert(path);
183,222✔
26

27
        fd = openat(dir_fd, path, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600);
183,222✔
28
        if (fd < 0)
183,222✔
29
                return -errno;
1✔
30

31
        /* This assumes that returned FILE object is short-lived and used within the same single-threaded
32
         * context and never shared externally, hence locking is not necessary. */
33

34
        r = take_fdopen_unlocked(&fd, "w", &f);
183,221✔
35
        if (r < 0) {
183,221✔
36
                (void) unlinkat(dir_fd, path, 0);
×
37
                return r;
×
38
        }
39

40
        if (ret_file)
183,221✔
41
                *ret_file = TAKE_PTR(f);
183,221✔
42

43
        return 0;
44
}
45

46
int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
183,218✔
47
        _cleanup_free_ char *t = NULL;
183,218✔
48
        int r;
183,218✔
49

50
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
183,218✔
51
        assert(path);
183,218✔
52

53
        r = tempfn_random(path, NULL, &t);
183,218✔
54
        if (r < 0)
183,218✔
55
                return r;
56

57
        r = fopen_temporary_internal(dir_fd, t, ret_file);
183,218✔
58
        if (r < 0)
183,218✔
59
                return r;
60

61
        if (ret_path)
183,217✔
62
                *ret_path = TAKE_PTR(t);
183,217✔
63

64
        return 0;
65
}
66

67
int fopen_temporary_child_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
4✔
68
        _cleanup_free_ char *t = NULL;
4✔
69
        int r;
4✔
70

71
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
4✔
72

73
        if (!path) {
4✔
74
                r = tmp_dir(&path);
1✔
75
                if (r < 0)
1✔
76
                        return r;
77
        }
78

79
        r = tempfn_random_child(path, NULL, &t);
4✔
80
        if (r < 0)
4✔
81
                return r;
82

83
        r = fopen_temporary_internal(dir_fd, t, ret_file);
4✔
84
        if (r < 0)
4✔
85
                return r;
86

87
        if (ret_path)
4✔
88
                *ret_path = TAKE_PTR(t);
4✔
89

90
        return 0;
91
}
92

93
/* This is much like mkostemp() but is subject to umask(). */
94
int mkostemp_safe(char *pattern) {
248✔
95
        assert(pattern);
248✔
96
        BLOCK_WITH_UMASK(0077);
248✔
97
        return RET_NERRNO(mkostemp(pattern, O_CLOEXEC));
248✔
98
}
99

100
int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
40✔
101
        _cleanup_close_ int fd = -EBADF;
40✔
102
        FILE *f;
40✔
103

104
        fd = mkostemp_safe(pattern);
40✔
105
        if (fd < 0)
40✔
106
                return fd;
107

108
        f = take_fdopen(&fd, mode);
40✔
109
        if (!f)
40✔
110
                return -errno;
×
111

112
        *ret_f = f;
40✔
113
        return 0;
40✔
114
}
115

116
void unlink_tempfilep(char (*p)[]) {
120✔
117
        assert(p);
120✔
118

119
        /* If the file is created with mkstemp(), it will (almost always) change the suffix.
120
         * Treat this as a sign that the file was successfully created. We ignore both the rare case
121
         * where the original suffix is used and unlink failures. */
122

123
        if (!endswith(*p, ".XXXXXX"))
120✔
124
                (void) unlink(*p);
118✔
125
}
120✔
126

127
static int tempfn_build(const char *p, const char *pre, const char *post, bool child, char **ret) {
329,929✔
128
        _cleanup_free_ char *d = NULL, *fn = NULL, *nf = NULL, *result = NULL;
329,929✔
129
        size_t len_pre, len_post, len_add;
329,929✔
130
        int r;
329,929✔
131

132
        assert(p);
329,929✔
133
        assert(ret);
329,929✔
134

135
        /*
136
         * Turns this:
137
         *         /foo/bar/waldo
138
         *
139
         * Into this :
140
         *         /foo/bar/waldo/.#<pre><post> (child == true)
141
         *         /foo/bar/.#<pre>waldo<post> (child == false)
142
         */
143

144
        if (pre && strchr(pre, '/'))
329,929✔
145
                return -EINVAL;
146

147
        if (post && strchr(post, '/'))
329,926✔
148
                return -EINVAL;
149

150
        len_pre = strlen_ptr(pre);
329,926✔
151
        len_post = strlen_ptr(post);
329,926✔
152
        /* NAME_MAX is counted *without* the trailing NUL byte. */
153
        if (len_pre > NAME_MAX - STRLEN(".#") ||
329,926✔
154
            len_post > NAME_MAX - STRLEN(".#") - len_pre)
329,926✔
155
                return -EINVAL;
156

157
        len_add = len_pre + len_post + STRLEN(".#");
329,926✔
158

159
        if (child) {
329,926✔
160
                d = strdup(p);
659✔
161
                if (!d)
659✔
162
                        return -ENOMEM;
163
        } else {
164
                r = path_split_prefix_filename(p, &d, &fn);
329,267✔
165
                if (r < 0)
329,267✔
166
                        return r;
167

168
                /* Truncate the filename if it would become too long after mangling. */
169
                strshorten(fn, NAME_MAX - len_add);
329,259✔
170
        }
171

172
        nf = strjoin(".#", strempty(pre), strempty(fn), strempty(post));
659,730✔
173
        if (!nf)
329,918✔
174
                return -ENOMEM;
175

176
        if (d) {
329,918✔
177
                if (!path_extend(&d, nf))
324,449✔
178
                        return -ENOMEM;
179

180
                result = path_simplify(TAKE_PTR(d));
324,449✔
181
        } else
182
                result = TAKE_PTR(nf);
183

184
        if (!path_is_valid(result)) /* New path is not valid? (Maybe because too long?) Refuse. */
329,918✔
185
                return -EINVAL;
186

187
        *ret = TAKE_PTR(result);
329,915✔
188
        return 0;
329,915✔
189
}
190

191
int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
27✔
192
        /*
193
         * Turns this:
194
         *         /foo/bar/waldo
195
         *
196
         * Into this:
197
         *         /foo/bar/.#<extra>waldoXXXXXX
198
         */
199

200
        return tempfn_build(p, extra, "XXXXXX", /* child= */ false, ret);
27✔
201
}
202

203
int tempfn_random(const char *p, const char *extra, char **ret) {
329,242✔
204
        _cleanup_free_ char *s = NULL;
329,242✔
205

206
        assert(p);
329,242✔
207
        assert(ret);
329,242✔
208

209
        /*
210
         * Turns this:
211
         *         /foo/bar/waldo
212
         *
213
         * Into this:
214
         *         /foo/bar/.#<extra>waldobaa2a261115984a9
215
         */
216

217
        if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
329,242✔
218
                return -ENOMEM;
219

220
        return tempfn_build(p, extra, s, /* child= */ false, ret);
329,242✔
221
}
222

223
int tempfn_random_child(const char *p, const char *extra, char **ret) {
660✔
224
        _cleanup_free_ char *s = NULL;
660✔
225
        int r;
660✔
226

227
        assert(ret);
660✔
228

229
        /* Turns this:
230
         *         /foo/bar/waldo
231
         * Into this:
232
         *         /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
233
         */
234

235
        if (!p) {
660✔
236
                r = tmp_dir(&p);
15✔
237
                if (r < 0)
15✔
238
                        return r;
239
        }
240

241
        if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
660✔
242
                return -ENOMEM;
243

244
        return tempfn_build(p, extra, s, /* child= */ true, ret);
660✔
245
}
246

247
int open_tmpfile_unlinkable(const char *directory, int flags) {
6,371✔
248
        int r;
6,371✔
249

250
        if (!directory) {
6,371✔
251
                r = tmp_dir(&directory);
7✔
252
                if (r < 0)
7✔
253
                        return r;
6,371✔
254
        } else if (isempty(directory))
12,735✔
255
                return -EINVAL;
256

257
        /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
258

259
        /* Try O_TMPFILE first, if it is supported */
260
        int fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
6,371✔
261
        if (fd >= 0)
6,371✔
262
                return fd;
263

264
        /* Fall back to unguessable name + unlinking */
265
        _cleanup_free_ char *p = path_join(directory, "/systemd-tmp-XXXXXX");
×
266
        if (!p)
×
267
                return -ENOMEM;
268

269
        fd = mkostemp_safe(p);
×
270
        if (fd < 0)
×
271
                return fd;
272

273
        (void) unlink(p);
×
274

275
        return fd;
×
276
}
277

278
int open_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path) {
22,046✔
279
        int r;
22,046✔
280

281
        assert(target);
22,046✔
282
        assert(ret_path);
22,046✔
283

284
        /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
285
        assert((flags & O_EXCL) == 0);
22,046✔
286

287
        /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
288
         * which case "ret_path" will be returned as NULL. If not possible the temporary path name used is returned in
289
         * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
290

291
        int fd = open_parent_at(dir_fd, target, O_TMPFILE|flags, 0640);
22,046✔
292
        if (fd >= 0) {
22,046✔
293
                *ret_path = NULL;
21,954✔
294
                return fd;
22,046✔
295
        }
296

297
        log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
92✔
298

299
        _cleanup_free_ char *tmp = NULL;
92✔
300
        r = tempfn_random(target, NULL, &tmp);
92✔
301
        if (r < 0)
92✔
302
                return r;
303

304
        fd = openat(dir_fd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
92✔
305
        if (fd < 0)
92✔
306
                return -errno;
33✔
307

308
        *ret_path = TAKE_PTR(tmp);
59✔
309

310
        return fd;
59✔
311
}
312

313
int fopen_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path, FILE **ret_file) {
5,472✔
314
        _cleanup_free_ char *path = NULL;
5,472✔
315
        _cleanup_fclose_ FILE *f = NULL;
5,472✔
316
        _cleanup_close_ int fd = -EBADF;
5,472✔
317

318
        assert(target);
5,472✔
319
        assert(ret_file);
5,472✔
320
        assert(ret_path);
5,472✔
321

322
        fd = open_tmpfile_linkable_at(dir_fd, target, flags, &path);
5,472✔
323
        if (fd < 0)
5,472✔
324
                return fd;
325

326
        f = take_fdopen(&fd, "w");
5,472✔
327
        if (!f)
5,472✔
328
                return -ENOMEM;
329

330
        *ret_path = TAKE_PTR(path);
5,472✔
331
        *ret_file = TAKE_PTR(f);
5,472✔
332
        return 0;
5,472✔
333
}
334

335
int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
21,857✔
336
        int r;
21,857✔
337

338
        assert(fd >= 0);
21,857✔
339
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
21,857✔
340
        assert(target);
21,857✔
341

342
        /* Moves a temporary file created with open_tmpfile() above into its final place. If "path" is NULL
343
         * an fd created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE
344
         * is not supported on the directory, and renameat2() is used instead. */
345

346
        if (FLAGS_SET(flags, LINK_TMPFILE_SYNC) && fsync(fd) < 0)
21,857✔
347
                return -errno;
×
348

349
        if (path) {
21,857✔
350
                if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
59✔
351
                        r = RET_NERRNO(renameat(dir_fd, path, dir_fd, target));
38✔
352
                else
353
                        r = rename_noreplace(dir_fd, path, dir_fd, target);
21✔
354
        } else {
355
                if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
21,798✔
356
                        r = linkat_replace(fd, /* oldpath= */ NULL, dir_fd, target);
20,893✔
357
                else
358
                        r = link_fd(fd, dir_fd, target);
905✔
359
        }
360
        if (r < 0)
21,819✔
361
                return r;
362

363
        if (FLAGS_SET(flags, LINK_TMPFILE_SYNC)) {
21,854✔
364
                r = fsync_full(fd);
1,460✔
365
                if (r < 0)
1,460✔
366
                        return r;
×
367
        }
368

369
        return 0;
370
}
371

372
int flink_tmpfile_at(FILE *f, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
5,472✔
373
        int fd, r;
5,472✔
374

375
        assert(f);
5,472✔
376
        assert(target);
5,472✔
377

378
        fd = fileno(f);
5,472✔
379
        if (fd < 0) /* Not all FILE* objects encapsulate fds */
5,472✔
380
                return -EBADF;
381

382
        r = fflush_and_check(f);
5,472✔
383
        if (r < 0)
5,472✔
384
                return r;
385

386
        return link_tmpfile_at(fd, dir_fd, path, target, flags);
5,472✔
387
}
388

389
int mkdtemp_malloc(const char *template, char **ret) {
4,009✔
390
        _cleanup_free_ char *p = NULL;
8,018✔
391
        int r;
4,009✔
392

393
        assert(ret);
4,009✔
394

395
        if (template)
4,009✔
396
                p = strdup(template);
3,880✔
397
        else {
398
                const char *tmp;
129✔
399

400
                r = tmp_dir(&tmp);
129✔
401
                if (r < 0)
129✔
402
                        return r;
×
403

404
                p = path_join(tmp, "XXXXXX");
129✔
405
        }
406
        if (!p)
4,009✔
407
                return -ENOMEM;
408

409
        if (!mkdtemp(p))
4,009✔
410
                return -errno;
×
411

412
        *ret = TAKE_PTR(p);
4,009✔
413
        return 0;
4,009✔
414
}
415

416
int mkdtemp_open(const char *template, int flags, char **ret) {
48✔
417
        _cleanup_free_ char *p = NULL;
48✔
418
        int fd, r;
48✔
419

420
        r = mkdtemp_malloc(template, &p);
48✔
421
        if (r < 0)
48✔
422
                return r;
423

424
        fd = RET_NERRNO(open(p, O_DIRECTORY|O_CLOEXEC|flags));
48✔
425
        if (fd < 0) {
×
426
                (void) rmdir(p);
×
427
                return fd;
×
428
        }
429

430
        if (ret)
48✔
431
                *ret = TAKE_PTR(p);
48✔
432

433
        return fd;
434
}
435

436
void cleanup_tmpfile_data_done(struct cleanup_tmpfile_data *d) {
3,007✔
437
        assert(d);
3,007✔
438

439
        if (!d->dir_fd ||
3,007✔
440
            *d->dir_fd < 0 ||
3,007✔
441
            !d->filename ||
509✔
442
            !*d->filename)
509✔
443
                return;
3,007✔
444

445
        PROTECT_ERRNO;
×
446

447
        (void) unlinkat(*d->dir_fd, *d->filename, /* flags= */ 0);
×
448
        d->dir_fd = NULL;
×
449
        d->filename = NULL;
×
450
}
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