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