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 (SIGRTMIN+2)
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(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;
114 BlockSignals(False
, RT_SIGNAL_NOTIFY
);
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
;
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;
143 BlockSignals(False
, RT_SIGNAL_NOTIFY
);
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
;
157 unsigned long kernel_flags
;
159 fd
= sys_open(path
,O_RDONLY
, 0);
162 DEBUG(3,("Failed to open directory %s for change notify\n", path
));
166 if (sys_fcntl_long(fd
, F_SETSIG
, RT_SIGNAL_NOTIFY
) == -1) {
167 DEBUG(3,("Failed to set signal handler for change notify\n"));
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"));
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)
203 fd
= open("/tmp", O_RDONLY
);
205 return False
; /* uggh! */
206 ret
= sys_fcntl_long(fd
, F_NOTIFY
, 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
;
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"));
231 if (!kernel_notify_available())
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
);
246 void notify_kernel_dummy(void) {}
247 #endif /* HAVE_KERNEL_CHANGE_NOTIFY */