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>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/strbuf.h>
40 #include <pulsecore/conf-parser.h>
41 #include <pulsecore/resampler.h>
42 #include <pulsecore/macro.h>
44 #include "daemon-conf.h"
46 #define DEFAULT_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "default.pa"
47 #define DEFAULT_SCRIPT_FILE_USER PA_PATH_SEP "default.pa"
48 #define DEFAULT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "daemon.conf"
49 #define DEFAULT_CONFIG_FILE_USER PA_PATH_SEP "daemon.conf"
51 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
52 #define ENV_CONFIG_FILE "PULSE_CONFIG"
53 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
55 static const pa_daemon_conf default_conf
= {
59 .high_priority
= TRUE
,
61 .realtime_scheduling
= FALSE
,
62 .realtime_priority
= 5, /* Half of JACK's default rtprio */
63 .disallow_module_loading
= FALSE
,
65 .module_idle_time
= 20,
66 .scache_idle_time
= 20,
68 .script_commands
= NULL
,
69 .dl_search_path
= NULL
,
70 .default_script_file
= NULL
,
71 .log_target
= PA_LOG_SYSLOG
,
72 .log_level
= PA_LOG_NOTICE
,
73 .resample_method
= PA_RESAMPLER_AUTO
,
74 .disable_remixing
= FALSE
,
77 .system_instance
= FALSE
,
78 .no_cpu_limit
= FALSE
,
80 .default_n_fragments
= 4,
81 .default_fragment_size_msec
= 25,
82 .default_sample_spec
= { .format
= PA_SAMPLE_S16NE
, .rate
= 44100, .channels
= 2 }
83 #ifdef HAVE_SYS_RESOURCE_H
84 , .rlimit_as
= { .value
= 0, .is_set
= FALSE
},
85 .rlimit_core
= { .value
= 0, .is_set
= FALSE
},
86 .rlimit_data
= { .value
= 0, .is_set
= FALSE
},
87 .rlimit_fsize
= { .value
= 0, .is_set
= FALSE
},
88 .rlimit_nofile
= { .value
= 256, .is_set
= TRUE
},
89 .rlimit_stack
= { .value
= 0, .is_set
= FALSE
}
91 , .rlimit_nproc
= { .value
= 0, .is_set
= FALSE
}
94 , .rlimit_memlock
= { .value
= 0, .is_set
= FALSE
}
97 , .rlimit_nice
= { .value
= 31, .is_set
= TRUE
} /* nice level of -11 */
100 , .rlimit_rtprio
= { .value
= 9, .is_set
= TRUE
} /* One below JACK's default for the server */
105 pa_daemon_conf
* pa_daemon_conf_new(void) {
107 pa_daemon_conf
*c
= pa_xnewdup(pa_daemon_conf
, &default_conf
, 1);
109 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
, "r")))
112 c
->dl_search_path
= pa_xstrdup(PA_DLSEARCHPATH
);
116 void pa_daemon_conf_free(pa_daemon_conf
*c
) {
118 pa_xfree(c
->script_commands
);
119 pa_xfree(c
->dl_search_path
);
120 pa_xfree(c
->default_script_file
);
121 pa_xfree(c
->config_file
);
125 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
129 if (!strcmp(string
, "auto"))
130 c
->auto_log_target
= 1;
131 else if (!strcmp(string
, "syslog")) {
132 c
->auto_log_target
= 0;
133 c
->log_target
= PA_LOG_SYSLOG
;
134 } else if (!strcmp(string
, "stderr")) {
135 c
->auto_log_target
= 0;
136 c
->log_target
= PA_LOG_STDERR
;
143 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
148 if (pa_atou(string
, &u
) >= 0) {
149 if (u
>= PA_LOG_LEVEL_MAX
)
152 c
->log_level
= (pa_log_level_t
) u
;
153 } else if (pa_startswith(string
, "debug"))
154 c
->log_level
= PA_LOG_DEBUG
;
155 else if (pa_startswith(string
, "info"))
156 c
->log_level
= PA_LOG_INFO
;
157 else if (pa_startswith(string
, "notice"))
158 c
->log_level
= PA_LOG_NOTICE
;
159 else if (pa_startswith(string
, "warn"))
160 c
->log_level
= PA_LOG_WARN
;
161 else if (pa_startswith(string
, "err"))
162 c
->log_level
= PA_LOG_ERROR
;
169 int pa_daemon_conf_set_resample_method(pa_daemon_conf
*c
, const char *string
) {
174 if ((m
= pa_parse_resample_method(string
)) < 0)
177 c
->resample_method
= m
;
181 static int parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
182 pa_daemon_conf
*c
= data
;
189 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
190 pa_log("[%s:%u] Invalid log target '%s'.", filename
, line
, rvalue
);
197 static int parse_log_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
198 pa_daemon_conf
*c
= data
;
205 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
206 pa_log("[%s:%u] Invalid log level '%s'.", filename
, line
, rvalue
);
213 static int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
214 pa_daemon_conf
*c
= data
;
221 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
222 pa_log("[%s:%u] Invalid resample method '%s'.", filename
, line
, rvalue
);
229 static int parse_rlimit(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
230 #ifdef HAVE_SYS_RESOURCE_H
231 struct pa_rlimit
*r
= data
;
238 if (rvalue
[strspn(rvalue
, "\t ")] == 0) {
244 if (pa_atoi(rvalue
, &k
) < 0) {
245 pa_log("[%s:%u] Invalid rlimit '%s'.", filename
, line
, rvalue
);
249 r
->value
= k
>= 0 ? (rlim_t
) k
: 0;
252 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename
, line
);
258 static int parse_sample_format(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
259 pa_daemon_conf
*c
= data
;
260 pa_sample_format_t f
;
267 if ((f
= pa_parse_sample_format(rvalue
)) < 0) {
268 pa_log("[%s:%u] Invalid sample format '%s'.", filename
, line
, rvalue
);
272 c
->default_sample_spec
.format
= f
;
276 static int parse_sample_rate(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
277 pa_daemon_conf
*c
= data
;
285 if (pa_atoi(rvalue
, &r
) < 0 || r
> PA_RATE_MAX
|| r
<= 0) {
286 pa_log("[%s:%u] Invalid sample rate '%s'.", filename
, line
, rvalue
);
290 c
->default_sample_spec
.rate
= r
;
294 static int parse_sample_channels(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
295 pa_daemon_conf
*c
= data
;
303 if (pa_atoi(rvalue
, &n
) < 0 || n
> PA_CHANNELS_MAX
|| n
<= 0) {
304 pa_log("[%s:%u] Invalid sample channels '%s'.", filename
, line
, rvalue
);
308 c
->default_sample_spec
.channels
= (uint8_t) n
;
312 static int parse_fragments(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
313 pa_daemon_conf
*c
= data
;
321 if (pa_atoi(rvalue
, &n
) < 0 || n
< 2) {
322 pa_log("[%s:%u] Invalid number of fragments '%s'.", filename
, line
, rvalue
);
326 c
->default_n_fragments
= (unsigned) n
;
330 static int parse_fragment_size_msec(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
331 pa_daemon_conf
*c
= data
;
339 if (pa_atoi(rvalue
, &n
) < 0 || n
< 1) {
340 pa_log("[%s:%u] Invalid fragment size '%s'.", filename
, line
, rvalue
);
344 c
->default_fragment_size_msec
= (unsigned) n
;
348 static int parse_nice_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
349 pa_daemon_conf
*c
= data
;
357 if (pa_atoi(rvalue
, &level
) < 0 || level
< -20 || level
> 19) {
358 pa_log("[%s:%u] Invalid nice level '%s'.", filename
, line
, rvalue
);
362 c
->nice_level
= (int) level
;
366 static int parse_rtprio(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
367 pa_daemon_conf
*c
= data
;
375 if (pa_atoi(rvalue
, &rtprio
) < 0 || rtprio
< sched_get_priority_min(SCHED_FIFO
) || rtprio
> sched_get_priority_max(SCHED_FIFO
)) {
376 pa_log("[%s:%u] Invalid realtime priority '%s'.", filename
, line
, rvalue
);
380 c
->realtime_priority
= (int) rtprio
;
384 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
388 pa_config_item table
[] = {
389 { "daemonize", pa_config_parse_bool
, NULL
},
390 { "fail", pa_config_parse_bool
, NULL
},
391 { "high-priority", pa_config_parse_bool
, NULL
},
392 { "realtime-scheduling", pa_config_parse_bool
, NULL
},
393 { "disallow-module-loading", pa_config_parse_bool
, NULL
},
394 { "use-pid-file", pa_config_parse_bool
, NULL
},
395 { "system-instance", pa_config_parse_bool
, NULL
},
396 { "no-cpu-limit", pa_config_parse_bool
, NULL
},
397 { "disable-shm", pa_config_parse_bool
, NULL
},
398 { "exit-idle-time", pa_config_parse_int
, NULL
},
399 { "module-idle-time", pa_config_parse_int
, NULL
},
400 { "scache-idle-time", pa_config_parse_int
, NULL
},
401 { "realtime-priority", parse_rtprio
, NULL
},
402 { "dl-search-path", pa_config_parse_string
, NULL
},
403 { "default-script-file", pa_config_parse_string
, NULL
},
404 { "log-target", parse_log_target
, NULL
},
405 { "log-level", parse_log_level
, NULL
},
406 { "verbose", parse_log_level
, NULL
},
407 { "resample-method", parse_resample_method
, NULL
},
408 { "default-sample-format", parse_sample_format
, NULL
},
409 { "default-sample-rate", parse_sample_rate
, NULL
},
410 { "default-sample-channels", parse_sample_channels
, NULL
},
411 { "default-fragments", parse_fragments
, NULL
},
412 { "default-fragment-size-msec", parse_fragment_size_msec
, NULL
},
413 { "nice-level", parse_nice_level
, NULL
},
414 { "disable-remixing", pa_config_parse_bool
, NULL
},
415 #ifdef HAVE_SYS_RESOURCE_H
416 { "rlimit-as", parse_rlimit
, NULL
},
417 { "rlimit-core", parse_rlimit
, NULL
},
418 { "rlimit-data", parse_rlimit
, NULL
},
419 { "rlimit-fsize", parse_rlimit
, NULL
},
420 { "rlimit-nofile", parse_rlimit
, NULL
},
421 { "rlimit-stack", parse_rlimit
, NULL
},
423 { "rlimit-nproc", parse_rlimit
, NULL
},
425 #ifdef RLIMIT_MEMLOCK
426 { "rlimit-memlock", parse_rlimit
, NULL
},
429 { "rlimit-nice", parse_rlimit
, NULL
},
432 { "rlimit-rtprio", parse_rlimit
, NULL
},
435 { NULL
, NULL
, NULL
},
438 table
[0].data
= &c
->daemonize
;
439 table
[1].data
= &c
->fail
;
440 table
[2].data
= &c
->high_priority
;
441 table
[3].data
= &c
->realtime_scheduling
;
442 table
[4].data
= &c
->disallow_module_loading
;
443 table
[5].data
= &c
->use_pid_file
;
444 table
[6].data
= &c
->system_instance
;
445 table
[7].data
= &c
->no_cpu_limit
;
446 table
[8].data
= &c
->disable_shm
;
447 table
[9].data
= &c
->exit_idle_time
;
448 table
[10].data
= &c
->module_idle_time
;
449 table
[11].data
= &c
->scache_idle_time
;
451 table
[13].data
= &c
->dl_search_path
;
452 table
[14].data
= &c
->default_script_file
;
463 table
[25].data
= &c
->disable_remixing
;
464 #ifdef HAVE_SYS_RESOURCE_H
465 table
[26].data
= &c
->rlimit_as
;
466 table
[27].data
= &c
->rlimit_core
;
467 table
[28].data
= &c
->rlimit_data
;
468 table
[29].data
= &c
->rlimit_fsize
;
469 table
[30].data
= &c
->rlimit_nofile
;
470 table
[31].data
= &c
->rlimit_stack
;
472 table
[32].data
= &c
->rlimit_nproc
;
474 #ifdef RLIMIT_MEMLOCK
476 #error "Houston, we have a numbering problem!"
478 table
[33].data
= &c
->rlimit_memlock
;
481 #ifndef RLIMIT_MEMLOCK
482 #error "Houston, we have a numbering problem!"
484 table
[34].data
= &c
->rlimit_nice
;
488 #error "Houston, we have a numbering problem!"
490 table
[35].data
= &c
->rlimit_rtprio
;
494 pa_xfree(c
->config_file
);
495 c
->config_file
= NULL
;
498 fopen(c
->config_file
= pa_xstrdup(filename
), "r") :
499 pa_open_config_file(DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
, ENV_CONFIG_FILE
, &c
->config_file
, "r");
501 if (!f
&& errno
!= ENOENT
) {
502 pa_log_warn("Failed to open configuration file '%s': %s", c
->config_file
, pa_cstrerror(errno
));
506 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
515 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
518 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
519 pa_xfree(c
->dl_search_path
);
520 c
->dl_search_path
= pa_xstrdup(e
);
522 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
523 pa_xfree(c
->default_script_file
);
524 c
->default_script_file
= pa_xstrdup(e
);
530 static const char* const log_level_to_string
[] = {
531 [PA_LOG_DEBUG
] = "debug",
532 [PA_LOG_INFO
] = "info",
533 [PA_LOG_NOTICE
] = "notice",
534 [PA_LOG_WARN
] = "warning",
535 [PA_LOG_ERROR
] = "error"
538 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
546 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
548 pa_assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
550 pa_strbuf_printf(s
, "daemonize = %s\n", pa_yes_no(c
->daemonize
));
551 pa_strbuf_printf(s
, "fail = %s\n", pa_yes_no(c
->fail
));
552 pa_strbuf_printf(s
, "high-priority = %s\n", pa_yes_no(c
->high_priority
));
553 pa_strbuf_printf(s
, "nice-level = %i\n", c
->nice_level
);
554 pa_strbuf_printf(s
, "realtime-scheduling = %s\n", pa_yes_no(c
->realtime_scheduling
));
555 pa_strbuf_printf(s
, "realtime-priority = %i\n", c
->realtime_priority
);
556 pa_strbuf_printf(s
, "disallow-module-loading = %s\n", pa_yes_no(c
->disallow_module_loading
));
557 pa_strbuf_printf(s
, "use-pid-file = %s\n", pa_yes_no(c
->use_pid_file
));
558 pa_strbuf_printf(s
, "system-instance = %s\n", pa_yes_no(c
->system_instance
));
559 pa_strbuf_printf(s
, "no-cpu-limit = %s\n", pa_yes_no(c
->no_cpu_limit
));
560 pa_strbuf_printf(s
, "disable-shm = %s\n", pa_yes_no(c
->disable_shm
));
561 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
562 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
563 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
564 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
565 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
566 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
567 pa_strbuf_printf(s
, "log-level = %s\n", log_level_to_string
[c
->log_level
]);
568 pa_strbuf_printf(s
, "resample-method = %s\n", pa_resample_method_to_string(c
->resample_method
));
569 pa_strbuf_printf(s
, "disable-remixing = %s\n", pa_yes_no(c
->disable_remixing
));
570 pa_strbuf_printf(s
, "default-sample-format = %s\n", pa_sample_format_to_string(c
->default_sample_spec
.format
));
571 pa_strbuf_printf(s
, "default-sample-rate = %u\n", c
->default_sample_spec
.rate
);
572 pa_strbuf_printf(s
, "default-sample-channels = %u\n", c
->default_sample_spec
.channels
);
573 pa_strbuf_printf(s
, "default-fragments = %u\n", c
->default_n_fragments
);
574 pa_strbuf_printf(s
, "default-fragment-size-msec = %u\n", c
->default_fragment_size_msec
);
575 #ifdef HAVE_SYS_RESOURCE_H
576 pa_strbuf_printf(s
, "rlimit-as = %li\n", c
->rlimit_as
.is_set
? (long int) c
->rlimit_as
.value
: -1);
577 pa_strbuf_printf(s
, "rlimit-core = %li\n", c
->rlimit_core
.is_set
? (long int) c
->rlimit_core
.value
: -1);
578 pa_strbuf_printf(s
, "rlimit-data = %li\n", c
->rlimit_data
.is_set
? (long int) c
->rlimit_data
.value
: -1);
579 pa_strbuf_printf(s
, "rlimit-fsize = %li\n", c
->rlimit_fsize
.is_set
? (long int) c
->rlimit_fsize
.value
: -1);
580 pa_strbuf_printf(s
, "rlimit-nofile = %li\n", c
->rlimit_nofile
.is_set
? (long int) c
->rlimit_nofile
.value
: -1);
581 pa_strbuf_printf(s
, "rlimit-stack = %li\n", c
->rlimit_stack
.is_set
? (long int) c
->rlimit_stack
.value
: -1);
583 pa_strbuf_printf(s
, "rlimit-nproc = %li\n", c
->rlimit_nproc
.is_set
? (long int) c
->rlimit_nproc
.value
: -1);
585 #ifdef RLIMIT_MEMLOCK
586 pa_strbuf_printf(s
, "rlimit-memlock = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_memlock
.value
: -1);
589 pa_strbuf_printf(s
, "rlimit-nice = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_nice
.value
: -1);
592 pa_strbuf_printf(s
, "rlimit-rtprio = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_rtprio
.value
: -1);
596 return pa_strbuf_tostring_free(s
);