]> code.delx.au - pulseaudio/blob - polyp/daemon-conf.c
* remove autospawn lock file usage
[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 .verbose = 0,
57 .high_priority = 0,
58 .disallow_module_loading = 0,
59 .exit_idle_time = -1,
60 .module_idle_time = 20,
61 .scache_idle_time = 20,
62 .auto_log_target = 1,
63 .script_commands = NULL,
64 .dl_search_path = NULL,
65 .default_script_file = NULL,
66 .log_target = PA_LOG_SYSLOG,
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_resample_method(struct pa_daemon_conf *c, const char *string) {
112 int m;
113 assert(c && string);
114
115 if ((m = pa_parse_resample_method(string)) < 0)
116 return -1;
117
118 c->resample_method = m;
119 return 0;
120 }
121
122 int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
123 struct pa_daemon_conf *c = data;
124 assert(filename && lvalue && rvalue && data);
125
126 if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
127 pa_log(__FILE__": [%s:%u] Invalid log target '%s'.\n", filename, line, rvalue);
128 return -1;
129 }
130
131 return 0;
132 }
133
134 int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
135 struct pa_daemon_conf *c = data;
136 assert(filename && lvalue && rvalue && data);
137
138 if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
139 pa_log(__FILE__": [%s:%u] Inavalid resample method '%s'.\n", filename, line, rvalue);
140 return -1;
141 }
142
143 return 0;
144 }
145
146 int pa_daemon_conf_load(struct pa_daemon_conf *c, const char *filename) {
147 int r = -1;
148 FILE *f = NULL;
149
150 struct pa_config_item table[] = {
151 { "verbose", pa_config_parse_bool, NULL },
152 { "daemonize", pa_config_parse_bool, NULL },
153 { "fail", pa_config_parse_bool, NULL },
154 { "high-priority", pa_config_parse_bool, NULL },
155 { "disallow-module-loading", pa_config_parse_bool, NULL },
156 { "exit-idle-time", pa_config_parse_int, NULL },
157 { "module-idle-time", pa_config_parse_int, NULL },
158 { "scache-idle-time", pa_config_parse_int, NULL },
159 { "dl-search-path", pa_config_parse_string, NULL },
160 { "default-script-file", pa_config_parse_string, NULL },
161 { "log-target", parse_log_target, NULL },
162 { "resample-method", parse_resample_method, NULL },
163 { "use-pid-file", pa_config_parse_bool, NULL },
164 { NULL, NULL, NULL },
165 };
166
167 table[0].data = &c->verbose;
168 table[1].data = &c->daemonize;
169 table[2].data = &c->fail;
170 table[3].data = &c->high_priority;
171 table[4].data = &c->disallow_module_loading;
172 table[5].data = &c->exit_idle_time;
173 table[6].data = &c->module_idle_time;
174 table[7].data = &c->scache_idle_time;
175 table[8].data = &c->dl_search_path;
176 table[9].data = &c->default_script_file;
177 table[10].data = c;
178 table[11].data = c;
179 table[12].data = &c->use_pid_file;
180
181 pa_xfree(c->config_file);
182 c->config_file = NULL;
183
184 f = filename ?
185 fopen(c->config_file = pa_xstrdup(filename), "r") :
186 pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file);
187
188 if (!f && errno != ENOENT) {
189 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));
190 goto finish;
191 }
192
193 r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
194
195 finish:
196 if (f)
197 fclose(f);
198
199 return r;
200 }
201
202 int pa_daemon_conf_env(struct pa_daemon_conf *c) {
203 char *e;
204
205 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
206 pa_xfree(c->dl_search_path);
207 c->dl_search_path = pa_xstrdup(e);
208 }
209 if ((e = getenv(ENV_SCRIPT_FILE))) {
210 pa_xfree(c->default_script_file);
211 c->default_script_file = pa_xstrdup(e);
212 }
213
214 return 0;
215 }
216
217 char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
218 struct pa_strbuf *s = pa_strbuf_new();
219
220 if (c->config_file)
221 pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
222
223 pa_strbuf_printf(s, "verbose = %i\n", !!c->verbose);
224 pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
225 pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
226 pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
227 pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
228 pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
229 pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
230 pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
231 pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
232 pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
233 pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
234 pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
235 pa_strbuf_printf(s, "use-pid-file = %i\n", c->use_pid_file);
236
237 return pa_strbuf_tostring_free(s);
238 }