initscript.in 11 KB

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