initscript.in 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. # Ignore instances that have been removed
  85. for FILE in `/bin/ls -d $instbase/slapd-* | sed -n '/\.removed$/!p' 2>/dev/null`; do
  86. if [ -d "$FILE" ] ; then
  87. inst=`echo "$FILE" | sed -e "s|$instbase/slapd-||"`
  88. INSTANCES="$INSTANCES $inst"
  89. fi
  90. done
  91. if [ -n "$2" ]; then
  92. for I in $INSTANCES; do
  93. if [ "$2" = "$I" ]; then
  94. INSTANCES="$2"
  95. fi
  96. done
  97. if [ "$2" != "$INSTANCES" ]; then
  98. echo_n "$2 is an invalid @package_name@ instance"
  99. failure; echo
  100. exit 1
  101. fi
  102. fi
  103. start() {
  104. if [ -n "$INSTANCES" ]; then
  105. LD_LIBRARY_PATH=@libdir@/@package_name@:@nss_libdir@
  106. export LD_LIBRARY_PATH
  107. echo "Starting $prog: "
  108. # Start every slapd instance that isn't already running
  109. errors=0
  110. successes=0
  111. for instance in $INSTANCES; do
  112. echo_n " $instance..."
  113. # the server creates pidfile and writes the pid to it when it is fully
  114. # started and available to serve clients
  115. pidfile=$piddir/slapd-$instance.pid
  116. # the server creates startpidfile and writes the pid to just after
  117. # the process begins i.e. it received the startup request and didn't
  118. # die a horrible death (e.g. shared lib problem, oom, etc.)
  119. startpidfile=$piddir/slapd-$instance.startpid
  120. server_running=0
  121. if [ -f $pidfile ]; then
  122. pid=`cat $pidfile`
  123. if kill -0 $pid > /dev/null 2>&1 ; then
  124. echo_n " already running"
  125. success; echo
  126. successes=`expr $successes + 1`
  127. server_running=1
  128. else
  129. echo " not running, but pid file exists"
  130. echo_n " $instance... attempting to start anyway"
  131. rm -f $pidfile
  132. fi
  133. fi
  134. server_started=0
  135. if [ $server_running -eq 0 ] ; then
  136. rm -f $pidfile
  137. rm -f $startpidfile
  138. fix_pid_dir_ownership $instbase/slapd-$instance
  139. # start the directory server in a subshell so that the instance specific
  140. # init config environment will not apply to any other instance
  141. (
  142. [ -f @initconfigdir@/@package_name@-$instance ] && . @initconfigdir@/@package_name@-$instance
  143. $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
  144. )
  145. if [ $? -eq 0 ]; then
  146. server_started=1 # well, perhaps not running, but started ok
  147. else
  148. failure; echo
  149. errors=`expr $errors + 1`
  150. fi
  151. fi
  152. # ok, if we started the server successfully, let's see if it is really
  153. # running and ready to serve requests
  154. if [ $server_started -eq 1 ] ; then
  155. loop_counter=1
  156. # wait for 10 seconds for the start pid file to appear
  157. max_count=10
  158. while test $loop_counter -le $max_count; do
  159. loop_counter=`expr $loop_counter + 1`
  160. if test ! -f $startpidfile ; then
  161. sleep 1
  162. else
  163. pid=`cat $startpidfile`
  164. fi
  165. done
  166. if test ! -f $startpidfile ; then
  167. failure; echo
  168. errors=`expr $errors + 1`
  169. server_started=0
  170. fi
  171. fi
  172. # ok, server wrote the startpid file - let's see if it comes up
  173. # ready to service requests
  174. if [ $server_started -eq 1 ] ; then
  175. loop_counter=1
  176. # wait for 10 minutes (600 times 1 seconds)
  177. max_count=600
  178. while test $loop_counter -le $max_count ; do
  179. loop_counter=`expr $loop_counter + 1`
  180. if test ! -f $pidfile ; then
  181. if kill -0 $pid > /dev/null 2>&1 ; then
  182. sleep 1
  183. else
  184. break
  185. fi
  186. else
  187. pid=`cat $pidfile`
  188. break
  189. fi
  190. done
  191. if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
  192. success; echo
  193. successes=`expr $successes + 1`
  194. else
  195. failure; echo
  196. errors=`expr $errors + 1`
  197. fi
  198. fi
  199. rm -f $startpidfile
  200. done
  201. if [ $successes -ge 1 ]; then
  202. touch $lockfile
  203. fi
  204. if [ $errors -ge 1 ]; then
  205. echo " *** Warning: $errors instance(s) failed to start"
  206. fi
  207. else
  208. echo " *** Error: no $prog instances configured"
  209. fi
  210. }
  211. stop() {
  212. echo "Shutting down $prog: "
  213. errors=0
  214. for instance in $INSTANCES; do
  215. echo_n " $instance..."
  216. pidfile=$piddir/slapd-$instance.pid
  217. if [ -f $pidfile ]; then
  218. pid=`cat $pidfile`
  219. server_stopped=0
  220. if kill -0 $pid > /dev/null 2>&1 ; then
  221. kill $pid
  222. if [ $? -eq 0 ]; then
  223. server_stopped=1
  224. else
  225. failure; echo
  226. errors=`expr $errors + 1`
  227. fi
  228. else
  229. echo_n " server not running"
  230. failure; echo
  231. errors=`expr $errors + 1`
  232. fi
  233. if [ $server_stopped -eq 1 ] ; then
  234. loop_counter=1
  235. # wait for 10 minutes (600 times 1 second)
  236. max_count=600
  237. while test $loop_counter -le $max_count; do
  238. loop_counter=`expr $loop_counter + 1`
  239. if kill -0 $pid > /dev/null 2>&1 ; then
  240. sleep 1
  241. else
  242. if test -f $pidfile ; then
  243. rm -f $pidfile
  244. fi
  245. break
  246. fi
  247. done
  248. if test -f $pidfile ; then
  249. failure; echo
  250. errors=`expr $errors + 1`
  251. else
  252. success; echo
  253. rm -f $pidfile
  254. fi
  255. fi
  256. else
  257. echo_n " server already stopped"
  258. failure; echo
  259. errors=`expr $errors + 1`
  260. fi
  261. done
  262. if [ $errors -ge 1 ]; then
  263. echo_n " *** Error: $errors instance(s) unsuccessfully stopped"
  264. failure; echo
  265. else
  266. rm -f $lockfile
  267. fi
  268. }
  269. restart() {
  270. stop
  271. start
  272. }
  273. status() {
  274. ret=0
  275. for instance in $INSTANCES; do
  276. if [ -f $piddir/slapd-$instance.pid ]; then
  277. pid=`cat $piddir/slapd-$instance.pid`
  278. if kill -0 $pid > /dev/null 2>&1 ; then
  279. echo "$prog $instance (pid $pid) is running..."
  280. else
  281. echo "$prog $instance dead but pid file exists"
  282. ret=1
  283. fi
  284. else
  285. echo "$prog $instance is stopped"
  286. ret=3
  287. fi
  288. done
  289. }
  290. case "$1" in
  291. start|stop|restart|reload|status)
  292. $1
  293. ;;
  294. condrestart)
  295. [ ! -f $lockfile ] || restart
  296. ;;
  297. *)
  298. echo Unknown command $1
  299. echo "Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
  300. exit 2
  301. esac