Use canonical mappings for file controls. Fixes W2KSP2 profile problems (I
[Samba/ekacnet.git] / source / smbd / notify_kernel.c
blobef4e47acf1ee90a631e8b08b6ac95b9d3c5cc143
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 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;
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
42 #ifndef RT_SIGNAL_NOTIFY
43 #define RT_SIGNAL_NOTIFY 34
44 #endif
46 #ifndef F_SETSIG
47 #define F_SETSIG 10
48 #endif
50 #ifndef F_NOTIFY
51 #define F_NOTIFY 1026
52 #endif
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;
62 /****************************************************************************
63 the signal handler for change notify
64 *****************************************************************************/
65 static void signal_handler(int sig, siginfo_t *info, void *unused)
67 BlockSignals(True, sig);
68 fd_pending = (SIG_ATOMIC_T)info->si_fd;
69 signals_received++;
70 sys_select_signal();
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)
82 struct change_data *data = (struct change_data *)datap;
84 if (t)
85 return False;
87 if (data->directory_handle != (int)fd_pending) return False;
89 DEBUG(3,("kernel change notify on %s fd=%d\n", path, (int)fd_pending));
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;
99 /****************************************************************************
100 remove a change notify data structure
101 *****************************************************************************/
102 static void kernel_remove_notify(void *datap)
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);
112 close(fd);
114 free(data);
115 DEBUG(3,("removed kernel change notify fd=%d\n", fd));
119 /****************************************************************************
120 register a change notify request
121 *****************************************************************************/
122 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
124 struct change_data data;
125 int fd;
126 unsigned long kernel_flags;
128 fd = dos_open(path, O_RDONLY, 0);
130 if (fd == -1) {
131 DEBUG(3,("Failed to open directory %s for change notify\n", path));
132 return NULL;
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;
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;
152 if (fcntl(fd, F_NOTIFY, kernel_flags) == -1) {
153 DEBUG(3,("Failed to set async flag for change notify\n"));
154 return NULL;
157 data.directory_handle = fd;
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));
162 return (void *)memdup(&data, sizeof(data));
165 /****************************************************************************
166 see if the kernel supports change notify
167 ****************************************************************************/
168 static BOOL kernel_notify_available(void)
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;
179 /****************************************************************************
180 setup kernel based change notify
181 ****************************************************************************/
182 struct cnotify_fns *kernel_notify_init(void)
184 static struct cnotify_fns cnotify;
185 struct sigaction act;
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;
195 if (!kernel_notify_available()) return NULL;
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;
202 return &cnotify;
206 #else
207 void notify_kernel_dummy(void) {}
208 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */