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