]> code.delx.au - pulseaudio/blob - src/daemon/daemon-conf.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / daemon / daemon-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/strbuf.h>
39 #include <pulsecore/conf-parser.h>
40 #include <pulsecore/resampler.h>
41 #include <pulsecore/macro.h>
42
43 #include "daemon-conf.h"
44
45 #define DEFAULT_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "default.pa"
46 #define DEFAULT_SCRIPT_FILE_USER PA_PATH_SEP "default.pa"
47 #define DEFAULT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "daemon.conf"
48 #define DEFAULT_CONFIG_FILE_USER PA_PATH_SEP "daemon.conf"
49
50 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
51 #define ENV_CONFIG_FILE "PULSE_CONFIG"
52 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
53
54 static const pa_daemon_conf default_conf = {
55 .cmd = PA_CMD_DAEMON,
56 .daemonize = 0,
57 .fail = 1,
58 .high_priority = 0,
59 .disallow_module_loading = 0,
60 .exit_idle_time = -1,
61 .module_idle_time = 20,
62 .scache_idle_time = 20,
63 .auto_log_target = 1,
64 .script_commands = NULL,
65 .dl_search_path = NULL,
66 .default_script_file = NULL,
67 .log_target = PA_LOG_SYSLOG,
68 .log_level = PA_LOG_NOTICE,
69 .resample_method = PA_RESAMPLER_AUTO,
70 .config_file = NULL,
71 .use_pid_file = 1,
72 .system_instance = 0,
73 .no_cpu_limit = 0,
74 .disable_shm = 0,
75 .default_n_fragments = 4,
76 .default_fragment_size_msec = 25,
77 .default_sample_spec = { .format = PA_SAMPLE_S16NE, .rate = 44100, .channels = 2 }
78 #ifdef HAVE_SYS_RESOURCE_H
79 , .rlimit_as = { .value = 0, .is_set = 0 },
80 .rlimit_core = { .value = 0, .is_set = 0 },
81 .rlimit_data = { .value = 0, .is_set = 0 },
82 .rlimit_fsize = { .value = 0, .is_set = 0 },
83 .rlimit_nofile = { .value = 256, .is_set = 1 },
84 .rlimit_stack = { .value = 0, .is_set = 0 }
85 #ifdef RLIMIT_NPROC
86 , .rlimit_nproc = { .value = 0, .is_set = 0 }
87 #endif
88 #ifdef RLIMIT_MEMLOCK
89 , .rlimit_memlock = { .value = 16384, .is_set = 1 }
90 #endif
91 #endif
92 };
93
94 pa_daemon_conf* pa_daemon_conf_new(void) {
95 FILE *f;
96 pa_daemon_conf *c = pa_xnewdup(pa_daemon_conf, &default_conf, 1);
97
98 if ((f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file, "r")))
99 fclose(f);
100
101 c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
102 return c;
103 }
104
105 void pa_daemon_conf_free(pa_daemon_conf *c) {
106 pa_assert(c);
107 pa_xfree(c->script_commands);
108 pa_xfree(c->dl_search_path);
109 pa_xfree(c->default_script_file);
110 pa_xfree(c->config_file);
111 pa_xfree(c);
112 }
113
114 int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
115 pa_assert(c);
116 pa_assert(string);
117
118 if (!strcmp(string, "auto"))
119 c->auto_log_target = 1;
120 else if (!strcmp(string, "syslog")) {
121 c->auto_log_target = 0;
122 c->log_target = PA_LOG_SYSLOG;
123 } else if (!strcmp(string, "stderr")) {
124 c->auto_log_target = 0;
125 c->log_target = PA_LOG_STDERR;
126 } else
127 return -1;
128
129 return 0;
130 }
131
132 int pa_daemon_conf_set_log_level(pa_daemon_conf *c, const char *string) {
133 uint32_t u;
134 pa_assert(c);
135 pa_assert(string);
136
137 if (pa_atou(string, &u) >= 0) {
138 if (u >= PA_LOG_LEVEL_MAX)
139 return -1;
140
141 c->log_level = (pa_log_level_t) u;
142 } else if (pa_startswith(string, "debug"))
143 c->log_level = PA_LOG_DEBUG;
144 else if (pa_startswith(string, "info"))
145 c->log_level = PA_LOG_INFO;
146 else if (pa_startswith(string, "notice"))
147 c->log_level = PA_LOG_NOTICE;
148 else if (pa_startswith(string, "warn"))
149 c->log_level = PA_LOG_WARN;
150 else if (pa_startswith(string, "err"))
151 c->log_level = PA_LOG_ERROR;
152 else
153 return -1;
154
155 return 0;
156 }
157
158 int pa_daemon_conf_set_resample_method(pa_daemon_conf *c, const char *string) {
159 int m;
160 pa_assert(c);
161 pa_assert(string);
162
163 if ((m = pa_parse_resample_method(string)) < 0)
164 return -1;
165
166 c->resample_method = m;
167 return 0;
168 }
169
170 static int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
171 pa_daemon_conf *c = data;
172
173 pa_assert(filename);
174 pa_assert(lvalue);
175 pa_assert(rvalue);
176 pa_assert(data);
177
178 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
179 pa_log("[%s:%u] Invalid log target '%s'.", filename, line, rvalue);
180 return -1;
181 }
182
183 return 0;
184 }
185
186 static int parse_log_level(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
187 pa_daemon_conf *c = data;
188
189 pa_assert(filename);
190 pa_assert(lvalue);
191 pa_assert(rvalue);
192 pa_assert(data);
193
194 if (pa_daemon_conf_set_log_level(c, rvalue) < 0) {
195 pa_log("[%s:%u] Invalid log level '%s'.", filename, line, rvalue);
196 return -1;
197 }
198
199 return 0;
200 }
201
202 static int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
203 pa_daemon_conf *c = data;
204
205 pa_assert(filename);
206 pa_assert(lvalue);
207 pa_assert(rvalue);
208 pa_assert(data);
209
210 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
211 pa_log("[%s:%u] Invalid resample method '%s'.", filename, line, rvalue);
212 return -1;
213 }
214
215 return 0;
216 }
217
218 static int parse_rlimit(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
219 #ifdef HAVE_SYS_RESOURCE_H
220 struct pa_rlimit *r = data;
221
222 pa_assert(filename);
223 pa_assert(lvalue);
224 pa_assert(rvalue);
225 pa_assert(r);
226
227 if (rvalue[strspn(rvalue, "\t ")] == 0) {
228 /* Empty string */
229 r->is_set = 0;
230 r->value = 0;
231 } else {
232 int32_t k;
233 if (pa_atoi(rvalue, &k) < 0) {
234 pa_log("[%s:%u] Invalid rlimit '%s'.", filename, line, rvalue);
235 return -1;
236 }
237 r->is_set = k >= 0;
238 r->value = k >= 0 ? (rlim_t) k : 0;
239 }
240 #else
241 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename, line);
242 #endif
243
244 return 0;
245 }
246
247 static int parse_sample_format(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
248 pa_daemon_conf *c = data;
249 pa_sample_format_t f;
250
251 pa_assert(filename);
252 pa_assert(lvalue);
253 pa_assert(rvalue);
254 pa_assert(data);
255
256 if ((f = pa_parse_sample_format(rvalue)) < 0) {
257 pa_log("[%s:%u] Invalid sample format '%s'.", filename, line, rvalue);
258 return -1;
259 }
260
261 c->default_sample_spec.format = f;
262 return 0;
263 }
264
265 static int parse_sample_rate(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
266 pa_daemon_conf *c = data;
267 int32_t r;
268
269 pa_assert(filename);
270 pa_assert(lvalue);
271 pa_assert(rvalue);
272 pa_assert(data);
273
274 if (pa_atoi(rvalue, &r) < 0 || r > PA_RATE_MAX || r <= 0) {
275 pa_log("[%s:%u] Invalid sample rate '%s'.", filename, line, rvalue);
276 return -1;
277 }
278
279 c->default_sample_spec.rate = r;
280 return 0;
281 }
282
283 static int parse_sample_channels(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
284 pa_daemon_conf *c = data;
285 int32_t n;
286
287 pa_assert(filename);
288 pa_assert(lvalue);
289 pa_assert(rvalue);
290 pa_assert(data);
291
292 if (pa_atoi(rvalue, &n) < 0 || n > PA_CHANNELS_MAX || n <= 0) {
293 pa_log("[%s:%u] Invalid sample channels '%s'.", filename, line, rvalue);
294 return -1;
295 }
296
297 c->default_sample_spec.channels = (uint8_t) n;
298 return 0;
299 }
300
301 static int parse_fragments(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
302 pa_daemon_conf *c = data;
303 int32_t n;
304
305 pa_assert(filename);
306 pa_assert(lvalue);
307 pa_assert(rvalue);
308 pa_assert(data);
309
310 if (pa_atoi(rvalue, &n) < 0 || n < 2) {
311 pa_log("[%s:%u] Invalid number of fragments '%s'.", filename, line, rvalue);
312 return -1;
313 }
314
315 c->default_n_fragments = (unsigned) n;
316 return 0;
317 }
318
319 static int parse_fragment_size_msec(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
320 pa_daemon_conf *c = data;
321 int32_t n;
322
323 pa_assert(filename);
324 pa_assert(lvalue);
325 pa_assert(rvalue);
326 pa_assert(data);
327
328 if (pa_atoi(rvalue, &n) < 0 || n < 1) {
329 pa_log("[%s:%u] Invalid fragment size '%s'.", filename, line, rvalue);
330 return -1;
331 }
332
333 c->default_fragment_size_msec = (unsigned) n;
334 return 0;
335 }
336
337 int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
338 int r = -1;
339 FILE *f = NULL;
340
341 pa_config_item table[] = {
342 { "daemonize", pa_config_parse_bool, NULL },
343 { "fail", pa_config_parse_bool, NULL },
344 { "high-priority", pa_config_parse_bool, NULL },
345 { "disallow-module-loading", pa_config_parse_bool, NULL },
346 { "exit-idle-time", pa_config_parse_int, NULL },
347 { "module-idle-time", pa_config_parse_int, NULL },
348 { "scache-idle-time", pa_config_parse_int, NULL },
349 { "dl-search-path", pa_config_parse_string, NULL },
350 { "default-script-file", pa_config_parse_string, NULL },
351 { "log-target", parse_log_target, NULL },
352 { "log-level", parse_log_level, NULL },
353 { "verbose", parse_log_level, NULL },
354 { "resample-method", parse_resample_method, NULL },
355 { "use-pid-file", pa_config_parse_bool, NULL },
356 { "system-instance", pa_config_parse_bool, NULL },
357 { "no-cpu-limit", pa_config_parse_bool, NULL },
358 { "disable-shm", pa_config_parse_bool, NULL },
359 { "default-sample-format", parse_sample_format, NULL },
360 { "default-sample-rate", parse_sample_rate, NULL },
361 { "default-sample-channels", parse_sample_channels, NULL },
362 { "default-fragments", parse_fragments, NULL },
363 { "default-fragment-size-msec", parse_fragment_size_msec, NULL },
364 #ifdef HAVE_SYS_RESOURCE_H
365 { "rlimit-as", parse_rlimit, NULL },
366 { "rlimit-core", parse_rlimit, NULL },
367 { "rlimit-data", parse_rlimit, NULL },
368 { "rlimit-fsize", parse_rlimit, NULL },
369 { "rlimit-nofile", parse_rlimit, NULL },
370 { "rlimit-stack", parse_rlimit, NULL },
371 #ifdef RLIMIT_NPROC
372 { "rlimit-nproc", parse_rlimit, NULL },
373 #endif
374 #ifdef RLIMIT_MEMLOCK
375 { "rlimit-memlock", parse_rlimit, NULL },
376 #endif
377 #endif
378 { NULL, NULL, NULL },
379 };
380
381 table[0].data = &c->daemonize;
382 table[1].data = &c->fail;
383 table[2].data = &c->high_priority;
384 table[3].data = &c->disallow_module_loading;
385 table[4].data = &c->exit_idle_time;
386 table[5].data = &c->module_idle_time;
387 table[6].data = &c->scache_idle_time;
388 table[7].data = &c->dl_search_path;
389 table[8].data = &c->default_script_file;
390 table[9].data = c;
391 table[10].data = c;
392 table[11].data = c;
393 table[12].data = c;
394 table[13].data = &c->use_pid_file;
395 table[14].data = &c->system_instance;
396 table[15].data = &c->no_cpu_limit;
397 table[16].data = &c->disable_shm;
398 table[17].data = c;
399 table[18].data = c;
400 table[19].data = c;
401 table[20].data = c;
402 table[21].data = c;
403 #ifdef HAVE_SYS_RESOURCE_H
404 table[22].data = &c->rlimit_as;
405 table[23].data = &c->rlimit_core;
406 table[24].data = &c->rlimit_data;
407 table[25].data = &c->rlimit_fsize;
408 table[26].data = &c->rlimit_nofile;
409 table[27].data = &c->rlimit_stack;
410 #ifdef RLIMIT_NPROC
411 table[28].data = &c->rlimit_nproc;
412 #endif
413 #ifdef RLIMIT_MEMLOCK
414 #ifndef RLIMIT_NPROC
415 #error "Houston, we have a numbering problem!"
416 #endif
417 table[29].data = &c->rlimit_memlock;
418 #endif
419 #endif
420
421 pa_xfree(c->config_file);
422 c->config_file = NULL;
423
424 f = filename ?
425 fopen(c->config_file = pa_xstrdup(filename), "r") :
426 pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file, "r");
427
428 if (!f && errno != ENOENT) {
429 pa_log_warn("Failed to open configuration file '%s': %s", c->config_file, pa_cstrerror(errno));
430 goto finish;
431 }
432
433 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
434
435 finish:
436 if (f)
437 fclose(f);
438
439 return r;
440 }
441
442 int pa_daemon_conf_env(pa_daemon_conf *c) {
443 char *e;
444
445 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
446 pa_xfree(c->dl_search_path);
447 c->dl_search_path = pa_xstrdup(e);
448 }
449 if ((e = getenv(ENV_SCRIPT_FILE))) {
450 pa_xfree(c->default_script_file);
451 c->default_script_file = pa_xstrdup(e);
452 }
453
454 return 0;
455 }
456
457 static const char* const log_level_to_string[] = {
458 [PA_LOG_DEBUG] = "debug",
459 [PA_LOG_INFO] = "info",
460 [PA_LOG_NOTICE] = "notice",
461 [PA_LOG_WARN] = "warning",
462 [PA_LOG_ERROR] = "error"
463 };
464
465 char *pa_daemon_conf_dump(pa_daemon_conf *c) {
466 pa_strbuf *s;
467
468 pa_assert(c);
469
470 s = pa_strbuf_new();
471
472 if (c->config_file)
473 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
474
475 pa_assert(c->log_level <= PA_LOG_LEVEL_MAX);
476
477 pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
478 pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
479 pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
480 pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
481 pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
482 pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
483 pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
484 pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
485 pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
486 pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
487 pa_strbuf_printf(s, "log-level = %s\n", log_level_to_string[c->log_level]);
488 pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
489 pa_strbuf_printf(s, "use-pid-file = %i\n", c->use_pid_file);
490 pa_strbuf_printf(s, "system-instance = %i\n", !!c->system_instance);
491 pa_strbuf_printf(s, "no-cpu-limit = %i\n", !!c->no_cpu_limit);
492 pa_strbuf_printf(s, "disable-shm = %i\n", !!c->disable_shm);
493 pa_strbuf_printf(s, "default-sample-format = %s\n", pa_sample_format_to_string(c->default_sample_spec.format));
494 pa_strbuf_printf(s, "default-sample-rate = %u\n", c->default_sample_spec.rate);
495 pa_strbuf_printf(s, "default-sample-channels = %u\n", c->default_sample_spec.channels);
496 pa_strbuf_printf(s, "default-fragments = %u\n", c->default_n_fragments);
497 pa_strbuf_printf(s, "default-fragment-size-msec = %u\n", c->default_fragment_size_msec);
498 #ifdef HAVE_SYS_RESOURCE_H
499 pa_strbuf_printf(s, "rlimit-as = %li\n", c->rlimit_as.is_set ? (long int) c->rlimit_as.value : -1);
500 pa_strbuf_printf(s, "rlimit-core = %li\n", c->rlimit_core.is_set ? (long int) c->rlimit_core.value : -1);
501 pa_strbuf_printf(s, "rlimit-data = %li\n", c->rlimit_data.is_set ? (long int) c->rlimit_data.value : -1);
502 pa_strbuf_printf(s, "rlimit-fsize = %li\n", c->rlimit_fsize.is_set ? (long int) c->rlimit_fsize.value : -1);
503 pa_strbuf_printf(s, "rlimit-nofile = %li\n", c->rlimit_nofile.is_set ? (long int) c->rlimit_nofile.value : -1);
504 pa_strbuf_printf(s, "rlimit-stack = %li\n", c->rlimit_stack.is_set ? (long int) c->rlimit_stack.value : -1);
505 #ifdef RLIMIT_NPROC
506 pa_strbuf_printf(s, "rlimit-nproc = %li\n", c->rlimit_nproc.is_set ? (long int) c->rlimit_nproc.value : -1);
507 #endif
508 #ifdef RLIMIT_MEMLOCK
509 pa_strbuf_printf(s, "rlimit-memlock = %li\n", c->rlimit_memlock.is_set ? (long int) c->rlimit_memlock.value : -1);
510 #endif
511 #endif
512
513 return pa_strbuf_tostring_free(s);
514 }