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
|
/*
* Copyright (c) 2017 Balabit
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "directory-monitor-inotify.h"
#include "messages.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct _DirectoryMonitorInotify
{
DirectoryMonitor super;
struct iv_inotify inotify;
struct iv_inotify_watch watcher;
} DirectoryMonitorInotify;
static DirectoryMonitorEventType
_get_event_type(struct inotify_event *event, gchar *filename)
{
if ((event->mask & IN_CREATE) || (event->mask & IN_MOVED_TO))
{
if (event->mask & IN_ISDIR)
{
return DIRECTORY_CREATED;
}
return FILE_CREATED;
}
else if ((event->mask & IN_DELETE) || (event->mask & IN_MOVED_FROM))
{
return FILE_DELETED;
}
else if ((event->mask & IN_DELETE_SELF) || (event->mask & IN_MOVE_SELF))
{
return DIRECTORY_DELETED;
}
return UNKNOWN;
}
static void
_handle_event(gpointer s, struct inotify_event *event)
{
DirectoryMonitorInotify *self = (DirectoryMonitorInotify *)s;
DirectoryMonitorEvent dir_event;
dir_event.name = g_strdup_printf("%.*s", event->len, &event->name[0]);
dir_event.full_path = build_filename(self->super.real_path, dir_event.name);
dir_event.event_type = _get_event_type(event, dir_event.full_path);
if (self->super.callback && dir_event.event_type != UNKNOWN)
{
self->super.callback(&dir_event, self->super.callback_data);
}
g_free(dir_event.full_path);
g_free((gchar *)dir_event.name);
}
static void
_start_watches(DirectoryMonitor *s)
{
DirectoryMonitorInotify *self = (DirectoryMonitorInotify *)s;
IV_INOTIFY_WATCH_INIT(&self->watcher);
self->watcher.inotify = &self->inotify;
self->watcher.pathname = self->super.dir;
self->watcher.mask = IN_CREATE | IN_DELETE | IN_MOVE | IN_DELETE_SELF | IN_MOVE_SELF;
self->watcher.cookie = self;
self->watcher.handler = _handle_event;
msg_trace("Starting to watch directory changes", evt_tag_str("dir", self->super.dir));
iv_inotify_watch_register(&self->watcher);
}
static void
_stop_watches(DirectoryMonitor *s)
{
DirectoryMonitorInotify *self = (DirectoryMonitorInotify *)s;
iv_inotify_watch_unregister(&self->watcher);
}
static void
_free(DirectoryMonitor *s)
{
DirectoryMonitorInotify *self = (DirectoryMonitorInotify *)s;
iv_inotify_unregister(&self->inotify);
}
DirectoryMonitor *
directory_monitor_inotify_new(const gchar *dir, guint recheck_time)
{
DirectoryMonitorInotify *self = g_new0(DirectoryMonitorInotify, 1);
directory_monitor_init_instance(&self->super, dir, recheck_time, "inotify");
IV_INOTIFY_INIT(&self->inotify);
if (iv_inotify_register(&self->inotify))
{
msg_error("directory-monitor-inotify: could not create inotify object, you may need to increase /proc/sys/fs/inotify/max_user_instances",
evt_tag_error("errno"));
directory_monitor_free(&self->super);
return NULL;
}
self->super.start_watches = _start_watches;
self->super.stop_watches = _stop_watches;
self->super.free_fn = _free;
return &self->super;
}
|