initscript.in 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. instlockfile="@localstatedir@/lock/@package_name@/slapd-$instance/server/$pid"
  124. name=`ps -p $pid | tail -1 | awk '{ print $4 }'`
  125. if kill -0 $pid && [ $name = "ns-slapd" ]; then
  126. echo_n " already running"
  127. success; echo
  128. successes=`expr $successes + 1`
  129. server_running=1
  130. else
  131. echo " not running, but pid file exists"
  132. echo_n " $instance... attempting to start anyway"
  133. # Clean up the instance lockfile since we know the pid. The
  134. # pidfiles will get cleaned up below.
  135. rm -f $instlockfile
  136. fi
  137. fi
  138. server_started=0
  139. if [ $server_running -eq 0 ] ; then
  140. rm -f $pidfile
  141. rm -f $startpidfile
  142. fix_pid_dir_ownership $instbase/slapd-$instance
  143. # start the directory server in a subshell so that the instance specific
  144. # init config environment will not apply to any other instance
  145. (
  146. umask 002 # reset umask to allow logs and other files modes to be explicitly set
  147. [ -f @initconfigdir@/@package_name@-$instance ] && . @initconfigdir@/@package_name@-$instance
  148. $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
  149. )
  150. if [ $? -eq 0 ]; then
  151. server_started=1 # well, perhaps not running, but started ok
  152. else
  153. failure; echo
  154. errors=`expr $errors + 1`
  155. fi
  156. fi
  157. # ok, if we started the server successfully, let's see if it is really
  158. # running and ready to serve requests
  159. if [ $server_started -eq 1 ] ; then
  160. loop_counter=1
  161. # wait for 10 seconds for the start pid file to appear
  162. max_count=10
  163. while test $loop_counter -le $max_count; do
  164. loop_counter=`expr $loop_counter + 1`
  165. if test ! -f $startpidfile ; then
  166. sleep 1
  167. else
  168. pid=`cat $startpidfile`
  169. fi
  170. done
  171. if test ! -f $startpidfile ; then
  172. failure; echo
  173. errors=`expr $errors + 1`
  174. server_started=0
  175. fi
  176. fi
  177. # ok, server wrote the startpid file - let's see if it comes up
  178. # ready to service requests
  179. if [ $server_started -eq 1 ] ; then
  180. loop_counter=1
  181. # wait for 10 minutes (600 times 1 seconds)
  182. max_count=600
  183. while test $loop_counter -le $max_count ; do
  184. loop_counter=`expr $loop_counter + 1`
  185. if test ! -f $pidfile ; then
  186. if kill -0 $pid > /dev/null 2>&1 ; then
  187. sleep 1
  188. else
  189. break
  190. fi
  191. else
  192. pid=`cat $pidfile`
  193. break
  194. fi
  195. done
  196. if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
  197. success; echo
  198. successes=`expr $successes + 1`
  199. else
  200. failure; echo
  201. errors=`expr $errors + 1`
  202. fi
  203. fi
  204. rm -f $startpidfile
  205. done
  206. if [ $successes -ge 1 ]; then
  207. touch $lockfile
  208. fi
  209. if [ $errors -ge 1 ]; then
  210. echo " *** Warning: $errors instance(s) failed to start"
  211. fi
  212. else
  213. echo " *** Error: no $prog instances configured"
  214. fi
  215. }
  216. stop() {
  217. echo "Shutting down $prog: "
  218. errors=0
  219. for instance in $INSTANCES; do
  220. echo_n " $instance..."
  221. pidfile=$piddir/slapd-$instance.pid
  222. if [ -f $pidfile ]; then
  223. pid=`cat $pidfile`
  224. server_stopped=0
  225. if kill -0 $pid > /dev/null 2>&1 ; then
  226. kill $pid
  227. if [ $? -eq 0 ]; then
  228. server_stopped=1
  229. else
  230. failure; echo
  231. errors=`expr $errors + 1`
  232. fi
  233. else
  234. echo_n " server not running"
  235. failure; echo
  236. errors=`expr $errors + 1`
  237. fi
  238. if [ $server_stopped -eq 1 ] ; then
  239. loop_counter=1
  240. # wait for 10 minutes (600 times 1 second)
  241. max_count=600
  242. while test $loop_counter -le $max_count; do
  243. loop_counter=`expr $loop_counter + 1`
  244. if kill -0 $pid > /dev/null 2>&1 ; then
  245. sleep 1
  246. else
  247. if test -f $pidfile ; then
  248. rm -f $pidfile
  249. fi
  250. break
  251. fi
  252. done
  253. if test -f $pidfile ; then
  254. failure; echo
  255. errors=`expr $errors + 1`
  256. else
  257. success; echo
  258. rm -f $pidfile
  259. fi
  260. fi
  261. else
  262. echo_n " server already stopped"
  263. failure; echo
  264. errors=`expr $errors + 1`
  265. fi
  266. done
  267. if [ $errors -ge 1 ]; then
  268. echo_n " *** Error: $errors instance(s) unsuccessfully stopped"
  269. failure; echo
  270. else
  271. rm -f $lockfile
  272. fi
  273. }
  274. restart() {
  275. stop
  276. start
  277. }
  278. status() {
  279. ret=0
  280. for instance in $INSTANCES; do
  281. if [ -f $piddir/slapd-$instance.pid ]; then
  282. pid=`cat $piddir/slapd-$instance.pid`
  283. if kill -0 $pid > /dev/null 2>&1 ; then
  284. echo "$prog $instance (pid $pid) is running..."
  285. else
  286. echo "$prog $instance dead but pid file exists"
  287. ret=1
  288. fi
  289. else
  290. echo "$prog $instance is stopped"
  291. ret=3
  292. fi
  293. done
  294. exit $ret
  295. }
  296. case "$1" in
  297. start|stop|restart|reload|status)
  298. $1
  299. ;;
  300. condrestart)
  301. [ ! -f $lockfile ] || restart
  302. ;;
  303. *)
  304. echo Unknown command $1
  305. echo "Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
  306. exit 2
  307. esac