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