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

systemd / systemd / 19448983682

17 Nov 2025 11:32PM UTC coverage: 72.503% (-0.2%) from 72.719%
19448983682

push

github

web-flow
core/unit: unit_process_job() tweaks (#39753)

6 of 8 new or added lines in 1 file covered. (75.0%)

3363 existing lines in 68 files now uncovered.

308308 of 425234 relevant lines covered (72.5%)

1141671.45 hits per line

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

90.37
/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) {
180,115✔
20
        _cleanup_fclose_ FILE *f = NULL;
180,115✔
21
        _cleanup_close_ int fd = -EBADF;
180,115✔
22
        int r;
180,115✔
23

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

27
        fd = openat(dir_fd, path, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600);
180,115✔
28
        if (fd < 0)
180,115✔
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);
180,114✔
35
        if (r < 0) {
180,114✔
36
                (void) unlinkat(dir_fd, path, 0);
×
37
                return r;
×
38
        }
39

40
        if (ret_file)
180,114✔
41
                *ret_file = TAKE_PTR(f);
180,114✔
42

43
        return 0;
44
}
45

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

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

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

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

61
        if (ret_path)
180,112✔
62
                *ret_path = TAKE_PTR(t);
180,112✔
63

64
        return 0;
65
}
66

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

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

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

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

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

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

90
        return 0;
91
}
92

93
/* This is much like mkostemp() but is subject to umask(). */
94
int mkostemp_safe(char *pattern) {
243✔
95
        assert(pattern);
243✔
96
        BLOCK_WITH_UMASK(0077);
243✔
97
        return RET_NERRNO(mkostemp(pattern, O_CLOEXEC));
243✔
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)[]) {
119✔
117
        assert(p);
119✔
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"))
119✔
124
                (void) unlink(*p);
117✔
125
}
119✔
126

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

132
        assert(p);
324,433✔
133
        assert(ret);
324,433✔
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, '/'))
324,433✔
145
                return -EINVAL;
146

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

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

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

159
        if (child) {
324,430✔
160
                d = strdup(p);
638✔
161
                if (!d)
638✔
162
                        return -ENOMEM;
163
        } else {
164
                r = path_extract_directory(p, &d);
323,792✔
165
                if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → No directory specified, just a filename */
323,792✔
166
                        return r;
167

168
                r = path_extract_filename(p, &fn);
323,784✔
169
                if (r < 0)
323,784✔
170
                        return r;
171

172
                if (strlen(fn) > NAME_MAX - len_add)
323,784✔
173
                        /* We cannot simply prepend and append strings to the filename. Let's truncate the filename. */
174
                        fn[NAME_MAX - len_add] = '\0';
4✔
175
        }
176

177
        nf = strjoin(".#", strempty(pre), strempty(fn), strempty(post));
648,728✔
178
        if (!nf)
324,422✔
179
                return -ENOMEM;
180

181
        if (d) {
324,422✔
182
                if (!path_extend(&d, nf))
319,075✔
183
                        return -ENOMEM;
184

185
                result = path_simplify(TAKE_PTR(d));
319,075✔
186
        } else
187
                result = TAKE_PTR(nf);
188

189
        if (!path_is_valid(result)) /* New path is not valid? (Maybe because too long?) Refuse. */
324,422✔
190
                return -EINVAL;
191

192
        *ret = TAKE_PTR(result);
324,419✔
193
        return 0;
324,419✔
194
}
195

196
int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
27✔
197
        /*
198
         * Turns this:
199
         *         /foo/bar/waldo
200
         *
201
         * Into this:
202
         *         /foo/bar/.#<extra>waldoXXXXXX
203
         */
204

205
        return tempfn_build(p, extra, "XXXXXX", /* child = */ false, ret);
27✔
206
}
207

208
int tempfn_random(const char *p, const char *extra, char **ret) {
323,767✔
209
        _cleanup_free_ char *s = NULL;
323,767✔
210

211
        assert(p);
323,767✔
212
        assert(ret);
323,767✔
213

214
        /*
215
         * Turns this:
216
         *         /foo/bar/waldo
217
         *
218
         * Into this:
219
         *         /foo/bar/.#<extra>waldobaa2a261115984a9
220
         */
221

222
        if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
323,767✔
223
                return -ENOMEM;
224

225
        return tempfn_build(p, extra, s, /* child = */ false, ret);
323,767✔
226
}
227

228
int tempfn_random_child(const char *p, const char *extra, char **ret) {
639✔
229
        _cleanup_free_ char *s = NULL;
639✔
230
        int r;
639✔
231

232
        assert(ret);
639✔
233

234
        /* Turns this:
235
         *         /foo/bar/waldo
236
         * Into this:
237
         *         /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
238
         */
239

240
        if (!p) {
639✔
241
                r = tmp_dir(&p);
15✔
242
                if (r < 0)
15✔
243
                        return r;
244
        }
245

246
        if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
639✔
247
                return -ENOMEM;
248

249
        return tempfn_build(p, extra, s, /* child = */ true, ret);
639✔
250
}
251

252
int open_tmpfile_unlinkable(const char *directory, int flags) {
6,371✔
253
        int r;
6,371✔
254

255
        if (!directory) {
6,371✔
256
                r = tmp_dir(&directory);
7✔
257
                if (r < 0)
7✔
258
                        return r;
6,371✔
259
        } else if (isempty(directory))
12,735✔
260
                return -EINVAL;
261

262
        /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
263

264
        /* Try O_TMPFILE first, if it is supported */
265
        int fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
6,371✔
266
        if (fd >= 0)
6,371✔
267
                return fd;
268

269
        /* Fall back to unguessable name + unlinking */
270
        _cleanup_free_ char *p = path_join(directory, "/systemd-tmp-XXXXXX");
×
271
        if (!p)
×
272
                return -ENOMEM;
273

274
        fd = mkostemp_safe(p);
×
275
        if (fd < 0)
×
276
                return fd;
277

278
        (void) unlink(p);
×
279

280
        return fd;
×
281
}
282

283
int open_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path) {
21,639✔
284
        int r;
21,639✔
285

286
        assert(target);
21,639✔
287
        assert(ret_path);
21,639✔
288

289
        /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
290
        assert((flags & O_EXCL) == 0);
21,639✔
291

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

296
        int fd = open_parent_at(dir_fd, target, O_TMPFILE|flags, 0640);
21,639✔
297
        if (fd >= 0) {
21,639✔
298
                *ret_path = NULL;
21,592✔
299
                return fd;
21,639✔
300
        }
301

302
        log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
47✔
303

304
        _cleanup_free_ char *tmp = NULL;
47✔
305
        r = tempfn_random(target, NULL, &tmp);
47✔
306
        if (r < 0)
47✔
307
                return r;
308

309
        fd = openat(dir_fd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
47✔
310
        if (fd < 0)
47✔
311
                return -errno;
33✔
312

313
        *ret_path = TAKE_PTR(tmp);
14✔
314

315
        return fd;
14✔
316
}
317

318
int fopen_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path, FILE **ret_file) {
5,410✔
319
        _cleanup_free_ char *path = NULL;
5,410✔
320
        _cleanup_fclose_ FILE *f = NULL;
5,410✔
321
        _cleanup_close_ int fd = -EBADF;
5,410✔
322

323
        assert(target);
5,410✔
324
        assert(ret_file);
5,410✔
325
        assert(ret_path);
5,410✔
326

327
        fd = open_tmpfile_linkable_at(dir_fd, target, flags, &path);
5,410✔
328
        if (fd < 0)
5,410✔
329
                return fd;
330

331
        f = take_fdopen(&fd, "w");
5,410✔
332
        if (!f)
5,410✔
333
                return -ENOMEM;
334

335
        *ret_path = TAKE_PTR(path);
5,410✔
336
        *ret_file = TAKE_PTR(f);
5,410✔
337
        return 0;
5,410✔
338
}
339

340
int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
21,550✔
341
        int r;
21,550✔
342

343
        assert(fd >= 0);
21,550✔
344
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
21,550✔
345
        assert(target);
21,550✔
346

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

351
        if (FLAGS_SET(flags, LINK_TMPFILE_SYNC) && fsync(fd) < 0)
21,550✔
352
                return -errno;
×
353

354
        if (path) {
21,550✔
355
                if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
14✔
356
                        r = RET_NERRNO(renameat(dir_fd, path, dir_fd, target));
×
357
                else
358
                        r = rename_noreplace(dir_fd, path, dir_fd, target);
14✔
359
        } else {
360
                if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
21,536✔
361
                        r = linkat_replace(fd, /* oldpath= */ NULL, dir_fd, target);
20,660✔
362
                else
363
                        r = link_fd(fd, dir_fd, target);
876✔
364
        }
365
        if (r < 0)
21,550✔
366
                return r;
367

368
        if (FLAGS_SET(flags, LINK_TMPFILE_SYNC)) {
21,547✔
369
                r = fsync_full(fd);
1,389✔
370
                if (r < 0)
1,389✔
371
                        return r;
×
372
        }
373

374
        return 0;
375
}
376

377
int flink_tmpfile_at(FILE *f, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
5,410✔
378
        int fd, r;
5,410✔
379

380
        assert(f);
5,410✔
381
        assert(target);
5,410✔
382

383
        fd = fileno(f);
5,410✔
384
        if (fd < 0) /* Not all FILE* objects encapsulate fds */
5,410✔
385
                return -EBADF;
386

387
        r = fflush_and_check(f);
5,410✔
388
        if (r < 0)
5,410✔
389
                return r;
390

391
        return link_tmpfile_at(fd, dir_fd, path, target, flags);
5,410✔
392
}
393

394
int mkdtemp_malloc(const char *template, char **ret) {
3,924✔
395
        _cleanup_free_ char *p = NULL;
7,848✔
396
        int r;
3,924✔
397

398
        assert(ret);
3,924✔
399

400
        if (template)
3,924✔
401
                p = strdup(template);
3,816✔
402
        else {
403
                const char *tmp;
108✔
404

405
                r = tmp_dir(&tmp);
108✔
406
                if (r < 0)
108✔
407
                        return r;
×
408

409
                p = path_join(tmp, "XXXXXX");
108✔
410
        }
411
        if (!p)
3,924✔
412
                return -ENOMEM;
413

414
        if (!mkdtemp(p))
3,924✔
415
                return -errno;
×
416

417
        *ret = TAKE_PTR(p);
3,924✔
418
        return 0;
3,924✔
419
}
420

421
int mkdtemp_open(const char *template, int flags, char **ret) {
46✔
422
        _cleanup_free_ char *p = NULL;
46✔
423
        int fd, r;
46✔
424

425
        r = mkdtemp_malloc(template, &p);
46✔
426
        if (r < 0)
46✔
427
                return r;
428

429
        fd = RET_NERRNO(open(p, O_DIRECTORY|O_CLOEXEC|flags));
46✔
430
        if (fd < 0) {
×
431
                (void) rmdir(p);
×
432
                return fd;
×
433
        }
434

435
        if (ret)
46✔
436
                *ret = TAKE_PTR(p);
46✔
437

438
        return fd;
439
}
440

441
void cleanup_tmpfile_data_done(struct cleanup_tmpfile_data *d) {
2,862✔
442
        assert(d);
2,862✔
443

444
        if (!d->dir_fd ||
2,862✔
445
            *d->dir_fd < 0 ||
2,862✔
446
            !d->filename ||
379✔
447
            !*d->filename)
379✔
448
                return;
2,862✔
449

450
        PROTECT_ERRNO;
×
451

452
        (void) unlinkat(*d->dir_fd, *d->filename, /* flags= */ 0);
×
UNCOV
453
        d->dir_fd = NULL;
×
UNCOV
454
        d->filename = NULL;
×
455
}
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