aboutsummaryrefslogtreecommitdiff
path: root/toys/other/inotifyd.c
blob: 42d1d76e7dedc0c39a26bb5bd6905b8c5f8180d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* inotifyd.c - inotify daemon. 
 *
 * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com>
 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
 *
 * No Standard.

USE_INOTIFYD(NEWTOY(inotifyd, "<2", TOYFLAG_USR|TOYFLAG_BIN))

config INOTIFYD
  bool "inotifyd"
  default y
  help
    usage: inotifyd PROG FILE[:MASK] ...

    When a filesystem event matching MASK occurs to a FILE, run PROG as:

      PROG EVENTS FILE [DIRFILE]

    If PROG is "-" events are sent to stdout.

    This file is:
      a  accessed    c  modified    e  metadata change  w  closed (writable)
      r  opened      D  deleted     M  moved            0  closed (unwritable)
      u  unmounted   o  overflow    x  unwatchable

    A file in this directory is:
      m  moved in    y  moved out   n  created          d  deleted

    When x event happens for all FILEs, inotifyd exits (after waiting for PROG).
*/

#define FOR_inotifyd
#include "toys.h"
#include <sys/inotify.h>

void inotifyd_main(void)
{
  struct pollfd fds;
  char *prog_args[5], **ss = toys.optargs;
  char *masklist ="acew0rmyndDM uox";

  fds.events = POLLIN;

  *prog_args = *toys.optargs;
  prog_args[4] = 0;
  if ((fds.fd = inotify_init()) == -1) perror_exit(0);

  // Track number of watched files. First one was program to run.
  toys.optc--;

  while (*++ss) {
    char *path = *ss, *masks = strchr(*ss, ':');
    int i, mask = 0;

    if (!masks) mask = 0xfff; // default to all
    else{
      *masks++ = 0;
      for (*masks++ = 0; *masks; masks++) {
        i = stridx(masklist, *masks);;
        if (i == -1) error_exit("bad mask '%c'", *masks);
        mask |= 1<<i;
      }
    }

    // This returns increasing numbers starting from 1, which coincidentally
    // is the toys.optargs position of the file. (0 is program to run.)
    if (inotify_add_watch(fds.fd, path, mask) < 0) perror_exit_raw(path);
  }

  for (;;) {
    int ret = 0, len;
    void *buf = 0;
    struct inotify_event *event;

    // Read next event(s)
    ret = poll(&fds, 1, -1);
    if (ret < 0 && errno == EINTR) continue;
    if (ret <= 0) break;
    xioctl(fds.fd, FIONREAD, &len);
    event = buf = xmalloc(len);
    len = readall(fds.fd, buf, len);

    // Loop through set of events.
    for (;;) {
      int left = len - (((char *)event)-(char *)buf),
          size = sizeof(struct inotify_event);

      // Don't dereference event if ->len is off end of bufer
      if (left >= size) size += event->len;
      if (left < size) break;

      if (event->mask) {
        char *s = toybuf, *m;

        for (m = masklist; *m; m++)
          if (event->mask & (1<<(m-masklist))) *s++ = *m;
        *s = 0;

        if (**prog_args == '-' && !prog_args[0][1]) {
          xprintf("%s\t%s\t%s\n" + 3*!event->len, toybuf,
              toys.optargs[event->wd], event->name);
        } else {
          prog_args[1] = toybuf;
          prog_args[2] = toys.optargs[event->wd];
          prog_args[3] = event->len ? event->name : 0;
          xrun(prog_args);
        }

        if (event->mask & IN_IGNORED) {
          if (--toys.optc <= 0) {
            free(buf);

            goto done;
          }
          inotify_rm_watch(fds.fd, event->wd);
        }
      }
      event = (void*)(size + (char*)event);
    }
    free(buf);
  }

done:
  toys.exitval = !!toys.signal;
}