smbd: Remove unused [push_pull]_file_id_24
[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 "librpc/gen_ndr/notify.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
84 #ifdef HAVE_FAMNOEXISTS
85         /* We should honor outside setting of the GAM_CLIENT_ID. */
86         setenv("GAM_CLIENT_ID","SAMBA",0);
87 #endif
88
89         if (asprintf(&name, "smbd (%lu)", (unsigned long)sys_getpid()) == -1) {
90                 DEBUG(0, ("No memory\n"));
91                 return NT_STATUS_NO_MEMORY;
92         }
93
94         res = FAMOpen2(fam_conn, name);
95
96 #ifdef HAVE_FAMNOEXISTS
97         /*
98          * This reduces the chatter between GAMIN and samba making the pair
99          * much more reliable.
100          */
101         FAMNoExists(fam_conn);
102 #endif
103
104         SAFE_FREE(name);
105
106         if (res < 0) {
107                 DEBUG(10, ("FAM file change notifications not available\n"));
108                 /*
109                  * No idea how to get NT_STATUS from a FAM result
110                  */
111                 FAMCONNECTION_GETFD(fam_conn) = -1;
112                 return NT_STATUS_UNEXPECTED_IO_ERROR;
113         }
114
115         if (event_add_fd(event_ctx, event_ctx,
116                          FAMCONNECTION_GETFD(fam_conn),
117                          EVENT_FD_READ, fam_handler,
118                          (void *)fam_conn) == NULL) {
119                 DEBUG(0, ("event_add_fd failed\n"));
120                 FAMClose(fam_conn);
121                 FAMCONNECTION_GETFD(fam_conn) = -1;
122                 return NT_STATUS_NO_MEMORY;
123         }
124
125         return NT_STATUS_OK;
126 }
127
128 static void fam_reopen(FAMConnection *fam_conn,
129                        struct event_context *event_ctx,
130                        struct fam_watch_context *notify_list)
131 {
132         struct fam_watch_context *ctx;
133
134         DEBUG(5, ("Re-opening FAM connection\n"));
135
136         FAMClose(fam_conn);
137
138         if (!NT_STATUS_IS_OK(fam_open_connection(fam_conn, event_ctx))) {
139                 DEBUG(5, ("Re-opening fam connection failed\n"));
140                 return;
141         }
142
143         for (ctx = notify_list; ctx; ctx = ctx->next) {
144                 FAMMonitorDirectory(fam_conn, ctx->path, &ctx->fr, NULL);
145         }
146 }
147
148 static void fam_handler(struct event_context *event_ctx,
149                         struct fd_event *fd_event,
150                         uint16 flags,
151                         void *private_data)
152 {
153         FAMConnection *fam_conn = (FAMConnection *)private_data;
154         FAMEvent fam_event;
155         struct fam_watch_context *ctx;
156         struct notify_event ne;
157
158         if (FAMPending(fam_conn) == 0) {
159                 DEBUG(10, ("fam_handler called but nothing pending\n"));
160                 return;
161         }
162
163         if (FAMNextEvent(fam_conn, &fam_event) != 1) {
164                 DEBUG(5, ("FAMNextEvent returned an error\n"));
165                 TALLOC_FREE(fd_event);
166                 fam_reopen(fam_conn, event_ctx, fam_notify_list);
167                 return;
168         }
169
170         DEBUG(10, ("Got FAMCode %d for %s\n", fam_event.code,
171                    fam_event.filename));
172
173         switch (fam_event.code) {
174         case FAMChanged:
175                 ne.action = NOTIFY_ACTION_MODIFIED;
176                 break;
177         case FAMCreated:
178                 ne.action = NOTIFY_ACTION_ADDED;
179                 break;
180         case FAMDeleted:
181                 ne.action = NOTIFY_ACTION_REMOVED;
182                 break;
183         default:
184                 DEBUG(10, ("Ignoring code FAMCode %d for file %s\n",
185                            (int)fam_event.code, fam_event.filename));
186                 return;
187         }
188
189         for (ctx = fam_notify_list; ctx; ctx = ctx->next) {
190                 if (memcmp(&fam_event.fr, &ctx->fr, sizeof(FAMRequest)) == 0) {
191                         break;
192                 }
193         }
194
195         if (ctx == NULL) {
196                 DEBUG(5, ("Discarding event for file %s\n",
197                           fam_event.filename));
198                 return;
199         }
200
201         if ((ne.path = strrchr_m(fam_event.filename, '\\')) == NULL) {
202                 ne.path = fam_event.filename;
203         }
204
205         ctx->callback(ctx->sys_ctx, ctx->private_data, &ne);
206 }
207
208 static int fam_watch_context_destructor(struct fam_watch_context *ctx)
209 {
210         if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
211                 FAMCancelMonitor(&fam_connection, &ctx->fr);
212         }
213         DLIST_REMOVE(fam_notify_list, ctx);
214         return 0;
215 }
216
217 /*
218   add a watch. The watch is removed when the caller calls
219   talloc_free() on *handle
220 */
221 static NTSTATUS fam_watch(vfs_handle_struct *vfs_handle,
222                           struct sys_notify_context *ctx,
223                           struct notify_entry *e,
224                           void (*callback)(struct sys_notify_context *ctx, 
225                                            void *private_data,
226                                            struct notify_event *ev),
227                           void *private_data, 
228                           void *handle_p)
229 {
230         const uint32 fam_mask = (FILE_NOTIFY_CHANGE_FILE_NAME|
231                                  FILE_NOTIFY_CHANGE_DIR_NAME);
232         struct fam_watch_context *watch;
233         void **handle = (void **)handle_p;
234
235         if ((e->filter & fam_mask) == 0) {
236                 DEBUG(10, ("filter = %u, ignoring in FAM\n", e->filter));
237                 return NT_STATUS_OK;
238         }
239
240         if (!fam_connection_initialized) {
241                 if (!NT_STATUS_IS_OK(fam_open_connection(&fam_connection,
242                                                          ctx->ev))) {
243                         /*
244                          * Just let smbd do all the work itself
245                          */
246                         return NT_STATUS_OK;
247                 }
248                 fam_connection_initialized = True;
249         }
250
251         if (!(watch = TALLOC_P(ctx, struct fam_watch_context))) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         watch->fam_connection = &fam_connection;
256
257         watch->callback = callback;
258         watch->private_data = private_data;
259         watch->sys_ctx = ctx;
260
261         if (!(watch->path = talloc_strdup(watch, e->path))) {
262                 DEBUG(0, ("talloc_asprintf failed\n"));
263                 TALLOC_FREE(watch);
264                 return NT_STATUS_NO_MEMORY;
265         }
266
267         /*
268          * The FAM module in this early state will only take care of
269          * FAMCreated and FAMDeleted events, Leave the rest to
270          * notify_internal.c
271          */
272
273         watch->filter = fam_mask;
274         e->filter &= ~fam_mask;
275
276         DLIST_ADD(fam_notify_list, watch);
277         talloc_set_destructor(watch, fam_watch_context_destructor);
278
279         /*
280          * Only directories monitored so far
281          */
282
283         if (FAMCONNECTION_GETFD(watch->fam_connection) != -1) {
284                 FAMMonitorDirectory(watch->fam_connection, watch->path,
285                                     &watch->fr, NULL);
286         }
287         else {
288                 /*
289                  * If the re-open is successful, this will establish the
290                  * FAMMonitor from the list
291                  */
292                 fam_reopen(watch->fam_connection, ctx->ev, fam_notify_list);
293         }
294
295         *handle = (void *)watch;
296
297         return NT_STATUS_OK;
298 }
299
300 /* VFS operations structure */
301
302 static struct vfs_fn_pointers notify_fam_fns = {
303         .notify_watch = fam_watch,
304 };
305
306
307 NTSTATUS vfs_notify_fam_init(void);
308 NTSTATUS vfs_notify_fam_init(void)
309 {
310         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "notify_fam",
311                                 &notify_fam_fns);
312 }