summaryrefslogtreecommitdiffstats
path: root/daemon/inotify.c
blob: cb0a3662d839cf6371007583c49a0b118997572b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2009-2012 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>
#endif

#include "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"
#include "optgroups.h"

#ifdef HAVE_SYS_INOTIFY_H
GUESTFSD_EXT_CMD(str_sort, sort);

/* Currently open inotify handle, or -1 if not opened. */
static int inotify_fd = -1;

static char inotify_buf[64*1024*1024];	/* Event buffer, [0..posn-1] is valid */
static size_t inotify_posn = 0;

/* Clean up the inotify handle on daemon exit. */
static void inotify_finalize (void) __attribute__((destructor));
static void
inotify_finalize (void)
{
  if (inotify_fd >= 0) {
    close (inotify_fd);
    inotify_fd = -1;
  }
}

int
optgroup_inotify_available (void)
{
  return 1;
}

/* Because inotify_init does NEED_ROOT, NEED_INOTIFY implies NEED_ROOT. */
#define NEED_INOTIFY(errcode)						\
  do {									\
    if (inotify_fd == -1) {						\
      reply_with_error ("%s: you must call 'inotify_init' first to initialize inotify", __func__); \
      return (errcode);							\
    }									\
  } while (0)

#define MQE_PATH "/proc/sys/fs/inotify/max_queued_events"

int
do_inotify_init (int max_events)
{
  FILE *fp;

  NEED_ROOT (, return -1);

  if (max_events < 0) {
    reply_with_error ("max_events < 0");
    return -1;
  }

  if (max_events > 0) {
    fp = fopen (MQE_PATH, "w");
    if (fp == NULL) {
      reply_with_perror (MQE_PATH);
      return -1;
    }
    fprintf (fp, "%d\n", max_events);
    fclose (fp);
  }

  if (inotify_fd >= 0)
    if (do_inotify_close () == -1)
      return -1;

#ifdef HAVE_INOTIFY_INIT1
  inotify_fd = inotify_init1 (IN_NONBLOCK | IN_CLOEXEC);
  if (inotify_fd == -1) {
    reply_with_perror ("inotify_init1");
    return -1;
  }
#else
  inotify_fd = inotify_init ();
  if (inotify_fd == -1) {
    reply_with_perror ("inotify_init");
    return -1;
  }
  if (fcntl (inotify_fd, F_SETFL, O_NONBLOCK) == -1) {
    reply_with_perror ("fcntl: O_NONBLOCK");
    close (inotify_fd);
    inotify_fd = -1;
    return -1;
  }
  if (fcntl (inotify_fd, F_SETFD, FD_CLOEXEC) == -1) {
    reply_with_perror ("fcntl: FD_CLOEXEC");
    close (inotify_fd);
    inotify_fd = -1;
    return -1;
  }
#endif

  return 0;
}

int
do_inotify_close (void)
{
  NEED_INOTIFY (-1);

  if (inotify_fd == -1) {
    reply_with_error ("handle is not open");
    return -1;
  }

  if (close (inotify_fd) == -1) {
    reply_with_perror ("close");
    return -1;
  }

  inotify_fd = -1;
  inotify_posn = 0;

  return 0;
}

int64_t
do_inotify_add_watch (const char *path, int mask)
{
  int64_t r;
  char *buf;

  NEED_INOTIFY (-1);

  buf = sysroot_path (path);
  if (!buf) {
    reply_with_perror ("malloc");
    return -1;
  }

  r = inotify_add_watch (inotify_fd, buf, mask);
  free (buf);
  if (r == -1) {
    reply_with_perror ("%s", path);
    return -1;
  }

  return r;
}

int
do_inotify_rm_watch (int wd)
{
  NEED_INOTIFY (-1);

  if (inotify_rm_watch (inotify_fd, wd) == -1) {
    reply_with_perror ("%d", wd);
    return -1;
  }

  return 0;
}

guestfs_int_inotify_event_list *
do_inotify_read (void)
{
  int space;
  guestfs_int_inotify_event_list *ret;

  NEED_INOTIFY (NULL);

  ret = malloc (sizeof *ret);
  if (ret == NULL) {
    reply_with_perror ("malloc");
    return NULL;
  }
  ret->guestfs_int_inotify_event_list_len = 0;
  ret->guestfs_int_inotify_event_list_val = NULL;

  /* Read events that are available, but make sure we won't exceed
   * maximum message size.  In order to achieve this we have to
   * guesstimate the remaining space available.
   */
  space = GUESTFS_MESSAGE_MAX / 2;

  while (space > 0) {
    struct inotify_event *event;
    int r;
    size_t n;

    r = read (inotify_fd, inotify_buf + inotify_posn,
              sizeof (inotify_buf) - inotify_posn);
    if (r == -1) {
      if (errno == EWOULDBLOCK || errno == EAGAIN) /* End of list. */
        break;
      reply_with_perror ("read");
      goto error;
    }
    if (r == 0) {		/* End of file - we're not expecting it. */
      reply_with_error ("unexpected end of file");
      goto error;
    }

    inotify_posn += r;

    /* Read complete events from the buffer and add them to the result. */
    n = 0;
    while (n < inotify_posn) {
      guestfs_int_inotify_event *np;
      guestfs_int_inotify_event *in;

      event = (struct inotify_event *) &inotify_buf[n];

      /* Have we got a complete event in the buffer? */
#ifdef __GNUC__
      if (n + sizeof (struct inotify_event) > inotify_posn ||
          n + sizeof (struct inotify_event) + event->len > inotify_posn)
        break;
#else
#error "this code needs fixing so it works on non-GCC compilers"
#endif

      /* Check event->len is reasonable (note the field is uint32_t). */
      if (event->len > PATH_MAX) {
        reply_with_error ("event->len = %" PRIu32 " > PATH_MAX", event->len);
        goto error;
      }

      np = realloc (ret->guestfs_int_inotify_event_list_val,
                    (ret->guestfs_int_inotify_event_list_len + 1) *
                    sizeof (guestfs_int_inotify_event));
      if (np == NULL) {
        reply_with_perror ("realloc");
        goto error;
      }
      ret->guestfs_int_inotify_event_list_val = np;
      in = &ret->guestfs_int_inotify_event_list_val[ret->guestfs_int_inotify_event_list_len];
      ret->guestfs_int_inotify_event_list_len++;

      in->in_wd = event->wd;
      in->in_mask = event->mask;
      in->in_cookie = event->cookie;

      if (event->len > 0)
        in->in_name = strdup (event->name);
      else
        in->in_name = strdup (""); /* Should have optional string fields XXX. */
      if (in->in_name == NULL) {
        reply_with_perror ("strdup");
        goto error;
      }

      /* Estimate space used by this event in the message. */
      space -= 16 + 4 + strlen (in->in_name) + 4;

      /* Move pointer to next event. */
#ifdef __GNUC__
      n += sizeof (struct inotify_event) + event->len;
#else
#error "this code needs fixing so it works on non-GCC compilers"
#endif
    }

    /* 'n' now points to the first unprocessed/incomplete
     * message in the buffer. Copy that to offset 0 in the buffer.
     */
    memmove (inotify_buf, &inotify_buf[n], inotify_posn - n);
    inotify_posn -= n;
  }

  /* Return the messages. */
  return ret;

 error:
  xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) ret);
  free (ret);
  return NULL;
}

char **
do_inotify_files (void)
{
  DECLARE_STRINGSBUF (ret);
  unsigned int i;
  FILE *fp = NULL;
  guestfs_int_inotify_event_list *events;
  char buf[PATH_MAX];
  char tempfile[] = "/tmp/inotifyXXXXXX";
  int fd;
  char cmd[64];

  NEED_INOTIFY (NULL);

  fd = mkstemp (tempfile);
  if (fd == -1) {
    reply_with_perror ("mkstemp");
    return NULL;
  }

  snprintf (cmd, sizeof cmd, "%s -u > %s", str_sort, tempfile);

  fp = popen (cmd, "w");
  if (fp == NULL) {
    reply_with_perror ("sort");
    return NULL;
  }

  while (1) {
    events = do_inotify_read ();
    if (events == NULL)
      goto error;

    if (events->guestfs_int_inotify_event_list_len == 0) {
      free (events);
      break;			/* End of list of events. */
    }

    for (i = 0; i < events->guestfs_int_inotify_event_list_len; ++i) {
      const char *name = events->guestfs_int_inotify_event_list_val[i].in_name;

      if (name[0] != '\0')
        fprintf (fp, "%s\n", name);
    }

    xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) events);
    free (events);
  }

  pclose (fp);

  fp = fdopen (fd, "r");
  if (fp == NULL) {
    reply_with_perror ("%s", tempfile);
    unlink (tempfile);
    close (fd);
    return NULL;
  }

  while (fgets (buf, sizeof buf, fp) != NULL) {
    size_t len = strlen (buf);

    if (len > 0 && buf[len-1] == '\n')
      buf[len-1] = '\0';

    if (add_string (&ret, buf) == -1)
      goto error;
  }

  fclose (fp); /* implicitly closes fd */
  fp = NULL;

  if (end_stringsbuf (&ret) == -1)
    goto error;

  unlink (tempfile);
  return ret.argv;

 error:
  if (fp != NULL)
    fclose (fp);

  unlink (tempfile);
  return NULL;
}

#else /* !HAVE_SYS_INOTIFY_H */

/* Note that the wrapper code (daemon/stubs.c) ensures that the
 * functions below are never called because optgroup_inotify_available
 * returns false.
 */
int
optgroup_inotify_available (void)
{
  return 0;
}

int
do_inotify_init (int max_events)
{
  abort ();
}

int
do_inotify_close (void)
{
  abort ();
}

int64_t
do_inotify_add_watch (const char *path, int mask)
{
  abort ();
}

int
do_inotify_rm_watch (int wd)
{
  abort ();
}

guestfs_int_inotify_event_list *
do_inotify_read (void)
{
  abort ();
}

char **
do_inotify_files (void)
{
  abort ();
}

#endif