00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "includes.h"
00026
00027 #ifdef HAVE_INOTIFY
00028
00029 #ifdef HAVE_ASM_TYPES_H
00030 #include <asm/types.h>
00031 #endif
00032
00033 #ifndef HAVE_INOTIFY_INIT
00034
00035 #include <linux/inotify.h>
00036 #include <asm/unistd.h>
00037
00038
00039
00040
00041
00042 static int inotify_init(void)
00043 {
00044 return syscall(__NR_inotify_init);
00045 }
00046
00047 static int inotify_add_watch(int fd, const char *path, __u32 mask)
00048 {
00049 return syscall(__NR_inotify_add_watch, fd, path, mask);
00050 }
00051
00052 static int inotify_rm_watch(int fd, int wd)
00053 {
00054 return syscall(__NR_inotify_rm_watch, fd, wd);
00055 }
00056 #else
00057
00058 #include <sys/inotify.h>
00059
00060 #endif
00061
00062
00063
00064 #ifndef IN_ONLYDIR
00065 #define IN_ONLYDIR 0x01000000
00066 #endif
00067 #ifndef IN_MASK_ADD
00068 #define IN_MASK_ADD 0x20000000
00069 #endif
00070
00071 struct inotify_private {
00072 struct sys_notify_context *ctx;
00073 int fd;
00074 struct inotify_watch_context *watches;
00075 BOOL broken_inotify;
00076 };
00077
00078 struct inotify_watch_context {
00079 struct inotify_watch_context *next, *prev;
00080 struct inotify_private *in;
00081 int wd;
00082 void (*callback)(struct sys_notify_context *ctx,
00083 void *private_data,
00084 struct notify_event *ev);
00085 void *private_data;
00086 uint32_t mask;
00087 uint32_t filter;
00088 const char *path;
00089 };
00090
00091
00092
00093
00094
00095 static int inotify_destructor(struct inotify_private *in)
00096 {
00097 close(in->fd);
00098 return 0;
00099 }
00100
00101
00102
00103
00104
00105
00106 static BOOL filter_match(struct inotify_watch_context *w,
00107 struct inotify_event *e)
00108 {
00109 DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
00110 e->mask, w->mask, w->filter));
00111
00112 if ((e->mask & w->mask) == 0) {
00113
00114
00115 return False;
00116 }
00117
00118
00119 if (e->mask & IN_ISDIR) {
00120 if ((w->filter & FILE_NOTIFY_CHANGE_DIR_NAME) == 0) {
00121 return False;
00122 }
00123 } else {
00124 if ((e->mask & IN_ATTRIB) &&
00125 (w->filter & (FILE_NOTIFY_CHANGE_ATTRIBUTES|
00126 FILE_NOTIFY_CHANGE_LAST_WRITE|
00127 FILE_NOTIFY_CHANGE_LAST_ACCESS|
00128 FILE_NOTIFY_CHANGE_EA|
00129 FILE_NOTIFY_CHANGE_SECURITY))) {
00130 return True;
00131 }
00132 if ((e->mask & IN_MODIFY) &&
00133 (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
00134 return True;
00135 }
00136 if ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
00137 return False;
00138 }
00139 }
00140
00141 return True;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 static void inotify_dispatch(struct inotify_private *in,
00152 struct inotify_event *e,
00153 uint32_t prev_cookie,
00154 struct inotify_event *e2)
00155 {
00156 struct inotify_watch_context *w, *next;
00157 struct notify_event ne;
00158
00159 DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
00160 e->mask, e->len ? e->name : ""));
00161
00162
00163 if ((e->mask & (IN_ATTRIB|IN_MODIFY|IN_CREATE|IN_DELETE|
00164 IN_MOVED_FROM|IN_MOVED_TO)) == 0) {
00165 return;
00166 }
00167
00168
00169
00170 if (e->mask & IN_CREATE) {
00171 ne.action = NOTIFY_ACTION_ADDED;
00172 } else if (e->mask & IN_DELETE) {
00173 ne.action = NOTIFY_ACTION_REMOVED;
00174 } else if (e->mask & IN_MOVED_FROM) {
00175 if (e2 != NULL && e2->cookie == e->cookie) {
00176 ne.action = NOTIFY_ACTION_OLD_NAME;
00177 } else {
00178 ne.action = NOTIFY_ACTION_REMOVED;
00179 }
00180 } else if (e->mask & IN_MOVED_TO) {
00181 if (e->cookie == prev_cookie) {
00182 ne.action = NOTIFY_ACTION_NEW_NAME;
00183 } else {
00184 ne.action = NOTIFY_ACTION_ADDED;
00185 }
00186 } else {
00187 ne.action = NOTIFY_ACTION_MODIFIED;
00188 }
00189 ne.path = e->name;
00190
00191 DEBUG(10, ("inotify_dispatch: ne.action = %d, ne.path = %s\n",
00192 ne.action, ne.path));
00193
00194
00195 for (w=in->watches;w;w=next) {
00196 next = w->next;
00197 if (w->wd == e->wd && filter_match(w, e)) {
00198 w->callback(in->ctx, w->private_data, &ne);
00199 }
00200 }
00201
00202
00203
00204
00205 if (ne.action != NOTIFY_ACTION_NEW_NAME ||
00206 (e->mask & IN_ISDIR) != 0) {
00207 return;
00208 }
00209
00210 ne.action = NOTIFY_ACTION_MODIFIED;
00211 e->mask = IN_ATTRIB;
00212
00213 for (w=in->watches;w;w=next) {
00214 next = w->next;
00215 if (w->wd == e->wd && filter_match(w, e) &&
00216 !(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
00217 w->callback(in->ctx, w->private_data, &ne);
00218 }
00219 }
00220 }
00221
00222
00223
00224
00225 static void inotify_handler(struct event_context *ev, struct fd_event *fde,
00226 uint16_t flags, void *private_data)
00227 {
00228 struct inotify_private *in = talloc_get_type(private_data,
00229 struct inotify_private);
00230 int bufsize = 0;
00231 struct inotify_event *e0, *e;
00232 uint32_t prev_cookie=0;
00233
00234
00235
00236
00237
00238
00239
00240 if ((ioctl(in->fd, FIONREAD, &bufsize) != 0) && (errno == EACCES)) {
00241
00242
00243
00244 TALLOC_FREE(fde);
00245 in->broken_inotify = True;
00246 return;
00247 }
00248 if (bufsize == 0) {
00249 DEBUG(0,("No data on inotify fd?!\n"));
00250 return;
00251 }
00252
00253 e0 = e = (struct inotify_event *)TALLOC_SIZE(in, bufsize);
00254 if (e == NULL) return;
00255
00256 if (read(in->fd, e0, bufsize) != bufsize) {
00257 DEBUG(0,("Failed to read all inotify data\n"));
00258 talloc_free(e0);
00259 return;
00260 }
00261
00262
00263 while (bufsize >= sizeof(*e)) {
00264 struct inotify_event *e2 = NULL;
00265 bufsize -= e->len + sizeof(*e);
00266 if (bufsize >= sizeof(*e)) {
00267 e2 = (struct inotify_event *)(e->len + sizeof(*e) + (char *)e);
00268 }
00269 inotify_dispatch(in, e, prev_cookie, e2);
00270 prev_cookie = e->cookie;
00271 e = e2;
00272 }
00273
00274 talloc_free(e0);
00275 }
00276
00277
00278
00279
00280
00281 static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
00282 {
00283 struct inotify_private *in;
00284
00285 if (!lp_parm_bool(-1, "notify", "inotify", True)) {
00286 return NT_STATUS_INVALID_SYSTEM_SERVICE;
00287 }
00288
00289 in = talloc(ctx, struct inotify_private);
00290 NT_STATUS_HAVE_NO_MEMORY(in);
00291 in->fd = inotify_init();
00292 if (in->fd == -1) {
00293 DEBUG(0,("Failed to init inotify - %s\n", strerror(errno)));
00294 talloc_free(in);
00295 return map_nt_error_from_unix(errno);
00296 }
00297 in->ctx = ctx;
00298 in->watches = NULL;
00299 in->broken_inotify = False;
00300
00301 ctx->private_data = in;
00302 talloc_set_destructor(in, inotify_destructor);
00303
00304
00305 event_add_fd(ctx->ev, in, in->fd, EVENT_FD_READ, inotify_handler, in);
00306
00307 return NT_STATUS_OK;
00308 }
00309
00310
00311
00312
00313
00314
00315 static const struct {
00316 uint32_t notify_mask;
00317 uint32_t inotify_mask;
00318 } inotify_mapping[] = {
00319 {FILE_NOTIFY_CHANGE_FILE_NAME, IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
00320 {FILE_NOTIFY_CHANGE_DIR_NAME, IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
00321 {FILE_NOTIFY_CHANGE_ATTRIBUTES, IN_ATTRIB|IN_MOVED_TO|IN_MOVED_FROM|IN_MODIFY},
00322 {FILE_NOTIFY_CHANGE_LAST_WRITE, IN_ATTRIB},
00323 {FILE_NOTIFY_CHANGE_LAST_ACCESS, IN_ATTRIB},
00324 {FILE_NOTIFY_CHANGE_EA, IN_ATTRIB},
00325 {FILE_NOTIFY_CHANGE_SECURITY, IN_ATTRIB}
00326 };
00327
00328 static uint32_t inotify_map(struct notify_entry *e)
00329 {
00330 int i;
00331 uint32_t out=0;
00332 for (i=0;i<ARRAY_SIZE(inotify_mapping);i++) {
00333 if (inotify_mapping[i].notify_mask & e->filter) {
00334 out |= inotify_mapping[i].inotify_mask;
00335 e->filter &= ~inotify_mapping[i].notify_mask;
00336 }
00337 }
00338 return out;
00339 }
00340
00341
00342
00343
00344 static int watch_destructor(struct inotify_watch_context *w)
00345 {
00346 struct inotify_private *in = w->in;
00347 int wd = w->wd;
00348 DLIST_REMOVE(w->in->watches, w);
00349
00350
00351 for (w=in->watches;w;w=w->next) {
00352 if (w->wd == wd) break;
00353 }
00354 if (w == NULL) {
00355 DEBUG(10, ("Deleting inotify watch %d\n", wd));
00356 if (inotify_rm_watch(in->fd, wd) == -1) {
00357 DEBUG(1, ("inotify_rm_watch returned %s\n",
00358 strerror(errno)));
00359 }
00360
00361 }
00362 return 0;
00363 }
00364
00365
00366
00367
00368
00369
00370 NTSTATUS inotify_watch(struct sys_notify_context *ctx,
00371 struct notify_entry *e,
00372 void (*callback)(struct sys_notify_context *ctx,
00373 void *private_data,
00374 struct notify_event *ev),
00375 void *private_data,
00376 void *handle_p)
00377 {
00378 struct inotify_private *in;
00379 int wd;
00380 uint32_t mask;
00381 struct inotify_watch_context *w;
00382 uint32_t filter = e->filter;
00383 void **handle = (void **)handle_p;
00384
00385
00386 if (ctx->private_data == NULL) {
00387 NTSTATUS status;
00388 status = inotify_setup(ctx);
00389 NT_STATUS_NOT_OK_RETURN(status);
00390 }
00391
00392 in = talloc_get_type(ctx->private_data, struct inotify_private);
00393
00394 if (in->broken_inotify) {
00395 return NT_STATUS_OK;
00396 }
00397
00398 mask = inotify_map(e);
00399 if (mask == 0) {
00400
00401 return NT_STATUS_INVALID_PARAMETER;
00402 }
00403
00404
00405
00406 mask |= (IN_MASK_ADD | IN_ONLYDIR);
00407
00408
00409 wd = inotify_add_watch(in->fd, e->path, mask);
00410 if (wd == -1) {
00411 e->filter = filter;
00412 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(errno)));
00413 return map_nt_error_from_unix(errno);
00414 }
00415
00416 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
00417 e->path, mask, wd));
00418
00419 w = talloc(in, struct inotify_watch_context);
00420 if (w == NULL) {
00421 inotify_rm_watch(in->fd, wd);
00422 e->filter = filter;
00423 return NT_STATUS_NO_MEMORY;
00424 }
00425
00426 w->in = in;
00427 w->wd = wd;
00428 w->callback = callback;
00429 w->private_data = private_data;
00430 w->mask = mask;
00431 w->filter = filter;
00432 w->path = talloc_strdup(w, e->path);
00433 if (w->path == NULL) {
00434 inotify_rm_watch(in->fd, wd);
00435 e->filter = filter;
00436 return NT_STATUS_NO_MEMORY;
00437 }
00438
00439 (*handle) = w;
00440
00441 DLIST_ADD(in->watches, w);
00442
00443
00444 talloc_set_destructor(w, watch_destructor);
00445
00446 return NT_STATUS_OK;
00447 }
00448
00449 #endif