4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/strbuf.h>
37 #include <pulsecore/conf-parser.h>
38 #include <pulsecore/resampler.h>
40 #include "daemon-conf.h"
45 # define PATH_SEP "\\"
48 #define DEFAULT_SCRIPT_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "default.pa"
49 #define DEFAULT_SCRIPT_FILE_USER PATH_SEP "default.pa"
50 #define DEFAULT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "daemon.conf"
51 #define DEFAULT_CONFIG_FILE_USER PATH_SEP "daemon.conf"
53 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
54 #define ENV_CONFIG_FILE "PULSE_CONFIG"
55 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
57 static const pa_daemon_conf default_conf
= {
62 .disallow_module_loading
= 0,
64 .module_idle_time
= 20,
65 .scache_idle_time
= 20,
67 .script_commands
= NULL
,
68 .dl_search_path
= NULL
,
69 .default_script_file
= NULL
,
70 .log_target
= PA_LOG_SYSLOG
,
71 .log_level
= PA_LOG_NOTICE
,
72 .resample_method
= PA_RESAMPLER_SRC_SINC_FASTEST
,
76 #ifdef HAVE_SYS_RESOURCE_H
77 , .rlimit_as
= { .value
= 0, .is_set
= 0 },
78 .rlimit_core
= { .value
= 0, .is_set
= 0 },
79 .rlimit_data
= { .value
= 0, .is_set
= 0 },
80 .rlimit_fsize
= { .value
= 0, .is_set
= 0 },
81 .rlimit_nofile
= { .value
= 200, .is_set
= 1 },
82 .rlimit_stack
= { .value
= 0, .is_set
= 0 }
84 , .rlimit_nproc
= { .value
= 0, .is_set
= 0 }
87 , .rlimit_memlock
= { .value
= 0, .is_set
= 1 }
92 pa_daemon_conf
* pa_daemon_conf_new(void) {
94 pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
96 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
, "r")))
99 c
->dl_search_path
= pa_xstrdup(PA_DLSEARCHPATH
);
103 void pa_daemon_conf_free(pa_daemon_conf
*c
) {
105 pa_xfree(c
->script_commands
);
106 pa_xfree(c
->dl_search_path
);
107 pa_xfree(c
->default_script_file
);
108 pa_xfree(c
->config_file
);
112 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
115 if (!strcmp(string
, "auto"))
116 c
->auto_log_target
= 1;
117 else if (!strcmp(string
, "syslog")) {
118 c
->auto_log_target
= 0;
119 c
->log_target
= PA_LOG_SYSLOG
;
120 } else if (!strcmp(string
, "stderr")) {
121 c
->auto_log_target
= 0;
122 c
->log_target
= PA_LOG_STDERR
;
129 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
133 if (pa_atou(string
, &u
) >= 0) {
134 if (u
>= PA_LOG_LEVEL_MAX
)
137 c
->log_level
= (pa_log_level_t
) u
;
138 } else if (pa_startswith(string
, "debug"))
139 c
->log_level
= PA_LOG_DEBUG
;
140 else if (pa_startswith(string
, "info"))
141 c
->log_level
= PA_LOG_INFO
;
142 else if (pa_startswith(string
, "notice"))
143 c
->log_level
= PA_LOG_NOTICE
;
144 else if (pa_startswith(string
, "warn"))
145 c
->log_level
= PA_LOG_WARN
;
146 else if (pa_startswith(string
, "err"))
147 c
->log_level
= PA_LOG_ERROR
;
154 int pa_daemon_conf_set_resample_method(pa_daemon_conf
*c
, const char *string
) {
158 if ((m
= pa_parse_resample_method(string
)) < 0)
161 c
->resample_method
= m
;
165 static int parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
166 pa_daemon_conf
*c
= data
;
167 assert(filename
&& lvalue
&& rvalue
&& data
);
169 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
170 pa_log(__FILE__
": [%s:%u] Invalid log target '%s'.", filename
, line
, rvalue
);
177 static int parse_log_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
178 pa_daemon_conf
*c
= data
;
179 assert(filename
&& lvalue
&& rvalue
&& data
);
181 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
182 pa_log(__FILE__
": [%s:%u] Invalid log level '%s'.", filename
, line
, rvalue
);
189 static int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
190 pa_daemon_conf
*c
= data
;
191 assert(filename
&& lvalue
&& rvalue
&& data
);
193 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
194 pa_log(__FILE__
": [%s:%u] Inavalid resample method '%s'.", filename
, line
, rvalue
);
201 static int parse_rlimit(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
202 #ifdef HAVE_SYS_RESOURCE_H
203 struct pa_rlimit
*r
= data
;
209 if (rvalue
[strspn(rvalue
, "\t ")] == 0) {
215 if (pa_atoi(rvalue
, &k
) < 0) {
216 pa_log(__FILE__
": [%s:%u] Inavalid rlimit '%s'.", filename
, line
, rvalue
);
220 r
->value
= k
>= 0 ? (rlim_t
) k
: 0;
223 pa_log_warn(__FILE__
": [%s:%u] rlimit not supported on this platform.", filename
, line
);
229 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
233 pa_config_item table
[] = {
234 { "daemonize", pa_config_parse_bool
, NULL
},
235 { "fail", pa_config_parse_bool
, NULL
},
236 { "high-priority", pa_config_parse_bool
, NULL
},
237 { "disallow-module-loading", pa_config_parse_bool
, NULL
},
238 { "exit-idle-time", pa_config_parse_int
, NULL
},
239 { "module-idle-time", pa_config_parse_int
, NULL
},
240 { "scache-idle-time", pa_config_parse_int
, NULL
},
241 { "dl-search-path", pa_config_parse_string
, NULL
},
242 { "default-script-file", pa_config_parse_string
, NULL
},
243 { "log-target", parse_log_target
, NULL
},
244 { "log-level", parse_log_level
, NULL
},
245 { "verbose", parse_log_level
, NULL
},
246 { "resample-method", parse_resample_method
, NULL
},
247 { "use-pid-file", pa_config_parse_bool
, NULL
},
248 { "system-instance", pa_config_parse_bool
, NULL
},
249 #ifdef HAVE_SYS_RESOURCE_H
250 { "rlimit-as", parse_rlimit
, NULL
},
251 { "rlimit-core", parse_rlimit
, NULL
},
252 { "rlimit-data", parse_rlimit
, NULL
},
253 { "rlimit-fsize", parse_rlimit
, NULL
},
254 { "rlimit-nofile", parse_rlimit
, NULL
},
255 { "rlimit-stack", parse_rlimit
, NULL
},
257 { "rlimit-nproc", parse_rlimit
, NULL
},
259 #ifdef RLIMIT_MEMLOCK
260 { "rlimit-memlock", parse_rlimit
, NULL
},
263 { NULL
, NULL
, NULL
},
266 table
[0].data
= &c
->daemonize
;
267 table
[1].data
= &c
->fail
;
268 table
[2].data
= &c
->high_priority
;
269 table
[3].data
= &c
->disallow_module_loading
;
270 table
[4].data
= &c
->exit_idle_time
;
271 table
[5].data
= &c
->module_idle_time
;
272 table
[6].data
= &c
->scache_idle_time
;
273 table
[7].data
= &c
->dl_search_path
;
274 table
[8].data
= &c
->default_script_file
;
279 table
[13].data
= &c
->use_pid_file
;
280 table
[14].data
= &c
->system_instance
;
281 #ifdef HAVE_SYS_RESOURCE_H
282 table
[15].data
= &c
->rlimit_as
;
283 table
[16].data
= &c
->rlimit_core
;
284 table
[17].data
= &c
->rlimit_data
;
285 table
[18].data
= &c
->rlimit_fsize
;
286 table
[19].data
= &c
->rlimit_nofile
;
287 table
[20].data
= &c
->rlimit_stack
;
289 table
[21].data
= &c
->rlimit_nproc
;
291 #ifdef RLIMIT_MEMLOCK
293 #error "Houston, we have a numbering problem!"
295 table
[22].data
= &c
->rlimit_memlock
;
300 pa_xfree(c
->config_file
);
301 c
->config_file
= NULL
;
304 fopen(c
->config_file
= pa_xstrdup(filename
), "r") :
305 pa_open_config_file(DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
, ENV_CONFIG_FILE
, &c
->config_file
, "r");
307 if (!f
&& errno
!= ENOENT
) {
308 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s", c
->config_file
, pa_cstrerror(errno
));
312 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
321 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
324 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
325 pa_xfree(c
->dl_search_path
);
326 c
->dl_search_path
= pa_xstrdup(e
);
328 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
329 pa_xfree(c
->default_script_file
);
330 c
->default_script_file
= pa_xstrdup(e
);
336 static const char* const log_level_to_string
[] = {
337 [PA_LOG_DEBUG
] = "debug",
338 [PA_LOG_INFO
] = "info",
339 [PA_LOG_NOTICE
] = "notice",
340 [PA_LOG_WARN
] = "warning",
341 [PA_LOG_ERROR
] = "error"
344 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
345 pa_strbuf
*s
= pa_strbuf_new();
348 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
350 assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
352 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
353 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
354 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
355 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
356 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
357 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
358 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
359 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
360 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
361 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
362 pa_strbuf_printf(s
, "log-level = %s\n", log_level_to_string
[c
->log_level
]);
363 pa_strbuf_printf(s
, "resample-method = %s\n", pa_resample_method_to_string(c
->resample_method
));
364 pa_strbuf_printf(s
, "use-pid-file = %i\n", c
->use_pid_file
);
365 pa_strbuf_printf(s
, "system-instance = %i\n", !!c
->system_instance
);
366 #ifdef HAVE_SYS_RESOURCE_H
367 pa_strbuf_printf(s
, "rlimit-as = %li\n", c
->rlimit_as
.is_set
? (long int) c
->rlimit_as
.value
: -1);
368 pa_strbuf_printf(s
, "rlimit-core = %li\n", c
->rlimit_core
.is_set
? (long int) c
->rlimit_core
.value
: -1);
369 pa_strbuf_printf(s
, "rlimit-data = %li\n", c
->rlimit_data
.is_set
? (long int) c
->rlimit_data
.value
: -1);
370 pa_strbuf_printf(s
, "rlimit-fsize = %li\n", c
->rlimit_fsize
.is_set
? (long int) c
->rlimit_fsize
.value
: -1);
371 pa_strbuf_printf(s
, "rlimit-nofile = %li\n", c
->rlimit_nofile
.is_set
? (long int) c
->rlimit_nofile
.value
: -1);
372 pa_strbuf_printf(s
, "rlimit-stack = %li\n", c
->rlimit_stack
.is_set
? (long int) c
->rlimit_stack
.value
: -1);
374 pa_strbuf_printf(s
, "rlimit-nproc = %li\n", c
->rlimit_nproc
.is_set
? (long int) c
->rlimit_nproc
.value
: -1);
376 #ifdef RLIMIT_MEMLOCK
377 pa_strbuf_printf(s
, "rlimit-memlock = %li\n", c
->rlimit_memlock
.is_set
? (long int) c
->rlimit_memlock
.value
: -1);
381 return pa_strbuf_tostring_free(s
);