summaryrefslogtreecommitdiffstats
path: root/ctdb/database/database_conf.c
blob: 4c7cb2d9ffe55f8cf0f378e3e92e0a34e61dd6f2 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
   CTDB database config handling

   Copyright (C) Martin Schwenke  2018

   database_conf_validate_lock_debug_script() based on
   event_conf_validatye_debug_script():

     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 "system/filesys.h"
#include "system/dir.h"

#include "lib/util/debug.h"
#include "lib/util/samba_util.h"

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

#include "database_conf.h"

#define DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT   CTDB_VARDIR "/volatile"
#define DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT CTDB_VARDIR "/persistent"
#define DATABASE_CONF_STATE_DB_DIR_DEFAULT      CTDB_VARDIR "/state"

static bool check_static_string_change(const char *key,
				       const char *old_value,
				       const char *new_value,
				       enum conf_update_mode mode)
{
	if (mode == CONF_MODE_RELOAD) {
		if (strcmp(old_value, new_value) != 0) {
			D_WARNING("Ignoring update of [%s] -> %s\n",
				  DATABASE_CONF_SECTION,
				  key);
		}
	}

	return true;
}

static bool check_static_boolean_change(const char *key,
					bool old_value,
					bool new_value,
					enum conf_update_mode mode)
{
	if (mode == CONF_MODE_RELOAD || CONF_MODE_API) {
		if (old_value != new_value) {
			D_WARNING("Ignoring update of [%s] -> %s\n",
				  DATABASE_CONF_SECTION,
				  key);
		}
	}

	return true;
}

static bool database_conf_validate_lock_debug_script(const char *key,
						     const char *old_script,
						     const char *new_script,
						     enum conf_update_mode mode)
{
	char script[PATH_MAX];
	char script_path[PATH_MAX];
	struct stat st;
	size_t len;
	int ret;

	if (new_script == NULL) {
		return true;
	}

	len = strlcpy(script, new_script, sizeof(script));
	if (len >= sizeof(script)) {
		D_ERR("lock debug script name too long\n");
		return false;
	}

	ret = snprintf(script_path,
		       sizeof(script_path),
		       "%s/%s",
		       path_etcdir(),
		       basename(script));
	if (ret < 0 || (size_t)ret >= sizeof(script_path)) {
		D_ERR("lock debug script path too long\n");
		return false;
	}

	ret = stat(script_path, &st);
	if (ret == -1) {
		D_ERR("lock debug script %s does not exist\n", script_path);
		return false;
	}

	if (! S_ISREG(st.st_mode)) {
		D_ERR("lock debug script %s is not a file\n", script_path);
		return false;
	}
	if (! (st.st_mode & S_IXUSR)) {
		D_ERR("lock debug script %s is not executable\n", script_path);
		return false;
	}

	return true;
}

static bool database_conf_validate_db_dir(const char *key,
					  const char *old_dir,
					  const char *new_dir,
					  enum conf_update_mode mode)
{
	if (! directory_exist(new_dir)) {
		D_ERR("%s \"%s\" does not exist\n", key, new_dir);
		return false;
	}

	/* This sometimes warns but always returns true */
	return check_static_string_change(key, old_dir, new_dir, mode);
}

void database_conf_init(struct conf_context *conf)
{
	conf_define_section(conf, DATABASE_CONF_SECTION, NULL);

	conf_define_string(conf,
			   DATABASE_CONF_SECTION,
			   DATABASE_CONF_VOLATILE_DB_DIR,
			   DATABASE_CONF_VOLATILE_DB_DIR_DEFAULT,
			   database_conf_validate_db_dir);
	conf_define_string(conf,
			   DATABASE_CONF_SECTION,
			   DATABASE_CONF_PERSISTENT_DB_DIR,
			   DATABASE_CONF_PERSISTENT_DB_DIR_DEFAULT,
			   database_conf_validate_db_dir);
	conf_define_string(conf,
			   DATABASE_CONF_SECTION,
			   DATABASE_CONF_STATE_DB_DIR,
			   DATABASE_CONF_STATE_DB_DIR_DEFAULT,
			   database_conf_validate_db_dir);
	conf_define_string(conf,
			   DATABASE_CONF_SECTION,
			   DATABASE_CONF_LOCK_DEBUG_SCRIPT,
			   NULL,
			   database_conf_validate_lock_debug_script);
	conf_define_boolean(conf,
			    DATABASE_CONF_SECTION,
			    DATABASE_CONF_TDB_MUTEXES,
			    true,
			    check_static_boolean_change);
}