2 * FAM file notification support.
4 * Copyright (c) James Peach 2005
5 * Copyright (c) Volker Lendecke 2007
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #if !defined(HAVE_FAM_H_FAMCODES_TYPEDEF)
29 /* Gamin provides this typedef which means we can't use 'enum FAMCodes' as per
30 * every other FAM implementation. Phooey.
32 typedef enum FAMCodes FAMCodes;
35 /* NOTE: There are multiple versions of FAM floating around the net, each with
36 * slight differences from the original SGI FAM implementation. In this file,
37 * we rely only on the SGI features and do not assume any extensions. For
38 * example, we do not look at FAMErrno, because it is not set by the original
42 * http://oss.sgi.com/projects/fam/
43 * http://savannah.nongnu.org/projects/fam/
44 * http://sourceforge.net/projects/bsdfam/
47 static void *fam_notify_add(TALLOC_CTX *mem_ctx,
48 struct event_context *event_ctx,
49 files_struct *fsp, uint32 *filter);
51 /* ------------------------------------------------------------------------- */
53 struct fam_notify_ctx {
54 struct fam_notify_ctx *prev, *next;
55 FAMConnection *fam_connection;
62 static struct fam_notify_ctx *fam_notify_list;
63 static FAMConnection fam_connection;
64 static void fam_handler(struct event_context *event_ctx,
65 struct fd_event *fd_event,
69 static NTSTATUS fam_open_connection(FAMConnection *fam_conn,
70 struct event_context *event_ctx)
75 ZERO_STRUCTP(fam_conn);
76 FAMCONNECTION_GETFD(fam_conn) = -1;
78 if (asprintf(&name, "smbd (%lu)", (unsigned long)sys_getpid()) == -1) {
79 DEBUG(0, ("No memory\n"));
80 return NT_STATUS_NO_MEMORY;
83 res = FAMOpen2(fam_conn, name);
87 DEBUG(5, ("FAM file change notifications not available\n"));
89 * No idea how to get NT_STATUS from a FAM result
91 FAMCONNECTION_GETFD(fam_conn) = -1;
92 return NT_STATUS_UNEXPECTED_IO_ERROR;
95 if (event_add_fd(event_ctx, event_ctx,
96 FAMCONNECTION_GETFD(fam_conn),
97 EVENT_FD_READ, fam_handler,
98 (void *)fam_conn) == NULL) {
99 DEBUG(0, ("event_add_fd failed\n"));
101 FAMCONNECTION_GETFD(fam_conn) = -1;
102 return NT_STATUS_NO_MEMORY;
108 static void fam_reopen(FAMConnection *fam_conn,
109 struct event_context *event_ctx,
110 struct fam_notify_ctx *notify_list)
112 struct fam_notify_ctx *ctx;
114 DEBUG(5, ("Re-opening FAM connection\n"));
118 if (!NT_STATUS_IS_OK(fam_open_connection(fam_conn, event_ctx))) {
119 DEBUG(5, ("Re-opening fam connection failed\n"));
123 for (ctx = notify_list; ctx; ctx = ctx->next) {
124 FAMMonitorDirectory(fam_conn, ctx->path, &ctx->fr, NULL);
128 static void fam_handler(struct event_context *event_ctx,
129 struct fd_event *fd_event,
133 FAMConnection *fam_conn = (FAMConnection *)private_data;
135 struct fam_notify_ctx *ctx;
138 if (FAMPending(fam_conn) == 0) {
139 DEBUG(10, ("fam_handler called but nothing pending\n"));
143 if (FAMNextEvent(fam_conn, &fam_event) != 1) {
144 DEBUG(10, ("FAMNextEvent returned an error\n"));
145 TALLOC_FREE(fd_event);
146 fam_reopen(fam_conn, event_ctx, fam_notify_list);
150 if ((fam_event.code != FAMCreated) && (fam_event.code != FAMDeleted)) {
151 DEBUG(10, ("Ignoring code FAMCode %d for file %s\n",
152 (int)fam_event.code, fam_event.filename));
156 for (ctx = fam_notify_list; ctx; ctx = ctx->next) {
157 if (memcmp(&fam_event.fr, &ctx->fr, sizeof(FAMRequest)) == 0) {
163 DEBUG(5, ("Discarding event for file %s\n",
164 fam_event.filename));
168 if ((name = strrchr_m(fam_event.filename, '\\')) == NULL) {
169 name = fam_event.filename;
173 fam_event.code == FAMCreated
174 ? NOTIFY_ACTION_ADDED : NOTIFY_ACTION_REMOVED,
178 static int fam_notify_ctx_destructor(struct fam_notify_ctx *ctx)
180 if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
181 FAMCancelMonitor(&fam_connection, &ctx->fr);
183 DLIST_REMOVE(fam_notify_list, ctx);
187 static void *fam_notify_add(TALLOC_CTX *mem_ctx,
188 struct event_context *event_ctx,
189 files_struct *fsp, uint32 *filter)
191 struct fam_notify_ctx *ctx;
193 if ((*filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
194 DEBUG(10, ("filter = %u, no FILE_NOTIFY_CHANGE_FILE_NAME\n",
199 if (!(ctx = TALLOC_P(mem_ctx, struct fam_notify_ctx))) {
204 ctx->fam_connection = &fam_connection;
207 * The FAM module in this early state will only take care of
208 * FAMCreated and FAMDeleted events
211 ctx->filter = FILE_NOTIFY_CHANGE_FILE_NAME;
213 if (!(ctx->path = talloc_asprintf(ctx, "%s/%s", fsp->conn->connectpath,
215 DEBUG(0, ("talloc_asprintf failed\n"));
221 * Leave the rest to smbd itself
224 *filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
226 DLIST_ADD(fam_notify_list, ctx);
227 talloc_set_destructor(ctx, fam_notify_ctx_destructor);
230 * Only directories monitored so far
233 if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
234 FAMMonitorDirectory(ctx->fam_connection, ctx->path, &ctx->fr,
239 * If the re-open is successful, this will establish the
240 * FAMMonitor from the list
242 fam_reopen(ctx->fam_connection, event_ctx, fam_notify_list);
248 static struct cnotify_fns global_fam_notify =
253 struct cnotify_fns *fam_notify_init(struct event_context *event_ctx)
256 ZERO_STRUCT(fam_connection);
257 FAMCONNECTION_GETFD(&fam_connection) = -1;
259 if (!NT_STATUS_IS_OK(fam_open_connection(&fam_connection,
261 DEBUG(0, ("FAM file change notifications not available\n"));
265 DEBUG(10, ("enabling FAM change notifications\n"));
266 return &global_fam_notify;
269 #endif /* HAVE_FAM_CHANGE_NOTIFY */