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