aboutsummaryrefslogtreecommitdiffstats
path: root/main/clamav/clamd.initd
blob: 35e6aaf6c9cf606659d28a4ff0eca33aef7b138a (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
#!/sbin/openrc-run

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

pidfile=/run/clamav/clamd.pid
command=/usr/sbin/clamd
extra_started_commands="reload"
extra_commands="logfix"
required_files=$CONF

depend() {
	need net
	after firewall
	provide antivirus
}

start_pre() {
	# fix clamd run permissions
	local pid=`awk '$1 == "PidFile" { print $2 }' $CONF`
	[ "x$pid" != "x" ] && pidfile=$pid
	local socket=`awk '$1 == "LocalSocket" { print $2 }' $CONF`
	local socketdir=${socket%/*}
	local clamav_user=`awk '$1 == "User" { print $2 }' $CONF`
	checkpath --directory --owner -m 750 ${clamav_user:-clamav} \
		${pidfile%/*}
	checkpath --directory --owner -m 750 ${clamav_user:-clamav} \
		${socketdir:-/run/clamav}
}

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 $command
	eend $? "Failed to start ${NAME}"
}

reload() {
	ebegin "Reloading ${SVCNAME}"
	start-stop-daemon --signal HUP --pidfile $pidfile --name $SVCNAME
	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
}