11.1Slukem#!/bin/sh
21.1Slukem#
31.13Smartin# $NetBSD: cleartmp,v 1.13 2018/09/30 10:38:05 martin Exp $
41.1Slukem#
51.1Slukem
61.1Slukem# PROVIDE: cleartmp
71.1Slukem# REQUIRE: mountall
81.4Sthorpej# BEFORE:  DAEMON
91.1Slukem
101.5Smycroft$_rc_subr_loaded . /etc/rc.subr
111.1Slukem
121.1Slukemname="cleartmp"
131.3Slukemrcvar="clear_tmp"
141.1Slukemstart_cmd="cleartmp_start"
151.1Slukemstop_cmd=":"
161.1Slukem
171.1Slukemcleartmp_start()
181.1Slukem{
191.8Selad	echo "Clearing temporary files."
201.9Sxtraeme	if checkyesno per_user_tmp && [ -d ${per_user_tmp_dir} ]; then
211.8Selad		tmp_dir=${per_user_tmp_dir}
221.8Selad	else
231.8Selad		tmp_dir="/tmp"
241.9Sxtraeme		# Check if /tmp was created by the perusertmp rc.d
251.9Sxtraeme		# script and recreate it if necessary.
261.10Smjf		if [ "$(/usr/bin/readlink /tmp)" = ${per_user_tmp_dir}/@ruid ]; then
271.9Sxtraeme			/bin/rm -rf ${tmp_dir}
281.9Sxtraeme			/bin/mkdir ${tmp_dir}
291.12Sdholland			/sbin/chown root:wheel ${tmp_dir}
301.9Sxtraeme			/bin/chmod 1777 ${tmp_dir}
311.9Sxtraeme		fi
321.8Selad	fi
331.8Selad
341.11Sapb	#
351.11Sapb	#	Delete almost everything, except lost+found, quota.user,
361.11Sapb	#	and quota.group in the top level.  (This is not needed
371.11Sapb	#	with mfs or tmpfs /tmp, but doesn't hurt anything).
381.11Sapb	#
391.11Sapb	#	The find command, with "-exec ... +" instead of "-exec
401.11Sapb	#	... \;", will pass many file or dir names to each
411.11Sapb	#	invocation of "rm -rf".  We avoid using any glob
421.11Sapb	#	patterns because of the risk of "Arg list too long"
431.11Sapb	#	errors when there are very many files.
441.11Sapb	#
451.11Sapb	(cd ${tmp_dir} &&
461.1Slukem	    find -x . ! -name . ! -name lost+found ! -name quota.user \
471.11Sapb		! -name quota.group -exec rm -rf -- {} \+ -type d -prune)
481.13Smartin
491.13Smartin	# if requested, create symlink for /var/shm
501.13Smartin	if [ -n "${var_shm_symlink}" ]; then
511.13Smartin		/bin/rm -rf /var/shm
521.13Smartin		/bin/mkdir -m 1777 "${var_shm_symlink}"
531.13Smartin		/bin/ln -s "${var_shm_symlink}" /var/shm
541.13Smartin	fi
551.1Slukem}
561.1Slukem
571.2Slukemload_rc_config $name
581.1Slukemrun_rc_command "$1"
59