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 "xmalloc.h"
39 static struct imsgbuf *ibuf;
41 int ctl_noarg(struct parse_result *, int, char **);
42 int ctl_add(struct parse_result *, int, char **);
44 struct ctl_command ctl_commands[] = {
45 { "play", PLAY, ctl_noarg, "" },
46 { "pause", PAUSE, ctl_noarg, "" },
47 { "toggle", TOGGLE, ctl_noarg, "" },
48 { "stop", STOP, ctl_noarg, "" },
49 { "restart", RESTART, ctl_noarg, "" },
50 { "add", ADD, ctl_add, "files...", 1 },
51 { "flush", FLUSH, ctl_noarg, "" },
52 { "show", SHOW, ctl_noarg, "" },
53 { NULL },
54 };
56 __dead void
57 usage(void)
58 {
59 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
60 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
61 exit(1);
62 }
64 static __dead void
65 ctl_usage(struct ctl_command *ctl)
66 {
67 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
68 ctl->name, ctl->usage);
69 exit(1);
70 }
72 static int
73 parse(int argc, char **argv)
74 {
75 struct ctl_command *ctl = NULL;
76 struct parse_result res;
77 int i, status;
79 memset(&res, 0, sizeof(res));
81 for (i = 0; ctl_commands[i].name != NULL; ++i) {
82 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
83 == 0) {
84 if (ctl != NULL) {
85 fprintf(stderr,
86 "ambiguous argument: %s\n", argv[0]);
87 usage();
88 }
89 ctl = &ctl_commands[i];
90 }
91 }
93 if (ctl == NULL) {
94 fprintf(stderr, "unknown argument: %s\n", argv[0]);
95 usage();
96 }
98 res.action = ctl->action;
99 res.ctl = ctl;
101 if (!ctl->has_pledge) {
102 /* pledge(2) default if command doesn't have its own */
103 if (pledge("stdio", NULL) == -1)
104 fatal("pledge");
107 status = ctl->main(&res, argc, argv);
108 close(ibuf->fd);
109 free(ibuf);
110 return status;
113 static int
114 enqueue_tracks(char **files)
116 char res[PATH_MAX];
117 int enq = 0;
119 for (; *files != NULL; ++files) {
120 memset(&res, 0, sizeof(res));
121 if (realpath(*files, res) == NULL) {
122 log_warn("realpath %s", *files);
123 continue;
126 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
127 res, sizeof(res));
128 enq++;
131 return enq == 0;
134 static void
135 print_error_message(const char *prfx, struct imsg *imsg)
137 size_t datalen;
138 char *msg;
140 datalen = IMSG_DATA_SIZE(*imsg);
141 if ((msg = calloc(1, datalen)) == NULL)
142 fatal("calloc %zu", datalen);
143 memcpy(msg, imsg->data, datalen);
144 if (datalen == 0 || msg[datalen-1] != '\0')
145 fatalx("malformed error message");
147 log_warnx("%s: %s", prfx, msg);
148 free(msg);
151 static int
152 show_add(struct imsg *imsg, int *ret, char ***files)
154 if (**files == NULL) {
155 log_warnx("received more replies than file sent");
156 *ret = 1;
157 return 1;
160 if (imsg->hdr.type == IMSG_CTL_ERR)
161 print_error_message(**files, imsg);
162 else if (imsg->hdr.type == IMSG_CTL_ADD)
163 log_debug("enqueued %s", **files);
164 else
165 fatalx("got invalid message %d", imsg->hdr.type);
167 (*files)++;
168 return (**files) == NULL;
171 static int
172 show_complete(struct imsg *imsg, int *ret)
174 size_t datalen;
175 char path[PATH_MAX];
177 if (imsg->hdr.type == IMSG_CTL_ERR) {
178 print_error_message("show failed", imsg);
179 *ret = 1;
180 return 1;
183 datalen = IMSG_DATA_SIZE(*imsg);
184 if (datalen == 0)
185 return 1;
187 if (datalen != sizeof(path))
188 fatalx("%s: data size mismatch", __func__);
189 memcpy(path, imsg->data, sizeof(path));
190 if (path[datalen-1] != '\0')
191 fatalx("%s: data corrupted?", __func__);
193 printf("%s\n", path);
194 return 0;
197 static int
198 ctlaction(struct parse_result *res)
200 struct imsg imsg;
201 ssize_t n;
202 int ret = 0, done = 1;
203 char **files;
205 switch (res->action) {
206 case PLAY:
207 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
208 break;
209 case PAUSE:
210 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
211 break;
212 case TOGGLE:
213 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
214 break;
215 case STOP:
216 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
217 break;
218 case RESTART:
219 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
220 break;
221 case ADD:
222 done = 0;
223 files = res->files;
224 ret = enqueue_tracks(res->files);
225 break;
226 case FLUSH:
227 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
228 break;
229 case SHOW:
230 done = 0;
231 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
232 break;
233 case NONE:
234 /* action not expected */
235 fatalx("invalid action %u", res->action);
236 break;
239 if (ret != 0)
240 goto end;
242 imsg_flush(ibuf);
244 while (!done) {
245 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
246 fatalx("imsg_read error");
247 if (n == 0)
248 fatalx("pipe closed");
250 while (!done) {
251 if ((n = imsg_get(ibuf, &imsg)) == -1)
252 fatalx("imsg_get error");
253 if (n == 0)
254 break;
256 switch (res->action) {
257 case ADD:
258 done = show_add(&imsg, &ret, &files);
259 break;
260 case SHOW:
261 done = show_complete(&imsg, &ret);
262 break;
263 default:
264 done = 1;
265 break;
268 imsg_free(&imsg);
272 end:
273 return ret;
276 int
277 ctl_noarg(struct parse_result *res, int argc, char **argv)
279 if (argc != 1)
280 ctl_usage(res->ctl);
281 return ctlaction(res);
284 int
285 ctl_add(struct parse_result *res, int argc, char **argv)
287 if (argc < 2)
288 ctl_usage(res->ctl);
289 res->files = argv+1;
290 return ctlaction(res);
293 static int
294 sockconn(void)
296 struct sockaddr_un sun;
297 int sock, saved_errno;
299 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
300 fatal("socket");
302 memset(&sun, 0, sizeof(sun));
303 sun.sun_family = AF_UNIX;
304 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
305 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
306 saved_errno = errno;
307 close(sock);
308 errno = saved_errno;
309 return -1;
312 return sock;
315 __dead void
316 ctl(int argc, char **argv)
318 int ctl_sock, i = 0;
320 log_init(1, LOG_DAEMON);
321 log_setverbose(verbose);
323 do {
324 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
326 if ((ctl_sock = sockconn()) != -1)
327 break;
328 if (errno != ENOENT && errno != ECONNREFUSED)
329 fatal("connect %s", csock);
331 if (i == 0)
332 spawn_daemon();
334 nanosleep(&ts, NULL);
335 } while (++i < 20);
337 if (ctl_sock == -1)
338 fatalx("failed to connect to the daemon");
340 ibuf = xmalloc(sizeof(*ibuf));
341 imsg_init(ibuf, ctl_sock);
343 optreset = 1;
344 optind = 1;
346 exit(parse(argc, argv));