initscript.in 9.4 KB

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