File: quagga.init.in

package info (click to toggle)
quagga 0.99.23.1-1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 14,748 kB
  • sloc: ansic: 215,042; sh: 12,693; perl: 805; makefile: 644; exp: 184; lisp: 62; awk: 30
file content (302 lines) | stat: -rwxr-xr-x 8,157 bytes parent folder | download | duplicates (4)
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
#!/sbin/sh
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# This file is part of Quagga.
#
# Quagga is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Quagga is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Quagga; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# Starts/stops the given daemon

SMFINCLUDE=/lib/svc/share/smf_include.sh
ROUTEADMINCLUDE=/lib/svc/share/routing_include.sh
GLOBAL_OPTIONS="PAfiug"
DAEMON_PATH=@sbindir@
USER=@enable_user@
GROUP=@enable_group@

# handle upgrade of daemon-args SMF property to new routeadm properties
# used during upgrade too by routeadm.
# relevant to S10U4+ only.
handle_routeadm_upgrade () {
	GLOBAL_OPTIONS="PAfiug"
	
	daemon_args=`get_daemon_args $SMF_FMRI`
	
	if [ -n "$daemon_args" ]; then
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "P" vty_port 0
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "A" vty_address
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "f" config_file
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "i" pid_file
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "u" user
		set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
			"$GLOBAL_OPTIONS" "g" group
		
		case "$1" in
		zebra)
			set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
				"${GLOBAL_OPTIONS}b" "b" batch true false
			;;
		ripd|ripngd)
			set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
				"${GLOBAL_OPTIONS}r" "r" retain true false
			;;
		bgpd)
			set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
				"${GLOBAL_OPTIONS}rnp" "r" retain true false
			set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
				"${GLOBAL_OPTIONS}rnp" "n" no_kernel true false
			set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
				"${GLOBAL_OPTIONS}rnp" "p" bgp_port
		esac
		clear_daemon_args $SMF_FMRI
	fi
}

upgrade_config () {
	DAEMON=$1
	# handle upgrade of SUNWzebra to Quagga
	if [ -d "/etc/quagga" -a ! -f "/etc/quagga/${DAEMON}.conf" ] ; then
		if [ -f "/etc/sfw/zebra/${DAEMON}.conf" ] ; then
			cp "/etc/sfw/zebra/${DAEMON}.conf" \
				"/etc/quagga/${DAEMON}.conf.upgrade" \
				|| exit $SMF_EXIT_ERR_FATAL
			chown "${USER}:${GROUP}" "/etc/quagga/${DAEMON}.conf.upgrade" \
				|| exit $SMF_EXIT_ERR_FATAL
			chmod 0600 "/etc/quagga/${DAEMON}.conf.upgrade" \
				|| exit $SMF_EXIT_ERR_FATAL
			mv "/etc/quagga/${DAEMON}.conf.upgrade" "/etc/quagga/${DAEMON}.conf" \
				|| exit $SMF_EXIT_ERR_FATAL
		fi
	fi

	if [ ! -f "/etc/quagga/${DAEMON}.conf" ] ; then
		touch "/etc/quagga/${DAEMON}.conf.new" \
			|| exit $SMF_EXIT_ERR_FATAL
		chown "${USER}:${GROUP}" "/etc/quagga/${DAEMON}.conf.new" \
			|| exit $SMF_EXIT_ERR_FATAL
		chmod 0600 "/etc/quagga/${DAEMON}.conf.new" \
			|| exit $SMF_EXIT_ERR_FATAL
		mv "/etc/quagga/${DAEMON}.conf.new" "/etc/quagga/${DAEMON}.conf" \
			|| exit $SMF_EXIT_ERR_FATAL
	fi
}

# Relevant to S10+
quagga_is_globalzone () {
	if [ "${QUAGGA_INIT_ZONENAME:=`/sbin/zonename`}" = "global" \
		-o `/sbin/zonename -t` = "exclusive" ]; then
		return 0
	else
		return 1
	fi
}

routeadm_daemon_args () {
	# globals
	args="`get_daemon_option_from_property $SMF_FMRI config_file f`"
	args="${args} `get_daemon_option_from_property $SMF_FMRI vty_port P`"
	args="${args} `get_daemon_option_from_property $SMF_FMRI vty_address A`"
	args="${args} `get_daemon_option_from_property $SMF_FMRI pid_file i`"
	
	# user and group we need for config file upgrade..
	SMF_USER=`get_routeadm_property $SMF_FMRI user`
	SMF_GROUP=`get_routeadm_property()$SMF_FMRI group`
	if [ "${SMF_USER}" ] ; then
		USER="${SMF_USER}"
		args="${args} -u ${SMF_USER}"
	fi
	if [ "${SMF_GROUP}" ] ; then 
		GROUP="${SMF_GROUP}"
		args="${args} -g ${SMF_GROUP}"
	fi

	case $1 in
	zebra)
		args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI batch -b true`"
		;;
	ripd|ripngd)
		args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI retain -r true`"
		;;
	bgpd)
		args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI retain -r true`"
		args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI no_kernel -n true`"
		args="${args} `get_daemon_option_from_property $SMF_FMRI bgp_port p 179`"
		;;
	esac
	echo ${args}
}

# certain daemons need zebra
routeadm_zebra_enable () {

	if [ "$DAEMON" = "zebra" ]; then
		return
	fi
	
	enable_zebra=`/usr/bin/svcprop -p \
		routing/enable_zebra $SMF_FMRI 2> /dev/null`
	if [ "$enable_zebra" != "false" ]; then
		zenabled=`/usr/bin/svcprop -p general/enabled zebra:quagga`
		zenabledt=`/usr/bin/svcprop -p general_ovr/enabled zebra:quagga`
		if [ "$zenabled" = "true" -o "$zenabledt" = "true" ]; then
			/usr/sbin/svcadm disable zebra:quagga
			/usr/sbin/svcadm enable -st zebra:quagga
		else
			/usr/sbin/svcadm enable -st zebra:quagga 
		fi
		if [ "$?" != "0" ]; then
			echo "Could not enable zebra:quagga"
			exit $SMF_EXIT_ERR_FATAL
		fi
	fi
}

# Include smf functions, if available. If not, define smf_present to indicate
# there is no SMF. Should allow this script to work pre-S10.
if [ -f "$SMFINCLUDE" ] ; then
	. "$SMFINCLUDE";
	
	# source the SMF-routeadm include if present..
	if [ -f "$ROUTEADMINCLUDE" ] ; then
		. "$ROUTEADMINCLUDE"
	fi
else
	# pre-SMF system, fake up any functions and exit codes
	# which SMFINCLUDE usually provides.
	smf_present () {
		return 1
	}
	SMF_EXIT_OK=0;
	SMF_EXIT_ERR_CONFIG=96;
	SMF_EXIT_ERR_FATAL=95;
fi
	
# if there's no SMF, set some default DAEMON_ARGS
smf_present || DAEMON_ARGS=""

usage () {
	if smf_present ; then
		echo "Usage: $0 <daemon>";
	else
		echo "Usage: $0 <stop|start> <daemon> <daemon arguments>";
	fi
	echo "The --pid_file argument is implied";
	echo "This help message: $0 <help|usage>";
}

# parse arguments, different according to SMF or not.
case $1 in
	'help' | 'usage')
		usage
		exit $SMF_EXIT_OK
		;;
esac

if smf_present ; then
	QUAGGA_METHOD="start"
else
	QUAGGA_METHOD="$1"
	shift;
fi

DAEMON="$1"

# daemon path must be given
if [ -z "$DAEMON_PATH/$DAEMON" ]; then
	usage
	exit $SMF_EXIT_ERR_FATAL
fi

# only bgpd is suitable for running in a non-global zone, at this
# time.
case "${DAEMON}" in
	bgpd)
	;;
	zebra | ospfd | ospf6d | ripd | ripngd )
		quagga_is_globalzone || exit $SMF_EXIT_OK
	;;
	*)
		usage
		exit $SMF_EXIT_ERR_CONFIG;
	;;
esac

# Older Quagga SMF packages pass daemon args on the commandline
# Newer SMF routeadm model uses properties for each argument
# so we must handle that.
if [ smf_present -a -f "$ROUTEADMINCLUDE" ]; then
	handle_routeadm_upgrade $DAEMON;
	DAEMON_ARGS=`routeadm_daemon_args`;
	routeadm_zebra_enable $DAEMON;
else
	if [ $# -gt 0 ] ; then
		shift
		DAEMON_ARGS="$@"
	fi
fi

upgrade_config "$DAEMON"

if [ ! -f "@sysconfdir@/${DAEMON}.conf" ] ; then
	echo "Could not find config file, @sysconfdir@/${DAEMON}.conf"
	exit $SMF_EXIT_ERR_CONFIG
fi

# we need @quagga_statedir@ to exist, it probably is on tmpfs.
if [ ! -d @quagga_statedir@ ] ; then
	mkdir -p @quagga_statedir@
	chown @enable_user@:@enable_group@ @quagga_statedir@
	chmod 751 @quagga_statedir@
fi

PIDFILE="@quagga_statedir@/${DAEMON}.pid"

start () {
	if [ ! -x "$DAEMON_PATH/$DAEMON" ] ; then
		echo "Error, could not find daemon, $DAEMON_PATH/$DAEMON"
		exit $SMF_EXIT_ERR_FATAL
	fi
	eval exec $DAEMON_PATH/$DAEMON $DAEMON_ARGS --pid_file ${PIDFILE} &
}

stop_by_pidfile () {
	if [ -f "${PIDFILE}" ]; then
		/usr/bin/kill -TERM `/usr/bin/cat "${PIDFILE}"`
	fi
}

case "$QUAGGA_METHOD" in
'start')
	start
	;;
'stop')
	stop_by_pidfile
	;;

*)
	usage
	exit $SMF_EXIT_ERR_FATAL
	;;
esac	

exit $SMF_EXIT_OK;