1 /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
25 #include <netinet/in.h>
42 #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 | SOCK_CLOEXEC | SOCK_NONBLOCK,
73 log_warn("%s: socket", __func__);
77 memset(&sun, 0, sizeof(sun));
78 sun.sun_family = AF_UNIX;
79 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
81 if (unlink(path) == -1)
82 if (errno != ENOENT) {
83 log_warn("%s: unlink %s", __func__, path);
88 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
89 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
90 log_warn("%s: bind: %s", __func__, path);
97 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
98 log_warn("%s: chmod", __func__);
108 control_listen(int fd)
110 if (control_state.fd != -1)
111 fatalx("%s: received unexpected controlsock", __func__);
113 control_state.fd = fd;
114 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
115 log_warn("%s: listen", __func__);
119 event_set(&control_state.ev, control_state.fd, EV_READ,
120 control_accept, NULL);
121 event_add(&control_state.ev, NULL);
122 evtimer_set(&control_state.evt, control_accept, NULL);
128 control_accept(int listenfd, short event, void *bula)
132 struct sockaddr_un sun;
135 event_add(&control_state.ev, NULL);
136 if ((event & EV_TIMEOUT))
140 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
141 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
143 * Pause accept if we are out of file descriptors, or
144 * libevent will haunt us here too.
146 if (errno == ENFILE || errno == EMFILE) {
147 struct timeval evtpause = { 1, 0 };
149 event_del(&control_state.ev);
150 evtimer_add(&control_state.evt, &evtpause);
151 } else if (errno != EWOULDBLOCK && errno != EINTR &&
152 errno != ECONNABORTED)
153 log_warn("%s: accept4", __func__);
157 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
158 log_warn("%s: calloc", __func__);
163 imsg_init(&c->iev.ibuf, connfd);
164 c->iev.handler = control_dispatch_imsg;
165 c->iev.events = EV_READ;
166 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
167 c->iev.handler, &c->iev);
168 event_add(&c->iev.ev, NULL);
170 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
174 control_connbyfd(int fd)
178 TAILQ_FOREACH(c, &ctl_conns, entry) {
179 if (c->iev.ibuf.fd == fd)
187 control_connbypid(pid_t pid)
191 TAILQ_FOREACH(c, &ctl_conns, entry) {
192 if (c->iev.ibuf.pid == pid)
200 control_close(int fd)
204 if ((c = control_connbyfd(fd)) == NULL) {
205 log_warnx("%s: fd %d: not found", __func__, fd);
209 /* abort the transaction if running by this user */
210 if (control_state.tx != -1 && c->iev.ibuf.fd == control_state.tx) {
211 playlist_free(&control_state.play);
212 control_state.tx = -1;
215 msgbuf_clear(&c->iev.ibuf.w);
216 TAILQ_REMOVE(&ctl_conns, c, entry);
218 event_del(&c->iev.ev);
219 close(c->iev.ibuf.fd);
221 /* Some file descriptors are available again. */
222 if (evtimer_pending(&control_state.evt, NULL)) {
223 evtimer_del(&control_state.evt);
224 event_add(&control_state.ev, NULL);
231 control_notify(struct imsgev *iev, int type)
235 TAILQ_FOREACH(c, &ctl_conns, entry) {
236 if (&c->iev == iev || !c->monitor)
239 imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
240 -1, &type, sizeof(type));
245 control_dispatch_imsg(int fd, short event, void *bula)
249 struct player_repeat rp;
252 if ((c = control_connbyfd(fd)) == NULL) {
253 log_warnx("%s: fd %d: not found", __func__, fd);
257 if (event & EV_READ) {
258 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
264 if (event & EV_WRITE) {
265 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
272 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
279 switch (imsg.hdr.type) {
281 switch (play_state) {
283 main_playlist_resume();
289 play_state = STATE_PLAYING;
290 main_send_player(IMSG_RESUME, -1, NULL, 0);
293 control_notify(&c->iev, imsg.hdr.type);
295 case IMSG_CTL_TOGGLE_PLAY:
296 switch (play_state) {
298 main_playlist_resume();
301 play_state = STATE_PAUSED;
302 main_send_player(IMSG_PAUSE, -1, NULL, 0);
305 play_state = STATE_PLAYING;
306 main_send_player(IMSG_RESUME, -1, NULL, 0);
309 control_notify(&c->iev, imsg.hdr.type);
312 if (play_state != STATE_PLAYING)
314 play_state = STATE_PAUSED;
315 main_send_player(IMSG_PAUSE, -1, NULL, 0);
316 control_notify(&c->iev, imsg.hdr.type);
319 if (play_state == STATE_STOPPED)
321 play_state = STATE_STOPPED;
322 main_send_player(IMSG_STOP, -1, NULL, 0);
323 control_notify(&c->iev, imsg.hdr.type);
325 case IMSG_CTL_RESTART:
326 main_send_player(IMSG_STOP, -1, NULL, 0);
327 main_restart_track();
328 control_notify(&c->iev, imsg.hdr.type);
332 control_notify(&c->iev, imsg.hdr.type);
335 main_send_playlist(&c->iev);
337 case IMSG_CTL_STATUS:
338 main_send_status(&c->iev);
341 main_send_player(IMSG_STOP, -1, NULL, 0);
342 main_playlist_advance();
343 control_notify(&c->iev, imsg.hdr.type);
346 main_send_player(IMSG_STOP, -1, NULL, 0);
347 main_playlist_previous();
348 control_notify(&c->iev, imsg.hdr.type);
351 main_playlist_jump(&c->iev, &imsg);
352 control_notify(&c->iev, imsg.hdr.type);
354 case IMSG_CTL_REPEAT:
355 if (IMSG_DATA_SIZE(imsg) != sizeof(rp)) {
356 log_warnx("%s: got wrong size", __func__);
359 memcpy(&rp, imsg.data, sizeof(rp));
360 if (rp.repeat_all != -1)
361 repeat_all = rp.repeat_all;
362 if (rp.repeat_one != -1)
363 repeat_one = rp.repeat_one;
364 control_notify(&c->iev, imsg.hdr.type);
367 if (control_state.tx != -1) {
368 main_senderr(&c->iev, "locked");
371 control_state.tx = c->iev.ibuf.fd;
372 imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
376 if (control_state.tx != -1 &&
377 control_state.tx != c->iev.ibuf.fd) {
378 main_senderr(&c->iev, "locked");
381 main_enqueue(control_state.tx != -1,
382 &control_state.play,&c->iev, &imsg);
383 if (control_state.tx == -1)
384 control_notify(&c->iev, imsg.hdr.type);
386 case IMSG_CTL_COMMIT:
387 if (control_state.tx != c->iev.ibuf.fd) {
388 main_senderr(&c->iev, "locked");
391 playlist_swap(&control_state.play);
392 memset(&control_state.play, 0,
393 sizeof(control_state.play));
394 control_state.tx = -1;
395 imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
397 control_notify(&c->iev, imsg.hdr.type);
399 case IMSG_CTL_MONITOR:
403 log_debug("%s: error handling imsg %d", __func__,
410 imsg_event_add(&c->iev);
414 control_imsg_relay(struct imsg *imsg)
418 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
421 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
422 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));