4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
34 #include <pulse/xmalloc.h>
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>
43 #include "daemon-conf.h"
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"
50 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
51 #define ENV_CONFIG_FILE "PULSE_CONFIG"
52 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
54 static const pa_daemon_conf default_conf
= {
59 .disallow_module_loading
= 0,
61 .module_idle_time
= 20,
62 .scache_idle_time
= 20,
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
,
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 }
86 , .rlimit_nproc
= { .value
= 0, .is_set
= 0 }
89 , .rlimit_memlock
= { .value
= 16384, .is_set
= 1 }
94 pa_daemon_conf
* pa_daemon_conf_new(void) {
96 pa_daemon_conf
*c
= pa_xnewdup(pa_daemon_conf
, &default_conf
, 1);
98 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
, "r")))
101 c
->dl_search_path
= pa_xstrdup(PA_DLSEARCHPATH
);
105 void pa_daemon_conf_free(pa_daemon_conf
*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
);
114 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
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
;
132 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
137 if (pa_atou(string
, &u
) >= 0) {
138 if (u
>= PA_LOG_LEVEL_MAX
)
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
;
158 int pa_daemon_conf_set_resample_method(pa_daemon_conf
*c
, const char *string
) {
163 if ((m
= pa_parse_resample_method(string
)) < 0)
166 c
->resample_method
= m
;
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
;
178 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
179 pa_log("[%s:%u] Invalid log target '%s'.", filename
, line
, rvalue
);
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
;
194 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
195 pa_log("[%s:%u] Invalid log level '%s'.", filename
, line
, rvalue
);
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
;
210 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
211 pa_log("[%s:%u] Invalid resample method '%s'.", filename
, line
, rvalue
);
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
;
227 if (rvalue
[strspn(rvalue
, "\t ")] == 0) {
233 if (pa_atoi(rvalue
, &k
) < 0) {
234 pa_log("[%s:%u] Invalid rlimit '%s'.", filename
, line
, rvalue
);
238 r
->value
= k
>= 0 ? (rlim_t
) k
: 0;
241 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename
, line
);
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
;
256 if ((f
= pa_parse_sample_format(rvalue
)) < 0) {
257 pa_log("[%s:%u] Invalid sample format '%s'.", filename
, line
, rvalue
);
261 c
->default_sample_spec
.format
= f
;
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
;
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
);
279 c
->default_sample_spec
.rate
= r
;
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
;
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
);
297 c
->default_sample_spec
.channels
= (uint8_t) n
;
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
;
310 if (pa_atoi(rvalue
, &n
) < 0 || n
< 2) {
311 pa_log("[%s:%u] Invalid number of fragments '%s'.", filename
, line
, rvalue
);
315 c
->default_n_fragments
= (unsigned) n
;
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
;
328 if (pa_atoi(rvalue
, &n
) < 0 || n
< 1) {
329 pa_log("[%s:%u] Invalid fragment size '%s'.", filename
, line
, rvalue
);
333 c
->default_fragment_size_msec
= (unsigned) n
;
337 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
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
},
372 { "rlimit-nproc", parse_rlimit
, NULL
},
374 #ifdef RLIMIT_MEMLOCK
375 { "rlimit-memlock", parse_rlimit
, NULL
},
378 { NULL
, NULL
, NULL
},
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
;
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
;
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
;
411 table
[28].data
= &c
->rlimit_nproc
;
413 #ifdef RLIMIT_MEMLOCK
415 #error "Houston, we have a numbering problem!"
417 table
[29].data
= &c
->rlimit_memlock
;
421 pa_xfree(c
->config_file
);
422 c
->config_file
= NULL
;
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");
428 if (!f
&& errno
!= ENOENT
) {
429 pa_log_warn("Failed to open configuration file '%s': %s", c
->config_file
, pa_cstrerror(errno
));
433 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
442 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
445 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
446 pa_xfree(c
->dl_search_path
);
447 c
->dl_search_path
= pa_xstrdup(e
);
449 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
450 pa_xfree(c
->default_script_file
);
451 c
->default_script_file
= pa_xstrdup(e
);
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"
465 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
473 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
475 pa_assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
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);
506 pa_strbuf_printf(s
, "rlimit-nproc = %li\n", c
->rlimit_nproc
.is_set
? (long int) c
->rlimit_nproc
.value
: -1);
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);
513 return pa_strbuf_tostring_free(s
);