ctdb-scripts: Avoid ShellCheck warning SC2162
[gd/samba-autobuild/.git] / ctdb / config / functions
1 # Hey Emacs, this is a -*- shell-script -*- !!!
2
3 # utility functions for ctdb event scripts
4
5 if [ -z "$CTDB_BASE" ]; then
6         echo 'CTDB_BASE unset in CTDB functions file'
7         exit 1
8 fi
9 export CTDB_BASE
10
11 # CTDB_VARDIR is used elsewhere
12 # shellcheck disable=SC2034
13 CTDB_VARDIR="/usr/local/var/lib/ctdb"
14
15 CTDB="${CTDB:-/usr/local/bin/ctdb}"
16
17 # Only (and always) override these variables in test code
18
19 if [ -z "$CTDB_SCRIPT_VARDIR" ]; then
20         CTDB_SCRIPT_VARDIR="/usr/local/var/lib/ctdb/scripts"
21 fi
22
23 if [ -z "$CTDB_SYS_ETCDIR" ]; then
24         CTDB_SYS_ETCDIR="/etc"
25 fi
26
27 if [ -z "$CTDB_HELPER_BINDIR" ]; then
28         CTDB_HELPER_BINDIR="/usr/local/libexec/ctdb"
29 fi
30
31 #######################################
32 # pull in a system config file, if any
33
34 load_system_config()
35 {
36         for _i; do
37
38                 if [ -f "${CTDB_SYS_ETCDIR}/sysconfig/${_i}" ]; then
39                         . "${CTDB_SYS_ETCDIR}/sysconfig/${_i}"
40                         return
41                 elif [ -f "${CTDB_SYS_ETCDIR}/default/${_i}" ]; then
42                         . "${CTDB_SYS_ETCDIR}/default/${_i}"
43                         return
44                 fi
45         done
46 }
47
48 # load_script_options [ component script ]
49 #   script is an event script name relative to a component
50 #   component is currently ignored
51 load_script_options()
52 {
53         if [ $# -eq 2 ]; then
54                 _script="$2"
55         elif [ $# -eq 0 ]; then
56                 _script=""
57         else
58                 die "usage: load_script_options [ component script ]"
59         fi
60
61         _options="${CTDB_BASE}/script.options"
62
63         if [ -r "$_options" ]; then
64                 . "$_options"
65         fi
66
67         if [ -n "$_script" ]; then
68                 _s="${CTDB_BASE}/events/legacy/${_script}"
69         else
70                 _s="${0%.script}"
71         fi
72         _options="${_s}.options"
73
74         if [ -r "$_options" ]; then
75                 . "$_options"
76         fi
77 }
78
79 ##############################################################
80
81 die()
82 {
83         _msg="$1"
84         _rc="${2:-1}"
85
86         echo "$_msg" >&2
87         exit "$_rc"
88 }
89
90 # Log given message or stdin to either syslog or a CTDB log file
91 # $1 is the tag passed to logger if syslog is in use.
92 script_log()
93 {
94         _tag="$1"
95         shift
96
97         case "$CTDB_LOGGING" in
98         file:* | "")
99                 if [ -n "$CTDB_LOGGING" ]; then
100                         _file="${CTDB_LOGGING#file:}"
101                 else
102                         _file="/usr/local/var/log/log.ctdb"
103                 fi
104                 {
105                         if [ -n "$*" ]; then
106                                 echo "$*"
107                         else
108                                 cat
109                         fi
110                 } >>"$_file"
111                 ;;
112         *)
113                 # Handle all syslog:* variants here too.  There's no tool to do
114                 # the lossy things, so just use logger.
115                 logger -t "ctdbd: ${_tag}" "$@"
116                 ;;
117         esac
118 }
119
120 # When things are run in the background in an eventscript then logging
121 # output might get lost.  This is the "solution".  :-)
122 background_with_logging()
123 {
124         (
125                 "$@" 2>&1 </dev/null |
126                         script_log "${script_name}&"
127         ) &
128
129         return 0
130 }
131
132 ##############################################################
133 # check number of args for different events
134 ctdb_check_args()
135 {
136         case "$1" in
137         takeip | releaseip)
138                 if [ $# != 4 ]; then
139                         echo "ERROR: must supply interface, IP and maskbits"
140                         exit 1
141                 fi
142                 ;;
143         updateip)
144                 if [ $# != 5 ]; then
145                         echo "ERROR: must supply old interface, new interface, IP and maskbits"
146                         exit 1
147                 fi
148                 ;;
149         esac
150 }
151
152 ##############################################################
153 # determine on what type of system (init style) we are running
154 detect_init_style()
155 {
156         # only do detection if not already set:
157         if [ -n "$CTDB_INIT_STYLE" ]; then
158                 return
159         fi
160
161         if [ -x /sbin/startproc ]; then
162                 CTDB_INIT_STYLE="suse"
163         elif [ -x /sbin/start-stop-daemon ]; then
164                 CTDB_INIT_STYLE="debian"
165         else
166                 CTDB_INIT_STYLE="redhat"
167         fi
168 }
169
170 ######################################################
171 # simulate /sbin/service on platforms that don't have it
172 # _service() makes it easier to hook the service() function for
173 # testing.
174 _service()
175 {
176         _service_name="$1"
177         _op="$2"
178
179         # do nothing, when no service was specified
180         [ -z "$_service_name" ] && return
181
182         if [ -x /sbin/service ]; then
183                 $_nice /sbin/service "$_service_name" "$_op"
184         elif [ -x /usr/sbin/service ]; then
185                 $_nice /usr/sbin/service "$_service_name" "$_op"
186         elif [ -x /bin/systemctl ]; then
187                 $_nice /bin/systemctl "$_op" "$_service_name"
188         elif [ -x "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" ]; then
189                 $_nice "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" "$_op"
190         elif [ -x "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" ]; then
191                 $_nice "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" "$_op"
192         fi
193 }
194
195 service()
196 {
197         _nice=""
198         _service "$@"
199 }
200
201 ######################################################
202 # simulate /sbin/service (niced) on platforms that don't have it
203 nice_service()
204 {
205         _nice="nice"
206         _service "$@"
207 }
208
209 ######################################################
210 # Cached retrieval of PNN from local node.  This never changes so why
211 # open a client connection to the server each time this is needed?
212 ctdb_get_pnn()
213 {
214         _pnn_file="${CTDB_SCRIPT_VARDIR}/my-pnn"
215         if [ ! -f "$_pnn_file" ]; then
216                 $CTDB pnn >"$_pnn_file"
217         fi
218
219         cat "$_pnn_file"
220 }
221
222 # Cached retrieval of private IP address from local node.  This never
223 # changes.
224 ctdb_get_ip_address()
225 {
226         _ip_addr_file="${CTDB_SCRIPT_VARDIR}/my-ip-address"
227         if [ ! -f "$_ip_addr_file" ]; then
228                 $CTDB -X nodestatus |
229                         awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file"
230         fi
231
232         cat "$_ip_addr_file"
233 }
234
235 # Cached retrieval of database options for use by event scripts.
236 #
237 # If the variables are already set then they should not be overwritten
238 # - this should only happen during event script testing.
239 ctdb_get_db_options()
240 {
241         _db_opts_file="${CTDB_SCRIPT_VARDIR}/db_options.cache"
242
243         if [ ! -f "$_db_opts_file" ]; then
244                 {
245                         ctdb_translate_option "database" \
246                                 "volatile database directory" \
247                                 "CTDB_DBDIR"
248                         ctdb_translate_option "database" \
249                                 "persistent database directory" \
250                                 "CTDB_DBDIR_PERSISTENT"
251                         ctdb_translate_option "database" \
252                                 "state database directory" \
253                                 "CTDB_DBDIR_STATE"
254                 } >"$_db_opts_file"
255         fi
256
257         . "$_db_opts_file"
258 }
259
260 ctdb_translate_option()
261 {
262         _section="$1"
263         _opt="$2"
264         _variable="$3"
265
266         # ctdb-config already prints an error if something goes wrong
267         _t=$("${CTDB_HELPER_BINDIR}/ctdb-config" get "$_section" "$_opt") ||
268                 exit $?
269         echo "${_variable}=\"${_t}\""
270 }
271
272 ######################################################
273 # wrapper around /proc/ settings to allow them to be hooked
274 # for testing
275 # 1st arg is relative path under /proc/, 2nd arg is value to set
276 set_proc()
277 {
278         echo "$2" >"/proc/$1"
279 }
280
281 set_proc_maybe()
282 {
283         if [ -w "/proc/$1" ]; then
284                 set_proc "$1" "$2"
285         fi
286 }
287
288 ######################################################
289 # wrapper around getting file contents from /proc/ to allow
290 # this to be hooked for testing
291 # 1st arg is relative path under /proc/
292 get_proc()
293 {
294         cat "/proc/$1"
295 }
296
297 ######################################################
298 # Print up to $_max kernel stack traces for processes named $_program
299 program_stack_traces()
300 {
301         _prog="$1"
302         _max="${2:-1}"
303
304         _count=1
305         for _pid in $(pidof "$_prog"); do
306                 [ "$_count" -le "$_max" ] || break
307
308                 # Do this first to avoid racing with process exit
309                 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
310                 if [ -n "$_stack" ]; then
311                         echo "Stack trace for ${_prog}[${_pid}]:"
312                         echo "$_stack"
313                         _count=$((_count + 1))
314                 fi
315         done
316 }
317
318 ######################################################
319 # Ensure $service_name is set
320 assert_service_name()
321 {
322         # service_name is set by the event script
323         # shellcheck disable=SC2154
324         [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
325 }
326
327 ######################################################
328 # check a set of directories is available
329 # return 1 on a missing directory
330 # directories are read from stdin
331 ######################################################
332 ctdb_check_directories_probe()
333 {
334         while IFS="" read -r d; do
335                 case "$d" in
336                 *%*)
337                         continue
338                         ;;
339                 *)
340                         [ -d "${d}/." ] || return 1
341                         ;;
342                 esac
343         done
344 }
345
346 ######################################################
347 # check a set of directories is available
348 # directories are read from stdin
349 ######################################################
350 ctdb_check_directories()
351 {
352         ctdb_check_directories_probe || {
353                 echo "ERROR: $service_name directory \"$d\" not available"
354                 exit 1
355         }
356 }
357
358 ######################################################
359 # check a set of tcp ports
360 # usage: ctdb_check_tcp_ports <ports...>
361 ######################################################
362
363 # Check whether something is listening on all of the given TCP ports
364 # using the "ctdb checktcpport" command.
365 ctdb_check_tcp_ports()
366 {
367         if [ -z "$1" ]; then
368                 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
369                 exit 1
370         fi
371
372         for _p; do # process each function argument (port)
373                 _cmd="$CTDB checktcpport $_p"
374                 _out=$($_cmd 2>&1)
375                 _ret=$?
376                 case "$_ret" in
377                 0)
378                         echo "$service_name not listening on TCP port $_p"
379                         return 1
380                         ;;
381                 98)
382                         # Couldn't bind, something already listening, next port
383                         continue
384                         ;;
385                 *)
386                         echo "unexpected error (${_ret}) running \"${_cmd}\""
387                         if [ -n "$_out" ]; then
388                                 echo "$_out"
389                         fi
390                         return $_ret
391                         ;;
392                 esac
393         done
394
395         # All ports listening
396         return 0
397 }
398
399 ######################################################
400 # check a unix socket
401 # usage: ctdb_check_unix_socket SOCKPATH
402 ######################################################
403 ctdb_check_unix_socket()
404 {
405         _sockpath="$1"
406
407         if [ -z "$_sockpath" ]; then
408                 echo "ERROR: ctdb_check_unix_socket() requires socket path"
409                 return 1
410         fi
411
412         _out=$(ss -l -x "src ${_sockpath}" | tail -n +2)
413         if [ -z "$_out" ]; then
414                 echo "ERROR: ${service_name} not listening on ${_sockpath}"
415                 return 1
416         fi
417 }
418
419 ################################################
420 # kill off any TCP connections with the given IP
421 ################################################
422 kill_tcp_connections()
423 {
424         _iface="$1"
425         _ip="$2"
426
427         _oneway=false
428         if [ "$3" = "oneway" ]; then
429                 _oneway=true
430         fi
431
432         get_tcp_connections_for_ip "$_ip" | {
433                 _killcount=0
434                 _connections=""
435                 _nl="
436 "
437                 while read -r _dst _src; do
438                         _destport="${_dst##*:}"
439                         __oneway=$_oneway
440                         case $_destport in
441                         # we only do one-way killtcp for CIFS
442                         139 | 445) __oneway=true ;;
443                         esac
444
445                         _connections="${_connections}${_nl}${_src} ${_dst}"
446                         if ! $__oneway; then
447                                 _connections="${_connections}${_nl}${_dst} ${_src}"
448                         fi
449
450                         _killcount=$((_killcount + 1))
451                 done
452
453                 if [ $_killcount -eq 0 ]; then
454                         return
455                 fi
456
457                 if [ -n "$CTDB_KILLTCP_DEBUGLEVEL" ]; then
458                         _debuglevel="$CTDB_KILLTCP_DEBUGLEVEL"
459                 else
460                         _debuglevel="$CTDB_DEBUGLEVEL"
461                 fi
462                 echo "$_connections" |
463                         CTDB_DEBUGLEVEL="$_debuglevel" \
464                                 "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || {
465                         echo "Failed to kill TCP connections"
466                         return
467                 }
468
469                 _connections=$(get_tcp_connections_for_ip "$_ip")
470                 if [ -z "$_connections" ]; then
471                         _remaining=0
472                 else
473                         _remaining=$(echo "$_connections" | wc -l)
474                 fi
475
476                 _actually_killed=$((_killcount - _remaining))
477
478                 _t="${_actually_killed}/${_killcount}"
479                 echo "Killed ${_t} TCP connections to released IP $_ip"
480
481                 if [ -n "$_connections" ]; then
482                         echo "Remaining connections:"
483                         echo "$_connections" | sed -e 's|^|  |'
484                 fi
485         }
486 }
487
488 ##################################################################
489 # kill off the local end for any TCP connections with the given IP
490 ##################################################################
491 kill_tcp_connections_local_only()
492 {
493         kill_tcp_connections "$@" "oneway"
494 }
495
496 ##################################################################
497 # tickle any TCP connections with the given IP
498 ##################################################################
499 tickle_tcp_connections()
500 {
501         _ip="$1"
502
503         # Get connections, both directions
504         _conns=$(get_tcp_connections_for_ip "$_ip" |
505                 awk '{ print $1, $2 ; print $2, $1 }')
506
507         echo "$_conns" | awk '{ print "Tickle TCP connection", $1, $2 }'
508         echo "$_conns" | ctdb tickle
509 }
510
511 get_tcp_connections_for_ip()
512 {
513         _ip="$1"
514
515         ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}'
516 }
517
518 ########################################################
519
520 add_ip_to_iface()
521 {
522         _iface=$1
523         _ip=$2
524         _maskbits=$3
525
526         # Ensure interface is up
527         ip link set "$_iface" up ||
528                 die "Failed to bringup interface $_iface"
529
530         # Only need to define broadcast for IPv4
531         case "$_ip" in
532         *:*) _bcast="" ;;
533         *) _bcast="brd +" ;;
534         esac
535
536         # Intentionally unquoted multi-word value here
537         # shellcheck disable=SC2086
538         ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
539                 echo "Failed to add $_ip/$_maskbits on dev $_iface"
540                 return 1
541         }
542
543         # Wait 5 seconds for IPv6 addresses to stop being tentative...
544         if [ -z "$_bcast" ]; then
545                 for _x in $(seq 1 10); do
546                         ip addr show to "${_ip}/128" | grep -q "tentative" || break
547                         sleep 0.5
548                 done
549
550                 # If the address was a duplicate then it won't be on the
551                 # interface so flag an error.
552                 _t=$(ip addr show to "${_ip}/128")
553                 case "$_t" in
554                 "")
555                         echo "Failed to add $_ip/$_maskbits on dev $_iface"
556                         return 1
557                         ;;
558                 *tentative* | *dadfailed*)
559                         echo "Failed to add $_ip/$_maskbits on dev $_iface"
560                         ip addr del "$_ip/$_maskbits" dev "$_iface"
561                         return 1
562                         ;;
563                 esac
564         fi
565 }
566
567 delete_ip_from_iface()
568 {
569         _iface=$1
570         _ip=$2
571         _maskbits=$3
572
573         # This could be set globally for all interfaces but it is probably
574         # better to avoid surprises, so limit it the interfaces where CTDB
575         # has public IP addresses.  There isn't anywhere else convenient
576         # to do this so just set it each time.  This is much cheaper than
577         # remembering and re-adding secondaries.
578         set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
579
580         ip addr del "$_ip/$_maskbits" dev "$_iface" || {
581                 echo "Failed to del $_ip on dev $_iface"
582                 return 1
583         }
584 }
585
586 # If the given IP is hosted then print 2 items: maskbits and iface
587 ip_maskbits_iface()
588 {
589         _addr="$1"
590
591         case "$_addr" in
592         *:*) _bits=128 ;;
593         *) _bits=32 ;;
594         esac
595         ip addr show to "${_addr}/${_bits}" 2>/dev/null |
596                 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ;
597                        sub("@.*", "", iface) }
598              $1 ~ /inet/ { mask = $2; sub(".*/", "", mask);
599                            print mask, iface }'
600 }
601
602 drop_ip()
603 {
604         _addr="${1%/*}" # Remove optional maskbits
605
606         # Intentional word splitting here
607         # shellcheck disable=SC2046
608         set -- $(ip_maskbits_iface "$_addr")
609         if [ -n "$1" ]; then
610                 _maskbits="$1"
611                 _iface="$2"
612                 echo "Removing public address $_addr/$_maskbits from device $_iface"
613                 delete_ip_from_iface "$_iface" "$_addr" "$_maskbits" >/dev/null 2>&1
614         fi
615 }
616
617 drop_all_public_ips()
618 {
619         # _x is intentionally ignored
620         # shellcheck disable=SC2034
621         while read -r _ip _x; do
622                 case "$_ip" in
623                 \#*) continue ;;
624                 esac
625                 drop_ip "$_ip"
626         done <"${CTDB_BASE}/public_addresses"
627 }
628
629 flush_route_cache()
630 {
631         set_proc_maybe sys/net/ipv4/route/flush 1
632         set_proc_maybe sys/net/ipv6/route/flush 1
633 }
634
635 ########################################################
636 # Interface monitoring
637
638 # If the interface is a virtual one (e.g. VLAN) then get the
639 # underlying interface
640 interface_get_real()
641 {
642         _iface="$1"
643
644         # If $_iface is a VLAN (i.e. contains an '@') then strip every
645         # before the '@', otherwise print the whole interface
646         echo "${_iface##*@}"
647 }
648
649 # Check whether an interface is operational
650 interface_monitor()
651 {
652         _iface="$1"
653
654         _iface_info=$(ip -br link show "$_iface" 2>&1) || {
655                 echo "ERROR: Monitored interface ${_iface} does not exist"
656                 return 1
657         }
658
659         # If the interface is a virtual one (e.g. VLAN) then get the
660         # underlying interface.
661         _realiface=$(interface_get_real "${_iface_info%% *}")
662
663         if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null); then
664                 # This is a bond: various monitoring strategies
665                 echo "$_bi" | grep -q 'Currently Active Slave: None' && {
666                         echo "ERROR: No active slaves for bond device ${_realiface}"
667                         return 1
668                 }
669                 echo "$_bi" | grep -q '^MII Status: up' || {
670                         echo "ERROR: public network interface ${_realiface} is down"
671                         return 1
672                 }
673                 echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
674                         # This works around a bug in the driver where the
675                         # overall bond status can be up but none of the actual
676                         # physical interfaces have a link.
677                         echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
678                                 echo "ERROR: No active slaves for 802.ad bond device ${_realiface}"
679                                 return 1
680                         }
681                 }
682
683                 return 0
684         else
685                 # Not a bond
686                 case "$_iface" in
687                 lo*)
688                         # loopback is always working
689                         return 0
690                         ;;
691                 ib*)
692                         # we don't know how to test ib links
693                         return 0
694                         ;;
695                 *)
696                         ethtool "$_iface" | grep -q 'Link detected: yes' || {
697                                 # On some systems, this is not successful when a
698                                 # cable is plugged but the interface has not been
699                                 # brought up previously. Bring the interface up
700                                 # and try again...
701                                 ip link set "$_iface" up
702                                 ethtool "$_iface" | grep -q 'Link detected: yes' || {
703                                         echo "ERROR: No link on the public network interface ${_iface}"
704                                         return 1
705                                 }
706                         }
707                         return 0
708                         ;;
709                 esac
710         fi
711 }
712
713 ########################################################
714 # Simple counters
715 _ctdb_counter_common()
716 {
717         [ $# -le 1 ] || die "usage: _ctdb_counter_common [name]"
718
719         if [ $# -eq 1 ]; then
720                 _counter_name="${1}.failcount"
721         else
722                 _counter_name="failcount"
723         fi
724
725         if [ -z "$script_state_dir" ]; then
726                 die "ctdb_counter_* functions need ctdb_setup_state_dir()"
727         fi
728
729         _counter_file="${script_state_dir}/${_counter_name}"
730 }
731 # Some code passes an argument
732 # shellcheck disable=SC2120
733 ctdb_counter_init()
734 {
735         _ctdb_counter_common "$1"
736
737         : >"$_counter_file"
738 }
739 ctdb_counter_incr()
740 {
741         _ctdb_counter_common "$1"
742
743         # unary counting using newlines!
744         echo >>"$_counter_file"
745 }
746 ctdb_counter_get()
747 {
748         _ctdb_counter_common "$1"
749         # unary counting!
750         _val=$(wc -c <"$_counter_file" 2>/dev/null || echo 0)
751         # Strip leading spaces from output of wc (on freebsd)
752         # shellcheck disable=SC2086
753         echo $_val
754 }
755
756 ########################################################
757
758 # ctdb_setup_state_dir <type> <name>
759 #   Sets/creates script_state_dir)
760 ctdb_setup_state_dir()
761 {
762         [ $# -eq 2 ] || die "usage: ctdb_setup_state_dir <type> <name>"
763
764         _type="$1"
765         _name="$2"
766
767         script_state_dir="${CTDB_SCRIPT_VARDIR}/${_type}/${_name}"
768
769         mkdir -p "$script_state_dir" ||
770                 die "Error creating script state dir \"${script_state_dir}\""
771 }
772
773 ##################################################################
774 # Reconfigure a service on demand
775
776 _ctdb_service_reconfigure_common()
777 {
778         if [ -z "$script_state_dir" ]; then
779                 die "ctdb_service_*_reconfigure() needs ctdb_setup_state_dir()"
780         fi
781
782         _ctdb_service_reconfigure_flag="${script_state_dir}/need_reconfigure"
783 }
784
785 ctdb_service_needs_reconfigure()
786 {
787         _ctdb_service_reconfigure_common
788         [ -e "$_ctdb_service_reconfigure_flag" ]
789 }
790
791 ctdb_service_set_reconfigure()
792 {
793         _ctdb_service_reconfigure_common
794         : >"$_ctdb_service_reconfigure_flag"
795 }
796
797 ctdb_service_unset_reconfigure()
798 {
799         _ctdb_service_reconfigure_common
800         rm -f "$_ctdb_service_reconfigure_flag"
801 }
802
803 ctdb_service_reconfigure()
804 {
805         echo "Reconfiguring service \"${service_name}\"..."
806         ctdb_service_unset_reconfigure
807         service_reconfigure || return $?
808         # Intentionally have this use $service_name as default
809         # shellcheck disable=SC2119
810         ctdb_counter_init
811 }
812
813 # Default service_reconfigure() function does nothing.
814 service_reconfigure()
815 {
816         :
817 }
818
819 # Default service_start() and service_stop() functions.
820
821 # These may be overridden in an eventscript.
822 service_start()
823 {
824         service "$service_name" start
825 }
826
827 service_stop()
828 {
829         service "$service_name" stop
830 }
831
832 ##################################################################
833
834 # This exists only for backward compatibility with 3rd party scripts
835 # that call it
836 ctdb_standard_event_handler()
837 {
838         :
839 }
840
841 iptables_wrapper()
842 {
843         _family="$1"
844         shift
845         if [ "$_family" = "inet6" ]; then
846                 _iptables_cmd="ip6tables"
847         else
848                 _iptables_cmd="iptables"
849         fi
850
851         # iptables doesn't like being re-entered, so flock-wrap it.
852         flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@"
853 }
854
855 # AIX (and perhaps others?) doesn't have mktemp
856 # type is commonly supported and more portable than which(1)
857 # shellcheck disable=SC2039
858 if ! type mktemp >/dev/null 2>&1; then
859         mktemp()
860         {
861                 _dir=false
862                 if [ "$1" = "-d" ]; then
863                         _dir=true
864                         shift
865                 fi
866                 _d="${TMPDIR:-/tmp}"
867                 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null |
868                         cksum |
869                         awk '{print $1}')
870                 _t="${_d}/tmp.${_hex10}"
871                 (
872                         umask 077
873                         if $_dir; then
874                                 mkdir "$_t"
875                         else
876                                 : >"$_t"
877                         fi
878                 )
879                 echo "$_t"
880         }
881 fi
882
883 ######################################################################
884 # NFS callout handling
885
886 nfs_callout_init()
887 {
888         _state_dir="$1"
889
890         if [ -z "$CTDB_NFS_CALLOUT" ]; then
891                 CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-linux-kernel-callout"
892         fi
893         # Always export, for statd callout
894         export CTDB_NFS_CALLOUT
895
896         # If the callout wants to use this then it must create it
897         export CTDB_NFS_CALLOUT_STATE_DIR="${_state_dir}/callout-state"
898
899         # Export, if set, for use by clustered NFS callouts
900         if [ -n "$CTDB_NFS_STATE_FS_TYPE" ]; then
901                 export CTDB_NFS_STATE_FS_TYPE
902         fi
903         if [ -n "$CTDB_NFS_STATE_MNT" ]; then
904                 export CTDB_NFS_STATE_MNT
905         fi
906
907         nfs_callout_cache="${_state_dir}/nfs_callout_cache"
908         nfs_callout_cache_callout="${nfs_callout_cache}/CTDB_NFS_CALLOUT"
909         nfs_callout_cache_ops="${nfs_callout_cache}/ops"
910 }
911
912 nfs_callout_register()
913 {
914         mkdir -p "$nfs_callout_cache_ops"
915         rm -f "$nfs_callout_cache_ops"/*
916
917         echo "$CTDB_NFS_CALLOUT" >"$nfs_callout_cache_callout"
918
919         _t=$("$CTDB_NFS_CALLOUT" "register")
920         if [ -n "$_t" ]; then
921                 echo "$_t" |
922                         while IFS="" read -r _op; do
923                                 touch "${nfs_callout_cache_ops}/${_op}"
924                         done
925         else
926                 touch "${nfs_callout_cache_ops}/ALL"
927         fi
928 }
929
930 nfs_callout()
931 {
932         # Re-run registration if $CTDB_NFS_CALLOUT has changed
933         _prev=""
934         if [ -r "$nfs_callout_cache_callout" ]; then
935                 read -r _prev <"$nfs_callout_cache_callout"
936         fi
937         if [ "$CTDB_NFS_CALLOUT" != "$_prev" ]; then
938                 nfs_callout_register
939         fi
940
941         # Run the operation if it is registered...
942         if [ -e "${nfs_callout_cache_ops}/${1}" ] ||
943                 [ -e "${nfs_callout_cache_ops}/ALL" ]; then
944                 "$CTDB_NFS_CALLOUT" "$@"
945         fi
946 }
947
948 ########################################################
949 # tickle handling
950 ########################################################
951
952 update_tickles()
953 {
954         _port="$1"
955
956         tickledir="${CTDB_SCRIPT_VARDIR}/tickles"
957         mkdir -p "$tickledir"
958
959         # What public IPs do I hold?
960         _pnn=$(ctdb_get_pnn)
961         _ips=$($CTDB -X ip | awk -F'|' -v pnn="$_pnn" '$3 == pnn {print $2}')
962
963         # IPs and port as ss filters
964         _ip_filter=""
965         for _ip in $_ips; do
966                 _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]"
967         done
968         _port_filter="sport == :${_port}"
969
970         # Record connections to our public IPs in a temporary file.
971         # This temporary file is in CTDB's private state directory and
972         # $$ is used to avoid a very rare race involving CTDB's script
973         # debugging.  No security issue, nothing to see here...
974         _my_connections="${tickledir}/${_port}.connections.$$"
975         # Parentheses are needed around the filters for precedence but
976         # the parentheses can't be empty!
977         #
978         # Recent versions of ss print square brackets around IPv6
979         # addresses.  While it is desirable to update CTDB's address
980         # parsing and printing code, something needs to be done here
981         # for backward compatibility, so just delete the brackets.
982         ss -tn state established \
983                 "${_ip_filter:+( ${_ip_filter} )}" \
984                 "${_port_filter:+( ${_port_filter} )}" |
985                 awk 'NR > 1 {print $4, $3}' |
986                 tr -d '][' |
987                 sort >"$_my_connections"
988
989         # Record our current tickles in a temporary file
990         _my_tickles="${tickledir}/${_port}.tickles.$$"
991         for _i in $_ips; do
992                 $CTDB -X gettickles "$_i" "$_port" |
993                         awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
994         done |
995                 sort >"$_my_tickles"
996
997         # Add tickles for connections that we haven't already got tickles for
998         comm -23 "$_my_connections" "$_my_tickles" |
999                 $CTDB addtickle
1000
1001         # Remove tickles for connections that are no longer there
1002         comm -13 "$_my_connections" "$_my_tickles" |
1003                 $CTDB deltickle
1004
1005         rm -f "$_my_connections" "$_my_tickles"
1006
1007         # Remove stale files from killed scripts
1008         # Files can't have spaces in name, more portable than -print0/-0
1009         # shellcheck disable=SC2038
1010         (cd "$tickledir" && find . -type f -mmin +10 | xargs -r rm)
1011 }
1012
1013 ########################################################
1014 # load a site local config file
1015 ########################################################
1016
1017 [ -x "${CTDB_BASE}/rc.local" ] && {
1018         . "${CTDB_BASE}/rc.local"
1019 }
1020
1021 [ -d "${CTDB_BASE}/rc.local.d" ] && {
1022         for i in "${CTDB_BASE}/rc.local.d"/*; do
1023                 [ -x "$i" ] && . "$i"
1024         done
1025 }
1026
1027 script_name="${0##*/}" # basename