[BACK]Return to aucat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/aucat.c, Revision 1.139

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