]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
Make -1 mean "current group/user" so that some platform dependent calls
[pulseaudio] / src / modules / module-protocol-stub.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42
43 #include "../pulsecore/winsock.h"
44
45 #include <pulse/xmalloc.h>
46
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/module.h>
49 #include <pulsecore/socket-server.h>
50 #include <pulsecore/socket-util.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/modargs.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/native-common.h>
55 #include <pulsecore/creds.h>
56
57 #ifdef USE_TCP_SOCKETS
58 #define SOCKET_DESCRIPTION "(TCP sockets)"
59 #define SOCKET_USAGE "port=<TCP port number> listen=<address to listen on>"
60 #else
61 #define SOCKET_DESCRIPTION "(UNIX sockets)"
62 #define SOCKET_USAGE "socket=<path to UNIX socket>"
63 #endif
64
65 #if defined(USE_PROTOCOL_SIMPLE)
66 #include <pulsecore/protocol-simple.h>
67 #define protocol_new pa_protocol_simple_new
68 #define protocol_free pa_protocol_simple_free
69 #define TCPWRAP_SERVICE "pulseaudio-simple"
70 #define IPV4_PORT 4711
71 #define UNIX_SOCKET "simple"
72 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
73 #if defined(USE_TCP_SOCKETS)
74 #include "module-simple-protocol-tcp-symdef.h"
75 #else
76 #include "module-simple-protocol-unix-symdef.h"
77 #endif
78 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
79 PA_MODULE_USAGE("rate=<sample rate> "
80 "format=<sample format> "
81 "channels=<number of channels> "
82 "sink=<sink to connect to> "
83 "source=<source to connect to> "
84 "playback=<enable playback?> "
85 "record=<enable record?> "
86 SOCKET_USAGE)
87 #elif defined(USE_PROTOCOL_CLI)
88 #include <pulsecore/protocol-cli.h>
89 #define protocol_new pa_protocol_cli_new
90 #define protocol_free pa_protocol_cli_free
91 #define TCPWRAP_SERVICE "pulseaudio-cli"
92 #define IPV4_PORT 4712
93 #define UNIX_SOCKET "cli"
94 #define MODULE_ARGUMENTS
95 #ifdef USE_TCP_SOCKETS
96 #include "module-cli-protocol-tcp-symdef.h"
97 #else
98 #include "module-cli-protocol-unix-symdef.h"
99 #endif
100 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
101 PA_MODULE_USAGE(SOCKET_USAGE)
102 #elif defined(USE_PROTOCOL_HTTP)
103 #include <pulsecore/protocol-http.h>
104 #define protocol_new pa_protocol_http_new
105 #define protocol_free pa_protocol_http_free
106 #define TCPWRAP_SERVICE "pulseaudio-http"
107 #define IPV4_PORT 4714
108 #define UNIX_SOCKET "http"
109 #define MODULE_ARGUMENTS
110 #ifdef USE_TCP_SOCKETS
111 #include "module-http-protocol-tcp-symdef.h"
112 #else
113 #include "module-http-protocol-unix-symdef.h"
114 #endif
115 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
116 PA_MODULE_USAGE(SOCKET_USAGE)
117 #elif defined(USE_PROTOCOL_NATIVE)
118 #include <pulsecore/protocol-native.h>
119 #define protocol_new pa_protocol_native_new
120 #define protocol_free pa_protocol_native_free
121 #define TCPWRAP_SERVICE "pulseaudio-native"
122 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
123 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
124 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
125 #ifdef USE_TCP_SOCKETS
126 #include "module-native-protocol-tcp-symdef.h"
127 #else
128 #include "module-native-protocol-unix-symdef.h"
129 #endif
130
131 #if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
132 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable="
133 #define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
134 #else
135 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
136 #define AUTH_USAGE
137 #endif
138
139 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
140 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
141 "cookie=<path to cookie file> "
142 AUTH_USAGE
143 SOCKET_USAGE)
144 #elif defined(USE_PROTOCOL_ESOUND)
145 #include <pulsecore/protocol-esound.h>
146 #include <pulsecore/esound.h>
147 #define protocol_new pa_protocol_esound_new
148 #define protocol_free pa_protocol_esound_free
149 #define TCPWRAP_SERVICE "esound"
150 #define IPV4_PORT ESD_DEFAULT_PORT
151 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
152 #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
153 #ifdef USE_TCP_SOCKETS
154 #include "module-esound-protocol-tcp-symdef.h"
155 #else
156 #include "module-esound-protocol-unix-symdef.h"
157 #endif
158 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
159 PA_MODULE_USAGE("sink=<sink to connect to> "
160 "source=<source to connect to> "
161 "auth-anonymous=<don't verify cookies?> "
162 "cookie=<path to cookie file> "
163 SOCKET_USAGE)
164 #else
165 #error "Broken build system"
166 #endif
167
168 PA_MODULE_AUTHOR("Lennart Poettering")
169 PA_MODULE_VERSION(PACKAGE_VERSION)
170
171 static const char* const valid_modargs[] = {
172 MODULE_ARGUMENTS
173 #if defined(USE_TCP_SOCKETS)
174 "port",
175 "listen",
176 #else
177 "socket",
178 #endif
179 NULL
180 };
181
182 struct userdata {
183 #if defined(USE_TCP_SOCKETS)
184 void *protocol_ipv4;
185 void *protocol_ipv6;
186 #else
187 void *protocol_unix;
188 char *socket_path;
189 #endif
190 };
191
192 int pa__init(pa_core *c, pa_module*m) {
193 pa_modargs *ma = NULL;
194 int ret = -1;
195
196 struct userdata *u = NULL;
197
198 #if defined(USE_TCP_SOCKETS)
199 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
200 uint32_t port = IPV4_PORT;
201 const char *listen_on;
202 #else
203 pa_socket_server *s;
204 int r;
205 char tmp[PATH_MAX];
206 #endif
207
208 assert(c && m);
209
210 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
211 pa_log(__FILE__": Failed to parse module arguments");
212 goto finish;
213 }
214
215 u = pa_xnew0(struct userdata, 1);
216
217 #if defined(USE_TCP_SOCKETS)
218 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
219 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
220 goto fail;
221 }
222
223 listen_on = pa_modargs_get_value(ma, "listen", NULL);
224
225 if (listen_on) {
226 s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
227 s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
228 } else {
229 s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
230 s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
231 }
232
233 if (!s_ipv4 && !s_ipv6)
234 goto fail;
235
236 if (s_ipv4)
237 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
238 pa_socket_server_unref(s_ipv4);
239
240 if (s_ipv6)
241 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
242 pa_socket_server_unref(s_ipv6);
243
244 if (!u->protocol_ipv4 && !u->protocol_ipv6)
245 goto fail;
246
247 #else
248
249 pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
250 u->socket_path = pa_xstrdup(tmp);
251
252 #if defined(USE_PROTOCOL_ESOUND)
253
254 /* This socket doesn't reside in our own runtime dir but in
255 * /tmp/.esd/, hence we have to create the dir first */
256
257 if (pa_make_secure_parent_dir(u->socket_path, c->is_system_instance ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
258 pa_log(__FILE__": Failed to create socket directory: %s\n", pa_cstrerror(errno));
259 goto fail;
260 }
261 #endif
262
263 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
264 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
265 goto fail;
266 }
267
268 if (r)
269 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
270
271 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
272 goto fail;
273
274 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
275 goto fail;
276
277 #endif
278
279 m->userdata = u;
280
281 ret = 0;
282
283 finish:
284 if (ma)
285 pa_modargs_free(ma);
286
287 return ret;
288
289 fail:
290 if (u) {
291 #if defined(USE_TCP_SOCKETS)
292 if (u->protocol_ipv4)
293 protocol_free(u->protocol_ipv4);
294 if (u->protocol_ipv6)
295 protocol_free(u->protocol_ipv6);
296 #else
297 if (u->protocol_unix)
298 protocol_free(u->protocol_unix);
299
300 if (u->socket_path)
301 pa_xfree(u->socket_path);
302 #endif
303
304 pa_xfree(u);
305 } else {
306 #if defined(USE_TCP_SOCKETS)
307 if (s_ipv4)
308 pa_socket_server_unref(s_ipv4);
309 if (s_ipv6)
310 pa_socket_server_unref(s_ipv6);
311 #else
312 if (s)
313 pa_socket_server_unref(s);
314 #endif
315 }
316
317 goto finish;
318 }
319
320 void pa__done(pa_core *c, pa_module*m) {
321 struct userdata *u;
322
323 assert(c);
324 assert(m);
325
326 u = m->userdata;
327
328 #if defined(USE_TCP_SOCKETS)
329 if (u->protocol_ipv4)
330 protocol_free(u->protocol_ipv4);
331 if (u->protocol_ipv6)
332 protocol_free(u->protocol_ipv6);
333 #else
334 if (u->protocol_unix)
335 protocol_free(u->protocol_unix);
336
337 #if defined(USE_PROTOCOL_ESOUND)
338 if (u->socket_path) {
339 char *p = pa_parent_dir(u->socket_path);
340 rmdir(p);
341 pa_xfree(p);
342 }
343 #endif
344
345
346 pa_xfree(u->socket_path);
347 #endif
348
349 pa_xfree(u);
350 }