summaryrefslogtreecommitdiff
path: root/ctdb/config/events/legacy/50.samba.script
blob: 44ae0a4e63b91b73761c59627943d90ec2eba2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
# ctdb event script for Samba

[ -n "$CTDB_BASE" ] ||
	CTDB_BASE=$(d=$(dirname "$0") && cd -P "$d" && dirname "$PWD")

. "${CTDB_BASE}/functions"

detect_init_style

case $CTDB_INIT_STYLE in
suse)
	CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
	;;
debian)
	CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smbd}
	;;
*)
	# Use redhat style as default:
	CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
	;;
esac

service_name="samba"

load_script_options

ctdb_setup_state_dir "service" "$service_name"

service_start()
{
	# make sure samba is not already started
	service "$CTDB_SERVICE_SMB" stop >/dev/null 2>&1
	killall -0 -q smbd && {
		sleep 1
		# make absolutely sure samba is dead
		killall -q -9 smbd
	}
	# 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
	nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
}

service_stop()
{
	service "$CTDB_SERVICE_SMB" stop
	program_stack_traces "smbd" 5
}

######################################################################
# Show the testparm output using a cached smb.conf to avoid delays due
# to registry access.

# script_state_dir set by ctdb_setup_state_dir()
# shellcheck disable=SC2154
smbconf_cache="$script_state_dir/smb.conf.cache"

testparm_foreground_update()
{
	_timeout="$1"

	# 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.
	_tmpfile="${smbconf_cache}.$$"
	# Patterns to exclude...
	_pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='
	grep -Ev "$_pat" <"$_out" >"$_tmpfile"
	mv "$_tmpfile" "$smbconf_cache" # atomic

	return 0
}

testparm_background_update()
{
	_timeout="$1"

	testparm_foreground_update "$_timeout" >/dev/null 2>&1 </dev/null &
}

testparm_cat()
{
	testparm -s "$smbconf_cache" "$@" 2>/dev/null
}

list_samba_shares()
{
	testparm_cat |
		sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
		sed -e 's/"//g'
}

list_samba_ports()
{
	testparm_cat --parameter-name="smb ports"
}

###########################

case "$1" in
startup)
	service_start
	;;

shutdown)
	service_stop
	;;

monitor)
	testparm_foreground_update 10
	ret=$?

	smb_ports="$CTDB_SAMBA_CHECK_PORTS"
	if [ -z "$smb_ports" ]; then
		smb_ports=$(list_samba_ports)
		[ -n "$smb_ports" ] || die "Failed to set smb ports"
	fi
	# Intentionally unquoted multi-word value here
	# shellcheck disable=SC2086
	ctdb_check_tcp_ports $smb_ports || exit $?

	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
	;;
esac

exit 0