1
0

initscript.in 10 KB

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