bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
5d4eaaf7babd0ada6fad82887b9cd9480dbc25b0Timo Sirainen#include "lib.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "lib-signals.h"
9a795eb60306eede5efc7e8ec5d584457a49e32eTimo Sirainen#include "hostpid.h"
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen#include "restrict-access.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "master-interface.h"
84703c2f19113ac731e4638ba782fa87e0748ba6Timo Sirainen#include "master-service-private.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "master-service-settings.h"
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen#include "log-error-buffer.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "log-connection.h"
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen#include "doveadm-connection.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include <unistd.h>
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
f2880c3c3de56ec757ffc393363bb863efd7bcbeTimo Sirainenbool verbose_proctitle;
2fb27941fb13f48e728365c1332039bfa63aca6eTimo Sirainenchar *global_log_prefix;
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainenstatic struct log_error_buffer *errorbuf;
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic void
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainensig_reopen_logs(const siginfo_t *si ATTR_UNUSED, void *context ATTR_UNUSED)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen{
84703c2f19113ac731e4638ba782fa87e0748ba6Timo Sirainen master_service->log_initialized = FALSE;
9a795eb60306eede5efc7e8ec5d584457a49e32eTimo Sirainen master_service_init_log(master_service, global_log_prefix);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic void main_init(void)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen{
5512d420d826a2f9d4e7cb4e4919e1864fe688b0Timo Sirainen lib_signals_set_handler(SIGUSR1, LIBSIG_FLAGS_SAFE,
5512d420d826a2f9d4e7cb4e4919e1864fe688b0Timo Sirainen sig_reopen_logs, NULL);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen errorbuf = log_error_buffer_init();
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen log_connections_init();
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic void main_deinit(void)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen{
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen log_connections_deinit();
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen log_error_buffer_deinit(&errorbuf);
2fb27941fb13f48e728365c1332039bfa63aca6eTimo Sirainen i_free(global_log_prefix);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen{
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen if (conn->fifo) {
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen log_connection_create(errorbuf, conn->fd, conn->listen_fd);
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen /* kludge: normally FIFOs aren't counted as connections,
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen but here we want log process to stay open until all writers
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen have closed */
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen conn->fifo = FALSE;
9da1a079c12108658d58876cd8d157418b3b518cTimo Sirainen } else if (strcmp(conn->name, "log-errors") == 0)
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen doveadm_connection_create(errorbuf, conn->fd);
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen else {
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen i_error("Unknown listener name: %s", conn->name);
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen return;
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen }
acba68a69cdd6f3f00faa18cccef356d95048e46Timo Sirainen
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen master_service_client_connection_accept(conn);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenint main(int argc, char *argv[])
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen{
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *error;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen master_service = master_service_init("log", 0, &argc, &argv, "");
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* use log prefix and log to stderr until we've configured the real
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen logging */
2fb27941fb13f48e728365c1332039bfa63aca6eTimo Sirainen global_log_prefix = i_strdup_printf("log(%s): ", my_pid);
9a795eb60306eede5efc7e8ec5d584457a49e32eTimo Sirainen i_set_failure_file("/dev/stderr", global_log_prefix);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen if (master_getopt(master_service) > 0)
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen return FATAL_DEFAULT;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen if (master_service_settings_read_simple(master_service,
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen NULL, &error) < 0)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen i_fatal("Error reading configuration: %s", error);
9a795eb60306eede5efc7e8ec5d584457a49e32eTimo Sirainen master_service_init_log(master_service, global_log_prefix);
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen
f2880c3c3de56ec757ffc393363bb863efd7bcbeTimo Sirainen verbose_proctitle = master_service_settings_get(master_service)->verbose_proctitle;
f2880c3c3de56ec757ffc393363bb863efd7bcbeTimo Sirainen
816d20be0cf95fc4eb1a8aa716639e73b8ba525eMartti Rannanjärvi restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen restrict_access_allow_coredumps(TRUE);
4ff8be55becadd47c67c57ff1ec4f61b100fcab9Timo Sirainen
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen /* logging should never die if there are some clients */
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen master_service_set_die_with_master(master_service, FALSE);
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen main_init();
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen master_service_init_finish(master_service);
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_run(master_service, client_connected);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen main_deinit();
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen master_service_deinit(&master_service);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen return 0;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen}