tst-inotify.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <error.h>
  14. #include <inttypes.h>
  15. #include <sys/inotify.h>
  16. #include <sys/fcntl.h>
  17. static int
  18. do_test(void)
  19. {
  20. int ifd, fd, ret, result = 0;
  21. struct inotify_event e;
  22. char tfile[] = "/tmp/inotify.XXXXXX";
  23. fd = mkstemp(tfile);
  24. close(fd);
  25. ifd = inotify_init1(IN_NONBLOCK);
  26. if (ifd < 0) {
  27. perror("inotify_init1()");
  28. result = 1;
  29. }
  30. if (inotify_add_watch(ifd, tfile, IN_DELETE_SELF) < 0) {
  31. perror("inotify_add_watch()");
  32. result = 1;
  33. }
  34. /* nonblocking inotify should return immediately with no events */
  35. ret = read(ifd, &e, sizeof(e));
  36. if (ret != -1 || errno != EAGAIN) {
  37. error(0, 0, "first read() returned %d", ret);
  38. result = 1;
  39. }
  40. /* generate an event */
  41. unlink(tfile);
  42. /* now check whether our event was seen */
  43. ret = read(ifd, &e, sizeof(e));
  44. if (ret != sizeof(e)) {
  45. error(0, 0, "second read() returned %d", ret);
  46. result = 1;
  47. }
  48. if (!(e.mask & IN_DELETE_SELF)) {
  49. error(0, 0, "incorrect event mask: %" PRIx32, e.mask);
  50. result = 1;
  51. }
  52. return result;
  53. }
  54. #define TIMEOUT 5
  55. #define TEST_FUNCTION do_test ()
  56. #include "../test-skeleton.c"