opnsense-src/libexec/rc/rc.d/cleanvar
Xin LI 39e8c2a29a cleanvar: Be more careful when cleaning up /var.
The cleanvar script uses find -delete to remove stale files under /var,
which could lead to unwanted removal of files in some unusual scenarios.
For example, when a mounted fdescfs(5) is present under /var/run/samba/fd,
find(1) could descend into a directory that is out of /var/run and remove
files that should not be removed.

To mitigate this, modify the script to use find -x, which restricts the
find scope to one file system only instead of descending into mounted
file systems.

PR:		269213
MFC after:	1 week
2023-02-12 20:56:17 -08:00

47 lines
917 B
Bash
Executable file

#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: cleanvar
# REQUIRE: var
. /etc/rc.subr
name="cleanvar"
desc="Purge /var directory"
rcvar="cleanvar_enable"
start_precmd="${name}_prestart"
start_cmd="${name}_start"
stop_cmd=":"
extra_commands="reload"
reload_cmd="${name}_start"
cleanvar_prestart()
{
# These files must be removed only the first time this script is run
# on boot.
#
rm -f /var/run/clean_var /var/spool/lock/clean_var
}
cleanvar_start()
{
if [ -d /var/run -a ! -f /var/run/clean_var ]; then
# Skip over logging sockets
find -x /var/run \( -type f -or -type s ! -name log -and ! -name logpriv \) -delete
>/var/run/clean_var
fi
if [ -d /var/spool/lock -a ! -f /var/spool/lock/clean_var ]; then
find -x /var/spool/lock -type f -delete
>/var/spool/lock/clean_var
fi
if [ -d /var/spool/uucp/.Temp ]; then
find -x /var/spool/uucp/.Temp -delete
fi
}
load_rc_config $name
run_rc_command "$1"