Eventscripts: Change the default reconfigure action to do nothing
[samba.git] / ctdb / config / events.d / 50.samba
1 #!/bin/sh
2 # ctdb event script for Samba
3
4 . $CTDB_BASE/functions
5
6 detect_init_style
7
8 case $CTDB_INIT_STYLE in
9         suse)
10                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
11                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
12                 ;;
13         debian)
14                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
15                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
16                 ;;
17         *)
18                 # Use redhat style as default:
19                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
20                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
21                 ;;
22 esac
23
24 service_name="samba"
25
26 loadconfig
27
28 ctdb_setup_service_state_dir
29
30 service_start ()
31 {
32     # make sure samba is not already started
33     service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
34     if [ -n "$CTDB_SERVICE_NMB" ] ; then
35         service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
36     fi
37     killall -0 -q smbd && {
38         sleep 1
39         # make absolutely sure samba is dead
40         killall -q -9 smbd
41     }
42     killall -0 -q nmbd && {
43         sleep 1
44         # make absolutely sure samba is dead
45         killall -q -9 nmbd
46     }
47
48     # start Samba service. Start it reniced, as under very heavy load
49     # the number of smbd processes will mean that it leaves few cycles
50     # for anything else
51     net serverid wipe
52
53     if [ -n "$CTDB_SERVICE_NMB" ] ; then
54         nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
55     fi
56
57     nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
58 }
59
60 service_stop ()
61 {
62     service "$CTDB_SERVICE_SMB" stop
63     if [ -n "$CTDB_SERVICE_NMB" ] ; then
64         service "$CTDB_SERVICE_NMB" stop
65     fi
66 }
67
68 # set default samba cleanup period - in minutes
69 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
70     SAMBA_CLEANUP_PERIOD=10
71 }
72
73 # we keep a cached copy of smb.conf here
74 smbconf_cache="$service_state_dir/smb.conf.cache"
75
76
77 #############################################
78 # update the smb.conf cache in the foreground
79 testparm_foreground_update() {
80     testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
81 }
82
83 #############################################
84 # update the smb.conf cache in the background
85 testparm_background_update() {
86     # if the cache doesn't exist, then update in the foreground
87     [ -f $smbconf_cache ] || {
88         testparm_foreground_update
89     }
90     # otherwise do a background update
91     (
92         tmpfile="${smbconf_cache}.$$"
93         testparm -s > $tmpfile 2> /dev/null &
94         # remember the pid of the teamparm process
95         pid="$!"
96         # give it 10 seconds to run
97         timeleft=10
98         while [ $timeleft -gt 0 ]; do
99             timeleft=$(($timeleft - 1))
100             # see if the process still exists
101             kill -0 $pid > /dev/null 2>&1 || {
102                 # it doesn't exist, grab its exit status
103                 wait $pid
104                 [ $? = 0 ] || {
105                     echo "50.samba: smb.conf background update exited with status $?"
106                     rm -f "${tmpfile}"
107                     exit 1
108                 }               
109                 # put the new smb.conf contents in the cache (atomic rename)
110                 # make sure we remove references to the registry while doing 
111                 # this to ensure that running testparm on the cache does
112                 # not use the registry
113                 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
114                 rm -f "$tmpfile"
115                 mv -f "${tmpfile}.2" "$smbconf_cache" || {
116                     echo "50.samba: failed to update background cache"
117                     rm -f "${tmpfile}.2"
118                     exit 1
119                 }
120                 exit 0
121             }
122             # keep waiting for testparm to finish
123             sleep 1
124         done
125         # it took more than 10 seconds - kill it off
126         rm -f "${tmpfile}"
127         kill -9 "$pid" > /dev/null 2>&1
128         echo "50.samba: timed out updating smbconf cache in background"
129         exit 1
130     ) &
131 }
132
133 ##################################################
134 # show the testparm output using a cached smb.conf 
135 # to avoid registry access
136 testparm_cat() {
137     [ -f $smbconf_cache ] || {
138         testparm_foreground_update
139     }
140     testparm -v -s "$smbconf_cache" "$@" 2>/dev/null
141 }
142
143 list_samba_shares ()
144 {
145     testparm_cat |
146     sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
147     sed -e 's/"//g'
148 }
149
150
151 ###########################
152 # periodic cleanup function
153 periodic_cleanup() {
154     # running smbstatus scrubs any dead entries from the connections
155     # and sessionid database
156     # echo "Running periodic cleanup of samba databases"
157     smbstatus -np > /dev/null 2>&1 &
158 }
159
160 ###########################
161
162 ctdb_start_stop_service
163
164 is_ctdb_managed_service || exit 0
165
166 ctdb_service_check_reconfigure
167
168 ###########################
169
170 case "$1" in 
171      startup)
172         ctdb_service_start
173         ;;
174         
175      shutdown)
176         ctdb_service_stop
177         ;;
178
179      monitor)
180         # Create a dummy file to track when we need to do periodic cleanup
181         # of samba databases
182         periodic_cleanup_file="$service_state_dir/periodic_cleanup"
183         [ -f "$periodic_cleanup_file" ] || {
184                 touch "$periodic_cleanup_file"
185         }
186         [ `find "$periodic_cleanup_file" -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
187                 # Cleanup the databases
188                 periodic_cleanup
189                 touch "$periodic_cleanup_file"
190         }
191
192         if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
193             testparm_background_update
194
195             testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
196                 testparm_foreground_update
197                 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && \
198                     die "ERROR: testparm shows smb.conf is not clean"
199             }
200                         
201             list_samba_shares | ctdb_check_directories_probe || {
202                 testparm_foreground_update
203                 list_samba_shares |
204                 ctdb_check_directories
205             } || exit $?
206         fi
207
208         smb_ports="$CTDB_SAMBA_CHECK_PORTS"
209         if [ -z "$smb_ports" ] ; then
210             smb_ports=`testparm_cat --parameter-name="smb ports"`
211         fi
212         ctdb_check_tcp_ports $smb_ports || exit $?
213         ;;
214
215     *)
216         ctdb_standard_event_handler "$@"
217         ;;
218 esac
219
220 exit 0