d2826225c8048fbbcfb555b132421165e24a0db6
[vlendec/samba-autobuild/.git] / event_config.c
1 /*
2    CTDB event daemon - config handling
3
4    Copyright (C) Amitay Isaacs  2018
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21
22 #include <talloc.h>
23
24 #include "common/conf.h"
25 #include "common/logging_conf.h"
26 #include "common/path.h"
27
28 #include "event/event_private.h"
29 #include "event/event_conf.h"
30
31 struct event_config {
32         char *config_file;
33         struct conf_context *conf;
34
35         const char *logging_location;
36         const char *logging_loglevel;
37         const char *debug_script;
38 };
39
40 int event_config_init(TALLOC_CTX *mem_ctx, struct event_config **result)
41 {
42         struct event_config *config;
43         int ret;
44         bool ok;
45
46         config = talloc_zero(mem_ctx, struct event_config);
47         if (config == NULL) {
48                 return ENOMEM;
49         }
50
51         config->config_file = path_config(config);
52         if (config->config_file == NULL) {
53                 talloc_free(config);
54                 return ENOMEM;
55         }
56
57         ret = conf_init(config, &config->conf);
58         if (ret != 0) {
59                 talloc_free(config);
60                 return ret;
61         }
62
63         logging_conf_init(config->conf, NULL);
64
65         conf_assign_string_pointer(config->conf,
66                                    LOGGING_CONF_SECTION,
67                                    LOGGING_CONF_LOCATION,
68                                    &config->logging_location);
69         conf_assign_string_pointer(config->conf,
70                                    LOGGING_CONF_SECTION,
71                                    LOGGING_CONF_LOG_LEVEL,
72                                    &config->logging_loglevel);
73
74         event_conf_init(config->conf);
75
76         conf_assign_string_pointer(config->conf,
77                                    EVENT_CONF_SECTION,
78                                    EVENT_CONF_DEBUG_SCRIPT,
79                                    &config->debug_script);
80
81         ok = conf_valid(config->conf);
82         if (!ok) {
83                 talloc_free(config);
84                 return EINVAL;
85         }
86
87         ret = conf_load(config->conf, config->config_file, true);
88         if (ret != 0 && ret != ENOENT) {
89                 talloc_free(config);
90                 return ret;
91         }
92
93         *result = config;
94         return 0;
95 }
96
97 const char *event_config_log_location(struct event_config *config)
98 {
99         return config->logging_location;
100 }
101
102 const char *event_config_log_level(struct event_config *config)
103 {
104         return config->logging_loglevel;
105 }
106
107 const char *event_config_debug_script(struct event_config *config)
108 {
109         return config->debug_script;
110 }
111
112 int event_config_reload(struct event_config *config)
113 {
114         int ret;
115
116         ret = conf_reload(config->conf);
117         if (ret != 0 && ret != ENOENT) {
118                 return ret;
119         }
120
121         return 0;
122 }