s3/libsmb: clang: Fix: The right operand of '<' is a garbage value
[Samba.git] / ctdb / config / functions
blob1dc165328902ac583000d2c6b1b2aea4def8c958
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" ; shift
96     case "$CTDB_LOGGING" in
97         file:*|"")
98             if [ -n "$CTDB_LOGGING" ] ; then
99                 _file="${CTDB_LOGGING#file:}"
100             else
101                 _file="/usr/local/var/log/log.ctdb"
102             fi
103             {
104                 if [ -n "$*" ] ; then
105                     echo "$*"
106                 else
107                     cat
108                 fi
109             } >>"$_file"
110             ;;
111         *)
112             # Handle all syslog:* variants here too.  There's no tool to do
113             # the lossy things, so just use logger.
114             logger -t "ctdbd: ${_tag}" "$@"
115             ;;
116     esac
119 # When things are run in the background in an eventscript then logging
120 # output might get lost.  This is the "solution".  :-)
121 background_with_logging ()
123     (
124         "$@" 2>&1 </dev/null |
125         script_log "${script_name}&"
126     )&
128     return 0
131 ##############################################################
132 # check number of args for different events
133 ctdb_check_args ()
135     case "$1" in
136         takeip|releaseip)
137             if [ $# != 4 ]; then
138                 echo "ERROR: must supply interface, IP and maskbits"
139                 exit 1
140             fi
141             ;;
142         updateip)
143             if [ $# != 5 ]; then
144                 echo "ERROR: must supply old interface, new interface, IP and maskbits"
145                 exit 1
146             fi
147             ;;
148     esac
151 ##############################################################
152 # determine on what type of system (init style) we are running
153 detect_init_style()
155         # only do detection if not already set:
156         if [ -n "$CTDB_INIT_STYLE" ] ; then
157                 return
158         fi
160         if [ -x /sbin/startproc ]; then
161                 CTDB_INIT_STYLE="suse"
162         elif [ -x /sbin/start-stop-daemon ]; then
163                 CTDB_INIT_STYLE="debian"
164         else
165                 CTDB_INIT_STYLE="redhat"
166         fi
169 ######################################################
170 # simulate /sbin/service on platforms that don't have it
171 # _service() makes it easier to hook the service() function for
172 # testing.
173 _service ()
175   _service_name="$1"
176   _op="$2"
178   # do nothing, when no service was specified
179   [ -z "$_service_name" ] && return
181   if [ -x /sbin/service ]; then
182       $_nice /sbin/service "$_service_name" "$_op"
183   elif [ -x /usr/sbin/service ]; then
184       $_nice /usr/sbin/service "$_service_name" "$_op"
185   elif [ -x /bin/systemctl ]; then
186       $_nice /bin/systemctl "$_op" "$_service_name"
187   elif [ -x "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" ]; then
188       $_nice "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" "$_op"
189   elif [ -x "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" ]; then
190       $_nice "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" "$_op"
191   fi
194 service()
196     _nice=""
197     _service "$@"
200 ######################################################
201 # simulate /sbin/service (niced) on platforms that don't have it
202 nice_service()
204     _nice="nice"
205     _service "$@"
208 ######################################################
209 # Cached retrieval of PNN from local node.  This never changes so why
210 # open a client connection to the server each time this is needed?
211 ctdb_get_pnn ()
213     _pnn_file="${CTDB_SCRIPT_VARDIR}/my-pnn"
214     if [ ! -f "$_pnn_file" ] ; then
215         $CTDB pnn >"$_pnn_file"
216     fi
218     cat "$_pnn_file"
221 # Cached retrieval of private IP address from local node.  This never
222 # changes.
223 ctdb_get_ip_address ()
225     _ip_addr_file="${CTDB_SCRIPT_VARDIR}/my-ip-address"
226     if [ ! -f "$_ip_addr_file" ] ; then
227         $CTDB -X nodestatus |
228             awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file"
229     fi
231     # ip_address is used by caller
232     # shellcheck disable=SC2034
233     cat "$_ip_addr_file"
236 # Cached retrieval of database options for use by event scripts.
238 # If the variables are already set then they should not be overwritten
239 # - this should only happen during event script testing.
240 ctdb_get_db_options ()
242         _db_opts_file="${CTDB_SCRIPT_VARDIR}/db_options.cache"
244         if [ ! -f "$_db_opts_file" ] ; then
245                 {
246                         ctdb_translate_option "database" \
247                                               "volatile database directory" \
248                                               "CTDB_DBDIR"
249                         ctdb_translate_option "database" \
250                                               "persistent database directory" \
251                                               "CTDB_DBDIR_PERSISTENT"
252                         ctdb_translate_option "database" \
253                                               "state database directory" \
254                                               "CTDB_DBDIR_STATE"
255                 } >"$_db_opts_file"
256         fi
258         . "$_db_opts_file"
261 ctdb_translate_option ()
263         _section="$1"
264         _opt="$2"
265         _variable="$3"
267         # ctdb-config already prints an error if something goes wrong
268         _t=$("${CTDB_HELPER_BINDIR}/ctdb-config" get "$_section" "$_opt") || \
269                 exit $?
270         echo "${_variable}=\"${_t}\""
273 ######################################################
274 # wrapper around /proc/ settings to allow them to be hooked
275 # for testing
276 # 1st arg is relative path under /proc/, 2nd arg is value to set
277 set_proc ()
279     echo "$2" >"/proc/$1"
282 set_proc_maybe ()
284     if [ -w "/proc/$1" ] ; then
285         set_proc "$1" "$2"
286     fi
289 ######################################################
290 # wrapper around getting file contents from /proc/ to allow
291 # this to be hooked for testing
292 # 1st arg is relative path under /proc/
293 get_proc ()
295     cat "/proc/$1"
298 ######################################################
299 # Print up to $_max kernel stack traces for processes named $_program
300 program_stack_traces ()
302     _prog="$1"
303     _max="${2:-1}"
305     _count=1
306     for _pid in $(pidof "$_prog") ; do
307         [ "$_count" -le "$_max" ] || break
309         # Do this first to avoid racing with process exit
310         _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
311         if [ -n "$_stack" ] ; then
312             echo "Stack trace for ${_prog}[${_pid}]:"
313             echo "$_stack"
314             _count=$((_count + 1))
315         fi
316     done
319 ######################################################
320 # Ensure $service_name is set
321 assert_service_name ()
323         # service_name is set by the event script
324         # shellcheck disable=SC2154
325         [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
328 ######################################################
329 # check a set of directories is available
330 # return 1 on a missing directory
331 # directories are read from stdin
332 ######################################################
333 ctdb_check_directories_probe()
335     while IFS="" read d ; do
336         case "$d" in
337             *%*)
338                 continue
339                 ;;
340             *)
341                 [ -d "${d}/." ] || return 1
342         esac
343     done
346 ######################################################
347 # check a set of directories is available
348 # directories are read from stdin
349 ######################################################
350 ctdb_check_directories()
352     ctdb_check_directories_probe || {
353         echo "ERROR: $service_name directory \"$d\" not available"
354         exit 1
355     }
358 ######################################################
359 # check a set of tcp ports
360 # usage: ctdb_check_tcp_ports <ports...>
361 ######################################################
363 # Check whether something is listening on all of the given TCP ports
364 # using the "ctdb checktcpport" command.
365 ctdb_check_tcp_ports()
367         if [ -z "$1" ] ; then
368                 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
369                 exit 1
370         fi
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
395         # All ports listening
396         return 0
399 ######################################################
400 # check a unix socket
401 # usage: ctdb_check_unix_socket SOCKPATH
402 ######################################################
403 ctdb_check_unix_socket()
405         _sockpath="$1"
407         if [ -z "$_sockpath" ] ; then
408                 echo "ERROR: ctdb_check_unix_socket() requires socket path"
409                 return 1
410         fi
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
419 ################################################
420 # kill off any TCP connections with the given IP
421 ################################################
422 kill_tcp_connections ()
424     _iface="$1"
425     _ip="$2"
427     _oneway=false
428     if [ "$3" = "oneway" ] ; then
429         _oneway=true
430     fi
432     get_tcp_connections_for_ip "$_ip" | {
433         _killcount=0
434         _connections=""
435         _nl="
437         while read _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
445             _connections="${_connections}${_nl}${_src} ${_dst}"
446             if ! $__oneway ; then
447                 _connections="${_connections}${_nl}${_dst} ${_src}"
448             fi
450             _killcount=$((_killcount + 1))
451         done
453         if [ $_killcount -eq 0 ] ; then
454             return
455         fi
457         echo "$_connections" | \
458                 "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || {
459                 echo "Failed to kill TCP connections"
460                 return
461         }
463         _connections=$(get_tcp_connections_for_ip "$_ip")
464         if [ -z "$_connections" ] ; then
465                 _remaining=0
466         else
467                 _remaining=$(echo "$_connections" | wc -l)
468         fi
470         _actually_killed=$((_killcount - _remaining))
472         _t="${_actually_killed}/${_killcount}"
473         echo "Killed ${_t} TCP connections to released IP $_ip"
475         if [ -n "$_connections" ] ; then
476                 echo "Remaining connections:"
477                 echo "$_connections" | sed -e 's|^|  |'
478         fi
479     }
482 ##################################################################
483 # kill off the local end for any TCP connections with the given IP
484 ##################################################################
485 kill_tcp_connections_local_only ()
487     kill_tcp_connections "$@" "oneway"
490 ##################################################################
491 # tickle any TCP connections with the given IP
492 ##################################################################
493 tickle_tcp_connections ()
495     _ip="$1"
497     # Get connections, both directions
498     _conns=$(get_tcp_connections_for_ip "$_ip" | \
499                     awk '{ print $1, $2 ; print $2, $1 }')
501     echo "$_conns" | awk '{ print "Tickle TCP connection", $1, $2 }'
502     echo "$_conns" | ctdb tickle
505 get_tcp_connections_for_ip ()
507     _ip="$1"
509     ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}'
512 ########################################################
514 add_ip_to_iface ()
516     _iface=$1
517     _ip=$2
518     _maskbits=$3
520     # Ensure interface is up
521     ip link set "$_iface" up || \
522         die "Failed to bringup interface $_iface"
524     # Only need to define broadcast for IPv4
525     case "$_ip" in
526         *:*) _bcast=""      ;;
527         *)   _bcast="brd +" ;;
528     esac
530     # Intentionally unquoted multi-word value here
531     # shellcheck disable=SC2086
532     ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
533         echo "Failed to add $_ip/$_maskbits on dev $_iface"
534         return 1
535     }
537     # Wait 5 seconds for IPv6 addresses to stop being tentative...
538     if [ -z "$_bcast" ] ; then
539         for _x in $(seq 1 10) ; do
540             ip addr show to "${_ip}/128" | grep -q "tentative" || break
541             sleep 0.5
542         done
544         # If the address was a duplicate then it won't be on the
545         # interface so flag an error.
546         _t=$(ip addr show to "${_ip}/128")
547         case "$_t" in
548             "")
549                 echo "Failed to add $_ip/$_maskbits on dev $_iface"
550                 return 1
551                 ;;
552             *tentative*|*dadfailed*)
553                 echo "Failed to add $_ip/$_maskbits on dev $_iface"
554                 ip addr del "$_ip/$_maskbits" dev "$_iface"
555                 return 1
556                 ;;
557         esac
558     fi
561 delete_ip_from_iface()
563     _iface=$1
564     _ip=$2
565     _maskbits=$3
567     # This could be set globally for all interfaces but it is probably
568     # better to avoid surprises, so limit it the interfaces where CTDB
569     # has public IP addresses.  There isn't anywhere else convenient
570     # to do this so just set it each time.  This is much cheaper than
571     # remembering and re-adding secondaries.
572     set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
574     ip addr del "$_ip/$_maskbits" dev "$_iface" || {
575         echo "Failed to del $_ip on dev $_iface"
576         return 1
577     }
580 # If the given IP is hosted then print 2 items: maskbits and iface
581 ip_maskbits_iface ()
583     _addr="$1"
585     case "$_addr" in
586         *:*) _bits=128 ;;
587         *)   _bits=32  ;;
588     esac
589     ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
590         awk 'NR == 1 { iface = $2; sub(":$", "", iface) ;
591                        sub("@.*", "", iface) }
592              $1 ~ /inet/ { mask = $2; sub(".*/", "", mask);
593                            print mask, iface }'
596 drop_ip ()
598     _addr="${1%/*}"  # Remove optional maskbits
600     # Intentional word splitting here
601     # shellcheck disable=SC2046
602     set -- $(ip_maskbits_iface "$_addr")
603     if [ -n "$1" ] ; then
604         _maskbits="$1"
605         _iface="$2"
606         echo "Removing public address $_addr/$_maskbits from device $_iface"
607         delete_ip_from_iface "$_iface" "$_addr" "$_maskbits" >/dev/null 2>&1
608     fi
611 drop_all_public_ips ()
613         # _x is intentionally ignored
614         # shellcheck disable=SC2034
615         while read _ip _x ; do
616                 drop_ip "$_ip"
617         done <"${CTDB_BASE}/public_addresses"
620 flush_route_cache ()
622     set_proc_maybe sys/net/ipv4/route/flush 1
623     set_proc_maybe sys/net/ipv6/route/flush 1
626 ########################################################
627 # Interface monitoring
629 # If the interface is a virtual one (e.g. VLAN) then get the
630 # underlying interface
631 interface_get_real ()
633     # Output of "ip link show <iface>"
634     _iface_info="$1"
636     # Extract the full interface description to see if it is a VLAN
637     _t=$(echo "$_iface_info" |
638                 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ;
639                                print iface }')
640     case "$_t" in
641         *@*)
642             # VLAN: use the underlying interface, after the '@'
643             echo "${_t##*@}"
644             ;;
645         *)
646             # Not a regular VLAN.  For backward compatibility, assume
647             # there is some other sort of VLAN that doesn't have the
648             # '@' in the output and only use what is before a '.'.  If
649             # there is no '.' then this will be the whole interface
650             # name.
651             echo "${_t%%.*}"
652     esac
655 # Check whether an interface is operational
656 interface_monitor ()
658     _iface="$1"
660     _iface_info=$(ip link show "$_iface" 2>&1) || {
661         echo "ERROR: Monitored interface ${_iface} does not exist"
662         return 1
663     }
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 () {
741     _ctdb_counter_common "$1"
743     : >"$_counter_file"
745 ctdb_counter_incr () {
746     _ctdb_counter_common "$1"
748     # unary counting using newlines!
749     echo >>"$_counter_file"
751 ctdb_counter_get () {
752     _ctdb_counter_common "$1"
753     # unary counting!
754     _val=$(wc -c < "$_counter_file" 2>/dev/null || echo 0)
755     # Strip leading spaces from ouput of wc (on freebsd)
756     # shellcheck disable=SC2086
757     echo $_val
760 ########################################################
762 # ctdb_setup_state_dir <type> <name>
763 #   Sets/creates script_state_dir)
764 ctdb_setup_state_dir ()
766         [ $# -eq 2 ] || die "usage: ctdb_setup_state_dir <type> <name>"
768         _type="$1"
769         _name="$2"
771         script_state_dir="${CTDB_SCRIPT_VARDIR}/${_type}/${_name}"
773         mkdir -p "$script_state_dir" || \
774                 die "Error creating script state dir \"${script_state_dir}\""
777 ##################################################################
778 # Reconfigure a service on demand
780 _ctdb_service_reconfigure_common ()
782         if [ -z "$script_state_dir" ] ; then
783                 die "ctdb_service_*_reconfigure() needs ctdb_setup_state_dir()"
784         fi
786         _ctdb_service_reconfigure_flag="${script_state_dir}/need_reconfigure"
789 ctdb_service_needs_reconfigure ()
791     _ctdb_service_reconfigure_common
792     [ -e "$_ctdb_service_reconfigure_flag" ]
795 ctdb_service_set_reconfigure ()
797     _ctdb_service_reconfigure_common
798     : >"$_ctdb_service_reconfigure_flag"
801 ctdb_service_unset_reconfigure ()
803     _ctdb_service_reconfigure_common
804     rm -f "$_ctdb_service_reconfigure_flag"
807 ctdb_service_reconfigure ()
809     echo "Reconfiguring service \"${service_name}\"..."
810     ctdb_service_unset_reconfigure
811     service_reconfigure || return $?
812     # Intentionally have this use $service_name as default
813     # shellcheck disable=SC2119
814     ctdb_counter_init
817 # Default service_reconfigure() function does nothing.
818 service_reconfigure ()
820     :
823 # Default service_start() and service_stop() functions.
825 # These may be overridden in an eventscript.
826 service_start ()
828     service "$service_name" start
831 service_stop ()
833     service "$service_name" stop
836 ##################################################################
838 # This exists only for backward compatibility with 3rd party scripts
839 # that call it
840 ctdb_standard_event_handler ()
842     :
845 iptables_wrapper ()
847     _family="$1" ; shift
848     if [ "$_family" = "inet6" ] ; then
849         _iptables_cmd="ip6tables"
850     else
851         _iptables_cmd="iptables"
852     fi
854     # iptables doesn't like being re-entered, so flock-wrap it.
855     flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@"
858 # AIX (and perhaps others?) doesn't have mktemp
859 # type is commonly supported and more portable than which(1)
860 # shellcheck disable=SC2039
861 if ! type mktemp >/dev/null 2>&1 ; then
862     mktemp ()
863     {
864         _dir=false
865         if [ "$1" = "-d" ] ; then
866             _dir=true
867             shift
868         fi
869         _d="${TMPDIR:-/tmp}"
870         _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
871             cksum | \
872             awk '{print $1}')
873         _t="${_d}/tmp.${_hex10}"
874         (
875             umask 077
876             if $_dir ; then
877                 mkdir "$_t"
878             else
879                 : >"$_t"
880             fi
881         )
882         echo "$_t"
883     }
886 ######################################################################
887 # NFS callout handling
889 nfs_callout_init ()
891         _state_dir="$1"
893         if [ -z "$CTDB_NFS_CALLOUT" ] ; then
894                 CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-linux-kernel-callout"
895         fi
896         # Always export, for statd callout
897         export CTDB_NFS_CALLOUT
899         # If the callout wants to use this then it must create it
900         export CTDB_NFS_CALLOUT_STATE_DIR="${_state_dir}/callout-state"
902         # Export, if set, for use by clustered NFS callouts
903         if [ -n "$CTDB_NFS_STATE_FS_TYPE" ] ; then
904                 export CTDB_NFS_STATE_FS_TYPE
905         fi
906         if [ -n "$CTDB_NFS_STATE_MNT" ] ; then
907                 export CTDB_NFS_STATE_MNT
908         fi
910         nfs_callout_cache="${_state_dir}/nfs_callout_cache"
911         nfs_callout_cache_callout="${nfs_callout_cache}/CTDB_NFS_CALLOUT"
912         nfs_callout_cache_ops="${nfs_callout_cache}/ops"
915 nfs_callout_register ()
917     mkdir -p "$nfs_callout_cache_ops"
918     rm -f "$nfs_callout_cache_ops"/*
920     echo "$CTDB_NFS_CALLOUT" >"$nfs_callout_cache_callout"
922     _t=$(eval "$CTDB_NFS_CALLOUT" "register")
923     if [ -n "$_t" ] ; then
924         echo "$_t" |
925             while IFS="" read _op ; do
926                 touch "${nfs_callout_cache_ops}/${_op}"
927             done
928     else
929         touch "${nfs_callout_cache_ops}/ALL"
930     fi
933 nfs_callout ()
935     # Re-run registration if $CTDB_NFS_CALLOUT has changed
936     _prev=""
937     if [ -r "$nfs_callout_cache_callout" ] ; then
938         read _prev <"$nfs_callout_cache_callout"
939     fi
940     if [ "$CTDB_NFS_CALLOUT" != "$_prev" ] ; then
941         nfs_callout_register
942     fi
944     # Run the operation if it is registered...
945     if [ -e "${nfs_callout_cache_ops}/${1}" ] || \
946            [ -e "${nfs_callout_cache_ops}/ALL" ]; then
947         eval "$CTDB_NFS_CALLOUT" "$@"
948     fi
951 ########################################################
952 # tickle handling
953 ########################################################
955 update_tickles ()
957         _port="$1"
959         tickledir="${CTDB_SCRIPT_VARDIR}/tickles"
960         mkdir -p "$tickledir"
962         # What public IPs do I hold?
963         _pnn=$(ctdb_get_pnn)
964         _ips=$($CTDB -X ip | awk -F'|' -v pnn="$_pnn" '$3 == pnn {print $2}')
966         # IPs and port as ss filters
967         _ip_filter=""
968         for _ip in $_ips ; do
969             _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]"
970         done
971         _port_filter="sport == :${_port}"
973         # Record connections to our public IPs in a temporary file.
974         # This temporary file is in CTDB's private state directory and
975         # $$ is used to avoid a very rare race involving CTDB's script
976         # debugging.  No security issue, nothing to see here...
977         _my_connections="${tickledir}/${_port}.connections.$$"
978         # Parentheses are needed around the filters for precedence but
979         # the parentheses can't be empty!
980         ss -tn state established \
981            "${_ip_filter:+( ${_ip_filter} )}" \
982            "${_port_filter:+( ${_port_filter} )}" |
983         awk 'NR > 1 {print $4, $3}' |
984         sort >"$_my_connections"
986         # Record our current tickles in a temporary file
987         _my_tickles="${tickledir}/${_port}.tickles.$$"
988         for _i in $_ips ; do
989                 $CTDB -X gettickles "$_i" "$_port" |
990                 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
991         done |
992         sort >"$_my_tickles"
994         # Add tickles for connections that we haven't already got tickles for
995         comm -23 "$_my_connections" "$_my_tickles" | \
996                 $CTDB addtickle
998         # Remove tickles for connections that are no longer there
999         comm -13 "$_my_connections" "$_my_tickles" | \
1000                 $CTDB deltickle
1002         rm -f "$_my_connections" "$_my_tickles"
1004         # Remove stale files from killed scripts
1005         # Files can't have spaces in name, more portable than -print0/-0
1006         # shellcheck disable=SC2038
1007         (cd "$tickledir" && find . -type f -mmin +10 | xargs -r rm)
1010 ########################################################
1011 # load a site local config file
1012 ########################################################
1014 [ -x "${CTDB_BASE}/rc.local" ] && {
1015         . "${CTDB_BASE}/rc.local"
1018 [ -d "${CTDB_BASE}/rc.local.d" ] && {
1019         for i in "${CTDB_BASE}/rc.local.d"/* ; do
1020                 [ -x "$i" ] && . "$i"
1021         done
1024 script_name="${0##*/}"       # basename