Files
librechat-custom/assets/log-guard.sh
T

41 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Log ownership guard for /app/data/logs.
#
# A host-side daily job creates the next day's dated log files
# (error-<date>.log, meiliSync-<date>.log, ...) as root:root at 00:00 UTC.
# LibreChat runs as the `cloudron` user and crashes (EACCES on uncaught
# error) the first time it tries to append to a root-owned log. This guard
# runs as root (via supervisord) and, every minute:
# 1. pre-creates TODAY's and TOMORROW's dated files owned by cloudron
# (an existing correctly-owned file defeats the ownership flip —
# open(O_TRUNC) preserves the inode's owner),
# 2. chowns any root-owned strays back to cloudron.
#
# This is a defense-in-depth stopgap; the host-side rotation config should
# still be fixed to create files as cloudron (see co-op/support thread and
# the overlay README).
set -u
LOGDIR="/app/data/logs"
mkdir -p "$LOGDIR"
while true; do
for offset in 0 1; do
d=$(date -u -d "+${offset} day" +%F 2>/dev/null)
[ -z "$d" ] && continue
for prefix in error meiliSync; do
f="$LOGDIR/${prefix}-${d}.log"
# Create with O_EXCL and never follow a symlink: a `cloudron` process
# that wins the race with a symlink must not make root truncate a
# file outside the log directory.
if [ ! -e "$f" ] && [ ! -L "$f" ]; then
( set -C; : > "$f" ) 2>/dev/null || true
fi
done
done
# Refuse symlinks outright (delete them), then repair regular-file
# ownership only — chown must not follow a link swapped in by cloudron.
find "$LOGDIR" -type l -delete 2>/dev/null
find "$LOGDIR" -type f ! -user cloudron -exec chown cloudron:cloudron {} + 2>/dev/null
sleep 60
done