1 /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
4 * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
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>
44 #define CONTROL_BACKLOG 5
50 } control_state = {.fd = -1, .tx = -1};
53 TAILQ_ENTRY(ctl_conn) entry;
54 int monitor; /* 1 if client is in monitor mode */
58 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
60 struct ctl_conn *control_connbyfd(int);
61 struct ctl_conn *control_connbypid(pid_t);
62 void control_close(int);
65 control_init(char *path)
67 struct sockaddr_un sun;
71 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
72 log_warn("%s: socket", __func__);
76 if ((flags = fcntl(fd, F_GETFL)) == -1 ||
77 fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
78 fatal("fcntl(O_NONBLOCK)");
80 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
81 fatal("fcntl(CLOEXEC)");
83 memset(&sun, 0, sizeof(sun));
84 sun.sun_family = AF_UNIX;
85 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
87 if (unlink(path) == -1)
88 if (errno != ENOENT) {
89 log_warn("%s: unlink %s", __func__, path);
94 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
95 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
96 log_warn("%s: bind: %s", __func__, path);
103 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
104 log_warn("%s: chmod", __func__);
114 enable_accept(int fd, int ev, void *bula)
116 ev_add(control_state.fd, POLLIN, control_accept, NULL);
120 control_listen(int fd)
122 if (control_state.fd != -1)
123 fatalx("%s: received unexpected controlsock", __func__);
125 control_state.fd = fd;
126 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
127 log_warn("%s: listen", __func__);
131 enable_accept(-1, 0, NULL);
136 control_accept(int listenfd, int event, void *bula)
140 struct sockaddr_un sun;
144 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
146 * Pause accept if we are out of file descriptors, or
147 * libevent will haunt us here too.
149 if (errno == ENFILE || errno == EMFILE) {
150 struct timeval evtpause = { 1, 0 };
152 ev_del(control_state.fd);
153 ev_timer(&evtpause, enable_accept, NULL);
154 } else if (errno != EWOULDBLOCK && errno != EINTR &&
155 errno != ECONNABORTED)
156 log_warn("%s: accept4", __func__);
160 if ((flags = fcntl(connfd, F_GETFL)) == -1 ||
161 fcntl(connfd, F_SETFL, flags | O_NONBLOCK) == -1)
162 fatal("fcntl(O_NONBLOCK)");
163 if (fcntl(connfd, F_SETFD, FD_CLOEXEC) == -1)
164 fatal("fcntl(CLOEXEC)");
166 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
167 log_warn("%s: calloc", __func__);
172 imsg_init(&c->iev.imsgbuf, connfd);
173 c->iev.handler = control_dispatch_imsg;
174 c->iev.events = POLLIN;
175 ev_add(c->iev.imsgbuf.fd, c->iev.events, c->iev.handler, &c->iev);
177 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
181 control_connbyfd(int fd)
185 TAILQ_FOREACH(c, &ctl_conns, entry) {
186 if (c->iev.imsgbuf.fd == fd)
194 control_connbypid(pid_t pid)
198 TAILQ_FOREACH(c, &ctl_conns, entry) {
199 if (c->iev.imsgbuf.pid == pid)
207 control_close(int fd)
211 if ((c = control_connbyfd(fd)) == NULL) {
212 log_warnx("%s: fd %d: not found", __func__, fd);
216 /* abort the transaction if running by this user */
217 if (control_state.tx != -1 && c->iev.imsgbuf.fd == control_state.tx) {
218 playlist_free(&control_state.play);
219 control_state.tx = -1;
222 msgbuf_clear(&c->iev.imsgbuf.w);
223 TAILQ_REMOVE(&ctl_conns, c, entry);
225 ev_del(c->iev.imsgbuf.fd);
226 close(c->iev.imsgbuf.fd);
228 /* Some file descriptors are available again. */
229 if (ev_timer_pending()) {
230 ev_timer(NULL, NULL, NULL);
231 ev_add(control_state.fd, POLLIN, control_accept, NULL);
238 control_notify(int type)
241 struct player_event ev;
243 memset(&ev, 0, sizeof(ev));
245 ev.position = current_position;
246 ev.duration = current_duration;
247 ev.mode.repeat_one = repeat_one;
248 ev.mode.repeat_all = repeat_all;
249 ev.mode.consume = consume;
251 TAILQ_FOREACH(c, &ctl_conns, entry) {
255 imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
256 -1, &ev, sizeof(ev));
261 new_mode(int val, int newval)
263 if (newval == MODE_UNDEF)
265 if (newval == MODE_TOGGLE)
271 control_dispatch_imsg(int fd, int event, void *bula)
274 struct imsgbuf *imsgbuf;
276 struct player_mode mode;
277 struct player_seek seek;
281 if ((c = control_connbyfd(fd)) == NULL) {
282 log_warnx("%s: fd %d: not found", __func__, fd);
286 imsgbuf = &c->iev.imsgbuf;
288 if (event & POLLIN) {
289 if (((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN) ||
295 if (event & POLLOUT) {
296 if (msgbuf_write(&imsgbuf->w) <= 0 && errno != EAGAIN) {
303 if ((n = imsg_get(imsgbuf, &imsg)) == -1) {
310 switch (type = imsg_get_type(&imsg)) {
312 switch (play_state) {
314 main_playlist_resume();
320 play_state = STATE_PLAYING;
321 main_send_player(IMSG_RESUME, -1, NULL ,0);
324 control_notify(type);
326 case IMSG_CTL_TOGGLE_PLAY:
327 switch (play_state) {
329 control_notify(IMSG_CTL_PLAY);
330 main_playlist_resume();
333 control_notify(IMSG_CTL_PAUSE);
334 play_state = STATE_PAUSED;
335 main_send_player(IMSG_PAUSE, -1, NULL, 0);
338 control_notify(IMSG_CTL_PLAY);
339 play_state = STATE_PLAYING;
340 main_send_player(IMSG_RESUME, -1, NULL, 0);
345 if (play_state != STATE_PLAYING)
347 play_state = STATE_PAUSED;
348 main_send_player(IMSG_PAUSE, -1, NULL, 0);
349 control_notify(type);
352 if (play_state == STATE_STOPPED)
354 play_state = STATE_STOPPED;
355 main_send_player(IMSG_STOP, -1, NULL, 0);
356 control_notify(type);
360 control_notify(IMSG_CTL_COMMIT);
363 main_send_playlist(&c->iev);
365 case IMSG_CTL_STATUS:
366 main_send_status(&c->iev);
369 control_notify(type);
370 main_send_player(IMSG_STOP, -1, NULL, 0);
371 main_playlist_advance();
374 control_notify(type);
375 main_send_player(IMSG_STOP, -1, NULL, 0);
376 main_playlist_previous();
379 main_playlist_jump(&c->iev, &imsg);
382 if (imsg_get_data(&imsg, &mode, sizeof(mode)) == -1) {
383 log_warnx("%s: got wrong size", __func__);
386 consume = new_mode(consume, mode.consume);
387 repeat_all = new_mode(repeat_all, mode.repeat_all);
388 repeat_one = new_mode(repeat_one, mode.repeat_one);
389 control_notify(type);
392 if (control_state.tx != -1) {
393 main_senderr(&c->iev, "locked");
396 control_state.tx = imsgbuf->fd;
397 imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
401 if (control_state.tx != -1 &&
402 control_state.tx != imsgbuf->fd) {
403 main_senderr(&c->iev, "locked");
406 main_enqueue(control_state.tx != -1,
407 &control_state.play,&c->iev, &imsg);
408 if (control_state.tx == -1)
409 control_notify(type);
411 case IMSG_CTL_COMMIT:
412 if (control_state.tx != imsgbuf->fd) {
413 main_senderr(&c->iev, "locked");
416 if (imsg_get_data(&imsg, &off, sizeof(off)) == -1) {
417 main_senderr(&c->iev, "wrong size");
420 playlist_swap(&control_state.play, off);
421 memset(&control_state.play, 0,
422 sizeof(control_state.play));
423 control_state.tx = -1;
424 imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
426 control_notify(type);
428 case IMSG_CTL_MONITOR:
432 if (imsg_get_data(&imsg, &seek, sizeof(seek)) == -1) {
433 main_senderr(&c->iev, "wrong size");
439 log_debug("%s: error handling imsg %d", __func__,
446 imsg_event_add(&c->iev);