r6303: Setting up for 3.0.15pre1
[Samba.git] / source / smbd / notify_kernel.c
blobc368fd79a129954ee27c8225a25fd369be42bd2c
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 (SIGRTMIN+2)
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(CONST_DISCARD(void *, &fd_pending_array[i]),
105 CONST_DISCARD(void *, &fd_pending_array[i+1]),
106 sizeof(SIG_ATOMIC_T)*(signals_received-i-1));
108 data->directory_handle = -1;
109 signals_received--;
110 ret = True;
111 break;
114 BlockSignals(False, RT_SIGNAL_NOTIFY);
115 return ret;
118 /****************************************************************************
119 Remove a change notify data structure.
120 *****************************************************************************/
122 static void kernel_remove_notify(void *datap)
124 struct change_data *data = (struct change_data *)datap;
125 int fd = data->directory_handle;
126 if (fd != -1) {
127 int i;
128 BlockSignals(True, RT_SIGNAL_NOTIFY);
129 for (i = 0; i < signals_received; i++) {
130 if (fd == (int)fd_pending_array[i]) {
131 fd_pending_array[i] = (SIG_ATOMIC_T)-1;
132 if (signals_received - i - 1) {
133 memmove(CONST_DISCARD(void *, &fd_pending_array[i]),
134 CONST_DISCARD(void *, &fd_pending_array[i+1]),
135 sizeof(SIG_ATOMIC_T)*(signals_received-i-1));
137 data->directory_handle = -1;
138 signals_received--;
139 break;
142 close(fd);
143 BlockSignals(False, RT_SIGNAL_NOTIFY);
145 SAFE_FREE(data);
146 DEBUG(3,("kernel_remove_notify: fd=%d\n", fd));
149 /****************************************************************************
150 Register a change notify request.
151 *****************************************************************************/
153 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
155 struct change_data data;
156 int fd;
157 unsigned long kernel_flags;
159 fd = sys_open(path,O_RDONLY, 0);
161 if (fd == -1) {
162 DEBUG(3,("Failed to open directory %s for change notify\n", path));
163 return NULL;
166 if (sys_fcntl_long(fd, F_SETSIG, RT_SIGNAL_NOTIFY) == -1) {
167 DEBUG(3,("Failed to set signal handler for change notify\n"));
168 return NULL;
171 kernel_flags = DN_CREATE|DN_DELETE|DN_RENAME; /* creation/deletion changes everything! */
172 if (flags & FILE_NOTIFY_CHANGE_FILE) kernel_flags |= DN_MODIFY;
173 if (flags & FILE_NOTIFY_CHANGE_DIR_NAME) kernel_flags |= DN_RENAME|DN_DELETE;
174 if (flags & FILE_NOTIFY_CHANGE_ATTRIBUTES) kernel_flags |= DN_ATTRIB;
175 if (flags & FILE_NOTIFY_CHANGE_SIZE) kernel_flags |= DN_MODIFY;
176 if (flags & FILE_NOTIFY_CHANGE_LAST_WRITE) kernel_flags |= DN_MODIFY;
177 if (flags & FILE_NOTIFY_CHANGE_LAST_ACCESS) kernel_flags |= DN_ACCESS;
178 if (flags & FILE_NOTIFY_CHANGE_CREATION) kernel_flags |= DN_CREATE;
179 if (flags & FILE_NOTIFY_CHANGE_SECURITY) kernel_flags |= DN_ATTRIB;
180 if (flags & FILE_NOTIFY_CHANGE_EA) kernel_flags |= DN_ATTRIB;
181 if (flags & FILE_NOTIFY_CHANGE_FILE_NAME) kernel_flags |= DN_RENAME|DN_DELETE;
183 if (sys_fcntl_long(fd, F_NOTIFY, kernel_flags) == -1) {
184 DEBUG(3,("Failed to set async flag for change notify\n"));
185 return NULL;
188 data.directory_handle = fd;
190 DEBUG(3,("kernel change notify on %s (ntflags=0x%x flags=0x%x) fd=%d\n",
191 path, (int)flags, (int)kernel_flags, fd));
193 return (void *)memdup(&data, sizeof(data));
196 /****************************************************************************
197 See if the kernel supports change notify.
198 ****************************************************************************/
200 static BOOL kernel_notify_available(void)
202 int fd, ret;
203 fd = open("/tmp", O_RDONLY);
204 if (fd == -1)
205 return False; /* uggh! */
206 ret = sys_fcntl_long(fd, F_NOTIFY, 0);
207 close(fd);
208 return ret == 0;
211 /****************************************************************************
212 Setup kernel based change notify.
213 ****************************************************************************/
215 struct cnotify_fns *kernel_notify_init(void)
217 static struct cnotify_fns cnotify;
218 struct sigaction act;
220 ZERO_STRUCT(act);
222 act.sa_handler = NULL;
223 act.sa_sigaction = signal_handler;
224 act.sa_flags = SA_SIGINFO;
225 sigemptyset( &act.sa_mask );
226 if (sigaction(RT_SIGNAL_NOTIFY, &act, NULL) != 0) {
227 DEBUG(0,("Failed to setup RT_SIGNAL_NOTIFY handler\n"));
228 return NULL;
231 if (!kernel_notify_available())
232 return NULL;
234 cnotify.register_notify = kernel_register_notify;
235 cnotify.check_notify = kernel_check_notify;
236 cnotify.remove_notify = kernel_remove_notify;
237 cnotify.select_time = -1;
239 /* the signal can start off blocked due to a bug in bash */
240 BlockSignals(False, RT_SIGNAL_NOTIFY);
242 return &cnotify;
245 #else
246 void notify_kernel_dummy(void) {}
247 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */