Commit cd2869d251a7481eec4a0caf7e284a3c9fc26cb0

Authored by Georg Hopp
0 parents

initial checkin

  1 +
  2 +INCLUDES = -I. -I./include @INOTIFY_INCLUDES@
  3 +
  4 +bin_PROGRAMS = inotify
  5 +
  6 +inotify_SOURCES = inotify1.c
  7 +inotify_INCLUDES = inotify.h
  8 +
  9 +SUBDIRS =
  10 +
  1 +#!/bin/sh
  2 +
  3 +aclocal
  4 +libtoolize --copy
  5 +autoheader
  6 +automake --gnu --add-missing --copy
  7 +autoconf
  1 +AC_PREREQ(2.68)
  2 +AC_INIT([checkout_files],
  3 + [0.0.1],
  4 + [georg@steffers.org])
  5 +LT_INIT
  6 +AM_INIT_AUTOMAKE
  7 +AM_SILENT_RULES([no])
  8 +AC_COPYRIGHT([Copyright © 2000 Georg Hopp])
  9 +AC_REVISION([0.0.1])
  10 +AC_CONFIG_SRCDIR([inotify1.c])
  11 +AC_CONFIG_HEADER([config.h])
  12 +AC_CONFIG_MACRO_DIR([m4])
  13 +
  14 +AC_CANONICAL_HOST
  15 +
  16 +use_inotify="yes"
  17 +
  18 +# Checks for programs.
  19 +AC_PROG_CC
  20 +AC_PROG_MAKE_SET
  21 +
  22 +# Checks for header files.
  23 +AC_HEADER_STDC
  24 +
  25 +# Checks for typedefs, structures, and compiler characteristics.
  26 +AC_C_CONST
  27 +
  28 +# Checks for library functions.
  29 +AC_CHECK_FUNCS([memset malloc free read ioctl syscall select perror printf])
  30 +
  31 +dnl We need to check if the right inotify version is accessible
  32 +AC_MSG_CHECKING([whether inotify is to be used for filemonitoring])
  33 +AC_ARG_ENABLE(inotify,
  34 + [ --disable-inotify disable inotify in the ecore_file module],
  35 + [
  36 + if test "$enableval" == "yes"; then
  37 + AC_MSG_RESULT([yes])
  38 + else
  39 + AC_MSG_RESULT([no - but we need it])
  40 + use_inotify="no"
  41 + fi
  42 + ], [
  43 + AC_MSG_RESULT([yes])
  44 + ]
  45 +)
  46 +
  47 +dnl It's hard to find a good test on how to check the correct
  48 +dnl inotify version. They changed the headers a lot.
  49 +dnl in kernel 2.6.13 __NR_inotify_init was added to the defined syscalls
  50 +dnl in asm/unistd.h and IN_MOVE_SELF was added to linux/inotify.h
  51 +dnl so with this check you need a very new kernel and kernel-headers!
  52 +dnl On my gentoo, /usr/include/asm and /usr/include/linux are no symlinks
  53 +dnl into the current kernel tree....so i also try to find the includes
  54 +dnl under /usr/src/linux or under /usr/include/linux-`uname -r`
  55 +linux_rev=`uname -r`
  56 +INOTIFY_INCLUDES=
  57 +if test "x$use_inotify" = "xyes"; then
  58 + AC_MSG_CHECKING([for sufficient inotify includes])
  59 + AC_TRY_COMPILE(
  60 + [
  61 + #include <asm/unistd.h>
  62 + #include <linux/inotify.h>
  63 + ],
  64 + [ int a = __NR_inotify_init; int b = IN_MOVE_SELF; ],
  65 + [
  66 + AC_DEFINE(HAVE_INOTIFY, 1, [ File monitoring with Inotify ])
  67 + an_inc=/usr/include
  68 + ],
  69 + [
  70 + AC_TRY_COMPILE(
  71 + [
  72 + #include "/usr/src/linux/include/asm/unistd.h"
  73 + #include "/usr/src/linux/include/linux/inotify.h"
  74 + ],
  75 + [ int a = __NR_inotify_init; int b = IN_MOVE_SELF; ],
  76 + [
  77 + AC_DEFINE(HAVE_INOTIFY, 1, [ File monitoring with Inotify ])
  78 + AC_DEFINE(IN_KERNEL, 1, [ blablabla ])
  79 + INOTIFY_INCLUDES=-I/usr/src/linux/include
  80 + an_inc=/usr/src/linux/include
  81 + ],
  82 + [
  83 + AC_TRY_COMPILE(
  84 + [
  85 + #include </usr/src/linux-$linux_rev/include/asm/unistd.h>
  86 + #include </usr/src/linux-$linux_rev/include/linux/inotify.h>
  87 + ],
  88 + [ int a = __NR_inotify_init; int b = IN_MOVE_SELF; ],
  89 + [
  90 + AC_DEFINE(HAVE_INOTIFY, 1, [ File monitoring with Inotify ])
  91 + AC_DEFINE(IN_KERNEL_UNAME, 1, [ blablabla ])
  92 + INOTIFY_INCLUDES=-I/usr/src/linux-$linux_rev/include
  93 + an_inc=/usr/src/linux-$linux_rev/include
  94 + ],
  95 + [
  96 + use_inotify="no"
  97 + an_inc="not found"
  98 + ]
  99 + )
  100 + ]
  101 + )
  102 + ]
  103 + )
  104 + AC_MSG_RESULT([$an_inc])
  105 + test "x$an_inc" == "xnot found" && exit
  106 +else
  107 + exit
  108 +fi
  109 +
  110 +AC_SUBST(INOTIFY_INCLUDES)
  111 +
  112 +AC_CONFIG_FILES([Makefile])
  113 +
  114 +AC_OUTPUT
  1 +#ifndef INOTIFY_H
  2 +#define INOTIFY_H
  3 +
  4 +#include <sys/syscall.h>
  5 +#include <asm/unistd.h>
  6 +#include <linux/inotify.h>
  7 +
  8 +#define IN_NEXT_EVENT(ev) \
  9 + (struct inotify_event *) \
  10 + ((char *)(ev) + sizeof (struct inotify_event) + (ev)->len)
  11 +
  12 +#define IN_NO_EVENT(ev) ((ev)->mask|IN_ALL_EVENTS) == IN_ALL_EVENTS
  13 +
  14 +
  15 +static inline int inotify_init (void)
  16 +{
  17 + return syscall (__NR_inotify_init);
  18 +}
  19 +
  20 +static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
  21 +{
  22 + return syscall (__NR_inotify_add_watch, fd, name, mask);
  23 +}
  24 +
  25 +static inline int inotify_rm_watch (int fd, __u32 wd)
  26 +{
  27 + return syscall (__NR_inotify_rm_watch, fd, wd);
  28 +}
  29 +
  30 +#endif /* INOTIFY_H */
  1 +#include <stdio.h>
  2 +#include <errno.h>
  3 +#include <string.h>
  4 +#include <sys/ioctl.h>
  5 +#include <sys/select.h>
  6 +
  7 +#include <inotify.h>
  8 +
  9 +int
  10 +main (int arg, char * argv [])
  11 +{
  12 + int notify_d,
  13 + watch_id;
  14 + fd_set rfds;
  15 + int sel_ret;
  16 +
  17 + FD_ZERO (&rfds);
  18 +
  19 + notify_d = inotify_init ();
  20 + if (notify_d == -1)
  21 + {
  22 + perror ("could not init inotify");
  23 + return 1;
  24 + }
  25 +
  26 + FD_SET (notify_d, &rfds);
  27 +
  28 + watch_id = inotify_add_watch (notify_d, argv[1],
  29 + IN_CREATE|IN_OPEN|IN_MODIFY|IN_CLOSE);
  30 + if (watch_id == -1)
  31 + {
  32 + perror ("could not add watch");
  33 + return 2;
  34 + }
  35 +
  36 + while (sel_ret = select (notify_d+1, &rfds, NULL, NULL, NULL))
  37 + {
  38 + char * in_ev = NULL;
  39 + struct inotify_event * act_ev = NULL;
  40 + size_t size = 0;
  41 + size_t size_avail;
  42 + char * ev_name = NULL;
  43 +
  44 + ioctl (notify_d, FIONREAD, &size_avail);
  45 + in_ev = (char *)malloc (size_avail);
  46 + if (in_ev == NULL)
  47 + {
  48 + perror ("could not get memory for event list");
  49 + return 4;
  50 + }
  51 + memset (in_ev, 0, size_avail);
  52 +
  53 + while (size < size_avail)
  54 + {
  55 + int got = read (notify_d, in_ev + size,
  56 + size_avail - size);
  57 + if (got == 0)
  58 + {
  59 + perror ("notify FD was closed unexpectedly");
  60 + return 3;
  61 + }
  62 + size += got;
  63 + }
  64 +
  65 + for (act_ev = (struct inotify_event *) in_ev;
  66 + (char *) act_ev < in_ev + size_avail && IN_NO_EVENT (act_ev);
  67 + act_ev = IN_NEXT_EVENT (act_ev))
  68 + {
  69 + printf ("Watch Descriptor: %d\n", act_ev->wd);
  70 + switch (act_ev->mask)
  71 + {
  72 + case IN_ACCESS:
  73 + ev_name = "IN_ACCESS"; break;
  74 + case IN_ATTRIB:
  75 + ev_name = "IN_ATTRIB"; break;
  76 + case IN_CLOSE_WRITE:
  77 + ev_name = "IN_CLOSE_WRITE"; break;
  78 + case IN_CLOSE_NOWRITE:
  79 + ev_name = "IN_CLOSE_NOWRITE"; break;
  80 + case IN_CREATE:
  81 + ev_name = "IN_CREATE"; break;
  82 + case IN_DELETE:
  83 + ev_name = "IN_DELETE"; break;
  84 + case IN_DELETE_SELF:
  85 + ev_name = "IN_DELETE_SELF"; break;
  86 + case IN_MODIFY:
  87 + ev_name = "IN_MODIFY"; break;
  88 + case IN_MOVE_SELF:
  89 + ev_name = "IN_MOVE_SELF"; break;
  90 + case IN_MOVED_FROM:
  91 + ev_name = "IN_MOVED_FROM"; break;
  92 + case IN_MOVED_TO:
  93 + ev_name = "IN_MOVED_TO"; break;
  94 + case IN_OPEN:
  95 + ev_name = "IN_OPEN"; break;
  96 + }
  97 + printf ("Mask of Events: %s(%d)\n", ev_name, act_ev->mask);
  98 + printf ("Events Cookie: %u\n", act_ev->cookie);
  99 + printf ("Length of name: %u\n", act_ev->len);
  100 + printf ("Event Name: %s\n", act_ev->name);
  101 + }
  102 +
  103 + if (in_ev)
  104 + {
  105 + free (in_ev);
  106 + in_ev = NULL;
  107 + }
  108 +
  109 + puts ("---");
  110 +
  111 + FD_SET (notify_d, &rfds);
  112 + }
  113 +
  114 + return 0;
  115 +}
Please register or login to post a comment