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 { NULL },
53 };
55 __dead void
56 usage(void)
57 {
58 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
59 fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
60 exit(1);
61 }
63 static __dead void
64 ctl_usage(struct ctl_command *ctl)
65 {
66 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
67 ctl->name, ctl->usage);
68 exit(1);
69 }
71 static int
72 parse(int argc, char **argv)
73 {
74 struct ctl_command *ctl = NULL;
75 struct parse_result res;
76 int i, status;
78 memset(&res, 0, sizeof(res));
80 for (i = 0; ctl_commands[i].name != NULL; ++i) {
81 if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
82 == 0) {
83 if (ctl != NULL) {
84 fprintf(stderr,
85 "ambiguous argument: %s\n", argv[0]);
86 usage();
87 }
88 ctl = &ctl_commands[i];
89 }
90 }
92 if (ctl == NULL) {
93 fprintf(stderr, "unknown argument: %s\n", argv[0]);
94 usage();
95 }
97 res.action = ctl->action;
98 res.ctl = ctl;
100 if (!ctl->has_pledge) {
101 /* pledge(2) default if command doesn't have its own */
102 if (pledge("stdio", NULL) == -1)
103 fatal("pledge");
106 status = ctl->main(&res, argc, argv);
107 close(ibuf->fd);
108 free(ibuf);
109 return status;
112 static int
113 enqueue_tracks(char **files)
115 char res[PATH_MAX];
116 int enq = 0;
118 for (; *files != NULL; ++files) {
119 memset(&res, 0, sizeof(res));
120 if (realpath(*files, res) == NULL) {
121 log_warn("realpath %s", *files);
122 continue;
125 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
126 res, sizeof(res));
127 enq++;
130 return enq == 0;
133 static void
134 print_error_message(const char *prfx, struct imsg *imsg)
136 size_t datalen;
137 char *msg;
139 datalen = IMSG_DATA_SIZE(*imsg);
140 if ((msg = calloc(1, datalen)) == NULL)
141 fatal("calloc %zu", datalen);
142 memcpy(msg, imsg->data, datalen);
143 if (datalen == 0 || msg[datalen-1] != '\0')
144 fatalx("malformed error message");
146 log_warnx("%s: %s", prfx, msg);
147 free(msg);
150 static int
151 show_add(struct imsg *imsg, int *ret, char ***files)
153 if (**files == NULL) {
154 log_warnx("received more replies than file sent");
155 *ret = 1;
156 return 1;
159 if (imsg->hdr.type == IMSG_CTL_ERR)
160 print_error_message(**files, imsg);
161 else if (imsg->hdr.type == IMSG_CTL_ADD)
162 log_debug("enqueued %s", **files);
163 else
164 fatalx("got invalid message %d", imsg->hdr.type);
166 (*files)++;
167 return (**files) == NULL;
170 static int
171 show_complete(struct imsg *imsg, int *ret)
173 size_t datalen;
174 char path[PATH_MAX];
176 if (imsg->hdr.type == IMSG_CTL_ERR) {
177 print_error_message("show failed", imsg);
178 *ret = 1;
179 return 1;
182 datalen = IMSG_DATA_SIZE(*imsg);
183 if (datalen == 0)
184 return 1;
186 if (datalen != sizeof(path))
187 fatalx("%s: data size mismatch", __func__);
188 memcpy(path, imsg->data, sizeof(path));
189 if (path[datalen-1] != '\0')
190 fatalx("%s: data corrupted?", __func__);
192 printf("%s\n", path);
193 return 0;
196 static int
197 ctlaction(struct parse_result *res)
199 struct imsg imsg;
200 ssize_t n;
201 int ret = 0, done = 1;
202 char **files;
204 switch (res->action) {
205 case PLAY:
206 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
207 break;
208 case PAUSE:
209 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
210 break;
211 case TOGGLE:
212 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
213 break;
214 case STOP:
215 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
216 break;
217 case RESTART:
218 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
219 break;
220 case ADD:
221 done = 0;
222 files = res->files;
223 ret = enqueue_tracks(res->files);
224 break;
225 case FLUSH:
226 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
227 break;
228 case SHOW:
229 done = 0;
230 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
231 break;
232 case NONE:
233 /* action not expected */
234 fatalx("invalid action %u", res->action);
235 break;
238 if (ret != 0)
239 goto end;
241 imsg_flush(ibuf);
243 while (!done) {
244 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
245 fatalx("imsg_read error");
246 if (n == 0)
247 fatalx("pipe closed");
249 while (!done) {
250 if ((n = imsg_get(ibuf, &imsg)) == -1)
251 fatalx("imsg_get error");
252 if (n == 0)
253 break;
255 switch (res->action) {
256 case ADD:
257 done = show_add(&imsg, &ret, &files);
258 break;
259 case SHOW:
260 done = show_complete(&imsg, &ret);
261 break;
262 default:
263 done = 1;
264 break;
267 imsg_free(&imsg);
271 end:
272 return ret;
275 int
276 ctl_noarg(struct parse_result *res, int argc, char **argv)
278 if (argc != 1)
279 ctl_usage(res->ctl);
280 return ctlaction(res);
283 int
284 ctl_add(struct parse_result *res, int argc, char **argv)
286 if (argc < 2)
287 ctl_usage(res->ctl);
288 res->files = argv+1;
289 return ctlaction(res);
292 __dead void
293 ctl(int argc, char **argv)
295 struct sockaddr_un sun;
296 int ctl_sock;
298 log_init(1, LOG_DAEMON);
299 log_setverbose(verbose);
301 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
302 fatal("socket");
304 memset(&sun, 0, sizeof(sun));
305 sun.sun_family = AF_UNIX;
306 strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
308 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
309 fatal("connect %s", csock);
311 ibuf = xmalloc(sizeof(*ibuf));
312 imsg_init(ibuf, ctl_sock);
314 optreset = 1;
315 optind = 1;
317 exit(parse(argc, argv));