quagga.init.in 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/sbin/sh
  2. #
  3. # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
  4. # Use is subject to license terms.
  5. #
  6. # This file is part of Quagga.
  7. #
  8. # Quagga is free software; you can redistribute it and/or modify it
  9. # under the terms of the GNU General Public License as published by the
  10. # Free Software Foundation; either version 2, or (at your option) any
  11. # later version.
  12. #
  13. # Quagga is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Quagga; see the file COPYING. If not, write to the Free
  20. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. # 02111-1307, USA.
  22. #
  23. # $Id$
  24. #
  25. # Starts/stops the given daemon
  26. SMFINCLUDE=/lib/svc/share/smf_include.sh
  27. ROUTEADMINCLUDE=/lib/svc/share/routing_include.sh
  28. GLOBAL_OPTIONS="PAfiug"
  29. DAEMON_PATH=@sbindir@
  30. USER=@enable_user@
  31. GROUP=@enable_group@
  32. # handle upgrade of daemon-args SMF property to new routeadm properties
  33. # used during upgrade too by routeadm.
  34. # relevant to S10U4+ only.
  35. handle_routeadm_upgrade () {
  36. GLOBAL_OPTIONS="PAfiug"
  37. daemon_args=`get_daemon_args $SMF_FMRI`
  38. if [ -n "$daemon_args" ]; then
  39. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  40. "$GLOBAL_OPTIONS" "P" vty_port 0
  41. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  42. "$GLOBAL_OPTIONS" "A" vty_address
  43. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  44. "$GLOBAL_OPTIONS" "f" config_file
  45. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  46. "$GLOBAL_OPTIONS" "i" pid_file
  47. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  48. "$GLOBAL_OPTIONS" "u" user
  49. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  50. "$GLOBAL_OPTIONS" "g" group
  51. case "$1" in
  52. zebra)
  53. set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
  54. "${GLOBAL_OPTIONS}b" "b" batch true false
  55. ;;
  56. ripd|ripngd)
  57. set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
  58. "${GLOBAL_OPTIONS}r" "r" retain true false
  59. ;;
  60. bgpd)
  61. set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
  62. "${GLOBAL_OPTIONS}rnp" "r" retain true false
  63. set_daemon_boolean_property "$SMF_FMRI" "$daemon_args" \
  64. "${GLOBAL_OPTIONS}rnp" "n" no_kernel true false
  65. set_daemon_value_property "$SMF_FMRI" "$daemon_args" \
  66. "${GLOBAL_OPTIONS}rnp" "p" bgp_port
  67. esac
  68. clear_daemon_args $SMF_FMRI
  69. fi
  70. }
  71. upgrade_config () {
  72. DAEMON=$1
  73. # handle upgrade of SUNWzebra to Quagga
  74. if [ -d "/etc/quagga" -a ! -f "/etc/quagga/${DAEMON}.conf" ] ; then
  75. if [ -f "/etc/sfw/zebra/${DAEMON}.conf" ] ; then
  76. cp "/etc/sfw/zebra/${DAEMON}.conf" \
  77. "/etc/quagga/${DAEMON}.conf.upgrade" \
  78. || exit $SMF_EXIT_ERR_FATAL
  79. chown "${USER}:${GROUP}" "/etc/quagga/${DAEMON}.conf.upgrade" \
  80. || exit $SMF_EXIT_ERR_FATAL
  81. chmod 0600 "/etc/quagga/${DAEMON}.conf.upgrade" \
  82. || exit $SMF_EXIT_ERR_FATAL
  83. mv "/etc/quagga/${DAEMON}.conf.upgrade" "/etc/quagga/${DAEMON}.conf" \
  84. || exit $SMF_EXIT_ERR_FATAL
  85. fi
  86. fi
  87. if [ ! -f "/etc/quagga/${DAEMON}.conf" ] ; then
  88. touch "/etc/quagga/${DAEMON}.conf.new" \
  89. || exit $SMF_EXIT_ERR_FATAL
  90. chown "${USER}:${GROUP}" "/etc/quagga/${DAEMON}.conf.new" \
  91. || exit $SMF_EXIT_ERR_FATAL
  92. chmod 0600 "/etc/quagga/${DAEMON}.conf.new" \
  93. || exit $SMF_EXIT_ERR_FATAL
  94. mv "/etc/quagga/${DAEMON}.conf.new" "/etc/quagga/${DAEMON}.conf" \
  95. || exit $SMF_EXIT_ERR_FATAL
  96. fi
  97. }
  98. # Relevant to S10+
  99. quagga_is_globalzone () {
  100. if [ "${QUAGGA_INIT_ZONENAME:=`/sbin/zonename`}" = "global" \
  101. -o `/sbin/zonename -t` = "exclusive" ]; then
  102. return 0
  103. else
  104. return 1
  105. fi
  106. }
  107. routeadm_daemon_args () {
  108. # globals
  109. args="`get_daemon_option_from_property $SMF_FMRI config_file f`"
  110. args="${args} `get_daemon_option_from_property $SMF_FMRI vty_port P`"
  111. args="${args} `get_daemon_option_from_property $SMF_FMRI vty_address A`"
  112. args="${args} `get_daemon_option_from_property $SMF_FMRI pid_file i`"
  113. # user and group we need for config file upgrade..
  114. SMF_USER=`get_routeadm_property $SMF_FMRI user`
  115. SMF_GROUP=`get_routeadm_property()$SMF_FMRI group`
  116. if [ "${SMF_USER}" ] ; then
  117. USER="${SMF_USER}"
  118. args="${args} -u ${SMF_USER}"
  119. fi
  120. if [ "${SMF_GROUP}" ] ; then
  121. GROUP="${SMF_GROUP}"
  122. args="${args} -g ${SMF_GROUP}"
  123. fi
  124. case $1 in
  125. zebra)
  126. args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI batch -b true`"
  127. ;;
  128. ripd|ripngd)
  129. args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI retain -r true`"
  130. ;;
  131. bgpd)
  132. args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI retain -r true`"
  133. args="${args} `get_daemon_option_from_boolean_property $SMF_FMRI no_kernel -n true`"
  134. args="${args} `get_daemon_option_from_property $SMF_FMRI bgp_port p 179`"
  135. ;;
  136. esac
  137. echo ${args}
  138. }
  139. # certain daemons need zebra
  140. routeadm_zebra_enable () {
  141. if [ "$DAEMON" = "zebra" ]; then
  142. return
  143. fi
  144. enable_zebra=`/usr/bin/svcprop -p \
  145. routing/enable_zebra $SMF_FMRI 2> /dev/null`
  146. if [ "$enable_zebra" != "false" ]; then
  147. zenabled=`/usr/bin/svcprop -p general/enabled zebra:quagga`
  148. zenabledt=`/usr/bin/svcprop -p general_ovr/enabled zebra:quagga`
  149. if [ "$zenabled" = "true" -o "$zenabledt" = "true" ]; then
  150. /usr/sbin/svcadm disable zebra:quagga
  151. /usr/sbin/svcadm enable -st zebra:quagga
  152. else
  153. /usr/sbin/svcadm enable -st zebra:quagga
  154. fi
  155. if [ "$?" != "0" ]; then
  156. echo "Could not enable zebra:quagga"
  157. exit $SMF_EXIT_ERR_FATAL
  158. fi
  159. fi
  160. }
  161. # Include smf functions, if available. If not, define smf_present to indicate
  162. # there is no SMF. Should allow this script to work pre-S10.
  163. if [ -f "$SMFINCLUDE" ] ; then
  164. . "$SMFINCLUDE";
  165. # source the SMF-routeadm include if present..
  166. if [ -f "$ROUTEADMINCLUDE" ] ; then
  167. . "$ROUTEADMINCLUDE"
  168. fi
  169. else
  170. # pre-SMF system, fake up any functions and exit codes
  171. # which SMFINCLUDE usually provides.
  172. smf_present () {
  173. return 1
  174. }
  175. SMF_EXIT_OK=0;
  176. SMF_EXIT_ERR_CONFIG=96;
  177. SMF_EXIT_ERR_FATAL=95;
  178. fi
  179. # if there's no SMF, set some default DAEMON_ARGS
  180. smf_present || DAEMON_ARGS=""
  181. usage () {
  182. if smf_present ; then
  183. echo "Usage: $0 <daemon>";
  184. else
  185. echo "Usage: $0 <stop|start> <daemon> <daemon arguments>";
  186. fi
  187. echo "The --pid_file argument is implied";
  188. echo "This help message: $0 <help|usage>";
  189. }
  190. # parse arguments, different according to SMF or not.
  191. case $1 in
  192. 'help' | 'usage')
  193. usage
  194. exit $SMF_EXIT_OK
  195. ;;
  196. esac
  197. if smf_present ; then
  198. QUAGGA_METHOD="start"
  199. else
  200. QUAGGA_METHOD="$1"
  201. shift;
  202. fi
  203. DAEMON="$1"
  204. # daemon path must be given
  205. if [ -z "$DAEMON_PATH/$DAEMON" ]; then
  206. usage
  207. exit $SMF_EXIT_ERR_FATAL
  208. fi
  209. # only bgpd is suitable for running in a non-global zone, at this
  210. # time.
  211. case "${DAEMON}" in
  212. bgpd)
  213. ;;
  214. zebra | ospfd | ospf6d | ripd | ripngd )
  215. quagga_is_globalzone || exit $SMF_EXIT_OK
  216. ;;
  217. *)
  218. usage
  219. exit $SMF_EXIT_ERR_CONFIG;
  220. ;;
  221. esac
  222. # Older Quagga SMF packages pass daemon args on the commandline
  223. # Newer SMF routeadm model uses properties for each argument
  224. # so we must handle that.
  225. if [ smf_present -a -f "$ROUTEADMINCLUDE" ]; then
  226. handle_routeadm_upgrade $DAEMON;
  227. DAEMON_ARGS=`routeadm_daemon_args`;
  228. routeadm_zebra_enable $DAEMON;
  229. else
  230. if [ $# -gt 0 ] ; then
  231. shift
  232. DAEMON_ARGS="$@"
  233. fi
  234. fi
  235. upgrade_config "$DAEMON"
  236. if [ ! -f "@sysconfdir@/${DAEMON}.conf" ] ; then
  237. echo "Could not find config file, @sysconfdir@/${DAEMON}.conf"
  238. exit $SMF_EXIT_ERR_CONFIG
  239. fi
  240. # we need @quagga_statedir@ to exist, it probably is on tmpfs.
  241. if [ ! -d @quagga_statedir@ ] ; then
  242. mkdir -p @quagga_statedir@
  243. chown @enable_user@:@enable_group@ @quagga_statedir@
  244. chmod 751 @quagga_statedir@
  245. fi
  246. PIDFILE="@quagga_statedir@/${DAEMON}.pid"
  247. start () {
  248. if [ ! -x "$DAEMON_PATH/$DAEMON" ] ; then
  249. echo "Error, could not find daemon, $DAEMON_PATH/$DAEMON"
  250. exit $SMF_EXIT_ERR_FATAL
  251. fi
  252. eval exec $DAEMON_PATH/$DAEMON $DAEMON_ARGS --pid_file ${PIDFILE} &
  253. }
  254. stop_by_pidfile () {
  255. if [ -f "${PIDFILE}" ]; then
  256. /usr/bin/kill -TERM `/usr/bin/cat "${PIDFILE}"`
  257. fi
  258. }
  259. case "$QUAGGA_METHOD" in
  260. 'start')
  261. start
  262. ;;
  263. 'stop')
  264. stop_by_pidfile
  265. ;;
  266. *)
  267. usage
  268. exit $SMF_EXIT_ERR_FATAL
  269. ;;
  270. esac
  271. exit $SMF_EXIT_OK;