Fix bug #6224 - nmbd waits 5 minutes at startup before checking if it needs to run...
[Samba.git] / source4 / param / generic.c
blobba5464a0f0aabf8166887073e17366630ec64126
1 /*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "../lib/util/dlinklist.h"
21 #include "param/param.h"
22 #include "param/loadparm.h"
23 #include "system/filesys.h"
25 struct param_section *param_get_section(struct param_context *ctx, const char *name)
27 struct param_section *sect;
29 if (name == NULL)
30 name = GLOBAL_NAME;
32 for (sect = ctx->sections; sect; sect = sect->next) {
33 if (!strcasecmp_m(sect->name, name))
34 return sect;
37 return NULL;
40 struct param_opt *param_section_get(struct param_section *section,
41 const char *name)
43 struct param_opt *p;
45 for (p = section->parameters; p; p = p->next) {
46 if (strcasecmp_m(p->key, name) == 0)
47 return p;
50 return NULL;
53 struct param_opt *param_get (struct param_context *ctx, const char *name, const char *section_name)
55 struct param_section *section = param_get_section(ctx, section_name);
56 if (section == NULL)
57 return NULL;
59 return param_section_get(section, name);
62 struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
64 struct param_section *section;
65 section = talloc_zero(ctx, struct param_section);
66 if (section == NULL)
67 return NULL;
69 section->name = talloc_strdup(section, section_name);
70 DLIST_ADD_END(ctx->sections, section, struct param_section *);
71 return section;
74 /* Look up parameter. If it is not found, add it */
75 struct param_opt *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
77 struct param_section *section;
78 struct param_opt *p;
80 SMB_ASSERT(section_name != NULL);
81 SMB_ASSERT(name != NULL);
83 section = param_get_section(ctx, section_name);
85 if (section == NULL) {
86 section = param_add_section(ctx, section_name);
89 p = param_section_get(section, name);
90 if (p == NULL) {
91 p = talloc_zero(section, struct param_opt);
92 if (p == NULL)
93 return NULL;
95 p->key = talloc_strdup(p, name);
96 DLIST_ADD_END(section->parameters, p, struct param_opt *);
99 return p;
102 const char *param_get_string(struct param_context *ctx, const char *param, const char *section)
104 struct param_opt *p = param_get(ctx, param, section);
106 if (p == NULL)
107 return NULL;
109 return p->value;
112 int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section)
114 struct param_opt *p = param_get_add(ctx, param, section);
116 if (p == NULL)
117 return -1;
119 p->value = talloc_strdup(p, value);
121 return 0;
124 const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section)
126 struct param_opt *p = param_get(ctx, param, section);
128 if (p == NULL)
129 return NULL;
131 return (const char **)str_list_make(ctx, p->value, separator);
134 int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
136 struct param_opt *p = param_get_add(ctx, param, section);
138 p->value = str_list_join(p, list, ' ');
140 return 0;
143 int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section)
145 const char *value = param_get_string(ctx, param, section);
147 if (value)
148 return strtol(value, NULL, 0);
150 return default_v;
153 void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
155 struct param_opt *p = param_get_add(ctx, section, param);
157 if (!p)
158 return;
160 p->value = talloc_asprintf(p, "%d", value);
163 unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
165 const char *value = param_get_string(ctx, param, section);
167 if (value)
168 return strtoul(value, NULL, 0);
170 return default_v;
173 void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
175 struct param_opt *p = param_get_add(ctx, name, section);
177 if (!p)
178 return;
180 p->value = talloc_asprintf(p, "%lu", value);
183 static bool param_sfunc (const char *name, void *_ctx)
185 struct param_context *ctx = (struct param_context *)_ctx;
186 struct param_section *section = param_get_section(ctx, name);
188 if (section == NULL) {
189 section = talloc_zero(ctx, struct param_section);
190 if (section == NULL)
191 return false;
193 section->name = talloc_strdup(section, name);
195 DLIST_ADD_END(ctx->sections, section, struct param_section *);
198 /* Make sure this section is on top of the list for param_pfunc */
199 DLIST_PROMOTE(ctx->sections, section);
201 return true;
204 static bool param_pfunc (const char *name, const char *value, void *_ctx)
206 struct param_context *ctx = (struct param_context *)_ctx;
207 struct param_opt *p = param_section_get(ctx->sections, name);
209 if (!p) {
210 p = talloc_zero(ctx->sections, struct param_opt);
211 if (p == NULL)
212 return false;
214 p->key = talloc_strdup(p, name);
215 p->value = talloc_strdup(p, value);
216 DLIST_ADD(ctx->sections->parameters, p);
217 } else { /* Replace current value */
218 talloc_free(p->value);
219 p->value = talloc_strdup(p, value);
222 return true;
225 struct param_context *param_init(TALLOC_CTX *mem_ctx)
227 return talloc_zero(mem_ctx, struct param_context);
231 int param_read(struct param_context *ctx, const char *fn)
233 ctx->sections = talloc_zero(ctx, struct param_section);
234 if (ctx->sections == NULL)
235 return -1;
237 ctx->sections->name = talloc_strdup(ctx->sections, "global");
238 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
239 return -1;
242 return 0;
245 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
247 struct param_section *section;
249 for (section = ctx->sections; section; section = section->next) {
250 struct param_opt *param;
251 bool isglobal = strcmp(section->name, "global") == 0;
252 for (param = section->parameters; param; param = param->next) {
253 if (isglobal)
254 lp_do_global_parameter(lp_ctx, param->key,
255 param->value);
256 else {
257 struct loadparm_service *service =
258 lp_service(lp_ctx, section->name);
259 if (service == NULL)
260 service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
261 lp_do_service_parameter(lp_ctx, service, param->key, param->value);
265 return 0;
268 int param_write(struct param_context *ctx, const char *fn)
270 int file;
271 struct param_section *section;
273 if (fn == NULL || ctx == NULL)
274 return -1;
276 file = open(fn, O_WRONLY|O_CREAT, 0755);
278 if (file == -1)
279 return -1;
281 for (section = ctx->sections; section; section = section->next) {
282 struct param_opt *param;
284 fdprintf(file, "[%s]\n", section->name);
285 for (param = section->parameters; param; param = param->next) {
286 fdprintf(file, "\t%s = %s\n", param->key, param->value);
288 fdprintf(file, "\n");
291 close(file);
293 return 0;