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
41 # define DEFAULT_CONFIG_DIR "/etc/polypaudio"
43 # define DEFAULT_CONFIG_DIR "%POLYP_ROOT%"
50 # define PATH_SEP "\\"
53 #define DEFAULT_SCRIPT_FILE DEFAULT_CONFIG_DIR PATH_SEP "default.pa"
54 #define DEFAULT_SCRIPT_FILE_USER ".polypaudio" PATH_SEP "default.pa"
55 #define DEFAULT_CONFIG_FILE DEFAULT_CONFIG_DIR PATH_SEP "daemon.conf"
56 #define DEFAULT_CONFIG_FILE_USER ".polypaudio" PATH_SEP "daemon.conf"
58 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
59 #define ENV_CONFIG_FILE "POLYP_CONFIG"
60 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
62 static const pa_daemon_conf default_conf
= {
67 .disallow_module_loading
= 0,
69 .module_idle_time
= 20,
70 .scache_idle_time
= 20,
72 .script_commands
= NULL
,
73 .dl_search_path
= NULL
,
74 .default_script_file
= NULL
,
75 .log_target
= PA_LOG_SYSLOG
,
76 .log_level
= PA_LOG_NOTICE
,
77 .resample_method
= PA_RESAMPLER_SRC_SINC_FASTEST
,
82 pa_daemon_conf
* pa_daemon_conf_new(void) {
84 pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
86 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
)))
90 c
->dl_search_path
= pa_xstrdup(DLSEARCHPATH
);
95 void pa_daemon_conf_free(pa_daemon_conf
*c
) {
97 pa_xfree(c
->script_commands
);
98 pa_xfree(c
->dl_search_path
);
99 pa_xfree(c
->default_script_file
);
100 pa_xfree(c
->config_file
);
104 int pa_daemon_conf_set_log_target(pa_daemon_conf
*c
, const char *string
) {
107 if (!strcmp(string
, "auto"))
108 c
->auto_log_target
= 1;
109 else if (!strcmp(string
, "syslog")) {
110 c
->auto_log_target
= 0;
111 c
->log_target
= PA_LOG_SYSLOG
;
112 } else if (!strcmp(string
, "stderr")) {
113 c
->auto_log_target
= 0;
114 c
->log_target
= PA_LOG_STDERR
;
121 int pa_daemon_conf_set_log_level(pa_daemon_conf
*c
, const char *string
) {
125 if (pa_atou(string
, &u
) >= 0) {
126 if (u
>= PA_LOG_LEVEL_MAX
)
129 c
->log_level
= (pa_log_level_t
) u
;
130 } else if (pa_startswith(string
, "debug"))
131 c
->log_level
= PA_LOG_DEBUG
;
132 else if (pa_startswith(string
, "info"))
133 c
->log_level
= PA_LOG_INFO
;
134 else if (pa_startswith(string
, "notice"))
135 c
->log_level
= PA_LOG_NOTICE
;
136 else if (pa_startswith(string
, "warn"))
137 c
->log_level
= PA_LOG_WARN
;
138 else if (pa_startswith(string
, "err"))
139 c
->log_level
= PA_LOG_ERROR
;
146 int pa_daemon_conf_set_resample_method(pa_daemon_conf
*c
, const char *string
) {
150 if ((m
= pa_parse_resample_method(string
)) < 0)
153 c
->resample_method
= m
;
157 static int parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
158 pa_daemon_conf
*c
= data
;
159 assert(filename
&& lvalue
&& rvalue
&& data
);
161 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
162 pa_log(__FILE__
": [%s:%u] Invalid log target '%s'.\n", filename
, line
, rvalue
);
169 static int parse_log_level(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
170 pa_daemon_conf
*c
= data
;
171 assert(filename
&& lvalue
&& rvalue
&& data
);
173 if (pa_daemon_conf_set_log_level(c
, rvalue
) < 0) {
174 pa_log(__FILE__
": [%s:%u] Invalid log level '%s'.\n", filename
, line
, rvalue
);
181 static int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, PA_GCC_UNUSED
void *userdata
) {
182 pa_daemon_conf
*c
= data
;
183 assert(filename
&& lvalue
&& rvalue
&& data
);
185 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
186 pa_log(__FILE__
": [%s:%u] Inavalid resample method '%s'.\n", filename
, line
, rvalue
);
193 int pa_daemon_conf_load(pa_daemon_conf
*c
, const char *filename
) {
197 pa_config_item table
[] = {
198 { "daemonize", pa_config_parse_bool
, NULL
},
199 { "fail", pa_config_parse_bool
, NULL
},
200 { "high-priority", pa_config_parse_bool
, NULL
},
201 { "disallow-module-loading", pa_config_parse_bool
, NULL
},
202 { "exit-idle-time", pa_config_parse_int
, NULL
},
203 { "module-idle-time", pa_config_parse_int
, NULL
},
204 { "scache-idle-time", pa_config_parse_int
, NULL
},
205 { "dl-search-path", pa_config_parse_string
, NULL
},
206 { "default-script-file", pa_config_parse_string
, NULL
},
207 { "log-target", parse_log_target
, NULL
},
208 { "log-level", parse_log_level
, NULL
},
209 { "verbose", parse_log_level
, NULL
},
210 { "resample-method", parse_resample_method
, NULL
},
211 { "use-pid-file", pa_config_parse_bool
, NULL
},
212 { NULL
, NULL
, NULL
},
215 table
[0].data
= &c
->daemonize
;
216 table
[1].data
= &c
->fail
;
217 table
[2].data
= &c
->high_priority
;
218 table
[3].data
= &c
->disallow_module_loading
;
219 table
[4].data
= &c
->exit_idle_time
;
220 table
[5].data
= &c
->module_idle_time
;
221 table
[6].data
= &c
->scache_idle_time
;
222 table
[7].data
= &c
->dl_search_path
;
223 table
[8].data
= &c
->default_script_file
;
228 table
[13].data
= &c
->use_pid_file
;
230 pa_xfree(c
->config_file
);
231 c
->config_file
= NULL
;
234 fopen(c
->config_file
= pa_xstrdup(filename
), "r") :
235 pa_open_config_file(DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
, ENV_CONFIG_FILE
, &c
->config_file
);
237 if (!f
&& errno
!= ENOENT
) {
238 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s\n", filename
, strerror(errno
));
242 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
251 int pa_daemon_conf_env(pa_daemon_conf
*c
) {
254 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
255 pa_xfree(c
->dl_search_path
);
256 c
->dl_search_path
= pa_xstrdup(e
);
258 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
259 pa_xfree(c
->default_script_file
);
260 c
->default_script_file
= pa_xstrdup(e
);
266 static const char* const log_level_to_string
[] = {
267 [PA_LOG_DEBUG
] = "debug",
268 [PA_LOG_INFO
] = "info",
269 [PA_LOG_NOTICE
] = "notice",
270 [PA_LOG_WARN
] = "warning",
271 [PA_LOG_ERROR
] = "error"
274 char *pa_daemon_conf_dump(pa_daemon_conf
*c
) {
275 pa_strbuf
*s
= pa_strbuf_new();
278 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
280 assert(c
->log_level
<= PA_LOG_LEVEL_MAX
);
282 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
283 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
284 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
285 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
286 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
287 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
288 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
289 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
290 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
291 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
292 pa_strbuf_printf(s
, "log-level = %s\n", log_level_to_string
[c
->log_level
]);
293 pa_strbuf_printf(s
, "resample-method = %s\n", pa_resample_method_to_string(c
->resample_method
));
294 pa_strbuf_printf(s
, "use-pid-file = %i\n", c
->use_pid_file
);
296 return pa_strbuf_tostring_free(s
);