c98c1906eb904a54e994d0c7a7f75b815004372d
[samba.git] / ctdb / config / ctdbd_wrapper
1 #!/bin/sh
2
3 # ctdbd wrapper - start or stop CTDB
4
5 usage ()
6 {
7     echo "usage: ctdbd_wrapper <pidfile> { start | stop }"
8     exit 1
9 }
10
11 [ $# -eq 2 ] || usage
12
13 pidfile="$1"
14 action="$2"
15
16 ############################################################
17
18 if [ -z "$CTDB_BASE" ] ; then
19     export CTDB_BASE="/usr/local/etc/ctdb"
20 fi
21
22 . "${CTDB_BASE}/functions"
23 loadconfig "ctdb"
24
25 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
26
27 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
28
29 ############################################################
30
31 # ctdbd_is_running()
32
33 # 1. Check if ctdbd is running.
34 #    - If the PID file is being used then, if the PID file is present,
35 #      ctdbd is only considered to running if the PID in the file is
36 #      active.
37 #    - If the PID file is not being used (i.e. we're upgrading from a
38 #      version that doesn't support it) then the presence of any ctdbd
39 #      processes is enough proof.
40
41 # 2. Print a comma-separated list of PIDs that can be
42 #    used with "pkill -s".
43 #    - If the PID file is being used then this is just the PID in that
44 #      file.  This also happens to be the session ID, so can be used
45 #      to kill all CTDB processes.
46 #    - If the PID file is not being used (i.e. upgrading) then this is
47 #      just any ctdbd processes that are running.  Hopefully one of
48 #      them is the session ID so that it can be used to kill all CTDB
49 #      processes.
50
51 # Combining these 2 checks is an optimisation to avoid potentially
52 # running too many pgrep/pkill processes on an already loaded system.
53 # Trawling through /proc/ can be very expensive.
54
55 ctdbd_is_running ()
56 {
57     # If the directory for the PID file exists then respect the
58     # existence of a PID file.
59     _pidfile_dir=$(dirname "$pidfile")
60     if [ -d "$_pidfile_dir" ] ; then
61         if read _pid 2>/dev/null <"$pidfile" ; then
62             echo "$_pid"
63
64             # Return value of kill is used
65             kill -0 $_pid 2>/dev/null
66         else
67             # Missing/empty PID file
68             return 1
69         fi
70     else
71         if _pid=$(pgrep -f "${ctdbd}\>") ; then
72             echo $_pid | sed -e 's@ @,@g'
73             return 0
74         else
75             return 1
76         fi
77     fi
78 }
79
80 ############################################################
81
82 build_ctdb_options ()
83 {
84     ctdb_options=""
85
86     maybe_set ()
87     {
88         # If the given variable isn't set then do nothing
89         [ -n "$2" ] || return
90         # If a required value for the variable and it doesn't match,
91         # then do nothing
92         [ -z "$3" -o "$3" = "$2" ] || return
93
94         val="'$2'"
95         case "$1" in
96             --*) sep="=" ;;
97             -*)  sep=" " ;;
98         esac
99         # For these options we're only passing a value-less flag.
100         if [ -n "$3" ] ; then
101             val=""
102             sep=""
103         fi
104
105         ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
106     }
107
108     if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
109         echo "No recovery lock specified. Starting CTDB without split brain prevention."
110     fi
111     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
112
113     maybe_set "--pidfile"                "$pidfile"
114
115     # build up ctdb_options variable from optional parameters
116     maybe_set "--logging"                "$CTDB_LOGGING"
117     maybe_set "--nlist"                  "$CTDB_NODES"
118     maybe_set "--socket"                 "$CTDB_SOCKET"
119     maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
120     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
121     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
122     maybe_set "--dbdir"                  "$CTDB_DBDIR"
123     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
124     maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
125     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
126     maybe_set "--transport"              "$CTDB_TRANSPORT"
127     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
128     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
129     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
130     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
131     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
132     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
133     maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
134     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
135     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
136 }
137
138 export_debug_variables ()
139 {
140     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
141     [ -n "$CTDB_EXTERNAL_TRACE" ] && export CTDB_EXTERNAL_TRACE
142     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
143 }
144
145 kill_ctdbd ()
146 {
147     _session="$1"
148
149     if [ -n "$_session" ] ; then
150         pkill -9 -s "$_session" 2>/dev/null
151         rm -f "$pidfile"
152     fi
153 }
154
155 ############################################################
156
157 start()
158 {
159     if _session=$(ctdbd_is_running) ; then
160         echo "CTDB is already running"
161         return 0
162     fi
163
164     # About to start new $ctdbd.  The main daemon is not running but
165     # there may still be other processes around, so do some cleanup.
166     kill_ctdbd "$_session"
167
168     build_ctdb_options
169
170     export_debug_variables
171
172     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
173         ulimit -c 0
174     else
175         ulimit -c unlimited
176     fi
177
178     if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
179         ulimit -n $CTDB_MAX_OPEN_FILES
180     fi
181
182     mkdir -p $(dirname "$pidfile")
183
184     if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
185         if [ "$CTDB_VALGRIND" = "yes" ] ; then
186             ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
187         else
188             ctdbd="${CTDB_VALGRIND} ${ctdbd}"
189         fi
190         ctdb_options="${ctdb_options} --valgrinding"
191     fi
192
193     case "$CTDB_LOGGING" in
194         syslog:udp|syslog:udp-rfc5424)
195             logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}.  If nothing is logged then check your syslogd configuration"
196             ;;
197         syslog|syslog:*) : ;;
198         file:*)
199             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in ${CTDB_LOGGING#file:}"
200             ;;
201         *)
202             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in log.ctdb"
203     esac
204
205     eval "$ctdbd" "$ctdb_options" || return 1
206
207     # Wait until ctdbd has started and is ready to respond to clients.
208     _pid=""
209     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
210     _count=0
211     while [ $_count -lt $_timeout ] ; do
212         # If we don't have the PID then try to read it.
213         [ -n "$_pid" ] || read _pid 2>/dev/null <"$pidfile"
214
215         # If we got the PID but the PID file has gone or the process
216         # is no longer running then stop waiting... CTDB is dead.
217         if [ -n "$_pid" ] ; then
218             if [ ! -e "$pidfile" ] || ! kill -0 "$_pid" 2>/dev/null ; then
219                 echo "CTDB exited during initialisation - check logs."
220                 kill_ctdbd "$_pid"
221                 drop_all_public_ips >/dev/null 2>&1
222                 return 1
223             fi
224
225             if ctdb runstate first_recovery startup running >/dev/null 2>&1 ; then
226                 return 0
227             fi
228         fi
229
230         _count=$(($_count + 1))
231         sleep 1
232     done
233
234     echo "Timed out waiting for initialisation - check logs - killing CTDB"
235     kill_ctdbd "$_pid"
236     drop_all_public_ips >/dev/null 2>&1
237     return 1
238 }
239
240 stop()
241 {
242     if ! _session=$(ctdbd_is_running) ; then
243         echo "CTDB is not running"
244         return 0
245     fi
246
247     ctdb shutdown
248
249     # Wait for remaining CTDB processes to exit...
250     _timeout=${CTDB_SHUTDOWN_TIMEOUT:-30}
251     _count=0
252     while [ $_count -lt $_timeout ] ; do
253         pkill -0 -s "$_session" 2>/dev/null || return 0
254
255         _count=$(($_count + 1))
256         sleep 1
257     done
258
259     echo "Timed out waiting for CTDB to shutdown.  Killing CTDB processes."
260     kill_ctdbd "$_session"
261     drop_all_public_ips >/dev/null 2>&1
262
263     sleep 1
264
265     if pkill -0 -s "$_session" ; then
266         # If SIGKILL didn't work then things are bad...
267         echo "Failed to kill all CTDB processes.  Giving up."
268         return 1
269     fi
270
271     return 0
272 }
273
274 ############################################################
275
276 # Allow notifications for start/stop.
277 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
278     "$CTDB_BASE/rc.ctdb" "$action"
279 fi
280
281 case "$action" in
282     start) start ;;
283     stop)  stop  ;;
284     *)
285         echo "usage: $0 {start|stop}"
286         exit 1
287 esac