fswatch: Add fswatch API
PATCH 1/2 fswatch: Add fswatch API
Add the header and the implementation for a filesystem watch API. It's a simple wrapper around the Linux inotify mechanism but not named inotify so as not to preclude using other mechanisms in the implemntation.
--- Makefile.am | 6 +- ell/ell.h | 1 + ell/fswatch.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ell/fswatch.h | 35 +++++++++ 4 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 ell/fswatch.c create mode 100644 ell/fswatch.h
Makefile.am +4 -2
@@ -45,7 +45,8 @@ pkginclude_HEADERS = ell/ell.h \ ell/pkcs5.h \ ell/file.h \ ell/net.h \ - ell/dhcp.h + ell/dhcp.h \ + ell/fswatch.h lib_LTLIBRARIES = ell/libell.la
@@ -104,7 +105,8 @@ ell_libell_la_SOURCES = $(linux_headers) \ ell/dhcp-private.h \ ell/dhcp.c \ ell/dhcp-transport.c \ - ell/dhcp-lease.c + ell/dhcp-lease.c \ + ell/fswatch.c ell_libell_la_LDFLAGS = -no-undefined \ -version-info $(ELL_CURRENT):$(ELL_REVISION):$(ELL_AGE)
ell/ell.h +1 -0
@@ -53,3 +53,4 @@#include <ell/dbus-service.h> #include <ell/dbus-client.h> #include <ell/dhcp.h> +#include <ell/fswatch.h>
ell/fswatch.c new file +231 -0
231 lines not on this page · raw message
ell/fswatch.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* +* Embedded Linux library +* +* Copyright (C) 2018 Intel Corporation. All rights reserved. +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifndef __ELL_FSWATCH_H +#define __ELL_FSWATCH_H + +struct l_fswatch ; + +typedef void (*l_fswatch_cb_t ) (struct l_fswatch *watch,const char *filename, +void *user_data); + +struct l_fswatch *l_fswatch_new (const char *path,l_fswatch_cb_t cb, +void *user_data); +bool l_fswatch_remove (struct l_fswatch *watch); + +#endif /* __ELL_FSWATCH_H */
Signature · 1 line
2.14.1
PATCH 2/2 unit: Add an fswatch unit test
--- .gitignore | 1 + Makefile.am | 5 +- unit/test-fswatch.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 unit/test-fswatch.c
.gitignore +1 -0
@@ -55,6 +55,7 @@ unit/test-uuid unit/test-key unit/test-pbkdf2 unit/test-dhcp +unit/test-fswatch unit/cert-*.pem unit/cert-*.csr unit/cert-*.srl
Makefile.am +4 -1
@@ -147,7 +147,8 @@ unit_tests = unit/test-unit \ unit/test-base64 \ unit/test-uuid \ unit/test-pbkdf2 \ - unit/test-dhcp + unit/test-dhcp \ + unit/test-fswatch dbus_tests = unit/test-hwdb \ unit/test-dbus \
@@ -264,6 +265,8 @@ unit_test_uuid_LDADD = ell/libell-private.la unit_test_dhcp_LDADD = ell/libell-private.la +unit_test_fswatch_LDADD = ell/libell-private.la + if MAINTAINER_MODE noinst_LTLIBRARIES += unit/example-plugin.la endif
unit/test-fswatch.c new file +232 -0
232 lines not on this page · raw message
Signature · 1 line
2.14.1
Hi Andrew,
<snip>
ell/fswatch.c line 43
Quoted 7 lines
ell/fswatch.c new file +7
-0+static struct l_io *inotify_io; +static struct l_queue *watches; +static bool in_notify; +static bool stale_items; + +struct l_fswatch { +char *path;
Why do we store the path? It isn't used anywhere
ell/fswatch.c line 50
Quoted 7 lines
ell/fswatch.c new file +7
-0+int id; +l_fswatch_cb_t cb; +void *user_data; +}; + +#define EVENT_MASK (IN_CREATE | IN_DELETE | IN_DELETE_SELF | \ + IN_MODIFY | IN_MOVE | IN_MOVE_SELF)
Should we expose some of this info? Wouldn't it be useful to know what exactly changed for a given path?
Quoted 12 lines
ell/fswatch.c new file +12
-0+ +typedef void (*l_fswatch_cb_t ) (struct l_fswatch *watch,const char *filename, +void *user_data); + +static void l_fswatch_destroy (void *data) +{ +struct l_fswatch *watch = data; + +l_free (watch->path); +l_free (watch); +} +
ell/fswatch.c line 72
Quoted 10 lines
ell/fswatch.c new file +10
-0+static void l_fswatch_shutdown (void ) +{ +if (!inotify_io) +return ; + +l_io_destroy (inotify_io); + inotify_io =NULL ; + +l_queue_destroy (watches, l_fswatch_destroy); + watches =NULL ;
Every time this function is called the watches queue is empty. It might be better to just hang the watches queue off of the inotify_io and provide a proper destroy callback. That was the queue will be destroyed whenever the inotify_io is destroyed. This probably also buys you auto-destruct at exit as well.
Quoted 36 lines
ell/fswatch.c new file +36
-0+} + +static bool l_fswatch_remove_func (void *data,void *user_data) +{ +struct l_fswatch *watch = data; + +if (watch->cb) +return false; + +l_fswatch_destroy (watch); + +return true; +} + +static bool inotify_read_cb (struct l_io *io,void *user_data) +{ +uint8_t buf[sizeof (struct inotify_event ) +NAME_MAX +1 ]; +const struct inotify_event *event; +ssize_t len; + + len =read (l_io_get_fd (io), buf,sizeof (buf)); +if (unlikely (len <=0 )) +return true; + + event = (struct inotify_event *) buf; + in_notify = true; + +while ((size_t ) len >=sizeof (struct inotify_event ) + event->len) { +const struct l_queue_entry *entry; +const uint8_t *next_ptr; +const char *name =NULL ; + +if (event->len) + name = event->name; + + for (entry = l_queue_get_entries(watches); entry;
ell/fswatch.c line 119
Quoted 10 lines
ell/fswatch.c new file +10
-0+ entry = entry->next) { +struct l_fswatch *watch = entry->data; + +if (watch->id != event->wd) +continue ; + +if (event->mask &IN_IGNORED ) + watch->id = -1; > + + if ((event->mask & EVENT_MASK) && watch->cb) + watch->cb(watch, name, watch->user_data);
This implies that watch->cb can be NULL? See discussion below
Quoted 13 lines
ell/fswatch.c new file +13
-0+ } + + len -=sizeof (struct inotify_event ) + event->len; +/* event->len is supposed to include alignment */ + next_ptr = (const uint8_t *) (event +1 ) + event->len; + event = (struct inotify_event *) next_ptr; + } + + in_notify = false; + +if (stale_items) { +l_queue_foreach_remove (watches, l_fswatch_remove_func,NULL ); + stale_items = false;
ell/fswatch.c line 142
Quoted 10 lines
ell/fswatch.c new file +10
-0+ +if (l_queue_isempty (watches)) +l_fswatch_shutdown (); + } + +return true; +} + +LIB_EXPORT struct l_fswatch *l_fswatch_new(const char *path, l_fswatch_cb_t cb, +void *user_data)
We really need a destroy callback here as well
Quoted 13 lines
ell/fswatch.c new file +13
-0+{ +struct l_fswatch *watch; +int id; + +if (!inotify_io) { +int inotify_fd =inotify_init1 (IN_CLOEXEC ); + +if (unlikely (inotify_fd <0 )) +return NULL ; + + inotify_io =l_io_new (inotify_fd); +if (unlikely (!inotify_io)) { +close (inotify_fd);
ell/fswatch.c line 165
Quoted 10 lines
ell/fswatch.c new file +10
-0+return NULL ; + } + +l_io_set_close_on_destroy (inotify_io, true); + +if (unlikely (!l_io_set_read_handler (inotify_io, inotify_read_cb, +NULL ,NULL ))) { +l_io_destroy (inotify_io); +return NULL ; + }
Can we split this out into a separate function? Most of these lazy-initialization details are irrelevant to l_fswatch_new, and if we ever move to a non-lazy initialization implementation, the changes will be compartmentalized better.
Quoted 20 lines
ell/fswatch.c new file +20
-0+ } + +/* +* inotify_watch_add already checks if the path is already being +* watched and will return the old watch ID so we're fine. +*/ + id =inotify_add_watch (l_io_get_fd (inotify_io), path, +EVENT_MASK |IN_EXCL_UNLINK ); +if (unlikely (id <0 )) { +if (l_queue_isempty (watches)) +l_fswatch_shutdown (); + +return NULL ; + } + + watch = l_new(struct l_fswatch, 1); + watch->path =l_strdup (path); + watch->id = id; + watch->cb = cb; + watch->user_data = user_data;
ell/fswatch.c line 195
Quoted 10 lines
ell/fswatch.c new file +10
-0+ +if (!watches) + watches =l_queue_new (); + +l_queue_push_tail (watches, watch); + +return watch; +} + +LIB_EXPORT bool l_fswatch_remove(struct l_fswatch *watch)
Not sure why half of our APIs use _remove and the other half uses _destroy. But why would this return a bool? That seems wrong, it should be void.
Quoted 15 lines
ell/fswatch.c new file +15
-0+{ +const struct l_queue_entry *entry; +int id; + +if (unlikely (!inotify_io || !watch)) +return false; + + id = watch->id; + +/* Check if we have any other watch with the same inotify ID */ +for (entry =l_queue_get_entries (watches); entry; + entry = entry->next) { +struct l_fswatch *watch2 = entry->data; + +if (watch2 != watch && watch2->id == id)
ell/fswatch.c line 220
Quoted 10 lines
ell/fswatch.c new file +10
-0+break ; + } + +if (!entry && id !=-1 ) +if (inotify_rm_watch (l_io_get_fd (inotify_io), id) <0 ) +return false; + +if (in_notify) { + watch->cb =NULL ; + stale_items = false;
should this be true actually? Also, you're not checking that cb is non-null in l_fswatch_new. So we should either introduce a different guard variable or ensure that cb isn't NULL at first.
ell/fswatch.c line 231
Quoted 11 lines
ell/fswatch.c new file +11
-0+return true; + } + +l_queue_remove (watches, watch); +l_fswatch_destroy (watch); + +if (l_queue_isempty (watches)) +l_fswatch_shutdown (); + +return true; +}
Regards,
-Denis
Hi Denis,
Quoted 4 lines · Denis Kenzior
Hi Andrew,
<snip>
Quoted 10 lines
ell/fswatch.c line 43
Quoted 7 lines
ell/fswatch.c new file +7
-0+static struct l_io *inotify_io; +static struct l_queue *watches; +static bool in_notify; +static bool stale_items; + +struct l_fswatch { +char *path;Why do we store the path? It isn't used anywhere.
True, I was thinking about having an accessor similar to l_io_get_fd but we would first need to figure out how to update the path on the IN_MOVE_SELF event.
Quoted 12 lines
ell/fswatch.c line 50
Quoted 8 lines
ell/fswatch.c new file +7
-0+int id; +l_fswatch_cb_t cb; +void *user_data; +}; + +#define EVENT_MASK (IN_CREATE | IN_DELETE | IN_DELETE_SELF | \ + IN_MODIFY | IN_MOVE | IN_MOVE_SELF)Should we expose some of this info? Wouldn't it be useful to know what exactly changed for a given path?
It might be useful, I can add an enum with values such as L_FSWATCH_EVENT_CREATE / MOVE / MODIFY / DELETE.
However relying on this info is seemingly discouraged by inotify(7) because there's no race-free way to use it. For our iwd use case we probably want to always reload the affected files and having this information isn't of any help.
Quoted 21 lines
Quoted 21 lines
(quoted diff) +2
-0+ +typedef void (*l_fswatch_cb_t) (struct l_fswatch *watch, const char*filename,
ell/fswatch.c new file +17
-0+ void *user_data); + +static void l_fswatch_destroy (void *data) +{ +struct l_fswatch *watch = data; + +l_free (watch->path); +l_free (watch); +} + +static void l_fswatch_shutdown (void ) +{ +if (!inotify_io) +return ; + +l_io_destroy (inotify_io); + inotify_io =NULL ;
Quoted 8 lines
ell/fswatch.c line 72
Quoted 3 lines
ell/fswatch.c new file +3
-0+ +l_queue_destroy (watches, l_fswatch_destroy); + watches =NULL ;Every time this function is called the watches queue is empty. It might be better to just hang the watches queue off of the
inotify_ioand provide a proper destroy callback.
We will still need a global variable for the queue because we need to access it when adding or removing watches.
Quoted 3 lines
That was the queue will be destroyed whenever the
inotify_iois destroyed. This probably also buys you auto-destruct at exit as well.
Ok, yep, it makes sense to move this part to the inotify_io's read destroy callback.
Quoted 73 lines
Quoted 39 lines
ell/fswatch.c new file +28
-0+} + +static bool l_fswatch_remove_func (void *data,void *user_data) +{ +struct l_fswatch *watch = data; + +if (watch->cb) +return false; + +l_fswatch_destroy (watch); + +return true; +} + +static bool inotify_read_cb (struct l_io *io,void *user_data) +{ +uint8_t buf[sizeof (struct inotify_event ) +NAME_MAX +1 ]; +const struct inotify_event *event; +ssize_t len; + + len =read (l_io_get_fd (io), buf,sizeof (buf)); +if (unlikely (len <=0 )) +return true; + + event = (struct inotify_event *) buf; + in_notify = true; + +while ((size_t ) len >=sizeof (struct inotify_event ) + event->len){
ell/fswatch.c new file +8
-0+const struct l_queue_entry *entry; +const uint8_t *next_ptr; +const char *name =NULL ; + +if (event->len) + name = event->name; + + for (entry = l_queue_get_entries(watches); entry;ell/fswatch.c line 119
Quoted 10 lines
ell/fswatch.c new file +10
-0+ entry = entry->next) { +struct l_fswatch *watch = entry->data; + +if (watch->id != event->wd) +continue ; + +if (event->mask &IN_IGNORED ) + watch->id = -1; > + + if ((event->mask & EVENT_MASK) && watch->cb) + watch->cb(watch, name, watch->user_data);This implies that
watch->cbcan be NULL? See discussion belowQuoted 20 lines
ell/fswatch.c new file +12
-0+ } + + len -=sizeof (struct inotify_event ) + event->len; +/* event->len is supposed to include alignment */ + next_ptr = (const uint8_t *) (event +1 ) + event->len; + event = (struct inotify_event *) next_ptr; + } + + in_notify = false; + +if (stale_items) { + l_queue_foreach_remove(watches, l_fswatch_remove_func,NULL);
ell/fswatch.c new file +7
-0+ stale_items = false; + +if (l_queue_isempty (watches)) +l_fswatch_shutdown (); + } + +return true;
Quoted 8 lines
Quoted 5 lines
ell/fswatch.c new file +3
-0+} + +LIB_EXPORT struct l_fswatch *l_fswatch_new(const char *path,
l_fswatch_cb_tcb,
+ void*user_data)We really need a destroy callback here as well
Ok, then I guess I can actually free the watches for which the kernel sends us IN_IGNORED events and call the destroy callbacks, rather than just set watch->id to -1.
Quoted 23 lines
Quoted 23 lines
ell/fswatch.c new file +19
-0+{ + struct l_fswatch *watch; + int id; + + if (!inotify_io) { + int inotify_fd = inotify_init1(IN_CLOEXEC); + + if (unlikely(inotify_fd < 0)) + return NULL; + + inotify_io = l_io_new(inotify_fd); + if (unlikely(!inotify_io)) { + close(inotify_fd); + return NULL; + } + + l_io_set_close_on_destroy(inotify_io, true); + + if (unlikely(!l_io_set_read_handler(inotify_io,
inotify_read_cb,ell/fswatch.c new file +2
-0+ NULL, NULL))) { +l_io_destroy (inotify_io);
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return NULL; + }Can we split this out into a separate function? Most of these lazy-initialization details are irrelevant to
l_fswatch_new, and if we ever move to a non-lazy initialization implementation, the changes will be compartmentalized better.
Ok.
Quoted 29 lines
Quoted 29 lines
ell/fswatch.c new file +8
-0+ } + +/* +* inotify_watch_add already checks if the path is already being +* watched and will return the old watch ID so we're fine. +*/ + id = inotify_add_watch(l_io_get_fd(inotify_io), path, + EVENT_MASK |
IN_EXCL_UNLINK);ell/fswatch.c new file +18
-0+if (unlikely (id <0 )) { +if (l_queue_isempty (watches)) +l_fswatch_shutdown (); + +return NULL ; + } + + watch = l_new(struct l_fswatch, 1); + watch->path =l_strdup (path); + watch->id = id; + watch->cb = cb; + watch->user_data = user_data; + +if (!watches) + watches =l_queue_new (); + +l_queue_push_tail (watches, watch); +
Quoted 8 lines
ell/fswatch.c line 195
Quoted 4 lines
ell/fswatch.c new file +4
-0+return watch; +} + +LIB_EXPORT bool l_fswatch_remove(struct l_fswatch *watch)Not sure why half of our APIs use
_removeand the other half uses_destroy. But why would this return a bool? That seems wrong, it should be void.
It's simliar to l_queue_remove and it may fail (in this case also because of IO) although it's different from l_queue_remove in that it also frees the object so I can rename it to _destroy.
Quoted 22 lines
Quoted 22 lines
ell/fswatch.c new file +20
-0+{ +const struct l_queue_entry *entry; +int id; + +if (unlikely (!inotify_io || !watch)) +return false; + + id = watch->id; + +/* Check if we have any other watch with the same inotify ID */ +for (entry =l_queue_get_entries (watches); entry; + entry = entry->next) { +struct l_fswatch *watch2 = entry->data; + +if (watch2 != watch && watch2->id == id) +break ; + } + +if (!entry && id !=-1 ) +if (inotify_rm_watch (l_io_get_fd (inotify_io), id) <0 )
Quoted 8 lines
ell/fswatch.c line 220
Quoted 5 lines
ell/fswatch.c new file +5
-0+return false; + +if (in_notify) { + watch->cb =NULL ; + stale_items = false;should this be true actually?
Oops, yes it should.
I was actually testing this code path in the unit test but changed the test before running it through valgrind.
Quoted 3 lines
Also, you're not checking that cb is non-null
inl_fswatch_new. So we should either introduce a different guard variable or ensure that cb isn't NULL at first.
Ok.
Best regards