debian packagin updates from Eloy
[Samba.git] / source / smbd / notify_kernel.c
blob372416c0ee99660b6e8bf3d2c1f8913119011887
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 change notify handling - linux kernel based implementation
5 Copyright (C) Andrew Tridgell 2000
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.
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.
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.
22 #include "includes.h"
24 #if HAVE_KERNEL_CHANGE_NOTIFY
26 #define FD_PENDING_SIZE 20
27 static SIG_ATOMIC_T fd_pending_array[FD_PENDING_SIZE];
28 static SIG_ATOMIC_T signals_received;
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
41 #ifndef RT_SIGNAL_NOTIFY
42 #define RT_SIGNAL_NOTIFY 34
43 #endif
45 #ifndef F_SETSIG
46 #define F_SETSIG 10
47 #endif
49 #ifndef F_NOTIFY
50 #define F_NOTIFY 1026
51 #endif
53 /****************************************************************************
54 This is the structure to keep the information needed to
55 determine if a directory has changed.
56 *****************************************************************************/
58 struct change_data {
59 int directory_handle;
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 *****************************************************************************/
71 static void signal_handler(int sig, siginfo_t *info, void *unused)
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();
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 *****************************************************************************/
86 static BOOL kernel_check_notify(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *datap, time_t t)
88 struct change_data *data = (struct change_data *)datap;
89 int i;
90 BOOL ret = False;
92 if (t)
93 return False;
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 ));
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));
107 data->directory_handle = -1;
108 signals_received--;
109 ret = True;
110 break;
113 BlockSignals(False, RT_SIGNAL_NOTIFY);
114 return ret;
117 /****************************************************************************
118 Remove a change notify data structure.
119 *****************************************************************************/
121 static void kernel_remove_notify(void *datap)
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 close(fd);
131 fd_pending_array[i] = (SIG_ATOMIC_T)-1;
132 if (signals_received - i - 1) {
133 memmove((void *)&fd_pending_array[i], (void *)&fd_pending_array[i+1],
134 sizeof(SIG_ATOMIC_T)*(signals_received-i-1));
136 data->directory_handle = -1;
137 signals_received--;
138 break;
141 BlockSignals(False, RT_SIGNAL_NOTIFY);
143 SAFE_FREE(data);
144 DEBUG(3,("kernel_remove_notify: fd=%d\n", fd));
147 /****************************************************************************
148 Register a change notify request.
149 *****************************************************************************/
151 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
153 struct change_data data;
154 int fd;
155 unsigned long kernel_flags;
157 fd = sys_open(dos_to_unix_static(path),O_RDONLY, 0);
159 if (fd == -1) {
160 DEBUG(3,("Failed to open directory %s for change notify\n", path));
161 return NULL;
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;
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;
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;
186 data.directory_handle = fd;
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));
191 return (void *)memdup(&data, sizeof(data));
194 /****************************************************************************
195 See if the kernel supports change notify.
196 ****************************************************************************/
198 static BOOL kernel_notify_available(void)
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;
209 /****************************************************************************
210 Setup kernel based change notify.
211 ****************************************************************************/
213 struct cnotify_fns *kernel_notify_init(void)
215 static struct cnotify_fns cnotify;
216 struct sigaction act;
218 act.sa_handler = NULL;
219 act.sa_sigaction = signal_handler;
220 act.sa_flags = SA_SIGINFO;
221 if (sigaction(RT_SIGNAL_NOTIFY, &act, NULL) != 0) {
222 DEBUG(0,("Failed to setup RT_SIGNAL_NOTIFY handler\n"));
223 return NULL;
226 if (!kernel_notify_available())
227 return NULL;
229 cnotify.register_notify = kernel_register_notify;
230 cnotify.check_notify = kernel_check_notify;
231 cnotify.remove_notify = kernel_remove_notify;
232 cnotify.select_time = -1;
234 return &cnotify;
237 #else
238 void notify_kernel_dummy(void) {}
239 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */