5
# Clean /tmp. Clean /var/run and /var/lock if not mounted as tmpfs
7
# DO NOT RUN AFTER S:55bootmisc.sh and do not run this script directly
8
# in runlevel S. Instead write an initscript to call it.
13
. /lib/lsb/init-functions
15
# Should be called outside verbose message block
18
# Prevent symlink attack (See #264234.)
19
[ -L "$1" ] && log_warning_msg "bootclean: Deleting symbolic link '$1'."
20
rm -f "$1" || { log_failure_msg "bootclean: Failure deleting '$1'." ; return 1 ; }
21
# No user processes should be running, so no one should be able to introduce
22
# a symlink here. As an extra precaution, set noclobber.
24
:> "$1" || { log_failure_msg "bootclean: Failure creating '$1'." ; return 1 ; }
29
cd /tmp || { log_failure_msg "bootclean: Could not cd to /tmp." ; return 1 ; }
32
# Only clean out /tmp if it is world-writable. This ensures
33
# it really is a/the temp directory we're cleaning.
35
[ "$(find . -maxdepth 0 -perm -002)" = "." ] || return 0
39
log_warning_msg "Using default TMPTIME 0."
43
[ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /tmp"
46
# Remove regardless of TMPTIME setting
51
# Don't clean remaining files if TMPTIME is negative or 'infinite'
55
[ "$VERBOSE" = no ] || log_action_end_msg 0 "skipped"
61
# Wipe /tmp, excluding system files, but including lost+found
63
# If TMPTIME is set to 0, we do not use any ctime expression
64
# at all, so we can also delete files with timestamps
72
TEXPR="-mtime +$TMPTIME -ctime +$TMPTIME -atime +$TMPTIME"
73
DEXPR="-mtime +$TMPTIME -ctime +$TMPTIME"
77
! ( -path ./lost+found -uid 0 )
78
! ( -path ./quota.user -uid 0 )
79
! ( -path ./aquota.user -uid 0 )
80
! ( -path ./quota.group -uid 0 )
81
! ( -path ./aquota.group -uid 0 )
82
! ( -path ./.journal -uid 0 )
83
! ( -path ./.clean -uid 0 )
84
! ( -path './...security*' -uid 0 )'
86
mkflagfile /tmp/.clean || return 1
90
if [ "$VERBOSE" = no ]
92
log_failure_msg "bootclean: Failure cleaning /tmp."
94
log_action_end_msg 1 "bootclean: Failure cleaning /tmp"
99
# First remove all old files...
101
find . -depth -xdev $TEXPR $EXCEPT ! -type d -delete \
102
|| { report_err ; return 1 ; }
105
# ...and then all empty directories
107
find . -depth -xdev $DEXPR $EXCEPT -type d -empty -delete \
108
|| { report_err ; return 1 ; }
110
[ "$VERBOSE" = no ] || log_action_end_msg 0
115
if [ yes = "$RAMLOCK" ] ; then
119
cd /var/lock || { log_failure_msg "bootclean: Could not cd to /var/lock." ; return 1 ; }
121
[ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /var/lock"
124
if [ "$VERBOSE" = no ]
126
log_failure_msg "bootclean: Failure cleaning /var/lock."
128
log_action_end_msg 1 "bootclean: Failure cleaning /var/lock"
131
find . ! -type d -delete \
132
|| { report_err ; return 1 ; }
133
[ "$VERBOSE" = no ] || log_action_end_msg 0
134
mkflagfile /var/lock/.clean || return 1
139
if [ yes = "$RAMRUN" ] ; then
143
cd /var/run || { log_action_end_msg 1 "bootclean: Could not cd to /var/run." ; return 1 ; }
145
[ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /var/run"
148
if [ "$VERBOSE" = no ]
150
log_failure_msg "bootclean: Failure cleaning /var/run."
152
log_action_end_msg 1 "bootclean: Failure cleaning /var/run"
155
find . ! -xtype d ! -name utmp ! -name innd.pid -delete \
156
|| { report_err ; return 1 ; }
157
[ "$VERBOSE" = no ] || log_action_end_msg 0
158
mkflagfile /var/run/.clean || return 1
162
which find >/dev/null 2>&1 || exit 1
163
log_begin_msg "Cleaning up temporary files..."
165
# If there are flag files that have not been created by root
167
for D in /tmp /var/run /var/lock
171
which stat >/dev/null 2>&1 && cleanuid="$(stat -c %u $D/.clean)"
172
# Poor's man stat %u, since stat (and /usr) might not be
173
# available in some bootup stages
174
[ "$cleanuid" ] || cleanuid="$(find $D/.clean -printf %U)"
175
[ "$cleanuid" ] || { log_failure_msg "bootclean: Could not stat '$D/.clean'." ; exit 1 ; }
176
if [ "$cleanuid" -ne 0 ]
178
rm -f $D/.clean || { log_failure_msg "bootclean: Could not delete '$D/.clean'." ; exit 1 ; }
183
[ -f /tmp/.clean ] && [ -f /var/run/.clean ] && [ -f /var/lock/.clean ] && { log_end_msg 0 ; exit 0 ; }
186
[ -d /tmp ] && ! [ -f /tmp/.clean ] && { clean_tmp || ES=1 ; }
187
[ -d /var/run ] && ! [ -f /var/run/.clean ] && { clean_run || ES=1 ; }
188
[ -d /var/lock ] && ! [ -f /var/lock/.clean ] && { clean_lock || ES=1 ; }