OSDN Git Service

test: Some more tests under conditionals
[uclinux-h8/uClibc.git] / test / misc / tst-inotify.c
1 /* vi: set sw=4 ts=4 sts=4: */
2 /*
3  * inotify test for uClibc
4  * Copyright (C) 2012 by Kevin Cernekee <cernekee@gmail.com>
5  *
6  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 #include <sys/inotify.h>
16 #include <sys/fcntl.h>
17
18 static int
19 do_test(void)
20 {
21         int ifd, fd, ret, result = 0;
22         struct inotify_event e;
23         char tfile[] = "/tmp/inotify.XXXXXX";
24
25         fd = mkstemp(tfile);
26         close(fd);
27
28         ifd = inotify_init1(IN_NONBLOCK);
29         if (ifd < 0) {
30                 perror("inotify_init1()");
31                 result = 1;
32         }
33         if (inotify_add_watch(ifd, tfile, IN_DELETE_SELF) < 0) {
34                 perror("inotify_add_watch()");
35                 result = 1;
36         }
37
38         /* nonblocking inotify should return immediately with no events */
39         ret = read(ifd, &e, sizeof(e));
40         if (ret != -1 || errno != EAGAIN) {
41                 fprintf(stderr, "first read() returned %d\n", ret);
42                 result = 1;
43         }
44
45         /* generate an event */
46         unlink(tfile);
47
48         /* now check whether our event was seen */
49         ret = read(ifd, &e, sizeof(e));
50         if (ret != sizeof(e)) {
51                 fprintf(stderr, "second read() returned %d\n", ret);
52                 result = 1;
53         }
54
55         if (!(e.mask & IN_DELETE_SELF)) {
56                 fprintf(stderr, "incorrect event mask: %" PRIx32 "\n", e.mask);
57                 result = 1;
58         }
59
60         return result;
61 }
62
63 #define TIMEOUT 5
64 #define TEST_FUNCTION do_test ()
65 #include "../test-skeleton.c"