summaryrefslogtreecommitdiffstats
path: root/ctdb/event/event_config.c
blob: d2826225c8048fbbcfb555b132421165e24a0db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
   CTDB event daemon - config handling

   Copyright (C) Amitay Isaacs  2018

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <talloc.h>

#include "common/conf.h"
#include "common/logging_conf.h"
#include "common/path.h"

#include "event/event_private.h"
#include "event/event_conf.h"

struct event_config {
	char *config_file;
	struct conf_context *conf;

	const char *logging_location;
	const char *logging_loglevel;
	const char *debug_script;
};

int event_config_init(TALLOC_CTX *mem_ctx, struct event_config **result)
{
	struct event_config *config;
	int ret;
	bool ok;

	config = talloc_zero(mem_ctx, struct event_config);
	if (config == NULL) {
		return ENOMEM;
	}

	config->config_file = path_config(config);
	if (config->config_file == NULL) {
		talloc_free(config);
		return ENOMEM;
	}

	ret = conf_init(config, &config->conf);
	if (ret != 0) {
		talloc_free(config);
		return ret;
	}

	logging_conf_init(config->conf, NULL);

	conf_assign_string_pointer(config->conf,
				   LOGGING_CONF_SECTION,
				   LOGGING_CONF_LOCATION,
				   &config->logging_location);
	conf_assign_string_pointer(config->conf,
				   LOGGING_CONF_SECTION,
				   LOGGING_CONF_LOG_LEVEL,
				   &config->logging_loglevel);

	event_conf_init(config->conf);

	conf_assign_string_pointer(config->conf,
				   EVENT_CONF_SECTION,
				   EVENT_CONF_DEBUG_SCRIPT,
				   &config->debug_script);

	ok = conf_valid(config->conf);
	if (!ok) {
		talloc_free(config);
		return EINVAL;
	}

	ret = conf_load(config->conf, config->config_file, true);
	if (ret != 0 && ret != ENOENT) {
		talloc_free(config);
		return ret;
	}

	*result = config;
	return 0;
}

const char *event_config_log_location(struct event_config *config)
{
	return config->logging_location;
}

const char *event_config_log_level(struct event_config *config)
{
	return config->logging_loglevel;
}

const char *event_config_debug_script(struct event_config *config)
{
	return config->debug_script;
}

int event_config_reload(struct event_config *config)
{
	int ret;

	ret = conf_reload(config->conf);
	if (ret != 0 && ret != ENOENT) {
		return ret;
	}

	return 0;
}