Debian Codegraph @

src/cfg.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2017 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include "compat.h"
22
23 #include <sys/stat.h>
24
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "cfg_private.h"
30 #include "cfgfile_xml.h"
31 #include "log.h"
32 #include "xalloc.h"
33
34 static struct cfg_program cfg_program;
35 static struct cfg cfg;
36 static struct cfg cfg_tmp;
37
38 static void _cfg_reset(struct cfg *);
39 static void _cfg_switch(struct cfg *, struct cfg *);
40 static int _cfg_load(void);
41 static void _cfg_save(void);
42 static void _cfg_restore(void);
43 static void _cfg_commit(void);
44
45 static void
46 _cfg_reset(struct cfg *c)
47 {
48 cfg_stream_list_destroy(&c->streams);
49 cfg_server_list_destroy(&c->servers);
50 cfg_intake_list_destroy(&c->intakes);
51 cfg_encoder_list_destroy(&c->encoders);
52 cfg_decoder_list_destroy(&c->decoders);
53
54 xfree(c->metadata.format_str);
55
56 memset(c, 0, sizeof(*c));
57
58 c->metadata.refresh_interval = -1;
59 }
60
61 static void
62 _cfg_switch(struct cfg *a, struct cfg *b)
63 {
64 struct cfg tmp;
65
66 memcpy(&tmp, a, sizeof(tmp));
67 memcpy(a, b, sizeof(*a));
68 memcpy(b, &tmp, sizeof(*b));
69 }
70
71 static int
72 _cfg_load(void)
73 {
74 switch (cfg_program.config_type) {
75 case CFG_TYPE_XMLFILE:
76 if (0 > cfgfile_xml_parse(cfg_program.config_file))
77 return (-1);
78 break;
79 default:
80 log_alert("unsupported config type %u",
81 cfg_program.config_type);
82 abort();
83 }
84 return (0);
85 }
86
87 static void
88 _cfg_save(void)
89 {
90 _cfg_reset(&cfg_tmp);
91 _cfg_switch(&cfg_tmp, &cfg);
92 _cfg_reset(&cfg);
93 }
94
95 static void
96 _cfg_restore(void)
97 {
98 _cfg_reset(&cfg);
99 _cfg_switch(&cfg, &cfg_tmp);
100 _cfg_reset(&cfg_tmp);
101 }
102
103 static void
104 _cfg_commit(void)
105 {
106 _cfg_reset(&cfg_tmp);
107 }
108
109 int
110 cfg_init(void)
111 {
112 _cfg_reset(&cfg);
113 _cfg_commit();
114 return (0);
115 }
116
117 void
118 cfg_exit(void)
119 {
120 (void)cfg_init();
121 }
122
123 int
124 cfg_file_reload(void)
125 {
126 _cfg_save();
127 if (0 > _cfg_load()) {
128 _cfg_restore();
129 return (-1);
130 }
131 _cfg_commit();
132
133 return (0);
134 }
135
136 int
137 cfg_check(const char **not_used)
138 {
139 (void)not_used;
140
141 return (0);
142 }
143
144 int
145 cfg_file_check(const char *file)
146 {
147 struct stat st;
148
149 if (0 > stat(file, &st)) {
150 log_error("%s: %s", file, strerror(errno));
151 return (-1);
152 }
153
154 if (st.st_mode & S_IROTH)
155 log_warning("%s: world readable", file);
156 else if (st.st_mode & S_IRGRP)
157 log_notice("%s: group readable", file);
158
159 if (st.st_mode & S_IWOTH) {
160 log_error("%s: world writeable", file);
161 return (-1);
162 }
163
164 return (0);
165 }
166
167 cfg_decoder_list_t
168 cfg_get_decoders(void)
169 {
170 if (!cfg.decoders)
171 cfg.decoders = cfg_decoder_list_create();
172
173 return (cfg.decoders);
174 }
175
176 cfg_encoder_list_t
177 cfg_get_encoders(void)
178 {
179 if (!cfg.encoders)
180 cfg.encoders = cfg_encoder_list_create();
181
182 return (cfg.encoders);
183 }
184
185 cfg_intake_list_t
186 cfg_get_intakes(void)
187 {
188 if (!cfg.intakes)
189 cfg.intakes = cfg_intake_list_create();
190
191 return (cfg.intakes);
192 }
193
194 cfg_server_list_t
195 cfg_get_servers(void)
196 {
197 if (!cfg.servers)
198 cfg.servers = cfg_server_list_create();
199
200 return (cfg.servers);
201 }
202
203 cfg_stream_list_t
204 cfg_get_streams(void)
205 {
206 if (!cfg.streams)
207 cfg.streams = cfg_stream_list_create();
208
209 return (cfg.streams);
210 }
211
212 int
213 cfg_set_program_name(const char *progname, const char **errstrp)
214 {
215 SET_STRLCPY(cfg_program.name, progname, errstrp);
216 return (0);
217 }
218
219 int
220 cfg_set_program_config_type(enum cfg_config_type type, const char **errstrp)
221 {
222 if (CFG_TYPE_MIN > type || CFG_TYPE_MAX < type) {
223 if (NULL != errstrp)
224 *errstrp = "invalid";
225 return (-1);
226 }
227 cfg_program.config_type = type;
228 return (0);
229 }
230
231 int
232 cfg_set_program_config_file(const char *file, const char **errstrp)
233 {
234 SET_STRLCPY(cfg_program.config_file, file, errstrp);
235 return (0);
236 }
237
238 int
239 cfg_set_program_pid_file(const char *file, const char **errstrp)
240 {
241 SET_STRLCPY(cfg_program.pid_file, file, errstrp);
242 return (0);
243 }
244
245 int
246 cfg_set_program_quiet_stderr(int quiet_stderr, const char **not_used)
247 {
248 (void)not_used;
249 cfg_program.quiet_stderr = quiet_stderr ? 1 : 0;
250 return (0);
251 }
252
253 int
254 cfg_set_program_rtstatus_output(int rtstatus_output, const char **not_used)
255 {
256 (void)not_used;
257 cfg_program.rtstatus_output = rtstatus_output ? 1 : 0;
258 return (0);
259 }
260
261 int
262 cfg_set_program_verbosity(unsigned int verbosity, const char **not_used)
263 {
264 (void)not_used;
265 cfg_program.verbosity = verbosity;
266 return (0);
267 }
268
269 int
270 cfg_set_metadata_program(const char *program, const char **errstrp)
271 {
272 SET_STRLCPY(cfg.metadata.program, program, errstrp);
273 return (0);
274 }
275
276 int
277 cfg_set_metadata_format_str(const char *format_str, const char **errstrp)
278 {
279 if (!format_str || !format_str[0]) {
280 if (errstrp)
281 *errstrp = "empty";
282 return (-1);
283 }
284
285 CHECKPH_PROHIBITED(format_str, PLACEHOLDER_METADATA);
286 CHECKPH_DUPLICATE(format_str, PLACEHOLDER_TRACK);
287 CHECKPH_DUPLICATE(format_str, PLACEHOLDER_STRING);
288 CHECKPH_DUPLICATE(format_str, PLACEHOLDER_ARTIST);
289 CHECKPH_DUPLICATE(format_str, PLACEHOLDER_TITLE);
290
291 if (cfg.metadata.format_str)
292 xfree(cfg.metadata.format_str);
293 cfg.metadata.format_str = xstrdup(format_str);
294
295 return (0);
296 }
297
298 int
299 cfg_set_metadata_refresh_interval(const char *num_str, const char **errstrp)
300 {
301 SET_INTNUM(cfg.metadata.refresh_interval, num_str, errstrp);
302 return (0);
303 }
304
305 int
306 cfg_set_metadata_normalize_strings(const char *normalize_strings,
307 const char **errstrp)
308 {
309 SET_BOOLEAN(cfg.metadata.normalize_strings, normalize_strings,
310 errstrp);
311 return (0);
312 }
313
314 int
315 cfg_set_metadata_no_updates(const char *no_updates, const char **errstrp)
316 {
317 SET_BOOLEAN(cfg.metadata.no_updates, no_updates,
318 errstrp);
319 return (0);
320 }
321
322 const char *
323 cfg_get_program_name(void)
324 {
325 return (cfg_program.name[0] ? cfg_program.name : NULL);
326 }
327
328 enum cfg_config_type
329 cfg_get_program_config_type(void)
330 {
331 return (cfg_program.config_type);
332 }
333
334 const char *
335 cfg_get_program_config_file(void)
336 {
337 return (cfg_program.config_file[0] ? cfg_program.config_file : NULL);
338 }
339
340 const char *
341 cfg_get_program_pid_file(void)
342 {
343 return (cfg_program.pid_file[0] ? cfg_program.pid_file : NULL);
344 }
345
346 int
347 cfg_get_program_quiet_stderr(void)
348 {
349 return (cfg_program.quiet_stderr);
350 }
351
352 int
353 cfg_get_program_rtstatus_output(void)
354 {
355 return (cfg_program.rtstatus_output);
356 }
357
358 unsigned int
359 cfg_get_program_verbosity(void)
360 {
361 return (cfg_program.verbosity);
362 }
363
364 const char *
365 cfg_get_metadata_program(void)
366 {
367 return (cfg.metadata.program[0] ? cfg.metadata.program : NULL);
368 }
369
370 const char *
371 cfg_get_metadata_format_str(void)
372 {
373 return (cfg.metadata.format_str);
374 }
375
376 int
377 cfg_get_metadata_refresh_interval(void)
378 {
379 return (cfg.metadata.refresh_interval);
380 }
381
382 int
383 cfg_get_metadata_normalize_strings(void)
384 {
385 return (cfg.metadata.normalize_strings);
386 }
387
388 int
389 cfg_get_metadata_no_updates(void)
390 {
391 return (cfg.metadata.no_updates);
392 }