bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "lib.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "array.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "aqueue.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "ioloop.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "service.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "service-process.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "service-process-notify.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstruct service_process_notify {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_notify_callback_t *write_callback;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen int fd;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct io *io_write;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct aqueue *process_queue;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct service_process *) processes;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen};
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstruct service_process_notify *
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenservice_process_notify_init(int fd,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_notify_callback_t *write_callback)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct service_process_notify *notify;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify = i_new(struct service_process_notify, 1);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify->fd = fd;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify->write_callback = write_callback;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen i_array_init(&notify->processes, 64);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify->process_queue = aqueue_init(&notify->processes.arr);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return notify;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstatic void service_process_notify_reset(struct service_process_notify *notify)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct service_process *const *processes, *process;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen unsigned int i, count;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (notify->io_write == NULL)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen processes = array_idx_modifiable(&notify->processes, 0);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen count = aqueue_count(notify->process_queue);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen for (i = 0; i < count; i++) {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen process = processes[aqueue_idx(notify->process_queue, i)];
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_unref(process);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen aqueue_clear(notify->process_queue);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen array_clear(&notify->processes);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen io_remove(&notify->io_write);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstatic void notify_flush(struct service_process_notify *notify)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct service_process *const *processes, *process;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen while (aqueue_count(notify->process_queue) > 0) {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen processes = array_idx_modifiable(&notify->processes, 0);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen process = processes[aqueue_idx(notify->process_queue, 0)];
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (notify->write_callback(notify->fd, process) < 0) {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (errno != EAGAIN)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_notify_reset(notify);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_unref(process);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen aqueue_delete_tail(notify->process_queue);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen io_remove(&notify->io_write);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenvoid service_process_notify_deinit(struct service_process_notify **_notify)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct service_process_notify *notify = *_notify;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen *_notify = NULL;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_notify_reset(notify);
5f1d689131a75c39f064cbd4202373e7edf78f18Josef 'Jeff' Sipek io_remove(&notify->io_write);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen aqueue_deinit(&notify->process_queue);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen array_free(&notify->processes);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen i_free(notify);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenvoid service_process_notify_add(struct service_process_notify *notify,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen struct service_process *process)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (notify->write_callback(notify->fd, process) < 0) {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (errno != EAGAIN)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if (notify->io_write == NULL) {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify->io_write = io_add(notify->fd, IO_WRITE,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen notify_flush, notify);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen aqueue_append(notify->process_queue, &process);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen service_process_ref(process);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}