]> code.delx.au - pulseaudio/blob - src/daemon/daemon-conf.c
8cab327f83c285bb79f9f1189443f886052aa6db
[pulseaudio] / src / daemon / daemon-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <unistd.h>
34
35 #include <pulse/xmalloc.h>
36
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
43 #include "daemon-conf.h"
44
45 #ifndef OS_IS_WIN32
46 # define PATH_SEP "/"
47 #else
48 # define PATH_SEP "\\"
49 #endif
50
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"
55
56 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
57 #define ENV_CONFIG_FILE "PULSE_CONFIG"
58 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
59
60 static const pa_daemon_conf default_conf = {
61 .cmd = PA_CMD_DAEMON,
62 .daemonize = 0,
63 .fail = 1,
64 .high_priority = 0,
65 .disallow_module_loading = 0,
66 .exit_idle_time = -1,
67 .module_idle_time = 20,
68 .scache_idle_time = 20,
69 .auto_log_target = 1,
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,
76 .config_file = NULL,
77 .use_pid_file = 1,
78 .system_instance = 0,
79 .no_cpu_limit = 0,
80 .disable_shm = 0
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 }
88 #ifdef RLIMIT_NPROC
89 , .rlimit_nproc = { .value = 0, .is_set = 0 }
90 #endif
91 #ifdef RLIMIT_MEMLOCK
92 , .rlimit_memlock = { .value = 0, .is_set = 1 }
93 #endif
94 #endif
95 };
96
97 pa_daemon_conf* pa_daemon_conf_new(void) {
98 FILE *f;
99 pa_daemon_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
100
101 if ((f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file, "r")))
102 fclose(f);
103
104 c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
105 return c;
106 }
107
108 void pa_daemon_conf_free(pa_daemon_conf *c) {
109 assert(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);
114 pa_xfree(c);
115 }
116
117 int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
118 assert(c && string);
119
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;
128 } else
129 return -1;
130
131 return 0;
132 }
133
134 int pa_daemon_conf_set_log_level(pa_daemon_conf *c, const char *string) {
135 uint32_t u;
136 assert(c && string);
137
138 if (pa_atou(string, &u) >= 0) {
139 if (u >= PA_LOG_LEVEL_MAX)
140 return -1;
141
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;
153 else
154 return -1;
155
156 return 0;
157 }
158
159 int pa_daemon_conf_set_resample_method(pa_daemon_conf *c, const char *string) {
160 int m;
161 assert(c && string);
162
163 if ((m = pa_parse_resample_method(string)) < 0)
164 return -1;
165
166 c->resample_method = m;
167 return 0;
168 }
169
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);
173
174 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
175 pa_log("[%s:%u] Invalid log target '%s'.", filename, line, rvalue);
176 return -1;
177 }
178
179 return 0;
180 }
181
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);
185
186 if (pa_daemon_conf_set_log_level(c, rvalue) < 0) {
187 pa_log("[%s:%u] Invalid log level '%s'.", filename, line, rvalue);
188 return -1;
189 }
190
191 return 0;
192 }
193
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);
197
198 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
199 pa_log("[%s:%u] Inavalid resample method '%s'.", filename, line, rvalue);
200 return -1;
201 }
202
203 return 0;
204 }
205
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;
209 assert(filename);
210 assert(lvalue);
211 assert(rvalue);
212 assert(r);
213
214 if (rvalue[strspn(rvalue, "\t ")] == 0) {
215 /* Empty string */
216 r->is_set = 0;
217 r->value = 0;
218 } else {
219 int32_t k;
220 if (pa_atoi(rvalue, &k) < 0) {
221 pa_log("[%s:%u] Inavalid rlimit '%s'.", filename, line, rvalue);
222 return -1;
223 }
224 r->is_set = k >= 0;
225 r->value = k >= 0 ? (rlim_t) k : 0;
226 }
227 #else
228 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename, line);
229 #endif
230
231 return 0;
232 }
233
234 int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
235 int r = -1;
236 FILE *f = NULL;
237
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 },
263 #ifdef RLIMIT_NPROC
264 { "rlimit-nproc", parse_rlimit, NULL },
265 #endif
266 #ifdef RLIMIT_MEMLOCK
267 { "rlimit-memlock", parse_rlimit, NULL },
268 #endif
269 #endif
270 { NULL, NULL, NULL },
271 };
272
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;
282 table[9].data = c;
283 table[10].data = c;
284 table[11].data = c;
285 table[12].data = c;
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;
297 #ifdef RLIMIT_NPROC
298 table[23].data = &c->rlimit_nproc;
299 #endif
300 #ifdef RLIMIT_MEMLOCK
301 #ifndef RLIMIT_NPROC
302 #error "Houston, we have a numbering problem!"
303 #endif
304 table[24].data = &c->rlimit_memlock;
305 #endif
306 #endif
307
308
309 pa_xfree(c->config_file);
310 c->config_file = NULL;
311
312 f = filename ?
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");
315
316 if (!f && errno != ENOENT) {
317 pa_log("WARNING: failed to open configuration file '%s': %s", c->config_file, pa_cstrerror(errno));
318 goto finish;
319 }
320
321 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
322
323 finish:
324 if (f)
325 fclose(f);
326
327 return r;
328 }
329
330 int pa_daemon_conf_env(pa_daemon_conf *c) {
331 char *e;
332
333 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
334 pa_xfree(c->dl_search_path);
335 c->dl_search_path = pa_xstrdup(e);
336 }
337 if ((e = getenv(ENV_SCRIPT_FILE))) {
338 pa_xfree(c->default_script_file);
339 c->default_script_file = pa_xstrdup(e);
340 }
341
342 return 0;
343 }
344
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"
351 };
352
353 char *pa_daemon_conf_dump(pa_daemon_conf *c) {
354 pa_strbuf *s = pa_strbuf_new();
355
356 if (c->config_file)
357 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
358
359 assert(c->log_level <= PA_LOG_LEVEL_MAX);
360
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);
384 #ifdef RLIMIT_NPROC
385 pa_strbuf_printf(s, "rlimit-nproc = %li\n", c->rlimit_nproc.is_set ? (long int) c->rlimit_nproc.value : -1);
386 #endif
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);
389 #endif
390 #endif
391
392 return pa_strbuf_tostring_free(s);
393 }