File: trivsoundd.c

package info (click to toggle)
chiark-utils 8.0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: ansic: 4,640; perl: 4,281; sh: 671; python: 465; makefile: 286; tcl: 228
file content (233 lines) | stat: -rw-r--r-- 5,947 bytes parent folder | download | duplicates (9)
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
/*
 * triv-sound-d.c
 * writebuffer adapted for sound-playing
 *
 * readbuffer and writebuffer are:
 *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
 *
 * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
 * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
 * chiark backup is:
 *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
 *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
 *
 * This 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 3,
 * or (at your option) any later version.
 *
 * This 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 file; if not, consult the Free Software
 * Foundation's website at www.fsf.org, or the GNU Project website at
 * www.gnu.org.
 *
 */

#include "rwbuffer.h"

const char *progname= "trivsoundd";

static int maxstartdelay=60, maxbadaccept=10;

struct inqnode {
  struct inqnode *next, *back;
  time_t accepted;
  int fd;
};

static struct { struct inqnode *head, *tail; } inq;
static int master, sdev;
static time_t now;

static void usageerr(const char *m) {
  fprintf(stderr,"bad usage: %s\n",m);
  exit(12);
}

static void bindmaster(const char *bindname) {
  union {
    struct sockaddr sa;
    struct sockaddr_in sin;
    struct sockaddr_un sun;
  } su;
  socklen_t sulen;
  const char *colon;
  char *copy, *ep;
  int r;
  unsigned long portul;
  struct hostent *he;
  struct servent *se;

  memset(&su,0,sizeof(su));

  if (bindname[0]=='/' || bindname[0]=='.') {

    if (strlen(bindname) >= sizeof(su.sun.sun_path))
      usageerr("AF_UNIX bind path too long");
    sulen= sizeof(su.sun);
    su.sun.sun_family= AF_UNIX;
    strcpy(su.sun.sun_path, bindname);

  } else if (bindname[0] != ':' && (colon= strrchr(bindname,':'))) {

    sulen= sizeof(su.sin);
    su.sin.sin_family= AF_INET;

    copy= xmalloc(colon - bindname + 1);
    memcpy(copy,bindname, colon - bindname + 1);
    copy[colon - bindname]= 0;
    portul= strtoul(colon+1,&ep,0);

    if (!*ep) {
      if (!portul || portul>=65536) usageerr("invalid port number");
      su.sin.sin_port= htons(portul);
    } else {
      se= getservbyname(colon+1, "tcp");
      if (!se) { fprintf(stderr,"unknown service `%s'\n",colon+1); exit(4); }
      su.sin.sin_port= htons(se->s_port);
    }

    if (!strcmp(copy,"any")) {
      su.sin.sin_addr.s_addr= INADDR_ANY;
    } else if (!inet_aton(copy,&su.sin.sin_addr)) {
      he= gethostbyname(copy);
      if (!he) { herror(copy); exit(4); }
      if (he->h_addrtype != AF_INET ||
	  he->h_length != sizeof(su.sin.sin_addr) ||
	  !he->h_addr_list[0] ||
	  he->h_addr_list[1]) {
	fprintf(stderr,"hostname lookup `%s' did not yield"
		" exactly one IPv4 address\n",copy);
	exit(4);
      }
      memcpy(&su.sin.sin_addr, he->h_addr_list[0], sizeof(su.sin.sin_addr));
    }

  } else {
    usageerr("unknown bind name");
    exit(12);
  }

  master= socket(su.sa.sa_family,SOCK_STREAM,0);
  if (master<0) { perror("socket"); exit(8); }

  r= bind(master, &su.sa, sulen);
  if (r) { perror("bind"); exit(8); }

  r= listen(master, 5);
  if (r) { perror("listen"); exit(8); }
}

static void opensounddevice(void) {
  int r;
  char cbuf[200];
  
  sdev= open("/dev/dsp", O_WRONLY);
  if (sdev<0) { perror("open sound device"); exit(8); }

  snprintf(cbuf, sizeof(cbuf), "sox -t raw -s -w -r 44100 -c 2"
	   " - </dev/null -t ossdsp - >&%d", sdev);
  r= system(cbuf);  if (r) { fprintf(stderr,"sox gave %d\n",r); exit(5); }
}

void wrbuf_report(const char *m) {
  printf("writing %s\n", m);
}

static void selectcopy(void) {
  int slave= inq.head ? inq.head->fd : -1;
  wrbufcore_prepselect(slave, sdev);
  fdsetset(master,&readfds);
  callselect();
  wrbufcore_afterselect(slave, sdev);
}

static void expireoldconns(void) {
  struct inqnode *searchold, *nextsearchold;
      
  for (searchold= inq.head ? inq.head->next : 0;
       searchold;
       searchold= nextsearchold) {
    nextsearchold= searchold->next;
    if (searchold->accepted < now-maxstartdelay) {
      printf("expired %p\n",searchold);
      LIST_UNLINK(inq,searchold);
      free(searchold);
    }
  }
}

static void acceptnewconns(void) {
  static int bad;
  
  int slave;
  struct inqnode *new;

  if (!FD_ISSET(master,&readfds)) return;

  slave= accept(master,0,0);
  if (slave < 0) {
    if (!(errno == EINTR ||
	  errno == EAGAIN ||
	  errno == EWOULDBLOCK)) {
      perror("accept");
      bad++;
      if (bad > maxbadaccept) {
	fprintf(stderr,"accept failures repeating\n");
	exit(4);
      }
    }
    /* any transient error will just send us round again via select */
    return;
  }

  bad= 0;
  new= xmalloc(sizeof(struct inqnode));
  new->accepted= now;
  new->fd= slave;
  LIST_LINK_TAIL(inq,new);

  printf("accepted %p\n",new);
}

static void switchinput(void) {
  struct inqnode *old;
  if (!seeneof) return;
  old= inq.head;
  assert(old);
  printf("finished %p\n",old);
  close(old->fd);
  LIST_UNLINK(inq,old);
  free(old);
  seeneof= 0;
}  

int main(int argc, const char *const *argv) {
  assert(argv[0]);
  if (!argv[1] || argv[2] || argv[1][0]=='-')
    usageerr("no options allowed, must have one argument (bindname)");

  buffersize= 44100*4* 5/*seconds*/;

  opensounddevice();
  bindmaster(argv[1]);
  nonblock(sdev,1);
  nonblock(master,1);

  startupcore();
  wrbufcore_startup();
  
  printf("started\n");
  for (;;) {
    selectcopy();
    if (time(&now)==(time_t)-1) { perror("time(2)"); exit(4); }
    expireoldconns();
    acceptnewconns();
    switchinput();
  }
}