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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
/*
* ALSA server
* Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <getopt.h>
#include <netinet/in.h>
#include <netdb.h>
#include <limits.h>
#include "aserver.h"
#include "list.h"
char *command;
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
#define ERROR(...) do {\
fprintf(stderr, "%s %s:%i:(%s) ", command, __FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, __VA_ARGS__); \
putc('\n', stderr); \
} while (0)
#else
#define ERROR(args...) do {\
fprintf(stderr, "%s %s:%i:(%s) ", command, __FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, ##args); \
putc('\n', stderr); \
} while (0)
#endif
#define SYSERROR(string) ERROR(string ": %s", strerror(errno))
int make_local_socket(const char *filename)
{
size_t l = strlen(filename);
size_t size = offsetof(struct sockaddr_un, sun_path) + l;
struct sockaddr_un *addr = alloca(size);
int sock;
sock = socket(PF_LOCAL, SOCK_STREAM, 0);
if (sock < 0) {
int result = -errno;
SYSERROR("socket failed");
return result;
}
unlink(filename);
addr->sun_family = AF_LOCAL;
memcpy(addr->sun_path, filename, l);
if (bind(sock, (struct sockaddr *) addr, size) < 0) {
int result = -errno;
SYSERROR("bind failed");
return result;
}
return sock;
}
int make_inet_socket(int port)
{
struct sockaddr_in addr;
int sock;
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
int result = -errno;
SYSERROR("socket failed");
return result;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
int result = -errno;
SYSERROR("bind failed");
return result;
}
return sock;
}
int send_fd(int socket, void *data, size_t len, int fd)
{
int ret;
size_t cmsg_len = CMSG_LEN(sizeof(int));
struct cmsghdr *cmsg = alloca(cmsg_len);
int *fds = (int *) CMSG_DATA(cmsg);
struct msghdr msghdr;
struct iovec vec;
vec.iov_base = (void *)&data;
vec.iov_len = len;
cmsg->cmsg_len = cmsg_len;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*fds = fd;
msghdr.msg_name = NULL;
msghdr.msg_namelen = 0;
msghdr.msg_iov = &vec;
msghdr.msg_iovlen = 1;
msghdr.msg_control = cmsg;
msghdr.msg_controllen = cmsg_len;
msghdr.msg_flags = 0;
ret = sendmsg(socket, &msghdr, 0 );
if (ret < 0) {
SYSERROR("sendmsg failed");
return -errno;
}
return ret;
}
struct pollfd *pollfds;
unsigned int pollfds_count = 0;
typedef struct waiter waiter_t;
typedef int (*waiter_handler_t)(waiter_t *waiter, unsigned short events);
struct waiter {
int fd;
void *private_data;
waiter_handler_t handler;
};
waiter_t *waiters;
void add_waiter(int fd, unsigned short events, waiter_handler_t handler,
void *data)
{
waiter_t *w = &waiters[fd];
struct pollfd *pfd = &pollfds[pollfds_count];
assert(!w->handler);
pfd->fd = fd;
pfd->events = events;
pfd->revents = 0;
w->fd = fd;
w->private_data = data;
w->handler = handler;
pollfds_count++;
}
void del_waiter(int fd)
{
waiter_t *w = &waiters[fd];
unsigned int k;
assert(w->handler);
w->handler = 0;
for (k = 0; k < pollfds_count; ++k) {
if (pollfds[k].fd == fd)
break;
}
assert(k < pollfds_count);
pollfds_count--;
memmove(&pollfds[k], &pollfds[k + 1], pollfds_count - k);
}
typedef struct client client_t;
typedef struct {
int (*open)(client_t *client, int *cookie);
int (*cmd)(client_t *client);
int (*close)(client_t *client);
} transport_ops_t;
struct client {
struct list_head list;
int poll_fd;
int ctrl_fd;
int local;
int transport_type;
int dev_type;
char name[256];
int stream;
int mode;
transport_ops_t *ops;
union {
struct {
snd_pcm_t *handle;
int fd;
} pcm;
struct {
snd_ctl_t *handle;
int fd;
} control;
#if 0
struct {
snd_rawmidi_t *handle;
} rawmidi;
struct {
snd_timer_open_t *handle;
} timer;
struct {
snd_hwdep_t *handle;
} hwdep;
struct {
snd_seq_t *handle;
} seq;
#endif
} device;
int polling;
int open;
int cookie;
union {
struct {
int ctrl_id;
void *ctrl;
} shm;
} transport;
};
LIST_HEAD(clients);
typedef struct {
struct list_head list;
int fd;
uint32_t cookie;
} inet_pending_t;
LIST_HEAD(inet_pendings);
int pcm_handler(waiter_t *waiter, unsigned short events)
{
client_t *client = waiter->private_data;
char buf[1];
ssize_t n;
if (events & POLLIN) {
n = write(client->poll_fd, buf, 1);
if (n != 1) {
SYSERROR("write failed");
return -errno;
}
} else if (events & POLLOUT) {
n = read(client->poll_fd, buf, 1);
if (n != 1) {
SYSERROR("read failed");
return -errno;
}
}
del_waiter(waiter->fd);
client->polling = 0;
return 0;
}
int pcm_shm_open(client_t *client, int *cookie)
{
int shmid;
snd_pcm_t *pcm;
int err;
int result;
err = snd_pcm_open(&pcm, client->name, snd_int_to_enum(client->stream), SND_PCM_NONBLOCK);
if (err < 0)
return err;
client->device.pcm.handle = pcm;
client->device.pcm.fd = snd_pcm_poll_descriptor(pcm);
shmid = shmget(IPC_PRIVATE, PCM_SHM_SIZE, 0666);
if (shmid < 0) {
result = -errno;
SYSERROR("shmget failed");
goto _err;
}
client->transport.shm.ctrl_id = shmid;
client->transport.shm.ctrl = shmat(shmid, 0, 0);
if (client->transport.shm.ctrl == (void*) -1) {
result = -errno;
shmctl(shmid, IPC_RMID, 0);
SYSERROR("shmat failed");
goto _err;
}
*cookie = shmid;
return 0;
_err:
snd_pcm_close(pcm);
return result;
}
int pcm_shm_close(client_t *client)
{
int err;
snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
if (client->polling) {
del_waiter(client->device.pcm.fd);
client->polling = 0;
}
err = snd_pcm_close(client->device.pcm.handle);
ctrl->result = err;
if (err < 0)
ERROR("snd_pcm_close");
if (client->transport.shm.ctrl) {
err = shmdt((void *)client->transport.shm.ctrl);
if (err < 0)
SYSERROR("shmdt failed");
err = shmctl(client->transport.shm.ctrl_id, IPC_RMID, 0);
if (err < 0)
SYSERROR("shmctl IPC_RMID failed");
client->transport.shm.ctrl = 0;
}
client->open = 0;
return 0;
}
int shm_ack(client_t *client)
{
struct pollfd pfd;
int err;
char buf[1];
pfd.fd = client->ctrl_fd;
pfd.events = POLLHUP;
if (poll(&pfd, 1, 0) == 1)
return -EBADFD;
err = write(client->ctrl_fd, buf, 1);
if (err != 1)
return -EBADFD;
return 0;
}
int shm_ack_fd(client_t *client, int fd)
{
struct pollfd pfd;
int err;
char buf[1];
pfd.fd = client->ctrl_fd;
pfd.events = POLLHUP;
if (poll(&pfd, 1, 0) == 1)
return -EBADFD;
err = send_fd(client->ctrl_fd, buf, 1, fd);
if (err != 1)
return -EBADFD;
return 0;
}
int pcm_shm_cmd(client_t *client)
{
volatile snd_pcm_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
char buf[1];
int err;
int cmd;
snd_pcm_t *pcm;
err = read(client->ctrl_fd, buf, 1);
if (err != 1)
return -EBADFD;
cmd = ctrl->cmd;
ctrl->cmd = 0;
pcm = client->device.pcm.handle;
switch (cmd) {
case SND_PCM_IOCTL_ASYNC:
ctrl->result = snd_pcm_async(pcm, ctrl->u.async.sig, ctrl->u.async.pid);
break;
case SNDRV_PCM_IOCTL_INFO:
ctrl->result = snd_pcm_info(pcm, (snd_pcm_info_t *) &ctrl->u.info);
break;
case SNDRV_PCM_IOCTL_HW_REFINE:
ctrl->result = snd_pcm_hw_refine(pcm, (snd_pcm_hw_params_t *) &ctrl->u.hw_refine);
break;
case SNDRV_PCM_IOCTL_HW_PARAMS:
ctrl->result = snd_pcm_hw_params(pcm, (snd_pcm_hw_params_t *) &ctrl->u.hw_params);
break;
case SNDRV_PCM_IOCTL_HW_FREE:
ctrl->result = snd_pcm_hw_free(pcm);
break;
case SNDRV_PCM_IOCTL_SW_PARAMS:
ctrl->result = snd_pcm_sw_params(pcm, (snd_pcm_sw_params_t *) &ctrl->u.sw_params);
break;
case SNDRV_PCM_IOCTL_STATUS:
ctrl->result = snd_pcm_status(pcm, (snd_pcm_status_t *) &ctrl->u.status);
break;
case SND_PCM_IOCTL_STATE:
ctrl->result = snd_enum_to_int(snd_pcm_state(pcm));
break;
case SNDRV_PCM_IOCTL_DELAY:
ctrl->result = snd_pcm_delay(pcm, (snd_pcm_sframes_t *) &ctrl->u.delay.frames);
break;
case SND_PCM_IOCTL_AVAIL_UPDATE:
ctrl->result = snd_pcm_avail_update(pcm);
ctrl->hw_ptr = *pcm->hw_ptr;
break;
case SNDRV_PCM_IOCTL_PREPARE:
ctrl->result = snd_pcm_prepare(pcm);
ctrl->appl_ptr = *pcm->appl_ptr;
ctrl->hw_ptr = *pcm->hw_ptr;
break;
case SNDRV_PCM_IOCTL_RESET:
ctrl->result = snd_pcm_reset(pcm);
ctrl->appl_ptr = *pcm->appl_ptr;
ctrl->hw_ptr = *pcm->hw_ptr;
break;
case SNDRV_PCM_IOCTL_START:
ctrl->result = snd_pcm_start(pcm);
ctrl->appl_ptr = *pcm->appl_ptr;
ctrl->hw_ptr = *pcm->hw_ptr;
break;
case SNDRV_PCM_IOCTL_DRAIN:
ctrl->result = snd_pcm_drain(pcm);
break;
case SNDRV_PCM_IOCTL_DROP:
ctrl->result = snd_pcm_drop(pcm);
break;
case SNDRV_PCM_IOCTL_PAUSE:
ctrl->result = snd_pcm_pause(pcm, ctrl->u.pause.enable);
break;
case SNDRV_PCM_IOCTL_CHANNEL_INFO:
ctrl->result = snd_pcm_channel_info(pcm, (snd_pcm_channel_info_t *) &ctrl->u.channel_info);
if (ctrl->result >= 0 &&
ctrl->u.channel_info.type == SND_PCM_AREA_MMAP)
return shm_ack_fd(client, ctrl->u.channel_info.u.mmap.fd);
break;
case SNDRV_PCM_IOCTL_REWIND:
ctrl->result = snd_pcm_rewind(pcm, ctrl->u.rewind.frames);
ctrl->appl_ptr = *pcm->appl_ptr;
break;
case SNDRV_PCM_IOCTL_LINK:
{
/* FIXME */
ctrl->result = -ENOSYS;
break;
}
case SNDRV_PCM_IOCTL_UNLINK:
ctrl->result = snd_pcm_unlink(pcm);
break;
case SND_PCM_IOCTL_MMAP:
{
ctrl->result = snd_pcm_mmap(pcm);
}
case SND_PCM_IOCTL_MUNMAP:
{
ctrl->result = snd_pcm_munmap(pcm);
break;
}
case SND_PCM_IOCTL_MMAP_FORWARD:
ctrl->result = snd_pcm_mmap_forward(pcm, ctrl->u.mmap_forward.frames);
ctrl->appl_ptr = *pcm->appl_ptr;
break;
case SND_PCM_IOCTL_POLL_DESCRIPTOR:
ctrl->result = 0;
return shm_ack_fd(client, snd_pcm_poll_descriptor(pcm));
case SND_PCM_IOCTL_CLOSE:
client->ops->close(client);
break;
default:
ERROR("Bogus cmd: %x", ctrl->cmd);
ctrl->result = -ENOSYS;
}
return shm_ack(client);
}
transport_ops_t pcm_shm_ops = {
open: pcm_shm_open,
cmd: pcm_shm_cmd,
close: pcm_shm_close,
};
int ctl_handler(waiter_t *waiter, unsigned short events)
{
client_t *client = waiter->private_data;
char buf[1];
ssize_t n;
if (events & POLLIN) {
n = write(client->poll_fd, buf, 1);
if (n != 1) {
SYSERROR("write failed");
return -errno;
}
}
del_waiter(waiter->fd);
client->polling = 0;
return 0;
}
int ctl_shm_open(client_t *client, int *cookie)
{
int shmid;
snd_ctl_t *ctl;
int err;
int result;
err = snd_ctl_open(&ctl, client->name);
if (err < 0)
return err;
client->device.control.handle = ctl;
client->device.control.fd = snd_ctl_poll_descriptor(ctl);
shmid = shmget(IPC_PRIVATE, CTL_SHM_SIZE, 0666);
if (shmid < 0) {
result = -errno;
SYSERROR("shmget failed");
goto _err;
}
client->transport.shm.ctrl_id = shmid;
client->transport.shm.ctrl = shmat(shmid, 0, 0);
if (!client->transport.shm.ctrl) {
result = -errno;
shmctl(shmid, IPC_RMID, 0);
SYSERROR("shmat failed");
goto _err;
}
*cookie = shmid;
add_waiter(client->device.control.fd, POLLIN, ctl_handler, client);
client->polling = 1;
return 0;
_err:
snd_ctl_close(ctl);
return result;
}
int ctl_shm_close(client_t *client)
{
int err;
snd_ctl_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
if (client->polling) {
del_waiter(client->device.control.fd);
client->polling = 0;
}
err = snd_ctl_close(client->device.control.handle);
ctrl->result = err;
if (err < 0)
ERROR("snd_ctl_close");
if (client->transport.shm.ctrl) {
err = shmdt((void *)client->transport.shm.ctrl);
if (err < 0)
SYSERROR("shmdt failed");
err = shmctl(client->transport.shm.ctrl_id, IPC_RMID, 0);
if (err < 0)
SYSERROR("shmctl failed");
client->transport.shm.ctrl = 0;
}
client->open = 0;
return 0;
}
extern int snd_ctl_read1(snd_ctl_t *ctl, snd_ctl_event_t *event);
int ctl_shm_cmd(client_t *client)
{
snd_ctl_shm_ctrl_t *ctrl = client->transport.shm.ctrl;
char buf[1];
int err;
int cmd;
snd_ctl_t *ctl;
err = read(client->ctrl_fd, buf, 1);
if (err != 1)
return -EBADFD;
cmd = ctrl->cmd;
ctrl->cmd = 0;
ctl = client->device.control.handle;
switch (cmd) {
case SNDRV_CTL_IOCTL_INFO:
ctrl->result = snd_ctl_info(ctl, &ctrl->u.hw_info);
break;
case SNDRV_CTL_IOCTL_CONTROL_LIST:
{
size_t maxsize = CTL_SHM_DATA_MAXLEN;
if (ctrl->u.clist.space * sizeof(*ctrl->u.clist.pids) > maxsize) {
ctrl->result = -EFAULT;
break;
}
ctrl->u.clist.pids = (snd_control_id_t*) ctrl->data;
ctrl->result = snd_ctl_clist(ctl, &ctrl->u.clist);
break;
}
case SNDRV_CTL_IOCTL_CONTROL_INFO:
ctrl->result = snd_ctl_cinfo(ctl, &ctrl->u.cinfo);
break;
case SNDRV_CTL_IOCTL_CONTROL_READ:
ctrl->result = snd_ctl_cread(ctl, &ctrl->u.cread);
break;
case SNDRV_CTL_IOCTL_CONTROL_WRITE:
ctrl->result = snd_ctl_cwrite(ctl, &ctrl->u.cwrite);
break;
case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
ctrl->result = snd_ctl_hwdep_next_device(ctl, &ctrl->u.device);
break;
case SNDRV_CTL_IOCTL_HWDEP_INFO:
ctrl->result = snd_ctl_hwdep_info(ctl, &ctrl->u.hwdep_info);
break;
case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
ctrl->result = snd_ctl_pcm_next_device(ctl, &ctrl->u.device);
break;
case SNDRV_CTL_IOCTL_PCM_INFO:
ctrl->result = snd_ctl_pcm_info(ctl, &ctrl->u.pcm_info);
break;
case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
ctrl->result = snd_ctl_pcm_prefer_subdevice(ctl, ctrl->u.pcm_prefer_subdevice);
break;
case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
ctrl->result = snd_ctl_rawmidi_next_device(ctl, &ctrl->u.device);
break;
case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
ctrl->result = snd_ctl_rawmidi_info(ctl, &ctrl->u.rawmidi_info);
break;
case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
ctrl->result = snd_ctl_rawmidi_prefer_subdevice(ctl, ctrl->u.rawmidi_prefer_subdevice);
break;
case SND_CTL_IOCTL_READ:
ctrl->result = snd_ctl_read1(ctl, &ctrl->u.read);
break;
case SND_CTL_IOCTL_CLOSE:
client->ops->close(client);
break;
case SND_CTL_IOCTL_POLL_DESCRIPTOR:
ctrl->result = 0;
return shm_ack_fd(client, snd_ctl_poll_descriptor(ctl));
default:
ERROR("Bogus cmd: %x", ctrl->cmd);
ctrl->result = -ENOSYS;
}
return shm_ack(client);
}
transport_ops_t ctl_shm_ops = {
open: ctl_shm_open,
cmd: ctl_shm_cmd,
close: ctl_shm_close,
};
int snd_client_open(client_t *client)
{
int err;
snd_client_open_request_t req;
snd_client_open_answer_t ans;
char *name;
memset(&ans, 0, sizeof(ans));
err = read(client->ctrl_fd, &req, sizeof(req));
if (err < 0) {
SYSERROR("read failed");
exit(1);
}
if (err != sizeof(req)) {
ans.result = -EINVAL;
goto _answer;
}
name = alloca(req.namelen);
err = read(client->ctrl_fd, name, req.namelen);
if (err < 0) {
SYSERROR("read failed");
exit(1);
}
if (err != req.namelen) {
ans.result = -EINVAL;
goto _answer;
}
switch (req.transport_type) {
case SND_TRANSPORT_TYPE_SHM:
if (!client->local) {
ans.result = -EINVAL;
goto _answer;
}
switch (req.dev_type) {
case SND_DEV_TYPE_PCM:
client->ops = &pcm_shm_ops;
break;
case SND_DEV_TYPE_CONTROL:
client->ops = &ctl_shm_ops;
break;
default:
ans.result = -EINVAL;
goto _answer;
}
break;
default:
ans.result = -EINVAL;
goto _answer;
}
name[req.namelen] = '\0';
client->transport_type = req.transport_type;
strcpy(client->name, name);
client->stream = req.stream;
client->mode = req.mode;
err = client->ops->open(client, &ans.cookie);
if (err < 0) {
ans.result = err;
} else {
client->open = 1;
ans.result = 0;
}
_answer:
err = write(client->ctrl_fd, &ans, sizeof(ans));
if (err != sizeof(ans)) {
SYSERROR("write failed");
exit(1);
}
return 0;
}
int client_poll_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
{
client_t *client = waiter->private_data;
if (client->open)
client->ops->close(client);
close(client->poll_fd);
close(client->ctrl_fd);
del_waiter(client->poll_fd);
del_waiter(client->ctrl_fd);
list_del(&client->list);
free(client);
return 0;
}
int client_ctrl_handler(waiter_t *waiter, unsigned short events)
{
client_t *client = waiter->private_data;
if (events & POLLHUP) {
if (client->open)
client->ops->close(client);
close(client->ctrl_fd);
del_waiter(client->ctrl_fd);
list_del(&client->list);
free(client);
return 0;
}
if (client->open)
return client->ops->cmd(client);
else
return snd_client_open(client);
}
int inet_pending_handler(waiter_t *waiter, unsigned short events)
{
inet_pending_t *pending = waiter->private_data;
inet_pending_t *pdata;
client_t *client;
uint32_t cookie;
struct list_head *item;
int remove = 0;
if (events & POLLHUP)
remove = 1;
else {
int err = read(waiter->fd, &cookie, sizeof(cookie));
if (err != sizeof(cookie))
remove = 1;
else {
err = write(waiter->fd, &cookie, sizeof(cookie));
if (err != sizeof(cookie))
remove = 1;
}
}
del_waiter(waiter->fd);
if (remove) {
close(waiter->fd);
list_del(&pending->list);
free(pending);
return 0;
}
list_for_each(item, &inet_pendings) {
pdata = list_entry(item, inet_pending_t, list);
if (pdata->cookie == cookie)
goto found;
}
pending->cookie = cookie;
return 0;
found:
client = calloc(1, sizeof(*client));
client->local = 0;
client->poll_fd = pdata->fd;
client->ctrl_fd = waiter->fd;
add_waiter(client->ctrl_fd, POLLIN | POLLHUP, client_ctrl_handler, client);
add_waiter(client->poll_fd, POLLHUP, client_poll_handler, client);
client->open = 0;
list_add_tail(&client->list, &clients);
list_del(&pending->list);
list_del(&pdata->list);
free(pending);
free(pdata);
return 0;
}
int local_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
{
int sock;
sock = accept(waiter->fd, 0, 0);
if (sock < 0) {
int result = -errno;
SYSERROR("accept failed");
return result;
} else {
client_t *client = calloc(1, sizeof(*client));
client->ctrl_fd = sock;
client->local = 1;
client->open = 0;
add_waiter(sock, POLLIN | POLLHUP, client_ctrl_handler, client);
list_add_tail(&client->list, &clients);
}
return 0;
}
int inet_handler(waiter_t *waiter, unsigned short events ATTRIBUTE_UNUSED)
{
int sock;
sock = accept(waiter->fd, 0, 0);
if (sock < 0) {
int result = -errno;
SYSERROR("accept failed");
return result;
} else {
inet_pending_t *pending = calloc(1, sizeof(*pending));
pending->fd = sock;
pending->cookie = 0;
add_waiter(sock, POLLIN, inet_pending_handler, pending);
list_add_tail(&pending->list, &inet_pendings);
}
return 0;
}
int server(const char *sockname, int port)
{
int err;
unsigned int k;
long open_max;
int result;
if (!sockname && port < 0)
return -EINVAL;
open_max = sysconf(_SC_OPEN_MAX);
if (open_max < 0) {
result = -errno;
SYSERROR("sysconf failed");
return result;
}
pollfds = calloc(open_max, sizeof(*pollfds));
waiters = calloc(open_max, sizeof(*waiters));
if (sockname) {
int sock = make_local_socket(sockname);
if (sock < 0)
return sock;
if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
result = -errno;
SYSERROR("fcntl O_NONBLOCK failed");
goto _end;
}
if (listen(sock, 4) < 0) {
result = -errno;
SYSERROR("listen failed");
goto _end;
}
add_waiter(sock, POLLIN, local_handler, NULL);
}
if (port >= 0) {
int sock = make_inet_socket(port);
if (sock < 0)
return sock;
if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
result = -errno;
SYSERROR("fcntl failed");
goto _end;
}
if (listen(sock, 4) < 0) {
result = -errno;
SYSERROR("listen failed");
goto _end;
}
add_waiter(sock, POLLIN, inet_handler, NULL);
}
while (1) {
struct pollfd pfds[open_max];
size_t pfds_count;
do {
err = poll(pollfds, pollfds_count, -1);
} while (err == 0);
if (err < 0) {
SYSERROR("poll failed");
continue;
}
pfds_count = pollfds_count;
memcpy(pfds, pollfds, sizeof(*pfds) * pfds_count);
for (k = 0; k < pfds_count; k++) {
struct pollfd *pfd = &pfds[k];
if (pfd->revents) {
waiter_t *w = &waiters[pfd->fd];
if (!w->handler)
continue;
err = w->handler(w, pfd->revents);
if (err < 0)
ERROR("waiter handler failed");
}
}
}
_end:
free(pollfds);
free(waiters);
return result;
}
void usage()
{
fprintf(stderr, "\
Usage: %s [OPTIONS] server
--help help
", command);
}
extern int is_local(struct hostent *hent);
int main(int argc, char **argv)
{
static struct option long_options[] = {
{"help", 0, 0, 'h'}
};
int c;
snd_config_t *conf;
snd_config_iterator_t i;
const char *socket = NULL;
const char *host = NULL;
long port = -1;
int err;
char *srvname;
struct hostent *h;
command = argv[0];
while ((c = getopt_long(argc, argv, "h", long_options, 0)) != -1) {
switch (c) {
case 'h':
usage();
return 0;
default:
fprintf(stderr, "Try `%s --help' for more information\n", command);
return 1;
}
}
if (argc - optind != 1) {
ERROR("you need to specify server name");
return 1;
}
err = snd_config_update();
if (err < 0) {
ERROR("cannot read configuration file");
return 1;
}
srvname = argv[optind];
err = snd_config_searchv(snd_config, &conf, "server", srvname, 0);
if (err < 0) {
ERROR("Missing definition for server %s", srvname);
return 1;
}
snd_config_foreach(i, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id = snd_config_get_id(n);
if (strcmp(id, "comment") == 0)
continue;
if (strcmp(id, "host") == 0) {
err = snd_config_get_string(n, &host);
if (err < 0) {
ERROR("Invalid type for %s", id);
return 1;
}
continue;
}
if (strcmp(id, "socket") == 0) {
err = snd_config_get_string(n, &socket);
if (err < 0) {
ERROR("Invalid type for %s", id);
return 1;
}
continue;
}
if (strcmp(id, "port") == 0) {
err = snd_config_get_integer(n, &port);
if (err < 0) {
ERROR("Invalid type for %s", id);
return 1;
}
continue;
}
ERROR("Unknown field %s", id);
return 1;
}
if (!host) {
ERROR("host is not defined");
return 1;
}
h = gethostbyname(host);
if (!h) {
ERROR("Cannot resolve %s", host);
return 1;
}
if (!is_local(h)) {
ERROR("%s is not the local host", host);
return 1;
}
if (!socket && port < 0) {
ERROR("either socket or port need to be defined");
return 1;
}
server(socket, port);
return 0;
}
|