001
2022-02-16
op
/* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
004
2022-02-16
op
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
006
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
007
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
008
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
010
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
011
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
012
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
013
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
014
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
015
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
016
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
018
2022-02-16
op
#include <sys/types.h>
019
2022-02-16
op
#include <sys/queue.h>
020
2022-02-16
op
#include <sys/stat.h>
021
2022-02-16
op
#include <sys/socket.h>
022
2022-02-16
op
#include <sys/uio.h>
023
2022-02-16
op
#include <sys/un.h>
025
2022-02-16
op
#include <netinet/in.h>
026
2022-02-16
op
#include <net/if.h>
028
2022-02-16
op
#include <errno.h>
029
2022-02-16
op
#include <event.h>
030
2022-02-16
op
#include <imsg.h>
031
2022-02-16
op
#include <limits.h>
032
2022-02-17
op
#include <stdio.h>
033
2022-02-16
op
#include <stdlib.h>
034
2022-02-16
op
#include <string.h>
035
2022-02-16
op
#include <unistd.h>
037
2022-02-16
op
#include "amused.h"
038
2022-02-16
op
#include "log.h"
039
2022-02-16
op
#include "control.h"
040
2022-02-16
op
#include "playlist.h"
042
2022-02-16
op
#define CONTROL_BACKLOG 5
045
2022-02-16
op
struct event ev;
046
2022-02-16
op
struct event evt;
048
2022-02-17
op
struct playlist play;
050
2022-02-17
op
} control_state = {.fd = -1, .tx = -1};
052
2022-02-16
op
struct ctl_conn {
053
2022-02-16
op
TAILQ_ENTRY(ctl_conn) entry;
054
2022-02-21
op
int monitor; /* 1 if client is in monitor mode */
055
2022-02-16
op
struct imsgev iev;
058
2022-02-16
op
TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
060
2022-02-16
op
struct ctl_conn *control_connbyfd(int);
061
2022-02-16
op
struct ctl_conn *control_connbypid(pid_t);
062
2022-02-16
op
void control_close(int);
065
2022-02-16
op
control_init(char *path)
067
2022-02-16
op
struct sockaddr_un sun;
069
2022-02-16
op
mode_t old_umask;
071
2022-02-16
op
if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
072
2022-02-16
op
0)) == -1) {
073
2022-02-16
op
log_warn("%s: socket", __func__);
074
2022-02-16
op
return (-1);
077
2022-02-16
op
memset(&sun, 0, sizeof(sun));
078
2022-02-16
op
sun.sun_family = AF_UNIX;
079
2022-02-16
op
strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
081
2022-02-16
op
if (unlink(path) == -1)
082
2022-02-16
op
if (errno != ENOENT) {
083
2022-02-16
op
log_warn("%s: unlink %s", __func__, path);
084
2022-02-16
op
close(fd);
085
2022-02-16
op
return (-1);
088
2022-02-16
op
old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
089
2022-02-16
op
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
090
2022-02-16
op
log_warn("%s: bind: %s", __func__, path);
091
2022-02-16
op
close(fd);
092
2022-02-16
op
umask(old_umask);
093
2022-02-16
op
return (-1);
095
2022-02-16
op
umask(old_umask);
097
2022-02-16
op
if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
098
2022-02-16
op
log_warn("%s: chmod", __func__);
099
2022-02-16
op
close(fd);
100
2022-02-16
op
(void)unlink(path);
101
2022-02-16
op
return (-1);
104
2022-02-16
op
return (fd);
108
2022-02-16
op
control_listen(int fd)
110
2022-02-16
op
if (control_state.fd != -1)
111
2022-02-16
op
fatalx("%s: received unexpected controlsock", __func__);
113
2022-02-16
op
control_state.fd = fd;
114
2022-02-16
op
if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
115
2022-02-16
op
log_warn("%s: listen", __func__);
116
2022-02-16
op
return (-1);
119
2022-02-16
op
event_set(&control_state.ev, control_state.fd, EV_READ,
120
2022-02-16
op
control_accept, NULL);
121
2022-02-16
op
event_add(&control_state.ev, NULL);
122
2022-02-16
op
evtimer_set(&control_state.evt, control_accept, NULL);
124
2022-02-16
op
return (0);
128
2022-02-16
op
control_accept(int listenfd, short event, void *bula)
130
2022-02-16
op
int connfd;
131
2022-02-16
op
socklen_t len;
132
2022-02-16
op
struct sockaddr_un sun;
133
2022-02-16
op
struct ctl_conn *c;
135
2022-02-16
op
event_add(&control_state.ev, NULL);
136
2022-02-16
op
if ((event & EV_TIMEOUT))
139
2022-02-16
op
len = sizeof(sun);
140
2022-02-16
op
if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
141
2022-02-16
op
SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
143
2022-02-16
op
* Pause accept if we are out of file descriptors, or
144
2022-02-16
op
* libevent will haunt us here too.
146
2022-02-16
op
if (errno == ENFILE || errno == EMFILE) {
147
2022-02-16
op
struct timeval evtpause = { 1, 0 };
149
2022-02-16
op
event_del(&control_state.ev);
150
2022-02-16
op
evtimer_add(&control_state.evt, &evtpause);
151
2022-02-16
op
} else if (errno != EWOULDBLOCK && errno != EINTR &&
152
2022-02-16
op
errno != ECONNABORTED)
153
2022-02-16
op
log_warn("%s: accept4", __func__);
157
2022-02-16
op
if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
158
2022-02-16
op
log_warn("%s: calloc", __func__);
159
2022-02-16
op
close(connfd);
163
2022-02-16
op
imsg_init(&c->iev.ibuf, connfd);
164
2022-02-16
op
c->iev.handler = control_dispatch_imsg;
165
2022-02-16
op
c->iev.events = EV_READ;
166
2022-02-16
op
event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
167
2022-02-16
op
c->iev.handler, &c->iev);
168
2022-02-16
op
event_add(&c->iev.ev, NULL);
170
2022-02-16
op
TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
173
2022-02-16
op
struct ctl_conn *
174
2022-02-16
op
control_connbyfd(int fd)
176
2022-02-16
op
struct ctl_conn *c;
178
2022-02-16
op
TAILQ_FOREACH(c, &ctl_conns, entry) {
179
2022-02-16
op
if (c->iev.ibuf.fd == fd)
183
2022-02-16
op
return (c);
186
2022-02-16
op
struct ctl_conn *
187
2022-02-16
op
control_connbypid(pid_t pid)
189
2022-02-16
op
struct ctl_conn *c;
191
2022-02-16
op
TAILQ_FOREACH(c, &ctl_conns, entry) {
192
2022-02-16
op
if (c->iev.ibuf.pid == pid)
196
2022-02-16
op
return (c);
200
2022-02-16
op
control_close(int fd)
202
2022-02-16
op
struct ctl_conn *c;
204
2022-02-16
op
if ((c = control_connbyfd(fd)) == NULL) {
205
2022-02-16
op
log_warnx("%s: fd %d: not found", __func__, fd);
209
2022-02-17
op
/* abort the transaction if running by this user */
210
2022-02-17
op
if (control_state.tx != -1 && c->iev.ibuf.fd == control_state.tx) {
211
2022-02-17
op
playlist_free(&control_state.play);
212
2022-02-17
op
control_state.tx = -1;
215
2022-02-16
op
msgbuf_clear(&c->iev.ibuf.w);
216
2022-02-16
op
TAILQ_REMOVE(&ctl_conns, c, entry);
218
2022-02-16
op
event_del(&c->iev.ev);
219
2022-02-16
op
close(c->iev.ibuf.fd);
221
2022-02-16
op
/* Some file descriptors are available again. */
222
2022-02-16
op
if (evtimer_pending(&control_state.evt, NULL)) {
223
2022-02-16
op
evtimer_del(&control_state.evt);
224
2022-02-16
op
event_add(&control_state.ev, NULL);
231
2022-02-21
op
control_notify(struct imsgev *iev, int type)
233
2022-02-21
op
struct ctl_conn *c;
235
2022-02-21
op
TAILQ_FOREACH(c, &ctl_conns, entry) {
236
2022-02-21
op
if (&c->iev == iev || !c->monitor)
237
2022-02-21
op
continue;
239
2022-02-21
op
imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
240
2022-02-21
op
-1, &type, sizeof(type));
245
2022-02-16
op
control_dispatch_imsg(int fd, short event, void *bula)
247
2022-02-19
op
struct ctl_conn *c;
248
2022-02-19
op
struct imsg imsg;
249
2022-02-19
op
struct player_repeat rp;
250
2022-03-02
op
ssize_t n, off;
252
2022-02-16
op
if ((c = control_connbyfd(fd)) == NULL) {
253
2022-02-16
op
log_warnx("%s: fd %d: not found", __func__, fd);
257
2022-02-16
op
if (event & EV_READ) {
258
2022-02-16
op
if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
259
2022-02-16
op
n == 0) {
260
2022-02-16
op
control_close(fd);
264
2022-02-16
op
if (event & EV_WRITE) {
265
2022-02-16
op
if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
266
2022-02-16
op
control_close(fd);
271
2022-02-16
op
for (;;) {
272
2022-02-16
op
if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
273
2022-02-16
op
control_close(fd);
276
2022-02-16
op
if (n == 0)
279
2022-02-16
op
switch (imsg.hdr.type) {
280
2022-02-16
op
case IMSG_CTL_PLAY:
281
2022-02-16
op
switch (play_state) {
282
2022-02-16
op
case STATE_STOPPED:
283
2022-02-17
op
main_playlist_resume();
285
2022-02-16
op
case STATE_PLAYING:
286
2022-02-16
op
/* do nothing */
288
2022-02-16
op
case STATE_PAUSED:
289
2022-02-16
op
play_state = STATE_PLAYING;
290
2022-05-10
op
main_send_player(IMSG_RESUME, -1);
293
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
295
2022-02-16
op
case IMSG_CTL_TOGGLE_PLAY:
296
2022-02-16
op
switch (play_state) {
297
2022-02-16
op
case STATE_STOPPED:
298
2022-02-17
op
main_playlist_resume();
300
2022-02-16
op
case STATE_PLAYING:
301
2022-02-16
op
play_state = STATE_PAUSED;
302
2022-05-10
op
main_send_player(IMSG_PAUSE, -1);
304
2022-02-16
op
case STATE_PAUSED:
305
2022-02-16
op
play_state = STATE_PLAYING;
306
2022-05-10
op
main_send_player(IMSG_RESUME, -1);
309
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
311
2022-02-16
op
case IMSG_CTL_PAUSE:
312
2022-02-16
op
if (play_state != STATE_PLAYING)
314
2022-02-16
op
play_state = STATE_PAUSED;
315
2022-05-10
op
main_send_player(IMSG_PAUSE, -1);
316
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
318
2022-02-16
op
case IMSG_CTL_STOP:
319
2022-02-16
op
if (play_state == STATE_STOPPED)
321
2022-02-17
op
play_state = STATE_STOPPED;
322
2022-05-10
op
main_send_player(IMSG_STOP, -1);
323
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
325
2022-02-16
op
case IMSG_CTL_RESTART:
326
2022-05-10
op
main_send_player(IMSG_STOP, -1);
327
2022-02-16
op
main_restart_track();
328
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
330
2022-02-16
op
case IMSG_CTL_FLUSH:
331
2022-02-16
op
playlist_truncate();
332
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
334
2022-02-16
op
case IMSG_CTL_SHOW:
335
2022-02-16
op
main_send_playlist(&c->iev);
337
2022-02-16
op
case IMSG_CTL_STATUS:
338
2022-02-16
op
main_send_status(&c->iev);
340
2022-02-17
op
case IMSG_CTL_NEXT:
341
2022-05-10
op
main_send_player(IMSG_STOP, -1);
342
2022-02-17
op
main_playlist_advance();
343
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
345
2022-02-17
op
case IMSG_CTL_PREV:
346
2022-05-10
op
main_send_player(IMSG_STOP, -1);
347
2022-02-17
op
main_playlist_previous();
348
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
350
2022-02-17
op
case IMSG_CTL_JUMP:
351
2022-02-17
op
main_playlist_jump(&c->iev, &imsg);
352
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
354
2022-02-19
op
case IMSG_CTL_REPEAT:
355
2022-02-19
op
if (IMSG_DATA_SIZE(imsg) != sizeof(rp)) {
356
2022-02-19
op
log_warnx("%s: got wrong size", __func__);
359
2022-02-19
op
memcpy(&rp, imsg.data, sizeof(rp));
360
2022-02-19
op
if (rp.repeat_all != -1)
361
2022-02-19
op
repeat_all = rp.repeat_all;
362
2022-02-19
op
if (rp.repeat_one != -1)
363
2022-02-19
op
repeat_one = rp.repeat_one;
364
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
366
2022-02-17
op
case IMSG_CTL_BEGIN:
367
2022-02-17
op
if (control_state.tx != -1) {
368
2022-02-17
op
main_senderr(&c->iev, "locked");
371
2022-02-17
op
control_state.tx = c->iev.ibuf.fd;
372
2022-02-17
op
imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
373
2022-02-17
op
NULL, 0);
375
2022-02-17
op
case IMSG_CTL_ADD:
376
2022-02-17
op
if (control_state.tx != -1 &&
377
2022-02-17
op
control_state.tx != c->iev.ibuf.fd) {
378
2022-02-17
op
main_senderr(&c->iev, "locked");
381
2022-02-17
op
main_enqueue(control_state.tx != -1,
382
2022-02-17
op
&control_state.play,&c->iev, &imsg);
383
2022-02-21
op
if (control_state.tx == -1)
384
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
386
2022-02-17
op
case IMSG_CTL_COMMIT:
387
2022-02-17
op
if (control_state.tx != c->iev.ibuf.fd) {
388
2022-02-17
op
main_senderr(&c->iev, "locked");
391
2022-03-02
op
if (IMSG_DATA_SIZE(imsg) != sizeof(off)) {
392
2022-03-02
op
main_senderr(&c->iev, "wrong size");
395
2022-03-02
op
memcpy(&off, imsg.data, sizeof(off));
396
2022-03-02
op
playlist_swap(&control_state.play, off);
397
2022-02-17
op
memset(&control_state.play, 0,
398
2022-02-17
op
sizeof(control_state.play));
399
2022-02-17
op
control_state.tx = -1;
400
2022-02-17
op
imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
401
2022-02-17
op
NULL, 0);
402
2022-02-21
op
control_notify(&c->iev, imsg.hdr.type);
404
2022-02-21
op
case IMSG_CTL_MONITOR:
405
2022-02-21
op
c->monitor = 1;
408
2022-02-16
op
log_debug("%s: error handling imsg %d", __func__,
409
2022-02-16
op
imsg.hdr.type);
412
2022-02-16
op
imsg_free(&imsg);
415
2022-02-16
op
imsg_event_add(&c->iev);
419
2022-02-16
op
control_imsg_relay(struct imsg *imsg)
421
2022-02-16
op
struct ctl_conn *c;
423
2022-02-16
op
if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
424
2022-02-16
op
return (0);
426
2022-02-16
op
return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
427
2022-02-16
op
-1, imsg->data, IMSG_DATA_SIZE(*imsg)));