aboutsummaryrefslogtreecommitdiffstats
path: root/main/clamav/clamd.initd
blob: d8c8f4549db62ad6b9c4a95fd42a67bf0d410251 (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
#!/sbin/runscript

extra_started_commands="reload"
extra_commands="logfix"

NAME=clamd
CONF=/etc/clamav/clamd.conf

depend() {
	need net
	after firewall
	provide antivirus
}

start() {
	local clamd_socket=$(awk '$1 == "LocalSocket" { print $2 }' $CONF)

	logfix

	if [ -S "${clamd_socket:=/tmp/clamd}" ]; then
		rm -f ${clamd_socket}
	fi

	local dbdir=$(awk '$1 == "DatabaseDirectory" { print $2 }' $CONF)
	local timeout=${FRESHCLAM_TIMEOUT:-120}
	local cvd="${dbdir:-/var/lib/clamav}"/main.cvd
	local cld="${dbdir:-/var/lib/clamav}"/main.cld

	if ! [ -e "$cld" ]; then
		if ! [ -e "$cvd" ]; then
			ebegin "Waiting for clamav database download"
			while ! [ -e "$cvd" ]; do
				timeout=$(( $timeout - 1 ))
				if [ $timeout -eq 0 ]; then
					eend 1 "Timed out"
					return 1
				fi
				sleep 1
			done
			eend 0
		fi
	fi
	ebegin "Starting ${NAME}"
	start-stop-daemon --start --quiet \
		--nicelevel ${CLAMD_NICELEVEL:-0} \
		--exec /usr/sbin/clamd
	eend $? "Failed to start ${NAME}"
}

stop() {
	ebegin "Stopping ${NAME}"
	start-stop-daemon --stop --quiet --exec /usr/sbin/clamd
	eend $?
}

reload() {
	ebegin "Reloading ${NAME}"
	if ! service_started "${NAME}" ; then
		eend 1 "${NAME} is not started"
		return 1
	fi
	start-stop-daemon --stop --signal HUP \
		--exec /usr/sbin/clamd
	eend $?
}

logfix() {
	# fix clamd log permissions
	# (might be clobbered by logrotate or something)
	local logfile=`awk '$1 == "LogFile" { print $2 }' $CONF`
	local clamav_user=`awk '$1 == "User" { print $2 }' $CONF`
	if [ -n "${logfile}" ] && [ -n "${clamav_user}" ]; then
		if [ ! -f "${logfile}" ]; then
			touch ${logfile}
		fi
		chown ${clamav_user} ${logfile}
		chmod 640 ${logfile}
	fi
}