NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
monitor.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <errno.h>
32#include <limits.h>
33#include <poll.h>
34#include <stdbool.h>
35#include <stdint.h>
36#include <string.h>
37#include <sys/inotify.h>
38#include <sys/stat.h>
39#include <unistd.h>
40#include "mutt/lib.h"
41#include "core/lib.h"
42#include "gui/lib.h"
43#include "monitor.h"
44#include "index/lib.h"
45#ifndef HAVE_INOTIFY_INIT1
46#include <fcntl.h>
47#endif
48
51
52static int INotifyFd = -1;
53static struct Monitor *Monitor = NULL;
54static size_t PollFdsCount = 0;
55static size_t PollFdsLen = 0;
56static struct pollfd *PollFds = NULL;
57
59
60#define INOTIFY_MASK_DIR (IN_MOVED_TO | IN_ATTRIB | IN_CLOSE_WRITE | IN_ISDIR)
61#define INOTIFY_MASK_FILE IN_CLOSE_WRITE
62
63#define EVENT_BUFLEN MAX(4096, sizeof(struct inotify_event) + NAME_MAX + 1)
64
69{
75};
76
80struct Monitor
81{
82 struct Monitor *next;
84 dev_t st_dev;
85 ino_t st_ino;
87 int desc;
88};
89
94{
96 bool is_dir;
97 const char *path;
98 dev_t st_dev;
99 ino_t st_ino;
102};
103
109static void mutt_poll_fd_add(int fd, short events)
110{
111 int i = 0;
112 for (; (i < PollFdsCount) && (PollFds[i].fd != fd); i++)
113 ; // do nothing
114
115 if (i == PollFdsCount)
116 {
118 {
119 PollFdsLen += 2;
120 mutt_mem_realloc(&PollFds, PollFdsLen * sizeof(struct pollfd));
121 }
122 PollFdsCount++;
123 PollFds[i].fd = fd;
124 PollFds[i].events = events;
125 }
126 else
127 PollFds[i].events |= events;
128}
129
136static int mutt_poll_fd_remove(int fd)
137{
138 int i = 0;
139 for (; (i < PollFdsCount) && (PollFds[i].fd != fd); i++)
140 ; // do nothing
141
142 if (i == PollFdsCount)
143 return -1;
144 int d = PollFdsCount - i - 1;
145 if (d != 0)
146 memmove(&PollFds[i], &PollFds[i + 1], d * sizeof(struct pollfd));
147 PollFdsCount--;
148 return 0;
149}
150
156static int monitor_init(void)
157{
158 if (INotifyFd != -1)
159 return 0;
160
161#ifdef HAVE_INOTIFY_INIT1
162 INotifyFd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
163 if (INotifyFd == -1)
164 {
165 mutt_debug(LL_DEBUG2, "inotify_init1 failed, errno=%d %s\n", errno, strerror(errno));
166 return -1;
167 }
168#else
169 INotifyFd = inotify_init();
170 if (INotifyFd == -1)
171 {
172 mutt_debug(LL_DEBUG2, "monitor: inotify_init failed, errno=%d %s\n", errno,
173 strerror(errno));
174 return -1;
175 }
176 fcntl(INotifyFd, F_SETFL, O_NONBLOCK);
177 fcntl(INotifyFd, F_SETFD, FD_CLOEXEC);
178#endif
179 mutt_poll_fd_add(0, POLLIN);
181
182 return 0;
183}
184
188static void monitor_check_free(void)
189{
190 if (!Monitor && (INotifyFd != -1))
191 {
193 close(INotifyFd);
194 INotifyFd = -1;
195 MonitorFilesChanged = false;
196 }
197}
198
205static struct Monitor *monitor_new(struct MonitorInfo *info, int descriptor)
206{
207 struct Monitor *monitor = mutt_mem_calloc(1, sizeof(struct Monitor));
208 monitor->type = info->type;
209 monitor->st_dev = info->st_dev;
210 monitor->st_ino = info->st_ino;
211 monitor->desc = descriptor;
212 monitor->next = Monitor;
213 if (info->type == MUTT_MH)
214 monitor->mh_backup_path = mutt_str_dup(info->path);
215
216 Monitor = monitor;
217
218 return monitor;
219}
220
225static void monitor_info_init(struct MonitorInfo *info)
226{
227 memset(info, 0, sizeof(*info));
228}
229
234static void monitor_info_free(struct MonitorInfo *info)
235{
237}
238
243static void monitor_delete(struct Monitor *monitor)
244{
245 if (!monitor)
246 return;
247
248 struct Monitor **ptr = &Monitor;
249
250 while (true)
251 {
252 if (!*ptr)
253 return;
254 if (*ptr == monitor)
255 break;
256 ptr = &(*ptr)->next;
257 }
258
259 FREE(&monitor->mh_backup_path);
260 monitor = monitor->next;
261 FREE(ptr);
262 *ptr = monitor;
263}
264
272{
273 int new_desc = -1;
274 struct Monitor *iter = Monitor;
275 struct stat st = { 0 };
276
277 while (iter && (iter->desc != desc))
278 iter = iter->next;
279
280 if (iter)
281 {
282 if ((iter->type == MUTT_MH) && (stat(iter->mh_backup_path, &st) == 0))
283 {
284 new_desc = inotify_add_watch(INotifyFd, iter->mh_backup_path, INOTIFY_MASK_FILE);
285 if (new_desc == -1)
286 {
287 mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n",
288 iter->mh_backup_path, errno, strerror(errno));
289 }
290 else
291 {
292 mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n",
293 desc, iter->mh_backup_path);
294 iter->st_dev = st.st_dev;
295 iter->st_ino = st.st_ino;
296 iter->desc = new_desc;
297 }
298 }
299 else
300 {
301 mutt_debug(LL_DEBUG3, "cleanup watch (implicitly removed) - descriptor=%d\n", desc);
302 }
303
304 if (MonitorContextDescriptor == desc)
305 MonitorContextDescriptor = new_desc;
306
307 if (new_desc == -1)
308 {
309 monitor_delete(iter);
311 }
312 }
313
314 return new_desc;
315}
316
329static enum ResolveResult monitor_resolve(struct MonitorInfo *info, struct Mailbox *m)
330{
331 char *fmt = NULL;
332 struct stat st = { 0 };
333
334 struct Mailbox *m_cur = get_current_mailbox();
335 if (m)
336 {
337 info->type = m->type;
338 info->path = m->realpath;
339 }
340 else if (m_cur)
341 {
342 info->type = m_cur->type;
343 info->path = m_cur->realpath;
344 }
345 else
346 {
348 }
349
350 if (info->type == MUTT_UNKNOWN)
351 {
353 }
354 else if (info->type == MUTT_MAILDIR)
355 {
356 info->is_dir = true;
357 fmt = "%s/new";
358 }
359 else
360 {
361 info->is_dir = false;
362 if (info->type == MUTT_MH)
363 fmt = "%s/.mh_sequences";
364 }
365 if (fmt)
366 {
367 mutt_buffer_printf(&info->path_buf, fmt, info->path);
368 info->path = mutt_buffer_string(&info->path_buf);
369 }
370 if (stat(info->path, &st) != 0)
372
373 struct Monitor *iter = Monitor;
374 while (iter && ((iter->st_ino != st.st_ino) || (iter->st_dev != st.st_dev)))
375 iter = iter->next;
376
377 info->st_dev = st.st_dev;
378 info->st_ino = st.st_ino;
379 info->monitor = iter;
380
382}
383
399{
400 int rc = 0;
401 char buf[EVENT_BUFLEN] __attribute__((aligned(__alignof__(struct inotify_event))));
402
403 MonitorFilesChanged = false;
404
405 if (INotifyFd != -1)
406 {
407 int fds = poll(PollFds, PollFdsCount, MuttGetchTimeout);
408
409 if (fds == -1)
410 {
411 rc = -1;
412 if (errno != EINTR)
413 {
414 mutt_debug(LL_DEBUG2, "poll() failed, errno=%d %s\n", errno, strerror(errno));
415 }
416 }
417 else
418 {
419 bool input_ready = false;
420 for (int i = 0; fds && (i < PollFdsCount); i++)
421 {
422 if (PollFds[i].revents)
423 {
424 fds--;
425 if (PollFds[i].fd == 0)
426 {
427 input_ready = true;
428 }
429 else if (PollFds[i].fd == INotifyFd)
430 {
431 MonitorFilesChanged = true;
432 mutt_debug(LL_DEBUG3, "file change(s) detected\n");
433 char *ptr = buf;
434 const struct inotify_event *event = NULL;
435
436 while (true)
437 {
438 int len = read(INotifyFd, buf, sizeof(buf));
439 if (len == -1)
440 {
441 if (errno != EAGAIN)
442 {
443 mutt_debug(LL_DEBUG2, "read inotify events failed, errno=%d %s\n",
444 errno, strerror(errno));
445 }
446 break;
447 }
448
449 while (ptr < (buf + len))
450 {
451 event = (const struct inotify_event *) ptr;
452 mutt_debug(LL_DEBUG3, "+ detail: descriptor=%d mask=0x%x\n",
453 event->wd, event->mask);
454 if (event->mask & IN_IGNORED)
455 monitor_handle_ignore(event->wd);
456 else if (event->wd == MonitorContextDescriptor)
458 ptr += sizeof(struct inotify_event) + event->len;
459 }
460 }
461 }
462 }
463 }
464 if (!input_ready)
465 rc = MonitorFilesChanged ? -2 : -3;
466 }
467 }
468
469 return rc;
470}
471
481{
482 struct MonitorInfo info;
483 monitor_info_init(&info);
484
485 int rc = 0;
486 enum ResolveResult desc = monitor_resolve(&info, m);
487 if (desc != RESOLVE_RES_OK_NOTEXISTING)
488 {
489 if (!m && (desc == RESOLVE_RES_OK_EXISTING))
491 rc = (desc == RESOLVE_RES_OK_EXISTING) ? 0 : -1;
492 goto cleanup;
493 }
494
495 uint32_t mask = info.is_dir ? INOTIFY_MASK_DIR : INOTIFY_MASK_FILE;
496 if (((INotifyFd == -1) && (monitor_init() == -1)) ||
497 ((desc = inotify_add_watch(INotifyFd, info.path, mask)) == -1))
498 {
499 mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n",
500 info.path, errno, strerror(errno));
501 rc = -1;
502 goto cleanup;
503 }
504
505 mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n", desc, info.path);
506 if (!m)
508
509 monitor_new(&info, desc);
510
511cleanup:
512 monitor_info_free(&info);
513 return rc;
514}
515
526{
527 struct MonitorInfo info, info2;
528 int rc = 0;
529
530 monitor_info_init(&info);
531 monitor_info_init(&info2);
532
533 if (!m)
534 {
536 MonitorContextChanged = false;
537 }
538
540 {
541 rc = 2;
542 goto cleanup;
543 }
544
545 struct Mailbox *m_cur = get_current_mailbox();
546 if (m_cur)
547 {
548 if (m)
549 {
550 if ((monitor_resolve(&info2, NULL) == RESOLVE_RES_OK_EXISTING) &&
551 (info.st_ino == info2.st_ino) && (info.st_dev == info2.st_dev))
552 {
553 rc = 1;
554 goto cleanup;
555 }
556 }
557 else
558 {
559 if (mailbox_find(m_cur->realpath))
560 {
561 rc = 1;
562 goto cleanup;
563 }
564 }
565 }
566
567 inotify_rm_watch(info.monitor->desc, INotifyFd);
568 mutt_debug(LL_DEBUG3, "inotify_rm_watch for '%s' descriptor=%d\n", info.path,
569 info.monitor->desc);
570
573
574cleanup:
575 monitor_info_free(&info);
576 monitor_info_free(&info2);
577 return rc;
578}
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:347
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
Convenience wrapper for the core headers.
int MuttGetchTimeout
Timeout in ms for mutt_getch()
Definition: curs_lib.c:123
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
Convenience wrapper for the gui headers.
GUI manage the main index (list of emails)
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:618
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:139
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
@ MUTT_MH
'MH' Mailbox type
Definition: mailbox.h:47
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition: mailbox.h:44
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:43
#define INOTIFY_MASK_DIR
Definition: monitor.c:60
int mutt_monitor_add(struct Mailbox *m)
Add a watch for a mailbox.
Definition: monitor.c:480
static void monitor_delete(struct Monitor *monitor)
Free a file monitor.
Definition: monitor.c:243
static int MonitorContextDescriptor
Definition: monitor.c:58
static int mutt_poll_fd_remove(int fd)
Remove a file from the watch list.
Definition: monitor.c:136
static int INotifyFd
Definition: monitor.c:52
static int monitor_handle_ignore(int desc)
Listen for when a backup file is closed.
Definition: monitor.c:271
static enum ResolveResult monitor_resolve(struct MonitorInfo *info, struct Mailbox *m)
Get the monitor for a mailbox.
Definition: monitor.c:329
static struct Monitor * monitor_new(struct MonitorInfo *info, int descriptor)
Create a new file monitor.
Definition: monitor.c:205
int mutt_monitor_poll(void)
Check for filesystem changes.
Definition: monitor.c:398
static size_t PollFdsCount
Definition: monitor.c:54
static void mutt_poll_fd_add(int fd, short events)
Add a file to the watch list.
Definition: monitor.c:109
ResolveResult
Results for the Monitor functions.
Definition: monitor.c:69
@ RESOLVE_RES_FAIL_NOTYPE
Can't identify Mailbox type.
Definition: monitor.c:71
@ RESOLVE_RES_FAIL_STAT
Can't stat() the Mailbox file.
Definition: monitor.c:72
@ RESOLVE_RES_OK_NOTEXISTING
File exists, no monitor is attached.
Definition: monitor.c:73
@ RESOLVE_RES_FAIL_NOMAILBOX
No Mailbox to work on.
Definition: monitor.c:70
@ RESOLVE_RES_OK_EXISTING
File exists, monitor is already attached.
Definition: monitor.c:74
bool MonitorFilesChanged
true after a monitored file has changed
Definition: monitor.c:49
static void monitor_check_free(void)
Close down file monitoring.
Definition: monitor.c:188
static int monitor_init(void)
Set up file monitoring.
Definition: monitor.c:156
static struct Monitor * Monitor
Definition: monitor.c:53
bool MonitorContextChanged
true after the current mailbox has changed
Definition: monitor.c:50
static void monitor_info_init(struct MonitorInfo *info)
Set up a file monitor.
Definition: monitor.c:225
#define INOTIFY_MASK_FILE
Definition: monitor.c:61
#define EVENT_BUFLEN
Definition: monitor.c:63
static void monitor_info_free(struct MonitorInfo *info)
Shutdown a file monitor.
Definition: monitor.c:234
int mutt_monitor_remove(struct Mailbox *m)
Remove a watch for a mailbox.
Definition: monitor.c:525
static size_t PollFdsLen
Definition: monitor.c:55
static struct pollfd * PollFds
Definition: monitor.c:56
Monitor files for changes.
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
String manipulation buffer.
Definition: buffer.h:34
A mailbox.
Definition: mailbox.h:79
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
Information about a monitored file.
Definition: monitor.c:94
dev_t st_dev
Definition: monitor.c:98
enum MailboxType type
Definition: monitor.c:95
struct Monitor * monitor
Definition: monitor.c:100
bool is_dir
Definition: monitor.c:96
ino_t st_ino
Definition: monitor.c:99
const char * path
Definition: monitor.c:97
struct Buffer path_buf
access via path only (maybe not initialized)
Definition: monitor.c:101
A watch on a file.
Definition: monitor.c:81
struct Monitor * next
Linked list.
Definition: monitor.c:82
ino_t st_ino
Definition: monitor.c:85
dev_t st_dev
Definition: monitor.c:84
int desc
Definition: monitor.c:87
enum MailboxType type
Definition: monitor.c:86
char * mh_backup_path
Definition: monitor.c:83