Commit Graph
32 Commits
Author SHA1 Message Date
Daan De Meyer 93a1f7921a basic: Stop including log.h in macro.h
Now that the necessary functions from log.h have been moved to macro.h,
we can stop including log.h in macro.h. This requires modifying source
files all over the tree to include log.h instead.
2025-04-18 14:19:15 +02:00
Mike Yuan d2e49d9389 edit-util: don't leave custom editor args around if we shall fall back
Also, let's complain loudly if the editor acquired from envvar
is not present.

Fixes #36796
2025-03-20 17:31:54 +01:00
A. Wilcox 79f0e94e27 edit-util: EditFileContext: avoid reserved 'stdin'
The identifier 'stdin' is reserved in C.  It can be #defined to any
statement that evaluates to a FILE*.  We do not want that for our field,
so change to a more descriptive name.
2024-09-06 08:29:45 +09:00
Mike Yuan e65b07293c edit-util: catch and warn about edits outside of markers
The users still periodically come back to #24208.
Let's add a detection for this hence.
2024-08-18 16:42:29 +02:00
Mike Yuan 40f5c372c2 edit-util: several cleanups for --stdin handling
Follow-up for 329050c5e2

I don't particularly favor the duplicated strstrip()
and such, so let's ensure if we get fixed data it's
only trimmed once. Subsequently we can benefit more
by making all copies reflinks.
2024-08-12 16:23:23 +02:00
Mike Yuan 3b5b2ff8fa edit-util: do not try to recreate temp file if missing
We initially read from temp file, then strip it, and write
back to it. If the file suddenly disappeared during the process,
it indicates someone else is touching our temp file
behind our back. Let's not silently continue.
2024-08-12 16:06:37 +02:00
Mike Yuan 7a729f876b edit-util: clean up run_editor() a bit
- Add missing assertions
- Close all fds before spawning editor
- Use FOREACH_STRING() + empty_to_null() where appropriate
  Note that this slightly changes the behavior, in that
  empty envvars would be treated as unset and we'd try
  the next candidate. But the new behavior is better IMO.
2024-08-12 16:04:11 +02:00
Mike Yuan 0e3638380d various: unexport a few internal structs
We don't expose destructors for these internal structs already.
Let's make the struct itself implementation detail too.
2024-01-03 08:40:33 +09:00
Mike Yuan 513412a69c various: don't log synthetic EIO for fwrite
Follow-up for f9568765d4
2023-12-08 10:49:08 +09:00
Zbigniew Jędrzejewski-Szmek 329050c5e2 systemctl: add "edit --stdin"
This is a fancy wrapper around "cat <<EOF", but:
- the user doesn't need to figure out the file name,
- parent directories are created automatically,
- daemon-reload is implied,
so it's a convenient way to create units or drop-ins.

Closes https://github.com/systemd/systemd/issues/21862.
2023-12-05 18:47:19 +01:00
Zbigniew Jędrzejewski-Szmek 232f017b1a systemctl: add message when edit is aborted
If the user edits result in an empty file (after stripping), we would silently
not do anything, aborting the edit. This is rather confusing, let's emit a
notice:

  $ build/systemctl --user edit asdf.service --full --stdin </dev/null
  /home/zbyszek/.config/systemd/user/asdf.service: after editing, new contents are empty, not writing file.

(This also works with an editor, instead of --stdin. The message is printed on
the console and is visible after the editor exits.)

While at it, fix the condition to skip writing file after stripping. We had
"old_contents", but we modified that string. In some code flows, we would
compare the stripped old contents (i.e. a string which by defintion doesn't
have a newline at the end) with a string to which we just appended a newline
(i.e. a string which by defintion has a newline at the end).
2023-12-05 18:11:45 +01:00
Zbigniew Jędrzejewski-Szmek 5161836bd0 shared/edit-util: split out function to populate temp file
In preparation for future changes: I want to add a mode where interactive
editing is not done, and when this preparation is moved to a helper, it's much
easier to skip it.

e->line is initialized to 1 and overwritten even if sync fails. Theoretically
this is against our style, but the alternative is to propagate a temporary
value of line through the layers, which adds a lot of noise. If we fail, this
EditFile object will not be used for anything, to the changed value of .line
has no effect.
2023-12-05 18:11:38 +01:00
Lennart Poettering e9ccae3135 process-util: add new FORK_DEATHSIG_SIGKILL flag, rename FORK_DEATHSIG → FORK_DEATHSIG_SIGTERM
Sometimes it makes sense to hard kill a client if we die. Let's hence
add a third FORK_DEATHSIG flag for this purpose: FORK_DEATHSIG_SIGKILL.

To make things less confusing this also renames FORK_DEATHSIG to
FORK_DEATHSIG_SIGTERM to make clear it sends SIGTERM. We already had
FORK_DEATHSIG_SIGINT, hence this makes things nicely symmetric.

A bunch of users are switched over for FORK_DEATHSIG_SIGKILL where we
know it's safe to abort things abruptly. This should make some kernel
cases more robust, since we cannot get confused by signal masks or such.

While we are at it, also fix a bunch of bugs where we didn't take
FORK_DEATHSIG_SIGINT into account in safe_fork()
2023-11-02 14:09:23 +01:00
Lennart Poettering d791013ff5 string-util: add strstrafter()
strstrafter() is like strstr() but returns a pointer to the first
character *after* the found substring, not on the substring itself.
Quite often this is what we actually want.

Inspired by #27267 I think it makes sense to add a helper for this,
to avoid the potentially fragile manual pointer increment afterwards.
2023-04-14 16:56:15 +02:00
Mike Yuan 77c9bb177a edit-util: improve log messages 2023-04-07 16:13:00 +08:00
Mike Yuan bc6c7a58c2 edit-util: introduce overwrite_with_origin switch
Before this commit, if `original_path` is given,
it will always be used to overwrite `path`.
After this commit, it's controlled by the newly-added
switch `overwrite_with_origin`.
2023-04-07 16:13:00 +08:00
Mike Yuan 54ad6aa164 edit-util: make original_path and comment_paths work together 2023-04-07 16:10:57 +08:00
Mike Yuan da037170c4 edit-util: alloc correct amount of memory 2023-03-22 23:25:25 +08:00
Daan De Meyer 7c2f5495e2 copy: Move chattr arguments to full function signatures
These are almost never used, so let's move them to the _full()
functions signatures.
2023-03-21 20:53:09 +01:00
Mike Yuan d3bf024f1a edit-util: log unexpected errors if we fail to remove the parent dir 2023-03-15 19:50:26 +08:00
Mike Yuan 00b6baf2be edit-util: use path_equal 2023-03-15 19:45:33 +08:00
Mike Yuan 978e222f72 edit-util: rename trim_edit_marker to strip_edit_temp_file
with some minor cleanups
2023-03-13 00:44:34 +08:00
Mike Yuan 52073ba247 edit-util: always create temp file
even if neither original_path nor comment_paths is specified.
2023-03-13 00:41:11 +08:00
Mike Yuan 8992667fef edit-util: minor cleanups
Reuse unlink_and_free() and avoid unnecessary
call to rmdir()
2023-03-13 00:40:15 +08:00
Yu Watanabe 0a742f3622 edit-util: unlink temporary file on failure
Addresses the suggestion
https://github.com/systemd/systemd/pull/26756#discussion_r1133078705.
2023-03-11 22:20:54 +09:00
Yu Watanabe aec8200b12 edit-util: fix potentical crash when no edit markers
This also makes trim_edit_markers() take EditFile as the argument.
2023-03-11 22:19:22 +09:00
Yu Watanabe c629a5bc44 edit-util: make create_edit_temp_file() take EditFile as the argument
No functional changes, just refactoring.
2023-03-11 22:19:22 +09:00
Yu Watanabe 043db3400c edit-util: make EditFile take reference of EditFileContext
No functional change, just preparation for later commits.
2023-03-11 22:19:22 +09:00
Mike Yuan f8970df54b edit-util: several cleanups for run_editor
run_editor is now switched to heap allocation
for simplicity. The code for child is made into
an individual function for simpler error handling.
2023-03-11 02:02:51 +08:00
Mike Yuan 9a11b4f953 edit-util: introduce EditFileContext
This is a rather large change which moves
the add and install logic into edit-util.

We store an EditFile array and the number of
elements, along with the edit markers used in
temporary files and whether to remove the parent
directories of the target files if they're empty
in an EditFileContext object.

Call edit_files_add() to add an file to edit,
and do_edit_files_and_install() to do the actual
editing (through create_edit_temp_file(),
run_editor() and trim_edit_markers()).
After that, edit_file_context_done() can be used
to destroy the object.
2023-03-11 02:01:00 +08:00
Mike Yuan fa2413dd57 edit-util: several cleanups to create_edit_temp_file
original_path and comment_paths can now be used
together.

Removes reference to "unit"
2023-03-11 01:38:52 +08:00
Mike Yuan a01c4bc9af shared: extract edit-util from systemctl-edit 2023-03-11 01:38:52 +08:00