bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "lib.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "buffer.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "settings-parser.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "service-settings.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#include "stats-settings.h"
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic bool stats_metric_settings_check(void *_set, pool_t pool, const char **error_r);
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen/* <settings checks> */
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic struct file_listener_settings stats_unix_listeners_array[] = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen { "stats-reader", 0600, "", "" },
49568744a836768dc6c76db837130b7966c5ff38Timo Sirainen { "stats-writer", 0660, "", "$default_internal_group" },
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic struct file_listener_settings *stats_unix_listeners[] = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen &stats_unix_listeners_array[0],
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen &stats_unix_listeners_array[1],
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic buffer_t stats_unix_listeners_buf = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen stats_unix_listeners, sizeof(stats_unix_listeners), { NULL, }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen/* </settings checks> */
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstruct service_settings stats_service_settings = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .name = "stats",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .protocol = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .type = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .executable = "stats",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .user = "$default_internal_user",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .group = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .privileged_group = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .extra_groups = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .chroot = "empty",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .drop_priv_before_exec = FALSE,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .process_min_avail = 0,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .process_limit = 1,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .client_limit = 0,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .service_count = 0,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .idle_kill = UINT_MAX,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .vsz_limit = (uoff_t)-1,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .unix_listeners = { { &stats_unix_listeners_buf,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen sizeof(stats_unix_listeners[0]) } },
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .inet_listeners = ARRAY_INIT,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#undef DEF
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#define DEF(type, name) \
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen { type, #name, offsetof(struct stats_metric_settings, name), NULL }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic const struct setting_define stats_metric_setting_defines[] = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEF(SET_STR, name),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEF(SET_STR, event_name),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEF(SET_STR, source_location),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEF(SET_STR, categories),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEF(SET_STR, fields),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen { SET_STRLIST, "filter", offsetof(struct stats_metric_settings, filter), NULL },
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen SETTING_DEFINE_LIST_END
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenconst struct stats_metric_settings stats_metric_default_settings = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .name = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .event_name = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .source_location = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .categories = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .fields = "",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenconst struct setting_parser_info stats_metric_setting_parser_info = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .defines = stats_metric_setting_defines,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .defaults = &stats_metric_default_settings,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .type_offset = offsetof(struct stats_metric_settings, name),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .struct_size = sizeof(struct stats_metric_settings),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .parent_offset = (size_t)-1,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .check_func = stats_metric_settings_check,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#undef DEFLIST_UNIQUE
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen#define DEFLIST_UNIQUE(field, name, defines) \
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen { SET_DEFLIST_UNIQUE, name, \
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen offsetof(struct stats_settings, field), defines }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic const struct setting_define stats_setting_defines[] = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen DEFLIST_UNIQUE(metrics, "metric", &stats_metric_setting_parser_info),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen SETTING_DEFINE_LIST_END
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenconst struct stats_settings stats_default_settings = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .metrics = ARRAY_INIT
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenconst struct setting_parser_info stats_setting_parser_info = {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .module_name = "stats",
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .defines = stats_setting_defines,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .defaults = &stats_default_settings,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .type_offset = (size_t)-1,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .struct_size = sizeof(struct stats_settings),
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen .parent_offset = (size_t)-1
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen};
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen/* <settings checks> */
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainenstatic bool stats_metric_settings_check(void *_set, pool_t pool ATTR_UNUSED,
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const char **error_r)
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen{
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen struct stats_metric_settings *set = _set;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen const char *p;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (set->name[0] == '\0') {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen *error_r = "Metric name can't be empty";
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return FALSE;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (set->source_location[0] != '\0') {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if ((p = strchr(set->source_location, ':')) == NULL) {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen *error_r = "source_location is missing ':'";
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return FALSE;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen if (str_to_uint(p+1, &set->parsed_source_linenum) < 0 ||
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen set->parsed_source_linenum == 0) {
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen *error_r = "source_location has invalid line number after ':'";
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return FALSE;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen }
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen return TRUE;
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen}
e9068f1c3326de0265d0ef542a0d7723f5f01bc8Timo Sirainen/* </settings checks> */