ctdb-scripts: Move event scripts to events/legacy/ directory
[kai/samba-autobuild/.git] / ctdb / config / events / legacy / 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:-smbd}
18                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmbd}
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 load_script_options
30
31 ctdb_setup_state_dir "service" "$service_name"
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     if [ -n "$CTDB_SERVICE_NMB" ] ; then
55         nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
56     fi
57
58     nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
59 }
60
61 service_stop ()
62 {
63     service "$CTDB_SERVICE_SMB" stop
64     program_stack_traces "smbd" 5
65     if [ -n "$CTDB_SERVICE_NMB" ] ; then
66         service "$CTDB_SERVICE_NMB" stop
67     fi
68 }
69
70 ######################################################################
71 # Show the testparm output using a cached smb.conf to avoid delays due
72 # to registry access.
73
74 # script_state_dir set by ctdb_setup_state_dir()
75 # shellcheck disable=SC2154
76 smbconf_cache="$script_state_dir/smb.conf.cache"
77
78 testparm_foreground_update ()
79 {
80     _timeout="$1"
81
82     # No need to remove these temporary files, since there are only 2
83     # of them.
84     _out="${smbconf_cache}.out"
85     _err="${smbconf_cache}.err"
86
87     timeout "$_timeout" testparm -v -s >"$_out" 2>"$_err"
88     case $? in
89         0) : ;;
90         124)
91             if [ -f "$smbconf_cache" ] ; then
92                 echo "WARNING: smb.conf cache update timed out - using old cache file"
93                 return 1
94             else
95                 echo "ERROR: smb.conf cache create failed - testparm command timed out"
96                 exit 1
97             fi
98             ;;
99         *)
100             if [ -f "$smbconf_cache" ] ; then
101                 echo "WARNING: smb.conf cache update failed - using old cache file"
102                 cat "$_err"
103                 return 1
104             else
105                 echo "ERROR: smb.conf cache create failed - testparm failed with:"
106                 cat "$_err"
107                 exit 1
108             fi
109     esac
110
111     # Only using $$ here to avoid a collision.  This is written into
112     # CTDB's own state directory so there is no real need for a secure
113     # temporary file.
114     _tmpfile="${smbconf_cache}.$$"
115     # Patterns to exclude...
116     _pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='
117     grep -Ev "$_pat" <"$_out" >"$_tmpfile"
118     mv "$_tmpfile" "$smbconf_cache" # atomic
119
120     return 0
121 }
122
123 testparm_background_update ()
124 {
125     _timeout="$1"
126
127     testparm_foreground_update "$_timeout" >/dev/null 2>&1 </dev/null &
128 }
129
130 testparm_cat ()
131 {
132     testparm -s "$smbconf_cache" "$@" 2>/dev/null
133 }
134
135 list_samba_shares ()
136 {
137     testparm_cat |
138     sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
139     sed -e 's/"//g'
140 }
141
142 list_samba_ports ()
143 {
144     testparm_cat --parameter-name="smb ports" |
145     sed -e 's@,@ @g'
146 }
147
148 ###########################
149
150 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
151
152 ###########################
153
154 case "$1" in
155 startup)
156         service_start
157         ;;
158
159 shutdown)
160         service_stop
161         ;;
162
163 monitor)
164         testparm_foreground_update 10
165         ret=$?
166
167         smb_ports="$CTDB_SAMBA_CHECK_PORTS"
168         if [ -z "$smb_ports" ] ; then
169             smb_ports=$(list_samba_ports)
170             [ -n "$smb_ports" ] || die "Failed to set smb ports"
171         fi
172         # Intentionally unquoted multi-word value here
173         # shellcheck disable=SC2086
174         ctdb_check_tcp_ports $smb_ports || exit $?
175
176         if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
177             list_samba_shares | ctdb_check_directories || exit $?
178         fi
179
180         if [ $ret -ne 0 ] ; then
181             testparm_background_update 10
182         fi
183         ;;
184 esac
185
186 exit 0