2007-06-01 14:54:26 +04:00
#!/bin/sh
# ctdb event script for Samba
2013-01-03 08:26:12 +04:00
[ -n "$CTDB_BASE" ] || \
2016-06-29 10:36:05 +03:00
CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
2013-01-03 08:26:12 +04:00
2016-06-29 10:36:05 +03:00
. "${CTDB_BASE}/functions"
2007-06-01 14:54:26 +04:00
2009-01-16 15:33:13 +03:00
detect_init_style
case $CTDB_INIT_STYLE in
suse)
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
2009-01-16 15:33:13 +03:00
;;
2009-09-15 13:33:35 +04:00
debian)
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
2009-01-16 15:33:13 +03:00
;;
*)
2012-08-16 08:41:11 +04:00
# Use redhat style as default:
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
2009-01-16 15:33:13 +03:00
;;
esac
2009-11-19 08:48:19 +03:00
service_name="samba"
loadconfig
2007-06-01 14:54:26 +04:00
2010-12-17 08:29:21 +03:00
ctdb_setup_service_state_dir
2011-08-11 03:39:25 +04:00
service_start ()
{
2012-08-16 08:41:11 +04:00
# make sure samba is not already started
service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
if [ -n "$CTDB_SERVICE_NMB" ] ; then
service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
fi
killall -0 -q smbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 smbd
}
killall -0 -q nmbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 nmbd
}
2010-03-26 19:33:51 +03:00
2012-08-16 08:41:11 +04:00
# start Samba service. Start it reniced, as under very heavy load
# the number of smbd processes will mean that it leaves few cycles
# for anything else
net serverid wipe
2010-03-26 19:33:51 +03:00
2012-08-16 08:41:11 +04:00
if [ -n "$CTDB_SERVICE_NMB" ] ; then
nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
fi
2009-11-19 08:48:19 +03:00
2012-08-16 08:41:11 +04:00
nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
2009-11-19 08:48:19 +03:00
}
2011-08-11 03:39:25 +04:00
service_stop ()
{
2012-08-16 08:41:11 +04:00
service "$CTDB_SERVICE_SMB" stop
if [ -n "$CTDB_SERVICE_NMB" ] ; then
service "$CTDB_SERVICE_NMB" stop
fi
2009-11-19 08:48:19 +03:00
}
2007-06-02 12:51:05 +04:00
2013-10-28 09:00:54 +04:00
######################################################################
# Show the testparm output using a cached smb.conf to avoid delays due
# to registry access.
2010-12-17 08:29:21 +03:00
smbconf_cache="$service_state_dir/smb.conf.cache"
2008-07-23 09:36:23 +04:00
2013-10-28 09:00:54 +04:00
testparm_foreground_update ()
{
_timeout="$1"
2015-07-30 09:49:35 +03:00
# No need to remove these temporary files, since there are only 2
# of them.
_out="${smbconf_cache}.out"
_err="${smbconf_cache}.err"
timeout $_timeout testparm -v -s >"$_out" 2>"$_err"
case $? in
0) : ;;
124)
if [ -f "$smbconf_cache" ] ; then
echo "WARNING: smb.conf cache update timed out - using old cache file"
return 1
else
echo "ERROR: smb.conf cache create failed - testparm command timed out"
exit 1
fi
;;
*)
if [ -f "$smbconf_cache" ] ; then
echo "WARNING: smb.conf cache update failed - using old cache file"
cat "$_err"
return 1
else
echo "ERROR: smb.conf cache create failed - testparm failed with:"
cat "$_err"
exit 1
fi
esac
# Only using $$ here to avoid a collision. This is written into
# CTDB's own state directory so there is no real need for a secure
# temporary file.
2013-10-28 09:00:54 +04:00
_tmpfile="${smbconf_cache}.$$"
# Patterns to exclude...
2015-07-30 09:49:35 +03:00
_pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='
grep -Ev "$_pat" <"$_out" >"$_tmpfile"
2013-10-28 09:00:54 +04:00
mv "$_tmpfile" "$smbconf_cache" # atomic
2008-07-23 09:36:23 +04:00
2013-10-28 09:00:54 +04:00
return 0
2008-07-23 09:36:23 +04:00
}
2013-10-28 09:00:54 +04:00
testparm_background_update ()
{
_timeout="$1"
testparm_foreground_update $_timeout >/dev/null 2>&1 </dev/null &
2008-07-23 09:36:23 +04:00
}
2013-10-28 09:00:54 +04:00
testparm_cat ()
{
testparm -s "$smbconf_cache" "$@" 2>/dev/null
2007-11-18 07:14:54 +03:00
}
2009-11-20 08:45:36 +03:00
list_samba_shares ()
{
testparm_cat |
sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
sed -e 's/"//g'
}
2013-10-28 09:00:54 +04:00
list_samba_ports ()
{
testparm_cat --parameter-name="smb ports" |
sed -e 's@,@ @g'
}
2009-11-20 08:45:36 +03:00
2010-11-18 03:04:52 +03:00
###########################
ctdb_start_stop_service
2012-08-16 08:41:11 +04:00
is_ctdb_managed_service || exit 0
2010-11-18 03:04:52 +03:00
###########################
2013-02-11 04:25:49 +04:00
case "$1" in
2007-06-01 14:54:26 +04:00
startup)
2009-11-19 08:48:19 +03:00
ctdb_service_start
2007-06-01 14:54:26 +04:00
;;
2013-02-11 04:25:49 +04:00
2007-06-01 14:54:26 +04:00
shutdown)
2009-11-19 08:48:19 +03:00
ctdb_service_stop
2007-06-01 14:54:26 +04:00
;;
2007-06-06 06:08:42 +04:00
monitor)
2013-10-28 09:00:54 +04:00
testparm_foreground_update 10
ret=$?
2007-06-17 06:05:29 +04:00
2012-08-16 08:41:11 +04:00
smb_ports="$CTDB_SAMBA_CHECK_PORTS"
if [ -z "$smb_ports" ] ; then
2013-10-28 09:00:54 +04:00
smb_ports=$(list_samba_ports)
[ -n "$smb_ports" ] || die "Failed to set smb ports"
2012-08-16 08:41:11 +04:00
fi
ctdb_check_tcp_ports $smb_ports || exit $?
2013-10-28 09:00:54 +04:00
if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
list_samba_shares | ctdb_check_directories || exit $?
fi
if [ $ret -ne 0 ] ; then
testparm_background_update 10
fi
2007-06-06 06:08:42 +04:00
;;
2009-12-01 09:43:47 +03:00
*)
ctdb_standard_event_handler "$@"
2009-11-19 08:48:19 +03:00
;;
2007-06-01 14:54:26 +04:00
esac
exit 0