]> code.delx.au - pulseaudio/blob - src/daemon/daemon-conf.c
remove all occurences of
[pulseaudio] / src / daemon / daemon-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <unistd.h>
31
32 #include <pulse/xmalloc.h>
33
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>
39
40 #include "daemon-conf.h"
41
42 #ifndef OS_IS_WIN32
43 # define PATH_SEP "/"
44 #else
45 # define PATH_SEP "\\"
46 #endif
47
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"
52
53 #define ENV_SCRIPT_FILE "PULSE_SCRIPT"
54 #define ENV_CONFIG_FILE "PULSE_CONFIG"
55 #define ENV_DL_SEARCH_PATH "PULSE_DLPATH"
56
57 static const pa_daemon_conf default_conf = {
58 .cmd = PA_CMD_DAEMON,
59 .daemonize = 0,
60 .fail = 1,
61 .high_priority = 0,
62 .disallow_module_loading = 0,
63 .exit_idle_time = -1,
64 .module_idle_time = 20,
65 .scache_idle_time = 20,
66 .auto_log_target = 1,
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,
73 .config_file = NULL,
74 .use_pid_file = 1,
75 .system_instance = 0,
76 .no_cpu_limit = 0
77 #ifdef HAVE_SYS_RESOURCE_H
78 , .rlimit_as = { .value = 0, .is_set = 0 },
79 .rlimit_core = { .value = 0, .is_set = 0 },
80 .rlimit_data = { .value = 0, .is_set = 0 },
81 .rlimit_fsize = { .value = 0, .is_set = 0 },
82 .rlimit_nofile = { .value = 200, .is_set = 1 },
83 .rlimit_stack = { .value = 0, .is_set = 0 }
84 #ifdef RLIMIT_NPROC
85 , .rlimit_nproc = { .value = 0, .is_set = 0 }
86 #endif
87 #ifdef RLIMIT_MEMLOCK
88 , .rlimit_memlock = { .value = 0, .is_set = 1 }
89 #endif
90 #endif
91 };
92
93 pa_daemon_conf* pa_daemon_conf_new(void) {
94 FILE *f;
95 pa_daemon_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
96
97 if ((f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file, "r")))
98 fclose(f);
99
100 c->dl_search_path = pa_xstrdup(PA_DLSEARCHPATH);
101 return c;
102 }
103
104 void pa_daemon_conf_free(pa_daemon_conf *c) {
105 assert(c);
106 pa_xfree(c->script_commands);
107 pa_xfree(c->dl_search_path);
108 pa_xfree(c->default_script_file);
109 pa_xfree(c->config_file);
110 pa_xfree(c);
111 }
112
113 int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
114 assert(c && string);
115
116 if (!strcmp(string, "auto"))
117 c->auto_log_target = 1;
118 else if (!strcmp(string, "syslog")) {
119 c->auto_log_target = 0;
120 c->log_target = PA_LOG_SYSLOG;
121 } else if (!strcmp(string, "stderr")) {
122 c->auto_log_target = 0;
123 c->log_target = PA_LOG_STDERR;
124 } else
125 return -1;
126
127 return 0;
128 }
129
130 int pa_daemon_conf_set_log_level(pa_daemon_conf *c, const char *string) {
131 uint32_t u;
132 assert(c && string);
133
134 if (pa_atou(string, &u) >= 0) {
135 if (u >= PA_LOG_LEVEL_MAX)
136 return -1;
137
138 c->log_level = (pa_log_level_t) u;
139 } else if (pa_startswith(string, "debug"))
140 c->log_level = PA_LOG_DEBUG;
141 else if (pa_startswith(string, "info"))
142 c->log_level = PA_LOG_INFO;
143 else if (pa_startswith(string, "notice"))
144 c->log_level = PA_LOG_NOTICE;
145 else if (pa_startswith(string, "warn"))
146 c->log_level = PA_LOG_WARN;
147 else if (pa_startswith(string, "err"))
148 c->log_level = PA_LOG_ERROR;
149 else
150 return -1;
151
152 return 0;
153 }
154
155 int pa_daemon_conf_set_resample_method(pa_daemon_conf *c, const char *string) {
156 int m;
157 assert(c && string);
158
159 if ((m = pa_parse_resample_method(string)) < 0)
160 return -1;
161
162 c->resample_method = m;
163 return 0;
164 }
165
166 static int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
167 pa_daemon_conf *c = data;
168 assert(filename && lvalue && rvalue && data);
169
170 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
171 pa_log("[%s:%u] Invalid log target '%s'.", filename, line, rvalue);
172 return -1;
173 }
174
175 return 0;
176 }
177
178 static int parse_log_level(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
179 pa_daemon_conf *c = data;
180 assert(filename && lvalue && rvalue && data);
181
182 if (pa_daemon_conf_set_log_level(c, rvalue) < 0) {
183 pa_log("[%s:%u] Invalid log level '%s'.", filename, line, rvalue);
184 return -1;
185 }
186
187 return 0;
188 }
189
190 static int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
191 pa_daemon_conf *c = data;
192 assert(filename && lvalue && rvalue && data);
193
194 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
195 pa_log("[%s:%u] Inavalid resample method '%s'.", filename, line, rvalue);
196 return -1;
197 }
198
199 return 0;
200 }
201
202 static int parse_rlimit(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
203 #ifdef HAVE_SYS_RESOURCE_H
204 struct pa_rlimit *r = data;
205 assert(filename);
206 assert(lvalue);
207 assert(rvalue);
208 assert(r);
209
210 if (rvalue[strspn(rvalue, "\t ")] == 0) {
211 /* Empty string */
212 r->is_set = 0;
213 r->value = 0;
214 } else {
215 int32_t k;
216 if (pa_atoi(rvalue, &k) < 0) {
217 pa_log("[%s:%u] Inavalid rlimit '%s'.", filename, line, rvalue);
218 return -1;
219 }
220 r->is_set = k >= 0;
221 r->value = k >= 0 ? (rlim_t) k : 0;
222 }
223 #else
224 pa_log_warn("[%s:%u] rlimit not supported on this platform.", filename, line);
225 #endif
226
227 return 0;
228 }
229
230 int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
231 int r = -1;
232 FILE *f = NULL;
233
234 pa_config_item table[] = {
235 { "daemonize", pa_config_parse_bool, NULL },
236 { "fail", pa_config_parse_bool, NULL },
237 { "high-priority", pa_config_parse_bool, NULL },
238 { "disallow-module-loading", pa_config_parse_bool, NULL },
239 { "exit-idle-time", pa_config_parse_int, NULL },
240 { "module-idle-time", pa_config_parse_int, NULL },
241 { "scache-idle-time", pa_config_parse_int, NULL },
242 { "dl-search-path", pa_config_parse_string, NULL },
243 { "default-script-file", pa_config_parse_string, NULL },
244 { "log-target", parse_log_target, NULL },
245 { "log-level", parse_log_level, NULL },
246 { "verbose", parse_log_level, NULL },
247 { "resample-method", parse_resample_method, NULL },
248 { "use-pid-file", pa_config_parse_bool, NULL },
249 { "system-instance", pa_config_parse_bool, NULL },
250 { "no-cpu-limit", pa_config_parse_bool, NULL },
251 #ifdef HAVE_SYS_RESOURCE_H
252 { "rlimit-as", parse_rlimit, NULL },
253 { "rlimit-core", parse_rlimit, NULL },
254 { "rlimit-data", parse_rlimit, NULL },
255 { "rlimit-fsize", parse_rlimit, NULL },
256 { "rlimit-nofile", parse_rlimit, NULL },
257 { "rlimit-stack", parse_rlimit, NULL },
258 #ifdef RLIMIT_NPROC
259 { "rlimit-nproc", parse_rlimit, NULL },
260 #endif
261 #ifdef RLIMIT_MEMLOCK
262 { "rlimit-memlock", parse_rlimit, NULL },
263 #endif
264 #endif
265 { NULL, NULL, NULL },
266 };
267
268 table[0].data = &c->daemonize;
269 table[1].data = &c->fail;
270 table[2].data = &c->high_priority;
271 table[3].data = &c->disallow_module_loading;
272 table[4].data = &c->exit_idle_time;
273 table[5].data = &c->module_idle_time;
274 table[6].data = &c->scache_idle_time;
275 table[7].data = &c->dl_search_path;
276 table[8].data = &c->default_script_file;
277 table[9].data = c;
278 table[10].data = c;
279 table[11].data = c;
280 table[12].data = c;
281 table[13].data = &c->use_pid_file;
282 table[14].data = &c->system_instance;
283 table[15].data = &c->no_cpu_limit;
284 #ifdef HAVE_SYS_RESOURCE_H
285 table[16].data = &c->rlimit_as;
286 table[17].data = &c->rlimit_core;
287 table[18].data = &c->rlimit_data;
288 table[19].data = &c->rlimit_fsize;
289 table[20].data = &c->rlimit_nofile;
290 table[21].data = &c->rlimit_stack;
291 #ifdef RLIMIT_NPROC
292 table[22].data = &c->rlimit_nproc;
293 #endif
294 #ifdef RLIMIT_MEMLOCK
295 #ifndef RLIMIT_NPROC
296 #error "Houston, we have a numbering problem!"
297 #endif
298 table[23].data = &c->rlimit_memlock;
299 #endif
300 #endif
301
302
303 pa_xfree(c->config_file);
304 c->config_file = NULL;
305
306 f = filename ?
307 fopen(c->config_file = pa_xstrdup(filename), "r") :
308 pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file, "r");
309
310 if (!f && errno != ENOENT) {
311 pa_log("WARNING: failed to open configuration file '%s': %s", c->config_file, pa_cstrerror(errno));
312 goto finish;
313 }
314
315 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
316
317 finish:
318 if (f)
319 fclose(f);
320
321 return r;
322 }
323
324 int pa_daemon_conf_env(pa_daemon_conf *c) {
325 char *e;
326
327 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
328 pa_xfree(c->dl_search_path);
329 c->dl_search_path = pa_xstrdup(e);
330 }
331 if ((e = getenv(ENV_SCRIPT_FILE))) {
332 pa_xfree(c->default_script_file);
333 c->default_script_file = pa_xstrdup(e);
334 }
335
336 return 0;
337 }
338
339 static const char* const log_level_to_string[] = {
340 [PA_LOG_DEBUG] = "debug",
341 [PA_LOG_INFO] = "info",
342 [PA_LOG_NOTICE] = "notice",
343 [PA_LOG_WARN] = "warning",
344 [PA_LOG_ERROR] = "error"
345 };
346
347 char *pa_daemon_conf_dump(pa_daemon_conf *c) {
348 pa_strbuf *s = pa_strbuf_new();
349
350 if (c->config_file)
351 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
352
353 assert(c->log_level <= PA_LOG_LEVEL_MAX);
354
355 pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
356 pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
357 pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
358 pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
359 pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
360 pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
361 pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
362 pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
363 pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
364 pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
365 pa_strbuf_printf(s, "log-level = %s\n", log_level_to_string[c->log_level]);
366 pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
367 pa_strbuf_printf(s, "use-pid-file = %i\n", c->use_pid_file);
368 pa_strbuf_printf(s, "system-instance = %i\n", !!c->system_instance);
369 pa_strbuf_printf(s, "no-cpu-limit = %i\n", !!c->no_cpu_limit);
370 #ifdef HAVE_SYS_RESOURCE_H
371 pa_strbuf_printf(s, "rlimit-as = %li\n", c->rlimit_as.is_set ? (long int) c->rlimit_as.value : -1);
372 pa_strbuf_printf(s, "rlimit-core = %li\n", c->rlimit_core.is_set ? (long int) c->rlimit_core.value : -1);
373 pa_strbuf_printf(s, "rlimit-data = %li\n", c->rlimit_data.is_set ? (long int) c->rlimit_data.value : -1);
374 pa_strbuf_printf(s, "rlimit-fsize = %li\n", c->rlimit_fsize.is_set ? (long int) c->rlimit_fsize.value : -1);
375 pa_strbuf_printf(s, "rlimit-nofile = %li\n", c->rlimit_nofile.is_set ? (long int) c->rlimit_nofile.value : -1);
376 pa_strbuf_printf(s, "rlimit-stack = %li\n", c->rlimit_stack.is_set ? (long int) c->rlimit_stack.value : -1);
377 #ifdef RLIMIT_NPROC
378 pa_strbuf_printf(s, "rlimit-nproc = %li\n", c->rlimit_nproc.is_set ? (long int) c->rlimit_nproc.value : -1);
379 #endif
380 #ifdef RLIMIT_MEMLOCK
381 pa_strbuf_printf(s, "rlimit-memlock = %li\n", c->rlimit_memlock.is_set ? (long int) c->rlimit_memlock.value : -1);
382 #endif
383 #endif
384
385 return pa_strbuf_tostring_free(s);
386 }