xref: /freebsd/libexec/rc/rc.d/cleartmp (revision 0696600c41600d80bcd993bfd8e675d0ae6951fe)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# $FreeBSD$
4*0696600cSBjoern A. Zeeb#
5*0696600cSBjoern A. Zeeb
6*0696600cSBjoern A. Zeeb# PROVIDE: cleartmp
7*0696600cSBjoern A. Zeeb# REQUIRE: mountcritremote tmp
8*0696600cSBjoern A. Zeeb# BEFORE: DAEMON
9*0696600cSBjoern A. Zeeb
10*0696600cSBjoern A. Zeeb. /etc/rc.subr
11*0696600cSBjoern A. Zeeb
12*0696600cSBjoern A. Zeebname="cleartmp"
13*0696600cSBjoern A. Zeebdesc="Purge /tmp directory"
14*0696600cSBjoern A. Zeeb# Disguise rcvar for the start method to run irrespective of its setting.
15*0696600cSBjoern A. Zeebrcvar1="clear_tmp_enable"
16*0696600cSBjoern A. Zeebstart_cmd="${name}_start"
17*0696600cSBjoern A. Zeebstop_cmd=":"
18*0696600cSBjoern A. Zeeb
19*0696600cSBjoern A. Zeebcleartmp_start()
20*0696600cSBjoern A. Zeeb{
21*0696600cSBjoern A. Zeeb	# Make /tmp location variable for easier debugging.
22*0696600cSBjoern A. Zeeb	local tmp="/tmp"
23*0696600cSBjoern A. Zeeb
24*0696600cSBjoern A. Zeeb	# X related directories to create in /tmp.
25*0696600cSBjoern A. Zeeb	local x11_socket_dirs="${tmp}/.X11-unix ${tmp}/.XIM-unix \
26*0696600cSBjoern A. Zeeb			       ${tmp}/.ICE-unix ${tmp}/.font-unix"
27*0696600cSBjoern A. Zeeb
28*0696600cSBjoern A. Zeeb	if checkyesno ${rcvar1}; then
29*0696600cSBjoern A. Zeeb		check_startmsgs && echo "Clearing ${tmp}."
30*0696600cSBjoern A. Zeeb
31*0696600cSBjoern A. Zeeb		# This is not needed for mfs, but doesn't hurt anything.
32*0696600cSBjoern A. Zeeb		# Things to note:
33*0696600cSBjoern A. Zeeb		# + The dot in ${tmp}/. is important.
34*0696600cSBjoern A. Zeeb		# + Put -prune before -exec so find never descends
35*0696600cSBjoern A. Zeeb		#   into a directory that was already passed to rm -rf.
36*0696600cSBjoern A. Zeeb		# + "--" in rm arguments isn't strictly necessary, but
37*0696600cSBjoern A. Zeeb		#   it can prevent foot-shooting in future.
38*0696600cSBjoern A. Zeeb		# + /tmp/lost+found is preserved, but its contents are removed.
39*0696600cSBjoern A. Zeeb		# + lost+found and quota.* in subdirectories are removed.
40*0696600cSBjoern A. Zeeb		# + .sujournal and .snap are preserved.
41*0696600cSBjoern A. Zeeb		find -x ${tmp}/. ! -name . \
42*0696600cSBjoern A. Zeeb		    ! \( -name .sujournal -type f -user root \) \
43*0696600cSBjoern A. Zeeb		    ! \( -name .snap -type d -user root \) \
44*0696600cSBjoern A. Zeeb		    ! \( -name lost+found -type d -user root \) \
45*0696600cSBjoern A. Zeeb		    ! \( \( -name quota.user -or -name quota.group \) \
46*0696600cSBjoern A. Zeeb			-type f -user root \) \
47*0696600cSBjoern A. Zeeb		    -prune -exec rm -rf -- {} +
48*0696600cSBjoern A. Zeeb	elif checkyesno clear_tmp_X; then
49*0696600cSBjoern A. Zeeb		# Remove X lock files, since they will prevent you from
50*0696600cSBjoern A. Zeeb		# restarting X.  Remove other X related directories.
51*0696600cSBjoern A. Zeeb		check_startmsgs && echo "Clearing ${tmp} (X related)."
52*0696600cSBjoern A. Zeeb		rm -rf ${tmp}/.X[0-9]-lock ${x11_socket_dirs}
53*0696600cSBjoern A. Zeeb	fi
54*0696600cSBjoern A. Zeeb	if checkyesno clear_tmp_X; then
55*0696600cSBjoern A. Zeeb		# Create X related directories with proper permissions.
56*0696600cSBjoern A. Zeeb		mkdir -m 1777 ${x11_socket_dirs}
57*0696600cSBjoern A. Zeeb	fi
58*0696600cSBjoern A. Zeeb}
59*0696600cSBjoern A. Zeeb
60*0696600cSBjoern A. Zeebload_rc_config $name
61*0696600cSBjoern A. Zeebrun_rc_command "$1"
62