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 3baa2617 2022-02-16 op #include <stdlib.h>
33 3baa2617 2022-02-16 op #include <string.h>
34 3baa2617 2022-02-16 op #include <unistd.h>
35 3baa2617 2022-02-16 op
36 3baa2617 2022-02-16 op #include "amused.h"
37 3baa2617 2022-02-16 op #include "log.h"
38 3baa2617 2022-02-16 op #include "control.h"
39 3baa2617 2022-02-16 op #include "playlist.h"
40 3baa2617 2022-02-16 op
41 3baa2617 2022-02-16 op #define CONTROL_BACKLOG 5
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op struct {
44 3baa2617 2022-02-16 op struct event ev;
45 3baa2617 2022-02-16 op struct event evt;
46 3baa2617 2022-02-16 op int fd;
47 3baa2617 2022-02-16 op } control_state = {.fd = -1};
48 3baa2617 2022-02-16 op
49 3baa2617 2022-02-16 op struct ctl_conn {
50 3baa2617 2022-02-16 op TAILQ_ENTRY(ctl_conn) entry;
51 3baa2617 2022-02-16 op struct imsgev iev;
52 3baa2617 2022-02-16 op };
53 3baa2617 2022-02-16 op
54 3baa2617 2022-02-16 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
55 3baa2617 2022-02-16 op
56 3baa2617 2022-02-16 op struct ctl_conn *control_connbyfd(int);
57 3baa2617 2022-02-16 op struct ctl_conn *control_connbypid(pid_t);
58 3baa2617 2022-02-16 op void control_close(int);
59 3baa2617 2022-02-16 op
60 3baa2617 2022-02-16 op int
61 3baa2617 2022-02-16 op control_init(char *path)
62 3baa2617 2022-02-16 op {
63 3baa2617 2022-02-16 op struct sockaddr_un sun;
64 3baa2617 2022-02-16 op int fd;
65 3baa2617 2022-02-16 op mode_t old_umask;
66 3baa2617 2022-02-16 op
67 3baa2617 2022-02-16 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
68 3baa2617 2022-02-16 op 0)) == -1) {
69 3baa2617 2022-02-16 op log_warn("%s: socket", __func__);
70 3baa2617 2022-02-16 op return (-1);
71 3baa2617 2022-02-16 op }
72 3baa2617 2022-02-16 op
73 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
74 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
75 3baa2617 2022-02-16 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
76 3baa2617 2022-02-16 op
77 3baa2617 2022-02-16 op if (unlink(path) == -1)
78 3baa2617 2022-02-16 op if (errno != ENOENT) {
79 3baa2617 2022-02-16 op log_warn("%s: unlink %s", __func__, path);
80 3baa2617 2022-02-16 op close(fd);
81 3baa2617 2022-02-16 op return (-1);
82 3baa2617 2022-02-16 op }
83 3baa2617 2022-02-16 op
84 3baa2617 2022-02-16 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
85 3baa2617 2022-02-16 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
86 3baa2617 2022-02-16 op log_warn("%s: bind: %s", __func__, path);
87 3baa2617 2022-02-16 op close(fd);
88 3baa2617 2022-02-16 op umask(old_umask);
89 3baa2617 2022-02-16 op return (-1);
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op umask(old_umask);
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
94 3baa2617 2022-02-16 op log_warn("%s: chmod", __func__);
95 3baa2617 2022-02-16 op close(fd);
96 3baa2617 2022-02-16 op (void)unlink(path);
97 3baa2617 2022-02-16 op return (-1);
98 3baa2617 2022-02-16 op }
99 3baa2617 2022-02-16 op
100 3baa2617 2022-02-16 op return (fd);
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op int
104 3baa2617 2022-02-16 op control_listen(int fd)
105 3baa2617 2022-02-16 op {
106 3baa2617 2022-02-16 op if (control_state.fd != -1)
107 3baa2617 2022-02-16 op fatalx("%s: received unexpected controlsock", __func__);
108 3baa2617 2022-02-16 op
109 3baa2617 2022-02-16 op control_state.fd = fd;
110 3baa2617 2022-02-16 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
111 3baa2617 2022-02-16 op log_warn("%s: listen", __func__);
112 3baa2617 2022-02-16 op return (-1);
113 3baa2617 2022-02-16 op }
114 3baa2617 2022-02-16 op
115 3baa2617 2022-02-16 op event_set(&control_state.ev, control_state.fd, EV_READ,
116 3baa2617 2022-02-16 op control_accept, NULL);
117 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
118 3baa2617 2022-02-16 op evtimer_set(&control_state.evt, control_accept, NULL);
119 3baa2617 2022-02-16 op
120 3baa2617 2022-02-16 op return (0);
121 3baa2617 2022-02-16 op }
122 3baa2617 2022-02-16 op
123 3baa2617 2022-02-16 op void
124 3baa2617 2022-02-16 op control_accept(int listenfd, short event, void *bula)
125 3baa2617 2022-02-16 op {
126 3baa2617 2022-02-16 op int connfd;
127 3baa2617 2022-02-16 op socklen_t len;
128 3baa2617 2022-02-16 op struct sockaddr_un sun;
129 3baa2617 2022-02-16 op struct ctl_conn *c;
130 3baa2617 2022-02-16 op
131 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
132 3baa2617 2022-02-16 op if ((event & EV_TIMEOUT))
133 3baa2617 2022-02-16 op return;
134 3baa2617 2022-02-16 op
135 3baa2617 2022-02-16 op len = sizeof(sun);
136 3baa2617 2022-02-16 op if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
137 3baa2617 2022-02-16 op SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
138 3baa2617 2022-02-16 op /*
139 3baa2617 2022-02-16 op * Pause accept if we are out of file descriptors, or
140 3baa2617 2022-02-16 op * libevent will haunt us here too.
141 3baa2617 2022-02-16 op */
142 3baa2617 2022-02-16 op if (errno == ENFILE || errno == EMFILE) {
143 3baa2617 2022-02-16 op struct timeval evtpause = { 1, 0 };
144 3baa2617 2022-02-16 op
145 3baa2617 2022-02-16 op event_del(&control_state.ev);
146 3baa2617 2022-02-16 op evtimer_add(&control_state.evt, &evtpause);
147 3baa2617 2022-02-16 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
148 3baa2617 2022-02-16 op errno != ECONNABORTED)
149 3baa2617 2022-02-16 op log_warn("%s: accept4", __func__);
150 3baa2617 2022-02-16 op return;
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op
153 3baa2617 2022-02-16 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
154 3baa2617 2022-02-16 op log_warn("%s: calloc", __func__);
155 3baa2617 2022-02-16 op close(connfd);
156 3baa2617 2022-02-16 op return;
157 3baa2617 2022-02-16 op }
158 3baa2617 2022-02-16 op
159 3baa2617 2022-02-16 op imsg_init(&c->iev.ibuf, connfd);
160 3baa2617 2022-02-16 op c->iev.handler = control_dispatch_imsg;
161 3baa2617 2022-02-16 op c->iev.events = EV_READ;
162 3baa2617 2022-02-16 op event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
163 3baa2617 2022-02-16 op c->iev.handler, &c->iev);
164 3baa2617 2022-02-16 op event_add(&c->iev.ev, NULL);
165 3baa2617 2022-02-16 op
166 3baa2617 2022-02-16 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
167 3baa2617 2022-02-16 op }
168 3baa2617 2022-02-16 op
169 3baa2617 2022-02-16 op struct ctl_conn *
170 3baa2617 2022-02-16 op control_connbyfd(int fd)
171 3baa2617 2022-02-16 op {
172 3baa2617 2022-02-16 op struct ctl_conn *c;
173 3baa2617 2022-02-16 op
174 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
175 3baa2617 2022-02-16 op if (c->iev.ibuf.fd == fd)
176 3baa2617 2022-02-16 op break;
177 3baa2617 2022-02-16 op }
178 3baa2617 2022-02-16 op
179 3baa2617 2022-02-16 op return (c);
180 3baa2617 2022-02-16 op }
181 3baa2617 2022-02-16 op
182 3baa2617 2022-02-16 op struct ctl_conn *
183 3baa2617 2022-02-16 op control_connbypid(pid_t pid)
184 3baa2617 2022-02-16 op {
185 3baa2617 2022-02-16 op struct ctl_conn *c;
186 3baa2617 2022-02-16 op
187 3baa2617 2022-02-16 op TAILQ_FOREACH(c, &ctl_conns, entry) {
188 3baa2617 2022-02-16 op if (c->iev.ibuf.pid == pid)
189 3baa2617 2022-02-16 op break;
190 3baa2617 2022-02-16 op }
191 3baa2617 2022-02-16 op
192 3baa2617 2022-02-16 op return (c);
193 3baa2617 2022-02-16 op }
194 3baa2617 2022-02-16 op
195 3baa2617 2022-02-16 op void
196 3baa2617 2022-02-16 op control_close(int fd)
197 3baa2617 2022-02-16 op {
198 3baa2617 2022-02-16 op struct ctl_conn *c;
199 3baa2617 2022-02-16 op
200 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
201 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
202 3baa2617 2022-02-16 op return;
203 3baa2617 2022-02-16 op }
204 3baa2617 2022-02-16 op
205 3baa2617 2022-02-16 op msgbuf_clear(&c->iev.ibuf.w);
206 3baa2617 2022-02-16 op TAILQ_REMOVE(&ctl_conns, c, entry);
207 3baa2617 2022-02-16 op
208 3baa2617 2022-02-16 op event_del(&c->iev.ev);
209 3baa2617 2022-02-16 op close(c->iev.ibuf.fd);
210 3baa2617 2022-02-16 op
211 3baa2617 2022-02-16 op /* Some file descriptors are available again. */
212 3baa2617 2022-02-16 op if (evtimer_pending(&control_state.evt, NULL)) {
213 3baa2617 2022-02-16 op evtimer_del(&control_state.evt);
214 3baa2617 2022-02-16 op event_add(&control_state.ev, NULL);
215 3baa2617 2022-02-16 op }
216 3baa2617 2022-02-16 op
217 3baa2617 2022-02-16 op free(c);
218 3baa2617 2022-02-16 op }
219 3baa2617 2022-02-16 op
220 3baa2617 2022-02-16 op void
221 3baa2617 2022-02-16 op control_dispatch_imsg(int fd, short event, void *bula)
222 3baa2617 2022-02-16 op {
223 3baa2617 2022-02-16 op struct ctl_conn *c;
224 3baa2617 2022-02-16 op struct imsg imsg;
225 3baa2617 2022-02-16 op ssize_t n;
226 3baa2617 2022-02-16 op
227 3baa2617 2022-02-16 op if ((c = control_connbyfd(fd)) == NULL) {
228 3baa2617 2022-02-16 op log_warnx("%s: fd %d: not found", __func__, fd);
229 3baa2617 2022-02-16 op return;
230 3baa2617 2022-02-16 op }
231 3baa2617 2022-02-16 op
232 3baa2617 2022-02-16 op if (event & EV_READ) {
233 3baa2617 2022-02-16 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
234 3baa2617 2022-02-16 op n == 0) {
235 3baa2617 2022-02-16 op control_close(fd);
236 3baa2617 2022-02-16 op return;
237 3baa2617 2022-02-16 op }
238 3baa2617 2022-02-16 op }
239 3baa2617 2022-02-16 op if (event & EV_WRITE) {
240 3baa2617 2022-02-16 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
241 3baa2617 2022-02-16 op control_close(fd);
242 3baa2617 2022-02-16 op return;
243 3baa2617 2022-02-16 op }
244 3baa2617 2022-02-16 op }
245 3baa2617 2022-02-16 op
246 3baa2617 2022-02-16 op for (;;) {
247 3baa2617 2022-02-16 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
248 3baa2617 2022-02-16 op control_close(fd);
249 3baa2617 2022-02-16 op return;
250 3baa2617 2022-02-16 op }
251 3baa2617 2022-02-16 op if (n == 0)
252 3baa2617 2022-02-16 op break;
253 3baa2617 2022-02-16 op
254 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
255 3baa2617 2022-02-16 op case IMSG_CTL_PLAY:
256 3baa2617 2022-02-16 op switch (play_state) {
257 3baa2617 2022-02-16 op case STATE_STOPPED:
258 aaccc123 2022-02-16 op main_playlist_advance();
259 3baa2617 2022-02-16 op break;
260 3baa2617 2022-02-16 op case STATE_PLAYING:
261 3baa2617 2022-02-16 op /* do nothing */
262 3baa2617 2022-02-16 op break;
263 3baa2617 2022-02-16 op case STATE_PAUSED:
264 950ad05c 2022-02-16 op play_state = STATE_PLAYING;
265 06961b72 2022-02-16 op main_send_player(IMSG_RESUME, -1, NULL, 0);
266 3baa2617 2022-02-16 op break;
267 3baa2617 2022-02-16 op }
268 3baa2617 2022-02-16 op break;
269 3baa2617 2022-02-16 op case IMSG_CTL_TOGGLE_PLAY:
270 3baa2617 2022-02-16 op switch (play_state) {
271 3baa2617 2022-02-16 op case STATE_STOPPED:
272 aaccc123 2022-02-16 op main_playlist_advance();
273 3baa2617 2022-02-16 op break;
274 3baa2617 2022-02-16 op case STATE_PLAYING:
275 2ac7eafd 2022-02-16 op play_state = STATE_PAUSED;
276 06961b72 2022-02-16 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
277 3baa2617 2022-02-16 op break;
278 3baa2617 2022-02-16 op case STATE_PAUSED:
279 2ac7eafd 2022-02-16 op play_state = STATE_PLAYING;
280 06961b72 2022-02-16 op main_send_player(IMSG_RESUME, -1, NULL, 0);
281 3baa2617 2022-02-16 op break;
282 3baa2617 2022-02-16 op }
283 3baa2617 2022-02-16 op break;
284 3baa2617 2022-02-16 op case IMSG_CTL_PAUSE:
285 3baa2617 2022-02-16 op if (play_state != STATE_PLAYING)
286 3baa2617 2022-02-16 op break;
287 950ad05c 2022-02-16 op play_state = STATE_PAUSED;
288 06961b72 2022-02-16 op main_send_player(IMSG_PAUSE, -1, NULL, 0);
289 3baa2617 2022-02-16 op break;
290 3baa2617 2022-02-16 op case IMSG_CTL_STOP:
291 3baa2617 2022-02-16 op if (play_state == STATE_STOPPED)
292 3baa2617 2022-02-16 op break;
293 06961b72 2022-02-16 op main_send_player(IMSG_STOP, -1, NULL, 0);
294 3baa2617 2022-02-16 op break;
295 3baa2617 2022-02-16 op case IMSG_CTL_RESTART:
296 06961b72 2022-02-16 op main_send_player(IMSG_STOP, -1, NULL, 0);
297 aaccc123 2022-02-16 op main_restart_track();
298 3baa2617 2022-02-16 op break;
299 3baa2617 2022-02-16 op case IMSG_CTL_ADD:
300 3baa2617 2022-02-16 op main_enqueue(&c->iev, &imsg);
301 3baa2617 2022-02-16 op break;
302 3baa2617 2022-02-16 op case IMSG_CTL_FLUSH:
303 3baa2617 2022-02-16 op playlist_truncate();
304 41f5e35a 2022-02-16 op break;
305 3baa2617 2022-02-16 op case IMSG_CTL_SHOW:
306 d980494c 2022-02-16 op main_send_playlist(&c->iev);
307 bb3f279f 2022-02-16 op break;
308 bb3f279f 2022-02-16 op case IMSG_CTL_STATUS:
309 bb3f279f 2022-02-16 op main_send_status(&c->iev);
310 d980494c 2022-02-16 op break;
311 3baa2617 2022-02-16 op default:
312 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
313 3baa2617 2022-02-16 op imsg.hdr.type);
314 3baa2617 2022-02-16 op break;
315 3baa2617 2022-02-16 op }
316 3baa2617 2022-02-16 op imsg_free(&imsg);
317 3baa2617 2022-02-16 op }
318 3baa2617 2022-02-16 op
319 3baa2617 2022-02-16 op imsg_event_add(&c->iev);
320 3baa2617 2022-02-16 op }
321 3baa2617 2022-02-16 op
322 3baa2617 2022-02-16 op int
323 3baa2617 2022-02-16 op control_imsg_relay(struct imsg *imsg)
324 3baa2617 2022-02-16 op {
325 3baa2617 2022-02-16 op struct ctl_conn *c;
326 3baa2617 2022-02-16 op
327 3baa2617 2022-02-16 op if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
328 3baa2617 2022-02-16 op return (0);
329 3baa2617 2022-02-16 op
330 3baa2617 2022-02-16 op return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
331 3baa2617 2022-02-16 op -1, imsg->data, IMSG_DATA_SIZE(*imsg)));
332 3baa2617 2022-02-16 op }