ctdb-scripts: Event script indentation and whitespace cleanups
[samba.git] / ctdb / config / events.d / 50.samba
1 #!/bin/sh
2 # ctdb event script for Samba
3
4 [ -n "$CTDB_BASE" ] || \
5     CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
6
7 . "${CTDB_BASE}/functions"
8
9 detect_init_style
10
11 case $CTDB_INIT_STYLE in
12         suse)
13                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
14                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
15                 ;;
16         debian)
17                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
18                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
19                 ;;
20         *)
21                 # Use redhat style as default:
22                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
23                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
24                 ;;
25 esac
26
27 service_name="samba"
28
29 loadconfig
30
31 ctdb_setup_service_state_dir
32
33 service_start ()
34 {
35     # make sure samba is not already started
36     service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
37     if [ -n "$CTDB_SERVICE_NMB" ] ; then
38         service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
39     fi
40     killall -0 -q smbd && {
41         sleep 1
42         # make absolutely sure samba is dead
43         killall -q -9 smbd
44     }
45     killall -0 -q nmbd && {
46         sleep 1
47         # make absolutely sure samba is dead
48         killall -q -9 nmbd
49     }
50
51     # start Samba service. Start it reniced, as under very heavy load
52     # the number of smbd processes will mean that it leaves few cycles
53     # for anything else
54     net serverid wipe
55
56     if [ -n "$CTDB_SERVICE_NMB" ] ; then
57         nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
58     fi
59
60     nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
61 }
62
63 service_stop ()
64 {
65     service "$CTDB_SERVICE_SMB" stop
66     if [ -n "$CTDB_SERVICE_NMB" ] ; then
67         service "$CTDB_SERVICE_NMB" stop
68     fi
69 }
70
71 ######################################################################
72 # Show the testparm output using a cached smb.conf to avoid delays due
73 # to registry access.
74
75 smbconf_cache="$service_state_dir/smb.conf.cache"
76
77 testparm_foreground_update ()
78 {
79     _timeout="$1"
80
81     # No need to remove these temporary files, since there are only 2
82     # of them.
83     _out="${smbconf_cache}.out"
84     _err="${smbconf_cache}.err"
85
86     timeout "$_timeout" testparm -v -s >"$_out" 2>"$_err"
87     case $? in
88         0) : ;;
89         124)
90             if [ -f "$smbconf_cache" ] ; then
91                 echo "WARNING: smb.conf cache update timed out - using old cache file"
92                 return 1
93             else
94                 echo "ERROR: smb.conf cache create failed - testparm command timed out"
95                 exit 1
96             fi
97             ;;
98         *)
99             if [ -f "$smbconf_cache" ] ; then
100                 echo "WARNING: smb.conf cache update failed - using old cache file"
101                 cat "$_err"
102                 return 1
103             else
104                 echo "ERROR: smb.conf cache create failed - testparm failed with:"
105                 cat "$_err"
106                 exit 1
107             fi
108     esac
109
110     # Only using $$ here to avoid a collision.  This is written into
111     # CTDB's own state directory so there is no real need for a secure
112     # temporary file.
113     _tmpfile="${smbconf_cache}.$$"
114     # Patterns to exclude...
115     _pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='
116     grep -Ev "$_pat" <"$_out" >"$_tmpfile"
117     mv "$_tmpfile" "$smbconf_cache" # atomic
118
119     return 0
120 }
121
122 testparm_background_update ()
123 {
124     _timeout="$1"
125
126     testparm_foreground_update "$_timeout" >/dev/null 2>&1 </dev/null &
127 }
128
129 testparm_cat ()
130 {
131     testparm -s "$smbconf_cache" "$@" 2>/dev/null
132 }
133
134 list_samba_shares ()
135 {
136     testparm_cat |
137     sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
138     sed -e 's/"//g'
139 }
140
141 list_samba_ports ()
142 {
143     testparm_cat --parameter-name="smb ports" |
144     sed -e 's@,@ @g'
145 }
146
147 ###########################
148
149 ctdb_start_stop_service
150
151 is_ctdb_managed_service || exit 0
152
153 ###########################
154
155 case "$1" in
156 startup)
157         ctdb_service_start
158         ;;
159
160 shutdown)
161         ctdb_service_stop
162         ;;
163
164 monitor)
165         testparm_foreground_update 10
166         ret=$?
167
168         smb_ports="$CTDB_SAMBA_CHECK_PORTS"
169         if [ -z "$smb_ports" ] ; then
170             smb_ports=$(list_samba_ports)
171             [ -n "$smb_ports" ] || die "Failed to set smb ports"
172         fi
173         ctdb_check_tcp_ports $smb_ports || exit $?
174
175         if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
176             list_samba_shares | ctdb_check_directories || exit $?
177         fi
178
179         if [ $ret -ne 0 ] ; then
180             testparm_background_update 10
181         fi
182         ;;
183 esac
184
185 exit 0