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
35 #include <pulse/xmalloc.h>
36 #include <pulse/timeval.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/conf-parser.h>
42 #include <pulsecore/resampler.h>
43 #include <pulsecore/macro.h>
45 #include "daemon-conf.h"
47 #define DEFAULT_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "default.pa"
48 #define DEFAULT_SCRIPT_FILE_USER PA_PATH_SEP "default.pa"
49 #define DEFAULT_SYSTEM_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "system.pa"
51 #define DEFAULT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "daemon.conf"
52 #define DEFAULT_CONFIG_FILE_USER PA_PATH_SEP "daemon.conf"
54 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
55 #define ENV_CONFIG_FILE "PULSE_CONFIG"
56 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
58 static const pa_daemon_conf default_conf = {
62 .high_priority = TRUE,
64 .realtime_scheduling = FALSE,
65 .realtime_priority = 5, /* Half of JACK's default rtprio */
66 .disallow_module_loading = FALSE,
68 .module_idle_time = 20,
69 .scache_idle_time = 20,
71 .script_commands = NULL,
72 .dl_search_path = NULL,
73 .load_default_script_file = TRUE,
74 .default_script_file = NULL,
75 .log_target = PA_LOG_SYSLOG,
76 .log_level = PA_LOG_NOTICE,
77 .resample_method = PA_RESAMPLER_AUTO,
78 .disable_remixing = FALSE,
81 .system_instance = FALSE,
82 .no_cpu_limit = FALSE,
84 .default_n_fragments = 4,
85 .default_fragment_size_msec = 25,
86 .default_sample_spec = { .format = PA_SAMPLE_S16NE, .rate = 44100, .channels = 2 }
87 #ifdef HAVE_SYS_RESOURCE_H
88 ,.rlimit_fsize = { .value = 0, .is_set = FALSE },
89 .rlimit_data = { .value = 0, .is_set = FALSE },
90 .rlimit_stack = { .value = 0, .is_set = FALSE },
91 .rlimit_core = { .value = 0, .is_set = FALSE },
92 .rlimit_rss = { .value = 0, .is_set = FALSE }
94 ,.rlimit_nproc = { .value = 0, .is_set = FALSE }
96 ,.rlimit_nofile = { .value = 256, .is_set = TRUE }
98 ,.rlimit_memlock = { .value = 0, .is_set = FALSE }
100 ,.rlimit_as = { .value = 0, .is_set = FALSE }
102 ,.rlimit_locks = { .value = 0, .is_set = FALSE }
104 #ifdef RLIMIT_SIGPENDING
105 ,.rlimit_sigpending = { .value = 0, .is_set = FALSE }
107 #ifdef RLIMIT_MSGQUEUE
108 ,.rlimit_msgqueue = { .value = 0, .is_set = FALSE }
111 ,.rlimit_nice = { .value = 31, .is_set = TRUE } /* nice level of -11 */
114 ,.rlimit_rtprio = { .value = 9, .is_set = TRUE } /* One below JACK's default for the server */
117 ,.rlimit_rttime = { .value = PA_USEC_PER_SEC, .is_set = TRUE }
122 pa_daemon_conf* pa_daemon_conf_new(void) {
123 pa_daemon_conf *c = pa_xnewdup(pa_daemon_conf, &default_conf, 1);
125 c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
129 void pa_daemon_conf_free(pa_daemon_conf *c) {
131 pa_xfree(c->script_commands);
132 pa_xfree(c->dl_search_path);
133 pa_xfree(c->default_script_file);
134 pa_xfree(c->config_file);
138 int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
142 if (!strcmp(string, "auto"))
143 c->auto_log_target = 1;
144 else if (!strcmp(string, "syslog")) {
145 c->auto_log_target = 0;
146 c->log_target = PA_LOG_SYSLOG;
147 } else if (!strcmp(string, "stderr")) {
148 c->auto_log_target = 0;
149 c->log_target = PA_LOG_STDERR;
156 int pa_daemon_conf_set_log_level(pa_daemon_conf *c, const char *string) {
161 if (pa_atou(string, &u) >= 0) {
162 if (u >= PA_LOG_LEVEL_MAX)
165 c->log_level = (pa_log_level_t) u;
166 } else if (pa_startswith(string, "debug"))
167 c->log_level = PA_LOG_DEBUG;
168 else if (pa_startswith(string, "info"))
169 c->log_level = PA_LOG_INFO;
170 else if (pa_startswith(string, "notice"))
171 c->log_level = PA_LOG_NOTICE;
172 else if (pa_startswith(string, "warn"))
173 c->log_level = PA_LOG_WARN;
174 else if (pa_startswith(string, "err"))
175 c->log_level = PA_LOG_ERROR;
182 int pa_daemon_conf_set_resample_method(pa_daemon_conf *c, const char *string) {
187 if ((m = pa_parse_resample_method(string)) < 0)
190 c->resample_method = m;
194 static int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
195 pa_daemon_conf *c = data;
202 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
203 pa_log("[%s:%u] Invalid log target '%s'.", filename, line, rvalue);
210 static int parse_log_level(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
211 pa_daemon_conf *c = data;
218 if (pa_daemon_conf_set_log_level(c, rvalue) < 0) {
219 pa_log("[%s:%u] Invalid log level '%s'.", filename, line, rvalue);
226 static int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
227 pa_daemon_conf *c = data;
234 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
235 pa_log("[%s:%u] Invalid resample method '%s'.", filename, line, rvalue);
242 static int parse_rlimit(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
243 #ifdef HAVE_SYS_RESOURCE_H
244 struct pa_rlimit *r = data;
251 if (rvalue[strspn(rvalue, "\t ")] == 0) {
257 if (pa_atoi(rvalue, &k) < 0) {
258 pa_log("[%s:%u] Invalid rlimit '%s'.", filename, line, rvalue);
262 r->value = k >= 0 ? (rlim_t) k : 0;
265 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename, line);
271 static int parse_sample_format(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
272 pa_daemon_conf *c = data;
273 pa_sample_format_t f;
280 if ((f = pa_parse_sample_format(rvalue)) < 0) {
281 pa_log("[%s:%u] Invalid sample format '%s'.", filename, line, rvalue);
285 c->default_sample_spec.format = f;
289 static int parse_sample_rate(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
290 pa_daemon_conf *c = data;
298 if (pa_atoi(rvalue, &r) < 0 || r > PA_RATE_MAX || r <= 0) {
299 pa_log("[%s:%u] Invalid sample rate '%s'.", filename, line, rvalue);
303 c->default_sample_spec.rate = r;
307 static int parse_sample_channels(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
308 pa_daemon_conf *c = data;
316 if (pa_atoi(rvalue, &n) < 0 || n > PA_CHANNELS_MAX || n <= 0) {
317 pa_log("[%s:%u] Invalid sample channels '%s'.", filename, line, rvalue);
321 c->default_sample_spec.channels = (uint8_t) n;
325 static int parse_fragments(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
326 pa_daemon_conf *c = data;
334 if (pa_atoi(rvalue, &n) < 0 || n < 2) {
335 pa_log("[%s:%u] Invalid number of fragments '%s'.", filename, line, rvalue);
339 c->default_n_fragments = (unsigned) n;
343 static int parse_fragment_size_msec(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
344 pa_daemon_conf *c = data;
352 if (pa_atoi(rvalue, &n) < 0 || n < 1) {
353 pa_log("[%s:%u] Invalid fragment size '%s'.", filename, line, rvalue);
357 c->default_fragment_size_msec = (unsigned) n;
361 static int parse_nice_level(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
362 pa_daemon_conf *c = data;
370 if (pa_atoi(rvalue, &level) < 0 || level < -20 || level > 19) {
371 pa_log("[%s:%u] Invalid nice level '%s'.", filename, line, rvalue);
375 c->nice_level = (int) level;
379 static int parse_rtprio(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
380 pa_daemon_conf *c = data;
388 if (pa_atoi(rvalue, &rtprio) < 0 || rtprio < sched_get_priority_min(SCHED_FIFO) || rtprio > sched_get_priority_max(SCHED_FIFO)) {
389 pa_log("[%s:%u] Invalid realtime priority '%s'.", filename, line, rvalue);
393 c->realtime_priority = (int) rtprio;
397 int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
401 pa_config_item table[] = {
402 { "daemonize", pa_config_parse_bool, NULL },
403 { "fail", pa_config_parse_bool, NULL },
404 { "high-priority", pa_config_parse_bool, NULL },
405 { "realtime-scheduling", pa_config_parse_bool, NULL },
406 { "disallow-module-loading", pa_config_parse_bool, NULL },
407 { "use-pid-file", pa_config_parse_bool, NULL },
408 { "system-instance", pa_config_parse_bool, NULL },
409 { "no-cpu-limit", pa_config_parse_bool, NULL },
410 { "disable-shm", pa_config_parse_bool, NULL },
411 { "exit-idle-time", pa_config_parse_int, NULL },
412 { "module-idle-time", pa_config_parse_int, NULL },
413 { "scache-idle-time", pa_config_parse_int, NULL },
414 { "realtime-priority", parse_rtprio, NULL },
415 { "dl-search-path", pa_config_parse_string, NULL },
416 { "default-script-file", pa_config_parse_string, NULL },
417 { "log-target", parse_log_target, NULL },
418 { "log-level", parse_log_level, NULL },
419 { "verbose", parse_log_level, NULL },
420 { "resample-method", parse_resample_method, NULL },
421 { "default-sample-format", parse_sample_format, NULL },
422 { "default-sample-rate", parse_sample_rate, NULL },
423 { "default-sample-channels", parse_sample_channels, NULL },
424 { "default-fragments", parse_fragments, NULL },
425 { "default-fragment-size-msec", parse_fragment_size_msec, NULL },
426 { "nice-level", parse_nice_level, NULL },
427 { "disable-remixing", pa_config_parse_bool, NULL },
428 { "load-default-script-file", pa_config_parse_bool, NULL },
429 #ifdef HAVE_SYS_RESOURCE_H
430 { "rlimit-fsize", parse_rlimit, NULL },
431 { "rlimit-data", parse_rlimit, NULL },
432 { "rlimit-stack", parse_rlimit, NULL },
433 { "rlimit-core", parse_rlimit, NULL },
434 { "rlimit-rss", parse_rlimit, NULL },
435 { "rlimit-nofile", parse_rlimit, NULL },
436 { "rlimit-as", parse_rlimit, NULL },
438 { "rlimit-nproc", parse_rlimit, NULL },
440 #ifdef RLIMIT_MEMLOCK
441 { "rlimit-memlock", parse_rlimit, NULL },
444 { "rlimit-locks", parse_rlimit, NULL },
446 #ifdef RLIMIT_SIGPENDING
447 { "rlimit-sigpending", parse_rlimit, NULL },
449 #ifdef RLIMIT_MSGQUEUE
450 { "rlimit-msgqueue", parse_rlimit, NULL },
453 { "rlimit-nice", parse_rlimit, NULL },
456 { "rlimit-rtprio", parse_rlimit, NULL },
459 { "rlimit-rttime", parse_rlimit, NULL },
462 { NULL, NULL, NULL },
465 table[0].data = &c->daemonize;
466 table[1].data = &c->fail;
467 table[2].data = &c->high_priority;
468 table[3].data = &c->realtime_scheduling;
469 table[4].data = &c->disallow_module_loading;
470 table[5].data = &c->use_pid_file;
471 table[6].data = &c->system_instance;
472 table[7].data = &c->no_cpu_limit;
473 table[8].data = &c->disable_shm;
474 table[9].data = &c->exit_idle_time;
475 table[10].data = &c->module_idle_time;
476 table[11].data = &c->scache_idle_time;
478 table[13].data = &c->dl_search_path;
479 table[14].data = &c->default_script_file;
490 table[25].data = &c->disable_remixing;
491 table[26].data = &c->load_default_script_file;
492 #ifdef HAVE_SYS_RESOURCE_H
493 table[27].data = &c->rlimit_fsize;
494 table[28].data = &c->rlimit_data;
495 table[29].data = &c->rlimit_stack;
496 table[30].data = &c->rlimit_as;
497 table[31].data = &c->rlimit_core;
498 table[32].data = &c->rlimit_nofile;
499 table[33].data = &c->rlimit_as;
501 table[34].data = &c->rlimit_nproc;
504 #ifdef RLIMIT_MEMLOCK
506 #error "Houston, we have a numbering problem!"
508 table[35].data = &c->rlimit_memlock;
512 #ifndef RLIMIT_MEMLOCK
513 #error "Houston, we have a numbering problem!"
515 table[36].data = &c->rlimit_locks;
518 #ifdef RLIMIT_SIGPENDING
520 #error "Houston, we have a numbering problem!"
522 table[37].data = &c->rlimit_sigpending;
525 #ifdef RLIMIT_MSGQUEUE
526 #ifndef RLIMIT_SIGPENDING
527 #error "Houston, we have a numbering problem!"
529 table[38].data = &c->rlimit_msgqueue;
533 #ifndef RLIMIT_MSGQUEUE
534 #error "Houston, we have a numbering problem!"
536 table[39].data = &c->rlimit_nice;
541 #error "Houston, we have a numbering problem!"
543 table[40].data = &c->rlimit_rtprio;
547 #ifndef RLIMIT_RTTIME
548 #error "Houston, we have a numbering problem!"
550 table[41].data = &c->rlimit_rttime;
554 pa_xfree(c->config_file);
555 c->config_file = NULL;
558 fopen(c->config_file = pa_xstrdup(filename), "r") :
559 pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file);
561 if (!f && errno != ENOENT) {
562 pa_log_warn("Failed to open configuration file: %s", pa_cstrerror(errno));
566 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
575 int pa_daemon_conf_env(pa_daemon_conf *c) {
579 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
580 pa_xfree(c->dl_search_path);
581 c->dl_search_path = pa_xstrdup(e);
583 if ((e = getenv(ENV_SCRIPT_FILE))) {
584 pa_xfree(c->default_script_file);
585 c->default_script_file = pa_xstrdup(e);
591 const char *pa_daemon_conf_get_default_script_file(pa_daemon_conf *c) {
594 if (!c->default_script_file) {
595 if (c->system_instance)
596 c->default_script_file = pa_find_config_file(DEFAULT_SYSTEM_SCRIPT_FILE, NULL, ENV_SCRIPT_FILE);
598 c->default_script_file = pa_find_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE);
601 return c->default_script_file;
604 FILE *pa_daemon_conf_open_default_script_file(pa_daemon_conf *c) {
608 if (!c->default_script_file) {
609 if (c->system_instance)
610 f = pa_open_config_file(DEFAULT_SYSTEM_SCRIPT_FILE, NULL, ENV_SCRIPT_FILE, &c->default_script_file);
612 f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file);
614 f = fopen(c->default_script_file, "r");
620 static const char* const log_level_to_string[] = {
621 [PA_LOG_DEBUG] = "debug",
622 [PA_LOG_INFO] = "info",
623 [PA_LOG_NOTICE] = "notice",
624 [PA_LOG_WARN] = "warning",
625 [PA_LOG_ERROR] = "error"
628 char *pa_daemon_conf_dump(pa_daemon_conf *c) {
636 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
638 pa_assert(c->log_level <= PA_LOG_LEVEL_MAX);
640 pa_strbuf_printf(s, "daemonize = %s\n", pa_yes_no(c->daemonize));
641 pa_strbuf_printf(s, "fail = %s\n", pa_yes_no(c->fail));
642 pa_strbuf_printf(s, "high-priority = %s\n", pa_yes_no(c->high_priority));
643 pa_strbuf_printf(s, "nice-level = %i\n", c->nice_level);
644 pa_strbuf_printf(s, "realtime-scheduling = %s\n", pa_yes_no(c->realtime_scheduling));
645 pa_strbuf_printf(s, "realtime-priority = %i\n", c->realtime_priority);
646 pa_strbuf_printf(s, "disallow-module-loading = %s\n", pa_yes_no(c->disallow_module_loading));
647 pa_strbuf_printf(s, "use-pid-file = %s\n", pa_yes_no(c->use_pid_file));
648 pa_strbuf_printf(s, "system-instance = %s\n", pa_yes_no(c->system_instance));
649 pa_strbuf_printf(s, "no-cpu-limit = %s\n", pa_yes_no(c->no_cpu_limit));
650 pa_strbuf_printf(s, "disable-shm = %s\n", pa_yes_no(c->disable_shm));
651 pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
652 pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
653 pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
654 pa_strbuf_printf(s, "dl-search-path = %s\n", pa_strempty(c->dl_search_path));
655 pa_strbuf_printf(s, "default-script-file = %s\n", pa_strempty(pa_daemon_conf_get_default_script_file(c)));
656 pa_strbuf_printf(s, "load-default-script-file = %s\n", pa_yes_no(c->load_default_script_file));
657 pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
658 pa_strbuf_printf(s, "log-level = %s\n", log_level_to_string[c->log_level]);
659 pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
660 pa_strbuf_printf(s, "disable-remixing = %s\n", pa_yes_no(c->disable_remixing));
661 pa_strbuf_printf(s, "default-sample-format = %s\n", pa_sample_format_to_string(c->default_sample_spec.format));
662 pa_strbuf_printf(s, "default-sample-rate = %u\n", c->default_sample_spec.rate);
663 pa_strbuf_printf(s, "default-sample-channels = %u\n", c->default_sample_spec.channels);
664 pa_strbuf_printf(s, "default-fragments = %u\n", c->default_n_fragments);
665 pa_strbuf_printf(s, "default-fragment-size-msec = %u\n", c->default_fragment_size_msec);
666 #ifdef HAVE_SYS_RESOURCE_H
667 pa_strbuf_printf(s, "rlimit-fsize = %li\n", c->rlimit_fsize.is_set ? (long int) c->rlimit_fsize.value : -1);
668 pa_strbuf_printf(s, "rlimit-data = %li\n", c->rlimit_data.is_set ? (long int) c->rlimit_data.value : -1);
669 pa_strbuf_printf(s, "rlimit-stack = %li\n", c->rlimit_stack.is_set ? (long int) c->rlimit_stack.value : -1);
670 pa_strbuf_printf(s, "rlimit-core = %li\n", c->rlimit_core.is_set ? (long int) c->rlimit_core.value : -1);
671 pa_strbuf_printf(s, "rlimit-as = %li\n", c->rlimit_as.is_set ? (long int) c->rlimit_as.value : -1);
672 pa_strbuf_printf(s, "rlimit-rss = %li\n", c->rlimit_rss.is_set ? (long int) c->rlimit_rss.value : -1);
674 pa_strbuf_printf(s, "rlimit-nproc = %li\n", c->rlimit_nproc.is_set ? (long int) c->rlimit_nproc.value : -1);
676 pa_strbuf_printf(s, "rlimit-nofile = %li\n", c->rlimit_nofile.is_set ? (long int) c->rlimit_nofile.value : -1);
677 #ifdef RLIMIT_MEMLOCK
678 pa_strbuf_printf(s, "rlimit-memlock = %li\n", c->rlimit_memlock.is_set ? (long int) c->rlimit_memlock.value : -1);
681 pa_strbuf_printf(s, "rlimit-locks = %li\n", c->rlimit_locks.is_set ? (long int) c->rlimit_locks.value : -1);
683 #ifdef RLIMIT_SIGPENDING
684 pa_strbuf_printf(s, "rlimit-sigpending = %li\n", c->rlimit_sigpending.is_set ? (long int) c->rlimit_sigpending.value : -1);
686 #ifdef RLIMIT_MSGQUEUE
687 pa_strbuf_printf(s, "rlimit-msgqueue = %li\n", c->rlimit_msgqueue.is_set ? (long int) c->rlimit_msgqueue.value : -1);
690 pa_strbuf_printf(s, "rlimit-nice = %li\n", c->rlimit_nice.is_set ? (long int) c->rlimit_nice.value : -1);
693 pa_strbuf_printf(s, "rlimit-rtprio = %li\n", c->rlimit_rtprio.is_set ? (long int) c->rlimit_rtprio.value : -1);
696 pa_strbuf_printf(s, "rlimit-rttime = %li\n", c->rlimit_rttime.is_set ? (long int) c->rlimit_rttime.value : -1);
700 return pa_strbuf_tostring_free(s);