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