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