Annotation of src/usr.bin/sndiod/sndiod.c, Revision 1.9
1.9 ! ratchov 1: /* $OpenBSD: sndiod.c,v 1.8 2015/04/26 17:26:59 dcoppa Exp $ */
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/queue.h>
18: #include <sys/stat.h>
19: #include <sys/types.h>
20: #include <sys/resource.h>
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"
38: #include "file.h"
39: #include "listen.h"
40: #include "midi.h"
41: #include "opt.h"
42: #include "sock.h"
43: #include "utils.h"
44:
45: /*
46: * unprivileged user name
47: */
48: #ifndef SNDIO_USER
49: #define SNDIO_USER "_sndio"
50: #endif
51:
52: /*
53: * priority when run as root
54: */
55: #ifndef SNDIO_PRIO
56: #define SNDIO_PRIO (-20)
57: #endif
58:
59: /*
60: * sample rate if no ``-r'' is used
61: */
62: #ifndef DEFAULT_RATE
63: #define DEFAULT_RATE 48000
64: #endif
65:
66: /*
67: * block size if neither ``-z'' nor ``-b'' is used
68: */
69: #ifndef DEFAULT_ROUND
70: #define DEFAULT_ROUND 960
71: #endif
72:
73: /*
74: * buffer size if neither ``-z'' nor ``-b'' is used
75: */
76: #ifndef DEFAULT_BUFSZ
1.8 dcoppa 77: #define DEFAULT_BUFSZ 7680
1.1 ratchov 78: #endif
79:
80: /*
81: * default device in server mode
82: */
83: #ifndef DEFAULT_DEV
84: #define DEFAULT_DEV "rsnd/0"
85: #endif
1.5 ratchov 86:
87: void sigint(int);
88: void opt_ch(int *, int *);
89: void opt_enc(struct aparams *);
90: int opt_mmc(void);
91: int opt_onoff(void);
92: int getword(char *, char **);
93: unsigned int opt_mode(void);
94: void getbasepath(char *, size_t);
95: void setsig(void);
96: void unsetsig(void);
97: void privdrop(void);
98: struct dev *mkdev(char *, struct aparams *,
99: int, int, int, int, int, int);
100: struct opt *mkopt(char *, struct dev *,
101: int, int, int, int, int, int, int, int);
1.1 ratchov 102:
103: unsigned int log_level = 0;
104: volatile sig_atomic_t quit_flag = 0;
105:
106: char usagestr[] = "usage: sndiod [-d] [-a flag] [-b nframes] "
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] [-z nframes]\n";
110:
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
114: * is blocked somewhere, so exit.
115: */
116: void
117: sigint(int s)
118: {
119: if (quit_flag)
120: _exit(1);
121: quit_flag = 1;
122: }
123:
124: void
125: opt_ch(int *rcmin, int *rcmax)
126: {
127: char *next, *end;
128: long cmin, cmax;
129:
130: errno = 0;
131: cmin = strtol(optarg, &next, 10);
132: if (next == optarg || *next != ':')
133: goto failed;
134: cmax = strtol(++next, &end, 10);
135: if (end == next || *end != '\0')
136: goto failed;
137: if (cmin < 0 || cmax < cmin || cmax >= NCHAN_MAX)
138: goto failed;
139: *rcmin = cmin;
140: *rcmax = cmax;
141: return;
142: failed:
143: errx(1, "%s: bad channel range", optarg);
144: }
145:
146: void
147: opt_enc(struct aparams *par)
148: {
149: int len;
150:
151: len = aparams_strtoenc(par, optarg);
152: if (len == 0 || optarg[len] != '\0')
153: errx(1, "%s: bad encoding", optarg);
154: }
155:
156: int
157: opt_mmc(void)
158: {
159: if (strcmp("off", optarg) == 0)
160: return 0;
161: if (strcmp("slave", optarg) == 0)
162: return 1;
163: errx(1, "%s: off/slave expected", optarg);
164: }
165:
166: int
167: opt_onoff(void)
168: {
169: if (strcmp("off", optarg) == 0)
170: return 0;
171: if (strcmp("on", optarg) == 0)
172: return 1;
173: errx(1, "%s: on/off expected", optarg);
174: }
175:
1.4 ratchov 176: int
177: getword(char *word, char **str)
178: {
179: char *p = *str;
180:
181: for (;;) {
182: if (*word == '\0')
183: break;
184: if (*word++ != *p++)
185: return 0;
186: }
187: if (*p == ',' || *p == '\0') {
188: *str = p;
189: return 1;
190: }
191: return 0;
192: }
193:
1.1 ratchov 194: unsigned int
195: opt_mode(void)
196: {
197: unsigned int mode = 0;
198: char *p = optarg;
199:
1.4 ratchov 200: for (;;) {
201: if (getword("play", &p)) {
1.1 ratchov 202: mode |= MODE_PLAY;
1.4 ratchov 203: } else if (getword("rec", &p)) {
1.1 ratchov 204: mode |= MODE_REC;
1.4 ratchov 205: } else if (getword("mon", &p)) {
1.1 ratchov 206: mode |= MODE_MON;
1.4 ratchov 207: } else if (getword("midi", &p)) {
1.1 ratchov 208: mode |= MODE_MIDIMASK;
1.4 ratchov 209: } else
1.1 ratchov 210: errx(1, "%s: bad mode", optarg);
211: if (*p == '\0')
212: break;
1.4 ratchov 213: p++;
1.1 ratchov 214: }
215: if (mode == 0)
216: errx(1, "empty mode");
217: return mode;
218: }
219:
220: void
221: setsig(void)
222: {
223: struct sigaction sa;
224:
225: quit_flag = 0;
226: sigfillset(&sa.sa_mask);
227: sa.sa_flags = SA_RESTART;
228: sa.sa_handler = sigint;
229: if (sigaction(SIGINT, &sa, NULL) < 0)
230: err(1, "sigaction(int) failed");
231: if (sigaction(SIGTERM, &sa, NULL) < 0)
232: err(1, "sigaction(term) failed");
233: if (sigaction(SIGHUP, &sa, NULL) < 0)
234: err(1, "sigaction(hup) failed");
235: }
236:
237: void
238: unsetsig(void)
239: {
240: struct sigaction sa;
241:
242: sigfillset(&sa.sa_mask);
243: sa.sa_flags = SA_RESTART;
244: sa.sa_handler = SIG_DFL;
245: if (sigaction(SIGHUP, &sa, NULL) < 0)
246: err(1, "unsetsig(hup): sigaction failed\n");
247: if (sigaction(SIGTERM, &sa, NULL) < 0)
248: err(1, "unsetsig(term): sigaction failed\n");
249: if (sigaction(SIGINT, &sa, NULL) < 0)
250: err(1, "unsetsig(int): sigaction failed\n");
251: }
252:
253: void
254: getbasepath(char *base, size_t size)
255: {
256: uid_t uid;
257: struct stat sb;
258: mode_t mask;
259:
260: uid = geteuid();
261: if (uid == 0) {
262: mask = 022;
1.9 ! ratchov 263: snprintf(base, PATH_MAX, SOCKPATH_DIR);
1.1 ratchov 264: } else {
265: mask = 077;
1.9 ! ratchov 266: snprintf(base, PATH_MAX, SOCKPATH_DIR "-%u", uid);
1.1 ratchov 267: }
268: if (mkdir(base, 0777 & ~mask) < 0) {
269: if (errno != EEXIST)
270: err(1, "mkdir(\"%s\")", base);
271: }
272: if (stat(base, &sb) < 0)
273: err(1, "stat(\"%s\")", base);
274: if (sb.st_uid != uid || (sb.st_mode & mask) != 0)
275: errx(1, "%s has wrong permissions", base);
276: }
277:
278: void
279: privdrop(void)
280: {
281: struct passwd *pw;
282:
283: if ((pw = getpwnam(SNDIO_USER)) == NULL)
284: errx(1, "unknown user %s", SNDIO_USER);
285: if (setpriority(PRIO_PROCESS, 0, SNDIO_PRIO) < 0)
286: err(1, "setpriority");
287: if (setgroups(1, &pw->pw_gid) ||
288: setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
289: setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
290: err(1, "cannot drop privileges");
291: }
292:
293: struct dev *
294: mkdev(char *path, struct aparams *par,
295: int mode, int bufsz, int round, int rate, int hold, int autovol)
296: {
297: struct dev *d;
298:
299: for (d = dev_list; d != NULL; d = d->next) {
300: if (strcmp(d->path, path) == 0)
301: return d;
302: }
303: if (!bufsz && !round) {
304: round = DEFAULT_ROUND;
305: bufsz = DEFAULT_BUFSZ;
306: } else if (!bufsz) {
307: bufsz = round * 2;
308: } else if (!round)
309: round = bufsz / 2;
310: d = dev_new(path, par, mode, bufsz, round, rate, hold, autovol);
311: if (d == NULL)
312: exit(1);
313: return d;
314: }
315:
316: struct opt *
317: mkopt(char *path, struct dev *d,
318: int pmin, int pmax, int rmin, int rmax,
319: int mode, int vol, int mmc, int dup)
320: {
321: struct opt *o;
322:
323: o = opt_new(path, d, pmin, pmax, rmin, rmax,
324: MIDI_TO_ADATA(vol), mmc, dup, mode);
325: if (o == NULL)
326: errx(1, "%s: couldn't create subdev", path);
327: dev_adjpar(d, o->mode, o->pmin, o->pmax, o->rmin, o->rmax);
328: return o;
329: }
330:
331: int
332: main(int argc, char **argv)
333: {
334: int c, background, unit;
335: int pmin, pmax, rmin, rmax;
336: char base[PATH_MAX], path[PATH_MAX];
337: unsigned int mode, dup, mmc, vol;
338: unsigned int hold, autovol, bufsz, round, rate;
339: const char *str;
340: struct aparams par;
341: struct dev *d;
342: struct port *p;
343: struct listen *l;
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;
366:
367: setsig();
368: filelist_init();
369:
1.2 ratchov 370: while ((c = getopt(argc, argv, "a:b:c:C:de:f:j:L:m:Mq:r:s:t:U:v:w:x:z:")) != -1) {
1.1 ratchov 371: switch (c) {
372: case 'd':
373: log_level++;
374: background = 0;
375: break;
376: case 'U':
377: if (listen_list)
378: errx(1, "-U must come before -L");
379: unit = strtonum(optarg, 0, 15, &str);
380: if (str)
381: errx(1, "%s: unit number is %s", optarg, str);
382: break;
383: case 'L':
384: listen_new_tcp(optarg, AUCAT_PORT + unit);
385: break;
386: case 'm':
387: mode = opt_mode();
388: break;
389: case 'j':
390: dup = opt_onoff();
391: break;
392: case 't':
393: mmc = opt_mmc();
394: break;
395: case 'c':
396: opt_ch(&pmin, &pmax);
397: break;
398: case 'C':
399: opt_ch(&rmin, &rmax);
400: break;
401: case 'e':
402: opt_enc(&par);
403: break;
404: case 'r':
405: rate = strtonum(optarg, RATE_MIN, RATE_MAX, &str);
406: if (str)
407: errx(1, "%s: rate is %s", optarg, str);
408: break;
409: case 'v':
410: vol = strtonum(optarg, 0, MIDI_MAXCTL, &str);
411: if (str)
412: errx(1, "%s: volume is %s", optarg, str);
413: break;
414: case 's':
415: if ((d = dev_list) == NULL) {
416: d = mkdev(DEFAULT_DEV, &par, 0, bufsz, round, rate,
417: hold, autovol);
418: }
419: mkopt(optarg, d, pmin, pmax, rmin, rmax,
420: mode, vol, mmc, dup);
421: break;
422: case 'q':
1.3 ratchov 423: p = port_new(optarg, MODE_MIDIMASK, hold);
1.1 ratchov 424: if (!p)
425: errx(1, "%s: can't open port", optarg);
426: break;
427: case 'a':
428: hold = opt_onoff();
429: break;
430: case 'w':
431: autovol = opt_onoff();
432: break;
433: case 'b':
434: bufsz = strtonum(optarg, 1, RATE_MAX, &str);
435: if (str)
436: errx(1, "%s: buffer size is %s", optarg, str);
437: break;
438: case 'z':
439: round = strtonum(optarg, 1, SHRT_MAX, &str);
440: if (str)
441: errx(1, "%s: block size is %s", optarg, str);
442: break;
443: case 'f':
444: mkdev(optarg, &par, 0, bufsz, round, rate, hold, autovol);
1.2 ratchov 445: break;
446: case 'M':
1.4 ratchov 447: /* XXX: for compatibility with aucat, remove this */
1.1 ratchov 448: break;
449: default:
450: fputs(usagestr, stderr);
451: return 1;
452: }
453: }
454: argc -= optind;
455: argv += optind;
456: if (argc > 0) {
457: fputs(usagestr, stderr);
458: return 1;
459: }
460: if (dev_list == NULL)
461: mkdev(DEFAULT_DEV, &par, 0, bufsz, round, rate, hold, autovol);
462: for (d = dev_list; d != NULL; d = d->next) {
463: if (opt_byname("default", d->num))
464: continue;
465: mkopt("default", d, pmin, pmax, rmin, rmax,
466: mode, vol, mmc, dup);
467: }
468: getbasepath(base, sizeof(base));
1.9 ! ratchov 469: snprintf(path, PATH_MAX, "%s/%s%u", base, SOCKPATH_FILE, unit);
1.1 ratchov 470: listen_new_un(path);
471: if (geteuid() == 0)
472: privdrop();
473: midi_init();
474: for (p = port_list; p != NULL; p = p->next) {
475: if (!port_init(p))
476: return 1;
477: }
478: for (d = dev_list; d != NULL; d = d->next) {
479: if (!dev_init(d))
480: return 1;
481: }
482: for (l = listen_list; l != NULL; l = l->next) {
483: if (!listen_init(l))
484: return 1;
485: }
486: if (background) {
487: log_flush();
488: log_level = 0;
489: if (daemon(0, 0) < 0)
490: err(1, "daemon");
491: }
492:
493: /*
494: * Loop, start audio.
495: */
496: for (;;) {
497: if (quit_flag)
498: break;
499: if (!file_poll())
500: break;
501: }
502: while (listen_list != NULL)
503: listen_close(listen_list);
504: while (sock_list != NULL)
505: sock_close(sock_list);
506: while (opt_list != NULL)
507: opt_del(opt_list);
508: for (d = dev_list; d != NULL; d = d->next)
509: dev_done(d);
510: for (p = port_list; p != NULL; p = p->next)
511: port_done(p);
512: midi_done();
513: while (file_poll())
514: ; /* nothing */
515: while (dev_list)
516: dev_del(dev_list);
517: while (port_list)
518: port_del(port_list);
519: filelist_done();
520: rmdir(base);
521: unsetsig();
522: return 0;
523: }