Use _ as site seperator, and - as word seperator in FLAGS.

This commit is contained in:
Darren 'Tadgy' Austin 2020-06-13 15:14:54 +01:00
commit 8bf8462b1d

View file

@ -19,15 +19,15 @@ check_leading_dirs() {
[[ -z "$1" || -z "$2" ]] && return 1 [[ -z "$1" || -z "$2" ]] && return 1
if ! is_dir "$(remove_expansions "$2")"; then if ! is_dir "$(remove_expansions "$2")"; then
(( FLAGS[${1}-template_prefix] == 0 )) && { (( FLAGS[${1}_template-prefix] == 0 )) && {
syslog "warn" "prefix directories of template do not exist: $(remove_expansions "$2")" syslog "warn" "prefix directories of template do not exist: $(remove_expansions "$2")"
FLAGS[${1}-template_prefix]=1 FLAGS[${1}_template-prefix]=1
} }
return 1 return 1
else else
(( FLAGS[${1}-template_prefix] == 1 )) && { (( FLAGS[${1}_template-prefix] == 1 )) && {
syslog "info" "prefix directories of template reappeared: $(remove_expansions "$2")" syslog "info" "prefix directories of template reappeared: $(remove_expansions "$2")"
FLAGS[${1}-template_prefix]=0 FLAGS[${1}_template-prefix]=0
} }
fi fi
return 0 return 0
@ -40,7 +40,7 @@ close_fd() {
# shellcheck disable=SC1083 # shellcheck disable=SC1083
{ exec {FDS[$1]}>&-; } 2>/dev/null || syslog "warn" "failed to close FD ${FDS[$1]} for $1" { exec {FDS[$1]}>&-; } 2>/dev/null || syslog "warn" "failed to close FD ${FDS[$1]} for $1"
unset "FDS[$1]" "FLAGS[${1}-template_prefix]" "FLAGS[${1}-make_dir_fail]" "FLAGS[${1}-fix_link]" unset "FDS[$1]" "FLAGS[${1}_template-prefix]" "FLAGS[${1}_make-dir-fail]" "FLAGS[${1}_fix-link]"
} }
create_missing_dirs() { create_missing_dirs() {
@ -50,15 +50,15 @@ create_missing_dirs() {
[[ -z "$1" || -z "$2" ]] && return 1 [[ -z "$1" || -z "$2" ]] && return 1
if ! make_dir "$2"; then if ! make_dir "$2"; then
(( FLAGS[${1}-make_dir_fail] == 0 )) && { (( FLAGS[${1}_make-dir-fail] == 0 )) && {
syslog "warn" "error creating log file's directory: $2" syslog "warn" "error creating log file's directory: $2"
FLAGS[${1}-make_dir_fail]=1 FLAGS[${1}_make-dir-fail]=1
} }
return 1 return 1
else else
(( FLAGS[${1}-make_dir_fail] == 1 )) && { (( FLAGS[${1}_make-dir-fail] == 1 )) && {
syslog "info" "created log file's directory: $2" syslog "info" "created log file's directory: $2"
FLAGS[${1}-make_dir_fail]=0 FLAGS[${1}_make-dir-fail]=0
} }
fi fi
return 0 return 0
@ -190,7 +190,7 @@ EOF
} }
exit_handler() { exit_handler() {
(( FLAGS[created_fifo] == 1 )) && { (( FLAGS[created-fifo] == 1 )) && {
rm -f "$INPUT" 2>/dev/null || syslog "warn" "failed to remove pipe/fifo: $INPUT" rm -f "$INPUT" 2>/dev/null || syslog "warn" "failed to remove pipe/fifo: $INPUT"
} }
} }
@ -226,15 +226,15 @@ open_fd() {
umask "$FILE_UMASK" umask "$FILE_UMASK"
# shellcheck disable=SC1083 # shellcheck disable=SC1083
if ! { exec {FDS[$1]}>>"$2"; } 2>/dev/null; then if ! { exec {FDS[$1]}>>"$2"; } 2>/dev/null; then
(( FLAGS[${1}-open_fd_fail] == 0 )) && { (( FLAGS[${1}_open-fd-fail] == 0 )) && {
syslog "error" "failed to open log file for writing: $2" syslog "error" "failed to open log file for writing: $2"
FLAGS[${1}-open_fd_fail]=1 FLAGS[${1}_open-fd-fail]=1
} }
return 1 return 1
else else
(( FLAGS[${1}-open_fd_fail] == 1 )) && { (( FLAGS[${1}_open-fd-fail] == 1 )) && {
syslog "info" "opened log file for writing: $2" syslog "info" "opened log file for writing: $2"
FLAGS[${1}-open_fd_fail]=0 FLAGS[${1}_open-fd-fail]=0
} }
fi fi
return 0 return 0
@ -351,7 +351,7 @@ PIPE_OWNER=""
SYSLOG_FACILITY="user" SYSLOG_FACILITY="user"
RUNAS_USER="" RUNAS_USER=""
RUNAS_GROUP="" RUNAS_GROUP=""
FLAGS=([flush]=0 [raw]=0 [compress]=0 [make_parents]=0 [created_fifo]=0 [timed_out]=0 [basedir_vanished]=0 [basedir_notdir]=0) FLAGS=([flush]=0 [raw]=0 [compress]=0 [make-parents]=0 [created-fifo]=0 [timed-out]=0 [basedir-vanished]=0 [basedir-notdir]=0)
# trap signals. # trap signals.
trap 'sigchld_handler' SIGCHLD trap 'sigchld_handler' SIGCHLD
@ -468,7 +468,7 @@ while :; do
;; ;;
-p) -p)
# Create parent directories. # Create parent directories.
FLAGS[make_parents]=1 FLAGS[make-parents]=1
ORIG_ARGS+=("$1") ORIG_ARGS+=("$1")
shift shift
continue continue
@ -546,7 +546,7 @@ TEMPLATE="$2"
if [[ ! -e "$INPUT" ]]; then if [[ ! -e "$INPUT" ]]; then
umask "$PIPE_UMASK" umask "$PIPE_UMASK"
mkfifo "$INPUT" 2>/dev/null || die "failed to create pipe/FIFO: $INPUT" mkfifo "$INPUT" 2>/dev/null || die "failed to create pipe/FIFO: $INPUT"
FLAGS[created_fifo]=1 FLAGS[created-fifo]=1
[[ -n "$PIPE_OWNER" ]] && { chown "$PIPE_OWNER" "$INPUT" >/dev/null 2>&1 || die "failed to chown pipe/FIFO: $INPUT"; } [[ -n "$PIPE_OWNER" ]] && { chown "$PIPE_OWNER" "$INPUT" >/dev/null 2>&1 || die "failed to chown pipe/FIFO: $INPUT"; }
elif [[ ! -p "$INPUT" ]]; then elif [[ ! -p "$INPUT" ]]; then
die "not a pipe/FIFO: $INPUT" die "not a pipe/FIFO: $INPUT"
@ -572,7 +572,7 @@ fi
while :; do while :; do
# Reset used variables. # Reset used variables.
unset LOG_VHOST LOG_DATA unset LOG_VHOST LOG_DATA
FLAGS[timed_out]=0 FLAGS[timed-out]=0
# Start compression jobs if there's any in the queue. # Start compression jobs if there's any in the queue.
start_compression_jobs start_compression_jobs
@ -593,7 +593,7 @@ while :; do
# Determine how the read above was exited. # Determine how the read above was exited.
if (( ERR > 128 )); then if (( ERR > 128 )); then
# If 'read' timed out, set a marker. # If 'read' timed out, set a marker.
FLAGS[timed_out]=1 FLAGS[timed-out]=1
elif (( ERR == 1 )); then elif (( ERR == 1 )); then
[[ "$INPUT" == "/dev/stdin" ]] && { [[ "$INPUT" == "/dev/stdin" ]] && {
# stdin has been closed by the parent, quit gracefully by raising a SIGTERM. # stdin has been closed by the parent, quit gracefully by raising a SIGTERM.
@ -608,29 +608,29 @@ while :; do
# Make sure the base path still exists - it could have disappeared while we were blocked in 'read'. # Make sure the base path still exists - it could have disappeared while we were blocked in 'read'.
# Note: We won't make this directory ourselves - as it's the base directory it should exist on the system to start with. # Note: We won't make this directory ourselves - as it's the base directory it should exist on the system to start with.
if [[ ! -e "$BASEDIR" ]]; then if [[ ! -e "$BASEDIR" ]]; then
(( FLAGS[basedir_vanished] == 0 )) && { (( FLAGS[basedir-vanished] == 0 )) && {
syslog "error" "base directory has vanished" syslog "error" "base directory has vanished"
FLAGS[basedir_vanished]=1 FLAGS[basedir-vanished]=1
} }
continue continue
else else
(( FLAGS[basedir_vanished] == 1 )) && { (( FLAGS[basedir-vanished] == 1 )) && {
syslog "info" "base directory has reappeared" syslog "info" "base directory has reappeared"
FLAGS[basedir_vanished]=0 FLAGS[basedir-vanished]=0
} }
fi fi
# Make sure the base path is a directory. # Make sure the base path is a directory.
if ! is_dir "$BASEDIR"; then if ! is_dir "$BASEDIR"; then
(( FLAGS[basedir_notdir] == 0 )) && { (( FLAGS[basedir-notdir] == 0 )) && {
syslog "error" "base path is no longer a directory" syslog "error" "base path is no longer a directory"
FLAGS[basedir_notdir]=1 FLAGS[basedir-notdir]=1
} }
continue continue
else else
(( FLAGS[basedir_notdir] == 1 )) && { (( FLAGS[basedir-notdir] == 1 )) && {
syslog "info" "base path has become directory again" syslog "info" "base path has become directory again"
FLAGS[basedir_notdir]=0 FLAGS[basedir-notdir]=0
} }
fi fi
@ -642,10 +642,10 @@ while :; do
[[ -z "$OLD_TEMPLATE" ]] && OLD_TEMPLATE="$EXPANDED_TEMPLATE" [[ -z "$OLD_TEMPLATE" ]] && OLD_TEMPLATE="$EXPANDED_TEMPLATE"
# If the 'read' timed out and the exapnded template is the same as the old expanded template, there is no need to do anything. # If the 'read' timed out and the exapnded template is the same as the old expanded template, there is no need to do anything.
(( FLAGS[timed_out] == 1 )) && [[ "$EXPANDED_TEMPLATE" == "$OLD_TEMPLATE" ]] && continue (( FLAGS[timed-out] == 1 )) && [[ "$EXPANDED_TEMPLATE" == "$OLD_TEMPLATE" ]] && continue
# If the 'read' did not time out but the line read is empty, don't do anything. # If the 'read' did not time out but the line read is empty, don't do anything.
(( FLAGS[timed_out] == 0 )) && [[ "$LOG_DATA" =~ ^[[:space:]]*$ ]] && continue (( FLAGS[timed-out] == 0 )) && [[ "$LOG_DATA" =~ ^[[:space:]]*$ ]] && continue
# If the new expanded template is different from the old, close and reopen all the logs and queue for compression (if required). # If the new expanded template is different from the old, close and reopen all the logs and queue for compression (if required).
[[ "$EXPANDED_TEMPLATE" != "$OLD_TEMPLATE" ]] && { [[ "$EXPANDED_TEMPLATE" != "$OLD_TEMPLATE" ]] && {
@ -674,15 +674,15 @@ while :; do
# Note: This will clobber anything that already exists with the link name. # Note: This will clobber anything that already exists with the link name.
rm -rf "${BASEDIR:?}/${LINKFILE_EXPANDED//\{\}/$SITE}" rm -rf "${BASEDIR:?}/${LINKFILE_EXPANDED//\{\}/$SITE}"
if ! ln -sfr "$FILENAME" "$BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}"; then if ! ln -sfr "$FILENAME" "$BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}"; then
(( FLAGS[${SITE}-fix_link] == 0 )) && { (( FLAGS[${SITE}_fix-link] == 0 )) && {
syslog "error" "failed to fix link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}" syslog "error" "failed to fix link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}"
FLAGS[${SITE}-fix_link]=1 FLAGS[${SITE}_fix-link]=1
} }
continue continue
else else
(( FLAGS[${SITE}-fix_link] == 1 )) && { (( FLAGS[${SITE}_fix-link] == 1 )) && {
syslog "info" "fixed link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}" syslog "info" "fixed link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$SITE}"
FLAGS[${SITE}-fix_link]=0 FLAGS[${SITE}_fix-link]=0
} }
fi fi
} }
@ -694,7 +694,7 @@ while :; do
} }
# If the 'read' did not time out, there must be a log line to write. # If the 'read' did not time out, there must be a log line to write.
(( FLAGS[timed_out] == 0 )) && { (( FLAGS[timed-out] == 0 )) && {
# If not in raw mode, an unset LOG_VHOST is an error. # If not in raw mode, an unset LOG_VHOST is an error.
# If in raw mode, we need a placeholder for the FDS array element as LOG_VHOST would normally be unset. # If in raw mode, we need a placeholder for the FDS array element as LOG_VHOST would normally be unset.
if (( FLAGS[raw] == 0 )); then if (( FLAGS[raw] == 0 )); then
@ -711,7 +711,7 @@ while :; do
# Unless the -p option has been used, make sure the directory leading up to the # Unless the -p option has been used, make sure the directory leading up to the
# expanded part of the template exists. # expanded part of the template exists.
(( FLAGS[make_parents] == 0 )) && { (( FLAGS[make-parents] == 0 )) && {
check_leading_dirs "$LOG_VHOST" "$BASEDIR/${TEMPLATE//\{\}/$LOG_VHOST}" || continue check_leading_dirs "$LOG_VHOST" "$BASEDIR/${TEMPLATE//\{\}/$LOG_VHOST}" || continue
} }
@ -729,15 +729,15 @@ while :; do
# Flush data to disk if requested. # Flush data to disk if requested.
(( FLAGS[flush] == 1 )) && { (( FLAGS[flush] == 1 )) && {
if ! sync "$FILENAME" 2>/dev/null; then if ! sync "$FILENAME" 2>/dev/null; then
(( FLAGS[sync_fail] == 0 )) && { (( FLAGS[sync-fail] == 0 )) && {
syslog "warn" "failed to sync: $FILENAME" syslog "warn" "failed to sync: $FILENAME"
FLAGS[sync_fail]=1 FLAGS[sync-fail]=1
} }
continue continue
else else
(( FLAGS[sync_fail] == 1 )) && { (( FLAGS[sync-fail] == 1 )) && {
syslog "info" "sync successful: $FILENAME" syslog "info" "sync successful: $FILENAME"
FLAGS[sync_fail]=0 FLAGS[sync-fail]=0
} }
fi fi
} }
@ -750,15 +750,15 @@ while :; do
# Note: This will clobber anything that already exists with the link name. # Note: This will clobber anything that already exists with the link name.
rm -rf "${BASEDIR:?}/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}" rm -rf "${BASEDIR:?}/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}"
if ! ln -sfr "$FILENAME" "$BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}" 2>/dev/null; then if ! ln -sfr "$FILENAME" "$BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}" 2>/dev/null; then
(( FLAGS[${LOG_VHOST}-create_link] == 0 )) && { (( FLAGS[${LOG_VHOST}_create-link] == 0 )) && {
syslog "error" "failed to create link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}" syslog "error" "failed to create link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}"
FLAGS[${LOG_VHOST}-create_link]=1 FLAGS[${LOG_VHOST}_create-link]=1
} }
continue continue
else else
(( FLAGS[${LOG_VHOST}-create_link] == 1 )) && { (( FLAGS[${LOG_VHOST}_create-link] == 1 )) && {
syslog "info" "created link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}" syslog "info" "created link: $BASEDIR/${LINKFILE_EXPANDED//\{\}/$LOG_VHOST}"
FLAGS[${LOG_VHOST}-create_link]=0 FLAGS[${LOG_VHOST}_create-link]=0
} }
fi fi
} }