dnotify_test.c 808 B

1234567891011121314151617181920212223242526272829303132333435
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE /* needed to get the defines */
  3. #include <fcntl.h> /* in glibc 2.2 this has the needed
  4. values defined */
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. static volatile int event_fd;
  9. static void handler(int sig, siginfo_t *si, void *data)
  10. {
  11. event_fd = si->si_fd;
  12. }
  13. int main(void)
  14. {
  15. struct sigaction act;
  16. int fd;
  17. act.sa_sigaction = handler;
  18. sigemptyset(&act.sa_mask);
  19. act.sa_flags = SA_SIGINFO;
  20. sigaction(SIGRTMIN + 1, &act, NULL);
  21. fd = open(".", O_RDONLY);
  22. fcntl(fd, F_SETSIG, SIGRTMIN + 1);
  23. fcntl(fd, F_NOTIFY, DN_MODIFY|DN_CREATE|DN_MULTISHOT);
  24. /* we will now be notified if any of the files
  25. in "." is modified or new files are created */
  26. while (1) {
  27. pause();
  28. printf("Got event on fd=%d\n", event_fd);
  29. }
  30. }