bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "lib.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "restrict-access.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "master-service.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "master-service-settings.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "stats-settings.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "stats-event-category.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "stats-metrics.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "client-writer.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "client-reader.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic struct stats_metrics *metrics;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic bool client_is_writer(const char *path)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const char *name, *suffix;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen name = strrchr(path, '/');
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (name == NULL)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen name = path;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen else
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen name++;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen suffix = strrchr(name, '-');
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (suffix == NULL)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen suffix = name;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen else
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen suffix++;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return strcmp(suffix, "writer") == 0;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (client_is_writer(conn->name))
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen (void)client_writer_create(conn->fd, metrics);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen else
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen (void)client_reader_create(conn->fd, metrics);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service_client_connection_accept(conn);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic void main_preinit(void)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
816d20be0cf95fc4eb1a8aa716639e73b8ba525eMartti Rannanjärvi restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen restrict_access_allow_coredumps(TRUE);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic void main_init(void)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen void **sets = master_service_settings_get_others(master_service);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const struct stats_settings *set = sets[0];
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen metrics = stats_metrics_init(set);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen stats_event_categories_init();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen client_readers_init();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen client_writers_init();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic void main_deinit(void)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen client_readers_deinit();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen client_writers_deinit();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen stats_event_categories_deinit();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen stats_metrics_deinit(&metrics);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenint main(int argc, char *argv[])
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const struct setting_parser_info *set_roots[] = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen &stats_setting_parser_info,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen NULL
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen };
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const enum master_service_flags service_flags =
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen MASTER_SERVICE_FLAG_NO_IDLE_DIE |
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen MASTER_SERVICE_FLAG_UPDATE_PROCTITLE;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const char *error;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service = master_service_init("stats", service_flags,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen &argc, &argv, "");
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (master_getopt(master_service) > 0)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return FATAL_DEFAULT;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (master_service_settings_read_simple(master_service, set_roots,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen &error) < 0)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen i_fatal("Error reading configuration: %s", error);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service_init_log(master_service, "stats: ");
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen main_preinit();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service_init_finish(master_service);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen main_init();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service_run(master_service, client_connected);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen main_deinit();
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen master_service_deinit(&master_service);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return 0;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}