-
Notifications
You must be signed in to change notification settings - Fork 11
/
osspd.c
2449 lines (2069 loc) · 57.7 KB
/
osspd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* osspd - OSS Proxy Daemon: emulate OSS device using CUSE
*
* This file is released under the GPLv2.
*/
#define FUSE_USE_VERSION 35
#define _GNU_SOURCE
#include <assert.h>
#include <cuse_lowlevel.h>
#include <fcntl.h>
#include <fuse_opt.h>
#include <libgen.h>
#include <limits.h>
#include <pthread.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/soundcard.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ossp.h"
#include "ossp-util.h"
#define DFL_MIXER_NAME "mixer"
#define DFL_DSP_NAME "dsp"
#define DFL_ADSP_NAME "adsp"
#define STRFMT "S[%u/%d]"
#define STRID(os) os->id, os->pid
#define dbg1_os(os, fmt, args...) dbg1(STRFMT" "fmt, STRID(os) , ##args)
#define dbg0_os(os, fmt, args...) dbg0(STRFMT" "fmt, STRID(os) , ##args)
#define warn_os(os, fmt, args...) warn(STRFMT" "fmt, STRID(os) , ##args)
#define err_os(os, fmt, args...) err(STRFMT" "fmt, STRID(os) , ##args)
#define warn_ose(os, err, fmt, args...) \
warn_e(err, STRFMT" "fmt, STRID(os) , ##args)
#define err_ose(os, err, fmt, args...) \
err_e(err, STRFMT" "fmt, STRID(os) , ##args)
enum {
SNDRV_OSS_VERSION = ((3<<16)|(8<<8)|(1<<4)|(0)), /* 3.8.1a */
DFL_MIXER_MAJOR = 14,
DFL_MIXER_MINOR = 0,
DFL_DSP_MAJOR = 14,
DFL_DSP_MINOR = 3,
DFL_ADSP_MAJOR = 14,
DFL_ADSP_MINOR = 12,
DFL_MAX_STREAMS = 128,
MIXER_PUT_DELAY = 600, /* 10 mins */
/* DSPS_MMAP_SIZE / 2 must be multiple of SHMLBA */
DSPS_MMAP_SIZE = 2 * (512 << 10), /* 512k for each dir */
};
struct ossp_uid_cnt {
struct list_head link;
uid_t uid;
unsigned nr_os;
};
struct ossp_mixer {
pid_t pgrp;
struct list_head link;
struct list_head delayed_put_link;
unsigned refcnt;
/* the following two fields are protected by mixer_mutex */
int vol[2][2];
int modify_counter;
time_t put_expires;
};
struct ossp_mixer_cmd {
struct ossp_mixer *mixer;
struct ossp_mixer_arg set;
int out_dir;
int rvol;
};
#define for_each_vol(i, j) \
for (i = 0, j = 0; i < 2; j += i << 1, j++, i = j >> 1, j &= 1)
struct ossp_stream {
unsigned id; /* stream ID */
struct list_head link;
struct list_head pgrp_link;
struct list_head notify_link;
unsigned refcnt;
pthread_mutex_t cmd_mutex;
pthread_mutex_t mmap_mutex;
struct fuse_pollhandle *ph;
/* stream owner info */
pid_t pid;
pid_t pgrp;
uid_t uid;
gid_t gid;
/* slave info */
pid_t slave_pid;
int cmd_fd;
int notify_tx;
int notify_rx;
/* the following dead flag is set asynchronously, keep it separate. */
int dead;
/* stream mixer state, protected by mixer_mutex */
int mixer_pending;
int vol[2][2];
int vol_set[2][2];
int mmap_fd;
size_t mmap_size;
void *mmap;
void *mmap_addr[2];
struct ossp_transfer *mmap_transfer;
struct ossp_uid_cnt *ucnt;
struct fuse_session *se; /* associated fuse session */
struct ossp_mixer *mixer;
};
struct ossp_dsp_stream {
struct ossp_stream os;
unsigned rw;
unsigned mmapped;
int nonblock;
};
#define os_to_dsps(_os) container_of(_os, struct ossp_dsp_stream, os)
static unsigned max_streams;
static unsigned umax_streams;
static unsigned hashtbl_size;
static char dsp_slave_path[PATH_MAX];
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mixer_mutex = PTHREAD_MUTEX_INITIALIZER;
static unsigned long *os_id_bitmap;
static unsigned nr_mixers;
static struct list_head *mixer_tbl; /* indexed by PGRP */
static struct list_head *os_tbl; /* indexed by ID */
static struct list_head *os_pgrp_tbl; /* indexed by PGRP */
static struct list_head *os_notify_tbl; /* indexed by notify fd */
static LIST_HEAD(uid_cnt_list);
static int notify_epfd; /* epoll used to monitor notify fds */
static pthread_t notify_poller_thread;
static pthread_t slave_reaper_thread;
static pthread_t mixer_delayed_put_thread;
static pthread_t cuse_mixer_thread;
static pthread_t cuse_adsp_thread;
static pthread_cond_t notify_poller_kill_wait = PTHREAD_COND_INITIALIZER;
static pthread_cond_t slave_reaper_wait = PTHREAD_COND_INITIALIZER;
static LIST_HEAD(slave_corpse_list);
static LIST_HEAD(mixer_delayed_put_head); /* delayed reference */
static pthread_cond_t mixer_delayed_put_cond = PTHREAD_COND_INITIALIZER;
static int init_wait_fd = -1;
static int exit_on_idle;
static struct fuse_session *mixer_se;
static struct fuse_session *dsp_se;
static struct fuse_session *adsp_se;
static void put_os(struct ossp_stream *os);
/***************************************************************************
* Accessors
*/
static struct list_head *mixer_tbl_head(pid_t pid)
{
return &mixer_tbl[pid % hashtbl_size];
}
static struct list_head *os_tbl_head(uint64_t id)
{
return &os_tbl[id % hashtbl_size];
}
static struct list_head *os_pgrp_tbl_head(pid_t pgrp)
{
return &os_pgrp_tbl[pgrp % hashtbl_size];
}
static struct list_head *os_notify_tbl_head(int notify_rx)
{
return &os_notify_tbl[notify_rx % hashtbl_size];
}
static struct ossp_mixer *find_mixer_locked(pid_t pgrp)
{
struct ossp_mixer *mixer;
list_for_each_entry(mixer, mixer_tbl_head(pgrp), link)
if (mixer->pgrp == pgrp)
return mixer;
return NULL;
}
static struct ossp_mixer *find_mixer(pid_t pgrp)
{
struct ossp_mixer *mixer;
pthread_mutex_lock(&mutex);
mixer = find_mixer_locked(pgrp);
pthread_mutex_unlock(&mutex);
return mixer;
}
static struct ossp_stream *find_os(unsigned id)
{
struct ossp_stream *os, *found = NULL;
pthread_mutex_lock(&mutex);
list_for_each_entry(os, os_tbl_head(id), link)
if (os->id == id) {
found = os;
break;
}
pthread_mutex_unlock(&mutex);
return found;
}
static struct ossp_stream *find_os_by_notify_rx(int notify_rx)
{
struct ossp_stream *os, *found = NULL;
pthread_mutex_lock(&mutex);
list_for_each_entry(os, os_notify_tbl_head(notify_rx), notify_link)
if (os->notify_rx == notify_rx) {
found = os;
break;
}
pthread_mutex_unlock(&mutex);
return found;
}
/***************************************************************************
* Command and ioctl helpers
*/
static ssize_t exec_cmd_intern(struct ossp_stream *os, enum ossp_opcode opcode,
const void *carg, size_t carg_size, const void *din, size_t din_size,
void *rarg, size_t rarg_size, void *dout, size_t *dout_sizep, int fd)
{
size_t dout_size = dout_sizep ? *dout_sizep : 0;
struct ossp_cmd cmd = { .magic = OSSP_CMD_MAGIC, .opcode = opcode,
.din_size = din_size,
.dout_size = dout_size };
struct iovec iov = { &cmd, sizeof(cmd) };
struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
struct ossp_reply reply = { };
char cmsg_buf[CMSG_SPACE(sizeof(fd))];
char reason[512];
int rc;
if (os->dead)
return -EIO;
dbg1_os(os, "%s carg=%zu din=%zu rarg=%zu dout=%zu",
ossp_cmd_str[opcode], carg_size, din_size, rarg_size,
dout_size);
if (fd >= 0) {
struct cmsghdr *cmsg;
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
*(int *)CMSG_DATA(cmsg) = fd;
msg.msg_controllen = cmsg->cmsg_len;
}
if (sendmsg(os->cmd_fd, &msg, 0) <= 0) {
rc = -errno;
snprintf(reason, sizeof(reason), "command sendmsg failed: %s",
strerror(-rc));
goto fail;
}
if ((rc = write_fill(os->cmd_fd, carg, carg_size)) < 0 ||
(rc = write_fill(os->cmd_fd, din, din_size)) < 0) {
snprintf(reason, sizeof(reason),
"can't tranfer command argument and/or data: %s",
strerror(-rc));
goto fail;
}
if ((rc = read_fill(os->cmd_fd, &reply, sizeof(reply))) < 0) {
snprintf(reason, sizeof(reason), "can't read reply: %s",
strerror(-rc));
goto fail;
}
if (reply.magic != OSSP_REPLY_MAGIC) {
snprintf(reason, sizeof(reason),
"reply magic mismatch %x != %x",
reply.magic, OSSP_REPLY_MAGIC);
rc = -EINVAL;
goto fail;
}
if (reply.result < 0)
goto out_unlock;
if (reply.dout_size > dout_size) {
snprintf(reason, sizeof(reason),
"data out size overflow %zu > %zu",
reply.dout_size, dout_size);
rc = -EINVAL;
goto fail;
}
dout_size = reply.dout_size;
if (dout_sizep)
*dout_sizep = dout_size;
if ((rc = read_fill(os->cmd_fd, rarg, rarg_size)) < 0 ||
(rc = read_fill(os->cmd_fd, dout, dout_size)) < 0) {
snprintf(reason, sizeof(reason), "can't read data out: %s",
strerror(-rc));
goto fail;
}
out_unlock:
dbg1_os(os, " completed, result=%d dout=%zu",
reply.result, dout_size);
return reply.result;
fail:
warn_os(os, "communication with slave failed (%s)", reason);
os->dead = 1;
return rc;
}
static ssize_t exec_cmd(struct ossp_stream *os, enum ossp_opcode opcode,
const void *carg, size_t carg_size, const void *din, size_t din_size,
void *rarg, size_t rarg_size, void *dout, size_t *dout_sizep, int fd)
{
int is_mixer;
int i, j;
ssize_t ret, mret;
/* mixer command is handled exlicitly below */
is_mixer = opcode == OSSP_MIXER;
if (is_mixer) {
ret = -pthread_mutex_trylock(&os->cmd_mutex);
if (ret)
return ret;
} else {
pthread_mutex_lock(&os->cmd_mutex);
ret = exec_cmd_intern(os, opcode, carg, carg_size,
din, din_size, rarg, rarg_size,
dout, dout_sizep, fd);
}
/* lazy mixer handling */
pthread_mutex_lock(&mixer_mutex);
if (os->mixer_pending) {
struct ossp_mixer_arg marg;
repeat_mixer:
/* we have mixer command pending */
memcpy(marg.vol, os->vol_set, sizeof(os->vol_set));
memset(os->vol_set, -1, sizeof(os->vol_set));
pthread_mutex_unlock(&mixer_mutex);
mret = exec_cmd_intern(os, OSSP_MIXER, &marg, sizeof(marg),
NULL, 0, &marg, sizeof(marg), NULL, NULL,
-1);
pthread_mutex_lock(&mixer_mutex);
/* was there mixer set request while executing mixer command? */
for_each_vol(i, j)
if (os->vol_set[i][j] >= 0)
goto repeat_mixer;
/* update internal mixer state */
if (mret == 0) {
for_each_vol(i, j) {
if (marg.vol[i][j] >= 0) {
if (os->vol[i][j] != marg.vol[i][j])
os->mixer->modify_counter++;
os->vol[i][j] = marg.vol[i][j];
}
}
}
os->mixer_pending = 0;
}
pthread_mutex_unlock(&os->cmd_mutex);
/*
* mixer mutex must be released after cmd_mutex so that
* exec_mixer_cmd() can guarantee that mixer_pending flags
* will be handled immediately or when the currently
* in-progress command completes.
*/
pthread_mutex_unlock(&mixer_mutex);
return is_mixer ? mret : ret;
}
static ssize_t exec_simple_cmd(struct ossp_stream *os,
enum ossp_opcode opcode, void *carg, void *rarg)
{
return exec_cmd(os, opcode,
carg, ossp_arg_sizes[opcode].carg_size, NULL, 0,
rarg, ossp_arg_sizes[opcode].rarg_size, NULL, NULL, -1);
}
static int ioctl_prep_uarg(fuse_req_t req, void *in, size_t in_sz, void *out,
size_t out_sz, void *uarg, const void *in_buf,
size_t in_bufsz, size_t out_bufsz)
{
struct iovec in_iov = { }, out_iov = { };
int retry = 0;
if (in) {
if (!in_bufsz) {
in_iov.iov_base = uarg;
in_iov.iov_len = in_sz;
retry = 1;
} else {
assert(in_bufsz == in_sz);
memcpy(in, in_buf, in_sz);
}
}
if (out) {
if (!out_bufsz) {
out_iov.iov_base = uarg;
out_iov.iov_len = out_sz;
retry = 1;
} else
assert(out_bufsz == out_sz);
}
if (retry)
fuse_reply_ioctl_retry(req, &in_iov, 1, &out_iov, 1);
return retry;
}
#define PREP_UARG(inp, outp) do { \
if (ioctl_prep_uarg(req, (inp), sizeof(*(inp)), \
(outp), sizeof(*(outp)), uarg, \
in_buf, in_bufsz, out_bufsz)) \
return; \
} while (0)
/***************************************************************************
* Mixer implementation
*/
static void put_mixer_real(struct ossp_mixer *mixer)
{
if (!--mixer->refcnt) {
dbg0("DESTROY mixer(%d)", mixer->pgrp);
list_del_init(&mixer->link);
list_del_init(&mixer->delayed_put_link);
free(mixer);
nr_mixers--;
/*
* If exit_on_idle, mixer for pgrp0 is touched during
* init and each stream has mixer attached. As mixers
* are destroyed after they have been idle for
* MIXER_PUT_DELAY seconds, we can use it for idle
* detection. Note that this might race with
* concurrent open. The race is inherent.
*/
if (exit_on_idle && !nr_mixers) {
info("idle, exiting");
exit(0);
}
}
}
static struct ossp_mixer *get_mixer(pid_t pgrp)
{
struct ossp_mixer *mixer;
pthread_mutex_lock(&mutex);
/* is there a matching one? */
mixer = find_mixer_locked(pgrp);
if (mixer) {
if (list_empty(&mixer->delayed_put_link))
mixer->refcnt++;
else
list_del_init(&mixer->delayed_put_link);
goto out_unlock;
}
/* reap delayed put list if there are too many mixers */
while (nr_mixers > 2 * max_streams &&
!list_empty(&mixer_delayed_put_head)) {
struct ossp_mixer *mixer =
list_first_entry(&mixer_delayed_put_head,
struct ossp_mixer, delayed_put_link);
assert(mixer->refcnt == 1);
put_mixer_real(mixer);
}
/* create a new one */
mixer = calloc(1, sizeof(*mixer));
if (!mixer) {
warn("failed to allocate mixer for %d", pgrp);
mixer = NULL;
goto out_unlock;
}
mixer->pgrp = pgrp;
INIT_LIST_HEAD(&mixer->link);
INIT_LIST_HEAD(&mixer->delayed_put_link);
mixer->refcnt = 1;
memset(mixer->vol, -1, sizeof(mixer->vol));
list_add(&mixer->link, mixer_tbl_head(pgrp));
nr_mixers++;
dbg0("CREATE mixer(%d)", pgrp);
out_unlock:
pthread_mutex_unlock(&mutex);
return mixer;
}
static void put_mixer(struct ossp_mixer *mixer)
{
pthread_mutex_lock(&mutex);
if (mixer) {
if (mixer->refcnt == 1) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
mixer->put_expires = ts.tv_sec + MIXER_PUT_DELAY;
list_add_tail(&mixer->delayed_put_link,
&mixer_delayed_put_head);
pthread_cond_signal(&mixer_delayed_put_cond);
} else
put_mixer_real(mixer);
}
pthread_mutex_unlock(&mutex);
}
static void *mixer_delayed_put_worker(void *arg)
{
struct ossp_mixer *mixer;
struct timespec ts;
time_t now;
pthread_mutex_lock(&mutex);
again:
clock_gettime(CLOCK_REALTIME, &ts);
now = ts.tv_sec;
mixer = NULL;
while (!list_empty(&mixer_delayed_put_head)) {
mixer = list_first_entry(&mixer_delayed_put_head,
struct ossp_mixer, delayed_put_link);
if (now <= mixer->put_expires)
break;
assert(mixer->refcnt == 1);
put_mixer_real(mixer);
mixer = NULL;
}
if (mixer) {
ts.tv_sec = mixer->put_expires + 1;
pthread_cond_timedwait(&mixer_delayed_put_cond, &mutex, &ts);
} else
pthread_cond_wait(&mixer_delayed_put_cond, &mutex);
goto again;
}
static void init_mixer_cmd(struct ossp_mixer_cmd *mxcmd,
struct ossp_mixer *mixer)
{
memset(mxcmd, 0, sizeof(*mxcmd));
memset(&mxcmd->set.vol, -1, sizeof(mxcmd->set.vol));
mxcmd->mixer = mixer;
mxcmd->out_dir = -1;
}
static int exec_mixer_cmd(struct ossp_mixer_cmd *mxcmd, struct ossp_stream *os)
{
int i, j, rc;
/*
* Set pending flags before trying to execute mixer command.
* Combined with lock release order in exec_cmd(), this
* guarantees that the mixer command will be executed
* immediately or when the current command completes.
*/
pthread_mutex_lock(&mixer_mutex);
os->mixer_pending = 1;
for_each_vol(i, j)
if (mxcmd->set.vol[i][j] >= 0)
os->vol_set[i][j] = mxcmd->set.vol[i][j];
pthread_mutex_unlock(&mixer_mutex);
rc = exec_simple_cmd(os, OSSP_MIXER, NULL, NULL);
if (rc >= 0) {
dbg0_os(os, "volume set=%d/%d:%d/%d get=%d/%d:%d/%d",
mxcmd->set.vol[PLAY][LEFT], mxcmd->set.vol[PLAY][RIGHT],
mxcmd->set.vol[REC][LEFT], mxcmd->set.vol[REC][RIGHT],
os->vol[PLAY][LEFT], os->vol[PLAY][RIGHT],
os->vol[REC][LEFT], os->vol[REC][RIGHT]);
} else if (rc != -EBUSY)
warn_ose(os, rc, "mixer command failed");
return rc;
}
static void finish_mixer_cmd(struct ossp_mixer_cmd *mxcmd)
{
struct ossp_mixer *mixer = mxcmd->mixer;
struct ossp_stream *os;
int dir = mxcmd->out_dir;
int vol[2][2] = { };
int cnt[2][2] = { };
int i, j;
pthread_mutex_lock(&mixer_mutex);
/* get volume of all streams attached to this mixer */
pthread_mutex_lock(&mutex);
list_for_each_entry(os, os_pgrp_tbl_head(mixer->pgrp), pgrp_link) {
if (os->pgrp != mixer->pgrp)
continue;
for_each_vol(i, j) {
if (os->vol[i][j] < 0)
continue;
vol[i][j] += os->vol[i][j];
cnt[i][j]++;
}
}
pthread_mutex_unlock(&mutex);
/* calculate the summary volume values */
for_each_vol(i, j) {
if (mxcmd->set.vol[i][j] >= 0)
vol[i][j] = mxcmd->set.vol[i][j];
else if (cnt[i][j])
vol[i][j] = vol[i][j] / cnt[i][j];
else if (mixer->vol[i][j] >= 0)
vol[i][j] = mixer->vol[i][j];
else
vol[i][j] = 100;
vol[i][j] = min(max(0, vol[i][j]), 100);
}
if (dir >= 0)
mxcmd->rvol = vol[dir][LEFT] | (vol[dir][RIGHT] << 8);
pthread_mutex_unlock(&mixer_mutex);
}
static void mixer_simple_ioctl(fuse_req_t req, struct ossp_mixer *mixer,
unsigned cmd, void *uarg, const void *in_buf,
size_t in_bufsz, size_t out_bufsz,
int *not_minep)
{
const char *id = "OSS Proxy", *name = "Mixer";
int i;
switch (cmd) {
case SOUND_MIXER_INFO: {
struct mixer_info info = { };
PREP_UARG(NULL, &info);
strncpy(info.id, id, sizeof(info.id) - 1);
strncpy(info.name, name, sizeof(info.name) - 1);
info.modify_counter = mixer->modify_counter;
fuse_reply_ioctl(req, 0, &info, sizeof(info));
break;
}
case SOUND_OLD_MIXER_INFO: {
struct _old_mixer_info info = { };
PREP_UARG(NULL, &info);
strncpy(info.id, id, sizeof(info.id) - 1);
strncpy(info.name, name, sizeof(info.name) - 1);
fuse_reply_ioctl(req, 0, &info, sizeof(info));
break;
}
case OSS_GETVERSION:
i = SNDRV_OSS_VERSION;
goto puti;
case SOUND_MIXER_READ_DEVMASK:
case SOUND_MIXER_READ_STEREODEVS:
i = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
goto puti;
case SOUND_MIXER_READ_CAPS:
i = SOUND_CAP_EXCL_INPUT;
goto puti;
case SOUND_MIXER_READ_RECMASK:
case SOUND_MIXER_READ_RECSRC:
i = SOUND_MASK_IGAIN;
goto puti;
puti:
PREP_UARG(NULL, &i);
fuse_reply_ioctl(req, 0, &i, sizeof(i));
break;
case SOUND_MIXER_WRITE_RECSRC:
fuse_reply_ioctl(req, 0, NULL, 0);
break;
default:
*not_minep = 1;
}
}
static void mixer_do_ioctl(fuse_req_t req, struct ossp_mixer *mixer,
unsigned cmd, void *uarg, const void *in_buf,
size_t in_bufsz, size_t out_bufsz)
{
struct ossp_mixer_cmd mxcmd;
struct ossp_stream *os, **osa;
int not_mine = 0;
int slot = cmd & 0xff, dir;
int nr_os;
int i, rc;
mixer_simple_ioctl(req, mixer, cmd, uarg, in_buf, in_bufsz, out_bufsz,
¬_mine);
if (!not_mine)
return;
rc = -ENXIO;
if (!(cmd & (SIOC_IN | SIOC_OUT)))
goto err;
/*
* Okay, it's not one of the easy ones. Build mxcmd for
* actual volume control.
*/
if (cmd & SIOC_IN)
PREP_UARG(&i, &i);
else
PREP_UARG(NULL, &i);
switch (slot) {
case SOUND_MIXER_PCM:
dir = PLAY;
break;
case SOUND_MIXER_IGAIN:
dir = REC;
break;
default:
i = 0;
fuse_reply_ioctl(req, 0, &i, sizeof(i));
return;
}
init_mixer_cmd(&mxcmd, mixer);
if (cmd & SIOC_IN) {
unsigned l, r;
rc = -EINVAL;
l = i & 0xff;
r = (i >> 8) & 0xff;
if (l > 100 || r > 100)
goto err;
mixer->vol[dir][LEFT] = mxcmd.set.vol[dir][LEFT] = l;
mixer->vol[dir][RIGHT] = mxcmd.set.vol[dir][RIGHT] = r;
}
mxcmd.out_dir = dir;
/*
* Apply volume conrol
*/
/* acquire target streams */
pthread_mutex_lock(&mutex);
osa = calloc(max_streams, sizeof(osa[0]));
if (!osa) {
pthread_mutex_unlock(&mutex);
rc = -ENOMEM;
goto err;
}
nr_os = 0;
list_for_each_entry(os, os_pgrp_tbl_head(mixer->pgrp), pgrp_link) {
if (os->pgrp == mixer->pgrp) {
osa[nr_os++] = os;
os->refcnt++;
}
}
pthread_mutex_unlock(&mutex);
/* execute mxcmd for each stream and put it */
for (i = 0; i < nr_os; i++) {
exec_mixer_cmd(&mxcmd, osa[i]);
put_os(osa[i]);
}
finish_mixer_cmd(&mxcmd);
free(osa);
if (out_bufsz)
fuse_reply_ioctl(req, 0, &mxcmd.rvol, sizeof(mxcmd.rvol));
else
fuse_reply_ioctl(req, 0, NULL, 0);
return;
err:
fuse_reply_err(req, -rc);
}
static void mixer_open(fuse_req_t req, struct fuse_file_info *fi)
{
pid_t pid = fuse_req_ctx(req)->pid, pgrp;
struct ossp_mixer *mixer;
int rc;
rc = get_proc_self_info(pid, &pgrp, NULL, 0);
if (rc) {
err_e(rc, "get_proc_self_info(%d) failed", pid);
fuse_reply_err(req, -rc);
return;
}
mixer = get_mixer(pgrp);
fi->fh = pgrp;
if (mixer)
fuse_reply_open(req, fi);
else
fuse_reply_err(req, ENOMEM);
}
static void mixer_ioctl(fuse_req_t req, int signed_cmd, void *uarg,
struct fuse_file_info *fi, unsigned int flags,
const void *in_buf, size_t in_bufsz, size_t out_bufsz)
{
struct ossp_mixer *mixer;
mixer = find_mixer(fi->fh);
if (!mixer) {
fuse_reply_err(req, EBADF);
return;
}
mixer_do_ioctl(req, mixer, signed_cmd, uarg, in_buf, in_bufsz,
out_bufsz);
}
static void mixer_release(fuse_req_t req, struct fuse_file_info *fi)
{
struct ossp_mixer *mixer;
mixer = find_mixer(fi->fh);
if (mixer) {
put_mixer(mixer);
fuse_reply_err(req, 0);
} else
fuse_reply_err(req, EBADF);
}
/***************************************************************************
* Stream implementation
*/
static int os_create_shared_memory(struct ossp_stream *os, size_t mmap_size)
{
int rc = 0;
os->mmap_fd = -1;
os->mmap_size = 0;
#ifdef OSSP_MMAP
if (mmap_size) {
char shmname[32];
int fd;
void *p;
sprintf(shmname, "/ossp.%i", getpid());
fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
0600);
if (fd == -1) {
rc = -errno;
warn_ose(os, rc, "failed to open shared memory");
return rc;
}
rc = shm_unlink(shmname);
if (rc == -1) {
rc = -errno;
close(fd);
warn_ose(os, rc, "failed to unlink shared memory");
return rc;
}
rc = ftruncate(fd, mmap_size + 2*sizeof(struct ossp_transfer));
if (rc == -1) {
rc = -errno;
close(fd);
warn_ose(os, rc, "failed to set shared memory size");
return rc;
}
rc = fcntl(fd, F_SETFD, 0); /* reset cloexec */
if (rc == -1) {
rc = -errno;
close(fd);
warn_ose(os, rc, "failed to reset close-on-exec for shared memory");
return rc;
}
p = mmap(NULL, mmap_size + 2 * sizeof(struct ossp_transfer),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
rc = -errno;
close(fd);
warn_ose(os, rc, "failed to mmap shared memory");
return rc;
}
os->mmap_addr[PLAY] = p;
os->mmap_addr[REC] = p + mmap_size / 2;
os->mmap_transfer = p + mmap_size;
if (sem_init(&os->mmap_transfer[PLAY].sem, 1, 0) == -1 ||
sem_init(&os->mmap_transfer[REC].sem, 1, 0) == -1) {
rc = -errno;
close(fd);
munmap(p, mmap_size + 2 * sizeof(struct ossp_transfer));
warn_ose(os, rc, "failed to init shared semaphore");
return rc;
}
os->mmap = p;
os->mmap_size = mmap_size;
os->mmap_fd = fd;
}
#endif
return rc;
}
static int alloc_os(size_t stream_size, size_t mmap_size, pid_t pid, uid_t pgrp,
uid_t uid, gid_t gid, int cmd_sock,
const int *notify, struct fuse_session *se,
struct ossp_stream **osp)
{
struct ossp_uid_cnt *tmp_ucnt, *ucnt = NULL;
struct ossp_stream *os;
int rc;
assert(stream_size >= sizeof(struct ossp_stream));
os = calloc(1, stream_size);
if (!os)
return -ENOMEM;
INIT_LIST_HEAD(&os->link);
INIT_LIST_HEAD(&os->pgrp_link);
INIT_LIST_HEAD(&os->notify_link);
os->refcnt = 1;
rc = -pthread_mutex_init(&os->cmd_mutex, NULL);
if (rc)
goto err_free;
rc = -pthread_mutex_init(&os->mmap_mutex, NULL);
if (rc)
goto err_destroy_cmd_mutex;
pthread_mutex_lock(&mutex);
list_for_each_entry(tmp_ucnt, &uid_cnt_list, link)
if (tmp_ucnt->uid == uid) {
ucnt = tmp_ucnt;
break;
}