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"
48 # define PATH_SEP "\\"
51 #define DEFAULT_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "default.pa"
52 #define DEFAULT_SCRIPT_FILE_USER PATH_SEP "default.pa"
53 #define DEFAULT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "daemon.conf"
54 #define DEFAULT_CONFIG_FILE_USER PATH_SEP "daemon.conf"
56 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
57 #define ENV_CONFIG_FILE "PULSE_CONFIG"
58 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
60 static const pa_daemon_conf default_conf
= {
65 .disallow_module_loading
= 0,
67 .module_idle_time
= 20,
68 .scache_idle_time
= 20,
70 .script_commands
= NULL
,
71 .dl_search_path
= NULL
,
72 .default_script_file
= NULL
,
73 .log_target
= PA_LOG_SYSLOG
,
74 .log_level
= PA_LOG_NOTICE
,
75 .resample_method
= PA_RESAMPLER_SRC_SINC_FASTEST
,
81 .default_n_fragments
= 4,
82 .default_fragment_size_msec
= 25,
83 .default_sample_spec
= { .format
= PA_SAMPLE_S16NE
, .rate
= 44100, .channels
= 2 }
84 #ifdef HAVE_SYS_RESOURCE_H
85 , .rlimit_as
= { .value
= 0, .is_set
= 0 },
86 .rlimit_core
= { .value
= 0, .is_set
= 0 },
87 .rlimit_data
= { .value
= 0, .is_set
= 0 },
88 .rlimit_fsize
= { .value
= 0, .is_set
= 0 },
89 .rlimit_nofile
= { .value
= 200, .is_set
= 1 },
90 .rlimit_stack
= { .value
= 0, .is_set
= 0 }
92 , .rlimit_nproc
= { .value
= 0, .is_set
= 0 }
95 , .rlimit_memlock
= { .value
= 0, .is_set
= 1 }
100 pa_daemon_conf
* pa_daemon_conf_new(void) {
102 pa_daemon_conf
*c
= pa_xnewdup(pa_daemon_conf
, &default_conf
, 1);
104 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
, "r")))
107 c
->dl_search_path
= pa_xstrdup(PA_DLSEARCHPATH
);
111 void pa_daemon_conf_free(pa_daemon_conf
*c
) {
113 pa_xfree(c
->script_commands
);
114 pa_xfree(c
->dl_search_path
);
115 pa_xfree(c
->default_script_file
);
116 pa_xfree(c
->config_file
);
120 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
124 if (!strcmp(string
, "auto"))
125 c
->auto_log_target
= 1;
126 else if (!strcmp(string
, "syslog")) {
127 c
->auto_log_target
= 0;
128 c
->log_target
= PA_LOG_SYSLOG
;
129 } else if (!strcmp(string
, "stderr")) {
130 c
->auto_log_target
= 0;
131 c
->log_target
= PA_LOG_STDERR
;
138 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
143 if (pa_atou(string
, &u
) >= 0) {
144 if (u
>= PA_LOG_LEVEL_MAX
)
147 c
->log_level
= (pa_log_level_t
) u
;
148 } else if (pa_startswith(string
, "debug"))
149 c
->log_level
= PA_LOG_DEBUG
;
150 else if (pa_startswith(string
, "info"))
151 c
->log_level
= PA_LOG_INFO
;
152 else if (pa_startswith(string
, "notice"))
153 c
->log_level
= PA_LOG_NOTICE
;
154 else if (pa_startswith(string
, "warn"))
155 c
->log_level
= PA_LOG_WARN
;
156 else if (pa_startswith(string
, "err"))
157 c
->log_level
= PA_LOG_ERROR
;
164 int pa_daemon_conf_set_resample_method(pa_daemon_conf
*c
, const char *string
) {
169 if ((m
= pa_parse_resample_method(string
)) < 0)
172 c
->resample_method
= m
;
176 static int parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
177 pa_daemon_conf
*c
= data
;
184 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
185 pa_log("[%s:%u] Invalid log target '%s'.", filename
, line
, rvalue
);
192 static int parse_log_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
193 pa_daemon_conf
*c
= data
;
200 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
201 pa_log("[%s:%u] Invalid log level '%s'.", filename
, line
, rvalue
);
208 static int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
209 pa_daemon_conf
*c
= data
;
216 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
217 pa_log("[%s:%u] Invalid resample method '%s'.", filename
, line
, rvalue
);
224 static int parse_rlimit(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
225 #ifdef HAVE_SYS_RESOURCE_H
226 struct pa_rlimit
*r
= data
;
233 if (rvalue
[strspn(rvalue
, "\t ")] == 0) {
239 if (pa_atoi(rvalue
, &k
) < 0) {
240 pa_log("[%s:%u] Invalid rlimit '%s'.", filename
, line
, rvalue
);
244 r
->value
= k
>= 0 ? (rlim_t
) k
: 0;
247 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename
, line
);
253 static int parse_sample_format(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
254 pa_daemon_conf
*c
= data
;
255 pa_sample_format_t f
;
262 if ((f
= pa_parse_sample_format(rvalue
)) < 0) {
263 pa_log("[%s:%u] Invalid sample format '%s'.", filename
, line
, rvalue
);
267 c
->default_sample_spec
.format
= f
;
271 static int parse_sample_rate(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
272 pa_daemon_conf
*c
= data
;
280 if (pa_atoi(rvalue
, &r
) < 0 || r
> PA_RATE_MAX
|| r
<= 0) {
281 pa_log("[%s:%u] Invalid sample rate '%s'.", filename
, line
, rvalue
);
285 c
->default_sample_spec
.rate
= r
;
289 static int parse_sample_channels(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
, &n
) < 0 || n
> PA_CHANNELS_MAX
|| n
<= 0) {
299 pa_log("[%s:%u] Invalid sample channels '%s'.", filename
, line
, rvalue
);
303 c
->default_sample_spec
.channels
= (uint8_t) n
;
307 static int parse_fragments(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
< 2) {
317 pa_log("[%s:%u] Invalid number of fragments '%s'.", filename
, line
, rvalue
);
321 c
->default_n_fragments
= (unsigned) n
;
325 static int parse_fragment_size_msec(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
< 1) {
335 pa_log("[%s:%u] Invalid fragment size '%s'.", filename
, line
, rvalue
);
339 c
->default_fragment_size_msec
= (unsigned) n
;
343 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
347 pa_config_item table
[] = {
348 { "daemonize", pa_config_parse_bool
, NULL
},
349 { "fail", pa_config_parse_bool
, NULL
},
350 { "high-priority", pa_config_parse_bool
, NULL
},
351 { "disallow-module-loading", pa_config_parse_bool
, NULL
},
352 { "exit-idle-time", pa_config_parse_int
, NULL
},
353 { "module-idle-time", pa_config_parse_int
, NULL
},
354 { "scache-idle-time", pa_config_parse_int
, NULL
},
355 { "dl-search-path", pa_config_parse_string
, NULL
},
356 { "default-script-file", pa_config_parse_string
, NULL
},
357 { "log-target", parse_log_target
, NULL
},
358 { "log-level", parse_log_level
, NULL
},
359 { "verbose", parse_log_level
, NULL
},
360 { "resample-method", parse_resample_method
, NULL
},
361 { "use-pid-file", pa_config_parse_bool
, NULL
},
362 { "system-instance", pa_config_parse_bool
, NULL
},
363 { "no-cpu-limit", pa_config_parse_bool
, NULL
},
364 { "disable-shm", pa_config_parse_bool
, NULL
},
365 { "default-sample-format", parse_sample_format
, NULL
},
366 { "default-sample-rate", parse_sample_rate
, NULL
},
367 { "default-sample-channels", parse_sample_channels
, NULL
},
368 { "default-fragments", parse_fragments
, NULL
},
369 { "default-fragment-size-msec", parse_fragment_size_msec
, NULL
},
370 #ifdef HAVE_SYS_RESOURCE_H
371 { "rlimit-as", parse_rlimit
, NULL
},
372 { "rlimit-core", parse_rlimit
, NULL
},
373 { "rlimit-data", parse_rlimit
, NULL
},
374 { "rlimit-fsize", parse_rlimit
, NULL
},
375 { "rlimit-nofile", parse_rlimit
, NULL
},
376 { "rlimit-stack", parse_rlimit
, NULL
},
378 { "rlimit-nproc", parse_rlimit
, NULL
},
380 #ifdef RLIMIT_MEMLOCK
381 { "rlimit-memlock", parse_rlimit
, NULL
},
384 { NULL
, NULL
, NULL
},
387 table
[0].data
= &c
->daemonize
;
388 table
[1].data
= &c
->fail
;
389 table
[2].data
= &c
->high_priority
;
390 table
[3].data
= &c
->disallow_module_loading
;
391 table
[4].data
= &c
->exit_idle_time
;
392 table
[5].data
= &c
->module_idle_time
;
393 table
[6].data
= &c
->scache_idle_time
;
394 table
[7].data
= &c
->dl_search_path
;
395 table
[8].data
= &c
->default_script_file
;
400 table
[13].data
= &c
->use_pid_file
;
401 table
[14].data
= &c
->system_instance
;
402 table
[15].data
= &c
->no_cpu_limit
;
403 table
[16].data
= &c
->disable_shm
;
409 #ifdef HAVE_SYS_RESOURCE_H
410 table
[22].data
= &c
->rlimit_as
;
411 table
[23].data
= &c
->rlimit_core
;
412 table
[24].data
= &c
->rlimit_data
;
413 table
[25].data
= &c
->rlimit_fsize
;
414 table
[26].data
= &c
->rlimit_nofile
;
415 table
[27].data
= &c
->rlimit_stack
;
417 table
[28].data
= &c
->rlimit_nproc
;
419 #ifdef RLIMIT_MEMLOCK
421 #error "Houston, we have a numbering problem!"
423 table
[29].data
= &c
->rlimit_memlock
;
427 pa_xfree(c
->config_file
);
428 c
->config_file
= NULL
;
431 fopen(c
->config_file
= pa_xstrdup(filename
), "r") :
432 pa_open_config_file(DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
, ENV_CONFIG_FILE
, &c
->config_file
, "r");
434 if (!f
&& errno
!= ENOENT
) {
435 pa_log("WARNING: failed to open configuration file '%s': %s", c
->config_file
, pa_cstrerror(errno
));
439 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
448 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
451 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
452 pa_xfree(c
->dl_search_path
);
453 c
->dl_search_path
= pa_xstrdup(e
);
455 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
456 pa_xfree(c
->default_script_file
);
457 c
->default_script_file
= pa_xstrdup(e
);
463 static const char* const log_level_to_string
[] = {
464 [PA_LOG_DEBUG
] = "debug",
465 [PA_LOG_INFO
] = "info",
466 [PA_LOG_NOTICE
] = "notice",
467 [PA_LOG_WARN
] = "warning",
468 [PA_LOG_ERROR
] = "error"
471 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
479 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
481 pa_assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
483 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
484 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
485 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
486 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
487 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
488 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
489 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
490 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
491 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
492 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
493 pa_strbuf_printf(s
, "log-level = %s\n", log_level_to_string
[c
->log_level
]);
494 pa_strbuf_printf(s
, "resample-method = %s\n", pa_resample_method_to_string(c
->resample_method
));
495 pa_strbuf_printf(s
, "use-pid-file = %i\n", c
->use_pid_file
);
496 pa_strbuf_printf(s
, "system-instance = %i\n", !!c
->system_instance
);
497 pa_strbuf_printf(s
, "no-cpu-limit = %i\n", !!c
->no_cpu_limit
);
498 pa_strbuf_printf(s
, "disable-shm = %i\n", !!c
->disable_shm
);
499 pa_strbuf_printf(s
, "default-sample-format = %s\n", pa_sample_format_to_string(c
->default_sample_spec
.format
));
500 pa_strbuf_printf(s
, "default-sample-rate = %u\n", c
->default_sample_spec
.rate
);
501 pa_strbuf_printf(s
, "default-sample-channels = %u\n", c
->default_sample_spec
.channels
);
502 pa_strbuf_printf(s
, "default-fragments = %u\n", c
->default_n_fragments
);
503 pa_strbuf_printf(s
, "default-fragment-size-msec = %u\n", c
->default_fragment_size_msec
);
504 #ifdef HAVE_SYS_RESOURCE_H
505 pa_strbuf_printf(s
, "rlimit-as = %li\n", c
->rlimit_as
.is_set
? (long int) c
->rlimit_as
.value
: -1);
506 pa_strbuf_printf(s
, "rlimit-core = %li\n", c
->rlimit_core
.is_set
? (long int) c
->rlimit_core
.value
: -1);
507 pa_strbuf_printf(s
, "rlimit-data = %li\n", c
->rlimit_data
.is_set
? (long int) c
->rlimit_data
.value
: -1);
508 pa_strbuf_printf(s
, "rlimit-fsize = %li\n", c
->rlimit_fsize
.is_set
? (long int) c
->rlimit_fsize
.value
: -1);
509 pa_strbuf_printf(s
, "rlimit-nofile = %li\n", c
->rlimit_nofile
.is_set
? (long int) c
->rlimit_nofile
.value
: -1);
510 pa_strbuf_printf(s
, "rlimit-stack = %li\n", c
->rlimit_stack
.is_set
? (long int) c
->rlimit_stack
.value
: -1);
512 pa_strbuf_printf(s
, "rlimit-nproc = %li\n", c
->rlimit_nproc
.is_set
? (long int) c
->rlimit_nproc
.value
: -1);
514 #ifdef RLIMIT_MEMLOCK
515 pa_strbuf_printf(s
, "rlimit-memlock = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_memlock
.value
: -1);
519 return pa_strbuf_tostring_free(s
);