summaryrefslogtreecommitdiffstats
path: root/wrappers/initscript.in
blob: f8bef0dc5a7398d93c1dc65d3a46d9ee2bdde3bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/sh
#
# @package_name@    This starts and stops @package_name@
#
# chkconfig:   - 21 79
# description: @package_name@ Directory Server
# processname: @sbindir@/ns-slapd
# configdir:   @sysconfdir@/@package_name@/
# piddir:      @localstatedir@/run/@package_name@
# datadir:     @localstatedir@/lib/@package_name@/slapd-<instance name>
#

# Source function library.
if [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
fi
# Source networking configuration.
if [ -f /etc/sysconfig/network ] ; then
. /etc/sysconfig/network
fi

# Check that networking is up.
if [ "${NETWORKING}" = "no" ]
then
    echo "Networking is down"
    exit 0
fi

# Solaris echo cannot use -n - linux echo by default cannot use \c
echo_n()
{
    echo @ECHO_N@ "$*@ECHO_C@"
}

# failure and success are not defined on some platforms
type failure > /dev/null 2>&1 || {
failure()
{
    echo_n " FAILED"
}
}

type success > /dev/null 2>&1 || {
success()
{
    echo_n " SUCCESS"
}
}

# On Solaris /var/run is in tmpfs and gets wiped out upon reboot
# we have to recreate the /var/run/@package_name@ directory
# We also have to make sure that the directory is writable
# by the directory server process
# the argument to this function is the server instance directory,
# which must have a dse.ldif file in it
fix_pid_dir_ownership()
{
    if [ ! -d $piddir ] ; then
	mkdir -p $piddir
	owner=`grep \^nsslapd-localuser $1/dse.ldif | awk '{print $2}'`
	if [ -n "$owner" ] ; then
	    chown $owner $piddir
	    chmod 700 $piddir
	fi
    fi
}

baseexec="ns-slapd"
exec="@sbindir@/$baseexec"
prog="@package_name@"
# Lockfile
if [ -d "@localstatedir@/lock/subsys" ] ; then
    lockfile="@localstatedir@/lock/subsys/@package_name@"
else
    lockfile="@localstatedir@/lock/@package_name@/lock"
fi
# PID directory
piddir="@localstatedir@/run/@package_name@"

# Instance basedir
instbase="@instconfigdir@"


[ -f $exec ] || exit 0


umask 077

INSTANCES=""

for FILE in `/bin/ls -d $instbase/slapd-* 2>/dev/null`; do
    if [ -d "$FILE" ] ; then
        inst=`echo "$FILE" | sed -e "s|$instbase/slapd-||"`
        INSTANCES="$INSTANCES $inst"
    fi
done

if [ -n "$2" ]; then
   for I in $INSTANCES; do
      if [ "$2" = "$I" ]; then
         INSTANCES="$2"
      fi
   done
   if [ "$2" != "$INSTANCES" ]; then
      echo_n "$2 is an invalid @package_name@ instance"
      failure; echo
      exit 1
   fi
fi

start() {
    if [ -n "$INSTANCES" ]; then
        LD_LIBRARY_PATH=@libdir@/@package_name@:@nss_libdir@
        export LD_LIBRARY_PATH
        echo  "Starting $prog: "
        # Start every slapd instance that isn't already running
        errors=0
        successes=0
        for instance in $INSTANCES; do
            echo_n "    $instance..."
            # the server creates pidfile and writes the pid to it when it is fully
            # started and available to serve clients
            pidfile=$piddir/slapd-$instance.pid
            # the server creates startpidfile and writes the pid to just after
            # the process begins i.e. it received the startup request and didn't
            # die a horrible death (e.g. shared lib problem, oom, etc.)
            startpidfile=$piddir/slapd-$instance.startpid
            server_running=0
            if [ -f $pidfile ]; then
                pid=`cat $pidfile`
                if kill -0 $pid > /dev/null 2>&1 ; then
                    echo_n " already running"
                    success; echo
                    successes=`expr $successes + 1`
                    server_running=1
                else
                    echo_n " not running, but pid file exists - attempt to start anyway..."
                    rm -f $pidfile
                fi
            fi
            server_started=0
            if [ $server_running -eq 0 ] ; then
                rm -f $pidfile
                rm -f $startpidfile
                fix_pid_dir_ownership $instbase/slapd-$instance
                $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
                if [ $? -eq 0 ]; then
                    server_started=1 # well, perhaps not running, but started ok
                else
                    failure; echo
                    errors=`expr $errors + 1`
                fi
            fi
            # ok, if we started the server successfully, let's see if it is really
            # running and ready to serve requests
            if [ $server_started -eq 1 ] ; then
                loop_counter=1
				# wait for 10 seconds for the start pid file to appear
                max_count=10
                while test $loop_counter -le $max_count; do
                    loop_counter=`expr $loop_counter + 1`
                    if test ! -f $startpidfile ; then
                        sleep 1
                    else
                        pid=`cat $startpidfile`
                    fi
                done
                if test ! -f $startpidfile ; then
                    failure; echo
                    errors=`expr $errors + 1`
                    server_started=0
                fi
            fi
            # ok, server wrote the startpid file - let's see if it comes up
            # ready to service requests
            if [ $server_started -eq 1 ] ; then
                loop_counter=1
				# wait for 10 minutes (600 times 1 seconds)
                max_count=600
                while test $loop_counter -le $max_count ; do
                    loop_counter=`expr $loop_counter + 1`
                    if test ! -f $pidfile ; then
                        if kill -0 $pid > /dev/null 2>&1 ; then
                            sleep 1
                        else
                            break
                        fi
                    else
                        pid=`cat $pidfile`
                        break
                    fi
                done
                if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
                    success; echo
                    successes=`expr $successes + 1`
                else
                    failure; echo
                    errors=`expr $errors + 1`
                fi
            fi
            rm -f $startpidfile
        done
        if [ $successes -ge 1 ]; then
            touch $lockfile
        fi
        if [ $errors -ge 1 ]; then
            echo "*** Warning: $errors instance(s) failed to start"
        fi
    else
        echo "*** Error: no $prog instances configured"
    fi
}

stop() {
    echo "Shutting down $prog: "
    errors=0
    for instance in $INSTANCES; do
        pidfile=$piddir/slapd-$instance.pid
        if [ -f $pidfile ]; then
            pid=`cat $pidfile`
            echo_n "    $instance..."
            server_stopped=0
            if kill -0 $pid > /dev/null 2>&1 ; then
                kill $pid
                if [ $? -eq 0 ]; then
                    server_stopped=1
                else
                    failure; echo
                    errors=`expr $errors + 1`
                fi
            fi
            if [ $server_stopped -eq 1 ] ; then
                loop_counter=1
				# wait for 10 minutes (600 times 1 second)
                max_count=600
                while test $loop_counter -le $max_count; do
                    loop_counter=`expr $loop_counter + 1`
                    if kill -0 $pid > /dev/null 2>&1 ; then
                        sleep 1
                    else
                        if test -f $pidfile ; then
                            rm -f $pidfile
                        fi
                        break
                    fi
                done
                if test -f $pidfile ; then
                    failure; echo
                    errors=`expr $errors + 1`
                else
                    success; echo
                    rm -f $pidfile
                fi
            fi
        fi
    done
    if [ $errors -ge 1 ]; then
        echo_n "*** Error: $errors instance(s) unsuccessfully stopped"
        failure; echo
    else
        rm -f $lockfile
    fi
}

restart() {
    stop
    start
}


status() {
     for instance in $INSTANCES; do
         if [ -f $piddir/slapd-$instance.pid ]; then
             pid=`cat $piddir/slapd-$instance.pid`
             if kill -0 $pid > /dev/null 2>&1 ; then
                 echo "$prog $instance (pid $pid) is running..."
             else
                echo "$prog $instance dead but pid file exists"
             fi
         else
             echo "$prog $instance is stopped"
         fi
     done
}


case "$1" in
    start|stop|restart|reload|status)
        $1
        ;;
    condrestart)
        [ ! -f $lockfile ] || restart
        ;;
    *)
        echo Unknown command $1
        echo "Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
        exit 2
esac