summaryrefslogtreecommitdiff
path: root/source3/smbd/notify_fam.c
blob: 306316e49f7b250b7f6b087ad0f14b05c483473f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * FAM file notification support.
 *
 * Copyright (c) James Peach 2005
 * Copyright (c) Volker Lendecke 2007
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "includes.h"

#ifdef HAVE_FAM_CHANGE_NOTIFY

#include <fam.h>

#if !defined(HAVE_FAM_H_FAMCODES_TYPEDEF)
/* Gamin provides this typedef which means we can't use 'enum FAMCodes' as per
 * every other FAM implementation. Phooey.
 */
typedef enum FAMCodes FAMCodes;
#endif

/* NOTE: There are multiple versions of FAM floating around the net, each with
 * slight differences from the original SGI FAM implementation. In this file,
 * we rely only on the SGI features and do not assume any extensions. For
 * example, we do not look at FAMErrno, because it is not set by the original
 * implementation.
 *
 * Random FAM links:
 *	http://oss.sgi.com/projects/fam/
 *	http://savannah.nongnu.org/projects/fam/
 *	http://sourceforge.net/projects/bsdfam/
 */

static void *fam_notify_add(TALLOC_CTX *mem_ctx,
			    struct event_context *event_ctx,
			    files_struct *fsp, uint32 *filter);

/* ------------------------------------------------------------------------- */

struct fam_notify_ctx {
	struct fam_notify_ctx *prev, *next;
	FAMConnection *fam_connection;
	struct FAMRequest fr;
	files_struct *fsp;
	char *path;
	uint32 filter;
};

static struct fam_notify_ctx *fam_notify_list;
static FAMConnection fam_connection;
static void fam_handler(struct event_context *event_ctx,
			struct fd_event *fd_event,
			uint16 flags,
			void *private_data);

static NTSTATUS fam_open_connection(FAMConnection *fam_conn,
				    struct event_context *event_ctx)
{
	int res;
	char *name;

	ZERO_STRUCTP(fam_conn);
	FAMCONNECTION_GETFD(fam_conn) = -1;

	if (asprintf(&name, "smbd (%lu)", (unsigned long)sys_getpid()) == -1) {
		DEBUG(0, ("No memory\n"));
		return NT_STATUS_NO_MEMORY;
	}

	res = FAMOpen2(fam_conn, name);
	SAFE_FREE(name);

	if (res < 0) {
		DEBUG(5, ("FAM file change notifications not available\n"));
		/*
		 * No idea how to get NT_STATUS from a FAM result
		 */
		FAMCONNECTION_GETFD(fam_conn) = -1;
		return NT_STATUS_UNEXPECTED_IO_ERROR;
	}

	if (event_add_fd(event_ctx, event_ctx,
			 FAMCONNECTION_GETFD(fam_conn),
			 EVENT_FD_READ, fam_handler,
			 (void *)fam_conn) == NULL) {
		DEBUG(0, ("event_add_fd failed\n"));
		FAMClose(fam_conn);
		FAMCONNECTION_GETFD(fam_conn) = -1;
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

static void fam_reopen(FAMConnection *fam_conn,
		       struct event_context *event_ctx,
		       struct fam_notify_ctx *notify_list)
{
	struct fam_notify_ctx *ctx;

	DEBUG(5, ("Re-opening FAM connection\n"));

	FAMClose(fam_conn);

	if (!NT_STATUS_IS_OK(fam_open_connection(fam_conn, event_ctx))) {
		DEBUG(5, ("Re-opening fam connection failed\n"));
		return;
	}

	for (ctx = notify_list; ctx; ctx = ctx->next) {
		FAMMonitorDirectory(fam_conn, ctx->path, &ctx->fr, NULL);
	}
}

static void fam_handler(struct event_context *event_ctx,
			struct fd_event *fd_event,
			uint16 flags,
			void *private_data)
{
	FAMConnection *fam_conn = (FAMConnection *)private_data;
	FAMEvent fam_event;
	struct fam_notify_ctx *ctx;
	char *name;

	if (FAMPending(fam_conn) == 0) {
		DEBUG(10, ("fam_handler called but nothing pending\n"));
		return;
	}

	if (FAMNextEvent(fam_conn, &fam_event) != 1) {
		DEBUG(10, ("FAMNextEvent returned an error\n"));
		TALLOC_FREE(fd_event);
		fam_reopen(fam_conn, event_ctx, fam_notify_list);
		return;
	}

	if ((fam_event.code != FAMCreated) && (fam_event.code != FAMDeleted)) {
		DEBUG(10, ("Ignoring code FAMCode %d for file %s\n",
			   (int)fam_event.code, fam_event.filename));
		return;
	}

	for (ctx = fam_notify_list; ctx; ctx = ctx->next) {
		if (memcmp(&fam_event.fr, &ctx->fr, sizeof(FAMRequest)) == 0) {
			break;
		}
	}

	if (ctx == NULL) {
		DEBUG(5, ("Discarding event for file %s\n",
			  fam_event.filename));
		return;
	}

	if ((name = strrchr_m(fam_event.filename, '\\')) == NULL) {
		name = fam_event.filename;
	}

	notify_fsp(ctx->fsp,
		   fam_event.code == FAMCreated
		   ? NOTIFY_ACTION_ADDED : NOTIFY_ACTION_REMOVED,
		   name);
}

static int fam_notify_ctx_destructor(struct fam_notify_ctx *ctx)
{
	if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
		FAMCancelMonitor(&fam_connection, &ctx->fr);
	}
	DLIST_REMOVE(fam_notify_list, ctx);
	return 0;
}

static void *fam_notify_add(TALLOC_CTX *mem_ctx,
			    struct event_context *event_ctx,
			    files_struct *fsp, uint32 *filter)
{
	struct fam_notify_ctx *ctx;
	pstring fullpath;

	if ((*filter & FILE_NOTIFY_CHANGE_FILE) == 0) {
		DEBUG(10, ("filter = %u, no FILE_NOTIFY_CHANGE_FILE\n",
			   *filter));
		return NULL;
	}

	/* FAM needs an absolute pathname. */

	pstrcpy(fullpath, fsp->fsp_name);
	if (!canonicalize_path(fsp->conn, fullpath)) {
		DEBUG(0, ("failed to canonicalize path '%s'\n", fullpath));
		return NULL;
	}

	if (*fullpath != '/') {
		DEBUG(0, ("canonicalized path '%s' into `%s`\n", fsp->fsp_name,
			  fullpath));
		DEBUGADD(0, ("but expected an absolute path\n"));
		return NULL;
	}
	
	if (!(ctx = TALLOC_P(mem_ctx, struct fam_notify_ctx))) {
		return NULL;
	}

	ctx->fsp = fsp;
	ctx->fam_connection = &fam_connection;

	/*
	 * The FAM module in this early state will only take care of
	 * FAMCreated and FAMDeleted events
	 */

	ctx->filter = FILE_NOTIFY_CHANGE_FILE;

	if (!(ctx->path = talloc_strdup(ctx, fullpath))) {
		DEBUG(0, ("talloc_strdup failed\n"));
		TALLOC_FREE(ctx);
		return NULL;
	}

	/*
	 * Leave the rest to smbd itself
	 */

	*filter &= ~FILE_NOTIFY_CHANGE_FILE;

	DLIST_ADD(fam_notify_list, ctx);
	talloc_set_destructor(ctx, fam_notify_ctx_destructor);

	/*
	 * Only directories monitored so far
	 */

	if (FAMCONNECTION_GETFD(ctx->fam_connection) != -1) {
		FAMMonitorDirectory(ctx->fam_connection, ctx->path, &ctx->fr,
				    NULL);
	}
	else {
		/*
		 * If the re-open is successful, this will establish the
		 * FAMMonitor from the list
		 */
		fam_reopen(ctx->fam_connection, event_ctx, fam_notify_list);
	}

	return ctx;
}

static struct cnotify_fns global_fam_notify =
{
    fam_notify_add,
};

struct cnotify_fns *fam_notify_init(struct event_context *event_ctx)
{

	ZERO_STRUCT(fam_connection);
	FAMCONNECTION_GETFD(&fam_connection) = -1;

	if (!NT_STATUS_IS_OK(fam_open_connection(&fam_connection,
						 event_ctx))) {
		DEBUG(0, ("FAM file change notifications not available\n"));
		return NULL;
	}

	DEBUG(10, ("enabling FAM change notifications\n"));
	return &global_fam_notify;
}

#endif /* HAVE_FAM_CHANGE_NOTIFY */