quagga.init.in 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. # Include smf functions, if available. If not, define smf_present to indicate
  138. # there is no SMF. Should allow this script to work pre-S10.
  139. if [ -f "$SMFINCLUDE" ] ; then
  140. . "$SMFINCLUDE";
  141. # source the SMF-routeadm include if present..
  142. if [ -f "$ROUTEADMINCLUDE" ] ; then
  143. . "$ROUTEADMINCLUDE"
  144. fi
  145. else
  146. # pre-SMF system, fake up any functions and exit codes
  147. # which SMFINCLUDE usually provides.
  148. smf_present () {
  149. return 1
  150. }
  151. SMF_EXIT_OK=0;
  152. SMF_EXIT_ERR_CONFIG=96;
  153. SMF_EXIT_ERR_FATAL=95;
  154. fi
  155. # if there's no SMF, set some default DAEMON_ARGS
  156. smf_present || DAEMON_ARGS=""
  157. usage () {
  158. if smf_present ; then
  159. echo "Usage: $0 <daemon>";
  160. else
  161. echo "Usage: $0 <stop|start> <daemon> <daemon arguments>";
  162. fi
  163. echo "The --pid_file argument is implied";
  164. echo "This help message: $0 <help|usage>";
  165. }
  166. # parse arguments, different according to SMF or not.
  167. case $1 in
  168. 'help' | 'usage')
  169. usage
  170. exit $SMF_EXIT_OK
  171. ;;
  172. esac
  173. if smf_present ; then
  174. QUAGGA_METHOD="start"
  175. else
  176. QUAGGA_METHOD="$1"
  177. shift;
  178. fi
  179. DAEMON="$1"
  180. # daemon path must be given
  181. if [ "$DAEMON_PATH/$DAEMON" = "/" ]; then
  182. usage
  183. exit $SMF_EXIT_ERR_FATAL
  184. fi
  185. # only bgpd is suitable for running in a non-global zone, at this
  186. # time.
  187. case "${DAEMON}" in
  188. bgpd)
  189. ;;
  190. zebra | ospfd | ospf6d | ripd | ripngd )
  191. quagga_is_globalzone || exit $SMF_EXIT_OK
  192. ;;
  193. *)
  194. usage
  195. exit $SMF_EXIT_ERR_CONFIG;
  196. ;;
  197. esac
  198. # Older Quagga SMF packages pass daemon args on the commandline
  199. # Newer SMF routeadm model uses properties for each argument
  200. # so we must handle that.
  201. if [ smf_present -a -f "$ROUTEADMINCLUDE" ]; then
  202. handle_routeadm_upgrade $DAEMON;
  203. DAEMON_ARGS=`routeadm_daemon_args`;
  204. else
  205. if [ $# -gt 0 ] ; then
  206. shift
  207. DAEMON_ARGS="$@"
  208. fi
  209. fi
  210. upgrade_config "$DAEMON"
  211. CONF_FILE=`get_routeadm_property $SMF_FMRI config_file`
  212. if [ -z "$CONF_FILE" ] ; then
  213. CONF_FILE="@sysconfdir@/${DAEMON}.conf"
  214. fi
  215. if [ ! -f "$CONF_FILE" ] ; then
  216. echo "Could not find config file, $CONF_FILE"
  217. exit $SMF_EXIT_ERR_CONFIG
  218. fi
  219. # we need @quagga_statedir@ to exist, it probably is on tmpfs.
  220. if [ ! -d @quagga_statedir@ ] ; then
  221. mkdir -p @quagga_statedir@
  222. chown @enable_user@:@enable_group@ @quagga_statedir@
  223. chmod 751 @quagga_statedir@
  224. fi
  225. PIDFILE="@quagga_statedir@/${DAEMON}.pid"
  226. start () {
  227. if [ ! -x "$DAEMON_PATH/$DAEMON" ] ; then
  228. echo "Error, could not find daemon, $DAEMON_PATH/$DAEMON"
  229. exit $SMF_EXIT_ERR_FATAL
  230. fi
  231. eval exec $DAEMON_PATH/$DAEMON $DAEMON_ARGS --pid_file ${PIDFILE} &
  232. }
  233. stop_by_pidfile () {
  234. if [ -f "${PIDFILE}" ]; then
  235. /usr/bin/kill -TERM `/usr/bin/cat "${PIDFILE}"`
  236. fi
  237. }
  238. case "$QUAGGA_METHOD" in
  239. 'start')
  240. start
  241. ;;
  242. 'stop')
  243. stop_by_pidfile
  244. ;;
  245. *)
  246. usage
  247. exit $SMF_EXIT_ERR_FATAL
  248. ;;
  249. esac
  250. exit $SMF_EXIT_OK;