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