Branch data Line data Source code
1 : : /***
2 : : This file is part of PulseAudio.
3 : :
4 : : Copyright 2004-2006 Lennart Poettering
5 : : Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6 : :
7 : : PulseAudio is free software; you can redistribute it and/or modify
8 : : it under the terms of the GNU Lesser General Public License as published
9 : : by the Free Software Foundation; either version 2.1 of the License,
10 : : or (at your option) any later version.
11 : :
12 : : PulseAudio is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU Lesser General Public License
18 : : along with PulseAudio; if not, write to the Free Software
19 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 : : USA.
21 : : ***/
22 : :
23 : : #ifdef HAVE_CONFIG_H
24 : : #include <config.h>
25 : : #endif
26 : :
27 : : #include <stdlib.h>
28 : : #include <unistd.h>
29 : : #include <errno.h>
30 : :
31 : : #include <pulse/xmalloc.h>
32 : :
33 : : #include <pulsecore/i18n.h>
34 : : #include <pulsecore/macro.h>
35 : : #include <pulsecore/core-error.h>
36 : : #include <pulsecore/log.h>
37 : : #include <pulsecore/conf-parser.h>
38 : : #include <pulsecore/core-util.h>
39 : : #include <pulsecore/authkey.h>
40 : :
41 : : #include "client-conf.h"
42 : :
43 : : #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "client.conf"
44 : : #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
45 : :
46 : : #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
47 : : #define ENV_DEFAULT_SINK "PULSE_SINK"
48 : : #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
49 : : #define ENV_DEFAULT_SERVER "PULSE_SERVER"
50 : : #define ENV_DAEMON_BINARY "PULSE_BINARY"
51 : : #define ENV_COOKIE_FILE "PULSE_COOKIE"
52 : :
53 : : static const pa_client_conf default_conf = {
54 : : .daemon_binary = NULL,
55 : : .extra_arguments = NULL,
56 : : .default_sink = NULL,
57 : : .default_source = NULL,
58 : : .default_server = NULL,
59 : : .default_dbus_server = NULL,
60 : : .autospawn = TRUE,
61 : : .disable_shm = FALSE,
62 : : .cookie_file = NULL,
63 : : .cookie_valid = FALSE,
64 : : .shm_size = 0,
65 : : .auto_connect_localhost = FALSE,
66 : : .auto_connect_display = FALSE
67 : : };
68 : :
69 : 0 : pa_client_conf *pa_client_conf_new(void) {
70 : 0 : pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
71 : :
72 : 0 : c->daemon_binary = pa_xstrdup(PA_BINARY);
73 : 0 : c->extra_arguments = pa_xstrdup("--log-target=syslog");
74 : 0 : c->cookie_file = NULL;
75 : :
76 : 0 : return c;
77 : : }
78 : :
79 : 0 : void pa_client_conf_free(pa_client_conf *c) {
80 [ # # ]: 0 : pa_assert(c);
81 : 0 : pa_xfree(c->daemon_binary);
82 : 0 : pa_xfree(c->extra_arguments);
83 : 0 : pa_xfree(c->default_sink);
84 : 0 : pa_xfree(c->default_source);
85 : 0 : pa_xfree(c->default_server);
86 : 0 : pa_xfree(c->default_dbus_server);
87 : 0 : pa_xfree(c->cookie_file);
88 : 0 : pa_xfree(c);
89 : 0 : }
90 : :
91 : 0 : int pa_client_conf_load(pa_client_conf *c, const char *filename) {
92 : 0 : FILE *f = NULL;
93 : 0 : char *fn = NULL;
94 : 0 : int r = -1;
95 : :
96 : : /* Prepare the configuration parse table */
97 : 0 : pa_config_item table[] = {
98 : 0 : { "daemon-binary", pa_config_parse_string, &c->daemon_binary, NULL },
99 : 0 : { "extra-arguments", pa_config_parse_string, &c->extra_arguments, NULL },
100 : 0 : { "default-sink", pa_config_parse_string, &c->default_sink, NULL },
101 : 0 : { "default-source", pa_config_parse_string, &c->default_source, NULL },
102 : 0 : { "default-server", pa_config_parse_string, &c->default_server, NULL },
103 : 0 : { "default-dbus-server", pa_config_parse_string, &c->default_dbus_server, NULL },
104 : 0 : { "autospawn", pa_config_parse_bool, &c->autospawn, NULL },
105 : 0 : { "cookie-file", pa_config_parse_string, &c->cookie_file, NULL },
106 : 0 : { "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
107 : 0 : { "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
108 : 0 : { "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL },
109 : 0 : { "auto-connect-localhost", pa_config_parse_bool, &c->auto_connect_localhost, NULL },
110 : 0 : { "auto-connect-display", pa_config_parse_bool, &c->auto_connect_display, NULL },
111 : : { NULL, NULL, NULL, NULL },
112 : : };
113 : :
114 [ # # ]: 0 : if (filename) {
115 : :
116 [ # # ]: 0 : if (!(f = pa_fopen_cloexec(filename, "r"))) {
117 : 0 : pa_log(_("Failed to open configuration file '%s': %s"), fn, pa_cstrerror(errno));
118 : 0 : goto finish;
119 : : }
120 : :
121 : 0 : fn = pa_xstrdup(fn);
122 : :
123 : : } else {
124 : :
125 [ # # ]: 0 : if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
126 [ # # ]: 0 : if (errno != ENOENT)
127 : : goto finish;
128 : : }
129 : :
130 [ # # ]: 0 : r = f ? pa_config_parse(fn, f, table, NULL, NULL) : 0;
131 : :
132 [ # # ]: 0 : if (!r)
133 : 0 : r = pa_client_conf_load_cookie(c);
134 : :
135 : : finish:
136 : 0 : pa_xfree(fn);
137 : :
138 [ # # ]: 0 : if (f)
139 : 0 : fclose(f);
140 : :
141 : 0 : return r;
142 : : }
143 : :
144 : 0 : int pa_client_conf_env(pa_client_conf *c) {
145 : : char *e;
146 : :
147 [ # # ]: 0 : if ((e = getenv(ENV_DEFAULT_SINK))) {
148 : 0 : pa_xfree(c->default_sink);
149 : 0 : c->default_sink = pa_xstrdup(e);
150 : : }
151 : :
152 [ # # ]: 0 : if ((e = getenv(ENV_DEFAULT_SOURCE))) {
153 : 0 : pa_xfree(c->default_source);
154 : 0 : c->default_source = pa_xstrdup(e);
155 : : }
156 : :
157 [ # # ]: 0 : if ((e = getenv(ENV_DEFAULT_SERVER))) {
158 : 0 : pa_xfree(c->default_server);
159 : 0 : c->default_server = pa_xstrdup(e);
160 : :
161 : : /* We disable autospawning automatically if a specific server was set */
162 : 0 : c->autospawn = FALSE;
163 : : }
164 : :
165 [ # # ]: 0 : if ((e = getenv(ENV_DAEMON_BINARY))) {
166 : 0 : pa_xfree(c->daemon_binary);
167 : 0 : c->daemon_binary = pa_xstrdup(e);
168 : : }
169 : :
170 [ # # ]: 0 : if ((e = getenv(ENV_COOKIE_FILE))) {
171 : 0 : pa_xfree(c->cookie_file);
172 : 0 : c->cookie_file = pa_xstrdup(e);
173 : :
174 : 0 : return pa_client_conf_load_cookie(c);
175 : : }
176 : :
177 : : return 0;
178 : : }
179 : :
180 : 0 : int pa_client_conf_load_cookie(pa_client_conf* c) {
181 : : int k;
182 : :
183 [ # # ]: 0 : pa_assert(c);
184 : :
185 : 0 : c->cookie_valid = FALSE;
186 : :
187 [ # # ]: 0 : if (c->cookie_file)
188 : 0 : k = pa_authkey_load_auto(c->cookie_file, TRUE, c->cookie, sizeof(c->cookie));
189 : : else {
190 : 0 : k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, FALSE, c->cookie, sizeof(c->cookie));
191 : :
192 [ # # ]: 0 : if (k < 0) {
193 : 0 : k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE_FALLBACK, FALSE, c->cookie, sizeof(c->cookie));
194 : :
195 [ # # ]: 0 : if (k < 0)
196 : 0 : k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, TRUE, c->cookie, sizeof(c->cookie));
197 : : }
198 : : }
199 : :
200 [ # # ]: 0 : if (k < 0)
201 : : return k;
202 : :
203 : 0 : c->cookie_valid = TRUE;
204 : 0 : return 0;
205 : : }
|