]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (mkfds) add a factory for making an inotify fd
authorMasatake YAMATO <yamato@redhat.com>
Fri, 2 Sep 2022 20:30:23 +0000 (05:30 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 8 Sep 2022 18:05:00 +0000 (03:05 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
tests/helpers/test_mkfds.c

index 261704ddb20f0042feed43bcf179ad66aaf420f9..b0a9d8af3c3180d521f5ce97f258f75e9ca47404 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/inotify.h>
 #include <sys/mman.h>
 #include <sys/prctl.h>
 #include <sys/socket.h>
@@ -697,6 +698,30 @@ static void make_pidfd(const struct factory *factory, struct fdesc fdescs[], pid
        };
 }
 
+static void make_inotify_fd(const struct factory *factory _U_, struct fdesc fdescs[], pid_t * child _U_,
+                           int argc _U_, char ** argv _U_)
+{
+       int fd = inotify_init();
+       if (fd < 0)
+               err(EXIT_FAILURE, "failed in inotify_init()");
+
+       if (fd != fdescs[0].fd) {
+               if (dup2(fd, fdescs[0].fd) < 0) {
+                       int e = errno;
+                       close(fd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to dup %d -> %d", fd, fdescs[0].fd);
+               }
+               close(fd);
+       }
+
+       fdescs[0] = (struct fdesc){
+               .fd    = fdescs[0].fd,
+               .close = close_fdesc,
+               .data  = NULL
+       };
+}
+
 #define PARAM_END { .name = NULL, }
 static const struct factory factories[] = {
        {
@@ -891,6 +916,18 @@ static const struct factory factories[] = {
                        PARAM_END
                },
        },
+       {
+               .name = "inotify",
+               .desc = "inotify fd returned from inotify_init(2)",
+               .priv = false,
+               .N    = 1,
+               .EX_N = 0,
+               .fork = false,
+               .make = make_inotify_fd,
+               .params = (struct parameter []) {
+                       PARAM_END
+               },
+       },
 };
 
 static int count_parameters(const struct factory *factory)