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>
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 #ifdef HAVE_SYS_RESOURCE_H
82 , .rlimit_as
= { .value
= 0, .is_set
= 0 },
83 .rlimit_core
= { .value
= 0, .is_set
= 0 },
84 .rlimit_data
= { .value
= 0, .is_set
= 0 },
85 .rlimit_fsize
= { .value
= 0, .is_set
= 0 },
86 .rlimit_nofile
= { .value
= 200, .is_set
= 1 },
87 .rlimit_stack
= { .value
= 0, .is_set
= 0 }
89 , .rlimit_nproc
= { .value
= 0, .is_set
= 0 }
92 , .rlimit_memlock
= { .value
= 0, .is_set
= 1 }
97 pa_daemon_conf
* pa_daemon_conf_new(void) {
99 pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
101 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
, "r")))
104 c
->dl_search_path
= pa_xstrdup(PA_DLSEARCHPATH
);
108 void pa_daemon_conf_free(pa_daemon_conf
*c
) {
110 pa_xfree(c
->script_commands
);
111 pa_xfree(c
->dl_search_path
);
112 pa_xfree(c
->default_script_file
);
113 pa_xfree(c
->config_file
);
117 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
120 if (!strcmp(string
, "auto"))
121 c
->auto_log_target
= 1;
122 else if (!strcmp(string
, "syslog")) {
123 c
->auto_log_target
= 0;
124 c
->log_target
= PA_LOG_SYSLOG
;
125 } else if (!strcmp(string
, "stderr")) {
126 c
->auto_log_target
= 0;
127 c
->log_target
= PA_LOG_STDERR
;
134 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
138 if (pa_atou(string
, &u
) >= 0) {
139 if (u
>= PA_LOG_LEVEL_MAX
)
142 c
->log_level
= (pa_log_level_t
) u
;
143 } else if (pa_startswith(string
, "debug"))
144 c
->log_level
= PA_LOG_DEBUG
;
145 else if (pa_startswith(string
, "info"))
146 c
->log_level
= PA_LOG_INFO
;
147 else if (pa_startswith(string
, "notice"))
148 c
->log_level
= PA_LOG_NOTICE
;
149 else if (pa_startswith(string
, "warn"))
150 c
->log_level
= PA_LOG_WARN
;
151 else if (pa_startswith(string
, "err"))
152 c
->log_level
= PA_LOG_ERROR
;
159 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
;
172 assert(filename
&& lvalue
&& rvalue
&& data
);
174 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
175 pa_log("[%s:%u] Invalid log target '%s'.", filename
, line
, rvalue
);
182 static int parse_log_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
183 pa_daemon_conf
*c
= data
;
184 assert(filename
&& lvalue
&& rvalue
&& data
);
186 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
187 pa_log("[%s:%u] Invalid log level '%s'.", filename
, line
, rvalue
);
194 static int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
195 pa_daemon_conf
*c
= data
;
196 assert(filename
&& lvalue
&& rvalue
&& data
);
198 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
199 pa_log("[%s:%u] Inavalid resample method '%s'.", filename
, line
, rvalue
);
206 static int parse_rlimit(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
207 #ifdef HAVE_SYS_RESOURCE_H
208 struct pa_rlimit
*r
= data
;
214 if (rvalue
[strspn(rvalue
, "\t ")] == 0) {
220 if (pa_atoi(rvalue
, &k
) < 0) {
221 pa_log("[%s:%u] Inavalid rlimit '%s'.", filename
, line
, rvalue
);
225 r
->value
= k
>= 0 ? (rlim_t
) k
: 0;
228 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename
, line
);
234 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
238 pa_config_item table
[] = {
239 { "daemonize", pa_config_parse_bool
, NULL
},
240 { "fail", pa_config_parse_bool
, NULL
},
241 { "high-priority", pa_config_parse_bool
, NULL
},
242 { "disallow-module-loading", pa_config_parse_bool
, NULL
},
243 { "exit-idle-time", pa_config_parse_int
, NULL
},
244 { "module-idle-time", pa_config_parse_int
, NULL
},
245 { "scache-idle-time", pa_config_parse_int
, NULL
},
246 { "dl-search-path", pa_config_parse_string
, NULL
},
247 { "default-script-file", pa_config_parse_string
, NULL
},
248 { "log-target", parse_log_target
, NULL
},
249 { "log-level", parse_log_level
, NULL
},
250 { "verbose", parse_log_level
, NULL
},
251 { "resample-method", parse_resample_method
, NULL
},
252 { "use-pid-file", pa_config_parse_bool
, NULL
},
253 { "system-instance", pa_config_parse_bool
, NULL
},
254 { "no-cpu-limit", pa_config_parse_bool
, NULL
},
255 { "disable-shm", pa_config_parse_bool
, NULL
},
256 #ifdef HAVE_SYS_RESOURCE_H
257 { "rlimit-as", parse_rlimit
, NULL
},
258 { "rlimit-core", parse_rlimit
, NULL
},
259 { "rlimit-data", parse_rlimit
, NULL
},
260 { "rlimit-fsize", parse_rlimit
, NULL
},
261 { "rlimit-nofile", parse_rlimit
, NULL
},
262 { "rlimit-stack", parse_rlimit
, NULL
},
264 { "rlimit-nproc", parse_rlimit
, NULL
},
266 #ifdef RLIMIT_MEMLOCK
267 { "rlimit-memlock", parse_rlimit
, NULL
},
270 { NULL
, NULL
, NULL
},
273 table
[0].data
= &c
->daemonize
;
274 table
[1].data
= &c
->fail
;
275 table
[2].data
= &c
->high_priority
;
276 table
[3].data
= &c
->disallow_module_loading
;
277 table
[4].data
= &c
->exit_idle_time
;
278 table
[5].data
= &c
->module_idle_time
;
279 table
[6].data
= &c
->scache_idle_time
;
280 table
[7].data
= &c
->dl_search_path
;
281 table
[8].data
= &c
->default_script_file
;
286 table
[13].data
= &c
->use_pid_file
;
287 table
[14].data
= &c
->system_instance
;
288 table
[15].data
= &c
->no_cpu_limit
;
289 table
[16].data
= &c
->disable_shm
;
290 #ifdef HAVE_SYS_RESOURCE_H
291 table
[17].data
= &c
->rlimit_as
;
292 table
[18].data
= &c
->rlimit_core
;
293 table
[19].data
= &c
->rlimit_data
;
294 table
[20].data
= &c
->rlimit_fsize
;
295 table
[21].data
= &c
->rlimit_nofile
;
296 table
[22].data
= &c
->rlimit_stack
;
298 table
[23].data
= &c
->rlimit_nproc
;
300 #ifdef RLIMIT_MEMLOCK
302 #error "Houston, we have a numbering problem!"
304 table
[24].data
= &c
->rlimit_memlock
;
309 pa_xfree(c
->config_file
);
310 c
->config_file
= NULL
;
313 fopen(c
->config_file
= pa_xstrdup(filename
), "r") :
314 pa_open_config_file(DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
, ENV_CONFIG_FILE
, &c
->config_file
, "r");
316 if (!f
&& errno
!= ENOENT
) {
317 pa_log("WARNING: failed to open configuration file '%s': %s", c
->config_file
, pa_cstrerror(errno
));
321 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
330 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
333 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
334 pa_xfree(c
->dl_search_path
);
335 c
->dl_search_path
= pa_xstrdup(e
);
337 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
338 pa_xfree(c
->default_script_file
);
339 c
->default_script_file
= pa_xstrdup(e
);
345 static const char* const log_level_to_string
[] = {
346 [PA_LOG_DEBUG
] = "debug",
347 [PA_LOG_INFO
] = "info",
348 [PA_LOG_NOTICE
] = "notice",
349 [PA_LOG_WARN
] = "warning",
350 [PA_LOG_ERROR
] = "error"
353 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
354 pa_strbuf
*s
= pa_strbuf_new();
357 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
359 assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
361 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
362 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
363 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
364 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
365 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
366 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
367 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
368 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
369 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
370 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
371 pa_strbuf_printf(s
, "log-level = %s\n", log_level_to_string
[c
->log_level
]);
372 pa_strbuf_printf(s
, "resample-method = %s\n", pa_resample_method_to_string(c
->resample_method
));
373 pa_strbuf_printf(s
, "use-pid-file = %i\n", c
->use_pid_file
);
374 pa_strbuf_printf(s
, "system-instance = %i\n", !!c
->system_instance
);
375 pa_strbuf_printf(s
, "no-cpu-limit = %i\n", !!c
->no_cpu_limit
);
376 pa_strbuf_printf(s
, "disable_shm = %i\n", !!c
->disable_shm
);
377 #ifdef HAVE_SYS_RESOURCE_H
378 pa_strbuf_printf(s
, "rlimit-as = %li\n", c
->rlimit_as
.is_set
? (long int) c
->rlimit_as
.value
: -1);
379 pa_strbuf_printf(s
, "rlimit-core = %li\n", c
->rlimit_core
.is_set
? (long int) c
->rlimit_core
.value
: -1);
380 pa_strbuf_printf(s
, "rlimit-data = %li\n", c
->rlimit_data
.is_set
? (long int) c
->rlimit_data
.value
: -1);
381 pa_strbuf_printf(s
, "rlimit-fsize = %li\n", c
->rlimit_fsize
.is_set
? (long int) c
->rlimit_fsize
.value
: -1);
382 pa_strbuf_printf(s
, "rlimit-nofile = %li\n", c
->rlimit_nofile
.is_set
? (long int) c
->rlimit_nofile
.value
: -1);
383 pa_strbuf_printf(s
, "rlimit-stack = %li\n", c
->rlimit_stack
.is_set
? (long int) c
->rlimit_stack
.value
: -1);
385 pa_strbuf_printf(s
, "rlimit-nproc = %li\n", c
->rlimit_nproc
.is_set
? (long int) c
->rlimit_nproc
.value
: -1);
387 #ifdef RLIMIT_MEMLOCK
388 pa_strbuf_printf(s
, "rlimit-memlock = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_memlock
.value
: -1);
392 return pa_strbuf_tostring_free(s
);