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

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

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

82.12
/src/shared/edit-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <sys/stat.h>
6

7
#include "alloc-util.h"
8
#include "copy.h"
9
#include "edit-util.h"
10
#include "errno-util.h"
11
#include "fd-util.h"
12
#include "fileio.h"
13
#include "fs-util.h"
14
#include "log.h"
15
#include "mkdir-label.h"
16
#include "path-util.h"
17
#include "process-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20
#include "tmpfile-util-label.h"
21

22
typedef struct EditFile {
23
        EditFileContext *context;
24
        char *path;
25
        char *original_path;
26
        char **comment_paths;
27
        char *temp;
28
        unsigned line;
29
} EditFile;
30

31
void edit_file_context_done(EditFileContext *context) {
28✔
32
        int r;
28✔
33

34
        assert(context);
28✔
35

36
        FOREACH_ARRAY(i, context->files, context->n_files) {
52✔
37
                unlink_and_free(i->temp);
24✔
38

39
                if (context->remove_parent) {
24✔
40
                        _cleanup_free_ char *parent = NULL;
16✔
41

42
                        r = path_extract_directory(i->path, &parent);
16✔
43
                        if (r < 0)
16✔
44
                                log_debug_errno(r, "Failed to extract directory from '%s', ignoring: %m", i->path);
×
45
                        else if (rmdir(parent) < 0 && !IN_SET(errno, ENOENT, ENOTEMPTY))
16✔
46
                                log_debug_errno(errno, "Failed to remove parent directory '%s', ignoring: %m", parent);
16✔
47
                }
48

49
                free(i->path);
24✔
50
                free(i->original_path);
24✔
51
                strv_free(i->comment_paths);
24✔
52
        }
53

54
        context->files = mfree(context->files);
28✔
55
        context->n_files = 0;
28✔
56
}
28✔
57

58
bool edit_files_contains(const EditFileContext *context, const char *path) {
24✔
59
        assert(context);
24✔
60
        assert(path);
24✔
61

62
        FOREACH_ARRAY(i, context->files, context->n_files)
25✔
63
                if (path_equal(i->path, path))
1✔
64
                        return true;
65

66
        return false;
67
}
68

69
int edit_files_add(
24✔
70
                EditFileContext *context,
71
                const char *path,
72
                const char *original_path,
73
                char * const *comment_paths) {
74

75
        _cleanup_free_ char *new_path = NULL, *new_original_path = NULL;
24✔
76
        _cleanup_strv_free_ char **new_comment_paths = NULL;
24✔
77

78
        assert(context);
24✔
79
        assert(path);
24✔
80

81
        if (edit_files_contains(context, path))
24✔
82
                return 0;
83

84
        if (!GREEDY_REALLOC(context->files, context->n_files + 1))
24✔
85
                return log_oom();
×
86

87
        new_path = strdup(path);
24✔
88
        if (!new_path)
24✔
89
                return log_oom();
×
90

91
        if (original_path) {
24✔
92
                new_original_path = strdup(original_path);
4✔
93
                if (!new_original_path)
4✔
94
                        return log_oom();
×
95
        }
96

97
        if (comment_paths) {
24✔
98
                new_comment_paths = strv_copy(comment_paths);
15✔
99
                if (!new_comment_paths)
15✔
100
                        return log_oom();
×
101
        }
102

103
        context->files[context->n_files] = (EditFile) {
24✔
104
                .context = context,
105
                .path = TAKE_PTR(new_path),
24✔
106
                .original_path = TAKE_PTR(new_original_path),
24✔
107
                .comment_paths = TAKE_PTR(new_comment_paths),
24✔
108
                .line = 1,
109
        };
110
        context->n_files++;
24✔
111

112
        return 1;
24✔
113
}
114

115
static int populate_edit_temp_file(EditFile *e, FILE *f, const char *filename) {
11✔
116
        assert(e);
11✔
117
        assert(e->context);
11✔
118
        assert(!e->context->read_from_stdin);
11✔
119
        assert(e->path);
11✔
120
        assert(f);
11✔
121
        assert(filename);
11✔
122

123
        bool has_original = e->original_path && access(e->original_path, F_OK) >= 0;
11✔
124
        bool has_target = access(e->path, F_OK) >= 0;
11✔
125
        const char *source;
11✔
126
        int r;
11✔
127

128
        if (has_original && (!has_target || e->context->overwrite_with_origin))
11✔
129
                /* We are asked to overwrite target with original_path or target doesn't exist. */
130
                source = e->original_path;
4✔
131
        else if (has_target)
7✔
132
                /* Target exists and shouldn't be overwritten. */
133
                source = e->path;
2✔
134
        else
135
                source = NULL;
136

137
        if (e->comment_paths) {
11✔
138
                _cleanup_free_ char *source_contents = NULL;
6✔
139

140
                if (source) {
6✔
141
                        r = read_full_file(source, &source_contents, NULL);
1✔
142
                        if (r < 0)
1✔
143
                                return log_error_errno(r, "Failed to read source file '%s': %m", source);
×
144
                }
145

146
                fprintf(f,
11✔
147
                        "### Editing %s\n"
148
                        "%s\n"
149
                        "\n"
150
                        "%s%s"
151
                        "\n"
152
                        "%s\n",
153
                        e->path,
154
                        e->context->marker_start,
155
                        strempty(source_contents),
156
                        source_contents && endswith(source_contents, "\n") ? "" : "\n",
1✔
157
                        e->context->marker_end);
6✔
158

159
                e->line = 4; /* Start editing at the contents area */
6✔
160

161
                STRV_FOREACH(path, e->comment_paths) {
20✔
162
                        _cleanup_free_ char *comment = NULL;
14✔
163

164
                        /* Skip the file which is being edited and the source file (can be the same) */
165
                        if (PATH_IN_SET(*path, e->path, source))
14✔
166
                                continue;
1✔
167

168
                        r = read_full_file(*path, &comment, NULL);
13✔
169
                        if (r < 0)
13✔
170
                                return log_error_errno(r, "Failed to read comment file '%s': %m", *path);
×
171

172
                        fprintf(f, "\n\n### %s", *path);
13✔
173

174
                        if (!isempty(comment)) {
26✔
175
                                _cleanup_free_ char *c = NULL;
13✔
176

177
                                c = strreplace(strstrip(comment), "\n", "\n# ");
13✔
178
                                if (!c)
13✔
179
                                        return log_oom();
×
180

181
                                fprintf(f, "\n# %s", c);
13✔
182
                        }
183
                }
184
        } else if (source) {
5✔
185
                r = copy_file_fd(source, fileno(f), COPY_REFLINK);
5✔
186
                if (r < 0) {
5✔
187
                        assert(r != -ENOENT);
×
188
                        return log_error_errno(r, "Failed to copy file '%s' to temporary file '%s': %m",
×
189
                                               source, filename);
190
                }
191
        }
192

193
        return 0;
194
}
195

196
static int create_edit_temp_file(EditFile *e, const char *contents, size_t contents_size) {
23✔
197
        _cleanup_(unlink_and_freep) char *temp = NULL;
×
198
        _cleanup_fclose_ FILE *f = NULL;
23✔
199
        int r;
23✔
200

201
        assert(e);
23✔
202
        assert(e->context);
23✔
203
        assert(e->path);
23✔
204
        assert(!e->comment_paths || (e->context->marker_start && e->context->marker_end));
23✔
205
        assert(contents || contents_size == 0);
23✔
206
        assert(e->context->read_from_stdin == !!contents);
23✔
207

208
        if (e->temp)
23✔
209
                return 0;
210

211
        r = mkdir_parents_label(e->path, 0755);
23✔
212
        if (r < 0)
23✔
213
                return log_error_errno(r, "Failed to create parent directories for '%s': %m", e->path);
×
214

215
        r = fopen_temporary_label(e->path, e->path, &f, &temp);
23✔
216
        if (r < 0)
23✔
217
                return log_error_errno(r, "Failed to create temporary file for '%s': %m", e->path);
×
218

219
        if (fchmod(fileno(f), 0644) < 0)
23✔
220
                return log_error_errno(errno, "Failed to change mode of temporary file '%s': %m", temp);
×
221

222
        if (e->context->read_from_stdin) {
23✔
223
                if (fwrite(contents, 1, contents_size, f) != contents_size)
12✔
224
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
225
                                               "Failed to write stdin data to temporary file '%s'.", temp);
226
        } else {
227
                r = populate_edit_temp_file(e, f, temp);
11✔
228
                if (r < 0)
11✔
229
                        return r;
230
        }
231

232
        r = fflush_and_check(f);
23✔
233
        if (r < 0)
23✔
234
                return log_error_errno(r, "Failed to write to temporary file '%s': %m", temp);
×
235

236
        e->temp = TAKE_PTR(temp);
23✔
237

238
        return 0;
23✔
239
}
240

241
static int run_editor_child(const EditFileContext *context) {
11✔
242
        _cleanup_strv_free_ char **args = NULL, **editor = NULL;
×
243
        int r;
11✔
244

245
        assert(context);
11✔
246
        assert(context->n_files >= 1);
11✔
247

248
        /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL.
249
         * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, we try to execute
250
         * well known editors. */
251
        FOREACH_STRING(e, "SYSTEMD_EDITOR", "EDITOR", "VISUAL") {
22✔
252
                const char *m = empty_to_null(getenv(e));
22✔
253
                if (m) {
11✔
254
                        editor = strv_split(m, WHITESPACE);
11✔
255
                        if (!editor)
11✔
256
                                return log_oom();
×
257

258
                        break;
259
                }
260
        }
261

262
        if (context->n_files == 1 && context->files[0].line > 1) {
11✔
263
                /* If editing a single file only, use the +LINE syntax to put cursor on the right line */
264
                r = strv_extendf(&args, "+%u", context->files[0].line);
6✔
265
                if (r < 0)
6✔
266
                        return log_oom();
×
267
        }
268

269
        FOREACH_ARRAY(i, context->files, context->n_files) {
22✔
270
                r = strv_extend(&args, i->temp);
11✔
271
                if (r < 0)
11✔
272
                        return log_oom();
×
273
        }
274

275
        size_t editor_n = strv_length(editor);
11✔
276
        if (editor_n > 0) {
11✔
277
                /* Strings are owned by 'editor' and 'args' */
278
                _cleanup_free_ char **cmdline = new(char*, editor_n + strv_length(args) + 1);
11✔
279
                if (!cmdline)
11✔
280
                        return log_oom();
×
281

282
                *mempcpy_typesafe(mempcpy_typesafe(cmdline, editor, editor_n), args, strv_length(args)) = NULL;
11✔
283

284
                execvp(cmdline[0], cmdline);
×
285
                log_warning_errno(errno, "Specified editor '%s' not available, trying fallbacks: %m", editor[0]);
×
286
        }
287

288
        bool prepended = false;
×
289
        FOREACH_STRING(name, "editor", "nano", "vim", "vi") {
×
290
                if (!prepended) {
×
291
                        r = strv_prepend(&args, name);
×
292
                        prepended = true;
293
                } else
294
                        r = free_and_strdup(&args[0], name);
×
295
                if (r < 0)
×
296
                        return log_oom();
×
297

298
                execvp(args[0], (char* const*) args);
×
299

300
                /* We do not fail if the editor doesn't exist because we want to try each one of them
301
                 * before failing. */
302
                if (errno != ENOENT)
×
303
                        return log_error_errno(errno, "Failed to execute '%s': %m", name);
×
304
        }
305

306
        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
307
                               "Cannot edit files, no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.");
308
}
309

310
static int run_editor(const EditFileContext *context) {
11✔
311
        int r;
11✔
312

313
        assert(context);
11✔
314

315
        r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_LOG|FORK_WAIT, NULL);
11✔
316
        if (r < 0)
22✔
317
                return r;
318
        if (r == 0) { /* Child */
22✔
319
                r = run_editor_child(context);
11✔
320
                _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
×
321
        }
322

323
        return 0;
324
}
325

326
static int strip_edit_temp_file(EditFile *e) {
23✔
327
        _cleanup_free_ char *old_contents = NULL, *tmp = NULL, *new_contents = NULL;
23✔
328
        const char *stripped;
23✔
329
        bool with_marker;
23✔
330
        int r;
23✔
331

332
        assert(e);
23✔
333
        assert(e->context);
23✔
334
        assert(!e->context->marker_start == !e->context->marker_end);
23✔
335
        assert(e->temp);
23✔
336

337
        r = read_full_file(e->temp, &old_contents, NULL);
23✔
338
        if (r < 0)
23✔
339
                return log_error_errno(r, "Failed to read temporary file '%s': %m", e->temp);
×
340

341
        tmp = strdup(old_contents);
23✔
342
        if (!tmp)
23✔
343
                return log_oom();
×
344

345
        with_marker = e->context->marker_start && !e->context->read_from_stdin;
23✔
346

347
        if (with_marker) {
23✔
348
                /* Trim out the lines between the two markers */
349
                char *contents_start, *contents_end;
9✔
350

351
                contents_start = strstrafter(tmp, e->context->marker_start) ?: tmp;
9✔
352

353
                contents_end = strstr(contents_start, e->context->marker_end);
9✔
354
                if (contents_end)
9✔
355
                        *contents_end = '\0';
2✔
356

357
                stripped = strstrip(contents_start);
9✔
358
        } else
359
                stripped = strstrip(tmp);
14✔
360

361
        if (isempty(stripped)) {
23✔
362
                /* People keep coming back to #24208 due to edits outside of markers. Let's detect this
363
                 * and point them in the right direction. */
364
                if (with_marker)
2✔
365
                        for (const char *p = old_contents;;) {
2✔
366
                                p = skip_leading_chars(p, WHITESPACE);
84✔
367
                                if (*p == '\0')
84✔
368
                                        break;
369
                                if (*p != '#') {
84✔
370
                                        log_warning("Found modifications outside of the staging area, which would be discarded.");
×
371
                                        break;
372
                                }
373

374
                                /* Skip the whole line if commented out */
375
                                p = strchr(p, '\n');
84✔
376
                                if (!p)
84✔
377
                                        break;
378
                                p++;
82✔
379
                        }
380

381
                return 0; /* File is empty (has no real changes) */
2✔
382
        }
383

384
        /* Trim prefix and suffix, but ensure suffixed by single newline */
385
        new_contents = strjoin(stripped, "\n");
21✔
386
        if (!new_contents)
21✔
387
                return log_oom();
×
388

389
        if (streq(old_contents, new_contents)) /* Don't touch the file if the above didn't change a thing */
21✔
390
                return 1; /* Contents have real changes */
391

392
        r = write_string_file(e->temp, new_contents,
3✔
393
                              WRITE_STRING_FILE_TRUNCATE | WRITE_STRING_FILE_AVOID_NEWLINE);
394
        if (r < 0)
3✔
395
                return log_error_errno(r, "Failed to strip temporary file '%s': %m", e->temp);
×
396

397
        return 1; /* Contents have real changes */
398
}
399

400
static int edit_file_install_one(EditFile *e) {
23✔
401
        int r;
23✔
402

403
        assert(e);
23✔
404
        assert(e->path);
23✔
405
        assert(e->temp);
23✔
406

407
        r = strip_edit_temp_file(e);
23✔
408
        if (r <= 0)
23✔
409
                return r;
410

411
        r = RET_NERRNO(rename(e->temp, e->path));
21✔
412
        if (r < 0)
×
413
                return log_error_errno(r,
×
414
                                       "Failed to rename temporary file '%s' to target file '%s': %m",
415
                                       e->temp, e->path);
416
        e->temp = mfree(e->temp);
21✔
417

418
        return 1;
21✔
419
}
420

421
static int edit_file_install_one_stdin(EditFile *e, const char *contents, size_t contents_size, int *fd) {
13✔
422
        int r;
13✔
423

424
        assert(e);
13✔
425
        assert(e->path);
13✔
426
        assert(contents || contents_size == 0);
13✔
427
        assert(fd);
13✔
428

429
        if (contents_size == 0)
13✔
430
                return 0;
13✔
431

432
        if (*fd >= 0) {
13✔
433
                r = mkdir_parents_label(e->path, 0755);
1✔
434
                if (r < 0)
1✔
435
                        return log_error_errno(r, "Failed to create parent directories for '%s': %m", e->path);
×
436

437
                r = copy_file_atomic_at(*fd, NULL, AT_FDCWD, e->path, 0644, COPY_REFLINK|COPY_REPLACE|COPY_MAC_CREATE);
1✔
438
                if (r < 0)
1✔
439
                        return log_error_errno(r, "Failed to copy stdin contents to '%s': %m", e->path);
×
440

441
                return 1;
442
        }
443

444
        r = create_edit_temp_file(e, contents, contents_size);
12✔
445
        if (r < 0)
12✔
446
                return r;
447

448
        _cleanup_close_ int tfd = open(e->temp, O_PATH|O_CLOEXEC);
25✔
449
        if (tfd < 0)
12✔
450
                return log_error_errno(errno, "Failed to pin temporary file '%s': %m", e->temp);
×
451

452
        r = edit_file_install_one(e);
12✔
453
        if (r <= 0)
12✔
454
                return r;
455

456
        *fd = TAKE_FD(tfd);
12✔
457

458
        return 1;
12✔
459
}
460

461
int do_edit_files_and_install(EditFileContext *context) {
23✔
462
        _cleanup_free_ char *stdin_data = NULL;
23✔
463
        size_t stdin_size = 0;
23✔
464
        int r;
23✔
465

466
        assert(context);
23✔
467

468
        if (context->n_files == 0)
23✔
469
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Got no files to edit.");
×
470

471
        if (context->read_from_stdin) {
23✔
472
                r = read_full_stream(stdin, &stdin_data, &stdin_size);
12✔
473
                if (r < 0)
12✔
474
                        return log_error_errno(r, "Failed to read stdin: %m");
×
475
        } else {
476
                FOREACH_ARRAY(editfile, context->files, context->n_files) {
22✔
477
                        r = create_edit_temp_file(editfile, /* contents = */ NULL, /* contents_size = */ 0);
11✔
478
                        if (r < 0)
11✔
479
                                return r;
480
                }
481

482
                r = run_editor(context);
11✔
483
                if (r < 0)
11✔
484
                        return r;
485
        }
486

487
        _cleanup_close_ int stdin_data_fd = -EBADF;
23✔
488

489
        FOREACH_ARRAY(editfile, context->files, context->n_files) {
47✔
490
                if (context->read_from_stdin) {
24✔
491
                        r = edit_file_install_one_stdin(editfile, stdin_data, stdin_size, &stdin_data_fd);
13✔
492
                        if (r == 0) {
13✔
493
                                log_notice("Stripped stdin content is empty, not writing file.");
×
494
                                return 0;
×
495
                        }
496
                } else {
497
                        r = edit_file_install_one(editfile);
11✔
498
                        if (r == 0) {
11✔
499
                                log_notice("%s: after editing, new contents are empty, not writing file.",
2✔
500
                                           editfile->path);
501
                                continue;
2✔
502
                        }
503
                }
504
                if (r < 0)
22✔
505
                        return r;
506

507
                log_info("Successfully installed edited file '%s'.", editfile->path);
22✔
508
        }
509

510
        return 0;
511
}
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