s4:torture: Fix memory leak
[Samba.git] / ctdb / server / ctdb_tunables.c
blob0dce656a93cc3c70f54652bdc9d54bae914f9ff0
1 /*
2 ctdb tunables code
4 Copyright (C) Andrew Tridgell 2007
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.
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.
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/>.
19 #include "replace.h"
20 #include "system/network.h"
22 #include <talloc.h>
23 #include <tdb.h>
25 #include "lib/util/debug.h"
27 #include "ctdb_private.h"
29 #include "common/common.h"
30 #include "common/logging.h"
31 #include "common/path.h"
32 #include "common/tunable.h"
35 set all tunables to defaults
37 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
39 ctdb_tunable_set_defaults(&ctdb->tunable);
44 get a tunable
46 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata,
47 TDB_DATA *outdata)
49 struct ctdb_control_get_tunable *t =
50 (struct ctdb_control_get_tunable *)indata.dptr;
51 char *name;
52 uint32_t val;
53 bool ret;
55 if (indata.dsize < sizeof(*t) ||
56 t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
57 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_get_tunable\n"));
58 return -1;
61 name = talloc_strndup(ctdb, (char*)t->name, t->length);
62 CTDB_NO_MEMORY(ctdb, name);
64 ret = ctdb_tunable_get_value(&ctdb->tunable, name, &val);
65 talloc_free(name);
66 if (! ret) {
67 return -EINVAL;
70 outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
71 CTDB_NO_MEMORY(ctdb, outdata->dptr);
73 *(uint32_t *)outdata->dptr = val;
74 outdata->dsize = sizeof(uint32_t);
76 return 0;
81 set a tunable
83 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
85 struct ctdb_tunable_old *t =
86 (struct ctdb_tunable_old *)indata.dptr;
87 char *name;
88 int ret;
89 bool obsolete;
91 if (indata.dsize < sizeof(*t) ||
92 t->length > indata.dsize - offsetof(struct ctdb_tunable_old, name)) {
93 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tunable\n"));
94 return -1;
97 name = talloc_strndup(ctdb, (char *)t->name, t->length);
98 CTDB_NO_MEMORY(ctdb, name);
100 ret = ctdb_tunable_set_value(&ctdb->tunable, name, t->value,
101 &obsolete);
102 if (! ret) {
103 talloc_free(name);
104 return -1;
107 if (obsolete) {
108 DEBUG(DEBUG_WARNING,
109 ("Setting obsolete tunable \"%s\"\n", name));
110 talloc_free(name);
111 return 1;
114 talloc_free(name);
115 return 0;
119 list tunables
121 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
123 char *list = NULL;
124 struct ctdb_control_list_tunable *t;
126 list = ctdb_tunable_names_to_string(outdata);
127 CTDB_NO_MEMORY(ctdb, list);
129 outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) +
130 strlen(list) + 1;
131 outdata->dptr = talloc_size(outdata, outdata->dsize);
132 CTDB_NO_MEMORY(ctdb, outdata->dptr);
134 t = (struct ctdb_control_list_tunable *)outdata->dptr;
135 t->length = strlen(list)+1;
137 memcpy(t->data, list, t->length);
138 talloc_free(list);
140 return 0;
143 bool ctdb_tunables_load(struct ctdb_context *ctdb)
145 bool status;
146 TALLOC_CTX *tmp_ctx;
147 char *file = NULL;
149 /* Fail by default */
150 status = false;
152 tmp_ctx = talloc_new(ctdb);
153 if (tmp_ctx == NULL) {
154 DBG_ERR("Memory allocation error\n");
155 goto done;
158 file = path_etcdir_append(tmp_ctx, "ctdb.tunables");
159 if (file == NULL) {
160 D_ERR("Failed to construct path for ctdb.tunables\n");
161 goto done;
164 status = ctdb_tunable_load_file(tmp_ctx, &ctdb->tunable, file);
165 /* No need to log error, already logged above */
167 done:
168 talloc_free(tmp_ctx);
169 return status;