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