bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "common.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "array.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "env-util.h"
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen#include "fdpass.h"
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen#include "ioloop.h"
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen#include "restrict-access.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "master-service.h"
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen#include "master-service-settings.h"
c405d7235ba2e21a306eacc9fc0f15e66f9ee718Timo Sirainen#include "master-interface.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "connect-limit.h"
7e7cdca78e6a67757188406c8de9db42fcd17881Timo Sirainen#include "penalty.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "anvil-connection.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen#include <unistd.h>
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstruct connect_limit *connect_limit;
7e7cdca78e6a67757188406c8de9db42fcd17881Timo Sirainenstruct penalty *penalty;
ab96e72a574ff5e579500513cd62f14ec142e467Timo Sirainenbool anvil_restarted;
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainenstatic struct io *log_fdpass_io;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
c405d7235ba2e21a306eacc9fc0f15e66f9ee718Timo Sirainen bool master = conn->listen_fd == MASTER_LISTEN_FD_FIRST;
c405d7235ba2e21a306eacc9fc0f15e66f9ee718Timo Sirainen
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen master_service_client_connection_accept(conn);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)anvil_connection_create(conn->fd, master, conn->fifo);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenstatic void ATTR_NULL(1)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenlog_fdpass_input(void *context ATTR_UNUSED)
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen{
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen int fd;
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen char c;
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen ssize_t ret;
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen /* master wants us to replace the log fd */
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen ret = fd_read(MASTER_ANVIL_LOG_FDPASS_FD, &c, 1, &fd);
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen if (ret < 0)
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen i_error("fd_read(log fd) failed: %m");
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen else if (ret == 0) {
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen /* master died. lib-master should notice it soon. */
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen io_remove(&log_fdpass_io);
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen } else {
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen if (dup2(fd, STDERR_FILENO) < 0)
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen i_fatal("dup2(fd_read log fd, stderr) failed: %m");
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen if (close(fd) < 0)
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen i_error("close(fd_read log fd) failed: %m");
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen }
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen}
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenint main(int argc, char *argv[])
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen const enum master_service_flags service_flags =
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen MASTER_SERVICE_FLAG_UPDATE_PROCTITLE;
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen const char *error;
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen master_service = master_service_init("anvil", service_flags,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen &argc, &argv, "");
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen if (master_getopt(master_service) > 0)
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen return FATAL_DEFAULT;
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen if (master_service_settings_read_simple(master_service,
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen NULL, &error) < 0)
737f6f1c3c591a47b8b48d54563d6ad1b694f256Timo Sirainen i_fatal("Error reading configuration: %s", error);
e0740628f6ca05f4bc79a9d8a90b650f4d38d4d0Timo Sirainen master_service_init_log(master_service, "anvil: ");
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen
816d20be0cf95fc4eb1a8aa716639e73b8ba525eMartti Rannanjärvi restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen restrict_access_allow_coredumps(TRUE);
ab96e72a574ff5e579500513cd62f14ec142e467Timo Sirainen anvil_restarted = getenv("ANVIL_RESTARTED") != NULL;
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen
8787860a1e0519f19ac2cdc270cecd4673fad541Timo Sirainen /* delay dying until all of our clients are gone */
8787860a1e0519f19ac2cdc270cecd4673fad541Timo Sirainen master_service_set_die_with_master(master_service, FALSE);
8787860a1e0519f19ac2cdc270cecd4673fad541Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen connect_limit = connect_limit_init();
7e7cdca78e6a67757188406c8de9db42fcd17881Timo Sirainen penalty = penalty_init();
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen log_fdpass_io = io_add(MASTER_ANVIL_LOG_FDPASS_FD, IO_READ,
677b75f90d81eafe742896d6570a2f63ce501d05Josef 'Jeff' Sipek log_fdpass_input, NULL);
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen master_service_init_finish(master_service);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_run(master_service, client_connected);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
5f1d689131a75c39f064cbd4202373e7edf78f18Josef 'Jeff' Sipek io_remove(&log_fdpass_io);
7e7cdca78e6a67757188406c8de9db42fcd17881Timo Sirainen penalty_deinit(&penalty);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen connect_limit_deinit(&connect_limit);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen anvil_connections_destroy_all();
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_deinit(&master_service);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return 0;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}