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