2 Unix SMB/Netbios implementation.
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.
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
;
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 */
41 #ifndef RT_SIGNAL_NOTIFY
42 #define RT_SIGNAL_NOTIFY 34
53 /****************************************************************************
54 This is the structure to keep the information needed to
55 determine if a directory has changed.
56 *****************************************************************************/
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
;
76 } /* Else signal is lost. */
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
;
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;
113 BlockSignals(False
, RT_SIGNAL_NOTIFY
);
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
;
127 BlockSignals(True
, RT_SIGNAL_NOTIFY
);
128 for (i
= 0; i
< signals_received
; i
++) {
129 if (fd
== (int)fd_pending_array
[i
]) {
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;
141 BlockSignals(False
, RT_SIGNAL_NOTIFY
);
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
;
155 unsigned long kernel_flags
;
157 fd
= sys_open(dos_to_unix_static(path
),O_RDONLY
, 0);
160 DEBUG(3,("Failed to open directory %s for change notify\n", path
));
164 if (sys_fcntl_long(fd
, F_SETSIG
, RT_SIGNAL_NOTIFY
) == -1) {
165 DEBUG(3,("Failed to set signal handler for change notify\n"));
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"));
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)
201 fd
= open("/tmp", O_RDONLY
);
203 return False
; /* uggh! */
204 ret
= sys_fcntl_long(fd
, F_NOTIFY
, 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"));
226 if (!kernel_notify_available())
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;
238 void notify_kernel_dummy(void) {}
239 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */