initscript.in 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/bin/sh
  2. #
  3. # @package_name@ This starts and stops @package_name@
  4. #
  5. # chkconfig: - 21 79
  6. # description: @capbrand@ Directory Server
  7. # processname: @sbindir@/ns-slapd
  8. # configdir: @sysconfdir@/@package_name@/
  9. # piddir: @localstatedir@/run/@package_name@
  10. # datadir: @localstatedir@/lib/@package_name@/slapd-<instance name>
  11. #
  12. # Source function library.
  13. if [ -f /etc/rc.d/init.d/functions ] ; then
  14. . /etc/rc.d/init.d/functions
  15. fi
  16. # Source networking configuration.
  17. if [ -f /etc/sysconfig/network ] ; then
  18. . /etc/sysconfig/network
  19. fi
  20. # Check that networking is up.
  21. if [ "${NETWORKING}" = "no" ]
  22. then
  23. echo "Networking is down"
  24. exit 0
  25. fi
  26. # Solaris echo cannot use -n - linux echo by default cannot use \c
  27. echo_n()
  28. {
  29. echo @ECHO_N@ "$*@ECHO_C@"
  30. }
  31. # failure and success are not defined on some platforms
  32. type failure > /dev/null 2>&1 || {
  33. failure()
  34. {
  35. echo_n " FAILED"
  36. }
  37. }
  38. type success > /dev/null 2>&1 || {
  39. success()
  40. {
  41. echo_n " SUCCESS"
  42. }
  43. }
  44. # On Solaris /var/run is in tmpfs and gets wiped out upon reboot
  45. # we have to recreate the /var/run/@package_name@ directory
  46. # We also have to make sure that the directory is writable
  47. # by the directory server process
  48. # the argument to this function is the server instance directory,
  49. # which must have a dse.ldif file in it
  50. fix_pid_dir_ownership()
  51. {
  52. if [ ! -d $piddir ] ; then
  53. mkdir -p $piddir
  54. owner=`grep \^nsslapd-localuser $1/dse.ldif | awk '{print $2}'`
  55. if [ -n "$owner" ] ; then
  56. chown $owner $piddir
  57. chmod 700 $piddir
  58. fi
  59. fi
  60. }
  61. baseexec="ns-slapd"
  62. exec="@sbindir@/$baseexec"
  63. prog="@package_name@"
  64. # Lockfile
  65. if [ -d "@localstatedir@/lock/subsys" ] ; then
  66. lockfile="@localstatedir@/lock/subsys/@package_name@"
  67. else
  68. lockfile="@localstatedir@/lock/@package_name@/lock"
  69. fi
  70. # PID directory
  71. piddir="@localstatedir@/run/@package_name@"
  72. # Instance basedir
  73. instbase="@instconfigdir@"
  74. [ -f $exec ] || exit 0
  75. umask 077
  76. [ -f @initconfigdir@/@package_name@ ] && . @initconfigdir@/@package_name@
  77. INSTANCES=""
  78. for FILE in `/bin/ls -d $instbase/slapd-* 2>/dev/null`; do
  79. if [ -d "$FILE" ] ; then
  80. inst=`echo "$FILE" | sed -e "s|$instbase/slapd-||"`
  81. INSTANCES="$INSTANCES $inst"
  82. fi
  83. done
  84. if [ -n "$2" ]; then
  85. for I in $INSTANCES; do
  86. if [ "$2" = "$I" ]; then
  87. INSTANCES="$2"
  88. fi
  89. done
  90. if [ "$2" != "$INSTANCES" ]; then
  91. echo_n "$2 is an invalid @package_name@ instance"
  92. failure; echo
  93. exit 1
  94. fi
  95. fi
  96. start() {
  97. if [ -n "$INSTANCES" ]; then
  98. LD_LIBRARY_PATH=@libdir@/@package_name@:@nss_libdir@
  99. export LD_LIBRARY_PATH
  100. echo "Starting $prog: "
  101. # Start every slapd instance that isn't already running
  102. errors=0
  103. successes=0
  104. for instance in $INSTANCES; do
  105. echo_n " $instance..."
  106. # the server creates pidfile and writes the pid to it when it is fully
  107. # started and available to serve clients
  108. pidfile=$piddir/slapd-$instance.pid
  109. # the server creates startpidfile and writes the pid to just after
  110. # the process begins i.e. it received the startup request and didn't
  111. # die a horrible death (e.g. shared lib problem, oom, etc.)
  112. startpidfile=$piddir/slapd-$instance.startpid
  113. server_running=0
  114. if [ -f $pidfile ]; then
  115. pid=`cat $pidfile`
  116. if kill -0 $pid > /dev/null 2>&1 ; then
  117. echo_n " already running"
  118. success; echo
  119. successes=`expr $successes + 1`
  120. server_running=1
  121. else
  122. echo_n " not running, but pid file exists - attempt to start anyway..."
  123. rm -f $pidfile
  124. fi
  125. fi
  126. server_started=0
  127. if [ $server_running -eq 0 ] ; then
  128. rm -f $pidfile
  129. rm -f $startpidfile
  130. fix_pid_dir_ownership $instbase/slapd-$instance
  131. # start the directory server in a subshell so that the instance specific
  132. # init config environment will not apply to any other instance
  133. (
  134. [ -f @initconfigdir@/@package_name@-$instance ] && . @initconfigdir@/@package_name@-$instance
  135. $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
  136. )
  137. if [ $? -eq 0 ]; then
  138. server_started=1 # well, perhaps not running, but started ok
  139. else
  140. failure; echo
  141. errors=`expr $errors + 1`
  142. fi
  143. fi
  144. # ok, if we started the server successfully, let's see if it is really
  145. # running and ready to serve requests
  146. if [ $server_started -eq 1 ] ; then
  147. loop_counter=1
  148. # wait for 10 seconds for the start pid file to appear
  149. max_count=10
  150. while test $loop_counter -le $max_count; do
  151. loop_counter=`expr $loop_counter + 1`
  152. if test ! -f $startpidfile ; then
  153. sleep 1
  154. else
  155. pid=`cat $startpidfile`
  156. fi
  157. done
  158. if test ! -f $startpidfile ; then
  159. failure; echo
  160. errors=`expr $errors + 1`
  161. server_started=0
  162. fi
  163. fi
  164. # ok, server wrote the startpid file - let's see if it comes up
  165. # ready to service requests
  166. if [ $server_started -eq 1 ] ; then
  167. loop_counter=1
  168. # wait for 10 minutes (600 times 1 seconds)
  169. max_count=600
  170. while test $loop_counter -le $max_count ; do
  171. loop_counter=`expr $loop_counter + 1`
  172. if test ! -f $pidfile ; then
  173. if kill -0 $pid > /dev/null 2>&1 ; then
  174. sleep 1
  175. else
  176. break
  177. fi
  178. else
  179. pid=`cat $pidfile`
  180. break
  181. fi
  182. done
  183. if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
  184. success; echo
  185. successes=`expr $successes + 1`
  186. else
  187. failure; echo
  188. errors=`expr $errors + 1`
  189. fi
  190. fi
  191. rm -f $startpidfile
  192. done
  193. if [ $successes -ge 1 ]; then
  194. touch $lockfile
  195. fi
  196. if [ $errors -ge 1 ]; then
  197. echo "*** Warning: $errors instance(s) failed to start"
  198. fi
  199. else
  200. echo "*** Error: no $prog instances configured"
  201. fi
  202. }
  203. stop() {
  204. echo "Shutting down $prog: "
  205. errors=0
  206. for instance in $INSTANCES; do
  207. pidfile=$piddir/slapd-$instance.pid
  208. if [ -f $pidfile ]; then
  209. pid=`cat $pidfile`
  210. echo_n " $instance..."
  211. server_stopped=0
  212. if kill -0 $pid > /dev/null 2>&1 ; then
  213. kill $pid
  214. if [ $? -eq 0 ]; then
  215. server_stopped=1
  216. else
  217. failure; echo
  218. errors=`expr $errors + 1`
  219. fi
  220. fi
  221. if [ $server_stopped -eq 1 ] ; then
  222. loop_counter=1
  223. # wait for 10 minutes (600 times 1 second)
  224. max_count=600
  225. while test $loop_counter -le $max_count; do
  226. loop_counter=`expr $loop_counter + 1`
  227. if kill -0 $pid > /dev/null 2>&1 ; then
  228. sleep 1
  229. else
  230. if test -f $pidfile ; then
  231. rm -f $pidfile
  232. fi
  233. break
  234. fi
  235. done
  236. if test -f $pidfile ; then
  237. failure; echo
  238. errors=`expr $errors + 1`
  239. else
  240. success; echo
  241. rm -f $pidfile
  242. fi
  243. fi
  244. fi
  245. done
  246. if [ $errors -ge 1 ]; then
  247. echo_n "*** Error: $errors instance(s) unsuccessfully stopped"
  248. failure; echo
  249. else
  250. rm -f $lockfile
  251. fi
  252. }
  253. restart() {
  254. stop
  255. start
  256. }
  257. status() {
  258. for instance in $INSTANCES; do
  259. if [ -f $piddir/slapd-$instance.pid ]; then
  260. pid=`cat $piddir/slapd-$instance.pid`
  261. if kill -0 $pid > /dev/null 2>&1 ; then
  262. echo "$prog $instance (pid $pid) is running..."
  263. else
  264. echo "$prog $instance dead but pid file exists"
  265. fi
  266. else
  267. echo "$prog $instance is stopped"
  268. fi
  269. done
  270. }
  271. case "$1" in
  272. start|stop|restart|reload|status)
  273. $1
  274. ;;
  275. condrestart)
  276. [ ! -f $lockfile ] || restart
  277. ;;
  278. *)
  279. echo Unknown command $1
  280. echo "Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
  281. exit 2
  282. esac