Blame


1 3baa2617 2022-02-16 op /* $OpenBSD: control.c,v 1.8 2021/03/02 04:10:07 jsg Exp $ */
2 3baa2617 2022-02-16 op
3 3baa2617 2022-02-16 op /*
4 3baa2617 2022-02-16 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 3baa2617 2022-02-16 op *
6 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
7 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
8 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
9 3baa2617 2022-02-16 op *
10 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op */
18 3baa2617 2022-02-16 op #include <sys/types.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/stat.h>
21 3baa2617 2022-02-16 op #include <sys/socket.h>
22 3baa2617 2022-02-16 op #include <sys/uio.h>
23 3baa2617 2022-02-16 op #include <sys/un.h>
24 3baa2617 2022-02-16 op
25 3baa2617 2022-02-16 op #include <netinet/in.h>
26 3baa2617 2022-02-16 op #include <net/if.h>
27 3baa2617 2022-02-16 op
28 3baa2617 2022-02-16 op #include <errno.h>
29 3baa2617 2022-02-16 op #include <event.h>
30 3baa2617 2022-02-16 op #include <imsg.h>
31 bb3f279f 2022-02-16 op #include <limits.h>
32 14be015f 2022-02-17 op #include <stdio.h>
33 3baa2617 2022-02-16 op #include <stdlib.h>
34 3baa2617 2022-02-16 op #include <string.h>
35 3baa2617 2022-02-16 op #include <unistd.h>
36 3baa2617 2022-02-16 op
37 3baa2617 2022-02-16 op #include "amused.h"
38 3baa2617 2022-02-16 op #include "log.h"
39 3baa2617 2022-02-16 op #include "control.h"
40 3baa2617 2022-02-16 op #include "playlist.h"
41 3baa2617 2022-02-16 op
42 3baa2617 2022-02-16 op #define CONTROL_BACKLOG 5
43 3baa2617 2022-02-16 op
44 3baa2617 2022-02-16 op struct {
45 3baa2617 2022-02-16 op struct event ev;
46 3baa2617 2022-02-16 op struct event evt;
47 3baa2617 2022-02-16 op int fd;
48 19d6b480 2022-02-17 op struct playlist play;
49 c6304ab9 2022-02-17 op int tx;
50 19d6b480 2022-02-17 op } control_state = {.fd = -1, .tx = -1};
51 3baa2617 2022-02-16 op
52 3baa2617 2022-02-16 op struct ctl_conn {
53 3baa2617 2022-02-16 op TAILQ_ENTRY(ctl_conn) entry;
54 87f575c3 2022-02-21 op int monitor; /* 1 if client is in monitor mode */
55 3baa2617 2022-02-16 op struct imsgev iev;
56 3baa2617 2022-02-16 op };
57 3baa2617 2022-02-16 op
58 3baa2617 2022-02-16 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 3baa2617 2022-02-16 op
60 3baa2617 2022-02-16 op struct ctl_conn *control_connbyfd(int);
61 3baa2617 2022-02-16 op struct ctl_conn *control_connbypid(pid_t);
62 3baa2617 2022-02-16 op void control_close(int);
63 3baa2617 2022-02-16 op
64 3baa2617 2022-02-16 op int
65 3baa2617 2022-02-16 op control_init(char *path)
66 3baa2617 2022-02-16 op {
67 3baa2617 2022-02-16 op struct sockaddr_un sun;
68 3baa2617 2022-02-16 op int fd;
69 3baa2617 2022-02-16 op mode_t old_umask;
70 3baa2617 2022-02-16 op
71 3baa2617 2022-02-16 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
72 3baa2617 2022-02-16 op 0)) == -1) {
73 3baa2617 2022-02-16 op log_warn("%s: socket", __func__);
74 3baa2617 2022-02-16 op return (-1);
75 3baa2617 2022-02-16 op }
76 3baa2617 2022-02-16 op
77 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
78 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
79 3baa2617 2022-02-16 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op if (unlink(path) == -1)
82 3baa2617 2022-02-16 op if (errno != ENOENT) {
83 3baa2617 2022-02-16 op log_warn("%s: unlink %s", __func__, path);
84 3baa2617 2022-02-16 op close(fd);
85 3baa2617 2022-02-16 op return (-1);
86 3baa2617 2022-02-16 op }
87 3baa2617 2022-02-16 op
88 3baa2617 2022-02-16 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
89 3baa2617 2022-02-16 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
90 3baa2617 2022-02-16 op log_warn("%s: bind: %s", __func__, path);
91 3baa2617 2022-02-16 op close(fd);
92 3baa2617 2022-02-16 op umask(old_umask);
93 3baa2617 2022-02-16 op return (-1);
94 3baa2617 2022-02-16 op }
95 3baa2617 2022-02-16 op umask(old_umask);
96 3baa2617 2022-02-16 op
97 3baa2617 2022-02-16 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
98 3baa2617 2022-02-16 op log_warn("%s: chmod", __func__);
99 3baa2617 2022-02-16 op close(fd);
100 3baa2617 2022-02-16 op (void)unlink(path);
101 3baa2617 2022-02-16 op return (-1);
102 3baa2617 2022-02-16 op }
103 3baa2617 2022-02-16 op
104 3baa2617 2022-02-16 op return (fd);
105 3baa2617 2022-02-16 op }
106 3baa2617 2022-02-16 op
107 3baa2617 2022-02-16 op int
108 3baa2617 2022-02-16 op control_listen(int fd)
109 3baa2617 2022-02-16 op {
110 3baa2617 2022-02-16 op if (control_state.fd != -1)
111 3baa2617 2022-02-16 op fatalx("%s: received unexpected controlsock", __func__);
112 3baa2617 2022-02-16 op
113 3baa2617 2022-02-16 op control_state.fd = fd;
114 3baa2617 2022-02-16 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
115 3baa2617 2022-02-16 op log_warn("%s: listen", __func__);
116 3baa2617 2022-02-16 op return (-1);
117 3baa2617 2022-02-16 op }
118 3baa2617 2022-02-16 op
119 3baa2617 2022-02-16 op event_set(&control_state.ev, control_state.fd, EV_READ,
120 3baa2617 2022-02-16 op control_accept, NULL);
121 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
122 3baa2617 2022-02-16 op evtimer_set(&control_state.evt, control_accept, NULL);
123 3baa2617 2022-02-16 op
124 3baa2617 2022-02-16 op return (0);
125 3baa2617 2022-02-16 op }
126 3baa2617 2022-02-16 op
127 3baa2617 2022-02-16 op void
128 3baa2617 2022-02-16 op control_accept(int listenfd, short event, void *bula)
129 3baa2617 2022-02-16 op {
130 3baa2617 2022-02-16 op int connfd;
131 3baa2617 2022-02-16 op socklen_t len;
132 3baa2617 2022-02-16 op struct sockaddr_un sun;
133 3baa2617 2022-02-16 op struct ctl_conn *c;
134 3baa2617 2022-02-16 op
135 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
136 3baa2617 2022-02-16 op if ((event & EV_TIMEOUT))
137 3baa2617 2022-02-16 op return;
138 3baa2617 2022-02-16 op
139 3baa2617 2022-02-16 op len = sizeof(sun);
140 3baa2617 2022-02-16 op if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
141 3baa2617 2022-02-16 op SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
142 3baa2617 2022-02-16 op /*
143 3baa2617 2022-02-16 op * Pause accept if we are out of file descriptors, or
144 3baa2617 2022-02-16 op * libevent will haunt us here too.
145 3baa2617 2022-02-16 op */
146 3baa2617 2022-02-16 op if (errno == ENFILE || errno == EMFILE) {
147 3baa2617 2022-02-16 op struct timeval evtpause = { 1, 0 };
148 3baa2617 2022-02-16 op
149 3baa2617 2022-02-16 op event_del(&control_state.ev);
150 3baa2617 2022-02-16 op evtimer_add(&control_state.evt, &evtpause);
151 3baa2617 2022-02-16 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
152 3baa2617 2022-02-16 op errno != ECONNABORTED)
153 3baa2617 2022-02-16 op log_warn("%s: accept4", __func__);
154 3baa2617 2022-02-16 op return;
155 3baa2617 2022-02-16 op }
156 3baa2617 2022-02-16 op
157 3baa2617 2022-02-16 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
158 3baa2617 2022-02-16 op log_warn("%s: calloc", __func__);
159 3baa2617 2022-02-16 op close(connfd);
160 3baa2617 2022-02-16 op return;
161 3baa2617 2022-02-16 op }
162 3baa2617 2022-02-16 op
163 3baa2617 2022-02-16 op imsg_init(&c->iev.ibuf, connfd);
164 3baa2617 2022-02-16 op c->iev.handler = control_dispatch_imsg;
165 3baa2617 2022-02-16 op c->iev.events = EV_READ;
166 3baa2617 2022-02-16 op event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
167 3baa2617 2022-02-16 op c->iev.handler, &c->iev);
168 3baa2617 2022-02-16 op event_add(&c->iev.ev, NULL);
169 3baa2617 2022-02-16 op
170 3baa2617 2022-02-16 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
171 3baa2617 2022-02-16 op }
172 3baa2617 2022-02-16 op
173 3baa2617 2022-02-16 op struct ctl_conn *
174 3baa2617 2022-02-16 op control_connbyfd(int fd)
175 3baa2617 2022-02-16 op {
176 3baa2617 2022-02-16 op struct ctl_conn *c;
177 3baa2617 2022-02-16 op
178 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
179 3baa2617 2022-02-16 op if (c->iev.ibuf.fd == fd)
180 3baa2617 2022-02-16 op break;
181 3baa2617 2022-02-16 op }
182 3baa2617 2022-02-16 op
183 3baa2617 2022-02-16 op return (c);
184 3baa2617 2022-02-16 op }
185 3baa2617 2022-02-16 op
186 3baa2617 2022-02-16 op struct ctl_conn *
187 3baa2617 2022-02-16 op control_connbypid(pid_t pid)
188 3baa2617 2022-02-16 op {
189 3baa2617 2022-02-16 op struct ctl_conn *c;
190 3baa2617 2022-02-16 op
191 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
192 3baa2617 2022-02-16 op if (c->iev.ibuf.pid == pid)
193 3baa2617 2022-02-16 op break;
194 3baa2617 2022-02-16 op }
195 3baa2617 2022-02-16 op
196 3baa2617 2022-02-16 op return (c);
197 3baa2617 2022-02-16 op }
198 3baa2617 2022-02-16 op
199 3baa2617 2022-02-16 op void
200 3baa2617 2022-02-16 op control_close(int fd)
201 3baa2617 2022-02-16 op {
202 3baa2617 2022-02-16 op struct ctl_conn *c;
203 3baa2617 2022-02-16 op
204 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
205 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
206 3baa2617 2022-02-16 op return;
207 19d6b480 2022-02-17 op }
208 19d6b480 2022-02-17 op
209 19d6b480 2022-02-17 op /* abort the transaction if running by this user */
210 c6304ab9 2022-02-17 op if (control_state.tx != -1 && c->iev.ibuf.fd == control_state.tx) {
211 19d6b480 2022-02-17 op playlist_free(&control_state.play);
212 19d6b480 2022-02-17 op control_state.tx = -1;
213 3baa2617 2022-02-16 op }
214 3baa2617 2022-02-16 op
215 3baa2617 2022-02-16 op msgbuf_clear(&c->iev.ibuf.w);
216 3baa2617 2022-02-16 op TAILQ_REMOVE(&ctl_conns, c, entry);
217 3baa2617 2022-02-16 op
218 3baa2617 2022-02-16 op event_del(&c->iev.ev);
219 3baa2617 2022-02-16 op close(c->iev.ibuf.fd);
220 3baa2617 2022-02-16 op
221 3baa2617 2022-02-16 op /* Some file descriptors are available again. */
222 3baa2617 2022-02-16 op if (evtimer_pending(&control_state.evt, NULL)) {
223 3baa2617 2022-02-16 op evtimer_del(&control_state.evt);
224 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
225 3baa2617 2022-02-16 op }
226 3baa2617 2022-02-16 op
227 3baa2617 2022-02-16 op free(c);
228 87f575c3 2022-02-21 op }
229 87f575c3 2022-02-21 op
230 87f575c3 2022-02-21 op void
231 87f575c3 2022-02-21 op control_notify(struct imsgev *iev, int type)
232 87f575c3 2022-02-21 op {
233 87f575c3 2022-02-21 op struct ctl_conn *c;
234 87f575c3 2022-02-21 op
235 87f575c3 2022-02-21 op TAILQ_FOREACH(c, &ctl_conns, entry) {
236 87f575c3 2022-02-21 op if (&c->iev == iev || !c->monitor)
237 87f575c3 2022-02-21 op continue;
238 87f575c3 2022-02-21 op
239 87f575c3 2022-02-21 op imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0,
240 87f575c3 2022-02-21 op -1, &type, sizeof(type));
241 87f575c3 2022-02-21 op }
242 3baa2617 2022-02-16 op }
243 3baa2617 2022-02-16 op
244 3baa2617 2022-02-16 op void
245 3baa2617 2022-02-16 op control_dispatch_imsg(int fd, short event, void *bula)
246 3baa2617 2022-02-16 op {
247 310ef57c 2022-02-19 op struct ctl_conn *c;
248 310ef57c 2022-02-19 op struct imsg imsg;
249 310ef57c 2022-02-19 op struct player_repeat rp;
250 3af93963 2022-03-02 op ssize_t n, off;
251 3baa2617 2022-02-16 op
252 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
253 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
254 3baa2617 2022-02-16 op return;
255 3baa2617 2022-02-16 op }
256 3baa2617 2022-02-16 op
257 3baa2617 2022-02-16 op if (event & EV_READ) {
258 3baa2617 2022-02-16 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
259 3baa2617 2022-02-16 op n == 0) {
260 3baa2617 2022-02-16 op control_close(fd);
261 3baa2617 2022-02-16 op return;
262 3baa2617 2022-02-16 op }
263 3baa2617 2022-02-16 op }
264 3baa2617 2022-02-16 op if (event & EV_WRITE) {
265 3baa2617 2022-02-16 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
266 3baa2617 2022-02-16 op control_close(fd);
267 3baa2617 2022-02-16 op return;
268 3baa2617 2022-02-16 op }
269 3baa2617 2022-02-16 op }
270 3baa2617 2022-02-16 op
271 3baa2617 2022-02-16 op for (;;) {
272 3baa2617 2022-02-16 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
273 3baa2617 2022-02-16 op control_close(fd);
274 3baa2617 2022-02-16 op return;
275 3baa2617 2022-02-16 op }
276 3baa2617 2022-02-16 op if (n == 0)
277 3baa2617 2022-02-16 op break;
278 3baa2617 2022-02-16 op
279 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
280 3baa2617 2022-02-16 op case IMSG_CTL_PLAY:
281 3baa2617 2022-02-16 op switch (play_state) {
282 3baa2617 2022-02-16 op case STATE_STOPPED:
283 2f589330 2022-02-17 op main_playlist_resume();
284 3baa2617 2022-02-16 op break;
285 3baa2617 2022-02-16 op case STATE_PLAYING:
286 3baa2617 2022-02-16 op /* do nothing */
287 3baa2617 2022-02-16 op break;
288 3baa2617 2022-02-16 op case STATE_PAUSED:
289 950ad05c 2022-02-16 op play_state = STATE_PLAYING;
290 5d86bc14 2022-05-10 op main_send_player(IMSG_RESUME, -1);
291 3baa2617 2022-02-16 op break;
292 3baa2617 2022-02-16 op }
293 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
294 3baa2617 2022-02-16 op break;
295 3baa2617 2022-02-16 op case IMSG_CTL_TOGGLE_PLAY:
296 3baa2617 2022-02-16 op switch (play_state) {
297 3baa2617 2022-02-16 op case STATE_STOPPED:
298 2f589330 2022-02-17 op main_playlist_resume();
299 3baa2617 2022-02-16 op break;
300 3baa2617 2022-02-16 op case STATE_PLAYING:
301 2ac7eafd 2022-02-16 op play_state = STATE_PAUSED;
302 5d86bc14 2022-05-10 op main_send_player(IMSG_PAUSE, -1);
303 3baa2617 2022-02-16 op break;
304 3baa2617 2022-02-16 op case STATE_PAUSED:
305 2ac7eafd 2022-02-16 op play_state = STATE_PLAYING;
306 5d86bc14 2022-05-10 op main_send_player(IMSG_RESUME, -1);
307 3baa2617 2022-02-16 op break;
308 3baa2617 2022-02-16 op }
309 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
310 3baa2617 2022-02-16 op break;
311 3baa2617 2022-02-16 op case IMSG_CTL_PAUSE:
312 3baa2617 2022-02-16 op if (play_state != STATE_PLAYING)
313 3baa2617 2022-02-16 op break;
314 950ad05c 2022-02-16 op play_state = STATE_PAUSED;
315 5d86bc14 2022-05-10 op main_send_player(IMSG_PAUSE, -1);
316 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
317 3baa2617 2022-02-16 op break;
318 3baa2617 2022-02-16 op case IMSG_CTL_STOP:
319 3baa2617 2022-02-16 op if (play_state == STATE_STOPPED)
320 3baa2617 2022-02-16 op break;
321 bfaa7897 2022-02-17 op play_state = STATE_STOPPED;
322 5d86bc14 2022-05-10 op main_send_player(IMSG_STOP, -1);
323 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
324 3baa2617 2022-02-16 op break;
325 3baa2617 2022-02-16 op case IMSG_CTL_RESTART:
326 5d86bc14 2022-05-10 op main_send_player(IMSG_STOP, -1);
327 aaccc123 2022-02-16 op main_restart_track();
328 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
329 3baa2617 2022-02-16 op break;
330 3baa2617 2022-02-16 op case IMSG_CTL_FLUSH:
331 3baa2617 2022-02-16 op playlist_truncate();
332 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
333 41f5e35a 2022-02-16 op break;
334 3baa2617 2022-02-16 op case IMSG_CTL_SHOW:
335 d980494c 2022-02-16 op main_send_playlist(&c->iev);
336 bb3f279f 2022-02-16 op break;
337 bb3f279f 2022-02-16 op case IMSG_CTL_STATUS:
338 bb3f279f 2022-02-16 op main_send_status(&c->iev);
339 af27e631 2022-02-17 op break;
340 af27e631 2022-02-17 op case IMSG_CTL_NEXT:
341 5d86bc14 2022-05-10 op main_send_player(IMSG_STOP, -1);
342 af27e631 2022-02-17 op main_playlist_advance();
343 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
344 af27e631 2022-02-17 op break;
345 af27e631 2022-02-17 op case IMSG_CTL_PREV:
346 5d86bc14 2022-05-10 op main_send_player(IMSG_STOP, -1);
347 af27e631 2022-02-17 op main_playlist_previous();
348 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
349 d980494c 2022-02-16 op break;
350 a913de21 2022-02-17 op case IMSG_CTL_JUMP:
351 a913de21 2022-02-17 op main_playlist_jump(&c->iev, &imsg);
352 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
353 310ef57c 2022-02-19 op break;
354 310ef57c 2022-02-19 op case IMSG_CTL_REPEAT:
355 310ef57c 2022-02-19 op if (IMSG_DATA_SIZE(imsg) != sizeof(rp)) {
356 310ef57c 2022-02-19 op log_warnx("%s: got wrong size", __func__);
357 310ef57c 2022-02-19 op break;
358 310ef57c 2022-02-19 op }
359 310ef57c 2022-02-19 op memcpy(&rp, imsg.data, sizeof(rp));
360 310ef57c 2022-02-19 op if (rp.repeat_all != -1)
361 310ef57c 2022-02-19 op repeat_all = rp.repeat_all;
362 310ef57c 2022-02-19 op if (rp.repeat_one != -1)
363 310ef57c 2022-02-19 op repeat_one = rp.repeat_one;
364 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
365 a913de21 2022-02-17 op break;
366 19d6b480 2022-02-17 op case IMSG_CTL_BEGIN:
367 19d6b480 2022-02-17 op if (control_state.tx != -1) {
368 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
369 19d6b480 2022-02-17 op break;
370 19d6b480 2022-02-17 op }
371 c6304ab9 2022-02-17 op control_state.tx = c->iev.ibuf.fd;
372 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_BEGIN, 0, 0, -1,
373 19d6b480 2022-02-17 op NULL, 0);
374 19d6b480 2022-02-17 op break;
375 19d6b480 2022-02-17 op case IMSG_CTL_ADD:
376 19d6b480 2022-02-17 op if (control_state.tx != -1 &&
377 c6304ab9 2022-02-17 op control_state.tx != c->iev.ibuf.fd) {
378 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
379 19d6b480 2022-02-17 op break;
380 19d6b480 2022-02-17 op }
381 19d6b480 2022-02-17 op main_enqueue(control_state.tx != -1,
382 19d6b480 2022-02-17 op &control_state.play,&c->iev, &imsg);
383 87f575c3 2022-02-21 op if (control_state.tx == -1)
384 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
385 19d6b480 2022-02-17 op break;
386 19d6b480 2022-02-17 op case IMSG_CTL_COMMIT:
387 c6304ab9 2022-02-17 op if (control_state.tx != c->iev.ibuf.fd) {
388 19d6b480 2022-02-17 op main_senderr(&c->iev, "locked");
389 19d6b480 2022-02-17 op break;
390 19d6b480 2022-02-17 op }
391 3af93963 2022-03-02 op if (IMSG_DATA_SIZE(imsg) != sizeof(off)) {
392 3af93963 2022-03-02 op main_senderr(&c->iev, "wrong size");
393 3af93963 2022-03-02 op break;
394 3af93963 2022-03-02 op }
395 3af93963 2022-03-02 op memcpy(&off, imsg.data, sizeof(off));
396 3af93963 2022-03-02 op playlist_swap(&control_state.play, off);
397 19d6b480 2022-02-17 op memset(&control_state.play, 0,
398 19d6b480 2022-02-17 op sizeof(control_state.play));
399 19d6b480 2022-02-17 op control_state.tx = -1;
400 19d6b480 2022-02-17 op imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1,
401 19d6b480 2022-02-17 op NULL, 0);
402 87f575c3 2022-02-21 op control_notify(&c->iev, imsg.hdr.type);
403 19d6b480 2022-02-17 op break;
404 87f575c3 2022-02-21 op case IMSG_CTL_MONITOR:
405 87f575c3 2022-02-21 op c->monitor = 1;
406 87f575c3 2022-02-21 op break;
407 3baa2617 2022-02-16 op default:
408 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
409 3baa2617 2022-02-16 op imsg.hdr.type);
410 3baa2617 2022-02-16 op break;
411 3baa2617 2022-02-16 op }
412 3baa2617 2022-02-16 op imsg_free(&imsg);
413 3baa2617 2022-02-16 op }
414 3baa2617 2022-02-16 op
415 3baa2617 2022-02-16 op imsg_event_add(&c->iev);
416 3baa2617 2022-02-16 op }
417 3baa2617 2022-02-16 op
418 3baa2617 2022-02-16 op int
419 3baa2617 2022-02-16 op control_imsg_relay(struct imsg *imsg)
420 3baa2617 2022-02-16 op {
421 3baa2617 2022-02-16 op struct ctl_conn *c;
422 3baa2617 2022-02-16 op
423 3baa2617 2022-02-16 op if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
424 3baa2617 2022-02-16 op return (0);
425 3baa2617 2022-02-16 op
426 3baa2617 2022-02-16 op return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
427 3baa2617 2022-02-16 op -1, imsg->data, IMSG_DATA_SIZE(*imsg)));
428 3baa2617 2022-02-16 op }