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