]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
tag modules that may only be loaded once at most especially, and enforce that in...
[pulseaudio] / src / modules / module-protocol-stub.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <limits.h>
34
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 #endif
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
43 #endif
44
45 #include <pulse/xmalloc.h>
46
47 #include <pulsecore/winsock.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/module.h>
50 #include <pulsecore/socket-server.h>
51 #include <pulsecore/socket-util.h>
52 #include <pulsecore/core-util.h>
53 #include <pulsecore/modargs.h>
54 #include <pulsecore/log.h>
55 #include <pulsecore/native-common.h>
56 #include <pulsecore/creds.h>
57
58 #ifdef USE_TCP_SOCKETS
59 #define SOCKET_DESCRIPTION "(TCP sockets)"
60 #define SOCKET_USAGE "port=<TCP port number> listen=<address to listen on>"
61 #else
62 #define SOCKET_DESCRIPTION "(UNIX sockets)"
63 #define SOCKET_USAGE "socket=<path to UNIX socket>"
64 #endif
65
66 #if defined(USE_PROTOCOL_SIMPLE)
67 #include <pulsecore/protocol-simple.h>
68 #define protocol_new pa_protocol_simple_new
69 #define protocol_free pa_protocol_simple_free
70 #define TCPWRAP_SERVICE "pulseaudio-simple"
71 #define IPV4_PORT 4711
72 #define UNIX_SOCKET "simple"
73 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
74 #if defined(USE_TCP_SOCKETS)
75 #include "module-simple-protocol-tcp-symdef.h"
76 #else
77 #include "module-simple-protocol-unix-symdef.h"
78 #endif
79 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION);
80 PA_MODULE_USAGE("rate=<sample rate> "
81 "format=<sample format> "
82 "channels=<number of channels> "
83 "sink=<sink to connect to> "
84 "source=<source to connect to> "
85 "playback=<enable playback?> "
86 "record=<enable record?> "
87 SOCKET_USAGE);
88 #elif defined(USE_PROTOCOL_CLI)
89 #include <pulsecore/protocol-cli.h>
90 #define protocol_new pa_protocol_cli_new
91 #define protocol_free pa_protocol_cli_free
92 #define TCPWRAP_SERVICE "pulseaudio-cli"
93 #define IPV4_PORT 4712
94 #define UNIX_SOCKET "cli"
95 #define MODULE_ARGUMENTS
96 #ifdef USE_TCP_SOCKETS
97 #include "module-cli-protocol-tcp-symdef.h"
98 #else
99 #include "module-cli-protocol-unix-symdef.h"
100 #endif
101 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION);
102 PA_MODULE_USAGE(SOCKET_USAGE);
103 #elif defined(USE_PROTOCOL_HTTP)
104 #include <pulsecore/protocol-http.h>
105 #define protocol_new pa_protocol_http_new
106 #define protocol_free pa_protocol_http_free
107 #define TCPWRAP_SERVICE "pulseaudio-http"
108 #define IPV4_PORT 4714
109 #define UNIX_SOCKET "http"
110 #define MODULE_ARGUMENTS
111 #ifdef USE_TCP_SOCKETS
112 #include "module-http-protocol-tcp-symdef.h"
113 #else
114 #include "module-http-protocol-unix-symdef.h"
115 #endif
116 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION);
117 PA_MODULE_USAGE(SOCKET_USAGE);
118 #elif defined(USE_PROTOCOL_NATIVE)
119 #include <pulsecore/protocol-native.h>
120 #define protocol_new pa_protocol_native_new
121 #define protocol_free pa_protocol_native_free
122 #define TCPWRAP_SERVICE "pulseaudio-native"
123 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
124 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
125 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
126 #ifdef USE_TCP_SOCKETS
127 #include "module-native-protocol-tcp-symdef.h"
128 #else
129 #include "module-native-protocol-unix-symdef.h"
130 #endif
131
132 #if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
133 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable",
134 #define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
135 #elif defined(USE_TCP_SOCKETS)
136 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
137 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
138 #else
139 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
140 #define AUTH_USAGE
141 #endif
142
143 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION);
144 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
145 "cookie=<path to cookie file> "
146 AUTH_USAGE
147 SOCKET_USAGE);
148 #elif defined(USE_PROTOCOL_ESOUND)
149 #include <pulsecore/protocol-esound.h>
150 #include <pulsecore/esound.h>
151 #define protocol_new pa_protocol_esound_new
152 #define protocol_free pa_protocol_esound_free
153 #define TCPWRAP_SERVICE "esound"
154 #define IPV4_PORT ESD_DEFAULT_PORT
155 #define MODULE_ARGUMENTS_COMMON "sink", "source", "auth-anonymous", "cookie",
156 #ifdef USE_TCP_SOCKETS
157 #include "module-esound-protocol-tcp-symdef.h"
158 #else
159 #include "module-esound-protocol-unix-symdef.h"
160 #endif
161
162 #if defined(USE_TCP_SOCKETS)
163 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
164 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
165 #else
166 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
167 #define AUTH_USAGE
168 #endif
169
170 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION);
171 PA_MODULE_USAGE("sink=<sink to connect to> "
172 "source=<source to connect to> "
173 "auth-anonymous=<don't verify cookies?> "
174 "cookie=<path to cookie file> "
175 AUTH_USAGE
176 SOCKET_USAGE);
177 #else
178 #error "Broken build system"
179 #endif
180
181 PA_MODULE_LOAD_ONCE(FALSE);
182 PA_MODULE_AUTHOR("Lennart Poettering");
183 PA_MODULE_VERSION(PACKAGE_VERSION);
184
185 static const char* const valid_modargs[] = {
186 MODULE_ARGUMENTS
187 #if defined(USE_TCP_SOCKETS)
188 "port",
189 "listen",
190 #else
191 "socket",
192 #endif
193 NULL
194 };
195
196 struct userdata {
197 #if defined(USE_TCP_SOCKETS)
198 void *protocol_ipv4;
199 void *protocol_ipv6;
200 #else
201 void *protocol_unix;
202 char *socket_path;
203 #endif
204 };
205
206 int pa__init(pa_module*m) {
207 pa_modargs *ma = NULL;
208 int ret = -1;
209 struct userdata *u = NULL;
210
211 #if defined(USE_TCP_SOCKETS)
212 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
213 uint32_t port = IPV4_PORT;
214 const char *listen_on;
215 #else
216 pa_socket_server *s;
217 int r;
218 char tmp[PATH_MAX];
219
220 #if defined(USE_PROTOCOL_ESOUND)
221 char tmp2[PATH_MAX];
222 #endif
223 #endif
224
225 pa_assert(m);
226
227 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
228 pa_log("Failed to parse module arguments");
229 goto finish;
230 }
231
232 u = pa_xnew0(struct userdata, 1);
233
234 #if defined(USE_TCP_SOCKETS)
235 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
236 pa_log("port= expects a numerical argument between 1 and 65535.");
237 goto fail;
238 }
239
240 listen_on = pa_modargs_get_value(ma, "listen", NULL);
241
242 if (listen_on) {
243 s_ipv6 = pa_socket_server_new_ipv6_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
244 s_ipv4 = pa_socket_server_new_ipv4_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
245 } else {
246 s_ipv6 = pa_socket_server_new_ipv6_any(m->core->mainloop, port, TCPWRAP_SERVICE);
247 s_ipv4 = pa_socket_server_new_ipv4_any(m->core->mainloop, port, TCPWRAP_SERVICE);
248 }
249
250 if (!s_ipv4 && !s_ipv6)
251 goto fail;
252
253 if (s_ipv4)
254 if (!(u->protocol_ipv4 = protocol_new(m->core, s_ipv4, m, ma)))
255 pa_socket_server_unref(s_ipv4);
256
257 if (s_ipv6)
258 if (!(u->protocol_ipv6 = protocol_new(m->core, s_ipv6, m, ma)))
259 pa_socket_server_unref(s_ipv6);
260
261 if (!u->protocol_ipv4 && !u->protocol_ipv6)
262 goto fail;
263
264 #else
265
266 #if defined(USE_PROTOCOL_ESOUND)
267
268 snprintf(tmp2, sizeof(tmp2), "/tmp/.esd-%lu/socket", (unsigned long) getuid());
269 pa_runtime_path(pa_modargs_get_value(ma, "socket", tmp2), tmp, sizeof(tmp));
270 u->socket_path = pa_xstrdup(tmp);
271
272 /* This socket doesn't reside in our own runtime dir but in
273 * /tmp/.esd/, hence we have to create the dir first */
274
275 if (pa_make_secure_parent_dir(u->socket_path, m->core->is_system_instance ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
276 pa_log("Failed to create socket directory '%s': %s\n", u->socket_path, pa_cstrerror(errno));
277 goto fail;
278 }
279
280 #else
281 pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
282 u->socket_path = pa_xstrdup(tmp);
283 #endif
284
285 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
286 pa_log("Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
287 goto fail;
288 }
289
290 if (r)
291 pa_log("Removed stale UNIX socket '%s'.", tmp);
292
293 if (!(s = pa_socket_server_new_unix(m->core->mainloop, tmp)))
294 goto fail;
295
296 if (!(u->protocol_unix = protocol_new(m->core, s, m, ma)))
297 goto fail;
298
299 #endif
300
301 m->userdata = u;
302
303 ret = 0;
304
305 finish:
306 if (ma)
307 pa_modargs_free(ma);
308
309 return ret;
310
311 fail:
312 if (u) {
313 #if defined(USE_TCP_SOCKETS)
314 if (u->protocol_ipv4)
315 protocol_free(u->protocol_ipv4);
316 if (u->protocol_ipv6)
317 protocol_free(u->protocol_ipv6);
318 #else
319 if (u->protocol_unix)
320 protocol_free(u->protocol_unix);
321
322 if (u->socket_path)
323 pa_xfree(u->socket_path);
324 #endif
325
326 pa_xfree(u);
327 } else {
328 #if defined(USE_TCP_SOCKETS)
329 if (s_ipv4)
330 pa_socket_server_unref(s_ipv4);
331 if (s_ipv6)
332 pa_socket_server_unref(s_ipv6);
333 #else
334 if (s)
335 pa_socket_server_unref(s);
336 #endif
337 }
338
339 goto finish;
340 }
341
342 void pa__done(pa_module*m) {
343 struct userdata *u;
344
345 pa_assert(m);
346
347 u = m->userdata;
348
349 #if defined(USE_TCP_SOCKETS)
350 if (u->protocol_ipv4)
351 protocol_free(u->protocol_ipv4);
352 if (u->protocol_ipv6)
353 protocol_free(u->protocol_ipv6);
354 #else
355 if (u->protocol_unix)
356 protocol_free(u->protocol_unix);
357
358 #if defined(USE_PROTOCOL_ESOUND)
359 if (u->socket_path) {
360 char *p = pa_parent_dir(u->socket_path);
361 rmdir(p);
362 pa_xfree(p);
363 }
364 #endif
365
366 pa_xfree(u->socket_path);
367 #endif
368
369 pa_xfree(u);
370 }