quagga.init.in 8.0 KB

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