]> code.delx.au - pulseaudio/blob - src/daemon/daemon-conf.c
4d458a5c09c30d5645a5e61cfa7aa1fc39da20a7
[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 DEFAULT_CONFIG_DIR
43 # ifndef OS_IS_WIN32
44 # define DEFAULT_CONFIG_DIR "/etc/pulse"
45 # else
46 # define DEFAULT_CONFIG_DIR "%POLYP_ROOT%"
47 # endif
48 #endif
49
50 #ifndef OS_IS_WIN32
51 # define PATH_SEP "/"
52 #else
53 # define PATH_SEP "\\"
54 #endif
55
56 #define DEFAULT_SCRIPT_FILE DEFAULT_CONFIG_DIR PATH_SEP "default.pa"
57 #define DEFAULT_SCRIPT_FILE_USER ".pulse" PATH_SEP "default.pa"
58 #define DEFAULT_CONFIG_FILE DEFAULT_CONFIG_DIR PATH_SEP "daemon.conf"
59 #define DEFAULT_CONFIG_FILE_USER ".pulse" PATH_SEP "daemon.conf"
60
61 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
62 #define ENV_CONFIG_FILE "POLYP_CONFIG"
63 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
64
65 static const pa_daemon_conf default_conf = {
66 .cmd = PA_CMD_DAEMON,
67 .daemonize = 0,
68 .fail = 1,
69 .high_priority = 0,
70 .disallow_module_loading = 0,
71 .exit_idle_time = -1,
72 .module_idle_time = 20,
73 .scache_idle_time = 20,
74 .auto_log_target = 1,
75 .script_commands = NULL,
76 .dl_search_path = NULL,
77 .default_script_file = NULL,
78 .log_target = PA_LOG_SYSLOG,
79 .log_level = PA_LOG_NOTICE,
80 .resample_method = PA_RESAMPLER_SRC_SINC_FASTEST,
81 .config_file = NULL,
82 .use_pid_file = 1
83 };
84
85 pa_daemon_conf* pa_daemon_conf_new(void) {
86 FILE *f;
87 pa_daemon_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
88
89 if ((f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file, "r")))
90 fclose(f);
91
92 #ifdef DLSEARCHPATH
93 c->dl_search_path = pa_xstrdup(DLSEARCHPATH);
94 #endif
95 return c;
96 }
97
98 void pa_daemon_conf_free(pa_daemon_conf *c) {
99 assert(c);
100 pa_xfree(c->script_commands);
101 pa_xfree(c->dl_search_path);
102 pa_xfree(c->default_script_file);
103 pa_xfree(c->config_file);
104 pa_xfree(c);
105 }
106
107 int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
108 assert(c && string);
109
110 if (!strcmp(string, "auto"))
111 c->auto_log_target = 1;
112 else if (!strcmp(string, "syslog")) {
113 c->auto_log_target = 0;
114 c->log_target = PA_LOG_SYSLOG;
115 } else if (!strcmp(string, "stderr")) {
116 c->auto_log_target = 0;
117 c->log_target = PA_LOG_STDERR;
118 } else
119 return -1;
120
121 return 0;
122 }
123
124 int pa_daemon_conf_set_log_level(pa_daemon_conf *c, const char *string) {
125 uint32_t u;
126 assert(c && string);
127
128 if (pa_atou(string, &u) >= 0) {
129 if (u >= PA_LOG_LEVEL_MAX)
130 return -1;
131
132 c->log_level = (pa_log_level_t) u;
133 } else if (pa_startswith(string, "debug"))
134 c->log_level = PA_LOG_DEBUG;
135 else if (pa_startswith(string, "info"))
136 c->log_level = PA_LOG_INFO;
137 else if (pa_startswith(string, "notice"))
138 c->log_level = PA_LOG_NOTICE;
139 else if (pa_startswith(string, "warn"))
140 c->log_level = PA_LOG_WARN;
141 else if (pa_startswith(string, "err"))
142 c->log_level = PA_LOG_ERROR;
143 else
144 return -1;
145
146 return 0;
147 }
148
149 int pa_daemon_conf_set_resample_method(pa_daemon_conf *c, const char *string) {
150 int m;
151 assert(c && string);
152
153 if ((m = pa_parse_resample_method(string)) < 0)
154 return -1;
155
156 c->resample_method = m;
157 return 0;
158 }
159
160 static int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
161 pa_daemon_conf *c = data;
162 assert(filename && lvalue && rvalue && data);
163
164 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
165 pa_log(__FILE__": [%s:%u] Invalid log target '%s'.", filename, line, rvalue);
166 return -1;
167 }
168
169 return 0;
170 }
171
172 static int parse_log_level(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
173 pa_daemon_conf *c = data;
174 assert(filename && lvalue && rvalue && data);
175
176 if (pa_daemon_conf_set_log_level(c, rvalue) < 0) {
177 pa_log(__FILE__": [%s:%u] Invalid log level '%s'.", filename, line, rvalue);
178 return -1;
179 }
180
181 return 0;
182 }
183
184 static int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, PA_GCC_UNUSED void *userdata) {
185 pa_daemon_conf *c = data;
186 assert(filename && lvalue && rvalue && data);
187
188 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
189 pa_log(__FILE__": [%s:%u] Inavalid resample method '%s'.", filename, line, rvalue);
190 return -1;
191 }
192
193 return 0;
194 }
195
196 int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
197 int r = -1;
198 FILE *f = NULL;
199
200 pa_config_item table[] = {
201 { "daemonize", pa_config_parse_bool, NULL },
202 { "fail", pa_config_parse_bool, NULL },
203 { "high-priority", pa_config_parse_bool, NULL },
204 { "disallow-module-loading", pa_config_parse_bool, NULL },
205 { "exit-idle-time", pa_config_parse_int, NULL },
206 { "module-idle-time", pa_config_parse_int, NULL },
207 { "scache-idle-time", pa_config_parse_int, NULL },
208 { "dl-search-path", pa_config_parse_string, NULL },
209 { "default-script-file", pa_config_parse_string, NULL },
210 { "log-target", parse_log_target, NULL },
211 { "log-level", parse_log_level, NULL },
212 { "verbose", parse_log_level, NULL },
213 { "resample-method", parse_resample_method, NULL },
214 { "use-pid-file", pa_config_parse_bool, NULL },
215 { NULL, NULL, NULL },
216 };
217
218 table[0].data = &c->daemonize;
219 table[1].data = &c->fail;
220 table[2].data = &c->high_priority;
221 table[3].data = &c->disallow_module_loading;
222 table[4].data = &c->exit_idle_time;
223 table[5].data = &c->module_idle_time;
224 table[6].data = &c->scache_idle_time;
225 table[7].data = &c->dl_search_path;
226 table[8].data = &c->default_script_file;
227 table[9].data = c;
228 table[10].data = c;
229 table[11].data = c;
230 table[12].data = c;
231 table[13].data = &c->use_pid_file;
232
233 pa_xfree(c->config_file);
234 c->config_file = NULL;
235
236 f = filename ?
237 fopen(c->config_file = pa_xstrdup(filename), "r") :
238 pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file, "r");
239
240 if (!f && errno != ENOENT) {
241 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
242 goto finish;
243 }
244
245 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
246
247 finish:
248 if (f)
249 fclose(f);
250
251 return r;
252 }
253
254 int pa_daemon_conf_env(pa_daemon_conf *c) {
255 char *e;
256
257 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
258 pa_xfree(c->dl_search_path);
259 c->dl_search_path = pa_xstrdup(e);
260 }
261 if ((e = getenv(ENV_SCRIPT_FILE))) {
262 pa_xfree(c->default_script_file);
263 c->default_script_file = pa_xstrdup(e);
264 }
265
266 return 0;
267 }
268
269 static const char* const log_level_to_string[] = {
270 [PA_LOG_DEBUG] = "debug",
271 [PA_LOG_INFO] = "info",
272 [PA_LOG_NOTICE] = "notice",
273 [PA_LOG_WARN] = "warning",
274 [PA_LOG_ERROR] = "error"
275 };
276
277 char *pa_daemon_conf_dump(pa_daemon_conf *c) {
278 pa_strbuf *s = pa_strbuf_new();
279
280 if (c->config_file)
281 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
282
283 assert(c->log_level <= PA_LOG_LEVEL_MAX);
284
285 pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
286 pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
287 pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
288 pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
289 pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
290 pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
291 pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
292 pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
293 pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
294 pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
295 pa_strbuf_printf(s, "log-level = %s\n", log_level_to_string[c->log_level]);
296 pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
297 pa_strbuf_printf(s, "use-pid-file = %i\n", c->use_pid_file);
298
299 return pa_strbuf_tostring_free(s);
300 }