1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* Evil evil evil hack to get OSS apps to cooperate with esd
* Copyright (C) 1998, 1999 Manish Singh <yosh@gimp.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*#define DSP_DEBUG */
/* This lets you run multiple instances of x11amp by setting the X11AMPNUM
environment variable. Only works on glibc2.
*/
/* #define MULTIPLE_X11AMP */
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#ifdef DSP_DEBUG
#define DPRINTF(format, args...) printf(format, ## args)
#else
#define DPRINTF(format, args...)
#endif
#include "config.h"
#include <dlfcn.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_MACHINE_SOUNDCARD_H
# include <machine/soundcard.h>
#else
# ifdef HAVE_SOUNDCARD_H
# include <soundcard.h>
# else
# include <sys/soundcard.h>
# endif
#endif
#include "gstosshelper.h"
/* BSDI has this functionality, but not define :() */
#if defined(RTLD_NEXT)
#define REAL_LIBC RTLD_NEXT
#else
#define REAL_LIBC ((void *) -1L)
#endif
#if defined(__FreeBSD__) || defined(__bsdi__)
typedef unsigned long request_t;
#else
typedef int request_t;
#endif
static int sndfd = -1;
static int new_format = 1;
static int fmt = AFMT_S16_LE;
static int speed = 44100;
static int stereo = 1;
int
open (const char *pathname, int flags, ...)
{
static int (*func) (const char *, int, mode_t) = NULL;
va_list args;
mode_t mode;
if (!func)
func = (int (*) (const char *, int, mode_t)) dlsym (REAL_LIBC, "open");
va_start (args, flags);
mode = va_arg (args, mode_t);
va_end (args);
if (!strcmp (pathname, "/dev/dsp")) {
DPRINTF ("hijacking /dev/dsp open, and taking it to GStreamer...\n");
return (sndfd = HELPER_MAGIC_SNDFD);
}
return (sndfd = (*func) (pathname, flags, mode));
}
static int
dspctl (int fd, request_t request, void *argp)
{
int *arg = (int *) argp;
DPRINTF ("hijacking /dev/dsp ioctl, and sending it to GStreamer "
"(%d : %x - %p)\n", fd, request, argp);
switch (request)
{
case SNDCTL_DSP_RESET:
case SNDCTL_DSP_POST:
break;
case SNDCTL_DSP_SETFMT:
fmt = *arg;
new_format = 1;
break;
case SNDCTL_DSP_SPEED:
speed = *arg;
new_format = 1;
break;
case SNDCTL_DSP_STEREO:
stereo = *arg;
new_format = 1;
break;
case SNDCTL_DSP_GETBLKSIZE:
*arg = 4096;
break;
case SNDCTL_DSP_GETFMTS:
*arg = 0x38;
break;
#ifdef SNDCTL_DSP_GETCAPS
case SNDCTL_DSP_GETCAPS:
*arg = 0;
break;
#endif
case SNDCTL_DSP_GETOSPACE:
{
audio_buf_info *bufinfo = (audio_buf_info *) argp;
bufinfo->bytes = 4096;
}
break;
default:
DPRINTF ("unhandled /dev/dsp ioctl (%x - %p)\n", request, argp);
break;
}
return 0;
}
void *
mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
{
static void * (*func) (void *, size_t, int, int, int, off_t) = NULL;
if (!func)
func = (void * (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");
if ((fd == sndfd) && (sndfd != -1))
{
DPRINTF("MMAP: oops... we're in trouble here. /dev/dsp mmap()ed. Not supported yet.\n");
errno = EACCES;
return (void *)-1; /* Better causing an error than silently not working, in this case */
}
return (*func) (start, length, prot, flags, fd, offset);
}
ssize_t
write (int fd, const void *buf, size_t len)
{
static int (*func) (int, const void *, size_t) = NULL;
command cmd;
if (!func)
func = (int (*) (int, const void *, size_t)) dlsym (REAL_LIBC, "write");
if ((fd != sndfd) || (sndfd == -1))
{
return (*func) (fd, buf, len);
}
DPRINTF("WRITE: called for %d bytes\n", len);
if (new_format) {
new_format = 0;
cmd.id = CMD_FORMAT;
cmd.cmd.format.format = fmt;
cmd.cmd.format.stereo = stereo;
cmd.cmd.format.rate = speed;
(*func) (HELPER_MAGIC_OUT, &cmd, sizeof(command));
}
cmd.id = CMD_DATA;
cmd.cmd.length = len;
(*func) (HELPER_MAGIC_OUT, &cmd, sizeof(command));
(*func) (HELPER_MAGIC_OUT, buf, len);
/*return (*func) (fd, buf, len); */
return len;
}
int
select (int n, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
{
static int (*func) (int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL;
if (!func)
func = (int (*) (int, fd_set *, fd_set *, fd_set *, struct timeval *)) dlsym (REAL_LIBC, "select");
if (n == sndfd) {
DPRINTF ("audiooss: hijacking /dev/dsp select() [output]\n");
}
return (*func) (n, readfds, writefds, exceptfds, timeout);
}
int
dup2 (int oldfd, int newfd)
{
static int (*func) (int, int) = NULL;
if (!func)
func = (int (*) (int, int)) dlsym (REAL_LIBC, "dup2");
if ((oldfd == sndfd) && (oldfd != -1) && (newfd != -1))
{
DPRINTF("dup2(%d,%d) (oldfd == sndfd) called\n", oldfd, newfd);
/* Do not close(newfd) as that would mark it available for reuse by the system -
* just tell the program that yes, we got the fd you asked for. Hackish. */
sndfd = newfd;
return newfd;
}
return (*func) (oldfd, newfd);
}
int
ioctl (int fd, request_t request, ...)
{
static int (*func) (int, request_t, void *) = NULL;
va_list args;
void *argp;
if (!func)
func = (int (*) (int, request_t, void *)) dlsym (REAL_LIBC, "ioctl");
va_start (args, request);
argp = va_arg (args, void *);
va_end (args);
if (fd == sndfd)
return dspctl (fd, request, argp);
return (*func) (fd, request, argp);
}
int
fcntl(int fd, int cmd, ...)
{
static int (*func) (int, int, void *) = NULL;
va_list args;
void *argp;
if (!func)
func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "fcntl");
va_start (args, cmd);
argp = va_arg (args, void *);
va_end (args);
if ((fd != -1) && (fd == sndfd))
{
DPRINTF ("hijacking /dev/dsp fcntl() "
"(%d : %x - %p)\n", fd, cmd, argp);
if (cmd == F_GETFL) return O_RDWR;
if (cmd == F_GETFD) return sndfd;
return 0;
}
else
{
return (*func) (fd, cmd, argp);
}
return 0;
}
int
close (int fd)
{
static int (*func) (int) = NULL;
if (!func)
func = (int (*) (int)) dlsym (REAL_LIBC, "close");
if (fd == sndfd)
sndfd = -1;
return (*func) (fd);
}
#ifdef MULTIPLE_X11AMP
#include <socketbits.h>
#include <sys/param.h>
#include <sys/un.h>
#define ENVSET "X11AMPNUM"
int
unlink (const char *filename)
{
static int (*func) (const char *) = NULL;
char *num;
if (!func)
func = (int (*) (const char *)) dlsym (REAL_LIBC, "unlink");
if (!strcmp (filename, "/tmp/X11Amp_CTRL") && (num = getenv (ENVSET)))
{
char buf[PATH_MAX] = "/tmp/X11Amp_CTRL";
strcat (buf, num);
return (*func) (buf);
}
else
return (*func) (filename);
}
typedef int (*sa_func_t) (int, struct sockaddr *, int);
static int
sockaddr_mangle (sa_func_t func, int fd, struct sockaddr *addr, int len)
{
char *num;
if (!strcmp (((struct sockaddr_un *) addr)->sun_path, "/tmp/X11Amp_CTRL")
&& (num = getenv(ENVSET)))
{
int ret;
char buf[PATH_MAX] = "/tmp/X11Amp_CTRL";
struct sockaddr_un *new_addr = malloc (len);
strcat (buf, num);
memcpy (new_addr, addr, len);
strcpy (new_addr->sun_path, buf);
ret = (*func) (fd, (struct sockaddr *) new_addr, len);
free (new_addr);
return ret;
}
else
return (*func) (fd, addr, len);
}
int
bind (int fd, struct sockaddr *addr, int len)
{
static sa_func_t func = NULL;
if (!func)
func = (sa_func_t) dlsym (REAL_LIBC, "bind");
return sockaddr_mangle (func, fd, addr, len);
}
int
connect (int fd, struct sockaddr *addr, int len)
{
static sa_func_t func = NULL;
if (!func)
func = (sa_func_t) dlsym (REAL_LIBC, "connect");
return sockaddr_mangle (func, fd, addr, len);
}
#endif /* MULTIPLE_X11AMP */
#else /* __GNUC__ */
static char *ident = NULL;
void
nogcc (void)
{
ident = NULL;
}
#endif /* __GNUC__ */
|