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