Initial import
[samba] / source / smbd / notify_kernel.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    change notify handling - linux kernel based implementation
5    Copyright (C) Andrew Tridgell 2000
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #if HAVE_KERNEL_CHANGE_NOTIFY
25
26 #define FD_PENDING_SIZE 20
27 static SIG_ATOMIC_T fd_pending_array[FD_PENDING_SIZE];
28 static SIG_ATOMIC_T signals_received;
29
30 #ifndef DN_ACCESS
31 #define DN_ACCESS       0x00000001      /* File accessed in directory */
32 #define DN_MODIFY       0x00000002      /* File modified in directory */
33 #define DN_CREATE       0x00000004      /* File created in directory */
34 #define DN_DELETE       0x00000008      /* File removed from directory */
35 #define DN_RENAME       0x00000010      /* File renamed in directory */
36 #define DN_ATTRIB       0x00000020      /* File changed attribute */
37 #define DN_MULTISHOT    0x80000000      /* Don't remove notifier */
38 #endif
39
40
41 #ifndef RT_SIGNAL_NOTIFY
42 #define RT_SIGNAL_NOTIFY (SIGRTMIN+2)
43 #endif
44
45 #ifndef F_SETSIG
46 #define F_SETSIG 10
47 #endif
48
49 #ifndef F_NOTIFY
50 #define F_NOTIFY 1026
51 #endif
52
53 /****************************************************************************
54  This is the structure to keep the information needed to
55  determine if a directory has changed.
56 *****************************************************************************/
57
58 struct change_data {
59         int directory_handle;
60 };
61
62 /****************************************************************************
63  The signal handler for change notify.
64  The Linux kernel has a bug in that we should be able to block any
65  further delivery of RT signals until the kernel_check_notify() function
66  unblocks them, but it seems that any signal mask we're setting here is
67  being overwritten on exit from this handler. I should create a standalone
68  test case for the kernel hackers. JRA.
69 *****************************************************************************/
70
71 static void signal_handler(int sig, siginfo_t *info, void *unused)
72 {
73         if (signals_received < FD_PENDING_SIZE - 1) {
74                 fd_pending_array[signals_received] = (SIG_ATOMIC_T)info->si_fd;
75                 signals_received++;
76         } /* Else signal is lost. */
77         sys_select_signal(RT_SIGNAL_NOTIFY);
78 }
79
80 /****************************************************************************
81  Check if a change notify should be issued.
82  time non-zero means timeout check (used for hash). Ignore this (async method
83  where time is zero will be used instead).
84 *****************************************************************************/
85
86 static BOOL kernel_check_notify(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *datap, time_t t)
87 {
88         struct change_data *data = (struct change_data *)datap;
89         int i;
90         BOOL ret = False;
91
92         if (t)
93                 return False;
94
95         BlockSignals(True, RT_SIGNAL_NOTIFY);
96         for (i = 0; i < signals_received; i++) {
97                 if (data->directory_handle == (int)fd_pending_array[i]) {
98                         DEBUG(3,("kernel_check_notify: kernel change notify on %s fd[%d]=%d (signals_received=%d)\n",
99                                                 path, i, (int)fd_pending_array[i], (int)signals_received ));
100
101                         close((int)fd_pending_array[i]);
102                         fd_pending_array[i] = (SIG_ATOMIC_T)-1;
103                         if (signals_received - i - 1) {
104                                 memmove((void *)&fd_pending_array[i], (void *)&fd_pending_array[i+1],
105                                                 sizeof(SIG_ATOMIC_T)*(signals_received-i-1));
106                         }
107                         data->directory_handle = -1;
108                         signals_received--;
109                         ret = True;
110                         break;
111                 }
112         }
113         BlockSignals(False, RT_SIGNAL_NOTIFY);
114         return ret;
115 }
116
117 /****************************************************************************
118  Remove a change notify data structure.
119 *****************************************************************************/
120
121 static void kernel_remove_notify(void *datap)
122 {
123         struct change_data *data = (struct change_data *)datap;
124         int fd = data->directory_handle;
125         if (fd != -1) {
126                 int i;
127                 BlockSignals(True, RT_SIGNAL_NOTIFY);
128                 for (i = 0; i < signals_received; i++) {
129                         if (fd == (int)fd_pending_array[i]) {
130                                 fd_pending_array[i] = (SIG_ATOMIC_T)-1;
131                                 if (signals_received - i - 1) {
132                                         memmove((void *)&fd_pending_array[i], (void *)&fd_pending_array[i+1],
133                                                         sizeof(SIG_ATOMIC_T)*(signals_received-i-1));
134                                 }
135                                 data->directory_handle = -1;
136                                 signals_received--;
137                                 break;
138                         }
139                 }
140                 close(fd);
141                 BlockSignals(False, RT_SIGNAL_NOTIFY);
142         }
143         SAFE_FREE(data);
144         DEBUG(3,("kernel_remove_notify: fd=%d\n", fd));
145 }
146
147 /****************************************************************************
148  Register a change notify request.
149 *****************************************************************************/
150
151 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
152 {
153         struct change_data data;
154         int fd;
155         unsigned long kernel_flags;
156         
157         fd = sys_open(path,O_RDONLY, 0);
158
159         if (fd == -1) {
160                 DEBUG(3,("Failed to open directory %s for change notify\n", path));
161                 return NULL;
162         }
163
164         if (sys_fcntl_long(fd, F_SETSIG, RT_SIGNAL_NOTIFY) == -1) {
165                 DEBUG(3,("Failed to set signal handler for change notify\n"));
166                 return NULL;
167         }
168
169         kernel_flags = DN_CREATE|DN_DELETE|DN_RENAME; /* creation/deletion changes everything! */
170         if (flags & FILE_NOTIFY_CHANGE_FILE)        kernel_flags |= DN_MODIFY;
171         if (flags & FILE_NOTIFY_CHANGE_DIR_NAME)    kernel_flags |= DN_RENAME|DN_DELETE;
172         if (flags & FILE_NOTIFY_CHANGE_ATTRIBUTES)  kernel_flags |= DN_ATTRIB;
173         if (flags & FILE_NOTIFY_CHANGE_SIZE)        kernel_flags |= DN_MODIFY;
174         if (flags & FILE_NOTIFY_CHANGE_LAST_WRITE)  kernel_flags |= DN_MODIFY;
175         if (flags & FILE_NOTIFY_CHANGE_LAST_ACCESS) kernel_flags |= DN_ACCESS;
176         if (flags & FILE_NOTIFY_CHANGE_CREATION)    kernel_flags |= DN_CREATE;
177         if (flags & FILE_NOTIFY_CHANGE_SECURITY)    kernel_flags |= DN_ATTRIB;
178         if (flags & FILE_NOTIFY_CHANGE_EA)          kernel_flags |= DN_ATTRIB;
179         if (flags & FILE_NOTIFY_CHANGE_FILE_NAME)   kernel_flags |= DN_RENAME|DN_DELETE;
180
181         if (sys_fcntl_long(fd, F_NOTIFY, kernel_flags) == -1) {
182                 DEBUG(3,("Failed to set async flag for change notify\n"));
183                 return NULL;
184         }
185
186         data.directory_handle = fd;
187
188         DEBUG(3,("kernel change notify on %s (ntflags=0x%x flags=0x%x) fd=%d\n", 
189                  path, (int)flags, (int)kernel_flags, fd));
190
191         return (void *)memdup(&data, sizeof(data));
192 }
193
194 /****************************************************************************
195  See if the kernel supports change notify.
196 ****************************************************************************/
197
198 static BOOL kernel_notify_available(void) 
199 {
200         int fd, ret;
201         fd = open("/tmp", O_RDONLY);
202         if (fd == -1)
203                 return False; /* uggh! */
204         ret = sys_fcntl_long(fd, F_NOTIFY, 0);
205         close(fd);
206         return ret == 0;
207 }
208
209 /****************************************************************************
210  Setup kernel based change notify.
211 ****************************************************************************/
212
213 struct cnotify_fns *kernel_notify_init(void) 
214 {
215         static struct cnotify_fns cnotify;
216         struct sigaction act;
217
218         ZERO_STRUCT(act);
219
220         act.sa_sigaction = signal_handler;
221         act.sa_flags = SA_SIGINFO;
222         sigemptyset( &act.sa_mask );
223         if (sigaction(RT_SIGNAL_NOTIFY, &act, NULL) != 0) {
224                 DEBUG(0,("Failed to setup RT_SIGNAL_NOTIFY handler\n"));
225                 return NULL;
226         }
227
228         if (!kernel_notify_available())
229                 return NULL;
230
231         cnotify.register_notify = kernel_register_notify;
232         cnotify.check_notify = kernel_check_notify;
233         cnotify.remove_notify = kernel_remove_notify;
234         cnotify.select_time = -1;
235
236         /* the signal can start off blocked due to a bug in bash */
237         BlockSignals(False, RT_SIGNAL_NOTIFY);
238
239         return &cnotify;
240 }
241
242 #else
243  void notify_kernel_dummy(void);
244
245  void notify_kernel_dummy(void) {}
246 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */