summaryrefslogtreecommitdiff
path: root/init.d/bootmisc.in
blob: c3e714a9a22be5fb5e6a0fab8344fd029ed561b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!@PREFIX@/sbin/runscript
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.

depend()
{
	use hostname
	need localmount
	before logger
	after clock sysctl
	keyword noprefix
}

dir_writeable()
{
	mkdir "$1"/.test.$$ 2>/dev/null && rmdir "$1"/.test.$$
}

cleanup_tmp_dir()
{
	local dir="$1"

	if ! [ -d "${dir}" ]; then
		mkdir -p "${dir}" || return $?
	fi
	dir_writeable "${dir}" || return 1
	chmod +t "${dir}"
	cd "${dir}"
	if yesno ${wipe_tmp:-${WIPE_TMP:-yes}}; then
		ebegin "Wiping ${dir} directory"
		local startopts="-x . -depth"
		[ "${RC_UNAME}" = "Linux" ] && startopts=". -xdev -depth"

		# Faster than find
		rm -rf -- [b-ikm-pr-zA-Z0-9\.]*

		find ${startopts} ! -name . \
			! -path "./lost+found" \
			! -path "./lost+found/*" \
			! -path "./quota.user" \
			! -path "./quota.user/*" \
			! -path "./aquota.user" \
			! -path "./aquota.user/*" \
			! -path "./quota.group" \
			! -path "./quota.group/*" \
			! -path "./aquota.group" \
			! -path "./aquota.group/*" \
			! -path "./journal" \
			! -path "./journal/*" \
			! -path "./.private" \
			! -path "./.private/*" \
			-exec rm -rf {} \;
		eend 0
	else
		ebegin "Cleaning ${dir} directory"
		rm -rf -- "${dir}"/.X*-lock "${dir}"/esrv* "${dir}"/kio* \
			"${dir}"/jpsock.* "${dir}"/.fam* "${dir}"/.esd* \
			"${dir}"/orbit-* "${dir}"/ssh-* "${dir}"/ksocket-* \
			"${dir}"/.*-unix
		eend 0
	fi
}

start()
{
	local logw=false
	# Ensure that our basic dirs exist
	for x in /var/log /var/run /tmp; do
		if ! [ -d "${x}" ]; then
			if ! mkdir -p "${x}"; then
				eend 1 "failed to create needed directory ${x}"
				return 1
			fi
		fi 
	done

	if dir_writeable /var/run; then
		ebegin "Creating user login records"
		cp /dev/null /var/run/utmp
		chgrp utmp /var/run/utmp
		chmod 0664 /var/run/utmp
		if dir_writeable /var/log; then
			logw=true
			[ -e /var/log/wtmp ] || cp /dev/null /var/log/wtmp
			chgrp utmp /var/log/wtmp
			chmod 0664 /var/log/wtmp
		fi
		eend 0

		ebegin "Cleaning /var/run"
		for x in $(find /var/run ! -type d ! -name utmp \
			! -name random-seed ! -name dev.db \
			! -name ld-elf.so.hints ! -name ld.so.hints);
		do
			# Clean stale sockets
			if [ -S "${x}" ]; then
				if type fuser >/dev/null 2>&1; then
					fuser "${x}" 2>/dev/null || rm "${x}"
				else
					rm "${x}"
				fi
			fi
			[ ! -f "${x}" ] && continue
			# Do not remove pidfiles of already running daemons
			case "${x}" in
				*.pid)
					start-stop-daemon --test --quiet \
					--stop --pidfile "${x}" && continue
				;;
			esac
			rm -f -- "${x}"
		done
		eend 0
	fi

	# Clean up /tmp directories
	local tmp=
	for tmp in ${wipe_tmp_dirs-/tmp}; do
		cleanup_tmp_dir "${tmp}"
	done

	if dir_writeable /tmp; then
		# Make sure our X11 stuff have the correct permissions
		# Omit the chown as bootmisc is run before network is up
		# and users may be using lame LDAP auth #139411
		rm -rf /tmp/.ICE-unix /tmp/.X11-unix 
		mkdir -p /tmp/.ICE-unix /tmp/.X11-unix
		chmod 1777 /tmp/.ICE-unix /tmp/.X11-unix
		[ -x /sbin/restorecon ] && restorecon /tmp/.ICE-unix /tmp/.X11-unix
	fi

	if ${logw} || dir_writeable /var/log; then
		# Create an 'after-boot' dmesg log
		if [ "${RC_SYS}" != "VSERVER" -a "${RC_SYS}" != "OPENVZ" ]; then
			dmesg > /var/log/dmesg
			chmod 640 /var/log/dmesg
		fi
	fi

	rm -f /etc/nologin
	return 0
}

stop()
{
	# Write a halt record if we're shutting down
	case "${RC_RUNLEVEL}" in
		reboot|shutdown) [ "${RC_UNAME}" = "Linux" ] && halt -w;;
	esac

	return 0
}