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