2 # ctdb event script for Samba
8 case $CTDB_INIT_STYLE in
10 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
11 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
12 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
15 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
16 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
17 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
20 # should not happen, but for now use redhat style as default:
21 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
22 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
23 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
31 ctdb_setup_service_state_dir
35 # If set then we force-start the relevant service.
38 # make sure samba is not already started
39 if [ "$_service_name" = "samba" ] || \
40 is_ctdb_managed_service "samba" ; then
41 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
42 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
43 killall -0 -q smbd && {
45 # make absolutely sure samba is dead
49 killall -0 -q nmbd && {
51 # make absolutely sure samba is dead
56 # make sure winbind is not already started
57 if [ "$_service_name" = "winbind" ] || \
58 check_ctdb_manages_winbind ; then
59 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
60 killall -0 -q winbindd && {
62 # make absolutely sure winbindd is dead
63 killall -q -9 winbindd
68 # start the winbind service
69 if [ "$_service_name" = "winbind" ] || \
70 check_ctdb_manages_winbind ; then
71 service "$CTDB_SERVICE_WINBIND" start || {
72 echo failed to start winbind
77 # start Samba service. Start it reniced, as under very heavy load
78 # the number of smbd processes will mean that it leaves few cycles for
80 if [ "$_service_name" = "samba" ] || \
81 is_ctdb_managed_service "samba" ; then
84 nice_service "$CTDB_SERVICE_NMB" start || {
85 echo failed to start nmbd
88 nice_service "$CTDB_SERVICE_SMB" start || {
89 echo failed to start samba
97 # If set then we force-stop the relevant service.
100 # shutdown Samba when ctdb goes down
101 if [ "$_service_name" = "samba" ] || \
102 is_ctdb_managed_service "samba" ; then
103 service "$CTDB_SERVICE_SMB" stop
104 service "$CTDB_SERVICE_NMB" stop
107 # stop the winbind service
108 if [ "$_service_name" = "winbind" ] || \
109 check_ctdb_manages_winbind ; then
110 service "$CTDB_SERVICE_WINBIND" stop
116 service_reconfigure ()
118 # Samba automatically reloads config - no restart needed.
122 # set default samba cleanup period - in minutes
123 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
124 SAMBA_CLEANUP_PERIOD=10
127 # we keep a cached copy of smb.conf here
128 smbconf_cache="$service_state_dir/smb.conf.cache"
131 #############################################
132 # update the smb.conf cache in the foreground
133 testparm_foreground_update() {
134 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
137 #############################################
138 # update the smb.conf cache in the background
139 testparm_background_update() {
140 # if the cache doesn't exist, then update in the foreground
141 [ -f $smbconf_cache ] || {
142 testparm_foreground_update
144 # otherwise do a background update
146 tmpfile="${smbconf_cache}.$$"
147 testparm -s > $tmpfile 2> /dev/null &
148 # remember the pid of the teamparm process
150 # give it 10 seconds to run
152 while [ $timeleft -gt 0 ]; do
153 timeleft=$(($timeleft - 1))
154 # see if the process still exists
155 kill -0 $pid > /dev/null 2>&1 || {
156 # it doesn't exist, grab its exit status
159 echo "50.samba: smb.conf background update exited with status $?"
163 # put the new smb.conf contents in the cache (atomic rename)
164 # make sure we remove references to the registry while doing
165 # this to ensure that running testparm on the cache does
166 # not use the registry
167 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
169 mv -f "${tmpfile}.2" "$smbconf_cache" || {
170 echo "50.samba: failed to update background cache"
176 # keep waiting for testparm to finish
179 # it took more than 10 seconds - kill it off
181 kill -9 "$pid" > /dev/null 2>&1
182 echo "50.samba: timed out updating smbconf cache in background"
187 ##################################################
188 # show the testparm output using a cached smb.conf
189 # to avoid registry access
191 [ -f $smbconf_cache ] || {
192 testparm_foreground_update
194 testparm -s "$smbconf_cache" "$@" 2>/dev/null
197 # function to see if ctdb manages winbind - this overrides with extra
198 # logic if $CTDB_MANAGES_WINBIND is not set or null.
199 check_ctdb_manages_winbind() {
200 if is_ctdb_managed_service "winbind" ; then
202 elif [ -n "$CTDB_MANAGES_WINBIND" ] ; then
203 # If this variable is set we want to respect it. We return
204 # false here because we know it is not set to "yes" - if it
205 # were then the 1st "if" above would have succeeded.
208 _secmode=`testparm_cat --parameter-name=security`
223 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
228 ###########################
229 # periodic cleanup function
231 # running smbstatus scrubs any dead entries from the connections
232 # and sessionid database
233 # echo "Running periodic cleanup of samba databases"
234 smbstatus -np > /dev/null 2>&1 &
237 ###########################
239 ctdb_start_stop_service
240 ctdb_start_stop_service "winbind"
242 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
244 ###########################
256 # Create a dummy file to track when we need to do periodic cleanup
258 periodic_cleanup_file="$service_state_dir/periodic_cleanup"
259 [ -f "$periodic_cleanup_file" ] || {
260 touch "$periodic_cleanup_file"
262 [ `find "$periodic_cleanup_file" -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
263 # Cleanup the databases
265 touch "$periodic_cleanup_file"
268 is_ctdb_managed_service "samba" && {
269 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
270 testparm_background_update
272 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
273 testparm_foreground_update
274 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
275 echo "ERROR: testparm shows smb.conf is not clean"
281 ctdb_check_directories_probe || {
282 testparm_foreground_update
284 ctdb_check_directories
288 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
289 [ -z "$smb_ports" ] && {
290 smb_ports=`testparm_cat --parameter-name="smb ports"`
292 ctdb_check_tcp_ports $smb_ports || exit $?
295 # check winbind is OK
296 check_ctdb_manages_winbind && {
297 ctdb_check_command "winbind" "wbinfo -p"
306 smbcontrol winbindd ip-dropped $ip >/dev/null 2>/dev/null
309 ctdb_standard_event_handler "$@"