forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
516 lines (463 loc) · 12.4 KB
/
monitor.c
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* @file
* Monitor files for changes
*
* @authors
* Copyright (C) 2018 Gero Treuer <[email protected]>
*
* @copyright
* 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, see <http://www.gnu.org/licenses/>.
*/
/**
* @page monitor Monitor files for changes
*
* Monitor files for changes
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "monitor.h"
#include "context.h"
#include "curs_lib.h"
#include "globals.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mx.h"
int MonitorFilesChanged = 0;
int MonitorContextChanged = 0;
static int INotifyFd = -1;
static struct Monitor *Monitor = NULL;
static size_t PollFdsCount = 0;
static size_t PollFdsLen = 0;
static struct pollfd *PollFds = NULL;
static int MonitorContextDescriptor = -1;
#define INOTIFY_MASK_DIR (IN_MOVED_TO | IN_ATTRIB | IN_CLOSE_WRITE | IN_ISDIR)
#define INOTIFY_MASK_FILE IN_CLOSE_WRITE
#define EVENT_BUFLEN MAX(4096, sizeof(struct inotify_event) + NAME_MAX + 1)
#define RESOLVERES_FAIL_NOMAILBOX -3
#define RESOLVERES_FAIL_NOMAGIC -2
#define RESOLVERES_FAIL_STAT -1
#define RESOLVERES_OK_NOTEXISTING 0
#define RESOLVERES_OK_EXISTING 1
/**
* struct Monitor - A watch on a file
*/
struct Monitor
{
struct Monitor *next;
char *mh_backup_path;
dev_t st_dev;
ino_t st_ino;
enum MailboxType magic;
int desc;
};
/**
* struct MonitorInfo - Information about a monitored file
*/
struct MonitorInfo
{
enum MailboxType magic;
short isdir;
const char *path;
dev_t st_dev;
ino_t st_ino;
struct Monitor *monitor;
char path_buf[PATH_MAX]; /* access via path only (maybe not initialized) */
};
/**
* mutt_poll_fd_add - Add a file to the watch list
* @param fd File to watch
* @param events Events to listen for, e.g. POLLIN
*/
static void mutt_poll_fd_add(int fd, short events)
{
int i = 0;
for (; (i < PollFdsCount) && (PollFds[i].fd != fd); i++)
;
if (i == PollFdsCount)
{
if (PollFdsCount == PollFdsLen)
{
PollFdsLen += 2;
mutt_mem_realloc(&PollFds, PollFdsLen * sizeof(struct pollfd));
}
PollFdsCount++;
PollFds[i].fd = fd;
PollFds[i].events = events;
}
else
PollFds[i].events |= events;
}
/**
* mutt_poll_fd_remove - Remove a file from the watch list
* @param fd File to remove
* @retval 0 Success
* @retval -1 Error
*/
static int mutt_poll_fd_remove(int fd)
{
int i = 0;
for (; (i < PollFdsCount) && (PollFds[i].fd != fd); i++)
;
if (i == PollFdsCount)
return -1;
int d = PollFdsCount - i - 1;
if (d != 0)
memmove(&PollFds[i], &PollFds[i + 1], d * sizeof(struct pollfd));
PollFdsCount--;
return 0;
}
/**
* monitor_init - Set up file monitoring
* @retval 0 Success
* @retval -1 Error
*/
static int monitor_init(void)
{
if (INotifyFd == -1)
{
INotifyFd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (INotifyFd == -1)
{
mutt_debug(LL_DEBUG2, "inotify_init1 failed, errno=%d %s\n", errno, strerror(errno));
return -1;
}
mutt_poll_fd_add(0, POLLIN);
mutt_poll_fd_add(INotifyFd, POLLIN);
}
return 0;
}
/**
* monitor_check_free - Close down file monitoring
*/
static void monitor_check_free(void)
{
if (!Monitor && (INotifyFd != -1))
{
mutt_poll_fd_remove(INotifyFd);
close(INotifyFd);
INotifyFd = -1;
MonitorFilesChanged = 0;
}
}
/**
* monitor_new - Create a new file monitor
* @param info Details of file to monitor
* @param descriptor Watch descriptor
* @retval ptr Newly allocated Monitor
*/
static struct Monitor *monitor_new(struct MonitorInfo *info, int descriptor)
{
struct Monitor *monitor = mutt_mem_calloc(1, sizeof(struct Monitor));
monitor->magic = info->magic;
monitor->st_dev = info->st_dev;
monitor->st_ino = info->st_ino;
monitor->desc = descriptor;
monitor->next = Monitor;
if (info->magic == MUTT_MH)
monitor->mh_backup_path = mutt_str_strdup(info->path);
Monitor = monitor;
return monitor;
}
/**
* monitor_delete - Free a file monitor
* @param monitor Monitor to free
*/
static void monitor_delete(struct Monitor *monitor)
{
struct Monitor **ptr = &Monitor;
if (!monitor)
return;
while (true)
{
if (!*ptr)
return;
if (*ptr == monitor)
break;
ptr = &(*ptr)->next;
}
FREE(&monitor->mh_backup_path);
monitor = monitor->next;
FREE(ptr);
*ptr = monitor;
}
/**
* monitor_handle_ignore - Listen for when a backup file is closed
* @param desc Watch descriptor
* @retval >=0 New descriptor
* @retval -1 Error
*/
static int monitor_handle_ignore(int desc)
{
int new_desc = -1;
struct Monitor *iter = Monitor;
struct stat sb;
while (iter && (iter->desc != desc))
iter = iter->next;
if (iter)
{
if ((iter->magic == MUTT_MH) && (stat(iter->mh_backup_path, &sb) == 0))
{
new_desc = inotify_add_watch(INotifyFd, iter->mh_backup_path, INOTIFY_MASK_FILE);
if (new_desc == -1)
{
mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n",
iter->mh_backup_path, errno, strerror(errno));
}
else
{
mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n",
desc, iter->mh_backup_path);
iter->st_dev = sb.st_dev;
iter->st_ino = sb.st_ino;
iter->desc = new_desc;
}
}
else
{
mutt_debug(LL_DEBUG3, "cleanup watch (implicitly removed) - descriptor=%d\n", desc);
}
if (MonitorContextDescriptor == desc)
MonitorContextDescriptor = new_desc;
if (new_desc == -1)
{
monitor_delete(iter);
monitor_check_free();
}
}
return new_desc;
}
/**
* monitor_resolve - Get the monitor for a mailbox
* @param[out] info Details of the mailbox's monitor
* @param[in] m Mailbox
* @retval >=0 mailbox is valid and locally accessible:
* 0: no monitor / 1: preexisting monitor
* @retval -3 no mailbox (MonitorInfo: no fields set)
* @retval -2 magic not set
* @retval -1 stat() failed (see errno; MonitorInfo fields: magic, isdir, path)
*
* If m is NULL, the current mailbox (Context) is used.
*/
static int monitor_resolve(struct MonitorInfo *info, struct Mailbox *m)
{
struct Monitor *iter;
char *fmt = NULL;
struct stat sb;
if (m)
{
info->magic = m->magic;
info->path = m->realpath;
}
else if (Context)
{
info->magic = Context->mailbox->magic;
info->path = Context->mailbox->realpath;
}
else
{
return RESOLVERES_FAIL_NOMAILBOX;
}
if (!info->magic)
{
return RESOLVERES_FAIL_NOMAGIC;
}
else if (info->magic == MUTT_MAILDIR)
{
info->isdir = 1;
fmt = "%s/new";
}
else
{
info->isdir = 0;
if (info->magic == MUTT_MH)
fmt = "%s/.mh_sequences";
}
if (fmt)
{
snprintf(info->path_buf, sizeof(info->path_buf), fmt, info->path);
info->path = info->path_buf;
}
if (stat(info->path, &sb) != 0)
return RESOLVERES_FAIL_STAT;
iter = Monitor;
while (iter && ((iter->st_ino != sb.st_ino) || (iter->st_dev != sb.st_dev)))
iter = iter->next;
info->st_dev = sb.st_dev;
info->st_ino = sb.st_ino;
info->monitor = iter;
return iter ? RESOLVERES_OK_EXISTING : RESOLVERES_OK_NOTEXISTING;
}
/**
* mutt_monitor_poll - Check for filesystem changes
* @retval -3 unknown/unexpected events: poll timeout / fds not handled by us
* @retval -2 monitor detected changes, no STDIN input
* @retval -1 error (see errno)
* @retval 0 (1) input ready from STDIN, or (2) monitoring inactive -> no poll()
*
* Wait for I/O ready file descriptors or signals.
*
* MonitorFilesChanged also reflects changes to monitored files.
*
* Only STDIN and INotify file handles currently expected/supported.
* More would ask for common infrastructure (sockets?).
*/
int mutt_monitor_poll(void)
{
int rc = 0;
char buf[EVENT_BUFLEN] __attribute__((aligned(__alignof__(struct inotify_event))));
MonitorFilesChanged = 0;
if (INotifyFd != -1)
{
int fds = poll(PollFds, PollFdsLen, MuttGetchTimeout);
if (fds == -1)
{
rc = -1;
if (errno != EINTR)
{
mutt_debug(LL_DEBUG2, "poll() failed, errno=%d %s\n", errno, strerror(errno));
}
}
else
{
bool inputReady = false;
for (int i = 0; fds && (i < PollFdsCount); i++)
{
if (PollFds[i].revents)
{
fds--;
if (PollFds[i].fd == 0)
{
inputReady = true;
}
else if (PollFds[i].fd == INotifyFd)
{
MonitorFilesChanged = 1;
mutt_debug(LL_DEBUG3, "file change(s) detected\n");
char *ptr = buf;
const struct inotify_event *event;
while (true)
{
int len = read(INotifyFd, buf, sizeof(buf));
if (len == -1)
{
if (errno != EAGAIN)
mutt_debug(LL_DEBUG2, "read inotify events failed, errno=%d %s\n",
errno, strerror(errno));
break;
}
while (ptr < (buf + len))
{
event = (const struct inotify_event *) ptr;
mutt_debug(LL_DEBUG3, "+ detail: descriptor=%d mask=0x%x\n",
event->wd, event->mask);
if (event->mask & IN_IGNORED)
monitor_handle_ignore(event->wd);
else if (event->wd == MonitorContextDescriptor)
MonitorContextChanged = 1;
ptr += sizeof(struct inotify_event) + event->len;
}
}
}
}
}
if (!inputReady)
rc = MonitorFilesChanged ? -2 : -3;
}
}
return rc;
}
/**
* mutt_monitor_add - Add a watch for a mailbox
* @param m Mailbox to watch
* @retval 0 success: new or already existing monitor
* @retval -1 failed: no mailbox, inaccessible file, create monitor/watcher failed
*
* If mailbox is NULL, the current mailbox (Context) is used.
*/
int mutt_monitor_add(struct Mailbox *m)
{
struct MonitorInfo info;
int desc = monitor_resolve(&info, m);
if (desc != RESOLVERES_OK_NOTEXISTING)
{
if (!m && (desc == RESOLVERES_OK_EXISTING))
MonitorContextDescriptor = info.monitor->desc;
return (desc == RESOLVERES_OK_EXISTING) ? 0 : -1;
}
uint32_t mask = info.isdir ? INOTIFY_MASK_DIR : INOTIFY_MASK_FILE;
if (((INotifyFd == -1) && (monitor_init() == -1)) ||
((desc = inotify_add_watch(INotifyFd, info.path, mask)) == -1))
{
mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n",
info.path, errno, strerror(errno));
return -1;
}
mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n", desc, info.path);
if (!m)
MonitorContextDescriptor = desc;
monitor_new(&info, desc);
return 0;
}
/**
* mutt_monitor_remove - Remove a watch for a mailbox
* @param m Mailbox
* @retval 0 monitor removed (not shared)
* @retval 1 monitor not removed (shared)
* @retval 2 no monitor
*
* If mailbox is NULL, the current mailbox (Context) is used.
*/
int mutt_monitor_remove(struct Mailbox *m)
{
struct MonitorInfo info, info2;
if (!m)
{
MonitorContextDescriptor = -1;
MonitorContextChanged = 0;
}
if (monitor_resolve(&info, m) != RESOLVERES_OK_EXISTING)
return 2;
if (Context)
{
if (m)
{
if ((monitor_resolve(&info2, NULL) == RESOLVERES_OK_EXISTING) &&
(info.st_ino == info2.st_ino) && (info.st_dev == info2.st_dev))
{
return 1;
}
}
else
{
if (mutt_find_mailbox(Context->mailbox->realpath))
return 1;
}
}
inotify_rm_watch(info.monitor->desc, INotifyFd);
mutt_debug(LL_DEBUG3, "inotify_rm_watch for '%s' descriptor=%d\n", info.path,
info.monitor->desc);
monitor_delete(info.monitor);
monitor_check_free();
return 0;
}