summaryrefslogtreecommitdiff
path: root/debian/sendmail.init.d
blob: 0788bda762a226d137515eb7368f4598b4cb7b56 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/sh
set -e
# Start or stop sendmail
#
# Robert Leslie <rob@mars.org>
# Johnie Ingram <johnie@netgod.net>
# David Rocher <rocher@mail.dotcom.fr>
# Richard Nelson <cowboy@debain.org>

# How often to run the queue
Q="10m"

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/sendmail
COMMAND=/usr/sbin/sendmail
PIDFILE=/var/run/sendmail.pid
QUEUE=/var/spool/mqueue
START_CMD="start-stop-daemon \
	--pidfile $PIDFILE \
	--exec $DAEMON \
	--startas $COMMAND \
	--start"
STOP_CMD="start-stop-daemon \
	--pidfile $PIDFILE \
	--stop"
NAME=sendmail
FLAGS="defaults 50"

# Support for coexistance with smtpd package
SMTPD=/usr/sbin/smtpd

test -x $DAEMON -a -d /usr/share/sendmail || exit 0

#
# Clean sendmail queues (somewhat)
queue_clean () {
	# remove lock files left because of kill/crash
	# rm -f $QUEUE/[lnx]f* doesn't work with a plethora of files ;-{
	for i in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
		rm -f [lnx]f${i}*
		done
	# remove zero length qf files
	for qffile in $QUEUE/qf*; do
		if [ -r "$qffile" -a ! -s "$qffile" ]; then
			echo -n "<zero: $qffile> "
			rm -f "$qffile"
			fi
		done
	# rename tf files to be qf if the qf does not exist
	for tffile in $QUEUE/tf*; do
		qffile=`echo "$tffile" | sed 's/t/q/'`
		if [ -r "$tffile" -a ! -f "$qffile" ]; then
			echo -n "<recovering: $tffile> "
			mv "$tffile" "$qffile"
		elif [ -f "$tffile" ]; then
			echo -n "<extra: $tffile> " 
			rm -f "$tffile"
			fi
		done
	# remove df files with no corresponding qf files
	for dffile in $QUEUE/df*; do
		qffile=`echo "$dffile" | sed 's/d/q/'`
		if [ -r "$dffile" -a ! -f "$qffile" ]; then
			echo -n "<incomplete: $dffile> "
			mv "$dffile" `echo $dffile | sed 's/d/D/'`
			fi
		done
	# announce files that have been saved during disaster recovery
	for xffile in $QUEUE/[A-Z]f*; do
		if [ -f "$xffile" ]; then
			echo -n "<panic: $xffile> "
			fi
		done
	}

#
# enhanced sendmail startup
start() {
	# cleanup queues
	queue_clean;

	# Skip daemon run for the following:
	#   * sendmail hasn't been configured
	#   * smptd, a firewall frontend for sendmail, is installed
	#   * If running a nullclient
	if [ ! -s /etc/mail/sendmail.mc ]; then
		echo "sendmail has not been configured, not started."
		echo "To configure sendmail, type sendmailconfig"
		exit 0
	elif [ -x $SMTPD ] ||  \
		egrep -qe "^[[:space:]]*FEATURE(nullclient)" \
		/etc/mail/sendmail.mc \
		; then
		echo "sendmail daemon not needed, not started."
		exit 0
		fi
	# Ok, really start the puppy
	cd /var/lib/sendmail
	$START_CMD -- -bd -q"$Q"
	}

#
# enhanced sendmail shutdown
stop () {
	$STOP_CMD --quiet --oknodo > /dev/null
	#
	#       Now we have to wait until sendmail has _really_ stopped.
	#
	sleep 2
	if $STOP_CMD --quiet > /dev/null; then
		echo -n "Waiting ."
		cnt=0
		while $STOP_CMD --quiet > /dev/null; do
			cnt=`expr $cnt + 1`
			if [ $cnt -gt 60 ]; then
				#
				#       Waited 120 seconds now. Fail.
				#
				echo -n " Failed "
				break
				fi
			sleep 2
			echo -n "."
			done
		echo -n " Done "
		fi
	}

case "$1" in
    start)
	echo -n "Starting mail transport agent: "
	start
	echo "$NAME."
    ;;

    stop)
	echo -n "Stopping mail transport agent: "
	stop
	echo "$NAME."
    ;;

    restart)
        $0 stop
        $0 start
    ;;
    
    reload)
	echo -n "Reloading $NAME configuration..."
	$STOP_CMD --signal HUP
	echo "done." 
    ;;

    force-reload)
        $0 reload
    ;;

    debug)
	echo -n "Dumping $NAME state..."
	$STOP_CMD --signal USR1
	echo "done."
    ;;

    *)
	echo "Usage: /etc/init.d/sendmail {start|stop|restart|reload|force-reload|debug}"
	exit 1
    ;;
esac

exit 0