Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/socket.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
22 3baa2617 2022-02-16 op
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <sndio.h>
27 3baa2617 2022-02-16 op #include <stdio.h>
28 3baa2617 2022-02-16 op #include <stdlib.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <string.h>
31 3baa2617 2022-02-16 op #include <syslog.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
34 3baa2617 2022-02-16 op
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "log.h"
37 3baa2617 2022-02-16 op #include "xmalloc.h"
38 3baa2617 2022-02-16 op
39 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
40 3baa2617 2022-02-16 op
41 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
42 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
43 3baa2617 2022-02-16 op
44 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
45 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
46 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
47 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
48 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
49 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
50 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
51 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
52 d980494c 2022-02-16 op { "show", SHOW, ctl_noarg, "" },
53 3baa2617 2022-02-16 op { NULL },
54 3baa2617 2022-02-16 op };
55 3baa2617 2022-02-16 op
56 3baa2617 2022-02-16 op __dead void
57 3baa2617 2022-02-16 op usage(void)
58 3baa2617 2022-02-16 op {
59 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
60 3baa2617 2022-02-16 op fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
61 3baa2617 2022-02-16 op exit(1);
62 3baa2617 2022-02-16 op }
63 3baa2617 2022-02-16 op
64 3baa2617 2022-02-16 op static __dead void
65 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
66 3baa2617 2022-02-16 op {
67 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
68 3baa2617 2022-02-16 op ctl->name, ctl->usage);
69 3baa2617 2022-02-16 op exit(1);
70 3baa2617 2022-02-16 op }
71 3baa2617 2022-02-16 op
72 3baa2617 2022-02-16 op static int
73 3baa2617 2022-02-16 op parse(int argc, char **argv)
74 3baa2617 2022-02-16 op {
75 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
76 3baa2617 2022-02-16 op struct parse_result res;
77 3baa2617 2022-02-16 op int i, status;
78 3baa2617 2022-02-16 op
79 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
82 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
83 3baa2617 2022-02-16 op == 0) {
84 3baa2617 2022-02-16 op if (ctl != NULL) {
85 3baa2617 2022-02-16 op fprintf(stderr,
86 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
87 3baa2617 2022-02-16 op usage();
88 3baa2617 2022-02-16 op }
89 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op }
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op if (ctl == NULL) {
94 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
95 3baa2617 2022-02-16 op usage();
96 3baa2617 2022-02-16 op }
97 3baa2617 2022-02-16 op
98 3baa2617 2022-02-16 op res.action = ctl->action;
99 3baa2617 2022-02-16 op res.ctl = ctl;
100 3baa2617 2022-02-16 op
101 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
102 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
103 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
104 3baa2617 2022-02-16 op fatal("pledge");
105 3baa2617 2022-02-16 op }
106 3baa2617 2022-02-16 op
107 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
108 3baa2617 2022-02-16 op close(ibuf->fd);
109 3baa2617 2022-02-16 op free(ibuf);
110 3baa2617 2022-02-16 op return status;
111 3baa2617 2022-02-16 op }
112 3baa2617 2022-02-16 op
113 3baa2617 2022-02-16 op static int
114 3baa2617 2022-02-16 op enqueue_tracks(char **files)
115 3baa2617 2022-02-16 op {
116 3baa2617 2022-02-16 op char res[PATH_MAX];
117 3baa2617 2022-02-16 op int enq = 0;
118 3baa2617 2022-02-16 op
119 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
120 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
121 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
122 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
123 3baa2617 2022-02-16 op continue;
124 3baa2617 2022-02-16 op }
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
127 3baa2617 2022-02-16 op res, sizeof(res));
128 3baa2617 2022-02-16 op enq++;
129 3baa2617 2022-02-16 op }
130 3baa2617 2022-02-16 op
131 3baa2617 2022-02-16 op return enq == 0;
132 3baa2617 2022-02-16 op }
133 3baa2617 2022-02-16 op
134 8ff9231f 2022-02-16 op static void
135 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
136 8ff9231f 2022-02-16 op {
137 8ff9231f 2022-02-16 op size_t datalen;
138 8ff9231f 2022-02-16 op char *msg;
139 8ff9231f 2022-02-16 op
140 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
141 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
142 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
143 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
144 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
145 8ff9231f 2022-02-16 op fatalx("malformed error message");
146 8ff9231f 2022-02-16 op
147 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
148 8ff9231f 2022-02-16 op free(msg);
149 8ff9231f 2022-02-16 op }
150 8ff9231f 2022-02-16 op
151 3baa2617 2022-02-16 op static int
152 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
153 8ff9231f 2022-02-16 op {
154 8ff9231f 2022-02-16 op if (**files == NULL) {
155 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
156 8ff9231f 2022-02-16 op *ret = 1;
157 8ff9231f 2022-02-16 op return 1;
158 8ff9231f 2022-02-16 op }
159 8ff9231f 2022-02-16 op
160 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
161 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
162 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
163 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
164 8ff9231f 2022-02-16 op else
165 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
166 8ff9231f 2022-02-16 op
167 8ff9231f 2022-02-16 op (*files)++;
168 8ff9231f 2022-02-16 op return (**files) == NULL;
169 8ff9231f 2022-02-16 op }
170 8ff9231f 2022-02-16 op
171 8ff9231f 2022-02-16 op static int
172 3baa2617 2022-02-16 op show_complete(struct imsg *imsg, int *ret)
173 3baa2617 2022-02-16 op {
174 3baa2617 2022-02-16 op size_t datalen;
175 3baa2617 2022-02-16 op char path[PATH_MAX];
176 3baa2617 2022-02-16 op
177 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
178 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
179 8ff9231f 2022-02-16 op *ret = 1;
180 8ff9231f 2022-02-16 op return 1;
181 8ff9231f 2022-02-16 op }
182 8ff9231f 2022-02-16 op
183 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
184 3baa2617 2022-02-16 op if (datalen == 0)
185 3baa2617 2022-02-16 op return 1;
186 3baa2617 2022-02-16 op
187 3baa2617 2022-02-16 op if (datalen != sizeof(path))
188 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
189 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
190 3baa2617 2022-02-16 op if (path[datalen-1] != '\0')
191 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
192 3baa2617 2022-02-16 op
193 3baa2617 2022-02-16 op printf("%s\n", path);
194 3baa2617 2022-02-16 op return 0;
195 3baa2617 2022-02-16 op }
196 3baa2617 2022-02-16 op
197 3baa2617 2022-02-16 op static int
198 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
199 3baa2617 2022-02-16 op {
200 3baa2617 2022-02-16 op struct imsg imsg;
201 3baa2617 2022-02-16 op ssize_t n;
202 3baa2617 2022-02-16 op int ret = 0, done = 1;
203 8ff9231f 2022-02-16 op char **files;
204 3baa2617 2022-02-16 op
205 3baa2617 2022-02-16 op switch (res->action) {
206 3baa2617 2022-02-16 op case PLAY:
207 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
208 3baa2617 2022-02-16 op break;
209 3baa2617 2022-02-16 op case PAUSE:
210 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
211 3baa2617 2022-02-16 op break;
212 3baa2617 2022-02-16 op case TOGGLE:
213 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
214 3baa2617 2022-02-16 op break;
215 3baa2617 2022-02-16 op case STOP:
216 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
217 3baa2617 2022-02-16 op break;
218 3baa2617 2022-02-16 op case RESTART:
219 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
220 3baa2617 2022-02-16 op break;
221 3baa2617 2022-02-16 op case ADD:
222 8ff9231f 2022-02-16 op done = 0;
223 8ff9231f 2022-02-16 op files = res->files;
224 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
225 3baa2617 2022-02-16 op break;
226 3baa2617 2022-02-16 op case FLUSH:
227 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
228 3baa2617 2022-02-16 op break;
229 3baa2617 2022-02-16 op case SHOW:
230 3baa2617 2022-02-16 op done = 0;
231 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
232 3baa2617 2022-02-16 op break;
233 3baa2617 2022-02-16 op case NONE:
234 3baa2617 2022-02-16 op /* action not expected */
235 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
236 3baa2617 2022-02-16 op break;
237 3baa2617 2022-02-16 op }
238 3baa2617 2022-02-16 op
239 3baa2617 2022-02-16 op if (ret != 0)
240 3baa2617 2022-02-16 op goto end;
241 3baa2617 2022-02-16 op
242 3baa2617 2022-02-16 op imsg_flush(ibuf);
243 3baa2617 2022-02-16 op
244 3baa2617 2022-02-16 op while (!done) {
245 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
246 3baa2617 2022-02-16 op fatalx("imsg_read error");
247 3baa2617 2022-02-16 op if (n == 0)
248 3baa2617 2022-02-16 op fatalx("pipe closed");
249 3baa2617 2022-02-16 op
250 3baa2617 2022-02-16 op while (!done) {
251 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
252 3baa2617 2022-02-16 op fatalx("imsg_get error");
253 3baa2617 2022-02-16 op if (n == 0)
254 3baa2617 2022-02-16 op break;
255 3baa2617 2022-02-16 op
256 3baa2617 2022-02-16 op switch (res->action) {
257 8ff9231f 2022-02-16 op case ADD:
258 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
259 8ff9231f 2022-02-16 op break;
260 3baa2617 2022-02-16 op case SHOW:
261 3baa2617 2022-02-16 op done = show_complete(&imsg, &ret);
262 3baa2617 2022-02-16 op break;
263 3baa2617 2022-02-16 op default:
264 3baa2617 2022-02-16 op done = 1;
265 3baa2617 2022-02-16 op break;
266 3baa2617 2022-02-16 op }
267 3baa2617 2022-02-16 op
268 3baa2617 2022-02-16 op imsg_free(&imsg);
269 3baa2617 2022-02-16 op }
270 3baa2617 2022-02-16 op }
271 3baa2617 2022-02-16 op
272 3baa2617 2022-02-16 op end:
273 3baa2617 2022-02-16 op return ret;
274 3baa2617 2022-02-16 op }
275 3baa2617 2022-02-16 op
276 3baa2617 2022-02-16 op int
277 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
278 3baa2617 2022-02-16 op {
279 3baa2617 2022-02-16 op if (argc != 1)
280 3baa2617 2022-02-16 op ctl_usage(res->ctl);
281 3baa2617 2022-02-16 op return ctlaction(res);
282 3baa2617 2022-02-16 op }
283 3baa2617 2022-02-16 op
284 3baa2617 2022-02-16 op int
285 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
286 3baa2617 2022-02-16 op {
287 3baa2617 2022-02-16 op if (argc < 2)
288 3baa2617 2022-02-16 op ctl_usage(res->ctl);
289 3baa2617 2022-02-16 op res->files = argv+1;
290 3baa2617 2022-02-16 op return ctlaction(res);
291 3baa2617 2022-02-16 op }
292 3baa2617 2022-02-16 op
293 fea541a8 2022-02-16 op static int
294 fea541a8 2022-02-16 op sockconn(void)
295 3baa2617 2022-02-16 op {
296 fea541a8 2022-02-16 op struct sockaddr_un sun;
297 fea541a8 2022-02-16 op int sock, saved_errno;
298 3baa2617 2022-02-16 op
299 fea541a8 2022-02-16 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
300 3baa2617 2022-02-16 op fatal("socket");
301 3baa2617 2022-02-16 op
302 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
303 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
304 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
305 fea541a8 2022-02-16 op if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
306 fea541a8 2022-02-16 op saved_errno = errno;
307 fea541a8 2022-02-16 op close(sock);
308 fea541a8 2022-02-16 op errno = saved_errno;
309 fea541a8 2022-02-16 op return -1;
310 fea541a8 2022-02-16 op }
311 3baa2617 2022-02-16 op
312 fea541a8 2022-02-16 op return sock;
313 fea541a8 2022-02-16 op }
314 fea541a8 2022-02-16 op
315 fea541a8 2022-02-16 op __dead void
316 fea541a8 2022-02-16 op ctl(int argc, char **argv)
317 fea541a8 2022-02-16 op {
318 fea541a8 2022-02-16 op int ctl_sock, i = 0;
319 3baa2617 2022-02-16 op
320 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
321 fea541a8 2022-02-16 op log_setverbose(verbose);
322 fea541a8 2022-02-16 op
323 fea541a8 2022-02-16 op do {
324 fea541a8 2022-02-16 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
325 fea541a8 2022-02-16 op
326 fea541a8 2022-02-16 op if ((ctl_sock = sockconn()) != -1)
327 fea541a8 2022-02-16 op break;
328 fea541a8 2022-02-16 op if (errno != ENOENT && errno != ECONNREFUSED)
329 fea541a8 2022-02-16 op fatal("connect %s", csock);
330 fea541a8 2022-02-16 op
331 fea541a8 2022-02-16 op if (i == 0)
332 fea541a8 2022-02-16 op spawn_daemon();
333 fea541a8 2022-02-16 op
334 fea541a8 2022-02-16 op nanosleep(&ts, NULL);
335 fea541a8 2022-02-16 op } while (++i < 20);
336 fea541a8 2022-02-16 op
337 fea541a8 2022-02-16 op if (ctl_sock == -1)
338 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
339 fea541a8 2022-02-16 op
340 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
341 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
342 3baa2617 2022-02-16 op
343 3baa2617 2022-02-16 op optreset = 1;
344 3baa2617 2022-02-16 op optind = 1;
345 3baa2617 2022-02-16 op
346 3baa2617 2022-02-16 op exit(parse(argc, argv));
347 3baa2617 2022-02-16 op }