Annotation of src/usr.bin/aucat/aucat.c, Revision 1.141
1.140 ratchov 1: /* $OpenBSD$ */
1.1 kstailey 2: /*
1.15 ratchov 3: * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
1.1 kstailey 4: *
1.15 ratchov 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/param.h>
18: #include <sys/queue.h>
1.55 ratchov 19: #include <sys/stat.h>
1.62 ratchov 20: #include <sys/types.h>
1.86 ratchov 21: #include <sys/resource.h>
1.13 uwe 22:
1.15 ratchov 23: #include <err.h>
1.55 ratchov 24: #include <errno.h>
1.1 kstailey 25: #include <fcntl.h>
1.138 ratchov 26: #include <grp.h>
1.55 ratchov 27: #include <limits.h>
1.86 ratchov 28: #include <pwd.h>
1.15 ratchov 29: #include <signal.h>
1.135 ratchov 30: #include <sndio.h>
1.1 kstailey 31: #include <stdio.h>
1.4 millert 32: #include <stdlib.h>
1.8 david 33: #include <string.h>
1.1 kstailey 34: #include <unistd.h>
35:
1.62 ratchov 36: #include "abuf.h"
1.112 ratchov 37: #include "amsg.h"
1.15 ratchov 38: #include "aparams.h"
39: #include "aproc.h"
1.62 ratchov 40: #include "conf.h"
41: #include "dev.h"
1.28 ratchov 42: #include "listen.h"
1.61 ratchov 43: #include "midi.h"
44: #include "opt.h"
1.62 ratchov 45: #include "wav.h"
1.78 ratchov 46: #ifdef DEBUG
47: #include "dbg.h"
48: #endif
1.11 jaredy 49:
1.86 ratchov 50: /*
51: * unprivileged user name
52: */
1.138 ratchov 53: #ifndef SNDIO_USER
1.86 ratchov 54: #define SNDIO_USER "_sndio"
1.138 ratchov 55: #endif
1.86 ratchov 56:
57: /*
58: * priority when run as root
59: */
1.138 ratchov 60: #ifndef SNDIO_PRIO
1.86 ratchov 61: #define SNDIO_PRIO (-20)
1.138 ratchov 62: #endif
1.86 ratchov 63:
1.61 ratchov 64: #define PROG_AUCAT "aucat"
1.129 ratchov 65: #define PROG_SNDIOD "sndiod"
1.61 ratchov 66:
1.108 ratchov 67: /*
68: * sample rate if no ``-r'' is used
69: */
70: #ifndef DEFAULT_RATE
1.132 ratchov 71: #define DEFAULT_RATE 48000
1.108 ratchov 72: #endif
73:
1.120 ratchov 74: /*
1.133 ratchov 75: * block size if neither ``-z'' nor ``-b'' is used
1.120 ratchov 76: */
77: #ifndef DEFAULT_ROUND
1.133 ratchov 78: #define DEFAULT_ROUND 960
79: #endif
80:
81: /*
82: * buffer size if neither ``-z'' nor ``-b'' is used
83: */
84: #ifndef DEFAULT_BUFSZ
85: #define DEFAULT_BUFSZ 7860
1.120 ratchov 86: #endif
87:
1.125 ratchov 88: /*
89: * default device in server mode
90: */
91: #ifndef DEFAULT_DEV
92: #define DEFAULT_DEV "rsnd/0"
93: #endif
94:
1.78 ratchov 95: #ifdef DEBUG
1.120 ratchov 96: volatile sig_atomic_t debug_level = 1;
1.78 ratchov 97: #endif
1.111 deraadt 98: volatile sig_atomic_t quit_flag = 0;
1.7 deraadt 99:
1.141 ! ratchov 100: char aucat_usage[] = "usage: " PROG_AUCAT " [-dMn] "
! 101: "[-C min:max] [-c min:max] [-e enc] [-f device]\n\t"
! 102: "[-h fmt] [-i file] [-j flag] [-m mode] [-o file] [-q port]\n\t"
! 103: "[-r rate] [-t mode] [-v volume] [-w flag] [-x policy]\n";
1.129 ratchov 104:
1.130 ratchov 105: char sndiod_usage[] = "usage: " PROG_SNDIOD " [-dM] [-a flag] [-b nframes] "
1.129 ratchov 106: "[-C min:max] [-c min:max] [-e enc]\n\t"
107: "[-f device] [-j flag] [-L addr] [-m mode] [-q port] [-r rate]\n\t"
108: "[-s name] [-t mode] [-U unit] [-v volume] [-w flag] [-x policy]\n\t"
109: "[-z nframes]\n";
1.120 ratchov 110:
1.28 ratchov 111: /*
112: * SIGINT handler, it raises the quit flag. If the flag is already set,
113: * that means that the last SIGINT was not handled, because the process
1.62 ratchov 114: * is blocked somewhere, so exit.
1.28 ratchov 115: */
116: void
117: sigint(int s)
118: {
119: if (quit_flag)
120: _exit(1);
121: quit_flag = 1;
122: }
1.22 ratchov 123:
1.78 ratchov 124: #ifdef DEBUG
125: /*
126: * Increase debug level on SIGUSR1.
127: */
128: void
129: sigusr1(int s)
130: {
131: if (debug_level < 4)
132: debug_level++;
133: }
134:
135: /*
136: * Decrease debug level on SIGUSR2.
137: */
138: void
139: sigusr2(int s)
140: {
141: if (debug_level > 0)
142: debug_level--;
143: }
144: #endif
1.15 ratchov 145:
146: void
147: opt_ch(struct aparams *par)
148: {
1.76 ratchov 149: char *next, *end;
150: long cmin, cmax;
1.13 uwe 151:
1.76 ratchov 152: errno = 0;
153: cmin = strtol(optarg, &next, 10);
154: if (next == optarg || *next != ':')
155: goto failed;
156: cmax = strtol(++next, &end, 10);
157: if (end == next || *end != '\0')
158: goto failed;
159: if (cmin < 0 || cmax < cmin || cmax > NCHAN_MAX)
160: goto failed;
161: par->cmin = cmin;
162: par->cmax = cmax;
163: return;
164: failed:
165: errx(1, "%s: bad channel range", optarg);
1.15 ratchov 166: }
1.13 uwe 167:
1.15 ratchov 168: void
169: opt_enc(struct aparams *par)
170: {
1.28 ratchov 171: int len;
172:
173: len = aparams_strtoenc(par, optarg);
174: if (len == 0 || optarg[len] != '\0')
175: errx(1, "%s: bad encoding", optarg);
1.15 ratchov 176: }
1.4 millert 177:
1.15 ratchov 178: int
179: opt_hdr(void)
180: {
181: if (strcmp("auto", optarg) == 0)
182: return HDR_AUTO;
183: if (strcmp("raw", optarg) == 0)
184: return HDR_RAW;
185: if (strcmp("wav", optarg) == 0)
186: return HDR_WAV;
1.35 ratchov 187: errx(1, "%s: bad header specification", optarg);
1.1 kstailey 188: }
189:
1.22 ratchov 190: int
1.74 ratchov 191: opt_mmc(void)
192: {
193: if (strcmp("off", optarg) == 0)
194: return 0;
195: if (strcmp("slave", optarg) == 0)
196: return 1;
197: errx(1, "%s: bad MMC mode", optarg);
198: }
199:
200: int
1.90 ratchov 201: opt_onoff(void)
1.84 ratchov 202: {
203: if (strcmp("off", optarg) == 0)
204: return 0;
205: if (strcmp("on", optarg) == 0)
206: return 1;
207: errx(1, "%s: bad join/expand setting", optarg);
208: }
209:
210: int
1.22 ratchov 211: opt_xrun(void)
212: {
213: if (strcmp("ignore", optarg) == 0)
214: return XRUN_IGNORE;
215: if (strcmp("sync", optarg) == 0)
216: return XRUN_SYNC;
217: if (strcmp("error", optarg) == 0)
218: return XRUN_ERROR;
1.73 ratchov 219: errx(1, "%s: bad underrun/overrun policy", optarg);
1.22 ratchov 220: }
221:
1.134 ratchov 222: unsigned int
1.43 ratchov 223: opt_mode(void)
224: {
1.134 ratchov 225: unsigned int mode = 0;
1.83 ratchov 226: char *p = optarg;
227: size_t len;
228:
1.104 ratchov 229: for (p = optarg; *p != '\0'; p++) {
1.83 ratchov 230: len = strcspn(p, ",");
231: if (strncmp("play", p, len) == 0) {
232: mode |= MODE_PLAY;
233: } else if (strncmp("rec", p, len) == 0) {
234: mode |= MODE_REC;
235: } else if (strncmp("mon", p, len) == 0) {
236: mode |= MODE_MON;
1.120 ratchov 237: } else if (strncmp("midi", p, len) == 0) {
238: mode |= MODE_MIDIMASK;
1.83 ratchov 239: } else
240: errx(1, "%s: bad mode", optarg);
241: p += len;
242: if (*p == '\0')
243: break;
244: }
245: if (mode == 0)
246: errx(1, "empty mode");
247: return mode;
1.43 ratchov 248: }
249:
1.113 ratchov 250: void
1.61 ratchov 251: setsig(void)
252: {
253: struct sigaction sa;
254:
255: quit_flag = 0;
256: sigfillset(&sa.sa_mask);
257: sa.sa_flags = SA_RESTART;
258: sa.sa_handler = sigint;
259: if (sigaction(SIGINT, &sa, NULL) < 0)
1.68 ratchov 260: err(1, "sigaction(int) failed");
1.61 ratchov 261: if (sigaction(SIGTERM, &sa, NULL) < 0)
1.68 ratchov 262: err(1, "sigaction(term) failed");
1.61 ratchov 263: if (sigaction(SIGHUP, &sa, NULL) < 0)
1.68 ratchov 264: err(1, "sigaction(hup) failed");
1.78 ratchov 265: #ifdef DEBUG
266: sa.sa_handler = sigusr1;
267: if (sigaction(SIGUSR1, &sa, NULL) < 0)
268: err(1, "sigaction(usr1) failed");
269: sa.sa_handler = sigusr2;
270: if (sigaction(SIGUSR2, &sa, NULL) < 0)
271: err(1, "sigaction(usr2) failed1n");
272: #endif
1.61 ratchov 273: }
274:
275: void
276: unsetsig(void)
277: {
278: struct sigaction sa;
279:
280: sigfillset(&sa.sa_mask);
281: sa.sa_flags = SA_RESTART;
282: sa.sa_handler = SIG_DFL;
1.78 ratchov 283: #ifdef DEBUG
284: if (sigaction(SIGUSR2, &sa, NULL) < 0)
285: err(1, "unsetsig(usr2): sigaction failed");
286: if (sigaction(SIGUSR1, &sa, NULL) < 0)
287: err(1, "unsetsig(usr1): sigaction failed");
288: #endif
1.61 ratchov 289: if (sigaction(SIGHUP, &sa, NULL) < 0)
1.68 ratchov 290: err(1, "unsetsig(hup): sigaction failed\n");
1.61 ratchov 291: if (sigaction(SIGTERM, &sa, NULL) < 0)
1.68 ratchov 292: err(1, "unsetsig(term): sigaction failed\n");
1.61 ratchov 293: if (sigaction(SIGINT, &sa, NULL) < 0)
1.68 ratchov 294: err(1, "unsetsig(int): sigaction failed\n");
1.61 ratchov 295: }
296:
297: void
298: getbasepath(char *base, size_t size)
299: {
300: uid_t uid;
301: struct stat sb;
1.86 ratchov 302: mode_t mask;
1.61 ratchov 303:
304: uid = geteuid();
1.86 ratchov 305: if (uid == 0) {
306: mask = 022;
307: snprintf(base, PATH_MAX, "/tmp/aucat");
308: } else {
309: mask = 077;
310: snprintf(base, PATH_MAX, "/tmp/aucat-%u", uid);
311: }
312: if (mkdir(base, 0777 & ~mask) < 0) {
1.61 ratchov 313: if (errno != EEXIST)
314: err(1, "mkdir(\"%s\")", base);
315: }
316: if (stat(base, &sb) < 0)
317: err(1, "stat(\"%s\")", base);
1.86 ratchov 318: if (sb.st_uid != uid || (sb.st_mode & mask) != 0)
1.61 ratchov 319: errx(1, "%s has wrong permissions", base);
320: }
321:
322: void
1.86 ratchov 323: privdrop(void)
324: {
325: struct passwd *pw;
326:
327: if ((pw = getpwnam(SNDIO_USER)) == NULL)
1.105 deraadt 328: errx(1, "unknown user %s", SNDIO_USER);
1.86 ratchov 329: if (setpriority(PRIO_PROCESS, 0, SNDIO_PRIO) < 0)
330: err(1, "setpriority");
331: if (setgroups(1, &pw->pw_gid) ||
332: setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
333: setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
334: err(1, "cannot drop privileges");
335: }
336:
1.120 ratchov 337: struct dev *
338: mkdev(char *path, int mode, int bufsz, int round, int hold, int autovol)
1.61 ratchov 339: {
1.120 ratchov 340: struct dev *d;
341:
342: if (path) {
343: for (d = dev_list; d != NULL; d = d->next) {
344: if (d->reqmode & (MODE_LOOP | MODE_THRU))
345: continue;
346: if (strcmp(d->path, path) == 0)
347: return d;
348: }
349: } else {
350: if (dev_list)
351: return dev_list;
1.135 ratchov 352: path = SIO_DEVANY;
1.120 ratchov 353: }
1.133 ratchov 354: if (!bufsz && !round) {
355: round = DEFAULT_ROUND;
356: bufsz = DEFAULT_BUFSZ;
357: } else if (!bufsz) {
1.132 ratchov 358: bufsz = round * 2;
1.120 ratchov 359: } else if (!round)
1.132 ratchov 360: round = bufsz / 2;
1.125 ratchov 361: d = dev_new(path, mode, bufsz, round, hold, autovol);
362: if (d == NULL)
363: exit(1);
364: return d;
1.120 ratchov 365: }
366:
367: struct opt *
368: mkopt(char *path, struct dev *d, struct aparams *rpar, struct aparams *ppar,
369: int mode, int vol, int mmc, int join)
370: {
371: struct opt *o;
372:
373: if (d->reqmode & MODE_LOOP)
374: errx(1, "%s: can't attach to loopback", path);
375: if (d->reqmode & MODE_THRU)
376: mode = MODE_MIDIMASK;
377: if (!rpar->rate)
378: ppar->rate = rpar->rate = DEFAULT_RATE;
379: o = opt_new(path, d, rpar, ppar, MIDI_TO_ADATA(vol), mmc, join, mode);
380: if (o == NULL)
381: errx(1, "%s: couldn't create subdev", path);
382: dev_adjpar(d, o->mode, rpar, ppar);
383: return o;
1.61 ratchov 384: }
385:
1.1 kstailey 386: int
1.120 ratchov 387: main(int argc, char **argv)
1.1 kstailey 388: {
1.122 ratchov 389: char *prog, *optstr, *usagestr;
1.128 ratchov 390: int c, background, unit, active;
1.92 ratchov 391: char base[PATH_MAX], path[PATH_MAX];
1.134 ratchov 392: unsigned int mode, hdr, xrun, rate, join, mmc, vol;
393: unsigned int hold, autovol, bufsz, round;
1.76 ratchov 394: const char *str;
1.120 ratchov 395: struct aparams ppar, rpar;
1.92 ratchov 396: struct dev *d, *dnext;
1.120 ratchov 397: struct listen *l;
398: struct wav *w;
1.19 ratchov 399:
1.92 ratchov 400: /*
401: * global options defaults
402: */
1.120 ratchov 403: hdr = HDR_AUTO;
404: xrun = XRUN_IGNORE;
405: vol = MIDI_MAXCTL;
1.124 ratchov 406: join = 1;
1.120 ratchov 407: mmc = 0;
1.124 ratchov 408: hold = 0;
409: autovol = 1;
1.120 ratchov 410: bufsz = 0;
411: round = 0;
412: unit = 0;
413: background = 0;
414: aparams_init(&ppar, 0, 1, DEFAULT_RATE);
415: aparams_init(&rpar, 0, 1, DEFAULT_RATE);
1.123 ratchov 416: mode = MODE_MIDIMASK | MODE_PLAY | MODE_REC;
1.120 ratchov 417:
418: #ifdef DEBUG
419: atexit(dbg_flush);
420: #endif
421: setsig();
422: filelist_init();
423:
424: prog = strrchr(argv[0], '/');
425: if (prog == NULL)
426: prog = argv[0];
427: else
428: prog++;
429: if (strcmp(prog, PROG_AUCAT) == 0) {
1.141 ! ratchov 430: optstr = "a:b:c:C:de:f:h:i:j:L:m:Mno:q:r:s:t:U:v:w:x:z:";
1.120 ratchov 431: usagestr = aucat_usage;
1.140 ratchov 432: hold = 1;
1.129 ratchov 433: } else if (strcmp(prog, PROG_SNDIOD) == 0) {
434: optstr = "a:b:c:C:de:f:j:L:m:Mq:r:s:t:U:v:w:x:z:";
435: usagestr = sndiod_usage;
436: background = 1;
1.137 ratchov 437: } else
438: errx(1, "%s: can't determine program to run", prog);
1.92 ratchov 439:
1.120 ratchov 440: while ((c = getopt(argc, argv, optstr)) != -1) {
1.15 ratchov 441: switch (c) {
1.69 ratchov 442: case 'd':
1.78 ratchov 443: #ifdef DEBUG
1.120 ratchov 444: if (debug_level < 4)
1.78 ratchov 445: debug_level++;
446: #endif
1.129 ratchov 447: background = 0;
1.51 ratchov 448: break;
1.92 ratchov 449: case 'U':
1.128 ratchov 450: if (listen_list)
451: errx(1, "-U must come before -L");
1.92 ratchov 452: unit = strtonum(optarg, 0, MIDI_MAXCTL, &str);
453: if (str)
454: errx(1, "%s: unit number is %s", optarg, str);
455: break;
1.113 ratchov 456: case 'L':
1.122 ratchov 457: listen_new_tcp(optarg, AUCAT_PORT + unit);
1.113 ratchov 458: break;
1.43 ratchov 459: case 'm':
1.120 ratchov 460: mode = opt_mode();
1.43 ratchov 461: break;
1.15 ratchov 462: case 'h':
1.120 ratchov 463: hdr = opt_hdr();
1.15 ratchov 464: break;
1.22 ratchov 465: case 'x':
1.120 ratchov 466: xrun = opt_xrun();
1.22 ratchov 467: break;
1.84 ratchov 468: case 'j':
1.120 ratchov 469: join = opt_onoff();
1.84 ratchov 470: break;
1.74 ratchov 471: case 't':
1.120 ratchov 472: mmc = opt_mmc();
1.74 ratchov 473: break;
1.15 ratchov 474: case 'c':
1.120 ratchov 475: opt_ch(&ppar);
1.15 ratchov 476: break;
477: case 'C':
1.120 ratchov 478: opt_ch(&rpar);
1.15 ratchov 479: break;
480: case 'e':
1.120 ratchov 481: opt_enc(&ppar);
482: aparams_copyenc(&rpar, &ppar);
1.15 ratchov 483: break;
484: case 'r':
1.92 ratchov 485: rate = strtonum(optarg, RATE_MIN, RATE_MAX, &str);
1.76 ratchov 486: if (str)
487: errx(1, "%s: rate is %s", optarg, str);
1.120 ratchov 488: ppar.rate = rpar.rate = rate;
1.15 ratchov 489: break;
1.35 ratchov 490: case 'v':
1.120 ratchov 491: vol = strtonum(optarg, 0, MIDI_MAXCTL, &str);
1.76 ratchov 492: if (str)
493: errx(1, "%s: volume is %s", optarg, str);
1.35 ratchov 494: break;
1.15 ratchov 495: case 'i':
1.120 ratchov 496: d = mkdev(NULL, 0, bufsz, round, 1, autovol);
497: w = wav_new_in(&wav_ops, d,
498: mode & (MODE_PLAY | MODE_MIDIOUT), optarg,
499: hdr, &ppar, xrun, vol, mmc, join);
500: if (w == NULL)
501: errx(1, "%s: couldn't create stream", optarg);
502: dev_adjpar(d, w->mode, NULL, &w->hpar);
503: break;
504: case 'o':
505: d = mkdev(NULL, 0, bufsz, round, 1, autovol);
506: w = wav_new_out(&wav_ops, d,
507: mode & (MODE_RECMASK | MODE_MIDIIN), optarg,
508: hdr, &rpar, xrun, mmc, join);
509: if (w == NULL)
510: errx(1, "%s: couldn't create stream", optarg);
511: dev_adjpar(d, w->mode, &w->hpar, NULL);
1.15 ratchov 512: break;
1.120 ratchov 513: case 's':
1.131 ratchov 514: if ((d = dev_list) == NULL) {
515: d = mkdev(DEFAULT_DEV, 0, bufsz, round,
516: hold, autovol);
517: }
518: mkopt(optarg, d, &rpar, &ppar, mode, vol, mmc, join);
1.120 ratchov 519: /* XXX: set device rate, if never set */
1.42 ratchov 520: break;
1.120 ratchov 521: case 'q':
522: d = mkdev(NULL, mode, bufsz, round, 1, autovol);
523: if (!devctl_add(d, optarg, MODE_MIDIMASK))
524: errx(1, "%s: can't open port", optarg);
525: d->reqmode |= MODE_MIDIMASK;
1.92 ratchov 526: break;
527: case 'a':
1.120 ratchov 528: hold = opt_onoff();
1.83 ratchov 529: break;
1.115 ratchov 530: case 'w':
1.120 ratchov 531: autovol = opt_onoff();
1.4 millert 532: break;
1.28 ratchov 533: case 'b':
1.120 ratchov 534: bufsz = strtonum(optarg, 1, RATE_MAX * 5, &str);
1.76 ratchov 535: if (str)
536: errx(1, "%s: buffer size is %s", optarg, str);
1.28 ratchov 537: break;
1.74 ratchov 538: case 'z':
1.120 ratchov 539: round = strtonum(optarg, 1, SHRT_MAX, &str);
1.76 ratchov 540: if (str)
541: errx(1, "%s: block size is %s", optarg, str);
1.74 ratchov 542: break;
1.92 ratchov 543: case 'f':
1.120 ratchov 544: mkdev(optarg, 0, bufsz, round, hold, autovol);
545: break;
546: case 'n':
547: mkdev("loopback", MODE_LOOP, bufsz, round, 1, autovol);
548: break;
549: case 'M':
1.127 ratchov 550: mkdev("midithru", MODE_THRU, 0, 0, hold, 0);
1.92 ratchov 551: break;
1.11 jaredy 552: default:
1.120 ratchov 553: fputs(usagestr, stderr);
1.15 ratchov 554: exit(1);
1.4 millert 555: }
556: }
557: argc -= optind;
558: argv += optind;
1.92 ratchov 559: if (argc > 0) {
1.120 ratchov 560: fputs(usagestr, stderr);
1.102 ratchov 561: exit(1);
1.15 ratchov 562: }
1.125 ratchov 563: if (wav_list) {
1.128 ratchov 564: if (opt_list || listen_list)
1.125 ratchov 565: errx(1, "-io not allowed in server mode");
566: if ((d = dev_list) && d->next)
567: errx(1, "only one device allowed in non-server mode");
568: if ((d->reqmode & MODE_THRU) && d->ctl_list == NULL) {
1.120 ratchov 569: if (!devctl_add(d, "default", MODE_MIDIMASK))
570: errx(1, "%s: can't open port", optarg);
571: d->reqmode |= MODE_MIDIMASK;
1.125 ratchov 572: }
573: } else {
574: if (dev_list == NULL)
575: mkdev(DEFAULT_DEV, 0, bufsz, round, hold, autovol);
576: for (d = dev_list; d != NULL; d = d->next) {
577: if (opt_byname("default", d->num))
578: continue;
579: mkopt("default", d, &rpar, &ppar, mode, vol, mmc, join);
1.92 ratchov 580: }
1.42 ratchov 581: }
1.128 ratchov 582: if (opt_list) {
1.61 ratchov 583: getbasepath(base, sizeof(base));
1.122 ratchov 584: snprintf(path, PATH_MAX, "%s/%s%u", base, AUCAT_PATH, unit);
1.120 ratchov 585: listen_new_un(path);
586: if (geteuid() == 0)
587: privdrop();
588: }
589: for (w = wav_list; w != NULL; w = w->next) {
590: if (!wav_init(w))
591: exit(1);
1.55 ratchov 592: }
1.120 ratchov 593: for (d = dev_list; d != NULL; d = d->next) {
594: if (!dev_init(d))
595: exit(1);
596: if (d->autostart && (d->mode & MODE_AUDIOMASK))
1.126 ratchov 597: dev_mmcstart(d);
1.56 ratchov 598: }
1.120 ratchov 599: for (l = listen_list; l != NULL; l = l->next) {
600: if (!listen_init(l))
601: exit(1);
1.98 ratchov 602: }
1.120 ratchov 603: if (background) {
1.107 ratchov 604: #ifdef DEBUG
1.98 ratchov 605: debug_level = 0;
606: dbg_flush();
1.107 ratchov 607: #endif
1.98 ratchov 608: if (daemon(0, 0) < 0)
1.56 ratchov 609: err(1, "daemon");
1.15 ratchov 610: }
1.13 uwe 611:
1.15 ratchov 612: /*
1.62 ratchov 613: * Loop, start audio.
1.15 ratchov 614: */
1.28 ratchov 615: for (;;) {
1.90 ratchov 616: if (quit_flag)
1.28 ratchov 617: break;
1.92 ratchov 618: active = 0;
619: for (d = dev_list; d != NULL; d = dnext) {
620: dnext = d->next;
621: if (!dev_run(d))
622: goto fatal;
1.127 ratchov 623: if (d->refcnt > 0)
1.92 ratchov 624: active = 1;
625: }
626: if (dev_list == NULL)
1.50 ratchov 627: break;
1.128 ratchov 628: if (!opt_list && !active)
1.83 ratchov 629: break;
1.50 ratchov 630: if (!file_poll())
631: break;
1.34 ratchov 632: }
1.92 ratchov 633: fatal:
1.120 ratchov 634: while (listen_list != NULL)
635: file_close(&listen_list->file);
1.110 ratchov 636:
1.90 ratchov 637: /*
638: * give a chance to drain
639: */
1.96 ratchov 640: for (d = dev_list; d != NULL; d = d->next)
641: dev_drain(d);
1.90 ratchov 642: while (file_poll())
643: ; /* nothing */
1.96 ratchov 644:
645: while (dev_list)
646: dev_del(dev_list);
1.83 ratchov 647: filelist_done();
1.128 ratchov 648: if (opt_list) {
1.86 ratchov 649: if (rmdir(base) < 0 && errno != ENOTEMPTY && errno != EPERM)
1.55 ratchov 650: warn("rmdir(\"%s\")", base);
651: }
1.61 ratchov 652: unsetsig();
653: return 0;
1.1 kstailey 654: }