auth/credentials: a temporary MEMORY ccache needs krb5_cc_destroy()
[Samba.git] / source3 / smbd / notify_fam.c
blob2cf1e357c800da9cbb67ea95f09f8cae7472fe3a
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "librpc/gen_ndr/notify.h"
25 #include <fam.h>
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.
31 typedef enum FAMCodes FAMCodes;
32 #endif
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.
40 * Random FAM links:
41 * http://oss.sgi.com/projects/fam/
42 * http://savannah.nongnu.org/projects/fam/
43 * http://sourceforge.net/projects/bsdfam/
46 /* ------------------------------------------------------------------------- */
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 uint32_t filter);
57 void *private_data;
58 uint32_t mask; /* the inotify mask */
59 uint32_t filter; /* the windows completion filter */
60 const char *path;
65 * We want one FAM connection per smbd, not one per tcon.
67 static FAMConnection fam_connection;
68 static bool fam_connection_initialized = False;
70 static struct fam_watch_context *fam_notify_list;
71 static void fam_handler(struct tevent_context *event_ctx,
72 struct tevent_fd *fd_event,
73 uint16_t flags,
74 void *private_data);
76 static NTSTATUS fam_open_connection(FAMConnection *fam_conn,
77 struct tevent_context *event_ctx)
79 int res;
80 char *name;
82 ZERO_STRUCTP(fam_conn);
83 FAMCONNECTION_GETFD(fam_conn) = -1;
86 #ifdef HAVE_FAMNOEXISTS
87 /* We should honor outside setting of the GAM_CLIENT_ID. */
88 setenv("GAM_CLIENT_ID","SAMBA",0);
89 #endif
91 if (asprintf(&name, "smbd (%lu)", (unsigned long)getpid()) == -1) {
92 DEBUG(0, ("No memory\n"));
93 return NT_STATUS_NO_MEMORY;
96 res = FAMOpen2(fam_conn, name);
98 #ifdef HAVE_FAMNOEXISTS
100 * This reduces the chatter between GAMIN and samba making the pair
101 * much more reliable.
103 FAMNoExists(fam_conn);
104 #endif
106 SAFE_FREE(name);
108 if (res < 0) {
109 DEBUG(10, ("FAM file change notifications not available: %s\n",
110 FamErrlist[-res]));
112 * No idea how to get NT_STATUS from a FAM result
114 FAMCONNECTION_GETFD(fam_conn) = -1;
115 return NT_STATUS_UNEXPECTED_IO_ERROR;
118 if (tevent_add_fd(event_ctx, event_ctx,
119 FAMCONNECTION_GETFD(fam_conn),
120 TEVENT_FD_READ, fam_handler,
121 (void *)fam_conn) == NULL) {
122 DEBUG(0, ("event_add_fd failed\n"));
123 FAMClose(fam_conn);
124 FAMCONNECTION_GETFD(fam_conn) = -1;
125 return NT_STATUS_NO_MEMORY;
128 return NT_STATUS_OK;
131 static void fam_reopen(FAMConnection *fam_conn,
132 struct tevent_context *event_ctx,
133 struct fam_watch_context *notify_list)
135 struct fam_watch_context *ctx;
137 DEBUG(5, ("Re-opening FAM connection\n"));
139 FAMClose(fam_conn);
141 if (!NT_STATUS_IS_OK(fam_open_connection(fam_conn, event_ctx))) {
142 DEBUG(5, ("Re-opening fam connection failed\n"));
143 return;
146 for (ctx = notify_list; ctx; ctx = ctx->next) {
147 FAMMonitorDirectory(fam_conn, ctx->path, &ctx->fr, NULL);
151 static void fam_handler(struct tevent_context *event_ctx,
152 struct tevent_fd *fd_event,
153 uint16_t flags,
154 void *private_data)
156 FAMConnection *fam_conn = (FAMConnection *)private_data;
157 FAMEvent fam_event;
158 struct fam_watch_context *ctx;
159 struct notify_event ne;
161 if (FAMPending(fam_conn) == 0) {
162 DEBUG(10, ("fam_handler called but nothing pending\n"));
163 return;
166 if (FAMNextEvent(fam_conn, &fam_event) != 1) {
167 DEBUG(5, ("FAMNextEvent returned an error\n"));
168 TALLOC_FREE(fd_event);
169 fam_reopen(fam_conn, event_ctx, fam_notify_list);
170 return;
173 DEBUG(10, ("Got FAMCode %d for %s\n", fam_event.code,
174 fam_event.filename));
176 switch (fam_event.code) {
177 case FAMChanged:
178 ne.action = NOTIFY_ACTION_MODIFIED;
179 break;
180 case FAMCreated:
181 ne.action = NOTIFY_ACTION_ADDED;
182 break;
183 case FAMDeleted:
184 ne.action = NOTIFY_ACTION_REMOVED;
185 break;
186 default:
187 DEBUG(10, ("Ignoring code FAMCode %d for file %s\n",
188 (int)fam_event.code, fam_event.filename));
189 return;
192 for (ctx = fam_notify_list; ctx; ctx = ctx->next) {
193 if (memcmp(&fam_event.fr, &ctx->fr, sizeof(FAMRequest)) == 0) {
194 break;
198 if (ctx == NULL) {
199 DEBUG(5, ("Discarding event for file %s\n",
200 fam_event.filename));
201 return;
204 if ((ne.path = strrchr_m(fam_event.filename, '\\')) == NULL) {
205 ne.path = fam_event.filename;
207 ne.dir = ctx->path;
209 ctx->callback(ctx->sys_ctx, ctx->private_data, &ne, UINT32_MAX);
212 static int fam_watch_context_destructor(struct fam_watch_context *ctx)
214 if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
215 FAMCancelMonitor(&fam_connection, &ctx->fr);
217 DLIST_REMOVE(fam_notify_list, ctx);
218 return 0;
222 add a watch. The watch is removed when the caller calls
223 talloc_free() on *handle
225 int fam_watch(TALLOC_CTX *mem_ctx,
226 struct sys_notify_context *ctx,
227 const char *path,
228 uint32_t *filter,
229 uint32_t *subdir_filter,
230 void (*callback)(struct sys_notify_context *ctx,
231 void *private_data,
232 struct notify_event *ev,
233 uint32_t filter),
234 void *private_data,
235 void *handle_p)
237 const uint32_t fam_mask = (FILE_NOTIFY_CHANGE_FILE_NAME|
238 FILE_NOTIFY_CHANGE_DIR_NAME);
239 struct fam_watch_context *watch;
240 void **handle = (void **)handle_p;
242 *handle = NULL;
244 if ((*filter & fam_mask) == 0) {
245 DEBUG(10, ("filter = %u, ignoring in FAM\n", *filter));
246 return 0;
249 if (!fam_connection_initialized) {
250 if (!NT_STATUS_IS_OK(fam_open_connection(&fam_connection,
251 ctx->ev))) {
253 * Just let smbd do all the work itself
255 return 0;
257 fam_connection_initialized = True;
260 if (!(watch = talloc(mem_ctx, struct fam_watch_context))) {
261 return ENOMEM;
264 watch->fam_connection = &fam_connection;
266 watch->callback = callback;
267 watch->private_data = private_data;
268 watch->sys_ctx = ctx;
270 watch->path = talloc_strdup(watch, path);
271 if (watch->path == NULL) {
272 DEBUG(0, ("talloc_asprintf failed\n"));
273 TALLOC_FREE(watch);
274 return ENOMEM;
278 * The FAM module in this early state will only take care of
279 * FAMCreated and FAMDeleted events, Leave the rest to notifyd
282 watch->filter = fam_mask;
283 *filter &= ~fam_mask;
285 DLIST_ADD(fam_notify_list, watch);
286 talloc_set_destructor(watch, fam_watch_context_destructor);
289 * Only directories monitored so far
292 if (FAMCONNECTION_GETFD(watch->fam_connection) != -1) {
293 FAMMonitorDirectory(watch->fam_connection, watch->path,
294 &watch->fr, NULL);
296 else {
298 * If the re-open is successful, this will establish the
299 * FAMMonitor from the list
301 fam_reopen(watch->fam_connection, ctx->ev, fam_notify_list);
304 *handle = watch;
306 return 0;