openrc/init.d/bootmisc.in
Mike Frysinger 7c1a2defb6 bootmisc: clean up tmpdir cleaning
Make sure that the `cd` into the $dir actually happened.  This we don't
have to worry about relative paths deleting stuff it shouldn't.  This
step shouldn't fail, but who knows, and better to be sane than to wipe
out someone's valuables.

When wiping, automatically fall back to a dedicated `find` if the initial
`rm` failed on us.  This should help with the speed issues related to the
later `find`.

Have the later find only search the top level allowing `rm` to walk the
directory contents.  This means that -xdev no longer applies, but since
the earlier `rm` wasn't doing -xdev either and no one has complained thus
far, let's assume it isn't an issue.  Also convert to the -exec...+ form
so that we don't have to worry about long argument lists, and add -- to
the `rm` that was previously missing.  In practice, this shouldn't matter
as we've already deleted all those files, but better safe than sorry.

When cleaning, since we've already done a `cd` into the $dir, no point in
prefixing all the paths with $dir too.  Go with the relative loving.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
X-Gentoo-Bug: 359831
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=359831
2011-03-25 11:25:25 -05:00

169 lines
3.7 KiB
Plaintext

#!@PREFIX@/sbin/runscript
# Copyright (c) 2007-2009 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
depend()
{
need localmount
before logger
after clock sysctl
keyword -prefix
}
dir_writeable()
{
mkdir "$1"/.test.$$ 2>/dev/null && rmdir "$1"/.test.$$
}
: ${wipe_tmp:=${WIPE_TMP:-yes}}
cleanup_tmp_dir()
{
local dir="$1"
if ! [ -d "$dir" ]; then
mkdir -p "$dir" || return $?
fi
dir_writeable "$dir" || return 1
chmod a+rwt "$dir" 2> /dev/null
cd "$dir" || return 1
if yesno $wipe_tmp; then
ebegin "Wiping $dir directory"
# Faster than raw find
if ! rm -rf -- [^ajlq\.]* 2>/dev/null ; then
# Blah, too many files
find . -maxdepth 1 -name '[^ajlq\.]*' -exec rm -rf -- {} +
fi
# pam_mktemp creates a .private directory within which
# each user gets a private directory with immutable
# bit set; remove the immutable bit before trying to
# remove it.
[ -d /tmp/.private ] && chattr -R -a /tmp/.private 2> /dev/null
# Prune the paths that are left
find . -maxdepth 1 \
! -name . \
! -name lost+found \
! -name quota.user \
! -name aquota.user \
! -name quota.group \
! -name aquota.group \
! -name journal \
-exec rm -rf -- {} +
eend 0
else
ebegin "Cleaning $dir directory"
rm -rf -- .X*-lock esrv* kio* \
jpsock.* .fam* .esd* \
orbit-* ssh-* ksocket-* \
.*-unix
eend 0
fi
}
mkutmp()
{
: >"$1"
# Not all systems have the utmp group
chgrp utmp "$1" 2>/dev/null
chmod 0664 "$1"
}
start()
{
# Remove any added console dirs
rm -rf "$RC_LIBEXECDIR"/console/*
local logw=false runw=false extra=
# Ensure that our basic dirs exist
[ "$RC_UNAME" = Linux ] && extra=/var/lib/misc # Satisfy Linux FHS
for x in /var/log /var/run /tmp $extra; do
if ! [ -d $x ]; then
if ! mkdir -p $x; then
eend 1 "failed to create needed directory $x"
return 1
fi
fi
done
if dir_writeable /var/run; then
ebegin "Creating user login records"
local xtra=
[ "$RC_UNAME" = NetBSD ] && xtra=x
for x in "" $xtra; do
mkutmp /var/run/utmp$x
done
[ -e /var/log/wtmp ] || mkutmp /var/log/wtmp
eend 0
ebegin "Cleaning /var/run"
for x in $(find /var/run ! -type d ! -name utmp \
! -name random-seed ! -name dev.db \
! -name ld-elf.so.hints ! -name ld.so.hints);
do
# Clean stale sockets
if [ -S "$x" ]; then
if type fuser >/dev/null 2>&1; then
fuser "$x" >/dev/null 2>&1 || rm -- "$x"
else
rm -- "$x"
fi
fi
[ ! -f "$x" ] && continue
# Do not remove pidfiles of already running daemons
case "$x" in
*.pid)
start-stop-daemon --test --quiet \
--stop --pidfile "$x" && continue
;;
esac
rm -f -- "$x"
done
eend 0
fi
# Clean up /tmp directories
local tmp=
for tmp in ${clean_tmp_dirs:-${wipe_tmp_dirs-/tmp}}; do
cleanup_tmp_dir "$tmp"
done
if dir_writeable /tmp; then
# Make sure our X11 stuff have the correct permissions
# Omit the chown as bootmisc is run before network is up
# and users may be using lame LDAP auth #139411
rm -rf /tmp/.ICE-unix /tmp/.X11-unix
mkdir -p /tmp/.ICE-unix /tmp/.X11-unix
chmod 1777 /tmp/.ICE-unix /tmp/.X11-unix
if [ -x /sbin/restorecon ]; then
restorecon /tmp/.ICE-unix /tmp/.X11-unix
fi
fi
if $logw || dir_writeable /var/log; then
# Create an 'after-boot' dmesg log
if [ "$RC_SYS" != VSERVER -a "$RC_SYS" != OPENVZ ]; then
dmesg > /var/log/dmesg
chmod 640 /var/log/dmesg
fi
fi
[ -w /etc/nologin ] && rm -f /etc/nologin
return 0
}
stop()
{
# Write a halt record if we're shutting down
if [ "$RC_RUNLEVEL" = shutdown ]; then
[ "$RC_UNAME" = Linux ] && halt -w
if [ "$RC_SYS" = OPENVZ ]; then
yesno $RC_REBOOT && printf "" >/reboot
fi
fi
return 0
}