Fix the chicken and egg problem with ctdb/samba and a registry smb.conf
[metze/ctdb/wip.git] / config / events.d / 50.samba
1 #!/bin/sh
2 # ctdb event script for Samba
3
4 PATH=/bin:/usr/bin:$PATH
5
6 . $CTDB_BASE/functions
7 loadconfig ctdb
8
9 cmd="$1"
10 shift
11
12 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
13
14 # set default samba cleanup period - in minutes
15 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
16     SAMBA_CLEANUP_PERIOD=10
17 }
18
19 # function to see if ctdb manages winbind
20 check_ctdb_manages_winbind() {
21   [ -z "$CTDB_MANAGES_WINBIND" ] && {
22     secmode=`testparm -s --parameter-name=security 2> /dev/null`
23     case $secmode in
24         ADS|DOMAIN)
25             CTDB_MANAGES_WINBIND="yes";
26             ;;
27         *)
28             CTDB_MANAGES_WINBIND="no";
29             ;;
30     esac
31   }
32 }
33
34 ###########################
35 # periodic cleanup function
36 periodic_cleanup() {
37     # running smbstatus scrubs any dead entries from the connections
38     # and sessionid database
39     echo "Running periodic cleanup of samba databases"
40     smbstatus -n > /dev/null 2>&1 &
41 }
42
43 case $cmd in 
44      startup)
45         # create the state directory for samba
46         /bin/mkdir -p $CTDB_BASE/state/samba
47
48         # make sure samba is not already started
49         service smb stop > /dev/null 2>&1
50         killall -0 -q smbd && {
51             sleep 1
52             # make absolutely sure samba is dead
53             killall -q -9 smbd
54         }
55
56         # restart the winbind service
57         check_ctdb_manages_winbind
58         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
59                 service winbind stop > /dev/null 2>&1
60                 killall -0 -q winbindd && {
61                     sleep 1
62                     # make absolutely sure winbindd is dead
63                     killall -q -9 winbindd
64                 }
65                 service winbind start
66         }
67
68         # start Samba service. Start it reniced, as under very heavy load 
69         # the number of smbd processes will mean that it leaves few cycles for
70         # anything else
71         nice_service smb start
72         ;;
73         
74      takeip)
75         # nothing special for Samba
76         ;;
77
78      releaseip)
79         # nothing special for Samba
80         ;;
81
82      recovered)
83         # nothing special for Samba
84         exit 0
85         ;;
86
87      shutdown)
88         # shutdown Samba when ctdb goes down
89         service smb stop
90
91         # stop the winbind service
92         check_ctdb_manages_winbind
93         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
94                 service winbind stop
95         }
96         ;;
97
98      monitor)
99         # Create a dummy file to track when we need to do periodic cleanup
100         # of samba databases
101         [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
102                 touch $CTDB_BASE/state/samba/periodic_cleanup
103         }
104         [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
105                 # Cleanup the databases
106                 periodic_cleanup
107                 touch $CTDB_BASE/state/samba/periodic_cleanup
108         }
109
110         testparm -s 2>&1 | egrep '^WARNING|^ERROR|^Unknown' && {
111             echo "ERROR: testparm shows smb.conf is not clean"
112             exit 1
113         }
114
115         smb_dirs=`testparm -s 2> /dev/null | egrep '^[[:space:]]*path = '  | cut -d= -f2`
116         ctdb_check_directories "Samba" $smb_dirs        
117
118         smb_ports=`testparm -s --parameter-name="smb ports" 2> /dev/null`
119         ctdb_check_tcp_ports "Samba" $smb_ports
120
121         # check winbind is OK
122         check_ctdb_manages_winbind
123         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
124                 ctdb_check_command "winbind" "wbinfo -p"
125         }
126         ;;
127
128 esac
129
130 # ignore unknown commands
131 exit 0