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 # set default samba cleanup period - in minutes
117 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
118 SAMBA_CLEANUP_PERIOD=10
121 # we keep a cached copy of smb.conf here
122 smbconf_cache="$service_state_dir/smb.conf.cache"
125 #############################################
126 # update the smb.conf cache in the foreground
127 testparm_foreground_update() {
128 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
131 #############################################
132 # update the smb.conf cache in the background
133 testparm_background_update() {
134 # if the cache doesn't exist, then update in the foreground
135 [ -f $smbconf_cache ] || {
136 testparm_foreground_update
138 # otherwise do a background update
140 tmpfile="${smbconf_cache}.$$"
141 testparm -s > $tmpfile 2> /dev/null &
142 # remember the pid of the teamparm process
144 # give it 10 seconds to run
146 while [ $timeleft -gt 0 ]; do
147 timeleft=$(($timeleft - 1))
148 # see if the process still exists
149 kill -0 $pid > /dev/null 2>&1 || {
150 # it doesn't exist, grab its exit status
153 echo "50.samba: smb.conf background update exited with status $?"
157 # put the new smb.conf contents in the cache (atomic rename)
158 # make sure we remove references to the registry while doing
159 # this to ensure that running testparm on the cache does
160 # not use the registry
161 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
163 mv -f "${tmpfile}.2" "$smbconf_cache" || {
164 echo "50.samba: failed to update background cache"
170 # keep waiting for testparm to finish
173 # it took more than 10 seconds - kill it off
175 kill -9 "$pid" > /dev/null 2>&1
176 echo "50.samba: timed out updating smbconf cache in background"
181 ##################################################
182 # show the testparm output using a cached smb.conf
183 # to avoid registry access
185 [ -f $smbconf_cache ] || {
186 testparm_foreground_update
188 testparm -s "$smbconf_cache" "$@" 2>/dev/null
191 # function to see if ctdb manages winbind - this overrides with extra
192 # logic if $CTDB_MANAGES_WINBIND is not set or null.
193 check_ctdb_manages_winbind() {
194 if is_ctdb_managed_service "winbind" ; then
196 elif [ -n "$CTDB_MANAGES_WINBIND" ] ; then
197 # If this variable is set we want to respect it. We return
198 # false here because we know it is not set to "yes" - if it
199 # were then the 1st "if" above would have succeeded.
202 _secmode=`testparm_cat --parameter-name=security`
217 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
222 ###########################
223 # periodic cleanup function
225 # running smbstatus scrubs any dead entries from the connections
226 # and sessionid database
227 # echo "Running periodic cleanup of samba databases"
228 smbstatus -np > /dev/null 2>&1 &
231 ###########################
233 ctdb_start_stop_service
234 ctdb_start_stop_service "winbind"
236 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
238 ###########################
250 # Create a dummy file to track when we need to do periodic cleanup
252 periodic_cleanup_file="$service_state_dir/periodic_cleanup"
253 [ -f "$periodic_cleanup_file" ] || {
254 touch "$periodic_cleanup_file"
256 [ `find "$periodic_cleanup_file" -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
257 # Cleanup the databases
259 touch "$periodic_cleanup_file"
262 is_ctdb_managed_service "samba" && {
263 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
264 testparm_background_update
266 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
267 testparm_foreground_update
268 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
269 echo "ERROR: testparm shows smb.conf is not clean"
275 ctdb_check_directories_probe || {
276 testparm_foreground_update
278 ctdb_check_directories
282 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
283 [ -z "$smb_ports" ] && {
284 smb_ports=`testparm_cat --parameter-name="smb ports"`
286 ctdb_check_tcp_ports $smb_ports || exit $?
289 # check winbind is OK
290 check_ctdb_manages_winbind && {
291 ctdb_check_command "winbind" "wbinfo -p"
300 smbcontrol winbindd ip-dropped $ip >/dev/null 2>/dev/null
303 ctdb_standard_event_handler "$@"