ioloop-notify-inotify.c revision a6280be05b9c90579bb59ff57a3035661706c3d3
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2005-2017 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct ioloop_notify_handler_context *io_loop_notify_handler_init(void);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic bool inotify_input_more(struct ioloop *ioloop)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* read as many events as there is available and fit into our buffer.
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen only full events are returned by the kernel. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = read(ctx->inotify_fd, event_buf, sizeof(event_buf));
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen /* nothing more to read */
e8762c2b4914db7997fa9eb644a91586952d1876Timo Sirainen event = (struct inotify_event *)(event_buf + pos);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen io = io_notify_fd_find(&ctx->fd_ctx, event->wd);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* calling inotify_rm_watch() would now give
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_error("read(inotify) returned partial event");
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic void inotify_input(struct ioloop *ioloop)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenio_add_notify(const char *path, const char *source_filename,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen io_callback_t *callback, void *context, struct io **io_r)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* ESTALE could happen with NFS. Don't bother giving an error
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen message then. */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_error("inotify_add_watch(%s) failed: %m", path);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_warning("Inotify watch limit for user exceeded, "
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen "disabling. Increase "
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ctx->event_io = io_add(ctx->inotify_fd, IO_READ,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen *io_r = io_notify_fd_add(&ctx->fd_ctx, wd, callback, context);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct io_notify *io = (struct io_notify *)_io;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* ernro=EINVAL happens if the file itself is deleted and
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen kernel has sent IN_IGNORED event which we haven't read. */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if (inotify_rm_watch(ctx->inotify_fd, io->fd) < 0 &&
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (ctx->fd_ctx.notifies == NULL && ctx->event_io != NULL)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void ioloop_inotify_user_limit_exceeded(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen name = t_strdup_printf("UID %s", dec2str(uid));
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_warning("Inotify instance limit for user %s exceeded, disabling. "
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen "Increase /proc/sys/fs/inotify/max_user_instances", name);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct ioloop_notify_handler_context *io_loop_notify_handler_init(void)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_new(struct ioloop_notify_handler_context, 1);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenvoid io_loop_notify_handler_deinit(struct ioloop *ioloop)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_warning("I/O notify leak: %p (%s:%u, fd %d)",
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenint io_loop_extract_notify_fd(struct ioloop *ioloop)