File: yadifad.init.example

package info (click to toggle)
yadifa 3.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,392 kB
  • sloc: ansic: 254,102; makefile: 1,409; pascal: 971; sh: 706
file content (311 lines) | stat: -rw-r--r-- 4,884 bytes parent folder | download | duplicates (5)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /bin/sh
#------------------------------------------------------------------------------
##
##
#------------------------------------------------------------------------------
### BEGIN INIT INFO
# Provides:          yadifad
# Required-Start:    $local_fs $network
# Required-Stop:
# Should-Start:      udev module-init-tools
# Should-Stop:		 $named
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Starts the YADIFAD name server
# Description:		Starts the YADIFAD name server 
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin
PREFIX="/usr/local"
NAME="yadifad"
DAEMON="/sbin/${NAME}"

SCRIPTNAME="/etc/init.d/${NAME}"
PIDFILE="$PREFIX/var/run/yadifa.pid"

CHROOTS=0
CHROOTDIR=$PREFIX
YADIFA_CHROOT=/var

if [ "$DEBUG" = "" ]
then
	DEBUG=0
fi

log()
{
	echo $*
	logger -t $NAME-init-script -- $*
}

debug()
{
	if [ $DEBUG -ne 0 ]
	then
		echo $*
		logger -t $NAME-init-script -- $*
	fi
}

if [ $CHROOTS -ne 0 ]
then
	PREFIX=
fi

VAR="$PREFIX/var"
CONF="$PREFIX/etc/yadifad.conf"

if [ "$MAX_STOP_WAIT" = "" ]
then
	MAX_STOP_WAIT=5
fi

# Inside the internal chroot

if [ "$CORE_PREFIX" = "" ]
then
	CORE_PREFIX=/log
fi

if [ "$DUMPS_CORE" = "" ]
then
	DUMPS_CORE=1
fi

if [ $DUMPS_CORE -ne 0 ]
then
	if [ ! -d $CHROOTDIR/$YADIFA_CHROOT/$CORE_PREFIX ]
	then
		log "Core dump enabled but '$CHROOTDIR/$YADIFA_CHROOT/$CORE_PREFIX' directory does not exists"
		exit 1
	fi
fi

# not yet:
#
# CHECK="/sbin/yadifa-checkconf"


# $? : supposed to be set
# 1  : message
# 2  : optional override code
# 3  : what to do in case of error (command)

die_on_error()
{
        ERR="$?"
        
        if [ ! "" = "$2" ]
        then
        	ERR="$2"
        fi
        
        
        if [ 0 -ne ${ERR} ]
        then
        	if [ ! "" = "$3" ]
	        then
        		$3
	        fi
	        
                log "error: '$1' ($ERR)"
                exit $err
        fi
}

#
# Function that checks if yadifa is running
#

do_status_internal()
{
	debug "checking for valid PIDFILE ${PIDFILE}"

	running=0

	if [ -f ${PIDFILE} ]
	then
		PID=$(cat ${PIDFILE})

		if [ "${PID}" != "" ]
		then
			PROC=$(ps -p ${PID} | tail -1| awk '{print $4}')

			if [ "$PROC" = "$NAME" ]
			then
				running=1
			else
				log "error : process with pid ${PID} is '$PROC' and not '$NAME' : deleting pid file"
				rm -f ${PIDFILE}
			fi
		else
			log "error: empty '${PIDFILE}' : deleting pid file"
			rm -f ${PIDFILE}
		fi
	fi

	return $running
}

do_status()
{
	do_status_internal

	running=$?
	
	if [ $running -eq 1 ]
	then
		log "${NAME} seems to be already running."
		exit 0
	else
		log "${NAME} does not seem to be running."
		exit 1
	fi
}

#
# Function that starts the daemon/service
#
do_start()
{
	log "Starting $NAME"

	debug "starting if no valid PIDFILE ${PIDFILE}"

	do_status_internal

	running=$?

	if [ $running -eq 1 ]
	then
		log "${NAME} seems to be already running."
		exit 1
	fi
	
	debug cd $CHROOTDIR
	cd $CHROOTDIR

	debug prefix = ${PREFIX}
	debug daemon = ${DAEMON}

	if [ $DUMPS_CORE -ne 0 ]
	then
		COREPATH="$CHROOTDIR/$YADIFA_CHROOT/$CORE_PREFIX/cores"

		log "enabling core dump in $COREPATH"

		ulimit -c unlimited


		mkdir -p $COREPATH
		chmod 0777 $COREPATH
		chmod +t $COREPATH

		log "core dump will be stored in '$COREPATH' ($CORE_PREFIX/cores)"

		echo "$CORE_PREFIX/cores/core.%e.%p.%h.%t" > /proc/sys/kernel/core_pattern
		echo 0x7 > /proc/self/coredump_filter
		echo -n 1 > /proc/sys/kernel/core_uses_pid
		echo -n 1 > /proc/sys/fs/suid_dumpable
	fi

	CMD="${PREFIX}${DAEMON} -c ${CONF} --daemon"
	
	if [ $CHROOTS -ne 0 ]
	then
		CMD="chroot $CHROOTDIR $CMD"
	fi

	debug $CMD

	$CMD

	die_on_error "${DAEMON} returned $?"

	exit 0
}


#
# Function that stops the daemon/service
#
do_stop()
{
	log "Stopping $NAME"

	debug "stopping if valid PIDFILE ${PIDFILE}"

	if [ -f ${PIDFILE} ]
	then
		while [ -f ${PIDFILE} ]
		do
			PID=$(cat ${PIDFILE})

			if [ "${PID}" = "" ]
			then
				log "error: empty '${PIDFILE}' : deleting pid file"
				rm -f ${PIDFILE}
				break
			fi

			PROC=$(ps -p ${PID} | tail -1| awk '{print $4}')

			if [ "$PROC" != "$NAME" ]
			then
				log "error : process with pid ${PID} is '$PROC' and not '$NAME' : deleting pid file"
				rm -f ${PIDFILE}
				break
			fi

			kill ${PID} > /dev/null 2>&1

			for count in $(seq 0 $MAX_STOP_WAIT)
			do
				if [ ! -f ${PIDFILE} ]
				then
					break
				fi

				kill -0 ${PID} > /dev/null 2>&1

				if [ $? -ne 0 ]
				then
					rm -f ${PIDFILE}
					break
				fi

				log "waiting for the process to stop (waited $count seconds)"

				sleep 1
			done
		done
	else
		log "${NAME} does not seem to be running ..."
		debug "reason: ${PIDFILE} not found"
	fi
}

debug called with $1

case "$1" in
  start)
	do_start
	;;
  stop)
	do_stop
	;;
  restart)
	do_stop
	do_start
	;;
  status)
  	do_status
	;;
  *)
	log "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
	exit 3
	;;
esac

: