tools: ynl: add ynl_dump_empty() helper
authorJakub Kicinski <kuba@kernel.org>
Fri, 29 Mar 2024 18:16:51 +0000 (11:16 -0700)
committerJakub Kicinski <kuba@kernel.org>
Wed, 3 Apr 2024 01:01:58 +0000 (18:01 -0700)
Checking if dump is empty requires a couple of casts.
Add a convenient wrapper.

Add an example use in the netdev sample, loopback is always
present so an empty dump is an error.

Reviewed-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Link: https://lore.kernel.org/r/20240329181651.319326-1-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/net/ynl/lib/ynl.h
tools/net/ynl/samples/netdev.c

index 9842e85a8c57da5104833ae2a7794c2285de8db3..eef7c6324ed48f3f5207af22577671b3e10cd636 100644 (file)
@@ -91,6 +91,18 @@ void ynl_sock_destroy(struct ynl_sock *ys);
             !ynl_dump_obj_is_last(iter);                               \
             iter = ynl_dump_obj_next(iter))
 
+/**
+ * ynl_dump_empty() - does the dump have no entries
+ * @dump: pointer to the dump list, as returned by a dump call
+ *
+ * Check if the dump is empty, i.e. contains no objects.
+ * Dump calls return NULL on error, and terminator element if empty.
+ */
+static inline bool ynl_dump_empty(void *dump)
+{
+       return dump == (void *)YNL_LIST_END;
+}
+
 int ynl_subscribe(struct ynl_sock *ys, const char *grp_name);
 int ynl_socket_get_fd(struct ynl_sock *ys);
 int ynl_ntf_check(struct ynl_sock *ys);
index 591b90e21890c32310c46fd900cd13c8c13f66f3..3e7b29bd55d5dc25659b783536f786846bec9195 100644 (file)
@@ -100,6 +100,8 @@ int main(int argc, char **argv)
                if (!devs)
                        goto err_close;
 
+               if (ynl_dump_empty(devs))
+                       fprintf(stderr, "Error: no devices reported\n");
                ynl_dump_foreach(devs, d)
                        netdev_print_device(d, 0);
                netdev_dev_get_list_free(devs);