ctdb-common: Fix the TCP packet length check
[vlendec/samba-autobuild/.git] / ctdb / common / logging_conf.c
1 /*
2    CTDB logging config handling
3
4    Copyright (C) Martin Schwenke  2017
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.h"
26 #include "common/logging_conf.h"
27
28 #define LOGGING_LOCATION_DEFAULT        "file:" LOGDIR "/log.ctdb"
29 #define LOGGING_LOG_LEVEL_DEFAULT       "ERROR"
30
31 static bool logging_conf_validate_log_level(const char *key,
32                                             const char *old_loglevel,
33                                             const char *new_loglevel,
34                                             enum conf_update_mode mode)
35 {
36         int log_level;
37         bool ok;
38
39         ok = debug_level_parse(new_loglevel, &log_level);
40         if (!ok) {
41                 return false;
42         }
43
44         return true;
45 }
46
47 static bool logging_conf_validate_location(const char *key,
48                                            const char *old_location,
49                                            const char *new_location,
50                                            enum conf_update_mode mode)
51 {
52         bool ok;
53
54         ok = logging_validate(new_location);
55         if (!ok) {
56                 return false;
57         }
58
59         if (mode == CONF_MODE_RELOAD &&
60             strcmp(old_location, new_location) != 0) {
61                 D_WARNING("Ignoring update of %s config option \"%s\"\n",
62                           LOGGING_CONF_SECTION, key);
63                 return false;
64         }
65
66         return true;
67 }
68
69 void logging_conf_init(struct conf_context *conf,
70                        const char *default_log_level)
71 {
72         const char *log_level;
73
74         log_level = (default_log_level == NULL) ?
75                         LOGGING_LOG_LEVEL_DEFAULT :
76                         default_log_level;
77
78         conf_define_section(conf, LOGGING_CONF_SECTION, NULL);
79
80         conf_define_string(conf,
81                            LOGGING_CONF_SECTION,
82                            LOGGING_CONF_LOCATION,
83                            LOGGING_LOCATION_DEFAULT,
84                            logging_conf_validate_location);
85
86         conf_define_string(conf,
87                            LOGGING_CONF_SECTION,
88                            LOGGING_CONF_LOG_LEVEL,
89                            log_level,
90                            logging_conf_validate_log_level);
91 }
92
93 const char *logging_conf_location(struct conf_context *conf)
94 {
95         const char *out = NULL;
96         int ret;
97
98         ret = conf_get_string(conf,
99                               LOGGING_CONF_SECTION,
100                               LOGGING_CONF_LOCATION,
101                               &out,
102                               NULL);
103         if (ret != 0) {
104                 /* Can't really happen, but return default */
105                 return LOGGING_LOCATION_DEFAULT;
106         }
107
108         return out;
109 }
110
111 const char *logging_conf_log_level(struct conf_context *conf)
112 {
113         const char *out = NULL;
114         int ret;
115
116         ret = conf_get_string(conf,
117                               LOGGING_CONF_SECTION,
118                               LOGGING_CONF_LOG_LEVEL,
119                               &out,
120                               NULL);
121         if (ret != 0) {
122                 /* Can't really happen, but return default */
123                 return LOGGING_LOG_LEVEL_DEFAULT;
124         }
125
126         return out;
127 }