1#!/bin/sh
2#
3# $NetBSD: cleartmp,v 1.13 2018/09/30 10:38:05 martin Exp $
4#
5
6# PROVIDE: cleartmp
7# REQUIRE: mountall
8# BEFORE:  DAEMON
9
10$_rc_subr_loaded . /etc/rc.subr
11
12name="cleartmp"
13rcvar="clear_tmp"
14start_cmd="cleartmp_start"
15stop_cmd=":"
16
17cleartmp_start()
18{
19	echo "Clearing temporary files."
20	if checkyesno per_user_tmp && [ -d ${per_user_tmp_dir} ]; then
21		tmp_dir=${per_user_tmp_dir}
22	else
23		tmp_dir="/tmp"
24		# Check if /tmp was created by the perusertmp rc.d
25		# script and recreate it if necessary.
26		if [ "$(/usr/bin/readlink /tmp)" = ${per_user_tmp_dir}/@ruid ]; then
27			/bin/rm -rf ${tmp_dir}
28			/bin/mkdir ${tmp_dir}
29			/sbin/chown root:wheel ${tmp_dir}
30			/bin/chmod 1777 ${tmp_dir}
31		fi
32	fi
33
34	#
35	#	Delete almost everything, except lost+found, quota.user,
36	#	and quota.group in the top level.  (This is not needed
37	#	with mfs or tmpfs /tmp, but doesn't hurt anything).
38	#
39	#	The find command, with "-exec ... +" instead of "-exec
40	#	... \;", will pass many file or dir names to each
41	#	invocation of "rm -rf".  We avoid using any glob
42	#	patterns because of the risk of "Arg list too long"
43	#	errors when there are very many files.
44	#
45	(cd ${tmp_dir} &&
46	    find -x . ! -name . ! -name lost+found ! -name quota.user \
47		! -name quota.group -exec rm -rf -- {} \+ -type d -prune)
48
49	# if requested, create symlink for /var/shm
50	if [ -n "${var_shm_symlink}" ]; then
51		/bin/rm -rf /var/shm
52		/bin/mkdir -m 1777 "${var_shm_symlink}"
53		/bin/ln -s "${var_shm_symlink}" /var/shm
54	fi
55}
56
57load_rc_config $name
58run_rc_command "$1"
59