Skip to content

Commit

Permalink
issuegen: use mktemp and mv when writing files
Browse files Browse the repository at this point in the history
Similar to `motdgen` (#44), use `mktemp` to create a uniquely-named
tmpfile file to stage changes and then `mv` the file to the target
location. This ensures no interleaving of writes when two processes
are running `issuegen`. See: #35
  • Loading branch information
Robert Fairley committed Jul 3, 2020
1 parent 592af45 commit 8fedbd3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 3 additions & 1 deletion usr/libexec/console-login-helper-messages/gensnippet_udev_if
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

set -e

. /usr/lib/console-login-helper-messages/libutil.sh
. /usr/libexec/console-login-helper-messages/issuegen.defs

# Add/remove data from udev rules.
outfile="${RUN_SNIPPETS}/22_${INTERFACE}.issue"
case "${ACTION}" in
add)
echo "${INTERFACE}: \\4{${INTERFACE}} \\6{${INTERFACE}}" > ${outfile}
echo "${INTERFACE}: \\4{${INTERFACE}} \\6{${INTERFACE}}" \
| write_via_tempfile ${outfile}
;;
remove)
rm -f ${outfile}
Expand Down
13 changes: 6 additions & 7 deletions usr/libexec/console-login-helper-messages/issuegen
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

set -e

. /usr/lib/console-login-helper-messages/libutil.sh
. /usr/libexec/console-login-helper-messages/issuegen.defs

# Provide key fingerprints via issue.
Expand All @@ -22,16 +23,14 @@ mkdir -p "${SSH_DIR}"
SSH_KEY_OUTDIR="${RUN_SNIPPETS}"
for KEY_FILE in $(find "${SSH_DIR}" -name 'ssh_host_*_key') ; do
ssh-keygen -l -f "${KEY_FILE}"
done | awk '{print "SSH host key: " $2 " " $4}' > "${SSH_KEY_OUTDIR}/21_ssh_host_keys.issue"
done \
| awk '{print "SSH host key: " $2 " " $4}' \
| write_via_tempfile "${SSH_KEY_OUTDIR}/21_ssh_host_keys.issue"


# Generate a final issue message from compiling the snippets.
# Pick 40 as a prefix as other files can order around it easily.
generated_file="/run/${PKG_NAME}/40_${PKG_NAME}.issue"
generated_string=''
# Hack around files potentially not existing in the below paths with `|| true`.
generated_string+=$(cat ${ETC_SNIPPETS}/* 2>/dev/null || true)
generated_string+=$(cat ${RUN_SNIPPETS}/* 2>/dev/null || true)
generated_string+=$(cat ${USR_LIB_SNIPPETS}/* 2>/dev/null || true)
source_dirs=("${ETC_SNIPPETS}" "${RUN_SNIPPETS}" "${USR_LIB_SNIPPETS}")

echo "${generated_string}" > "${generated_file}"
cat_via_tempfile "${generated_file}" ".issue" "${source_dirs[@]}"

0 comments on commit 8fedbd3

Please sign in to comment.