1 /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
4 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/socket.h>
26 #include <netinet/in.h>
41 #define CONTROL_BACKLOG 5
49 } control_state = {.fd = -1, .tx = -1};
52 TAILQ_ENTRY(ctl_conn) entry;
53 int monitor; /* 1 if client is in monitor mode */
57 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 struct ctl_conn *control_connbyfd(int);
60 struct ctl_conn *control_connbypid(pid_t);
61 void control_close(int);
64 control_init(char *path)
66 struct sockaddr_un sun;
70 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
72 log_warn("%s: socket", __func__);
76 memset(&sun, 0, sizeof(sun));
77 sun.sun_family = AF_UNIX;
78 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
80 if (unlink(path) == -1)
81 if (errno != ENOENT) {
82 log_warn("%s: unlink %s", __func__, path);
87 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
88 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
89 log_warn("%s: bind: %s", __func__, path);
96 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 log_warn("%s: chmod", __func__);
107 control_listen(int fd)
109 if (control_state.fd != -1)
110 fatalx("%s: received unexpected controlsock", __func__);
112 control_state.fd = fd;
113 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
114 log_warn("%s: listen", __func__);
118 event_set(&control_state.ev, control_state.fd, EV_READ,
119 control_accept, NULL);
120 event_add(&control_state.ev, NULL);
121 evtimer_set(&control_state.evt, control_accept, NULL);
127 control_accept(int listenfd, short event, void *bula)
131 struct sockaddr_un sun;
134 event_add(&control_state.ev, NULL);
135 if ((event & EV_TIMEOUT))
139 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
140 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
142 * Pause accept if we are out of file descriptors, or
143 * libevent will haunt us here too.
145 if (errno == ENFILE || errno == EMFILE) {
146 struct timeval evtpause = { 1, 0 };
148 event_del(&control_state.ev);
149 evtimer_add(&control_state.evt, &evtpause);
150 } else if (errno != EWOULDBLOCK && errno != EINTR &&
151 errno != ECONNABORTED)
152 log_warn("%s: accept4", __func__);
156 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
157 log_warn("%s: calloc", __func__);
162 imsg_init(&c->iev.ibuf, connfd);
163 c->iev.handler = control_dispatch_imsg;
164 c->iev.events = EV_READ;
165 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
166 c->iev.handler, &c->iev);
167 event_add(&c->iev.ev, NULL);
169 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
173 control_connbyfd(int fd)
177 TAILQ_FOREACH(c, &ctl_conns, entry) {
178 if (c->iev.ibuf.fd == fd)
186 control_connbypid(pid_t pid)
190 TAILQ_FOREACH(c, &ctl_conns, entry) {
191 if (c->iev.ibuf.pid == pid)
199 control_close(int fd)
203 if ((c = control_connbyfd(fd)) == NULL) {
204 log_warnx("%s: fd %d: not found", __func__, fd);
208 /* abort the transaction if running by this user */
209 if (control_state.tx != -1 && c->iev.ibuf.fd == control_state.tx) {
210 playlist_free(&control_state.play);
211 control_state.tx = -1;
214 msgbuf_clear(&c->iev.ibuf.w);
215 TAILQ_REMOVE(&ctl_conns, c, entry);
217 event_del(&c->iev.ev);
218 close(c->iev.ibuf.fd);
220 /* Some file descriptors are available again. */
221 if (evtimer_pending(&control_state.evt, NULL)) {
222 evtimer_del(&control_state.evt);
223 event_add(&control_state.ev, NULL);
230 control_notify(struct imsgev *iev, int type)
234 TAILQ_FOREACH(c, &ctl_conns, entry) {
235 if (&c->iev == iev || !c->monitor)
238 imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
239 -1, &type, sizeof(type));
244 control_dispatch_imsg(int fd, short event, void *bula)
248 struct player_repeat rp;
251 if ((c = control_connbyfd(fd)) == NULL) {
252 log_warnx("%s: fd %d: not found", __func__, fd);
256 if (event & EV_READ) {
257 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
263 if (event & EV_WRITE) {
264 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
271 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
278 switch (imsg.hdr.type) {
280 switch (play_state) {
282 main_playlist_resume();
288 play_state = STATE_PLAYING;
289 main_send_player(IMSG_RESUME, -1, NULL ,0);
292 control_notify(&c->iev, imsg.hdr.type);
294 case IMSG_CTL_TOGGLE_PLAY:
295 switch (play_state) {
297 main_playlist_resume();
300 play_state = STATE_PAUSED;
301 main_send_player(IMSG_PAUSE, -1, NULL, 0);
304 play_state = STATE_PLAYING;
305 main_send_player(IMSG_RESUME, -1, NULL, 0);
308 control_notify(&c->iev, imsg.hdr.type);
311 if (play_state != STATE_PLAYING)
313 play_state = STATE_PAUSED;
314 main_send_player(IMSG_PAUSE, -1, NULL, 0);
315 control_notify(&c->iev, imsg.hdr.type);
318 if (play_state == STATE_STOPPED)
320 play_state = STATE_STOPPED;
321 main_send_player(IMSG_STOP, -1, NULL, 0);
322 control_notify(&c->iev, imsg.hdr.type);
326 control_notify(&c->iev, imsg.hdr.type);
329 main_send_playlist(&c->iev);
331 case IMSG_CTL_STATUS:
332 main_send_status(&c->iev);
335 main_send_player(IMSG_STOP, -1, NULL, 0);
336 main_playlist_advance();
337 control_notify(&c->iev, imsg.hdr.type);
340 main_send_player(IMSG_STOP, -1, NULL, 0);
341 main_playlist_previous();
342 control_notify(&c->iev, imsg.hdr.type);
345 main_playlist_jump(&c->iev, &imsg);
346 control_notify(&c->iev, imsg.hdr.type);
348 case IMSG_CTL_REPEAT:
349 if (IMSG_DATA_SIZE(imsg) != sizeof(rp)) {
350 log_warnx("%s: got wrong size", __func__);
353 memcpy(&rp, imsg.data, sizeof(rp));
354 if (rp.repeat_all != -1)
355 repeat_all = rp.repeat_all;
356 if (rp.repeat_one != -1)
357 repeat_one = rp.repeat_one;
358 control_notify(&c->iev, imsg.hdr.type);
361 if (control_state.tx != -1) {
362 main_senderr(&c->iev, "locked");
365 control_state.tx = c->iev.ibuf.fd;
366 imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
370 if (control_state.tx != -1 &&
371 control_state.tx != c->iev.ibuf.fd) {
372 main_senderr(&c->iev, "locked");
375 main_enqueue(control_state.tx != -1,
376 &control_state.play,&c->iev, &imsg);
377 if (control_state.tx == -1)
378 control_notify(&c->iev, imsg.hdr.type);
380 case IMSG_CTL_COMMIT:
381 if (control_state.tx != c->iev.ibuf.fd) {
382 main_senderr(&c->iev, "locked");
385 if (IMSG_DATA_SIZE(imsg) != sizeof(off)) {
386 main_senderr(&c->iev, "wrong size");
389 memcpy(&off, imsg.data, sizeof(off));
390 playlist_swap(&control_state.play, off);
391 memset(&control_state.play, 0,
392 sizeof(control_state.play));
393 control_state.tx = -1;
394 imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
396 control_notify(&c->iev, imsg.hdr.type);
398 case IMSG_CTL_MONITOR:
402 if (IMSG_DATA_SIZE(imsg) !=
403 sizeof(struct player_seek)) {
404 main_senderr(&c->iev, "wrong size");
407 switch (play_state) {
409 main_playlist_resume();
414 play_state = STATE_PLAYING;
417 main_send_player(IMSG_CTL_SEEK, -1, imsg.data,
418 IMSG_DATA_SIZE(imsg));
419 control_notify(&c->iev, imsg.hdr.type);
422 log_debug("%s: error handling imsg %d", __func__,
429 imsg_event_add(&c->iev);
433 control_imsg_relay(struct imsg *imsg)
437 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
440 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
441 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));