forked from coreos/console-login-helper-messages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
motdgen: do not share a staged file, use mktemp and mv
With the `staged` file shared, there would be potential for two or more processes executing `motdgen` to write to it resulting in corrupted output, or the error in the `cat` command due to missing file reported in coreos#35 (comment). Currently, this is not a problem with motdgen, but could be if `motdgen` were invoked by something like the udev rules that invoke `issuegen`. A bug reported in `issuegen` for this reason is: coreos#35 Instead, write the intermediate output to a unique tempfile and mv the tempfile to the final output location. If the final output is on the same filesystem as the tempfile, this operation should be atomic. This ensures only valid output is written to the issue file shown to the terminal. Additionally, perform code tidyups similar to those done for `issuegen` in coreos#40.
- Loading branch information
Robert Fairley
committed
Jul 6, 2020
1 parent
323a5ef
commit ab31f7c
Showing
3 changed files
with
68 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/bash | ||
# | ||
# Collection of util functions and common definitions for | ||
# console-login-helper-messages scripts. | ||
|
||
PKG_NAME="console-login-helper-messages" | ||
|
||
tempfile_template="${PKG_NAME}.XXXXXXXXXX.tmp" | ||
# Use same filesystem, under /run, as where snippets are generated, so | ||
# that rename operations through `mv` are atomic. | ||
tempfile_dir="/run/${PKG_NAME}" | ||
# Default SELinux context at destination is applied, e.g. for sshd which | ||
# requires that written files in `/run/motd.d` maintain the type | ||
# `pam_var_run_t`. | ||
mv_Z="mv -Z" | ||
|
||
# Write stdin to a tempfile, and rename the tempfile to the path given | ||
# as an argument. When called from multiple processes on the same | ||
# generated file path, this avoids interleaving writes to the generated | ||
# file by using `mv` to overwrite the file. | ||
write_via_tempfile() { | ||
local generated_file="$1" | ||
local staged_file="$(mktemp --tmpdir="${tempfile_dir}" "${tempfile_template}")" | ||
cat > "${staged_file}" | ||
${mv_Z} "${staged_file}" "${generated_file}" | ||
} | ||
|
||
# Write concatenation of all files with a given suffix from a list of | ||
# source directories to a target file. The target file is the first | ||
# argument; suffix the second; and source directories the remaining, | ||
# searched in the given order in the list. Atomicity of the write to | ||
# the target file is given by appending file contents to a tempfile | ||
# before moving to the target file. | ||
cat_via_tempfile() { | ||
local generated_file="$1" | ||
local filter_suffix="$2" | ||
shift 2 | ||
local source_dirs="$@" | ||
local staged_file="$(mktemp --tmpdir="${tempfile_dir}" "${tempfile_template}")" | ||
for source_dir in ${source_dirs[@]}; do | ||
# Ignore stderr, and let the command succeed if no files are | ||
# found in the source directory. | ||
cat "${source_dir}"/*"$filter_suffix" 2>/dev/null >> "${staged_file}" || : | ||
done | ||
${mv_Z} "${staged_file}" "${generated_file}" | ||
} |
2 changes: 1 addition & 1 deletion
2
usr/lib/systemd/system/console-login-helper-messages-motdgen.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters