a555f2
#!/bin/sh
a555f2
a555f2
# Some useful functions used in other MySQL helper scripts
a555f2
# This scripts defines variables datadir, errlogfile, socketfile
a555f2
a555f2
export LC_ALL=C
a555f2
a555f2
# extract value of a MySQL option from config files
a555f2
# Usage: get_mysql_option VARNAME DEFAULT SECTION [ SECTION, ... ]
a555f2
# result is returned in $result
a555f2
# We use my_print_defaults which prints all options from multiple files,
a555f2
# with the more specific ones later; hence take the last match.
a555f2
get_mysql_option(){
a555f2
	if [ $# -ne 3 ] ; then
a555f2
		echo "get_mysql_option requires 3 arguments: section option default_value"
a555f2
		return
a555f2
	fi
a555f2
	sections="$1"
a555f2
	option_name="$2"
a555f2
	default_value="$3"
a555f2
	result=`@bindir@/my_print_defaults $my_print_defaults_extra_args $sections | sed -n "s/^--${option_name}=//p" | tail -n 1`
a555f2
	if [ -z "$result" ]; then
a555f2
	    # not found, use default
a555f2
	    result="${default_value}"
a555f2
	fi
a555f2
}
a555f2
a555f2
# For the case of running more instances via systemd, scrits that source
a555f2
# this file can get --default-group-suffix or similar option as the first
a555f2
# argument. The utility my_print_defaults needs to use it as well, so the
a555f2
# scripts sourcing this file work with the same options as the daemon.
a555f2
my_print_defaults_extra_args=''
a555f2
while echo "$1" | grep -q '^--defaults' ; do
a555f2
	my_print_defaults_extra_args="${my_print_defaults_extra_args} $1"
a555f2
	shift
a555f2
done
a555f2
a555f2
# Defaults here had better match what mysqld_safe will default to
a555f2
# The option values are generally defined on three important places
a555f2
# on the default installation:
a555f2
#  1) default values are hardcoded in the code of mysqld daemon or
a555f2
#     mysqld_safe script
a555f2
#  2) configurable values are defined in @sysconfdir@/my.cnf
a555f2
#  3) default values for helper scripts are specified bellow
a555f2
# So, in case values are defined in my.cnf, we need to get that value.
a555f2
# In case they are not defined in my.cnf, we need to get the same value
a555f2
# in the daemon, as in the helper scripts. Thus, default values here
a555f2
# must correspond with values defined in mysqld_safe script and source
a555f2
# code itself.
a555f2
a555f2
server_sections="mysqld_safe mysqld server mysqld-@MAJOR_VERSION@.@MINOR_VERSION@ mariadb mariadb-@MAJOR_VERSION@.@MINOR_VERSION@ client-server"
a555f2
a555f2
get_mysql_option "$server_sections" datadir "@MYSQL_DATADIR@"
a555f2
datadir="$result"
a555f2
a555f2
# if there is log_error in the my.cnf, my_print_defaults still
a555f2
# returns log-error
a555f2
# log-error might be defined in mysqld_safe and mysqld sections,
a555f2
# the former has bigger priority
a555f2
get_mysql_option "$server_sections" log-error "$datadir/`uname -n`.err"
a555f2
errlogfile="$result"
a555f2
a555f2
get_mysql_option "$server_sections" socket "@MYSQL_UNIX_ADDR@"
a555f2
socketfile="$result"
a555f2
a555f2
get_mysql_option "$server_sections" pid-file "$datadir/`uname -n`.pid"
a555f2
pidfile="$result"
a555f2