ctdb-scripts: Drop CTDB_PIDFILE configuration option
[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 { start | stop }"
8     exit 1
9 }
10
11 [ $# -eq 1 ] || usage
12
13 action="$1"
14
15 ############################################################
16
17 if [ -z "$CTDB_BASE" ] ; then
18     export CTDB_BASE="/usr/local/etc/ctdb"
19 fi
20
21 . "${CTDB_BASE}/functions"
22 loadconfig "ctdb"
23
24 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
25
26 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
27
28 ############################################################
29
30 # If necessary, mount volatile database directory on tmpfs
31 dbdir_tmpfs_start ()
32 {
33     if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
34         return
35     fi
36
37     # Shortcut for readability
38     _opts="$CTDB_DBDIR_TMPFS_OPTIONS"
39
40     mkdir -p "$CTDB_DBDIR" || exit $?
41
42     # If already mounted then remount, otherwise mount
43     if findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
44         mount -t tmpfs -o "remount,$_opts" none "$CTDB_DBDIR" || \
45             exit $?
46     else
47         mount -t tmpfs -o "$_opts" none "$CTDB_DBDIR" || exit $?
48     fi
49 }
50
51 # If necessary, unmount volatile database tmpfs directory on exit
52 dbdir_tmpfs_stop ()
53 {
54     if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
55         return
56     fi
57
58     if [ -d "$CTDB_DBDIR" ] && findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
59         umount "$CTDB_DBDIR"
60     fi
61 }
62
63 # Only the nested function references its arguments
64 # shellcheck disable=SC2120
65 build_ctdb_options ()
66 {
67     ctdb_options=""
68
69     maybe_set ()
70     {
71         # If the given variable isn't set then do nothing
72         [ -n "$2" ] || return
73         # If a required value for the variable and it doesn't match,
74         # then do nothing
75         [ -z "$3" -o "$3" = "$2" ] || return
76
77         val="'$2'"
78         case "$1" in
79             --*) sep="=" ;;
80             -*)  sep=" " ;;
81         esac
82         # For these options we're only passing a value-less flag.
83         if [ -n "$3" ] ; then
84             val=""
85             sep=""
86         fi
87
88         ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
89     }
90
91     if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
92         echo "No recovery lock specified. Starting CTDB without split brain prevention."
93     fi
94     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
95
96     # build up ctdb_options variable from optional parameters
97     maybe_set "--logging"                "$CTDB_LOGGING"
98     maybe_set "--nlist"                  "$CTDB_NODES"
99     maybe_set "--socket"                 "$CTDB_SOCKET"
100     maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
101     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
102     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
103     maybe_set "--dbdir"                  "$CTDB_DBDIR"
104     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
105     maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
106     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
107     maybe_set "--transport"              "$CTDB_TRANSPORT"
108     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
109     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
110     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
111     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
112     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
113     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
114     maybe_set "--nosetsched"             "$CTDB_NOSETSCHED"           "yes"
115     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
116     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
117 }
118
119 export_debug_variables ()
120 {
121     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
122     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
123 }
124
125 ############################################################
126
127 start()
128 {
129     dbdir_tmpfs_start
130
131     # build_ctdb_options() takes no arguments
132     # shellcheck disable=SC2119
133     build_ctdb_options
134
135     export_debug_variables
136
137     # Explicitly trying to disable core files, no other way
138     # shellcheck disable=SC2039
139     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
140         ulimit -c 0
141     else
142         ulimit -c unlimited
143     fi
144
145     # Unsupported option easily avoided by not using configuration variable
146     # shellcheck disable=SC2039
147     if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
148         ulimit -n "$CTDB_MAX_OPEN_FILES"
149     fi
150
151     if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
152         if [ "$CTDB_VALGRIND" = "yes" ] ; then
153             ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
154         else
155             ctdbd="${CTDB_VALGRIND} ${ctdbd}"
156         fi
157         ctdb_options="${ctdb_options} --valgrinding"
158     fi
159
160     case "$CTDB_LOGGING" in
161         syslog:udp|syslog:udp-rfc5424)
162             logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}.  If nothing is logged then check your syslogd configuration"
163             ;;
164         syslog|syslog:*) : ;;
165         file:*)
166             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in ${CTDB_LOGGING#file:}"
167             ;;
168         *)
169             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in log.ctdb"
170     esac
171
172     eval "$ctdbd" "$ctdb_options" || return 1
173
174     # Wait until ctdbd has started and is ready to respond to clients.
175     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
176     _count=0
177     while [ "$_count" -lt "$_timeout" ] ; do
178         if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
179             return 0
180         fi
181
182         _count=$((_count + 1))
183         sleep 1
184     done
185
186     echo "Timed out waiting for initialisation - check logs"
187     # Attempt a shutdown just in case things are still running
188     $CTDB shutdown >/dev/null 2>&1
189     drop_all_public_ips >/dev/null 2>&1
190     return 1
191 }
192
193 stop()
194 {
195         $CTDB shutdown
196
197         # The above command is important and needs to stand out, so
198         # post-check exit status
199         # shellcheck disable=SC2181
200         if [ $? -ne 0 ] ; then
201                 echo "Error while shutting down CTDB"
202                 drop_all_public_ips >/dev/null 2>&1
203                 return 1
204         fi
205
206         dbdir_tmpfs_stop
207
208         return 0
209 }
210
211 ############################################################
212
213 # Allow notifications for start/stop.
214 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
215     "$CTDB_BASE/rc.ctdb" "$action"
216 fi
217
218 case "$action" in
219     start) start ;;
220     stop)  stop  ;;
221     *)
222         echo "usage: $0 {start|stop}"
223         exit 1
224 esac