Rizin
unix-like reverse engineering framework and cli tools
linux-inotify.c File Reference
#include "uv.h"
#include "uv/tree.h"
#include "internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  watcher_list
 
struct  watcher_root
 

Macros

#define CAST(p)   ((struct watcher_root*)(p))
 

Functions

static int compare_watchers (const struct watcher_list *a, const struct watcher_list *b)
 
static void uv__inotify_read (uv_loop_t *loop, uv__io_t *w, unsigned int revents)
 
static void maybe_free_watcher_list (struct watcher_list *w, uv_loop_t *loop)
 
static int init_inotify (uv_loop_t *loop)
 
int uv__inotify_fork (uv_loop_t *loop, void *old_watchers)
 
static struct watcher_listfind_watcher (uv_loop_t *loop, int wd)
 
int uv_fs_event_init (uv_loop_t *loop, uv_fs_event_t *handle)
 
int uv_fs_event_start (uv_fs_event_t *handle, uv_fs_event_cb cb, const char *path, unsigned int flags)
 
int uv_fs_event_stop (uv_fs_event_t *handle)
 
void uv__fs_event_close (uv_fs_event_t *handle)
 

Macro Definition Documentation

◆ CAST

#define CAST (   p)    ((struct watcher_root*)(p))

Definition at line 47 of file linux-inotify.c.

Function Documentation

◆ compare_watchers()

static int compare_watchers ( const struct watcher_list a,
const struct watcher_list b 
)
static

Definition at line 50 of file linux-inotify.c.

51  {
52  if (a->wd < b->wd) return -1;
53  if (a->wd > b->wd) return 1;
54  return 0;
55 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, and b.

◆ find_watcher()

static struct watcher_list* find_watcher ( uv_loop_t loop,
int  wd 
)
static

Definition at line 151 of file linux-inotify.c.

151  {
152  struct watcher_list w;
153  w.wd = wd;
154  return RB_FIND(watcher_root, CAST(&loop->inotify_watchers), &w);
155 }
#define w
Definition: crypto_rc6.c:13
#define RB_FIND(name, x, y)
Definition: tree.h:729
#define CAST(p)
Definition: linux-inotify.c:47
uv_loop_t * loop
Definition: main.c:7

References CAST, loop, RB_FIND, and w.

Referenced by uv__inotify_read(), uv_fs_event_start(), and uv_fs_event_stop().

◆ init_inotify()

static int init_inotify ( uv_loop_t loop)
static

Definition at line 68 of file linux-inotify.c.

68  {
69  int fd;
70 
71  if (loop->inotify_fd != -1)
72  return 0;
73 
74  fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
75  if (fd < 0)
76  return UV__ERR(errno);
77 
78  loop->inotify_fd = fd;
79  uv__io_init(&loop->inotify_read_watcher, uv__inotify_read, loop->inotify_fd);
80  uv__io_start(loop, &loop->inotify_read_watcher, POLLIN);
81 
82  return 0;
83 }
#define UV__ERR(x)
Definition: errno.h:29
static void uv__inotify_read(uv_loop_t *loop, uv__io_t *w, unsigned int revents)
void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:882
void uv__io_init(uv__io_t *w, uv__io_cb cb, int fd)
Definition: core.c:865
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, loop, UV__ERR, uv__inotify_read(), uv__io_init(), and uv__io_start().

Referenced by uv_fs_event_start().

◆ maybe_free_watcher_list()

static void maybe_free_watcher_list ( struct watcher_list w,
uv_loop_t loop 
)
static

Definition at line 157 of file linux-inotify.c.

157  {
158  /* if the watcher_list->watchers is being iterated over, we can't free it. */
159  if ((!w->iterating) && QUEUE_EMPTY(&w->watchers)) {
160  /* No watchers left for this path. Clean up. */
161  RB_REMOVE(watcher_root, CAST(&loop->inotify_watchers), w);
162  inotify_rm_watch(loop->inotify_fd, w->wd);
163  uv__free(w);
164  }
165 }
#define RB_REMOVE(name, x, y)
Definition: tree.h:728
#define QUEUE_EMPTY(q)
Definition: queue.h:39
void uv__free(void *ptr)
Definition: uv-common.c:81

References CAST, loop, QUEUE_EMPTY, RB_REMOVE, uv__free(), and w.

Referenced by uv__inotify_fork(), uv__inotify_read(), and uv_fs_event_stop().

◆ uv__fs_event_close()

void uv__fs_event_close ( uv_fs_event_t handle)

Definition at line 325 of file linux-inotify.c.

325  {
327 }
static mcore_handle handle
Definition: asm_mcore.c:8
int uv_fs_event_stop(uv_fs_event_t *handle)

References handle, and uv_fs_event_stop().

◆ uv__inotify_fork()

int uv__inotify_fork ( uv_loop_t loop,
void *  old_watchers 
)

Definition at line 86 of file linux-inotify.c.

86  {
87  /* Open the inotify_fd, and re-arm all the inotify watchers. */
88  int err;
89  struct watcher_list* tmp_watcher_list_iter;
90  struct watcher_list* watcher_list;
91  struct watcher_list tmp_watcher_list;
92  QUEUE queue;
93  QUEUE* q;
95  char* tmp_path;
96 
97  if (old_watchers != NULL) {
98  /* We must restore the old watcher list to be able to close items
99  * out of it.
100  */
101  loop->inotify_watchers = old_watchers;
102 
103  QUEUE_INIT(&tmp_watcher_list.watchers);
104  /* Note that the queue we use is shared with the start and stop()
105  * functions, making QUEUE_FOREACH unsafe to use. So we use the
106  * QUEUE_MOVE trick to safely iterate. Also don't free the watcher
107  * list until we're done iterating. c.f. uv__inotify_read.
108  */
110  CAST(&old_watchers), tmp_watcher_list_iter) {
111  watcher_list->iterating = 1;
112  QUEUE_MOVE(&watcher_list->watchers, &queue);
113  while (!QUEUE_EMPTY(&queue)) {
114  q = QUEUE_HEAD(&queue);
115  handle = QUEUE_DATA(q, uv_fs_event_t, watchers);
116  /* It's critical to keep a copy of path here, because it
117  * will be set to NULL by stop() and then deallocated by
118  * maybe_free_watcher_list
119  */
120  tmp_path = uv__strdup(handle->path);
121  assert(tmp_path != NULL);
122  QUEUE_REMOVE(q);
123  QUEUE_INSERT_TAIL(&watcher_list->watchers, q);
125 
126  QUEUE_INSERT_TAIL(&tmp_watcher_list.watchers, &handle->watchers);
127  handle->path = tmp_path;
128  }
129  watcher_list->iterating = 0;
131  }
132 
133  QUEUE_MOVE(&tmp_watcher_list.watchers, &queue);
134  while (!QUEUE_EMPTY(&queue)) {
135  q = QUEUE_HEAD(&queue);
136  QUEUE_REMOVE(q);
137  handle = QUEUE_DATA(q, uv_fs_event_t, watchers);
138  tmp_path = handle->path;
139  handle->path = NULL;
140  err = uv_fs_event_start(handle, handle->cb, tmp_path, 0);
141  uv__free(tmp_path);
142  if (err)
143  return err;
144  }
145  }
146 
147  return 0;
148 }
static bool err
Definition: armass.c:435
#define NULL
Definition: cris-opc.c:27
#define RB_FOREACH_SAFE(x, name, head, y)
Definition: tree.h:746
int uv_fs_event_start(uv_fs_event_t *handle, uv_fs_event_cb cb, const char *path, unsigned int flags)
static void maybe_free_watcher_list(struct watcher_list *w, uv_loop_t *loop)
assert(limit<=UINT32_MAX/2)
#define QUEUE_DATA(ptr, type, field)
Definition: queue.h:30
#define QUEUE_HEAD(q)
Definition: queue.h:42
#define QUEUE_INSERT_TAIL(h, q)
Definition: queue.h:92
#define QUEUE_MOVE(h, n)
Definition: queue.h:72
#define QUEUE_INIT(q)
Definition: queue.h:45
void * QUEUE[2]
Definition: queue.h:21
#define QUEUE_REMOVE(q)
Definition: queue.h:101
char * uv__strdup(const char *s)
Definition: uv-common.c:55
uv_pipe_t queue
Definition: worker.c:9

References assert(), CAST, err, handle, loop, maybe_free_watcher_list(), NULL, queue, QUEUE_DATA, QUEUE_EMPTY, QUEUE_HEAD, QUEUE_INIT, QUEUE_INSERT_TAIL, QUEUE_MOVE, QUEUE_REMOVE, RB_FOREACH_SAFE, uv__free(), uv__strdup(), uv_fs_event_start(), and uv_fs_event_stop().

Referenced by uv__io_fork().

◆ uv__inotify_read()

static void uv__inotify_read ( uv_loop_t loop,
uv__io_t w,
unsigned int  revents 
)
static

Definition at line 167 of file linux-inotify.c.

169  {
170  const struct inotify_event* e;
171  struct watcher_list* w;
172  uv_fs_event_t* h;
173  QUEUE queue;
174  QUEUE* q;
175  const char* path;
176  ssize_t size;
177  const char *p;
178  /* needs to be large enough for sizeof(inotify_event) + strlen(path) */
179  char buf[4096];
180 
181  while (1) {
182  do
183  size = read(loop->inotify_fd, buf, sizeof(buf));
184  while (size == -1 && errno == EINTR);
185 
186  if (size == -1) {
187  assert(errno == EAGAIN || errno == EWOULDBLOCK);
188  break;
189  }
190 
191  assert(size > 0); /* pre-2.6.21 thing, size=0 == read buffer too small */
192 
193  /* Now we have one or more inotify_event structs. */
194  for (p = buf; p < buf + size; p += sizeof(*e) + e->len) {
195  e = (const struct inotify_event*) p;
196 
197  events = 0;
198  if (e->mask & (IN_ATTRIB|IN_MODIFY))
199  events |= UV_CHANGE;
200  if (e->mask & ~(IN_ATTRIB|IN_MODIFY))
201  events |= UV_RENAME;
202 
203  w = find_watcher(loop, e->wd);
204  if (w == NULL)
205  continue; /* Stale event, no watchers left. */
206 
207  /* inotify does not return the filename when monitoring a single file
208  * for modifications. Repurpose the filename for API compatibility.
209  * I'm not convinced this is a good thing, maybe it should go.
210  */
211  path = e->len ? (const char*) (e + 1) : uv__basename_r(w->path);
212 
213  /* We're about to iterate over the queue and call user's callbacks.
214  * What can go wrong?
215  * A callback could call uv_fs_event_stop()
216  * and the queue can change under our feet.
217  * So, we use QUEUE_MOVE() trick to safely iterate over the queue.
218  * And we don't free the watcher_list until we're done iterating.
219  *
220  * First,
221  * tell uv_fs_event_stop() (that could be called from a user's callback)
222  * not to free watcher_list.
223  */
224  w->iterating = 1;
225  QUEUE_MOVE(&w->watchers, &queue);
226  while (!QUEUE_EMPTY(&queue)) {
227  q = QUEUE_HEAD(&queue);
228  h = QUEUE_DATA(q, uv_fs_event_t, watchers);
229 
230  QUEUE_REMOVE(q);
231  QUEUE_INSERT_TAIL(&w->watchers, q);
232 
233  h->cb(h, path, events, 0);
234  }
235  /* done iterating, time to (maybe) free empty watcher_list */
236  w->iterating = 0;
238  }
239  }
240 }
#define e(frag)
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
static struct watcher_list * find_watcher(uv_loop_t *loop, int wd)
#define EINTR
Definition: sftypes.h:114
#define EAGAIN
Definition: sftypes.h:121
int ssize_t
Definition: sftypes.h:39
#define h(i)
Definition: sha256.c:48
@ UV_CHANGE
Definition: uv.h:1542
@ UV_RENAME
Definition: uv.h:1541
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References assert(), e, EAGAIN, EINTR, find_watcher(), h, loop, maybe_free_watcher_list(), NULL, p, path, queue, QUEUE_DATA, QUEUE_EMPTY, QUEUE_HEAD, QUEUE_INSERT_TAIL, QUEUE_MOVE, QUEUE_REMOVE, read(), UV_CHANGE, UV_RENAME, and w.

Referenced by init_inotify().

◆ uv_fs_event_init()

int uv_fs_event_init ( uv_loop_t loop,
uv_fs_event_t handle 
)

Definition at line 243 of file linux-inotify.c.

243  {
244  uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
245  return 0;
246 }
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:301

References handle, loop, and uv__handle_init.

◆ uv_fs_event_start()

int uv_fs_event_start ( uv_fs_event_t handle,
uv_fs_event_cb  cb,
const char *  path,
unsigned int  flags 
)

Definition at line 249 of file linux-inotify.c.

252  {
253  struct watcher_list* w;
254  size_t len;
255  int events;
256  int err;
257  int wd;
258 
259  if (uv__is_active(handle))
260  return UV_EINVAL;
261 
262  err = init_inotify(handle->loop);
263  if (err)
264  return err;
265 
266  events = IN_ATTRIB
267  | IN_CREATE
268  | IN_MODIFY
269  | IN_DELETE
270  | IN_DELETE_SELF
271  | IN_MOVE_SELF
272  | IN_MOVED_FROM
273  | IN_MOVED_TO;
274 
275  wd = inotify_add_watch(handle->loop->inotify_fd, path, events);
276  if (wd == -1)
277  return UV__ERR(errno);
278 
279  w = find_watcher(handle->loop, wd);
280  if (w)
281  goto no_insert;
282 
283  len = strlen(path) + 1;
284  w = uv__malloc(sizeof(*w) + len);
285  if (w == NULL)
286  return UV_ENOMEM;
287 
288  w->wd = wd;
289  w->path = memcpy(w + 1, path, len);
290  QUEUE_INIT(&w->watchers);
291  w->iterating = 0;
292  RB_INSERT(watcher_root, CAST(&handle->loop->inotify_watchers), w);
293 
294 no_insert:
296  QUEUE_INSERT_TAIL(&w->watchers, &handle->watchers);
297  handle->path = w->path;
298  handle->cb = cb;
299  handle->wd = wd;
300 
301  return 0;
302 }
size_t len
Definition: 6502dis.c:15
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define RB_INSERT(name, x, y)
Definition: tree.h:727
static int init_inotify(uv_loop_t *loop)
Definition: linux-inotify.c:68
void * uv__malloc(size_t size)
Definition: uv-common.c:75
#define uv__is_active(h)
Definition: uv-common.h:252
#define uv__handle_start(h)
Definition: uv-common.h:258
static const char * cb[]
Definition: z80_tab.h:176

References CAST, cb, err, find_watcher(), handle, init_inotify(), len, memcpy(), NULL, path, QUEUE_INIT, QUEUE_INSERT_TAIL, RB_INSERT, UV__ERR, uv__handle_start, uv__is_active, uv__malloc(), and w.

Referenced by uv__inotify_fork().

◆ uv_fs_event_stop()

int uv_fs_event_stop ( uv_fs_event_t handle)

Definition at line 305 of file linux-inotify.c.

305  {
306  struct watcher_list* w;
307 
308  if (!uv__is_active(handle))
309  return 0;
310 
311  w = find_watcher(handle->loop, handle->wd);
312  assert(w != NULL);
313 
314  handle->wd = -1;
315  handle->path = NULL;
317  QUEUE_REMOVE(&handle->watchers);
318 
320 
321  return 0;
322 }
#define uv__handle_stop(h)
Definition: uv-common.h:266

References assert(), find_watcher(), handle, maybe_free_watcher_list(), NULL, QUEUE_REMOVE, uv__handle_stop, uv__is_active, and w.

Referenced by uv__fs_event_close(), and uv__inotify_fork().