tst-inotify.c 1.4 KB

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