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 1, 2020
1 parent
c5edd4d
commit 0ad679a
Showing
3 changed files
with
46 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,23 @@ | ||
#!/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 | ||
|
||
# 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() { | ||
generated_file=$1 | ||
staged_file=$(mktemp --tmpdir=$TEMPFILE_DIR $TEMPFILE_TEMPLATE) | ||
cat > "${staged_file}" | ||
# Atomically rename staged to generated file (see TEMPFILE_DIR | ||
# above). | ||
mv "$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