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

Annotation of src/usr.bin/sndiod/sndiod.c, Revision 1.24

1.18      ratchov     1: /*     $OpenBSD$       */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      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/stat.h>
                     18: #include <sys/types.h>
                     19: #include <sys/resource.h>
1.18      ratchov    20: #include <sys/socket.h>
1.1       ratchov    21:
                     22: #include <err.h>
                     23: #include <errno.h>
                     24: #include <fcntl.h>
                     25: #include <grp.h>
                     26: #include <limits.h>
                     27: #include <pwd.h>
                     28: #include <signal.h>
                     29: #include <sndio.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #include "amsg.h"
                     36: #include "defs.h"
                     37: #include "dev.h"
1.18      ratchov    38: #include "fdpass.h"
1.1       ratchov    39: #include "file.h"
                     40: #include "listen.h"
                     41: #include "midi.h"
                     42: #include "opt.h"
                     43: #include "sock.h"
                     44: #include "utils.h"
                     45:
                     46: /*
                     47:  * unprivileged user name
                     48:  */
                     49: #ifndef SNDIO_USER
                     50: #define SNDIO_USER     "_sndio"
                     51: #endif
                     52:
                     53: /*
1.18      ratchov    54:  * privileged user name
                     55:  */
                     56: #ifndef SNDIO_PRIV_USER
                     57: #define SNDIO_PRIV_USER        "_sndiop"
                     58: #endif
                     59:
                     60: /*
1.1       ratchov    61:  * priority when run as root
                     62:  */
                     63: #ifndef SNDIO_PRIO
                     64: #define SNDIO_PRIO     (-20)
                     65: #endif
                     66:
                     67: /*
                     68:  * sample rate if no ``-r'' is used
                     69:  */
                     70: #ifndef DEFAULT_RATE
                     71: #define DEFAULT_RATE   48000
                     72: #endif
                     73:
                     74: /*
                     75:  * block size if neither ``-z'' nor ``-b'' is used
                     76:  */
                     77: #ifndef DEFAULT_ROUND
                     78: #define DEFAULT_ROUND  960
                     79: #endif
                     80:
                     81: /*
                     82:  * buffer size if neither ``-z'' nor ``-b'' is used
                     83:  */
                     84: #ifndef DEFAULT_BUFSZ
1.8       dcoppa     85: #define DEFAULT_BUFSZ  7680
1.1       ratchov    86: #endif
                     87:
                     88: /*
                     89:  * default device in server mode
                     90:  */
                     91: #ifndef DEFAULT_DEV
                     92: #define DEFAULT_DEV "rsnd/0"
                     93: #endif
1.5       ratchov    94:
                     95: void sigint(int);
                     96: void opt_ch(int *, int *);
                     97: void opt_enc(struct aparams *);
                     98: int opt_mmc(void);
                     99: int opt_onoff(void);
                    100: int getword(char *, char **);
                    101: unsigned int opt_mode(void);
                    102: void getbasepath(char *, size_t);
                    103: void setsig(void);
                    104: void unsetsig(void);
                    105: struct dev *mkdev(char *, struct aparams *,
                    106:     int, int, int, int, int, int);
                    107: struct opt *mkopt(char *, struct dev *,
                    108:     int, int, int, int, int, int, int, int);
1.1       ratchov   109:
                    110: unsigned int log_level = 0;
                    111: volatile sig_atomic_t quit_flag = 0;
                    112:
                    113: char usagestr[] = "usage: sndiod [-d] [-a flag] [-b nframes] "
                    114:     "[-C min:max] [-c min:max] [-e enc]\n\t"
                    115:     "[-f device] [-j flag] [-L addr] [-m mode] [-q port] [-r rate]\n\t"
                    116:     "[-s name] [-t mode] [-U unit] [-v volume] [-w flag] [-z nframes]\n";
                    117:
                    118: /*
                    119:  * SIGINT handler, it raises the quit flag. If the flag is already set,
                    120:  * that means that the last SIGINT was not handled, because the process
                    121:  * is blocked somewhere, so exit.
                    122:  */
                    123: void
                    124: sigint(int s)
                    125: {
                    126:        if (quit_flag)
                    127:                _exit(1);
                    128:        quit_flag = 1;
                    129: }
                    130:
                    131: void
                    132: opt_ch(int *rcmin, int *rcmax)
                    133: {
                    134:        char *next, *end;
                    135:        long cmin, cmax;
                    136:
                    137:        errno = 0;
                    138:        cmin = strtol(optarg, &next, 10);
                    139:        if (next == optarg || *next != ':')
                    140:                goto failed;
                    141:        cmax = strtol(++next, &end, 10);
                    142:        if (end == next || *end != '\0')
                    143:                goto failed;
                    144:        if (cmin < 0 || cmax < cmin || cmax >= NCHAN_MAX)
                    145:                goto failed;
                    146:        *rcmin = cmin;
                    147:        *rcmax = cmax;
                    148:        return;
                    149: failed:
                    150:        errx(1, "%s: bad channel range", optarg);
                    151: }
                    152:
                    153: void
                    154: opt_enc(struct aparams *par)
                    155: {
                    156:        int len;
                    157:
                    158:        len = aparams_strtoenc(par, optarg);
                    159:        if (len == 0 || optarg[len] != '\0')
                    160:                errx(1, "%s: bad encoding", optarg);
                    161: }
                    162:
                    163: int
                    164: opt_mmc(void)
                    165: {
                    166:        if (strcmp("off", optarg) == 0)
                    167:                return 0;
                    168:        if (strcmp("slave", optarg) == 0)
                    169:                return 1;
                    170:        errx(1, "%s: off/slave expected", optarg);
                    171: }
                    172:
                    173: int
                    174: opt_onoff(void)
                    175: {
                    176:        if (strcmp("off", optarg) == 0)
                    177:                return 0;
                    178:        if (strcmp("on", optarg) == 0)
                    179:                return 1;
                    180:        errx(1, "%s: on/off expected", optarg);
                    181: }
                    182:
1.4       ratchov   183: int
                    184: getword(char *word, char **str)
                    185: {
                    186:        char *p = *str;
                    187:
                    188:        for (;;) {
                    189:                if (*word == '\0')
                    190:                        break;
                    191:                if (*word++ != *p++)
                    192:                        return 0;
                    193:        }
                    194:        if (*p == ',' || *p == '\0') {
                    195:                *str = p;
                    196:                return 1;
                    197:        }
                    198:        return 0;
                    199: }
                    200:
1.1       ratchov   201: unsigned int
                    202: opt_mode(void)
                    203: {
                    204:        unsigned int mode = 0;
                    205:        char *p = optarg;
                    206:
1.4       ratchov   207:        for (;;) {
                    208:                if (getword("play", &p)) {
1.1       ratchov   209:                        mode |= MODE_PLAY;
1.4       ratchov   210:                } else if (getword("rec", &p)) {
1.1       ratchov   211:                        mode |= MODE_REC;
1.4       ratchov   212:                } else if (getword("mon", &p)) {
1.1       ratchov   213:                        mode |= MODE_MON;
1.4       ratchov   214:                } else if (getword("midi", &p)) {
1.1       ratchov   215:                        mode |= MODE_MIDIMASK;
1.4       ratchov   216:                } else
1.1       ratchov   217:                        errx(1, "%s: bad mode", optarg);
                    218:                if (*p == '\0')
                    219:                        break;
1.4       ratchov   220:                p++;
1.1       ratchov   221:        }
                    222:        if (mode == 0)
                    223:                errx(1, "empty mode");
                    224:        return mode;
                    225: }
                    226:
                    227: void
                    228: setsig(void)
                    229: {
                    230:        struct sigaction sa;
                    231:
                    232:        quit_flag = 0;
                    233:        sigfillset(&sa.sa_mask);
                    234:        sa.sa_flags = SA_RESTART;
                    235:        sa.sa_handler = sigint;
                    236:        if (sigaction(SIGINT, &sa, NULL) < 0)
                    237:                err(1, "sigaction(int) failed");
                    238:        if (sigaction(SIGTERM, &sa, NULL) < 0)
                    239:                err(1, "sigaction(term) failed");
                    240:        if (sigaction(SIGHUP, &sa, NULL) < 0)
                    241:                err(1, "sigaction(hup) failed");
                    242: }
                    243:
                    244: void
                    245: unsetsig(void)
                    246: {
                    247:        struct sigaction sa;
                    248:
                    249:        sigfillset(&sa.sa_mask);
                    250:        sa.sa_flags = SA_RESTART;
                    251:        sa.sa_handler = SIG_DFL;
                    252:        if (sigaction(SIGHUP, &sa, NULL) < 0)
                    253:                err(1, "unsetsig(hup): sigaction failed\n");
                    254:        if (sigaction(SIGTERM, &sa, NULL) < 0)
                    255:                err(1, "unsetsig(term): sigaction failed\n");
                    256:        if (sigaction(SIGINT, &sa, NULL) < 0)
                    257:                err(1, "unsetsig(int): sigaction failed\n");
                    258: }
                    259:
                    260: void
                    261: getbasepath(char *base, size_t size)
                    262: {
                    263:        uid_t uid;
                    264:        struct stat sb;
1.14      ratchov   265:        mode_t mask, omask;
1.1       ratchov   266:
                    267:        uid = geteuid();
                    268:        if (uid == 0) {
                    269:                mask = 022;
1.10      ratchov   270:                snprintf(base, SOCKPATH_MAX, SOCKPATH_DIR);
1.1       ratchov   271:        } else {
                    272:                mask = 077;
1.10      ratchov   273:                snprintf(base, SOCKPATH_MAX, SOCKPATH_DIR "-%u", uid);
1.1       ratchov   274:        }
1.14      ratchov   275:        omask = umask(mask);
                    276:        if (mkdir(base, 0777) < 0) {
1.1       ratchov   277:                if (errno != EEXIST)
                    278:                        err(1, "mkdir(\"%s\")", base);
                    279:        }
1.14      ratchov   280:        umask(omask);
1.1       ratchov   281:        if (stat(base, &sb) < 0)
                    282:                err(1, "stat(\"%s\")", base);
                    283:        if (sb.st_uid != uid || (sb.st_mode & mask) != 0)
                    284:                errx(1, "%s has wrong permissions", base);
                    285: }
                    286:
                    287: struct dev *
                    288: mkdev(char *path, struct aparams *par,
                    289:     int mode, int bufsz, int round, int rate, int hold, int autovol)
                    290: {
                    291:        struct dev *d;
                    292:
                    293:        for (d = dev_list; d != NULL; d = d->next) {
                    294:                if (strcmp(d->path, path) == 0)
                    295:                        return d;
                    296:        }
                    297:        if (!bufsz && !round) {
                    298:                round = DEFAULT_ROUND;
                    299:                bufsz = DEFAULT_BUFSZ;
                    300:        } else if (!bufsz) {
                    301:                bufsz = round * 2;
                    302:        } else if (!round)
                    303:                round = bufsz / 2;
                    304:        d = dev_new(path, par, mode, bufsz, round, rate, hold, autovol);
                    305:        if (d == NULL)
                    306:                exit(1);
                    307:        return d;
                    308: }
                    309:
                    310: struct opt *
                    311: mkopt(char *path, struct dev *d,
                    312:     int pmin, int pmax, int rmin, int rmax,
                    313:     int mode, int vol, int mmc, int dup)
                    314: {
                    315:        struct opt *o;
                    316:
                    317:        o = opt_new(path, d, pmin, pmax, rmin, rmax,
                    318:            MIDI_TO_ADATA(vol), mmc, dup, mode);
                    319:        if (o == NULL)
                    320:                errx(1, "%s: couldn't create subdev", path);
                    321:        dev_adjpar(d, o->mode, o->pmin, o->pmax, o->rmin, o->rmax);
                    322:        return o;
                    323: }
                    324:
                    325: int
                    326: main(int argc, char **argv)
                    327: {
                    328:        int c, background, unit;
                    329:        int pmin, pmax, rmin, rmax;
1.13      ratchov   330:        char base[SOCKPATH_MAX], path[SOCKPATH_MAX], *tcpaddr;
1.1       ratchov   331:        unsigned int mode, dup, mmc, vol;
                    332:        unsigned int hold, autovol, bufsz, round, rate;
                    333:        const char *str;
                    334:        struct aparams par;
                    335:        struct dev *d;
                    336:        struct port *p;
                    337:        struct listen *l;
1.17      ratchov   338:        struct passwd *pw;
1.18      ratchov   339:        int s[2];
                    340:        pid_t pid;
1.20      ratchov   341:        uid_t euid, hpw_uid, wpw_uid;
                    342:        gid_t hpw_gid, wpw_gid;
                    343:        char *wpw_dir;
1.1       ratchov   344:
                    345:        atexit(log_flush);
                    346:
                    347:        /*
                    348:         * global options defaults
                    349:         */
                    350:        vol = 118;
                    351:        dup = 1;
                    352:        mmc = 0;
                    353:        hold = 0;
                    354:        autovol = 1;
                    355:        bufsz = 0;
                    356:        round = 0;
                    357:        rate = DEFAULT_RATE;
                    358:        unit = 0;
                    359:        background = 1;
                    360:        pmin = 0;
                    361:        pmax = 1;
                    362:        rmin = 0;
                    363:        rmax = 1;
                    364:        aparams_init(&par);
                    365:        mode = MODE_PLAY | MODE_REC;
1.13      ratchov   366:        tcpaddr = NULL;
1.1       ratchov   367:
1.15      ratchov   368:        while ((c = getopt(argc, argv, "a:b:c:C:de:f:j:L:m:q:r:s:t:U:v:w:x:z:")) != -1) {
1.1       ratchov   369:                switch (c) {
                    370:                case 'd':
                    371:                        log_level++;
                    372:                        background = 0;
                    373:                        break;
                    374:                case 'U':
                    375:                        unit = strtonum(optarg, 0, 15, &str);
                    376:                        if (str)
                    377:                                errx(1, "%s: unit number is %s", optarg, str);
                    378:                        break;
                    379:                case 'L':
1.13      ratchov   380:                        tcpaddr = optarg;
1.1       ratchov   381:                        break;
                    382:                case 'm':
                    383:                        mode = opt_mode();
                    384:                        break;
                    385:                case 'j':
                    386:                        dup = opt_onoff();
                    387:                        break;
                    388:                case 't':
                    389:                        mmc = opt_mmc();
                    390:                        break;
                    391:                case 'c':
                    392:                        opt_ch(&pmin, &pmax);
                    393:                        break;
                    394:                case 'C':
                    395:                        opt_ch(&rmin, &rmax);
                    396:                        break;
                    397:                case 'e':
                    398:                        opt_enc(&par);
                    399:                        break;
                    400:                case 'r':
                    401:                        rate = strtonum(optarg, RATE_MIN, RATE_MAX, &str);
                    402:                        if (str)
                    403:                                errx(1, "%s: rate is %s", optarg, str);
                    404:                        break;
                    405:                case 'v':
                    406:                        vol = strtonum(optarg, 0, MIDI_MAXCTL, &str);
                    407:                        if (str)
                    408:                                errx(1, "%s: volume is %s", optarg, str);
                    409:                        break;
                    410:                case 's':
                    411:                        if ((d = dev_list) == NULL) {
                    412:                                d = mkdev(DEFAULT_DEV, &par, 0, bufsz, round, rate,
                    413:                                    hold, autovol);
                    414:                        }
                    415:                        mkopt(optarg, d, pmin, pmax, rmin, rmax,
                    416:                            mode, vol, mmc, dup);
                    417:                        break;
                    418:                case 'q':
1.3       ratchov   419:                        p = port_new(optarg, MODE_MIDIMASK, hold);
1.1       ratchov   420:                        if (!p)
                    421:                                errx(1, "%s: can't open port", optarg);
                    422:                        break;
                    423:                case 'a':
                    424:                        hold = opt_onoff();
                    425:                        break;
                    426:                case 'w':
                    427:                        autovol = opt_onoff();
                    428:                        break;
                    429:                case 'b':
                    430:                        bufsz = strtonum(optarg, 1, RATE_MAX, &str);
                    431:                        if (str)
                    432:                                errx(1, "%s: buffer size is %s", optarg, str);
                    433:                        break;
                    434:                case 'z':
                    435:                        round = strtonum(optarg, 1, SHRT_MAX, &str);
                    436:                        if (str)
                    437:                                errx(1, "%s: block size is %s", optarg, str);
                    438:                        break;
                    439:                case 'f':
                    440:                        mkdev(optarg, &par, 0, bufsz, round, rate, hold, autovol);
                    441:                        break;
                    442:                default:
                    443:                        fputs(usagestr, stderr);
                    444:                        return 1;
                    445:                }
                    446:        }
                    447:        argc -= optind;
                    448:        argv += optind;
                    449:        if (argc > 0) {
                    450:                fputs(usagestr, stderr);
                    451:                return 1;
                    452:        }
                    453:        if (dev_list == NULL)
                    454:                mkdev(DEFAULT_DEV, &par, 0, bufsz, round, rate, hold, autovol);
                    455:        for (d = dev_list; d != NULL; d = d->next) {
                    456:                if (opt_byname("default", d->num))
                    457:                        continue;
                    458:                mkopt("default", d, pmin, pmax, rmin, rmax,
                    459:                    mode, vol, mmc, dup);
                    460:        }
1.17      ratchov   461:
                    462:        setsig();
                    463:        filelist_init();
                    464:
1.20      ratchov   465:        euid = geteuid();
                    466:        if (euid == 0) {
                    467:                if ((pw = getpwnam(SNDIO_PRIV_USER)) == NULL)
                    468:                        errx(1, "unknown user %s", SNDIO_PRIV_USER);
                    469:                hpw_uid = pw->pw_uid;
                    470:                hpw_gid = pw->pw_gid;
                    471:                if ((pw = getpwnam(SNDIO_USER)) == NULL)
                    472:                        errx(1, "unknown user %s", SNDIO_USER);
                    473:                wpw_uid = pw->pw_uid;
                    474:                wpw_gid = pw->pw_gid;
                    475:                wpw_dir = xstrdup(pw->pw_dir);
                    476:        } else {
1.21      ratchov   477:                hpw_uid = wpw_uid = hpw_gid = wpw_gid = 0xdeadbeef;
                    478:                wpw_dir = NULL;
1.20      ratchov   479:        }
                    480:
1.18      ratchov   481:        /* start subprocesses */
                    482:
                    483:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) < 0) {
                    484:                perror("socketpair");
                    485:                return 1;
1.13      ratchov   486:        }
1.18      ratchov   487:        pid = fork();
                    488:        if (pid == -1) {
                    489:                log_puts("can't fork\n");
                    490:                return 1;
1.1       ratchov   491:        }
1.18      ratchov   492:        if (pid == 0) {
                    493:                setproctitle("helper");
                    494:                close(s[0]);
                    495:                if (fdpass_new(s[1], &helper_fileops) == NULL)
1.1       ratchov   496:                        return 1;
1.18      ratchov   497:                if (background) {
                    498:                        log_flush();
                    499:                        log_level = 0;
                    500:                        if (daemon(0, 0) < 0)
                    501:                                err(1, "daemon");
                    502:                }
1.20      ratchov   503:                if (euid == 0) {
                    504:                        if (setgroups(1, &hpw_gid) ||
                    505:                            setresgid(hpw_gid, hpw_gid, hpw_gid) ||
                    506:                            setresuid(hpw_uid, hpw_uid, hpw_uid))
1.18      ratchov   507:                                err(1, "cannot drop privileges");
                    508:                }
1.22      ratchov   509:                if (pledge("stdio sendfd rpath wpath", NULL) < 0)
                    510:                        err(1, "pledge");
1.18      ratchov   511:                while (file_poll())
                    512:                        ; /* nothing */
                    513:        } else {
                    514:                close(s[1]);
                    515:                if (fdpass_new(s[0], &worker_fileops) == NULL)
1.1       ratchov   516:                        return 1;
1.18      ratchov   517:
                    518:                getbasepath(base, sizeof(base));
                    519:                snprintf(path,
                    520:                    SOCKPATH_MAX, "%s/" SOCKPATH_FILE "%u",
                    521:                    base, unit);
1.23      ratchov   522:                if (!listen_new_un(path))
                    523:                        return 1;
                    524:                if (tcpaddr) {
                    525:                        if (!listen_new_tcp(tcpaddr, AUCAT_PORT + unit))
                    526:                                return 1;
                    527:                }
1.18      ratchov   528:                for (l = listen_list; l != NULL; l = l->next) {
                    529:                        if (!listen_init(l))
                    530:                                return 1;
                    531:                }
                    532:
                    533:                midi_init();
                    534:                for (p = port_list; p != NULL; p = p->next) {
                    535:                        if (!port_init(p))
                    536:                                return 1;
                    537:                }
                    538:                for (d = dev_list; d != NULL; d = d->next) {
                    539:                        if (!dev_init(d))
                    540:                                return 1;
                    541:                }
                    542:                if (background) {
                    543:                        log_flush();
                    544:                        log_level = 0;
                    545:                        if (daemon(0, 0) < 0)
                    546:                                err(1, "daemon");
                    547:                }
1.20      ratchov   548:                if (euid == 0) {
1.18      ratchov   549:                        if (setpriority(PRIO_PROCESS, 0, SNDIO_PRIO) < 0)
                    550:                                err(1, "setpriority");
1.20      ratchov   551:                        if (chroot(wpw_dir) != 0 || chdir("/") != 0)
                    552:                                err(1, "cannot chroot to %s", wpw_dir);
                    553:                        if (setgroups(1, &wpw_gid) ||
                    554:                            setresgid(wpw_gid, wpw_gid, wpw_gid) ||
                    555:                            setresuid(wpw_uid, wpw_uid, wpw_uid))
1.18      ratchov   556:                                err(1, "cannot drop privileges");
1.22      ratchov   557:                }
                    558:                if (tcpaddr) {
                    559:                        if (pledge("stdio audio recvfd unix inet", NULL) == -1)
                    560:                                err(1, "pledge");
                    561:                } else {
                    562:                        if (pledge("stdio audio recvfd unix", NULL) == -1)
                    563:                                err(1, "pledge");
1.18      ratchov   564:                }
                    565:                for (;;) {
                    566:                        if (quit_flag)
                    567:                                break;
                    568:                        if (!fdpass_peer)
                    569:                                break;
                    570:                        if (!file_poll())
                    571:                                break;
                    572:                }
                    573:                if (fdpass_peer)
                    574:                        fdpass_close(fdpass_peer);
                    575:                while (listen_list != NULL)
                    576:                        listen_close(listen_list);
                    577:                while (sock_list != NULL)
                    578:                        sock_close(sock_list);
                    579:                for (d = dev_list; d != NULL; d = d->next)
                    580:                        dev_done(d);
                    581:                for (p = port_list; p != NULL; p = p->next)
                    582:                        port_done(p);
                    583:                while (file_poll())
                    584:                        ; /* nothing */
                    585:                midi_done();
1.1       ratchov   586:        }
                    587:        while (opt_list != NULL)
                    588:                opt_del(opt_list);
                    589:        while (dev_list)
                    590:                dev_del(dev_list);
                    591:        while (port_list)
                    592:                port_del(port_list);
                    593:        filelist_done();
                    594:        unsetsig();
                    595:        return 0;
                    596: }