2 Unix SMB/CIFS implementation.
3 change notify handling - linux kernel based implementation
4 Copyright (C) Andrew Tridgell 2000
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.
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.
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.
23 #if HAVE_KERNEL_CHANGE_NOTIFY
25 static VOLATILE sig_atomic_t fd_pending;
26 static VOLATILE sig_atomic_t signals_received;
27 static VOLATILE sig_atomic_t signals_processed;
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 */
40 #ifndef RT_SIGNAL_NOTIFY
41 #define RT_SIGNAL_NOTIFY 34
52 /****************************************************************************
53 This is the structure to keep the information needed to
54 determine if a directory has changed.
55 *****************************************************************************/
60 /****************************************************************************
61 the signal handler for change notify
62 *****************************************************************************/
63 static void signal_handler(int sig, siginfo_t *info, void *unused)
65 BlockSignals(True, sig);
66 fd_pending = (sig_atomic_t)info->si_fd;
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)
80 struct change_data *data = (struct change_data *)datap;
85 if (data->directory_handle != (int)fd_pending) return False;
87 DEBUG(3,("kernel change notify on %s fd=%d\n", path, (int)fd_pending));
89 close((int)fd_pending);
90 fd_pending = (sig_atomic_t)-1;
91 data->directory_handle = -1;
93 BlockSignals(False, RT_SIGNAL_NOTIFY);
97 /****************************************************************************
98 remove a change notify data structure
99 *****************************************************************************/
100 static void kernel_remove_notify(void *datap)
102 struct change_data *data = (struct change_data *)datap;
103 int fd = data->directory_handle;
105 if (fd == (int)fd_pending) {
106 fd_pending = (sig_atomic_t)-1;
108 BlockSignals(False, RT_SIGNAL_NOTIFY);
113 DEBUG(3,("removed kernel change notify fd=%d\n", fd));
117 /****************************************************************************
118 register a change notify request
119 *****************************************************************************/
120 static void *kernel_register_notify(connection_struct *conn, char *path, uint32 flags)
122 struct change_data data;
124 unsigned long kernel_flags;
126 fd = conn->vfs_ops.open(conn, path, O_RDONLY, 0);
129 DEBUG(3,("Failed to open directory %s for change notify\n", path));
133 if (fcntl(fd, F_SETSIG, RT_SIGNAL_NOTIFY) == -1) {
134 DEBUG(3,("Failed to set signal handler for change notify\n"));
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;
150 if (fcntl(fd, F_NOTIFY, kernel_flags) == -1) {
151 DEBUG(3,("Failed to set async flag for change notify\n"));
155 data.directory_handle = fd;
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));
160 return (void *)memdup(&data, sizeof(data));
163 /****************************************************************************
164 see if the kernel supports change notify
165 ****************************************************************************/
166 static BOOL kernel_notify_available(void)
169 fd = open("/tmp", O_RDONLY);
170 if (fd == -1) return False; /* uggh! */
171 ret = fcntl(fd, F_NOTIFY, 0);
177 /****************************************************************************
178 setup kernel based change notify
179 ****************************************************************************/
180 struct cnotify_fns *kernel_notify_init(void)
182 static struct cnotify_fns cnotify;
183 struct sigaction act;
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"));
193 if (!kernel_notify_available()) return NULL;
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;
205 void notify_kernel_dummy(void) {}
206 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */