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