11a5592458fa48692e4ce2db95211633e1e19d0f
[tprouty/samba.git] / 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 extern int DEBUGLEVEL;
27 static VOLATILE sig_atomic_t fd_pending;
28 static VOLATILE sig_atomic_t signals_received;
29 static VOLATILE sig_atomic_t signals_processed;
30
31 #ifndef DN_ACCESS
32 #define DN_ACCESS       0x00000001      /* File accessed in directory */
33 #define DN_MODIFY       0x00000002      /* File modified in directory */
34 #define DN_CREATE       0x00000004      /* File created in directory */
35 #define DN_DELETE       0x00000008      /* File removed from directory */
36 #define DN_RENAME       0x00000010      /* File renamed in directory */
37 #define DN_ATTRIB       0x00000020      /* File changed attribute */
38 #define DN_MULTISHOT    0x80000000      /* Don't remove notifier */
39 #endif
40
41
42 #ifndef RT_SIGNAL_NOTIFY
43 #define RT_SIGNAL_NOTIFY 34
44 #endif
45
46 #ifndef F_SETSIG
47 #define F_SETSIG 10
48 #endif
49
50 #ifndef F_NOTIFY
51 #define F_NOTIFY 1026
52 #endif
53
54 /****************************************************************************
55  This is the structure to keep the information needed to
56  determine if a directory has changed.
57 *****************************************************************************/
58 struct change_data {
59         int directory_handle;
60 };
61
62 /****************************************************************************
63 the signal handler for change notify
64 *****************************************************************************/
65 static void signal_handler(int sig, siginfo_t *info, void *unused)
66 {
67         BlockSignals(True, sig);
68         fd_pending = (sig_atomic_t)info->si_fd;
69         signals_received++;
70         sys_select_signal();
71 }
72
73
74
75 /****************************************************************************
76  Check if a change notify should be issued.
77  time non-zero means timeout check (used for hash). Ignore this (async method
78  where time is zero will be used instead).
79 *****************************************************************************/
80 static BOOL kernel_check_notify(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *datap, time_t t)
81 {
82         struct change_data *data = (struct change_data *)datap;
83
84         if (t)
85                 return False;
86
87         if (data->directory_handle != (int)fd_pending) return False;
88
89         DEBUG(3,("kernel change notify on %s fd=%d\n", path, (int)fd_pending));
90
91         close((int)fd_pending);
92         fd_pending = (sig_atomic_t)-1;
93         data->directory_handle = -1;
94         signals_processed++;
95         BlockSignals(False, RT_SIGNAL_NOTIFY);
96         return True;
97 }
98
99 /****************************************************************************
100 remove a change notify data structure
101 *****************************************************************************/
102 static void kernel_remove_notify(void *datap)
103 {
104         struct change_data *data = (struct change_data *)datap;
105         int fd = data->directory_handle;
106         if (fd != -1) {
107                 if (fd == (int)fd_pending) {
108                         fd_pending = (sig_atomic_t)-1;
109                         signals_processed++;
110                         BlockSignals(False, RT_SIGNAL_NOTIFY);
111                 }
112                 close(fd);
113         }
114         free(data);
115         DEBUG(3,("removed kernel change notify fd=%d\n", fd));
116 }
117
118
119 /****************************************************************************
120 register a change notify request
121 *****************************************************************************/
122 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
123 {
124         struct change_data data;
125         int fd;
126         unsigned long kernel_flags;
127         
128         fd = conn->vfs_ops.open(conn, path, O_RDONLY, 0);
129
130         if (fd == -1) {
131                 DEBUG(3,("Failed to open directory %s for change notify\n", path));
132                 return NULL;
133         }
134
135         if (fcntl(fd, F_SETSIG, RT_SIGNAL_NOTIFY) == -1) {
136                 DEBUG(3,("Failed to set signal handler for change notify\n"));
137                 return NULL;
138         }
139
140         kernel_flags = DN_CREATE|DN_DELETE|DN_RENAME; /* creation/deletion changes everything! */
141         if (flags & FILE_NOTIFY_CHANGE_FILE)        kernel_flags |= DN_MODIFY;
142         if (flags & FILE_NOTIFY_CHANGE_DIR_NAME)    kernel_flags |= DN_RENAME|DN_DELETE;
143         if (flags & FILE_NOTIFY_CHANGE_ATTRIBUTES)  kernel_flags |= DN_ATTRIB;
144         if (flags & FILE_NOTIFY_CHANGE_SIZE)        kernel_flags |= DN_MODIFY;
145         if (flags & FILE_NOTIFY_CHANGE_LAST_WRITE)  kernel_flags |= DN_MODIFY;
146         if (flags & FILE_NOTIFY_CHANGE_LAST_ACCESS) kernel_flags |= DN_ACCESS;
147         if (flags & FILE_NOTIFY_CHANGE_CREATION)    kernel_flags |= DN_CREATE;
148         if (flags & FILE_NOTIFY_CHANGE_SECURITY)    kernel_flags |= DN_ATTRIB;
149         if (flags & FILE_NOTIFY_CHANGE_EA)          kernel_flags |= DN_ATTRIB;
150         if (flags & FILE_NOTIFY_CHANGE_FILE_NAME)   kernel_flags |= DN_RENAME|DN_DELETE;
151
152         if (fcntl(fd, F_NOTIFY, kernel_flags) == -1) {
153                 DEBUG(3,("Failed to set async flag for change notify\n"));
154                 return NULL;
155         }
156
157         data.directory_handle = fd;
158
159         DEBUG(3,("kernel change notify on %s (ntflags=0x%x flags=0x%x) fd=%d\n", 
160                  path, (int)flags, (int)kernel_flags, fd));
161
162         return (void *)memdup(&data, sizeof(data));
163 }
164
165 /****************************************************************************
166 see if the kernel supports change notify
167 ****************************************************************************/
168 static BOOL kernel_notify_available(void) 
169 {
170         int fd, ret;
171         fd = open("/tmp", O_RDONLY);
172         if (fd == -1) return False; /* uggh! */
173         ret = fcntl(fd, F_NOTIFY, 0);
174         close(fd);
175         return ret == 0;
176 }
177
178
179 /****************************************************************************
180 setup kernel based change notify
181 ****************************************************************************/
182 struct cnotify_fns *kernel_notify_init(void) 
183 {
184         static struct cnotify_fns cnotify;
185         struct sigaction act;
186
187         act.sa_handler = NULL;
188         act.sa_sigaction = signal_handler;
189         act.sa_flags = SA_SIGINFO;
190         if (sigaction(RT_SIGNAL_NOTIFY, &act, NULL) != 0) {
191                 DEBUG(0,("Failed to setup RT_SIGNAL_NOTIFY handler\n"));
192                 return NULL;
193         }
194
195         if (!kernel_notify_available()) return NULL;
196
197         cnotify.register_notify = kernel_register_notify;
198         cnotify.check_notify = kernel_check_notify;
199         cnotify.remove_notify = kernel_remove_notify;
200         cnotify.select_time = -1;
201
202         return &cnotify;
203 }
204
205
206 #else
207  void notify_kernel_dummy(void) {}
208 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */