]> code.delx.au - pulseaudio/blob - src/utils/padsp.c
make pulseaudio compile again on FreeBSD (patch from Diego "Flameeyes" Petteno)
[pulseaudio] / src / utils / padsp.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 #ifdef _FILE_OFFSET_BITS
27 #undef _FILE_OFFSET_BITS
28 #endif
29
30 #ifndef _LARGEFILE64_SOURCE
31 #define _LARGEFILE64_SOURCE 1
32 #endif
33
34 #include <sys/soundcard.h>
35 #include <sys/ioctl.h>
36 #include <pthread.h>
37 #include <unistd.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <dlfcn.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <signal.h>
47
48 #ifdef __linux__
49 #include <linux/sockios.h>
50 #endif
51
52 #include <pulse/pulseaudio.h>
53 #include <pulsecore/llist.h>
54 #include <pulsecore/gccmacro.h>
55
56 typedef enum {
57 FD_INFO_MIXER,
58 FD_INFO_STREAM,
59 } fd_info_type_t;
60
61 typedef struct fd_info fd_info;
62
63 struct fd_info {
64 pthread_mutex_t mutex;
65 int ref;
66 int unusable;
67
68 fd_info_type_t type;
69 int app_fd, thread_fd;
70
71 pa_sample_spec sample_spec;
72 size_t fragment_size;
73 unsigned n_fragments;
74
75 pa_threaded_mainloop *mainloop;
76 pa_context *context;
77 pa_stream *play_stream;
78 pa_stream *rec_stream;
79
80 pa_io_event *io_event;
81 pa_io_event_flags_t io_flags;
82
83 void *buf;
84 size_t rec_offset;
85
86 int operation_success;
87
88 pa_cvolume sink_volume, source_volume;
89 uint32_t sink_index, source_index;
90 int volume_modify_count;
91
92 PA_LLIST_FIELDS(fd_info);
93 };
94
95 static int dsp_drain(fd_info *i);
96 static void fd_info_remove_from_list(fd_info *i);
97
98 static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
99 static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
100
101 static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
102
103 static int (*_ioctl)(int, int, void*) = NULL;
104 static int (*_close)(int) = NULL;
105 static int (*_open)(const char *, int, mode_t) = NULL;
106 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
107 #ifdef HAVE_OPEN64
108 static int (*_open64)(const char *, int, mode_t) = NULL;
109 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
110 #endif
111 static int (*_fclose)(FILE *f) = NULL;
112 static int (*_access)(const char *, int) = NULL;
113
114 /* dlsym() violates ISO C, so confide the breakage into this function to
115 * avoid warnings. */
116 typedef void (*fnptr)(void);
117 static inline fnptr dlsym_fn(void *handle, const char *symbol) {
118 return (fnptr) (long) dlsym(handle, symbol);
119 }
120
121 #define LOAD_IOCTL_FUNC() \
122 do { \
123 pthread_mutex_lock(&func_mutex); \
124 if (!_ioctl) \
125 _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
126 pthread_mutex_unlock(&func_mutex); \
127 } while(0)
128
129 #define LOAD_OPEN_FUNC() \
130 do { \
131 pthread_mutex_lock(&func_mutex); \
132 if (!_open) \
133 _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
134 pthread_mutex_unlock(&func_mutex); \
135 } while(0)
136
137 #define LOAD_OPEN64_FUNC() \
138 do { \
139 pthread_mutex_lock(&func_mutex); \
140 if (!_open64) \
141 _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
142 pthread_mutex_unlock(&func_mutex); \
143 } while(0)
144
145 #define LOAD_CLOSE_FUNC() \
146 do { \
147 pthread_mutex_lock(&func_mutex); \
148 if (!_close) \
149 _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
150 pthread_mutex_unlock(&func_mutex); \
151 } while(0)
152
153 #define LOAD_ACCESS_FUNC() \
154 do { \
155 pthread_mutex_lock(&func_mutex); \
156 if (!_access) \
157 _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
158 pthread_mutex_unlock(&func_mutex); \
159 } while(0)
160
161 #define LOAD_FOPEN_FUNC() \
162 do { \
163 pthread_mutex_lock(&func_mutex); \
164 if (!_fopen) \
165 _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
166 pthread_mutex_unlock(&func_mutex); \
167 } while(0)
168
169 #define LOAD_FOPEN64_FUNC() \
170 do { \
171 pthread_mutex_lock(&func_mutex); \
172 if (!_fopen64) \
173 _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
174 pthread_mutex_unlock(&func_mutex); \
175 } while(0)
176
177 #define LOAD_FCLOSE_FUNC() \
178 do { \
179 pthread_mutex_lock(&func_mutex); \
180 if (!_fclose) \
181 _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
182 pthread_mutex_unlock(&func_mutex); \
183 } while(0)
184
185 #define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
186 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
187 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
188 goto label; \
189 } \
190 } while(0);
191
192 #define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
193 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
194 !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
195 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
196 goto label; \
197 } \
198 } while(0);
199
200 #define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
201 if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
202 !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
203 debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
204 goto label; \
205 } \
206 } while(0);
207
208 static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
209
210 #define DEBUG_LEVEL_ALWAYS 0
211 #define DEBUG_LEVEL_NORMAL 1
212 #define DEBUG_LEVEL_VERBOSE 2
213
214 static void debug(int level, const char *format, ...) {
215 va_list ap;
216 const char *dlevel_s;
217 int dlevel;
218
219 dlevel_s = getenv("PADSP_DEBUG");
220 if (!dlevel_s)
221 return;
222
223 dlevel = atoi(dlevel_s);
224
225 if (dlevel < level)
226 return;
227
228 va_start(ap, format);
229 vfprintf(stderr, format, ap);
230 va_end(ap);
231 }
232
233 static int padsp_disabled(void) {
234 static int *sym;
235 static int sym_resolved = 0;
236
237 /* If the current process has a symbol __padsp_disabled__ we use
238 * it to detect whether we should enable our stuff or not. A
239 * program needs to be compiled with -rdynamic for this to work!
240 * The symbol must be an int containing a three bit bitmask: bit 1
241 * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
242 * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
243 * of 7 disables padsp entirely. */
244
245 pthread_mutex_lock(&func_mutex);
246 if (!sym_resolved) {
247 sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
248 sym_resolved = 1;
249
250 }
251 pthread_mutex_unlock(&func_mutex);
252
253 if (!sym)
254 return 0;
255
256 return *sym;
257 }
258
259 static int dsp_cloak_enable(void) {
260 if (padsp_disabled() & 1)
261 return 0;
262
263 if (getenv("PADSP_NO_DSP"))
264 return 0;
265
266 return 1;
267 }
268
269 static int sndstat_cloak_enable(void) {
270 if (padsp_disabled() & 2)
271 return 0;
272
273 if (getenv("PADSP_NO_SNDSTAT"))
274 return 0;
275
276 return 1;
277 }
278
279 static int mixer_cloak_enable(void) {
280 if (padsp_disabled() & 4)
281 return 0;
282
283 if (getenv("PADSP_NO_MIXER"))
284 return 0;
285
286 return 1;
287 }
288 static pthread_key_t recursion_key;
289
290 static void recursion_key_alloc(void) {
291 pthread_key_create(&recursion_key, NULL);
292 }
293
294 static int function_enter(void) {
295 /* Avoid recursive calls */
296 static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
297 pthread_once(&recursion_key_once, recursion_key_alloc);
298
299 if (pthread_getspecific(recursion_key))
300 return 0;
301
302 pthread_setspecific(recursion_key, (void*) 1);
303 return 1;
304 }
305
306 static void function_exit(void) {
307 pthread_setspecific(recursion_key, NULL);
308 }
309
310 static void fd_info_free(fd_info *i) {
311 assert(i);
312
313 debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
314
315 dsp_drain(i);
316
317 if (i->mainloop)
318 pa_threaded_mainloop_stop(i->mainloop);
319
320 if (i->play_stream) {
321 pa_stream_disconnect(i->play_stream);
322 pa_stream_unref(i->play_stream);
323 }
324
325 if (i->rec_stream) {
326 pa_stream_disconnect(i->rec_stream);
327 pa_stream_unref(i->rec_stream);
328 }
329
330 if (i->context) {
331 pa_context_disconnect(i->context);
332 pa_context_unref(i->context);
333 }
334
335 if (i->mainloop)
336 pa_threaded_mainloop_free(i->mainloop);
337
338 if (i->app_fd >= 0) {
339 LOAD_CLOSE_FUNC();
340 _close(i->app_fd);
341 }
342
343 if (i->thread_fd >= 0) {
344 LOAD_CLOSE_FUNC();
345 _close(i->thread_fd);
346 }
347
348 free(i->buf);
349
350 pthread_mutex_destroy(&i->mutex);
351 free(i);
352 }
353
354 static fd_info *fd_info_ref(fd_info *i) {
355 assert(i);
356
357 pthread_mutex_lock(&i->mutex);
358 assert(i->ref >= 1);
359 i->ref++;
360
361 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
362 pthread_mutex_unlock(&i->mutex);
363
364 return i;
365 }
366
367 static void fd_info_unref(fd_info *i) {
368 int r;
369 pthread_mutex_lock(&i->mutex);
370 assert(i->ref >= 1);
371 r = --i->ref;
372 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
373 pthread_mutex_unlock(&i->mutex);
374
375 if (r <= 0)
376 fd_info_free(i);
377 }
378
379 static void context_state_cb(pa_context *c, void *userdata) {
380 fd_info *i = userdata;
381 assert(c);
382
383 switch (pa_context_get_state(c)) {
384 case PA_CONTEXT_READY:
385 case PA_CONTEXT_TERMINATED:
386 case PA_CONTEXT_FAILED:
387 pa_threaded_mainloop_signal(i->mainloop, 0);
388 break;
389
390 case PA_CONTEXT_UNCONNECTED:
391 case PA_CONTEXT_CONNECTING:
392 case PA_CONTEXT_AUTHORIZING:
393 case PA_CONTEXT_SETTING_NAME:
394 break;
395 }
396 }
397
398 static void reset_params(fd_info *i) {
399 assert(i);
400
401 i->sample_spec.format = PA_SAMPLE_U8;
402 i->sample_spec.channels = 1;
403 i->sample_spec.rate = 8000;
404 i->fragment_size = 0;
405 i->n_fragments = 0;
406 }
407
408 static const char *client_name(char *buf, size_t n) {
409 char p[PATH_MAX];
410 const char *e;
411
412 if ((e = getenv("PADSP_CLIENT_NAME")))
413 return e;
414
415 if (pa_get_binary_name(p, sizeof(p)))
416 snprintf(buf, n, "OSS Emulation[%s]", p);
417 else
418 snprintf(buf, n, "OSS");
419
420 return buf;
421 }
422
423 static const char *stream_name(void) {
424 const char *e;
425
426 if ((e = getenv("PADSP_STREAM_NAME")))
427 return e;
428
429 return "Audio Stream";
430 }
431
432 static void atfork_prepare(void) {
433 fd_info *i;
434
435 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
436
437 function_enter();
438
439 pthread_mutex_lock(&fd_infos_mutex);
440
441 for (i = fd_infos; i; i = i->next) {
442 pthread_mutex_lock(&i->mutex);
443 pa_threaded_mainloop_lock(i->mainloop);
444 }
445
446 pthread_mutex_lock(&func_mutex);
447
448
449 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
450 }
451
452 static void atfork_parent(void) {
453 fd_info *i;
454
455 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
456
457 pthread_mutex_unlock(&func_mutex);
458
459 for (i = fd_infos; i; i = i->next) {
460 pa_threaded_mainloop_unlock(i->mainloop);
461 pthread_mutex_unlock(&i->mutex);
462 }
463
464 pthread_mutex_unlock(&fd_infos_mutex);
465
466 function_exit();
467
468 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
469 }
470
471 static void atfork_child(void) {
472 fd_info *i;
473
474 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
475
476 /* We do only the bare minimum to get all fds closed */
477 pthread_mutex_init(&func_mutex, NULL);
478 pthread_mutex_init(&fd_infos_mutex, NULL);
479
480 for (i = fd_infos; i; i = i->next) {
481 pthread_mutex_init(&i->mutex, NULL);
482
483 if (i->context) {
484 pa_context_disconnect(i->context);
485 pa_context_unref(i->context);
486 i->context = NULL;
487 }
488
489 if (i->play_stream) {
490 pa_stream_unref(i->play_stream);
491 i->play_stream = NULL;
492 }
493
494 if (i->rec_stream) {
495 pa_stream_unref(i->rec_stream);
496 i->rec_stream = NULL;
497 }
498
499 if (i->app_fd >= 0) {
500 close(i->app_fd);
501 i->app_fd = -1;
502 }
503
504 if (i->thread_fd >= 0) {
505 close(i->thread_fd);
506 i->thread_fd = -1;
507 }
508
509 i->unusable = 1;
510 }
511
512 function_exit();
513
514 debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
515 }
516
517 static void install_atfork(void) {
518 pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
519 }
520
521 static void stream_success_cb(pa_stream *s, int success, void *userdata) {
522 fd_info *i = userdata;
523
524 assert(s);
525 assert(i);
526
527 i->operation_success = success;
528 pa_threaded_mainloop_signal(i->mainloop, 0);
529 }
530
531 static void context_success_cb(pa_context *c, int success, void *userdata) {
532 fd_info *i = userdata;
533
534 assert(c);
535 assert(i);
536
537 i->operation_success = success;
538 pa_threaded_mainloop_signal(i->mainloop, 0);
539 }
540
541 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
542 fd_info *i;
543 int sfds[2] = { -1, -1 };
544 char name[64];
545 static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
546
547 debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
548
549 signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
550
551 pthread_once(&install_atfork_once, install_atfork);
552
553 if (!(i = malloc(sizeof(fd_info)))) {
554 *_errno = ENOMEM;
555 goto fail;
556 }
557
558 i->app_fd = i->thread_fd = -1;
559 i->type = type;
560
561 i->mainloop = NULL;
562 i->context = NULL;
563 i->play_stream = NULL;
564 i->rec_stream = NULL;
565 i->io_event = NULL;
566 i->io_flags = 0;
567 pthread_mutex_init(&i->mutex, NULL);
568 i->ref = 1;
569 i->buf = NULL;
570 i->rec_offset = 0;
571 i->unusable = 0;
572 pa_cvolume_reset(&i->sink_volume, 2);
573 pa_cvolume_reset(&i->source_volume, 2);
574 i->volume_modify_count = 0;
575 i->sink_index = (uint32_t) -1;
576 i->source_index = (uint32_t) -1;
577 PA_LLIST_INIT(fd_info, i);
578
579 reset_params(i);
580
581 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
582 *_errno = errno;
583 debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
584 goto fail;
585 }
586
587 i->app_fd = sfds[0];
588 i->thread_fd = sfds[1];
589
590 if (!(i->mainloop = pa_threaded_mainloop_new())) {
591 *_errno = EIO;
592 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
593 goto fail;
594 }
595
596 if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
597 *_errno = EIO;
598 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
599 goto fail;
600 }
601
602 pa_context_set_state_callback(i->context, context_state_cb, i);
603
604 if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
605 *_errno = ECONNREFUSED;
606 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
607 goto fail;
608 }
609
610 pa_threaded_mainloop_lock(i->mainloop);
611
612 if (pa_threaded_mainloop_start(i->mainloop) < 0) {
613 *_errno = EIO;
614 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
615 goto unlock_and_fail;
616 }
617
618 /* Wait until the context is ready */
619 pa_threaded_mainloop_wait(i->mainloop);
620
621 if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
622 *_errno = ECONNREFUSED;
623 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
624 goto unlock_and_fail;
625 }
626
627 pa_threaded_mainloop_unlock(i->mainloop);
628 return i;
629
630 unlock_and_fail:
631
632 pa_threaded_mainloop_unlock(i->mainloop);
633
634 fail:
635
636 if (i)
637 fd_info_unref(i);
638
639 return NULL;
640 }
641
642 static void fd_info_add_to_list(fd_info *i) {
643 assert(i);
644
645 pthread_mutex_lock(&fd_infos_mutex);
646 PA_LLIST_PREPEND(fd_info, fd_infos, i);
647 pthread_mutex_unlock(&fd_infos_mutex);
648
649 fd_info_ref(i);
650 }
651
652 static void fd_info_remove_from_list(fd_info *i) {
653 assert(i);
654
655 pthread_mutex_lock(&fd_infos_mutex);
656 PA_LLIST_REMOVE(fd_info, fd_infos, i);
657 pthread_mutex_unlock(&fd_infos_mutex);
658
659 fd_info_unref(i);
660 }
661
662 static fd_info* fd_info_find(int fd) {
663 fd_info *i;
664
665 pthread_mutex_lock(&fd_infos_mutex);
666
667 for (i = fd_infos; i; i = i->next)
668 if (i->app_fd == fd && !i->unusable) {
669 fd_info_ref(i);
670 break;
671 }
672
673 pthread_mutex_unlock(&fd_infos_mutex);
674
675 return i;
676 }
677
678 static void fix_metrics(fd_info *i) {
679 size_t fs;
680 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
681
682 fs = pa_frame_size(&i->sample_spec);
683
684 /* Don't fix things more than necessary */
685 if ((i->fragment_size % fs) == 0 &&
686 i->n_fragments >= 2 &&
687 i->fragment_size > 0)
688 return;
689
690 i->fragment_size = (i->fragment_size/fs)*fs;
691
692 /* Number of fragments set? */
693 if (i->n_fragments < 2) {
694 if (i->fragment_size > 0) {
695 i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
696 if (i->n_fragments < 2)
697 i->n_fragments = 2;
698 } else
699 i->n_fragments = 12;
700 }
701
702 /* Fragment size set? */
703 if (i->fragment_size <= 0) {
704 i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
705 if (i->fragment_size < 1024)
706 i->fragment_size = 1024;
707 }
708
709 debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
710 debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
711 }
712
713 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
714 fd_info *i = userdata;
715 assert(s);
716
717 if (i->io_event) {
718 pa_mainloop_api *api;
719 size_t n;
720
721 api = pa_threaded_mainloop_get_api(i->mainloop);
722
723 if (s == i->play_stream) {
724 n = pa_stream_writable_size(i->play_stream);
725 if (n == (size_t)-1) {
726 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
727 pa_strerror(pa_context_errno(i->context)));
728 }
729
730 if (n >= i->fragment_size)
731 i->io_flags |= PA_IO_EVENT_INPUT;
732 else
733 i->io_flags &= ~PA_IO_EVENT_INPUT;
734 }
735
736 if (s == i->rec_stream) {
737 n = pa_stream_readable_size(i->rec_stream);
738 if (n == (size_t)-1) {
739 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
740 pa_strerror(pa_context_errno(i->context)));
741 }
742
743 if (n >= i->fragment_size)
744 i->io_flags |= PA_IO_EVENT_OUTPUT;
745 else
746 i->io_flags &= ~PA_IO_EVENT_OUTPUT;
747 }
748
749 api->io_enable(i->io_event, i->io_flags);
750 }
751 }
752
753 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
754 fd_info *i = userdata;
755 assert(s);
756
757 pa_threaded_mainloop_signal(i->mainloop, 0);
758 }
759
760 static void fd_info_shutdown(fd_info *i) {
761 assert(i);
762
763 if (i->io_event) {
764 pa_mainloop_api *api;
765 api = pa_threaded_mainloop_get_api(i->mainloop);
766 api->io_free(i->io_event);
767 i->io_event = NULL;
768 i->io_flags = 0;
769 }
770
771 if (i->thread_fd >= 0) {
772 close(i->thread_fd);
773 i->thread_fd = -1;
774 }
775 }
776
777 static int fd_info_copy_data(fd_info *i, int force) {
778 size_t n;
779
780 if (!i->play_stream && !i->rec_stream)
781 return -1;
782
783 if ((i->play_stream) && (pa_stream_get_state(i->play_stream) == PA_STREAM_READY)) {
784 n = pa_stream_writable_size(i->play_stream);
785
786 if (n == (size_t)-1) {
787 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
788 pa_strerror(pa_context_errno(i->context)));
789 return -1;
790 }
791
792 while (n >= i->fragment_size || force) {
793 ssize_t r;
794
795 if (!i->buf) {
796 if (!(i->buf = malloc(i->fragment_size))) {
797 debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
798 return -1;
799 }
800 }
801
802 if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
803
804 if (errno == EAGAIN)
805 break;
806
807 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
808 return -1;
809 }
810
811 if (pa_stream_write(i->play_stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
812 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
813 return -1;
814 }
815
816 i->buf = NULL;
817
818 assert(n >= (size_t) r);
819 n -= r;
820 }
821
822 if (n >= i->fragment_size)
823 i->io_flags |= PA_IO_EVENT_INPUT;
824 else
825 i->io_flags &= ~PA_IO_EVENT_INPUT;
826 }
827
828 if ((i->rec_stream) && (pa_stream_get_state(i->rec_stream) == PA_STREAM_READY)) {
829 n = pa_stream_readable_size(i->rec_stream);
830
831 if (n == (size_t)-1) {
832 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
833 pa_strerror(pa_context_errno(i->context)));
834 return -1;
835 }
836
837 while (n >= i->fragment_size || force) {
838 ssize_t r;
839 const void *data;
840 const char *buf;
841 size_t len;
842
843 if (pa_stream_peek(i->rec_stream, &data, &len) < 0) {
844 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i->context)));
845 return -1;
846 }
847
848 if (!data)
849 break;
850
851 buf = (const char*)data + i->rec_offset;
852
853 if ((r = write(i->thread_fd, buf, len - i->rec_offset)) <= 0) {
854
855 if (errno == EAGAIN)
856 break;
857
858 debug(DEBUG_LEVEL_NORMAL, __FILE__": write(): %s\n", strerror(errno));
859 return -1;
860 }
861
862 assert((size_t)r <= len - i->rec_offset);
863 i->rec_offset += r;
864
865 if (i->rec_offset == len) {
866 if (pa_stream_drop(i->rec_stream) < 0) {
867 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i->context)));
868 return -1;
869 }
870 i->rec_offset = 0;
871 }
872
873 assert(n >= (size_t) r);
874 n -= r;
875 }
876
877 if (n >= i->fragment_size)
878 i->io_flags |= PA_IO_EVENT_OUTPUT;
879 else
880 i->io_flags &= ~PA_IO_EVENT_OUTPUT;
881 }
882
883 if (i->io_event) {
884 pa_mainloop_api *api;
885
886 api = pa_threaded_mainloop_get_api(i->mainloop);
887 api->io_enable(i->io_event, i->io_flags);
888 }
889
890 return 0;
891 }
892
893 static void stream_state_cb(pa_stream *s, void * userdata) {
894 fd_info *i = userdata;
895 assert(s);
896
897 switch (pa_stream_get_state(s)) {
898
899 case PA_STREAM_READY:
900 debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
901 break;
902
903 case PA_STREAM_FAILED:
904 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
905 fd_info_shutdown(i);
906 break;
907
908 case PA_STREAM_TERMINATED:
909 case PA_STREAM_UNCONNECTED:
910 case PA_STREAM_CREATING:
911 break;
912 }
913 }
914
915 static int create_playback_stream(fd_info *i) {
916 pa_buffer_attr attr;
917 int n;
918
919 assert(i);
920
921 fix_metrics(i);
922
923 if (!(i->play_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
924 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
925 goto fail;
926 }
927
928 pa_stream_set_state_callback(i->play_stream, stream_state_cb, i);
929 pa_stream_set_write_callback(i->play_stream, stream_request_cb, i);
930 pa_stream_set_latency_update_callback(i->play_stream, stream_latency_update_cb, i);
931
932 memset(&attr, 0, sizeof(attr));
933 attr.maxlength = i->fragment_size * (i->n_fragments+1);
934 attr.tlength = i->fragment_size * i->n_fragments;
935 attr.prebuf = i->fragment_size;
936 attr.minreq = i->fragment_size;
937
938 if (pa_stream_connect_playback(i->play_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
939 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
940 goto fail;
941 }
942
943 n = i->fragment_size;
944 setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
945 n = i->fragment_size;
946 setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
947
948 return 0;
949
950 fail:
951 return -1;
952 }
953
954 static int create_record_stream(fd_info *i) {
955 pa_buffer_attr attr;
956 int n;
957
958 assert(i);
959
960 fix_metrics(i);
961
962 if (!(i->rec_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
963 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
964 goto fail;
965 }
966
967 pa_stream_set_state_callback(i->rec_stream, stream_state_cb, i);
968 pa_stream_set_read_callback(i->rec_stream, stream_request_cb, i);
969 pa_stream_set_latency_update_callback(i->rec_stream, stream_latency_update_cb, i);
970
971 memset(&attr, 0, sizeof(attr));
972 attr.maxlength = i->fragment_size * (i->n_fragments+1);
973 attr.fragsize = i->fragment_size;
974
975 if (pa_stream_connect_record(i->rec_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) < 0) {
976 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
977 goto fail;
978 }
979
980 n = i->fragment_size;
981 setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
982 n = i->fragment_size;
983 setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
984
985 return 0;
986
987 fail:
988 return -1;
989 }
990
991 static void free_streams(fd_info *i) {
992 assert(i);
993
994 if (i->play_stream) {
995 pa_stream_disconnect(i->play_stream);
996 pa_stream_unref(i->play_stream);
997 i->play_stream = NULL;
998 }
999
1000 if (i->rec_stream) {
1001 pa_stream_disconnect(i->rec_stream);
1002 pa_stream_unref(i->rec_stream);
1003 i->rec_stream = NULL;
1004 }
1005 }
1006
1007 static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
1008 fd_info *i = userdata;
1009
1010 pa_threaded_mainloop_signal(i->mainloop, 0);
1011
1012 if (flags & PA_IO_EVENT_INPUT) {
1013
1014 if (!i->play_stream) {
1015 if (create_playback_stream(i) < 0)
1016 goto fail;
1017 } else {
1018 if (fd_info_copy_data(i, 0) < 0)
1019 goto fail;
1020 }
1021
1022 } else if (flags & PA_IO_EVENT_OUTPUT) {
1023
1024 if (!i->rec_stream) {
1025 if (create_record_stream(i) < 0)
1026 goto fail;
1027 } else {
1028 if (fd_info_copy_data(i, 0) < 0)
1029 goto fail;
1030 }
1031
1032 } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
1033 goto fail;
1034
1035 return;
1036
1037 fail:
1038 /* We can't do anything better than removing the event source */
1039 fd_info_shutdown(i);
1040 }
1041
1042 static int dsp_open(int flags, int *_errno) {
1043 fd_info *i;
1044 pa_mainloop_api *api;
1045 int ret;
1046 int f;
1047
1048 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
1049
1050 if (!(i = fd_info_new(FD_INFO_STREAM, _errno)))
1051 return -1;
1052
1053 if ((flags & O_NONBLOCK) == O_NONBLOCK) {
1054 if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
1055 fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
1056 }
1057 if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
1058 fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
1059
1060 fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
1061 fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
1062
1063 pa_threaded_mainloop_lock(i->mainloop);
1064 api = pa_threaded_mainloop_get_api(i->mainloop);
1065
1066 switch (flags & O_ACCMODE) {
1067 case O_RDONLY:
1068 i->io_flags = PA_IO_EVENT_OUTPUT;
1069 shutdown(i->thread_fd, SHUT_RD);
1070 shutdown(i->app_fd, SHUT_WR);
1071 break;
1072 case O_WRONLY:
1073 i->io_flags = PA_IO_EVENT_INPUT;
1074 shutdown(i->thread_fd, SHUT_WR);
1075 shutdown(i->app_fd, SHUT_RD);
1076 break;
1077 case O_RDWR:
1078 i->io_flags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
1079 break;
1080 default:
1081 return -1;
1082 }
1083
1084 if (!(i->io_event = api->io_new(api, i->thread_fd, i->io_flags, io_event_cb, i)))
1085 goto fail;
1086
1087 pa_threaded_mainloop_unlock(i->mainloop);
1088
1089 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
1090
1091 fd_info_add_to_list(i);
1092 ret = i->app_fd;
1093 fd_info_unref(i);
1094
1095 return ret;
1096
1097 fail:
1098 pa_threaded_mainloop_unlock(i->mainloop);
1099
1100 if (i)
1101 fd_info_unref(i);
1102
1103 *_errno = EIO;
1104
1105 debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
1106
1107 return -1;
1108 }
1109
1110 static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
1111 fd_info *i = userdata;
1112
1113 if (!si && eol < 0) {
1114 i->operation_success = 0;
1115 pa_threaded_mainloop_signal(i->mainloop, 0);
1116 return;
1117 }
1118
1119 if (eol)
1120 return;
1121
1122 if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
1123 i->volume_modify_count++;
1124
1125 i->sink_volume = si->volume;
1126 i->sink_index = si->index;
1127
1128 i->operation_success = 1;
1129 pa_threaded_mainloop_signal(i->mainloop, 0);
1130 }
1131
1132 static void source_info_cb(pa_context *context, const pa_source_info *si, int eol, void *userdata) {
1133 fd_info *i = userdata;
1134
1135 if (!si && eol < 0) {
1136 i->operation_success = 0;
1137 pa_threaded_mainloop_signal(i->mainloop, 0);
1138 return;
1139 }
1140
1141 if (eol)
1142 return;
1143
1144 if (!pa_cvolume_equal(&i->source_volume, &si->volume))
1145 i->volume_modify_count++;
1146
1147 i->source_volume = si->volume;
1148 i->source_index = si->index;
1149
1150 i->operation_success = 1;
1151 pa_threaded_mainloop_signal(i->mainloop, 0);
1152 }
1153
1154 static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1155 fd_info *i = userdata;
1156 pa_operation *o = NULL;
1157
1158 if (i->sink_index != idx)
1159 return;
1160
1161 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
1162 return;
1163
1164 if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
1165 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1166 return;
1167 }
1168
1169 pa_operation_unref(o);
1170 }
1171
1172 static int mixer_open(int flags, int *_errno) {
1173 fd_info *i;
1174 pa_operation *o = NULL;
1175 int ret;
1176
1177 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
1178
1179 if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
1180 return -1;
1181
1182 pa_threaded_mainloop_lock(i->mainloop);
1183
1184 pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
1185
1186 if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
1187 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1188 *_errno = EIO;
1189 goto fail;
1190 }
1191
1192 i->operation_success = 0;
1193 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1194 pa_threaded_mainloop_wait(i->mainloop);
1195 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1196 }
1197
1198 pa_operation_unref(o);
1199 o = NULL;
1200
1201 if (!i->operation_success) {
1202 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
1203 *_errno = EIO;
1204 goto fail;
1205 }
1206
1207 /* Get sink info */
1208
1209 if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
1210 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1211 *_errno = EIO;
1212 goto fail;
1213 }
1214
1215 i->operation_success = 0;
1216 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1217 pa_threaded_mainloop_wait(i->mainloop);
1218 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1219 }
1220
1221 pa_operation_unref(o);
1222 o = NULL;
1223
1224 if (!i->operation_success) {
1225 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
1226 *_errno = EIO;
1227 goto fail;
1228 }
1229
1230 /* Get source info */
1231
1232 if (!(o = pa_context_get_source_info_by_name(i->context, NULL, source_info_cb, i))) {
1233 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1234 *_errno = EIO;
1235 goto fail;
1236 }
1237
1238 i->operation_success = 0;
1239 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1240 pa_threaded_mainloop_wait(i->mainloop);
1241 CONTEXT_CHECK_DEAD_GOTO(i, fail);
1242 }
1243
1244 pa_operation_unref(o);
1245 o = NULL;
1246
1247 if (!i->operation_success) {
1248 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
1249 *_errno = EIO;
1250 goto fail;
1251 }
1252
1253 pa_threaded_mainloop_unlock(i->mainloop);
1254
1255 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
1256
1257 fd_info_add_to_list(i);
1258 ret = i->app_fd;
1259 fd_info_unref(i);
1260
1261 return ret;
1262
1263 fail:
1264 if (o)
1265 pa_operation_unref(o);
1266
1267 pa_threaded_mainloop_unlock(i->mainloop);
1268
1269 if (i)
1270 fd_info_unref(i);
1271
1272 *_errno = EIO;
1273
1274 debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
1275
1276 return -1;
1277 }
1278
1279 static int sndstat_open(int flags, int *_errno) {
1280 static const char sndstat[] =
1281 "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
1282 "Kernel: POSIX\n"
1283 "Config options: 0\n"
1284 "\n"
1285 "Installed drivers:\n"
1286 "Type 255: PulseAudio Virtual OSS\n"
1287 "\n"
1288 "Card config:\n"
1289 "PulseAudio Virtual OSS\n"
1290 "\n"
1291 "Audio devices:\n"
1292 "0: PulseAudio Virtual OSS\n"
1293 "\n"
1294 "Synth devices: NOT ENABLED IN CONFIG\n"
1295 "\n"
1296 "Midi devices:\n"
1297 "\n"
1298 "Timers:\n"
1299 "\n"
1300 "Mixers:\n"
1301 "0: PulseAudio Virtual OSS\n";
1302
1303 char fn[] = "/tmp/padsp-sndstat-XXXXXX";
1304 mode_t u;
1305 int fd = -1;
1306 int e;
1307
1308 debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
1309
1310 if (flags != O_RDONLY
1311 #ifdef O_LARGEFILE
1312 && flags != (O_RDONLY|O_LARGEFILE)
1313 #endif
1314 ) {
1315 *_errno = EACCES;
1316 debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
1317 goto fail;
1318 }
1319
1320 u = umask(0077);
1321 fd = mkstemp(fn);
1322 e = errno;
1323 umask(u);
1324
1325 if (fd < 0) {
1326 *_errno = e;
1327 debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
1328 goto fail;
1329 }
1330
1331 unlink(fn);
1332
1333 if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
1334 *_errno = errno;
1335 debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
1336 goto fail;
1337 }
1338
1339 if (lseek(fd, SEEK_SET, 0) < 0) {
1340 *_errno = errno;
1341 debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
1342 goto fail;
1343 }
1344
1345 return fd;
1346
1347 fail:
1348 if (fd >= 0)
1349 close(fd);
1350 return -1;
1351 }
1352
1353 int open(const char *filename, int flags, ...) {
1354 va_list args;
1355 mode_t mode = 0;
1356 int r, _errno = 0;
1357
1358 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
1359
1360 va_start(args, flags);
1361 if (flags & O_CREAT) {
1362 if (sizeof(mode_t) < sizeof(int))
1363 mode = va_arg(args, int);
1364 else
1365 mode = va_arg(args, mode_t);
1366 }
1367 va_end(args);
1368
1369 if (!function_enter()) {
1370 LOAD_OPEN_FUNC();
1371 return _open(filename, flags, mode);
1372 }
1373
1374 if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
1375 r = dsp_open(flags, &_errno);
1376 } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
1377 r = mixer_open(flags, &_errno);
1378 } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
1379 r = sndstat_open(flags, &_errno);
1380 } else {
1381 function_exit();
1382 LOAD_OPEN_FUNC();
1383 return _open(filename, flags, mode);
1384 }
1385
1386 function_exit();
1387
1388 if (_errno)
1389 errno = _errno;
1390
1391 return r;
1392 }
1393
1394 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1395 int ret = -1;
1396
1397 switch (request) {
1398 case SOUND_MIXER_READ_DEVMASK :
1399 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
1400
1401 *(int*) argp = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
1402 break;
1403
1404 case SOUND_MIXER_READ_RECMASK :
1405 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
1406
1407 *(int*) argp = SOUND_MASK_IGAIN;
1408 break;
1409
1410 case SOUND_MIXER_READ_STEREODEVS:
1411 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
1412
1413 pa_threaded_mainloop_lock(i->mainloop);
1414 *(int*) argp = 0;
1415 if (i->sink_volume.channels > 1)
1416 *(int*) argp |= SOUND_MASK_PCM;
1417 if (i->source_volume.channels > 1)
1418 *(int*) argp |= SOUND_MASK_IGAIN;
1419 pa_threaded_mainloop_unlock(i->mainloop);
1420
1421 break;
1422
1423 case SOUND_MIXER_READ_RECSRC:
1424 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
1425
1426 *(int*) argp = SOUND_MASK_IGAIN;
1427 break;
1428
1429 case SOUND_MIXER_WRITE_RECSRC:
1430 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_RECSRC\n");
1431 break;
1432
1433 case SOUND_MIXER_READ_CAPS:
1434 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
1435
1436 *(int*) argp = 0;
1437 break;
1438
1439 case SOUND_MIXER_READ_PCM:
1440 case SOUND_MIXER_READ_IGAIN: {
1441 pa_cvolume *v;
1442
1443 if (request == SOUND_MIXER_READ_PCM)
1444 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
1445 else
1446 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_IGAIN\n");
1447
1448 pa_threaded_mainloop_lock(i->mainloop);
1449
1450 if (request == SOUND_MIXER_READ_PCM)
1451 v = &i->sink_volume;
1452 else
1453 v = &i->source_volume;
1454
1455 *(int*) argp =
1456 ((v->values[0]*100/PA_VOLUME_NORM)) |
1457 ((v->values[v->channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
1458
1459 pa_threaded_mainloop_unlock(i->mainloop);
1460
1461 break;
1462 }
1463
1464 case SOUND_MIXER_WRITE_PCM:
1465 case SOUND_MIXER_WRITE_IGAIN: {
1466 pa_cvolume v, *pv;
1467
1468 if (request == SOUND_MIXER_READ_PCM)
1469 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
1470 else
1471 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
1472
1473 pa_threaded_mainloop_lock(i->mainloop);
1474
1475 if (request == SOUND_MIXER_READ_PCM) {
1476 v = i->sink_volume;
1477 pv = &i->sink_volume;
1478 } else {
1479 v = i->source_volume;
1480 pv = &i->source_volume;
1481 }
1482
1483 pv->values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
1484 pv->values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
1485
1486 if (!pa_cvolume_equal(pv, &v)) {
1487 pa_operation *o;
1488
1489 if (request == SOUND_MIXER_READ_PCM)
1490 o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, NULL, NULL);
1491 else
1492 o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, NULL, NULL);
1493
1494 if (!o)
1495 debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
1496 else {
1497
1498 i->operation_success = 0;
1499 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1500 CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
1501
1502 pa_threaded_mainloop_wait(i->mainloop);
1503 }
1504 exit_loop:
1505
1506 if (!i->operation_success)
1507 debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
1508
1509 pa_operation_unref(o);
1510 }
1511
1512 /* We don't wait for completion here */
1513 i->volume_modify_count++;
1514 }
1515
1516 pa_threaded_mainloop_unlock(i->mainloop);
1517
1518 break;
1519 }
1520
1521 case SOUND_MIXER_INFO: {
1522 mixer_info *mi = argp;
1523
1524 debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
1525
1526 memset(mi, 0, sizeof(mixer_info));
1527 strncpy(mi->id, "PULSEAUDIO", sizeof(mi->id));
1528 strncpy(mi->name, "PulseAudio Virtual OSS", sizeof(mi->name));
1529 pa_threaded_mainloop_lock(i->mainloop);
1530 mi->modify_counter = i->volume_modify_count;
1531 pa_threaded_mainloop_unlock(i->mainloop);
1532 break;
1533 }
1534
1535 default:
1536 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
1537
1538 *_errno = EINVAL;
1539 goto fail;
1540 }
1541
1542 ret = 0;
1543
1544 fail:
1545
1546 return ret;
1547 }
1548
1549 static int map_format(int *fmt, pa_sample_spec *ss) {
1550
1551 switch (*fmt) {
1552 case AFMT_MU_LAW:
1553 ss->format = PA_SAMPLE_ULAW;
1554 break;
1555
1556 case AFMT_A_LAW:
1557 ss->format = PA_SAMPLE_ALAW;
1558 break;
1559
1560 case AFMT_S8:
1561 *fmt = AFMT_U8;
1562 /* fall through */
1563 case AFMT_U8:
1564 ss->format = PA_SAMPLE_U8;
1565 break;
1566
1567 case AFMT_U16_BE:
1568 *fmt = AFMT_S16_BE;
1569 /* fall through */
1570 case AFMT_S16_BE:
1571 ss->format = PA_SAMPLE_S16BE;
1572 break;
1573
1574 case AFMT_U16_LE:
1575 *fmt = AFMT_S16_LE;
1576 /* fall through */
1577 case AFMT_S16_LE:
1578 ss->format = PA_SAMPLE_S16LE;
1579 break;
1580
1581 default:
1582 ss->format = PA_SAMPLE_S16NE;
1583 *fmt = AFMT_S16_NE;
1584 break;
1585 }
1586
1587 return 0;
1588 }
1589
1590 static int map_format_back(pa_sample_format_t format) {
1591 switch (format) {
1592 case PA_SAMPLE_S16LE: return AFMT_S16_LE;
1593 case PA_SAMPLE_S16BE: return AFMT_S16_BE;
1594 case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
1595 case PA_SAMPLE_ALAW: return AFMT_A_LAW;
1596 case PA_SAMPLE_U8: return AFMT_U8;
1597 default:
1598 abort();
1599 }
1600 }
1601
1602 static int dsp_flush_fd(int fd) {
1603 #ifdef SIOCINQ
1604 int l;
1605
1606 if (ioctl(fd, SIOCINQ, &l) < 0) {
1607 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1608 return -1;
1609 }
1610
1611 while (l > 0) {
1612 char buf[1024];
1613 size_t k;
1614
1615 k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
1616 if (read(fd, buf, k) < 0)
1617 debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
1618 l -= k;
1619 }
1620
1621 return 0;
1622 #else
1623 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1624 return 0;
1625 #endif
1626 }
1627
1628 static int dsp_flush_socket(fd_info *i) {
1629 int res = 0;
1630
1631 if ((i->thread_fd < 0) && (i->app_fd < 0))
1632 return -1;
1633
1634 if (i->thread_fd >= 0)
1635 res = dsp_flush_fd(i->thread_fd);
1636
1637 if (res < 0)
1638 return res;
1639
1640 if (i->app_fd >= 0)
1641 res = dsp_flush_fd(i->app_fd);
1642
1643 if (res < 0)
1644 return res;
1645
1646 return 0;
1647 }
1648
1649 static int dsp_empty_socket(fd_info *i) {
1650 #ifdef SIOCINQ
1651 int ret = -1;
1652
1653 /* Empty the socket */
1654 for (;;) {
1655 int l;
1656
1657 if (i->thread_fd < 0)
1658 break;
1659
1660 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
1661 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
1662 break;
1663 }
1664
1665 if (!l) {
1666 ret = 0;
1667 break;
1668 }
1669
1670 pa_threaded_mainloop_wait(i->mainloop);
1671 }
1672
1673 return ret;
1674 #else
1675 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1676 return 0;
1677 #endif
1678 }
1679
1680 static int dsp_drain(fd_info *i) {
1681 pa_operation *o = NULL;
1682 int r = -1;
1683
1684 if (!i->mainloop)
1685 return 0;
1686
1687 debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
1688
1689 pa_threaded_mainloop_lock(i->mainloop);
1690
1691 if (dsp_empty_socket(i) < 0)
1692 goto fail;
1693
1694 if (!i->play_stream)
1695 goto fail;
1696
1697 debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
1698
1699 if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
1700 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
1701 goto fail;
1702 }
1703
1704 i->operation_success = 0;
1705 while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
1706 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1707
1708 pa_threaded_mainloop_wait(i->mainloop);
1709 }
1710
1711 if (!i->operation_success) {
1712 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
1713 goto fail;
1714 }
1715
1716 r = 0;
1717
1718 fail:
1719
1720 if (o)
1721 pa_operation_unref(o);
1722
1723 pa_threaded_mainloop_unlock(i->mainloop);
1724
1725 return 0;
1726 }
1727
1728 static int dsp_trigger(fd_info *i) {
1729 pa_operation *o = NULL;
1730 int r = -1;
1731
1732 if (!i->play_stream)
1733 return 0;
1734
1735 pa_threaded_mainloop_lock(i->mainloop);
1736
1737 if (dsp_empty_socket(i) < 0)
1738 goto fail;
1739
1740 debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
1741
1742 if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
1743 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1744 goto fail;
1745 }
1746
1747 i->operation_success = 0;
1748 while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
1749 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
1750
1751 pa_threaded_mainloop_wait(i->mainloop);
1752 }
1753
1754 if (!i->operation_success) {
1755 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
1756 goto fail;
1757 }
1758
1759 r = 0;
1760
1761 fail:
1762
1763 if (o)
1764 pa_operation_unref(o);
1765
1766 pa_threaded_mainloop_unlock(i->mainloop);
1767
1768 return 0;
1769 }
1770
1771 static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
1772 int ret = -1;
1773
1774 switch (request) {
1775 case SNDCTL_DSP_SETFMT: {
1776 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
1777
1778 pa_threaded_mainloop_lock(i->mainloop);
1779
1780 if (*(int*) argp == AFMT_QUERY)
1781 *(int*) argp = map_format_back(i->sample_spec.format);
1782 else {
1783 map_format((int*) argp, &i->sample_spec);
1784 free_streams(i);
1785 }
1786
1787 pa_threaded_mainloop_unlock(i->mainloop);
1788 break;
1789 }
1790
1791 case SNDCTL_DSP_SPEED: {
1792 pa_sample_spec ss;
1793 int valid;
1794 char t[256];
1795
1796 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
1797
1798 pa_threaded_mainloop_lock(i->mainloop);
1799
1800 ss = i->sample_spec;
1801 ss.rate = *(int*) argp;
1802
1803 if ((valid = pa_sample_spec_valid(&ss))) {
1804 i->sample_spec = ss;
1805 free_streams(i);
1806 }
1807
1808 debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
1809
1810 pa_threaded_mainloop_unlock(i->mainloop);
1811
1812 if (!valid) {
1813 *_errno = EINVAL;
1814 goto fail;
1815 }
1816
1817 break;
1818 }
1819
1820 case SNDCTL_DSP_STEREO:
1821 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
1822
1823 pa_threaded_mainloop_lock(i->mainloop);
1824
1825 i->sample_spec.channels = *(int*) argp ? 2 : 1;
1826 free_streams(i);
1827
1828 pa_threaded_mainloop_unlock(i->mainloop);
1829 return 0;
1830
1831 case SNDCTL_DSP_CHANNELS: {
1832 pa_sample_spec ss;
1833 int valid;
1834
1835 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
1836
1837 pa_threaded_mainloop_lock(i->mainloop);
1838
1839 ss = i->sample_spec;
1840 ss.channels = *(int*) argp;
1841
1842 if ((valid = pa_sample_spec_valid(&ss))) {
1843 i->sample_spec = ss;
1844 free_streams(i);
1845 }
1846
1847 pa_threaded_mainloop_unlock(i->mainloop);
1848
1849 if (!valid) {
1850 *_errno = EINVAL;
1851 goto fail;
1852 }
1853
1854 break;
1855 }
1856
1857 case SNDCTL_DSP_GETBLKSIZE:
1858 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
1859
1860 pa_threaded_mainloop_lock(i->mainloop);
1861
1862 fix_metrics(i);
1863 *(int*) argp = i->fragment_size;
1864
1865 pa_threaded_mainloop_unlock(i->mainloop);
1866
1867 break;
1868
1869 case SNDCTL_DSP_SETFRAGMENT:
1870 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
1871
1872 pa_threaded_mainloop_lock(i->mainloop);
1873
1874 i->fragment_size = 1 << (*(int*) argp);
1875 i->n_fragments = (*(int*) argp) >> 16;
1876
1877 /* 0x7FFF means that we can set whatever we like */
1878 if (i->n_fragments == 0x7FFF)
1879 i->n_fragments = 12;
1880
1881 free_streams(i);
1882
1883 pa_threaded_mainloop_unlock(i->mainloop);
1884
1885 break;
1886
1887 case SNDCTL_DSP_GETCAPS:
1888 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
1889
1890 *(int*) argp = DSP_CAP_DUPLEX
1891 #ifdef DSP_CAP_MULTI
1892 | DSP_CAP_MULTI
1893 #endif
1894 ;
1895 break;
1896
1897 case SNDCTL_DSP_GETODELAY: {
1898 int l;
1899
1900 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
1901
1902 pa_threaded_mainloop_lock(i->mainloop);
1903
1904 *(int*) argp = 0;
1905
1906 for (;;) {
1907 pa_usec_t usec;
1908
1909 PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
1910
1911 if (pa_stream_get_latency(i->play_stream, &usec, NULL) >= 0) {
1912 *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
1913 break;
1914 }
1915
1916 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
1917 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
1918 break;
1919 }
1920
1921 pa_threaded_mainloop_wait(i->mainloop);
1922 }
1923
1924 exit_loop:
1925
1926 #ifdef SIOCINQ
1927 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
1928 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
1929 else
1930 *(int*) argp += l;
1931 #else
1932 # warning "Your platform does not support SIOCINQ, something might not work as intended."
1933 #endif
1934
1935 pa_threaded_mainloop_unlock(i->mainloop);
1936
1937 debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
1938
1939 break;
1940 }
1941
1942 case SNDCTL_DSP_RESET: {
1943 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
1944
1945 pa_threaded_mainloop_lock(i->mainloop);
1946
1947 free_streams(i);
1948 dsp_flush_socket(i);
1949 reset_params(i);
1950
1951 pa_threaded_mainloop_unlock(i->mainloop);
1952 break;
1953 }
1954
1955 case SNDCTL_DSP_GETFMTS: {
1956 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
1957
1958 *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
1959 break;
1960 }
1961
1962 case SNDCTL_DSP_POST:
1963 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
1964
1965 if (dsp_trigger(i) < 0)
1966 *_errno = EIO;
1967 break;
1968
1969 case SNDCTL_DSP_SYNC:
1970 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
1971
1972 if (dsp_drain(i) < 0)
1973 *_errno = EIO;
1974
1975 break;
1976
1977 case SNDCTL_DSP_GETOSPACE:
1978 case SNDCTL_DSP_GETISPACE: {
1979 audio_buf_info *bi = (audio_buf_info*) argp;
1980 int l = 0;
1981 size_t k = 0;
1982
1983 if (request == SNDCTL_DSP_GETOSPACE)
1984 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
1985 else
1986 debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETISPACE\n");
1987
1988 pa_threaded_mainloop_lock(i->mainloop);
1989
1990 fix_metrics(i);
1991
1992 if (request == SNDCTL_DSP_GETOSPACE) {
1993 if (i->play_stream) {
1994 if ((k = pa_stream_writable_size(i->play_stream)) == (size_t) -1)
1995 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
1996 } else
1997 k = i->fragment_size * i->n_fragments;
1998
1999 #ifdef SIOCINQ
2000 if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
2001 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
2002 l = 0;
2003 }
2004 #else
2005 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2006 #endif
2007
2008 bi->bytes = k > (size_t) l ? k - l : 0;
2009 } else {
2010 if (i->rec_stream) {
2011 if ((k = pa_stream_readable_size(i->rec_stream)) == (size_t) -1)
2012 debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
2013 } else
2014 k = 0;
2015
2016 #ifdef SIOCINQ
2017 if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
2018 debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
2019 l = 0;
2020 }
2021 #else
2022 # warning "Your platform does not dsp_flush_fd, something might not work as intended."
2023 #endif
2024 bi->bytes = k + l;
2025 }
2026
2027 bi->fragsize = i->fragment_size;
2028 bi->fragstotal = i->n_fragments;
2029 bi->fragments = bi->bytes / bi->fragsize;
2030
2031 pa_threaded_mainloop_unlock(i->mainloop);
2032
2033 debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
2034
2035 break;
2036 }
2037
2038 case SNDCTL_DSP_GETIPTR:
2039 debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETIPTR\n");
2040 goto inval;
2041
2042 case SNDCTL_DSP_GETOPTR:
2043 debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETOPTR\n");
2044 goto inval;
2045
2046 default:
2047 debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
2048
2049 inval:
2050 *_errno = EINVAL;
2051 goto fail;
2052 }
2053
2054 ret = 0;
2055
2056 fail:
2057
2058 return ret;
2059 }
2060
2061 int ioctl(int fd, unsigned long request, ...) {
2062 fd_info *i;
2063 va_list args;
2064 void *argp;
2065 int r, _errno = 0;
2066
2067 debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
2068
2069 va_start(args, request);
2070 argp = va_arg(args, void *);
2071 va_end(args);
2072
2073 if (!function_enter()) {
2074 LOAD_IOCTL_FUNC();
2075 return _ioctl(fd, request, argp);
2076 }
2077
2078 if (!(i = fd_info_find(fd))) {
2079 function_exit();
2080 LOAD_IOCTL_FUNC();
2081 return _ioctl(fd, request, argp);
2082 }
2083
2084 if (i->type == FD_INFO_MIXER)
2085 r = mixer_ioctl(i, request, argp, &_errno);
2086 else
2087 r = dsp_ioctl(i, request, argp, &_errno);
2088
2089 fd_info_unref(i);
2090
2091 if (_errno)
2092 errno = _errno;
2093
2094 function_exit();
2095
2096 return r;
2097 }
2098
2099 int close(int fd) {
2100 fd_info *i;
2101
2102 debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
2103
2104 if (!function_enter()) {
2105 LOAD_CLOSE_FUNC();
2106 return _close(fd);
2107 }
2108
2109 if (!(i = fd_info_find(fd))) {
2110 function_exit();
2111 LOAD_CLOSE_FUNC();
2112 return _close(fd);
2113 }
2114
2115 fd_info_remove_from_list(i);
2116 fd_info_unref(i);
2117
2118 function_exit();
2119
2120 return 0;
2121 }
2122
2123 int access(const char *pathname, int mode) {
2124 debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
2125
2126 if (strcmp(pathname, "/dev/dsp") != 0 &&
2127 strcmp(pathname, "/dev/adsp") != 0 &&
2128 strcmp(pathname, "/dev/sndstat") != 0 &&
2129 strcmp(pathname, "/dev/mixer") != 0) {
2130 LOAD_ACCESS_FUNC();
2131 return _access(pathname, mode);
2132 }
2133
2134 if (mode & (W_OK | X_OK)) {
2135 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
2136 errno = EACCES;
2137 return -1;
2138 }
2139
2140 debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
2141
2142 return 0;
2143 }
2144
2145 #ifdef HAVE_OPEN64
2146
2147 int open64(const char *filename, int flags, ...) {
2148 va_list args;
2149 mode_t mode = 0;
2150
2151 debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
2152
2153 va_start(args, flags);
2154 if (flags & O_CREAT)
2155 mode = va_arg(args, mode_t);
2156 va_end(args);
2157
2158 if (strcmp(filename, "/dev/dsp") != 0 &&
2159 strcmp(filename, "/dev/adsp") != 0 &&
2160 strcmp(filename, "/dev/sndstat") != 0 &&
2161 strcmp(filename, "/dev/mixer") != 0) {
2162 LOAD_OPEN64_FUNC();
2163 return _open64(filename, flags, mode);
2164 }
2165
2166 return open(filename, flags, mode);
2167 }
2168
2169 #endif
2170
2171 FILE* fopen(const char *filename, const char *mode) {
2172 FILE *f = NULL;
2173 int fd;
2174 mode_t m;
2175
2176 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
2177
2178 if (strcmp(filename, "/dev/dsp") != 0 &&
2179 strcmp(filename, "/dev/adsp") != 0 &&
2180 strcmp(filename, "/dev/sndstat") != 0 &&
2181 strcmp(filename, "/dev/mixer") != 0) {
2182 LOAD_FOPEN_FUNC();
2183 return _fopen(filename, mode);
2184 }
2185
2186 switch (mode[0]) {
2187 case 'r':
2188 m = O_RDONLY;
2189 break;
2190 case 'w':
2191 case 'a':
2192 m = O_WRONLY;
2193 break;
2194 default:
2195 errno = EINVAL;
2196 return NULL;
2197 }
2198
2199 if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
2200 m = O_RDWR;
2201
2202 if ((fd = open(filename, m)) < 0)
2203 return NULL;
2204
2205 if (!(f = fdopen(fd, mode))) {
2206 close(fd);
2207 return NULL;
2208 }
2209
2210 return f;
2211 }
2212
2213 #ifdef HAVE_OPEN64
2214
2215 FILE *fopen64(const char *filename, const char *mode) {
2216
2217 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
2218
2219 if (strcmp(filename, "/dev/dsp") != 0 &&
2220 strcmp(filename, "/dev/adsp") != 0 &&
2221 strcmp(filename, "/dev/sndstat") != 0 &&
2222 strcmp(filename, "/dev/mixer") != 0) {
2223 LOAD_FOPEN64_FUNC();
2224 return _fopen64(filename, mode);
2225 }
2226
2227 return fopen(filename, mode);
2228 }
2229
2230 #endif
2231
2232 int fclose(FILE *f) {
2233 fd_info *i;
2234
2235 debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
2236
2237 if (!function_enter()) {
2238 LOAD_FCLOSE_FUNC();
2239 return _fclose(f);
2240 }
2241
2242 if (!(i = fd_info_find(fileno(f)))) {
2243 function_exit();
2244 LOAD_FCLOSE_FUNC();
2245 return _fclose(f);
2246 }
2247
2248 fd_info_remove_from_list(i);
2249
2250 /* Dirty trick to avoid that the fd is not freed twice, once by us
2251 * and once by the real fclose() */
2252 i->app_fd = -1;
2253
2254 fd_info_unref(i);
2255
2256 function_exit();
2257
2258 LOAD_FCLOSE_FUNC();
2259 return _fclose(f);
2260 }