Skip to contentSkip to conversation

fswatch: Add fswatch API

PATCH In review Andrew Zaborowski 2 participants 10 messages Jul 20, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

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 implementation.

---
 Makefile.am   |   6 +-
 ell/ell.h     |   1 +
 ell/fswatch.c | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/fswatch.h |  45 ++++++++++
 4 files changed, 307 insertions(+), 2 deletions(-)
 create mode 100644 ell/fswatch.c
 create mode 100644 ell/fswatch.h
Makefile.am +4 -2
index 100e24c..8203fd4 100644
@@ -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
index 03bb61d..c03faf2 100644
@@ -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 +257 -0

257 lines not on this page · raw message

ell/fswatch.h new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.14.1
#1 lore raw

PATCH 2/2 unit: Add an fswatch unit test

---
 .gitignore          |   1 +
 Makefile.am         |   5 +-
 unit/test-fswatch.c | 290 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-fswatch.c
.gitignore +1 -0
index 4dc3777..6c9ffb0 100644
@@ -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
index 8203fd4..9fe00fb 100644
@@ -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 +290 -0

290 lines not on this page · raw message

Signature · 1 line
2.14.1
#2 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

ell/fswatch.c line 85

Quoted 8 lines
ell/fswatch.c new file +8 -0
+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;
+	enum l_fswatch_event event_type;
+
+	len = read(l_io_get_fd(io), buf, sizeof(buf));

This might need a TEMP_FAILURE_RETRY()

ell/fswatch.c line 98

Quoted 13 lines
ell/fswatch.c new file +13 -0
+	if (unlikely(len <= 0))
+		return true;
+
+	event = (struct inotify_event *) buf;
+
+	if (event->mask & IN_CREATE)
+		event_type = L_FSWATCH_EVENT_CREATE;
+	else if (event->mask & (IN_DELETE | IN_DELETE_SELF))
+		event_type = L_FSWATCH_EVENT_DELETE;
+	else if (event->mask & (IN_MOVE | IN_MOVE_SELF))
+		event_type = L_FSWATCH_EVENT_MOVE;
+	else
+		event_type = L_FSWATCH_EVENT_MODIFY;

Why is this outside the while loop below? It sounds like there may be multiple inotify events during a single read and each one would have a different mask, no?

In fact, this might be better written as a for loop, similar to how the manpage does it.

Quoted 52 lines
ell/fswatch.c new file +52 -0
+
+	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;
+				entry = entry->next) {
+			struct l_fswatch *watch = entry->data;
+
+			if (watch->id != event->wd)
+				continue;
+
+			if ((event->mask & EVENT_MASK) && watch->cb)
+				watch->cb(watch, name, event_type,
+						watch->user_data);
+
+			if (event->mask & IN_IGNORED) {
+				stale_items = true;
+				watch->cb = NULL;
+			}
+		}
+
+		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) {
+		struct l_fswatch *watch;
+
+		while ((watch = l_queue_remove_if(watches,
+						l_fswatch_remove_func, NULL)))
+			l_fswatch_free(watch);
+
+		stale_items = false;
+
+		if (l_queue_isempty(watches))
+			l_fswatch_shutdown();
+	}
+
+	return true;
+}
+

<snip>

ell/fswatch.c line 221

Quoted 2 lines
ell/fswatch.c new file +2 -0
+
+LIB_EXPORT bool l_fswatch_destroy(struct l_fswatch *watch)

I still don't see why this returns bool, what can the caller do if the call fails?

ell/fswatch.c line 227

Quoted 6 lines
ell/fswatch.c new file +6 -0
+{
+	const struct l_queue_entry *entry;
+	int id;
+
+	if (unlikely(!inotify_io || !watch))
+		return false;

This can just as easily be void

Quoted 5 lines
ell/fswatch.c new file +5 -0
+
+	id = watch->id;
+
+	/* Check if we have any other watch with the same inotify ID */
+	for (entry = l_queue_get_entries(watches); entry;

ell/fswatch.c line 242

Quoted 10 lines
ell/fswatch.c new file +10 -0
+			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)
+			return false;

So how do we delete the struct l_fswatch *watch memory here?

Quoted 15 lines
ell/fswatch.c new file +15 -0
+
+	if (in_notify) {
+		watch->cb = NULL;
+		stale_items = true;
+		return true;
+	}
+
+	l_queue_remove(watches, watch);
+	l_fswatch_free(watch);
+
+	if (l_queue_isempty(watches))
+		l_fswatch_shutdown();
+
+	return true;
+}

<snip>

ell/fswatch.h line 38

Quoted 2 lines
ell/fswatch.h new file +2 -0
+typedef void (*l_fswatch_destroy_cb_t) (struct l_fswatch *watch,
+					void *user_data);

Why would this destroy callback signature be different from all others? It also precludes us from doing something like:

l_fswatch_new("/foo/bar", cb, u, (l_fswatch_destroy_cb_t) l_free);

Regards,
-Denis

#3 lore raw

Hi Denis,

Quoted 11 lines · Denis Kenzior

ell/fswatch.c line 85

Quoted 8 lines
ell/fswatch.c new file +8 -0
+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;
+       enum l_fswatch_event event_type;
+
+       len = read(l_io_get_fd(io), buf, sizeof(buf));

This might need a TEMP_FAILURE_RETRY()

In theory it shouldn't because if we don't manage to read the event the main loop will keep calling us but let me do this anyway.

Quoted 11 lines
Quoted 11 lines
ell/fswatch.c new file +10 -0
+       if (unlikely(len <= 0))
+               return true;
+
+       event = (struct inotify_event *) buf;
+
+       if (event->mask & IN_CREATE)
+               event_type = L_FSWATCH_EVENT_CREATE;
+       else if (event->mask & (IN_DELETE | IN_DELETE_SELF))
+               event_type = L_FSWATCH_EVENT_DELETE;
+       else if (event->mask & (IN_MOVE | IN_MOVE_SELF))
Quoted 8 lines

ell/fswatch.c line 98

Quoted 3 lines
ell/fswatch.c new file +3 -0
+               event_type = L_FSWATCH_EVENT_MOVE;
+       else
+               event_type = L_FSWATCH_EVENT_MODIFY;

Why is this outside the while loop below? It sounds like there may be multiple inotify events during a single read and each one would have a different mask, no?

Oops, yes, totally right.

Quoted 63 lines

In fact, this might be better written as a for loop, similar to how the manpage does it.

Quoted 54 lines
ell/fswatch.c new file +4 -0
+
+       in_notify = true;
+
+       while ((size_t) len >= sizeof(struct inotify_event) + event->len)

{

ell/fswatch.c new file +37 -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;
+                               entry = entry->next) {
+                       struct l_fswatch *watch = entry->data;
+
+                       if (watch->id != event->wd)
+                               continue;
+
+                       if ((event->mask & EVENT_MASK) && watch->cb)
+                               watch->cb(watch, name, event_type,
+                                               watch->user_data);
+
+                       if (event->mask & IN_IGNORED) {
+                               stale_items = true;
+                               watch->cb = NULL;
+                       }
+               }
+
+               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) {
+               struct l_fswatch *watch;
+
+               while ((watch = l_queue_remove_if(watches,
+                                               l_fswatch_remove_func,

NULL)))

ell/fswatch.c new file +11 -0
+                       l_fswatch_free(watch);
+
+               stale_items = false;
+
+               if (l_queue_isempty(watches))
+                       l_fswatch_shutdown();
+       }
+
+       return true;
+}
+

<snip>

Quoted 6 lines

ell/fswatch.c line 221

Quoted 2 lines
ell/fswatch.c new file +2 -0
+
+LIB_EXPORT bool l_fswatch_destroy(struct l_fswatch *watch)

I still don't see why this returns bool, what can the caller do if the call fails?

Not much, but we can't do much either. We can free the memeory, yes, but should we hide the error?

Quoted 49 lines

ell/fswatch.c line 227

Quoted 7 lines
ell/fswatch.c new file +6 -0
+{
+       const struct l_queue_entry *entry;
+       int id;
+
+       if (unlikely(!inotify_io || !watch))
+               return false;

This can just as easily be void

Quoted 5 lines
ell/fswatch.c new file +5 -0
+
+       id = watch->id;
+
+       /* Check if we have any other watch with the same inotify ID */
+       for (entry = l_queue_get_entries(watches); entry;

ell/fswatch.c line 242

Quoted 10 lines
ell/fswatch.c new file +10 -0
+                       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)
+                       return false;

So how do we delete the struct l_fswatch *watch memory here?

Quoted 15 lines
ell/fswatch.c new file +15 -0
+
+       if (in_notify) {
+               watch->cb = NULL;
+               stale_items = true;
+               return true;
+       }
+
+       l_queue_remove(watches, watch);
+       l_fswatch_free(watch);
+
+       if (l_queue_isempty(watches))
+               l_fswatch_shutdown();
+
+       return true;
+}

<snip>

Quoted 8 lines

ell/fswatch.h line 38

Quoted 2 lines
ell/fswatch.h new file +2 -0
+typedef void (*l_fswatch_destroy_cb_t) (struct l_fswatch *watch,
+                                       void *user_data);

Why would this destroy callback signature be different from all others? It also precludes us from doing something like:

l_fswatch_new("/foo/bar", cb, u, (l_fswatch_destroy_cb_t) l_free);

Ok, thought it might be useful to pass the watch pointer, it doesn't cost us anything and could simplify things for some users.

Best regards

#4 lore raw

Hi Andrew,

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+LIB_EXPORT bool l_fswatch_destroy(struct l_fswatch *watch)

I still don't see why this returns bool, what can the caller do if the call fails?

Not much, but we can't do much either. We can free the memeory, yes, but should we hide the error?

There's no point returning an error out to the user if they can't do anything about it, especially if it leaves them powerless from reclaiming memory allocated to the watch.

So yes, it is probably better to return void (and maybe print an l_error) than to return an error here.

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
ell/fswatch.h new file +2 -0
+typedef void (*l_fswatch_destroy_cb_t) (struct l_fswatch *watch,
+                                       void *user_data);
Quoted 8 lines
Quoted 4 lines

Why would this destroy callback signature be different from all others? It also precludes us from doing something like:

l_fswatch_new("/foo/bar", cb, u, (l_fswatch_destroy_cb_t) l_free);

Ok, thought it might be useful to pass the watch pointer, it doesn't cost us anything and could simplify things for some users.

This is useful for callbacks, but almost never happens for destroy callbacks. In too many cases the destroy callback is just l_free or some_structure_destroy(struct some_structure). So having to come up with another function just to have the correct signature is wasteful. For the 1-5% usecase where this might be useful, the caller can carry the fswatch pointer in the userdata structure itself.

Regards,
-Denis

#5 lore raw
Quoted 10 lines · Denis Kenzior
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+LIB_EXPORT bool l_fswatch_destroy(struct l_fswatch *watch)

I still don't see why this returns bool, what can the caller do if the call
fails?

Not much, but we can't do much either. We can free the memeory, yes, but should we hide the error?

Quoted 6 lines

There's no point returning an error out to the user if they can't do anything about it, especially if it leaves them powerless from reclaiming memory allocated to the watch.

So yes, it is probably better to return void (and maybe print an l_error) than to return an error here.

Ok.

Quoted 17 lines
Quoted 10 lines
Quoted 10 lines

ell/fswatch.h line 38

Quoted 3 lines
ell/fswatch.h new file +2 -0
+typedef void (*l_fswatch_destroy_cb_t) (struct l_fswatch *watch,
+                                       void *user_data);

Why would this destroy callback signature be different from all others? It
also precludes us from doing something like:

Quoted 6 lines
Quoted 1 line

l_fswatch_new("/foo/bar", cb, u, (l_fswatch_destroy_cb_t) l_free);

Ok, thought it might be useful to pass the watch pointer, it doesn't cost us anything and could simplify things for some users.

Quoted 6 lines

This is useful for callbacks, but almost never happens for destroy callbacks. In too many cases the destroy callback is just l_free or some_structure_destroy(struct some_structure). So having to come up with another function just to have the correct signature is wasteful. For the 1-5% usecase where this might be useful, the caller can carry the fswatch pointer in the userdata structure itself.

Fair enough although I was specifically thinking about simplifying cases where there's no userdata and the watch pointer is enough to identify the watch... but then you can also just have multiple destroy callbacks.

BTW I looked at the for loop in the man page and it seems it's actually not initialising 'event' before first use so I won't be following that pattern.

Cheers

#6 lore raw

Hi Andrew,

Quoted 3 lines

BTW I looked at the for loop in the man page and it seems it's actually not initialising 'event' before first use so I won't be following that pattern.

for (ptr = buf; ptr < buf + len;

	ptr += sizeof(struct inotify_event) + event->len) {

	event = (const struct inotify_event *) ptr;

...
}

This looks fine to me? The ptr += is not executed until the end of the iteration.

Regards,
-Denis

#7 lore raw
Quoted 10 lines · Denis Kenzior
Quoted 3 lines

BTW I looked at the for loop in the man page and it seems it's actually not initialising 'event' before first use so I won't be following that pattern.

for (ptr = buf; ptr < buf + len;

        ptr += sizeof(struct inotify_event) + event->len) {

        event = (const struct inotify_event *) ptr;
Quoted 5 lines

...
}

This looks fine to me? The ptr += is not executed until the end of the iteration.

Oh true, it doesn't do the length check before initial iteration. I thought it's better to have a sanity check.

The loop is wrong though and would cause valgrind to complain because after your last iteration ptr equals buf + len, so 'event' also points at the end of buf and event->len is beyond the end of your buffer.

Cheers

#8 lore raw
Quoted 3 lines · Andrew Zaborowski

The loop is wrong though and would cause valgrind to complain because after your last iteration ptr equals buf + len, so 'event' also points at the end of buf and event->len is beyond the end of your buffer.

Uhh no, it's actually only checking that ptr < buf + len, I'm not reading correctly today.

Best regards

#9 lore raw

Hi Andrew,

Quoted 7 lines · Andrew Zaborowski
Quoted 3 lines · Andrew Zaborowski

The loop is wrong though and would cause valgrind to complain because after your last iteration ptr equals buf + len, so 'event' also points at the end of buf and event->len is beyond the end of your buffer.

Uhh no, it's actually only checking that ptr < buf + len, I'm not reading correctly today.

Right. The only thing it doesn't do is check that len >= sizeof(struct inotify_event). But then I imagine you can trust the kernel to not screw this up.

I find the for loop version a bit more readable, but I'm okay applying your version as well.

Regards,
-Denis