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 **);
45 struct ctl_command ctl_commands[] = {
46 { "play", PLAY, ctl_noarg, "" },
47 { "pause", PAUSE, ctl_noarg, "" },
48 { "toggle", TOGGLE, ctl_noarg, "" },
49 { "stop", STOP, ctl_noarg, "" },
50 { "restart", RESTART, ctl_noarg, "" },
51 { "add", ADD, ctl_add, "files...", 1 },
52 { "flush", FLUSH, ctl_noarg, "" },
53 { "show", SHOW, ctl_noarg, "" },
54 { "status", STATUS, ctl_noarg, "" },
55 { NULL },
56 };
58 __dead void
59 usage(void)
60 {
61 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
62 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
63 exit(1);
64 }
66 static __dead void
67 ctl_usage(struct ctl_command *ctl)
68 {
69 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
70 ctl->name, ctl->usage);
71 exit(1);
72 }
74 static int
75 parse(int argc, char **argv)
76 {
77 struct ctl_command *ctl = NULL;
78 struct parse_result res;
79 int i, status;
81 memset(&res, 0, sizeof(res));
83 for (i = 0; ctl_commands[i].name != NULL; ++i) {
84 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
85 == 0) {
86 if (ctl != NULL) {
87 fprintf(stderr,
88 "ambiguous argument: %s\n", argv[0]);
89 usage();
90 }
91 ctl = &ctl_commands[i];
92 }
93 }
95 if (ctl == NULL) {
96 fprintf(stderr, "unknown argument: %s\n", argv[0]);
97 usage();
98 }
100 res.action = ctl->action;
101 res.ctl = ctl;
103 if (!ctl->has_pledge) {
104 /* pledge(2) default if command doesn't have its own */
105 if (pledge("stdio", NULL) == -1)
106 fatal("pledge");
109 status = ctl->main(&res, argc, argv);
110 close(ibuf->fd);
111 free(ibuf);
112 return status;
115 static int
116 enqueue_tracks(char **files)
118 char res[PATH_MAX];
119 int enq = 0;
121 for (; *files != NULL; ++files) {
122 memset(&res, 0, sizeof(res));
123 if (realpath(*files, res) == NULL) {
124 log_warn("realpath %s", *files);
125 continue;
128 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
129 res, sizeof(res));
130 enq++;
133 return enq == 0;
136 static void
137 print_error_message(const char *prfx, struct imsg *imsg)
139 size_t datalen;
140 char *msg;
142 datalen = IMSG_DATA_SIZE(*imsg);
143 if ((msg = calloc(1, datalen)) == NULL)
144 fatal("calloc %zu", datalen);
145 memcpy(msg, imsg->data, datalen);
146 if (datalen == 0 || msg[datalen-1] != '\0')
147 fatalx("malformed error message");
149 log_warnx("%s: %s", prfx, msg);
150 free(msg);
153 static int
154 show_add(struct imsg *imsg, int *ret, char ***files)
156 if (**files == NULL) {
157 log_warnx("received more replies than file sent");
158 *ret = 1;
159 return 1;
162 if (imsg->hdr.type == IMSG_CTL_ERR)
163 print_error_message(**files, imsg);
164 else if (imsg->hdr.type == IMSG_CTL_ADD)
165 log_debug("enqueued %s", **files);
166 else
167 fatalx("got invalid message %d", imsg->hdr.type);
169 (*files)++;
170 return (**files) == NULL;
173 static int
174 show_complete(struct imsg *imsg, int *ret)
176 size_t datalen;
177 char path[PATH_MAX];
179 if (imsg->hdr.type == IMSG_CTL_ERR) {
180 print_error_message("show failed", imsg);
181 *ret = 1;
182 return 1;
185 datalen = IMSG_DATA_SIZE(*imsg);
186 if (datalen == 0)
187 return 1;
189 if (datalen != sizeof(path))
190 fatalx("%s: data size mismatch", __func__);
191 memcpy(path, imsg->data, sizeof(path));
192 if (path[datalen-1] != '\0')
193 fatalx("%s: data corrupted?", __func__);
195 printf("%s\n", path);
196 return 0;
199 static int
200 show_status(struct imsg *imsg, int *ret)
202 struct player_status s;
203 size_t datalen;
205 if (imsg->hdr.type == IMSG_CTL_ERR) {
206 print_error_message("show failed", imsg);
207 *ret = 1;
208 return 1;
211 if (imsg->hdr.type != IMSG_CTL_STATUS)
212 fatalx("%s: got wrong reply", __func__);
214 datalen = IMSG_DATA_SIZE(*imsg);
215 if (datalen != sizeof(s))
216 fatalx("%s: data size mismatch", __func__);
217 memcpy(&s, imsg->data, sizeof(s));
218 if (s.path[sizeof(s.path)-1] != '\0')
219 fatalx("%s: data corrupted?", __func__);
221 switch (s.status) {
222 case STATE_STOPPED:
223 printf("stopped ");
224 break;
225 case STATE_PLAYING:
226 printf("playing ");
227 break;
228 case STATE_PAUSED:
229 printf("paused ");
230 break;
231 default:
232 printf("unknown ");
233 break;
236 printf("%s\n", s.path);
237 return 1;
240 static int
241 ctlaction(struct parse_result *res)
243 struct imsg imsg;
244 ssize_t n;
245 int ret = 0, done = 1;
246 char **files;
248 switch (res->action) {
249 case PLAY:
250 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
251 break;
252 case PAUSE:
253 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
254 break;
255 case TOGGLE:
256 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
257 break;
258 case STOP:
259 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
260 break;
261 case RESTART:
262 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
263 break;
264 case ADD:
265 done = 0;
266 files = res->files;
267 ret = enqueue_tracks(res->files);
268 break;
269 case FLUSH:
270 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
271 break;
272 case SHOW:
273 done = 0;
274 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
275 break;
276 case STATUS:
277 done = 0;
278 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
279 break;
280 case NONE:
281 /* action not expected */
282 fatalx("invalid action %u", res->action);
283 break;
286 if (ret != 0)
287 goto end;
289 imsg_flush(ibuf);
291 while (!done) {
292 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
293 fatalx("imsg_read error");
294 if (n == 0)
295 fatalx("pipe closed");
297 while (!done) {
298 if ((n = imsg_get(ibuf, &imsg)) == -1)
299 fatalx("imsg_get error");
300 if (n == 0)
301 break;
303 switch (res->action) {
304 case ADD:
305 done = show_add(&imsg, &ret, &files);
306 break;
307 case SHOW:
308 done = show_complete(&imsg, &ret);
309 break;
310 case STATUS:
311 done = show_status(&imsg, &ret);
312 break;
313 default:
314 done = 1;
315 break;
318 imsg_free(&imsg);
322 end:
323 return ret;
326 int
327 ctl_noarg(struct parse_result *res, int argc, char **argv)
329 if (argc != 1)
330 ctl_usage(res->ctl);
331 return ctlaction(res);
334 int
335 ctl_add(struct parse_result *res, int argc, char **argv)
337 if (argc < 2)
338 ctl_usage(res->ctl);
339 res->files = argv+1;
340 return ctlaction(res);
343 static int
344 sockconn(void)
346 struct sockaddr_un sun;
347 int sock, saved_errno;
349 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
350 fatal("socket");
352 memset(&sun, 0, sizeof(sun));
353 sun.sun_family = AF_UNIX;
354 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
355 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
356 saved_errno = errno;
357 close(sock);
358 errno = saved_errno;
359 return -1;
362 return sock;
365 __dead void
366 ctl(int argc, char **argv)
368 int ctl_sock, i = 0;
370 log_init(1, LOG_DAEMON);
371 log_setverbose(verbose);
373 do {
374 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
376 if ((ctl_sock = sockconn()) != -1)
377 break;
378 if (errno != ENOENT && errno != ECONNREFUSED)
379 fatal("connect %s", csock);
381 if (i == 0)
382 spawn_daemon();
384 nanosleep(&ts, NULL);
385 } while (++i < 20);
387 if (ctl_sock == -1)
388 fatalx("failed to connect to the daemon");
390 ibuf = xmalloc(sizeof(*ibuf));
391 imsg_init(ibuf, ctl_sock);
393 optreset = 1;
394 optind = 1;
396 exit(parse(argc, argv));