sad

simple audio daemon
git clone git://git.2f30.org/sad.git
Log | Files | Refs | LICENSE

sad.c (3114B)


      1 #include <sys/select.h>
      2 #include <sys/socket.h>
      3 #include <sys/types.h>
      4 #include <sys/un.h>
      5 
      6 #include <err.h>
      7 #include <errno.h>
      8 #include <fcntl.h>
      9 #include <limits.h>
     10 #include <signal.h>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 #include <unistd.h>
     15 
     16 #include "arg.h"
     17 #include "sad.h"
     18 
     19 fd_set   master;
     20 fd_set   rfds;
     21 int      fdmax;
     22 char    *argv0;
     23 
     24 static int listenfd = -1;
     25 static volatile sig_atomic_t isrunning = 1;
     26 static char *socketpath = "/tmp/sad-sock";
     27 
     28 static int
     29 servlisten(const char *name)
     30 {
     31 	struct sockaddr_un sun;
     32 	int    fd, r;
     33 	socklen_t len;
     34 
     35 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
     36 	if (fd < 0)
     37 		err(1, "socket");
     38 
     39 	unlink(name);
     40 
     41 	memset(&sun, 0, sizeof(sun));
     42 	sun.sun_family = AF_UNIX;
     43 	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
     44 
     45 	len = sizeof(sun);
     46 	r = bind(fd, (struct sockaddr *)&sun, len);
     47 	if (r < 0)
     48 		err(1, "bind");
     49 
     50 	r = listen(fd, 5);
     51 	if (r < 0)
     52 		err(1, "listen");
     53 
     54 	return fd;
     55 }
     56 
     57 static int
     58 servaccept(int listenfd)
     59 {
     60 	struct sockaddr_un sun;
     61 	int    clifd;
     62 	socklen_t len;
     63 
     64 	len = sizeof(sun);
     65 	clifd = accept(listenfd, (struct sockaddr *)&sun, &len);
     66 	if (clifd < 0)
     67 		err(1, "accept");
     68 	return clifd;
     69 }
     70 
     71 static void
     72 playaudio(void)
     73 {
     74 	Song    *s;
     75 	unsigned char buf[4096];
     76 	int      nbytes;
     77 
     78 	s = getcursong();
     79 	if (!s)
     80 		return;
     81 
     82 	switch (s->state) {
     83 	case PREPARE:
     84 		if (s->decoder->open(&s->fmt, s->path) < 0) {
     85 			s->state = NONE;
     86 			return;
     87 		}
     88 		s->state = PLAYING;
     89 		break;
     90 	case PLAYING:
     91 		if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) {
     92 			playsong(picknextsong());
     93 			notify(EVSONGFINISHED);
     94 		} else {
     95 			playoutputs(&s->fmt, buf, nbytes);
     96 		}
     97 		break;
     98 	}
     99 }
    100 
    101 static void
    102 sighandler(int signum)
    103 {
    104 	if (signum == SIGINT || signum == SIGTERM)
    105 		isrunning = 0;
    106 }
    107 
    108 static void
    109 usage(void)
    110 {
    111 	fprintf(stderr, "usage: %s [-f sock]\n", argv0);
    112 	exit(1);
    113 }
    114 
    115 int
    116 main(int argc, char *argv[])
    117 {
    118 	struct sigaction sa;
    119 	struct timeval tv;
    120 	int    clifd, n, i;
    121 
    122 	ARGBEGIN {
    123 	case 'f':
    124 		socketpath = EARGF(usage());
    125 		break;
    126 	default:
    127 		usage();
    128 	} ARGEND;
    129 
    130 	memset(&sa, 0, sizeof(sa));
    131 	sa.sa_flags = SA_RESTART;
    132 	sa.sa_handler = sighandler;
    133 	sigaction(SIGTERM, &sa, NULL);
    134 	sigaction(SIGINT, &sa, NULL);
    135 
    136 	FD_ZERO(&master);
    137 	FD_ZERO(&rfds);
    138 
    139 	listenfd = servlisten(socketpath);
    140 	FD_SET(listenfd, &master);
    141 	fdmax = listenfd;
    142 
    143 	initoutputs();
    144 	openoutputs();
    145 	initnotifier();
    146 
    147 	playlistmode(REPEAT);
    148 
    149 	while (isrunning) {
    150 		rfds = master;
    151 		tv.tv_sec = 0;
    152 		tv.tv_usec = 10000;
    153 		n = select(fdmax + 1, &rfds, NULL, NULL, &tv);
    154 		if (n < 0) {
    155 			if (errno == EINTR)
    156 				goto fini;
    157 			err(1, "select");
    158 		}
    159 
    160 		playaudio();
    161 
    162 		for (i = 0; i <= fdmax; i++) {
    163 			if (!FD_ISSET(i, &rfds))
    164 				continue;
    165 			if (i == listenfd) {
    166 				clifd = servaccept(listenfd);
    167 				FD_SET(clifd, &master);
    168 				if (clifd > fdmax)
    169 					fdmax = clifd;
    170 			} else {
    171 				if (docmd(i) < 0) {
    172 					close(i);
    173 					FD_CLR(i, &master);
    174 					removesubscriber(i);
    175 				}
    176 			}
    177 		}
    178 	}
    179 fini:
    180 	for (i = 0; i <= fdmax; i++) {
    181 		if (FD_ISSET(i, &master)) {
    182 			close(i);
    183 			FD_CLR(i, &master);
    184 			removesubscriber(i);
    185 		}
    186 	}
    187 	if (listenfd != -1)
    188 		unlink(socketpath);
    189 	return 0;
    190 }