KCC: docstring for kcc.graph.undemote_vertex()
[Samba.git] / ctdb / config / ctdbd_wrapper
blobc5fe00451dfe1e188c1bbcd4646c0f0f65bbd9ed
1 #!/bin/sh
3 # ctdbd wrapper - start or stop CTDB
5 usage ()
7 echo "usage: ctdbd_wrapper <pidfile> { start | stop }"
8 exit 1
11 [ $# -eq 2 ] || usage
13 pidfile="$1"
14 action="$2"
16 ############################################################
18 [ -n "$CTDB_BASE" ] || export CTDB_BASE="/etc/ctdb"
20 . "${CTDB_BASE}/functions"
21 loadconfig "ctdb"
23 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
25 ctdbd="${CTDBD:-/usr/sbin/ctdbd}"
27 ############################################################
29 # ctdbd_is_running()
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.
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.
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.
53 ctdbd_is_running ()
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"
62 # Return value of kill is used
63 kill -0 $_pid 2>/dev/null
64 else
65 # Missing/empty PID file
66 return 1
68 else
69 if _pid=$(pgrep -f "${ctdbd}\>") ; then
70 echo $_pid | sed -e 's@ @,@g'
71 return 0
72 else
73 return 1
78 ############################################################
80 build_ctdb_options ()
82 ctdb_options=""
84 maybe_set ()
86 # If the given variable isn't set then do nothing
87 [ -n "$2" ] || return
88 # If a required value for the variable and it doesn't match,
89 # then do nothing
90 [ -z "$3" -o "$3" = "$2" ] || return
92 val="'$2'"
93 case "$1" in
94 --*) sep="=" ;;
95 -*) sep=" " ;;
96 esac
97 # For these options we're only passing a value-less flag.
98 if [ -n "$3" ] ; then
99 val=""
100 sep=""
103 ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
106 if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
107 echo "No recovery lock specified. Starting CTDB without split brain prevention."
109 maybe_set "--reclock" "$CTDB_RECOVERY_LOCK"
111 maybe_set "--pidfile" "$pidfile"
113 # build up ctdb_options variable from optional parameters
114 maybe_set "--logging" "$CTDB_LOGGING"
115 maybe_set "--nlist" "$CTDB_NODES"
116 maybe_set "--socket" "$CTDB_SOCKET"
117 maybe_set "--listen" "$CTDB_NODE_ADDRESS"
118 maybe_set "--public-addresses" "$CTDB_PUBLIC_ADDRESSES"
119 maybe_set "--public-interface" "$CTDB_PUBLIC_INTERFACE"
120 maybe_set "--dbdir" "$CTDB_DBDIR"
121 maybe_set "--dbdir-persistent" "$CTDB_DBDIR_PERSISTENT"
122 maybe_set "--dbdir-state" "$CTDB_DBDIR_STATE"
123 maybe_set "--event-script-dir" "$CTDB_EVENT_SCRIPT_DIR"
124 maybe_set "--transport" "$CTDB_TRANSPORT"
125 maybe_set "-d" "$CTDB_DEBUGLEVEL"
126 maybe_set "--notification-script" "$CTDB_NOTIFY_SCRIPT"
127 maybe_set "--start-as-disabled" "$CTDB_START_AS_DISABLED" "yes"
128 maybe_set "--start-as-stopped " "$CTDB_START_AS_STOPPED" "yes"
129 maybe_set "--no-recmaster" "$CTDB_CAPABILITY_RECMASTER" "no"
130 maybe_set "--no-lmaster" "$CTDB_CAPABILITY_LMASTER" "no"
131 maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
132 maybe_set "--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
133 maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
136 export_debug_variables ()
138 [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
139 [ -n "$CTDB_EXTERNAL_TRACE" ] && export CTDB_EXTERNAL_TRACE
140 [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
143 kill_ctdbd ()
145 _session="$1"
147 if [ -n "$_session" ] ; then
148 pkill -9 -s "$_session" 2>/dev/null
150 rm -f "$pidfile"
153 ############################################################
155 start()
157 if _session=$(ctdbd_is_running) ; then
158 echo "CTDB is already running"
159 return 0
162 # About to start new $ctdbd. The main daemon is not running but
163 # there may still be other processes around, so do some cleanup.
164 # Note that starting ctdbd below will destroy the Unix domain
165 # socket, so any processes that aren't yet completely useless soon
166 # will be, so this can really do no harm.
167 kill_ctdbd "$_session"
169 build_ctdb_options
171 export_debug_variables
173 if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
174 ulimit -c 0
175 else
176 ulimit -c unlimited
179 if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
180 ulimit -n $CTDB_MAX_OPEN_FILES
183 mkdir -p $(dirname "$pidfile")
185 if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
186 if [ "$CTDB_VALGRIND" = "yes" ] ; then
187 ctdbd="valgrind -q --log-file=/var/log/ctdb_valgrind ${ctdbd}"
188 else
189 ctdbd="${CTDB_VALGRIND} ${ctdbd}"
191 ctdb_options="${ctdb_options} --valgrinding"
194 case "$CTDB_LOGGING" in
195 syslog:udp|syslog:udp-rfc5424)
196 logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}. If nothing is logged then check your syslogd configuration"
198 syslog|syslog:*) : ;;
199 file:*)
200 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in ${CTDB_LOGGING#file:}"
203 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in log.ctdb"
204 esac
206 eval "$ctdbd" "$ctdb_options" || return 1
208 # Wait until ctdbd has started and is ready to respond to clients.
209 _pid=""
210 _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
211 _count=0
212 while [ $_count -lt $_timeout ] ; do
213 # If we don't have the PID then try to read it.
214 [ -n "$_pid" ] || read _pid 2>/dev/null <"$pidfile"
216 # If we got the PID but the PID file has gone or the process
217 # is no longer running then stop waiting... CTDB is dead.
218 if [ -n "$_pid" ] ; then
219 if [ ! -e "$pidfile" ] || ! kill -0 "$_pid" 2>/dev/null ; then
220 echo "CTDB exited during initialisation - check logs."
221 kill_ctdbd "$_pid"
222 drop_all_public_ips >/dev/null 2>&1
223 return 1
226 if ctdb runstate first_recovery startup running >/dev/null 2>&1 ; then
227 return 0
231 _count=$(($_count + 1))
232 sleep 1
233 done
235 echo "Timed out waiting for initialisation - check logs - killing CTDB"
236 kill_ctdbd "$_pid"
237 drop_all_public_ips >/dev/null 2>&1
238 return 1
241 stop()
243 if ! _session=$(ctdbd_is_running) ; then
244 echo "CTDB is not running"
245 return 0
248 ctdb shutdown
250 # Wait for remaining CTDB processes to exit...
251 _timeout=${CTDB_SHUTDOWN_TIMEOUT:-30}
252 _count=0
253 while [ $_count -lt $_timeout ] ; do
254 pkill -0 -s "$_session" 2>/dev/null || return 0
256 _count=$(($_count + 1))
257 sleep 1
258 done
260 echo "Timed out waiting for CTDB to shutdown. Killing CTDB processes."
261 kill_ctdbd "$_session"
262 drop_all_public_ips >/dev/null 2>&1
264 sleep 1
266 if pkill -0 -s "$_session" ; then
267 # If SIGKILL didn't work then things are bad...
268 echo "Failed to kill all CTDB processes. Giving up."
269 return 1
272 return 0
275 ############################################################
277 # Allow notifications for start/stop.
278 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
279 "$CTDB_BASE/rc.ctdb" "$action"
282 case "$action" in
283 start) start ;;
284 stop) stop ;;
286 echo "usage: $0 {start|stop}"
287 exit 1
288 esac