Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/un.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <limits.h>
26 #include <sndio.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <unistd.h>
33 #include <imsg.h>
35 #include "amused.h"
36 #include "log.h"
37 #include "playlist.h"
38 #include "xmalloc.h"
40 static struct imsgbuf *ibuf;
42 int ctl_noarg(struct parse_result *, int, char **);
43 int ctl_add(struct parse_result *, int, char **);
44 int ctl_load(struct parse_result *, int, char **);
46 struct ctl_command ctl_commands[] = {
47 { "play", PLAY, ctl_noarg, "" },
48 { "pause", PAUSE, ctl_noarg, "" },
49 { "toggle", TOGGLE, ctl_noarg, "" },
50 { "stop", STOP, ctl_noarg, "" },
51 { "restart", RESTART, ctl_noarg, "" },
52 { "add", ADD, ctl_add, "files...", 1 },
53 { "flush", FLUSH, ctl_noarg, "" },
54 { "show", SHOW, ctl_noarg, "" },
55 { "status", STATUS, ctl_noarg, "" },
56 { "next", NEXT, ctl_noarg, "" },
57 { "prev", PREV, ctl_noarg, "" },
58 { "load", LOAD, ctl_load, "[file]", 1 },
59 { NULL },
60 };
62 __dead void
63 usage(void)
64 {
65 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
66 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
67 exit(1);
68 }
70 static __dead void
71 ctl_usage(struct ctl_command *ctl)
72 {
73 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
74 ctl->name, ctl->usage);
75 exit(1);
76 }
78 static int
79 parse(int argc, char **argv)
80 {
81 struct ctl_command *ctl = NULL;
82 struct parse_result res;
83 int i, status;
85 memset(&res, 0, sizeof(res));
87 for (i = 0; ctl_commands[i].name != NULL; ++i) {
88 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
89 == 0) {
90 if (ctl != NULL) {
91 fprintf(stderr,
92 "ambiguous argument: %s\n", argv[0]);
93 usage();
94 }
95 ctl = &ctl_commands[i];
96 }
97 }
99 if (ctl == NULL) {
100 fprintf(stderr, "unknown argument: %s\n", argv[0]);
101 usage();
104 res.action = ctl->action;
105 res.ctl = ctl;
107 if (!ctl->has_pledge) {
108 /* pledge(2) default if command doesn't have its own */
109 if (pledge("stdio", NULL) == -1)
110 fatal("pledge");
113 status = ctl->main(&res, argc, argv);
114 close(ibuf->fd);
115 free(ibuf);
116 return status;
119 static int
120 enqueue_tracks(char **files)
122 char res[PATH_MAX];
123 int enq = 0;
125 for (; *files != NULL; ++files) {
126 memset(&res, 0, sizeof(res));
127 if (realpath(*files, res) == NULL) {
128 log_warn("realpath %s", *files);
129 continue;
132 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
133 res, sizeof(res));
134 enq++;
137 return enq == 0;
140 static void
141 print_error_message(const char *prfx, struct imsg *imsg)
143 size_t datalen;
144 char *msg;
146 datalen = IMSG_DATA_SIZE(*imsg);
147 if ((msg = calloc(1, datalen)) == NULL)
148 fatal("calloc %zu", datalen);
149 memcpy(msg, imsg->data, datalen);
150 if (datalen == 0 || msg[datalen-1] != '\0')
151 fatalx("malformed error message");
153 log_warnx("%s: %s", prfx, msg);
154 free(msg);
157 static int
158 show_add(struct imsg *imsg, int *ret, char ***files)
160 if (**files == NULL) {
161 log_warnx("received more replies than file sent");
162 *ret = 1;
163 return 1;
166 if (imsg->hdr.type == IMSG_CTL_ERR)
167 print_error_message(**files, imsg);
168 else if (imsg->hdr.type == IMSG_CTL_ADD)
169 log_debug("enqueued %s", **files);
170 else
171 fatalx("got invalid message %d", imsg->hdr.type);
173 (*files)++;
174 return (**files) == NULL;
177 static int
178 show_complete(struct imsg *imsg, int *ret)
180 struct player_status s;
181 size_t datalen;
183 if (imsg->hdr.type == IMSG_CTL_ERR) {
184 print_error_message("show failed", imsg);
185 *ret = 1;
186 return 1;
189 datalen = IMSG_DATA_SIZE(*imsg);
190 if (datalen == 0)
191 return 1;
193 if (datalen != sizeof(s))
194 fatalx("%s: data size mismatch", __func__);
195 memcpy(&s, imsg->data, sizeof(s));
196 if (s.path[sizeof(s.path)-1] != '\0')
197 fatalx("%s: data corrupted?", __func__);
199 printf("%s %s\n", s.status == STATE_PLAYING ? ">" : " ", s.path);
200 return 0;
203 static int
204 show_status(struct imsg *imsg, int *ret)
206 struct player_status s;
207 size_t datalen;
209 if (imsg->hdr.type == IMSG_CTL_ERR) {
210 print_error_message("show failed", imsg);
211 *ret = 1;
212 return 1;
215 if (imsg->hdr.type != IMSG_CTL_STATUS)
216 fatalx("%s: got wrong reply", __func__);
218 datalen = IMSG_DATA_SIZE(*imsg);
219 if (datalen != sizeof(s))
220 fatalx("%s: data size mismatch", __func__);
221 memcpy(&s, imsg->data, sizeof(s));
222 if (s.path[sizeof(s.path)-1] != '\0')
223 fatalx("%s: data corrupted?", __func__);
225 switch (s.status) {
226 case STATE_STOPPED:
227 printf("stopped ");
228 break;
229 case STATE_PLAYING:
230 printf("playing ");
231 break;
232 case STATE_PAUSED:
233 printf("paused ");
234 break;
235 default:
236 printf("unknown ");
237 break;
240 printf("%s\n", s.path);
241 return 1;
244 static int
245 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
247 const char *file;
248 char *line = NULL;
249 char path[PATH_MAX];
250 size_t linesize = 0;
251 ssize_t linelen;
252 int any = 0;
254 if (imsg->hdr.type == IMSG_CTL_ERR) {
255 print_error_message("load failed", imsg);
256 *ret = 1;
257 return 1;
260 if (imsg->hdr.type == IMSG_CTL_ADD)
261 return 0;
263 if (imsg->hdr.type == IMSG_CTL_COMMIT)
264 return 1;
266 if (imsg->hdr.type != IMSG_CTL_BEGIN)
267 fatalx("got unexpected message %d", imsg->hdr.type);
269 while ((linelen = getline(&line, &linesize, res->file)) != -1) {
270 if (linelen == 0)
271 continue;
272 line[linelen-1] = '\0';
273 file = line;
274 if (file[0] == '>' && file[1] == ' ')
275 file += 2;
276 if (file[0] == ' ' && file[1] == ' ')
277 file += 2;
279 memset(&path, 0, sizeof(path));
280 if (realpath(file, path) == NULL) {
281 log_warn("realpath %s", file);
282 continue;
285 any++;
286 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
287 path, sizeof(path));
290 free(line);
291 if (ferror(res->file))
292 fatal("getline");
293 fclose(res->file);
294 res->file = NULL;
296 if (!any) {
297 *ret = 1;
298 return 1;
301 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
302 NULL, 0);
303 imsg_flush(ibuf);
304 return 0;
307 static int
308 ctlaction(struct parse_result *res)
310 struct imsg imsg;
311 ssize_t n;
312 int ret = 0, done = 1;
313 char **files;
315 switch (res->action) {
316 case PLAY:
317 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
318 break;
319 case PAUSE:
320 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
321 break;
322 case TOGGLE:
323 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
324 break;
325 case STOP:
326 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
327 break;
328 case RESTART:
329 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
330 break;
331 case ADD:
332 done = 0;
333 files = res->files;
334 ret = enqueue_tracks(res->files);
335 break;
336 case FLUSH:
337 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
338 break;
339 case SHOW:
340 done = 0;
341 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
342 break;
343 case STATUS:
344 done = 0;
345 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 break;
347 case NEXT:
348 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
349 break;
350 case PREV:
351 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
352 break;
353 case LOAD:
354 done = 0;
355 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
356 break;
357 case NONE:
358 /* action not expected */
359 fatalx("invalid action %u", res->action);
360 break;
363 if (ret != 0)
364 goto end;
366 imsg_flush(ibuf);
368 while (!done) {
369 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
370 fatalx("imsg_read error");
371 if (n == 0)
372 fatalx("pipe closed");
374 while (!done) {
375 if ((n = imsg_get(ibuf, &imsg)) == -1)
376 fatalx("imsg_get error");
377 if (n == 0)
378 break;
380 switch (res->action) {
381 case ADD:
382 done = show_add(&imsg, &ret, &files);
383 break;
384 case SHOW:
385 done = show_complete(&imsg, &ret);
386 break;
387 case STATUS:
388 done = show_status(&imsg, &ret);
389 break;
390 case LOAD:
391 done = show_load(res, &imsg, &ret);
392 break;
393 default:
394 done = 1;
395 break;
398 imsg_free(&imsg);
402 end:
403 return ret;
406 int
407 ctl_noarg(struct parse_result *res, int argc, char **argv)
409 if (argc != 1)
410 ctl_usage(res->ctl);
411 return ctlaction(res);
414 int
415 ctl_add(struct parse_result *res, int argc, char **argv)
417 if (argc < 2)
418 ctl_usage(res->ctl);
419 res->files = argv+1;
421 if (pledge("stdio rpath", NULL) == -1)
422 fatal("pledge");
424 return ctlaction(res);
427 int
428 ctl_load(struct parse_result *res, int argc, char **argv)
430 if (argc < 2)
431 res->file = stdin;
432 else if (argc == 2) {
433 if ((res->file = fopen(argv[1], "r")) == NULL)
434 fatal("open %s", argv[1]);
435 } else
436 ctl_usage(res->ctl);
438 if (pledge("stdio rpath", NULL) == -1)
439 fatal("pledge");
441 return ctlaction(res);
444 static int
445 sockconn(void)
447 struct sockaddr_un sun;
448 int sock, saved_errno;
450 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
451 fatal("socket");
453 memset(&sun, 0, sizeof(sun));
454 sun.sun_family = AF_UNIX;
455 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
456 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
457 saved_errno = errno;
458 close(sock);
459 errno = saved_errno;
460 return -1;
463 return sock;
466 __dead void
467 ctl(int argc, char **argv)
469 int ctl_sock, i = 0;
471 log_init(1, LOG_DAEMON);
472 log_setverbose(verbose);
474 do {
475 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
477 if ((ctl_sock = sockconn()) != -1)
478 break;
479 if (errno != ENOENT && errno != ECONNREFUSED)
480 fatal("connect %s", csock);
482 if (i == 0)
483 spawn_daemon();
485 nanosleep(&ts, NULL);
486 } while (++i < 20);
488 if (ctl_sock == -1)
489 fatalx("failed to connect to the daemon");
491 ibuf = xmalloc(sizeof(*ibuf));
492 imsg_init(ibuf, ctl_sock);
494 optreset = 1;
495 optind = 1;
497 exit(parse(argc, argv));