Advertisement
teknoraver

sd_event_source_get_inotify_path

Apr 19th, 2024 (edited)
713
0
148 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. /* SPDX-License-Identifier: MIT-0 */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/inotify.h>
  6.  
  7. #include <systemd/sd-event.h>
  8.  
  9. #define _cleanup_(f) __attribute__((cleanup(f)))
  10.  
  11. static int inotify_handler(sd_event_source *source,
  12.                const struct inotify_event *event, void *userdata)
  13. {
  14.     char *path = NULL;
  15.  
  16.     int r = sd_event_source_get_inotify_path(source, &path);
  17.     printf("path: in inotify_handler: %s, err: %d\n", path, r);
  18.  
  19.     return 0;
  20. }
  21.  
  22. int main(int argc, char **argv)
  23. {
  24.     sd_event_source *source = NULL;
  25.     sd_event *event = NULL;
  26.     const char *path = argc > 1 ? argv[1] : "/tmp";
  27.     char *path2;
  28.  
  29.     sd_event_default(&event);
  30.  
  31.     sd_event_add_inotify(event, &source, path,
  32.                  IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
  33.                  inotify_handler, NULL);
  34.  
  35.     sd_event_source_get_inotify_path(source, &path2);
  36.     printf("path in main: %s\n", path2);
  37.  
  38.     sd_event_loop(event);
  39.  
  40.     return 0;
  41. }
  42.  
  43. /*
  44. $ ./sd
  45. path in main: /tmp
  46. path: in inotify_handler: (null), err: -116
  47. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement