chiark / gitweb /
actually compile
[chiark-utils.git] / cprogs / trivsoundd.c
1 /*
2  * triv-sound-d.c
3  * writebuffer adapted for sound-playing
4  *
5  * readbuffer and writebuffer are:
6  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
7  *
8  * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
9  * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
10  * chiark backup is:
11  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
12  *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
13  *
14  * This is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 3,
17  * or (at your option) any later version.
18  *
19  * This is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public
25  * License along with this file; if not, consult the Free Software
26  * Foundation's website at www.fsf.org, or the GNU Project website at
27  * www.gnu.org.
28  *
29  */
30
31 #include "rwbuffer.h"
32
33 const char *progname= "trivsoundd";
34
35 static int maxstartdelay=60, maxbadaccept=10;
36
37 struct inqnode {
38   struct inqnode *next, *back;
39   time_t accepted;
40   int fd;
41 };
42
43 static struct { struct inqnode *head, *tail; } inq;
44 static int master, sdev;
45 static time_t now;
46
47 static void usageerr(const char *m) {
48   fprintf(stderr,"bad usage: %s\n",m);
49   exit(12);
50 }
51
52 static void bindmaster(const char *bindname) {
53   union {
54     struct sockaddr sa;
55     struct sockaddr_in sin;
56     struct sockaddr_un sun;
57   } su;
58   socklen_t sulen;
59   const char *colon;
60   char *copy, *ep;
61   int r;
62   unsigned long portul;
63   struct hostent *he;
64   struct servent *se;
65
66   memset(&su,0,sizeof(su));
67
68   if (bindname[0]=='/' || bindname[0]=='.') {
69
70     if (strlen(bindname) >= sizeof(su.sun.sun_path))
71       usageerr("AF_UNIX bind path too long");
72     sulen= sizeof(su.sun);
73     su.sun.sun_family= AF_UNIX;
74     strcpy(su.sun.sun_path, bindname);
75
76   } else if (bindname[0] != ':' && (colon= strrchr(bindname,':'))) {
77
78     sulen= sizeof(su.sin);
79     su.sin.sin_family= AF_INET;
80
81     copy= xmalloc(colon - bindname + 1);
82     memcpy(copy,bindname, colon - bindname + 1);
83     copy[colon - bindname]= 0;
84     portul= strtoul(colon+1,&ep,0);
85
86     if (!*ep) {
87       if (!portul || portul>=65536) usageerr("invalid port number");
88       su.sin.sin_port= htons(portul);
89     } else {
90       se= getservbyname(colon+1, "tcp");
91       if (!se) { fprintf(stderr,"unknown service `%s'\n",colon+1); exit(4); }
92       su.sin.sin_port= htons(se->s_port);
93     }
94
95     if (!strcmp(copy,"any")) {
96       su.sin.sin_addr.s_addr= INADDR_ANY;
97     } else if (!inet_aton(copy,&su.sin.sin_addr)) {
98       he= gethostbyname(copy);
99       if (!he) { herror(copy); exit(4); }
100       if (he->h_addrtype != AF_INET ||
101           he->h_length != sizeof(su.sin.sin_addr) ||
102           !he->h_addr_list[0] ||
103           he->h_addr_list[1]) {
104         fprintf(stderr,"hostname lookup `%s' did not yield"
105                 " exactly one IPv4 address\n",copy);
106         exit(4);
107       }
108       memcpy(&su.sin.sin_addr, he->h_addr_list[0], sizeof(su.sin.sin_addr));
109     }
110
111   } else {
112     usageerr("unknown bind name");
113     exit(12);
114   }
115
116   master= socket(su.sa.sa_family,SOCK_STREAM,0);
117   if (master<0) { perror("socket"); exit(8); }
118
119   r= bind(master, &su.sa, sulen);
120   if (r) { perror("bind"); exit(8); }
121
122   r= listen(master, 5);
123   if (r) { perror("listen"); exit(8); }
124 }
125
126 static void opensounddevice(void) {
127   int r;
128   char cbuf[200];
129   
130   sdev= open("/dev/dsp", O_WRONLY);
131   if (sdev<0) { perror("open sound device"); exit(8); }
132
133   snprintf(cbuf, sizeof(cbuf), "sox -t raw -s -w -r 44100 -c 2"
134            " - </dev/null -t ossdsp - >&%d", sdev);
135   r= system(cbuf);  if (r) { fprintf(stderr,"sox gave %d\n",r); exit(5); }
136 }
137
138 void wrbuf_report(const char *m) {
139   printf("writing %s\n", m);
140 }
141
142 static void selectcopy(void) {
143   int slave= inq.head ? inq.head->fd : -1;
144   wrbufcore_prepselect(slave, sdev);
145   fdsetset(master,&readfds);
146   callselect();
147   wrbufcore_afterselect(slave, sdev);
148 }
149
150 static void expireoldconns(void) {
151   struct inqnode *searchold, *nextsearchold;
152       
153   for (searchold= inq.head ? inq.head->next : 0;
154        searchold;
155        searchold= nextsearchold) {
156     nextsearchold= searchold->next;
157     if (searchold->accepted < now-maxstartdelay) {
158       printf("expired %p\n",searchold);
159       LIST_UNLINK(inq,searchold);
160       free(searchold);
161     }
162   }
163 }
164
165 static void acceptnewconns(void) {
166   static int bad;
167   
168   int slave;
169   struct inqnode *new;
170
171   if (!FD_ISSET(master,&readfds)) return;
172
173   slave= accept(master,0,0);
174   if (slave < 0) {
175     if (!(errno == EINTR ||
176           errno == EAGAIN ||
177           errno == EWOULDBLOCK)) {
178       perror("accept");
179       bad++;
180       if (bad > maxbadaccept) {
181         fprintf(stderr,"accept failures repeating\n");
182         exit(4);
183       }
184     }
185     /* any transient error will just send us round again via select */
186     return;
187   }
188
189   bad= 0;
190   new= xmalloc(sizeof(struct inqnode));
191   new->accepted= now;
192   new->fd= slave;
193   LIST_LINK_TAIL(inq,new);
194
195   printf("accepted %p\n",new);
196 }
197
198 static void switchinput(void) {
199   struct inqnode *old;
200   if (!seeneof) return;
201   old= inq.head;
202   assert(old);
203   printf("finished %p\n",old);
204   close(old->fd);
205   LIST_UNLINK(inq,old);
206   free(old);
207   seeneof= 0;
208 }  
209
210 int main(int argc, const char *const *argv) {
211   assert(argv[0]);
212   if (!argv[1] || argv[2] || argv[1][0]=='-')
213     usageerr("no options allowed, must have one argument (bindname)");
214
215   buffersize= 44100*4* 5/*seconds*/;
216
217   opensounddevice();
218   bindmaster(argv[1]);
219   nonblock(sdev,1);
220   nonblock(master,1);
221
222   startupcore();
223   wrbufcore_startup();
224   
225   printf("started\n");
226   for (;;) {
227     selectcopy();
228     if (time(&now)==(time_t)-1) { perror("time(2)"); exit(4); }
229     expireoldconns();
230     acceptnewconns();
231     switchinput();
232   }
233 }