1 | /*
|
---|
2 | * FAM file notification support.
|
---|
3 | *
|
---|
4 | * Copyright (c) James Peach 2005
|
---|
5 | * Copyright (c) Volker Lendecke 2007
|
---|
6 | *
|
---|
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.
|
---|
11 | *
|
---|
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.
|
---|
16 | *
|
---|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 |
|
---|
24 | #include <fam.h>
|
---|
25 |
|
---|
26 | #if !defined(HAVE_FAM_H_FAMCODES_TYPEDEF)
|
---|
27 | /* Gamin provides this typedef which means we can't use 'enum FAMCodes' as per
|
---|
28 | * every other FAM implementation. Phooey.
|
---|
29 | */
|
---|
30 | typedef enum FAMCodes FAMCodes;
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | /* NOTE: There are multiple versions of FAM floating around the net, each with
|
---|
34 | * slight differences from the original SGI FAM implementation. In this file,
|
---|
35 | * we rely only on the SGI features and do not assume any extensions. For
|
---|
36 | * example, we do not look at FAMErrno, because it is not set by the original
|
---|
37 | * implementation.
|
---|
38 | *
|
---|
39 | * Random FAM links:
|
---|
40 | * http://oss.sgi.com/projects/fam/
|
---|
41 | * http://savannah.nongnu.org/projects/fam/
|
---|
42 | * http://sourceforge.net/projects/bsdfam/
|
---|
43 | */
|
---|
44 |
|
---|
45 | /* ------------------------------------------------------------------------- */
|
---|
46 |
|
---|
47 | struct fam_watch_context {
|
---|
48 | struct fam_watch_context *prev, *next;
|
---|
49 | FAMConnection *fam_connection;
|
---|
50 | struct FAMRequest fr;
|
---|
51 | struct sys_notify_context *sys_ctx;
|
---|
52 | void (*callback)(struct sys_notify_context *ctx,
|
---|
53 | void *private_data,
|
---|
54 | struct notify_event *ev);
|
---|
55 | void *private_data;
|
---|
56 | uint32_t mask; /* the inotify mask */
|
---|
57 | uint32_t filter; /* the windows completion filter */
|
---|
58 | const char *path;
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * We want one FAM connection per smbd, not one per tcon.
|
---|
64 | */
|
---|
65 | static FAMConnection fam_connection;
|
---|
66 | static BOOL fam_connection_initialized = False;
|
---|
67 |
|
---|
68 | static struct fam_watch_context *fam_notify_list;
|
---|
69 | static void fam_handler(struct event_context *event_ctx,
|
---|
70 | struct fd_event *fd_event,
|
---|
71 | uint16 flags,
|
---|
72 | void *private_data);
|
---|
73 |
|
---|
74 | static NTSTATUS fam_open_connection(FAMConnection *fam_conn,
|
---|
75 | struct event_context *event_ctx)
|
---|
76 | {
|
---|
77 | int res;
|
---|
78 | char *name;
|
---|
79 |
|
---|
80 | ZERO_STRUCTP(fam_conn);
|
---|
81 | FAMCONNECTION_GETFD(fam_conn) = -1;
|
---|
82 |
|
---|
83 | if (asprintf(&name, "smbd (%lu)", (unsigned long)sys_getpid()) == -1) {
|
---|
84 | DEBUG(0, ("No memory\n"));
|
---|
85 | return NT_STATUS_NO_MEMORY;
|
---|
86 | }
|
---|
87 |
|
---|
88 | res = FAMOpen2(fam_conn, name);
|
---|
89 | SAFE_FREE(name);
|
---|
90 |
|
---|
91 | if (res < 0) {
|
---|
92 | DEBUG(10, ("FAM file change notifications not available\n"));
|
---|
93 | /*
|
---|
94 | * No idea how to get NT_STATUS from a FAM result
|
---|
95 | */
|
---|
96 | FAMCONNECTION_GETFD(fam_conn) = -1;
|
---|
97 | return NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (event_add_fd(event_ctx, event_ctx,
|
---|
101 | FAMCONNECTION_GETFD(fam_conn),
|
---|
102 | EVENT_FD_READ, fam_handler,
|
---|
103 | (void *)fam_conn) == NULL) {
|
---|
104 | DEBUG(0, ("event_add_fd failed\n"));
|
---|
105 | FAMClose(fam_conn);
|
---|
106 | FAMCONNECTION_GETFD(fam_conn) = -1;
|
---|
107 | return NT_STATUS_NO_MEMORY;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return NT_STATUS_OK;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void fam_reopen(FAMConnection *fam_conn,
|
---|
114 | struct event_context *event_ctx,
|
---|
115 | struct fam_watch_context *notify_list)
|
---|
116 | {
|
---|
117 | struct fam_watch_context *ctx;
|
---|
118 |
|
---|
119 | DEBUG(5, ("Re-opening FAM connection\n"));
|
---|
120 |
|
---|
121 | FAMClose(fam_conn);
|
---|
122 |
|
---|
123 | if (!NT_STATUS_IS_OK(fam_open_connection(fam_conn, event_ctx))) {
|
---|
124 | DEBUG(5, ("Re-opening fam connection failed\n"));
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | for (ctx = notify_list; ctx; ctx = ctx->next) {
|
---|
129 | FAMMonitorDirectory(fam_conn, ctx->path, &ctx->fr, NULL);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void fam_handler(struct event_context *event_ctx,
|
---|
134 | struct fd_event *fd_event,
|
---|
135 | uint16 flags,
|
---|
136 | void *private_data)
|
---|
137 | {
|
---|
138 | FAMConnection *fam_conn = (FAMConnection *)private_data;
|
---|
139 | FAMEvent fam_event;
|
---|
140 | struct fam_watch_context *ctx;
|
---|
141 | struct notify_event ne;
|
---|
142 |
|
---|
143 | if (FAMPending(fam_conn) == 0) {
|
---|
144 | DEBUG(10, ("fam_handler called but nothing pending\n"));
|
---|
145 | return;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (FAMNextEvent(fam_conn, &fam_event) != 1) {
|
---|
149 | DEBUG(5, ("FAMNextEvent returned an error\n"));
|
---|
150 | TALLOC_FREE(fd_event);
|
---|
151 | fam_reopen(fam_conn, event_ctx, fam_notify_list);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | DEBUG(10, ("Got FAMCode %d for %s\n", fam_event.code,
|
---|
156 | fam_event.filename));
|
---|
157 |
|
---|
158 | switch (fam_event.code) {
|
---|
159 | case FAMChanged:
|
---|
160 | ne.action = NOTIFY_ACTION_MODIFIED;
|
---|
161 | break;
|
---|
162 | case FAMCreated:
|
---|
163 | ne.action = NOTIFY_ACTION_ADDED;
|
---|
164 | break;
|
---|
165 | case FAMDeleted:
|
---|
166 | ne.action = NOTIFY_ACTION_REMOVED;
|
---|
167 | break;
|
---|
168 | default:
|
---|
169 | DEBUG(10, ("Ignoring code FAMCode %d for file %s\n",
|
---|
170 | (int)fam_event.code, fam_event.filename));
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | for (ctx = fam_notify_list; ctx; ctx = ctx->next) {
|
---|
175 | if (memcmp(&fam_event.fr, &ctx->fr, sizeof(FAMRequest)) == 0) {
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (ctx == NULL) {
|
---|
181 | DEBUG(5, ("Discarding event for file %s\n",
|
---|
182 | fam_event.filename));
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if ((ne.path = strrchr_m(fam_event.filename, '\\')) == NULL) {
|
---|
187 | ne.path = fam_event.filename;
|
---|
188 | }
|
---|
189 |
|
---|
190 | ctx->callback(ctx->sys_ctx, ctx->private_data, &ne);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static int fam_watch_context_destructor(struct fam_watch_context *ctx)
|
---|
194 | {
|
---|
195 | if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
|
---|
196 | FAMCancelMonitor(&fam_connection, &ctx->fr);
|
---|
197 | }
|
---|
198 | DLIST_REMOVE(fam_notify_list, ctx);
|
---|
199 | return 0;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /*
|
---|
203 | add a watch. The watch is removed when the caller calls
|
---|
204 | talloc_free() on *handle
|
---|
205 | */
|
---|
206 | static NTSTATUS fam_watch(vfs_handle_struct *vfs_handle,
|
---|
207 | struct sys_notify_context *ctx,
|
---|
208 | struct notify_entry *e,
|
---|
209 | void (*callback)(struct sys_notify_context *ctx,
|
---|
210 | void *private_data,
|
---|
211 | struct notify_event *ev),
|
---|
212 | void *private_data,
|
---|
213 | void *handle_p)
|
---|
214 | {
|
---|
215 | const uint32 fam_mask = (FILE_NOTIFY_CHANGE_FILE_NAME|
|
---|
216 | FILE_NOTIFY_CHANGE_DIR_NAME);
|
---|
217 | struct fam_watch_context *watch;
|
---|
218 | void **handle = (void **)handle_p;
|
---|
219 |
|
---|
220 | if ((e->filter & fam_mask) == 0) {
|
---|
221 | DEBUG(10, ("filter = %u, ignoring in FAM\n", e->filter));
|
---|
222 | return NT_STATUS_OK;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (!fam_connection_initialized) {
|
---|
226 | if (!NT_STATUS_IS_OK(fam_open_connection(&fam_connection,
|
---|
227 | ctx->ev))) {
|
---|
228 | /*
|
---|
229 | * Just let smbd do all the work itself
|
---|
230 | */
|
---|
231 | return NT_STATUS_OK;
|
---|
232 | }
|
---|
233 | fam_connection_initialized = True;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (!(watch = TALLOC_P(ctx, struct fam_watch_context))) {
|
---|
237 | return NT_STATUS_NO_MEMORY;
|
---|
238 | }
|
---|
239 |
|
---|
240 | watch->fam_connection = &fam_connection;
|
---|
241 |
|
---|
242 | watch->callback = callback;
|
---|
243 | watch->private_data = private_data;
|
---|
244 | watch->sys_ctx = ctx;
|
---|
245 |
|
---|
246 | if (!(watch->path = talloc_strdup(watch, e->path))) {
|
---|
247 | DEBUG(0, ("talloc_asprintf failed\n"));
|
---|
248 | TALLOC_FREE(watch);
|
---|
249 | return NT_STATUS_NO_MEMORY;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * The FAM module in this early state will only take care of
|
---|
254 | * FAMCreated and FAMDeleted events, Leave the rest to
|
---|
255 | * notify_internal.c
|
---|
256 | */
|
---|
257 |
|
---|
258 | watch->filter = fam_mask;
|
---|
259 | e->filter &= ~fam_mask;
|
---|
260 |
|
---|
261 | DLIST_ADD(fam_notify_list, watch);
|
---|
262 | talloc_set_destructor(watch, fam_watch_context_destructor);
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * Only directories monitored so far
|
---|
266 | */
|
---|
267 |
|
---|
268 | if (FAMCONNECTION_GETFD(watch->fam_connection) != -1) {
|
---|
269 | FAMMonitorDirectory(watch->fam_connection, watch->path,
|
---|
270 | &watch->fr, NULL);
|
---|
271 | }
|
---|
272 | else {
|
---|
273 | /*
|
---|
274 | * If the re-open is successful, this will establish the
|
---|
275 | * FAMMonitor from the list
|
---|
276 | */
|
---|
277 | fam_reopen(watch->fam_connection, ctx->ev, fam_notify_list);
|
---|
278 | }
|
---|
279 |
|
---|
280 | *handle = (void *)watch;
|
---|
281 |
|
---|
282 | return NT_STATUS_OK;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* VFS operations structure */
|
---|
286 |
|
---|
287 | static vfs_op_tuple notify_fam_op_tuples[] = {
|
---|
288 |
|
---|
289 | {SMB_VFS_OP(fam_watch),
|
---|
290 | SMB_VFS_OP_NOTIFY_WATCH,
|
---|
291 | SMB_VFS_LAYER_OPAQUE},
|
---|
292 |
|
---|
293 | {SMB_VFS_OP(NULL),
|
---|
294 | SMB_VFS_OP_NOOP,
|
---|
295 | SMB_VFS_LAYER_NOOP}
|
---|
296 |
|
---|
297 | };
|
---|
298 |
|
---|
299 |
|
---|
300 | NTSTATUS vfs_notify_fam_init(void);
|
---|
301 | NTSTATUS vfs_notify_fam_init(void)
|
---|
302 | {
|
---|
303 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "notify_fam",
|
---|
304 | notify_fam_op_tuples);
|
---|
305 | }
|
---|