Skip to content

Commit

Permalink
motdgen: do not share a staged file
Browse files Browse the repository at this point in the history
For the same reason as in coreos#39
(copying the phrasing below):

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`.

Instead, write the intermediate output to a variable before writing
to the final output file. 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 Apr 20, 2020
1 parent a75a523 commit b9a816d
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions usr/libexec/console-login-helper-messages/motdgen
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@
set -e

PKG_NAME=console-login-helper-messages
MOTD_DIR_PUBLIC=motd.d
# Should only be read by this script.
MOTD_DIR_PRIVATE="${PKG_NAME}/motd.d"
MOTD_SNIPPETS_PATH="${PKG_NAME}/motd.d"
ETC_SNIPPETS="/etc/${MOTD_SNIPPETS_PATH}"
RUN_SNIPPETS="/run/${MOTD_SNIPPETS_PATH}"
USR_LIB_SNIPPETS="/usr/lib/${MOTD_SNIPPETS_PATH}"

staged="/run/${PKG_NAME}/40_${PKG_NAME}.motd.staged"
# Pick 40 as an index as other files can order around it easily.
generated="/run/motd.d/40_${PKG_NAME}.motd"
# Parts of this script write to the `${RUN_SNIPPETS}` directory,
# make sure it is created upfront.
mkdir -p "${RUN_SNIPPETS}"

mkdir -p "/run/${MOTD_DIR_PRIVATE}"
mkdir -p "/run/${MOTD_DIR_PUBLIC}"
rm -f "${generated}"

# Write distro release information to MOTD, in distro-specific color.
source /usr/lib/os-release
echo -e "\e[${ANSI_COLOR}m${PRETTY_NAME}\e[39m" > "/run/${MOTD_DIR_PRIVATE}/21_os_release.motd"

# Generate a motd from files found in the private (package-specific) directories,
# and place the motd in a public directory.
if [[ -d "/etc/${MOTD_DIR_PRIVATE}" ]]; then
cat /etc/${MOTD_DIR_PRIVATE}/* 2>/dev/null >> "${staged}" || true
fi
if [[ -d /run/"${MOTD_DIR_PRIVATE}" ]]; then
cat /run/${MOTD_DIR_PRIVATE}/* 2>/dev/null >> "${staged}" || true
fi
if [[ -d /usr/lib/"${MOTD_DIR_PRIVATE}" ]]; then
cat /usr/lib/${MOTD_DIR_PRIVATE}/* 2>/dev/null >> "${staged}" || true
fi

cat "${staged}" > "${generated}"
rm -rf "${staged}"
echo -e "\e[${ANSI_COLOR}m${PRETTY_NAME}\e[39m" > "${RUN_SNIPPETS}/21_os_release.motd"


# Generate a final motd from compiling the snippets.
# Pick 40 as a prefix as other files can order around it easily.
generated="/run/motd.d/40_${PKG_NAME}.motd"
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)

echo "${generated_string}" > "${generated}"

0 comments on commit b9a816d

Please sign in to comment.