4 This file is part of polypaudio.
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.
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.
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
32 #include "daemon-conf.h"
36 #include "conf-parser.h"
37 #include "resampler.h"
39 #ifndef DEFAULT_CONFIG_DIR
40 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
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"
48 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
49 #define ENV_CONFIG_FILE "POLYP_CONFIG"
50 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
52 static const struct pa_daemon_conf default_conf
= {
57 .disallow_module_loading
= 0,
59 .module_idle_time
= 20,
60 .scache_idle_time
= 20,
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
,
72 struct pa_daemon_conf
* pa_daemon_conf_new(void) {
74 struct pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
76 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
)))
80 c
->dl_search_path
= pa_xstrdup(DLSEARCHPATH
);
85 void pa_daemon_conf_free(struct pa_daemon_conf
*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
);
94 int pa_daemon_conf_set_log_target(struct pa_daemon_conf
*c
, const char *string
) {
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
;
111 int pa_daemon_conf_set_log_level(struct pa_daemon_conf
*c
, const char *string
) {
115 if (pa_atou(string
, &u
) >= 0) {
116 if (u
>= PA_LOG_LEVEL_MAX
)
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
;
136 int pa_daemon_conf_set_resample_method(struct pa_daemon_conf
*c
, const char *string
) {
140 if ((m
= pa_parse_resample_method(string
)) < 0)
143 c
->resample_method
= m
;
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
);
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
);
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
);
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
);
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
);
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
);
183 int pa_daemon_conf_load(struct pa_daemon_conf
*c
, const char *filename
) {
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
},
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
;
218 table
[13].data
= &c
->use_pid_file
;
220 pa_xfree(c
->config_file
);
221 c
->config_file
= NULL
;
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
);
227 if (!f
&& errno
!= ENOENT
) {
228 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s\n", filename
, strerror(errno
));
232 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
241 int pa_daemon_conf_env(struct pa_daemon_conf
*c
) {
244 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
245 pa_xfree(c
->dl_search_path
);
246 c
->dl_search_path
= pa_xstrdup(e
);
248 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
249 pa_xfree(c
->default_script_file
);
250 c
->default_script_file
= pa_xstrdup(e
);
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"
264 char *pa_daemon_conf_dump(struct pa_daemon_conf
*c
) {
265 struct pa_strbuf
*s
= pa_strbuf_new();
268 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
270 assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
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
);
286 return pa_strbuf_tostring_free(s
);