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