Blob


1 /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 #include <sys/un.h>
25 #include <netinet/in.h>
26 #include <net/if.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <imsg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include "amused.h"
36 #include "log.h"
37 #include "control.h"
38 #include "playlist.h"
40 #define CONTROL_BACKLOG 5
42 struct {
43 struct event ev;
44 struct event evt;
45 int fd;
46 } control_state = {.fd = -1};
48 struct ctl_conn {
49 TAILQ_ENTRY(ctl_conn) entry;
50 struct imsgev iev;
51 };
53 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
55 struct ctl_conn *control_connbyfd(int);
56 struct ctl_conn *control_connbypid(pid_t);
57 void control_close(int);
59 int
60 control_init(char *path)
61 {
62 struct sockaddr_un sun;
63 int fd;
64 mode_t old_umask;
66 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
67 0)) == -1) {
68 log_warn("%s: socket", __func__);
69 return (-1);
70 }
72 memset(&sun, 0, sizeof(sun));
73 sun.sun_family = AF_UNIX;
74 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
76 if (unlink(path) == -1)
77 if (errno != ENOENT) {
78 log_warn("%s: unlink %s", __func__, path);
79 close(fd);
80 return (-1);
81 }
83 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
84 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
85 log_warn("%s: bind: %s", __func__, path);
86 close(fd);
87 umask(old_umask);
88 return (-1);
89 }
90 umask(old_umask);
92 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
93 log_warn("%s: chmod", __func__);
94 close(fd);
95 (void)unlink(path);
96 return (-1);
97 }
99 return (fd);
102 int
103 control_listen(int fd)
105 if (control_state.fd != -1)
106 fatalx("%s: received unexpected controlsock", __func__);
108 control_state.fd = fd;
109 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
110 log_warn("%s: listen", __func__);
111 return (-1);
114 event_set(&control_state.ev, control_state.fd, EV_READ,
115 control_accept, NULL);
116 event_add(&control_state.ev, NULL);
117 evtimer_set(&control_state.evt, control_accept, NULL);
119 return (0);
122 void
123 control_accept(int listenfd, short event, void *bula)
125 int connfd;
126 socklen_t len;
127 struct sockaddr_un sun;
128 struct ctl_conn *c;
130 event_add(&control_state.ev, NULL);
131 if ((event & EV_TIMEOUT))
132 return;
134 len = sizeof(sun);
135 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
136 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
137 /*
138 * Pause accept if we are out of file descriptors, or
139 * libevent will haunt us here too.
140 */
141 if (errno == ENFILE || errno == EMFILE) {
142 struct timeval evtpause = { 1, 0 };
144 event_del(&control_state.ev);
145 evtimer_add(&control_state.evt, &evtpause);
146 } else if (errno != EWOULDBLOCK && errno != EINTR &&
147 errno != ECONNABORTED)
148 log_warn("%s: accept4", __func__);
149 return;
152 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
153 log_warn("%s: calloc", __func__);
154 close(connfd);
155 return;
158 imsg_init(&c->iev.ibuf, connfd);
159 c->iev.handler = control_dispatch_imsg;
160 c->iev.events = EV_READ;
161 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
162 c->iev.handler, &c->iev);
163 event_add(&c->iev.ev, NULL);
165 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
168 struct ctl_conn *
169 control_connbyfd(int fd)
171 struct ctl_conn *c;
173 TAILQ_FOREACH(c, &ctl_conns, entry) {
174 if (c->iev.ibuf.fd == fd)
175 break;
178 return (c);
181 struct ctl_conn *
182 control_connbypid(pid_t pid)
184 struct ctl_conn *c;
186 TAILQ_FOREACH(c, &ctl_conns, entry) {
187 if (c->iev.ibuf.pid == pid)
188 break;
191 return (c);
194 void
195 control_close(int fd)
197 struct ctl_conn *c;
199 if ((c = control_connbyfd(fd)) == NULL) {
200 log_warnx("%s: fd %d: not found", __func__, fd);
201 return;
204 msgbuf_clear(&c->iev.ibuf.w);
205 TAILQ_REMOVE(&ctl_conns, c, entry);
207 event_del(&c->iev.ev);
208 close(c->iev.ibuf.fd);
210 /* Some file descriptors are available again. */
211 if (evtimer_pending(&control_state.evt, NULL)) {
212 evtimer_del(&control_state.evt);
213 event_add(&control_state.ev, NULL);
216 free(c);
219 void
220 control_dispatch_imsg(int fd, short event, void *bula)
222 struct ctl_conn *c;
223 struct imsg imsg;
224 ssize_t n;
225 const char *song;
227 if ((c = control_connbyfd(fd)) == NULL) {
228 log_warnx("%s: fd %d: not found", __func__, fd);
229 return;
232 if (event & EV_READ) {
233 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
234 n == 0) {
235 control_close(fd);
236 return;
239 if (event & EV_WRITE) {
240 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
241 control_close(fd);
242 return;
246 for (;;) {
247 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
248 control_close(fd);
249 return;
251 if (n == 0)
252 break;
254 switch (imsg.hdr.type) {
255 case IMSG_CTL_PLAY:
256 switch (play_state) {
257 case STATE_STOPPED:
258 song = playlist_advance();
259 if (song == NULL)
260 break;
261 /* XXX: watch out for failures! */
262 main_play_song(song);
263 break;
264 case STATE_PLAYING:
265 /* do nothing */
266 break;
267 case STATE_PAUSED:
268 imsg_compose_event(iev_player, IMSG_RESUME,
269 0, 0, -1, NULL, 0);
270 break;
272 break;
273 case IMSG_CTL_TOGGLE_PLAY:
274 switch (play_state) {
275 case STATE_STOPPED:
276 song = playlist_advance();
277 if (song == NULL)
278 break;
279 /* XXX: watch out for failures! */
280 main_play_song(song);
281 break;
282 case STATE_PLAYING:
283 imsg_compose_event(iev_player, IMSG_PAUSE, 0, 0, -1,
284 NULL, 0);
285 break;
286 case STATE_PAUSED:
287 imsg_compose_event(iev_player, IMSG_RESUME, 0, 0, -1,
288 NULL, 0);
289 break;
291 break;
292 case IMSG_CTL_PAUSE:
293 if (play_state != STATE_PLAYING)
294 break;
295 imsg_compose_event(iev_player, IMSG_PAUSE, 0, 0, -1, NULL, 0);
296 break;
297 case IMSG_CTL_STOP:
298 if (play_state == STATE_STOPPED)
299 break;
300 imsg_compose_event(iev_player, IMSG_STOP, 0, 0, -1, NULL, 0);
301 break;
302 case IMSG_CTL_RESTART:
303 imsg_compose_event(iev_player, IMSG_STOP, 0, 0, -1, NULL, 0);
304 song = playlist_current();
305 if (song == NULL)
306 break;
307 /* XXX: watch out for failures */
308 main_play_song(song);
309 break;
310 case IMSG_CTL_ADD:
311 main_enqueue(&c->iev, &imsg);
312 break;
313 case IMSG_CTL_FLUSH:
314 playlist_truncate();
315 case IMSG_CTL_SHOW:
316 default:
317 log_debug("%s: error handling imsg %d", __func__,
318 imsg.hdr.type);
319 break;
321 imsg_free(&imsg);
324 imsg_event_add(&c->iev);
327 int
328 control_imsg_relay(struct imsg *imsg)
330 struct ctl_conn *c;
332 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
333 return (0);
335 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
336 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));