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 3baa2617 2022-02-16 op { NULL },
53 3baa2617 2022-02-16 op };
54 3baa2617 2022-02-16 op
55 3baa2617 2022-02-16 op __dead void
56 3baa2617 2022-02-16 op usage(void)
57 3baa2617 2022-02-16 op {
58 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
59 3baa2617 2022-02-16 op fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
60 3baa2617 2022-02-16 op exit(1);
61 3baa2617 2022-02-16 op }
62 3baa2617 2022-02-16 op
63 3baa2617 2022-02-16 op static __dead void
64 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
65 3baa2617 2022-02-16 op {
66 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
67 3baa2617 2022-02-16 op ctl->name, ctl->usage);
68 3baa2617 2022-02-16 op exit(1);
69 3baa2617 2022-02-16 op }
70 3baa2617 2022-02-16 op
71 3baa2617 2022-02-16 op static int
72 3baa2617 2022-02-16 op parse(int argc, char **argv)
73 3baa2617 2022-02-16 op {
74 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
75 3baa2617 2022-02-16 op struct parse_result res;
76 3baa2617 2022-02-16 op int i, status;
77 3baa2617 2022-02-16 op
78 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
79 3baa2617 2022-02-16 op
80 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
81 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
82 3baa2617 2022-02-16 op == 0) {
83 3baa2617 2022-02-16 op if (ctl != NULL) {
84 3baa2617 2022-02-16 op fprintf(stderr,
85 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
86 3baa2617 2022-02-16 op usage();
87 3baa2617 2022-02-16 op }
88 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
89 3baa2617 2022-02-16 op }
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op
92 3baa2617 2022-02-16 op if (ctl == NULL) {
93 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
94 3baa2617 2022-02-16 op usage();
95 3baa2617 2022-02-16 op }
96 3baa2617 2022-02-16 op
97 3baa2617 2022-02-16 op res.action = ctl->action;
98 3baa2617 2022-02-16 op res.ctl = ctl;
99 3baa2617 2022-02-16 op
100 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
101 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
102 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
103 3baa2617 2022-02-16 op fatal("pledge");
104 3baa2617 2022-02-16 op }
105 3baa2617 2022-02-16 op
106 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
107 3baa2617 2022-02-16 op close(ibuf->fd);
108 3baa2617 2022-02-16 op free(ibuf);
109 3baa2617 2022-02-16 op return status;
110 3baa2617 2022-02-16 op }
111 3baa2617 2022-02-16 op
112 3baa2617 2022-02-16 op static int
113 3baa2617 2022-02-16 op enqueue_tracks(char **files)
114 3baa2617 2022-02-16 op {
115 3baa2617 2022-02-16 op char res[PATH_MAX];
116 3baa2617 2022-02-16 op int enq = 0;
117 3baa2617 2022-02-16 op
118 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
119 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
120 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
121 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
122 3baa2617 2022-02-16 op continue;
123 3baa2617 2022-02-16 op }
124 3baa2617 2022-02-16 op
125 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
126 3baa2617 2022-02-16 op res, sizeof(res));
127 3baa2617 2022-02-16 op enq++;
128 3baa2617 2022-02-16 op }
129 3baa2617 2022-02-16 op
130 3baa2617 2022-02-16 op return enq == 0;
131 3baa2617 2022-02-16 op }
132 3baa2617 2022-02-16 op
133 3baa2617 2022-02-16 op static int
134 3baa2617 2022-02-16 op show_complete(struct imsg *imsg, int *ret)
135 3baa2617 2022-02-16 op {
136 3baa2617 2022-02-16 op size_t datalen;
137 3baa2617 2022-02-16 op char path[PATH_MAX];
138 3baa2617 2022-02-16 op
139 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
140 3baa2617 2022-02-16 op if (datalen == 0)
141 3baa2617 2022-02-16 op return 1;
142 3baa2617 2022-02-16 op
143 3baa2617 2022-02-16 op if (datalen != sizeof(path))
144 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
145 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
146 3baa2617 2022-02-16 op if (path[datalen-1] != '\0')
147 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
148 3baa2617 2022-02-16 op
149 3baa2617 2022-02-16 op printf("%s\n", path);
150 3baa2617 2022-02-16 op return 0;
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op
153 3baa2617 2022-02-16 op static int
154 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
155 3baa2617 2022-02-16 op {
156 3baa2617 2022-02-16 op struct imsg imsg;
157 3baa2617 2022-02-16 op ssize_t n;
158 3baa2617 2022-02-16 op int ret = 0, done = 1;
159 3baa2617 2022-02-16 op
160 3baa2617 2022-02-16 op switch (res->action) {
161 3baa2617 2022-02-16 op case PLAY:
162 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
163 3baa2617 2022-02-16 op break;
164 3baa2617 2022-02-16 op case PAUSE:
165 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
166 3baa2617 2022-02-16 op break;
167 3baa2617 2022-02-16 op case TOGGLE:
168 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
169 3baa2617 2022-02-16 op break;
170 3baa2617 2022-02-16 op case STOP:
171 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
172 3baa2617 2022-02-16 op break;
173 3baa2617 2022-02-16 op case RESTART:
174 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
175 3baa2617 2022-02-16 op break;
176 3baa2617 2022-02-16 op case ADD:
177 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
178 3baa2617 2022-02-16 op break;
179 3baa2617 2022-02-16 op case FLUSH:
180 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
181 3baa2617 2022-02-16 op break;
182 3baa2617 2022-02-16 op case SHOW:
183 3baa2617 2022-02-16 op done = 0;
184 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
185 3baa2617 2022-02-16 op break;
186 3baa2617 2022-02-16 op case NONE:
187 3baa2617 2022-02-16 op /* action not expected */
188 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
189 3baa2617 2022-02-16 op break;
190 3baa2617 2022-02-16 op }
191 3baa2617 2022-02-16 op
192 3baa2617 2022-02-16 op if (ret != 0)
193 3baa2617 2022-02-16 op goto end;
194 3baa2617 2022-02-16 op
195 3baa2617 2022-02-16 op imsg_flush(ibuf);
196 3baa2617 2022-02-16 op
197 3baa2617 2022-02-16 op while (!done) {
198 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
199 3baa2617 2022-02-16 op fatalx("imsg_read error");
200 3baa2617 2022-02-16 op if (n == 0)
201 3baa2617 2022-02-16 op fatalx("pipe closed");
202 3baa2617 2022-02-16 op
203 3baa2617 2022-02-16 op while (!done) {
204 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
205 3baa2617 2022-02-16 op fatalx("imsg_get error");
206 3baa2617 2022-02-16 op if (n == 0)
207 3baa2617 2022-02-16 op break;
208 3baa2617 2022-02-16 op
209 3baa2617 2022-02-16 op switch (res->action) {
210 3baa2617 2022-02-16 op case SHOW:
211 3baa2617 2022-02-16 op done = show_complete(&imsg, &ret);
212 3baa2617 2022-02-16 op break;
213 3baa2617 2022-02-16 op default:
214 3baa2617 2022-02-16 op done = 1;
215 3baa2617 2022-02-16 op break;
216 3baa2617 2022-02-16 op }
217 3baa2617 2022-02-16 op
218 3baa2617 2022-02-16 op imsg_free(&imsg);
219 3baa2617 2022-02-16 op }
220 3baa2617 2022-02-16 op }
221 3baa2617 2022-02-16 op
222 3baa2617 2022-02-16 op end:
223 3baa2617 2022-02-16 op return ret;
224 3baa2617 2022-02-16 op }
225 3baa2617 2022-02-16 op
226 3baa2617 2022-02-16 op int
227 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
228 3baa2617 2022-02-16 op {
229 3baa2617 2022-02-16 op if (argc != 1)
230 3baa2617 2022-02-16 op ctl_usage(res->ctl);
231 3baa2617 2022-02-16 op return ctlaction(res);
232 3baa2617 2022-02-16 op }
233 3baa2617 2022-02-16 op
234 3baa2617 2022-02-16 op int
235 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
236 3baa2617 2022-02-16 op {
237 3baa2617 2022-02-16 op if (argc < 2)
238 3baa2617 2022-02-16 op ctl_usage(res->ctl);
239 3baa2617 2022-02-16 op res->files = argv+1;
240 3baa2617 2022-02-16 op return ctlaction(res);
241 3baa2617 2022-02-16 op }
242 3baa2617 2022-02-16 op
243 3baa2617 2022-02-16 op __dead void
244 3baa2617 2022-02-16 op ctl(int argc, char **argv)
245 3baa2617 2022-02-16 op {
246 3baa2617 2022-02-16 op struct sockaddr_un sun;
247 3baa2617 2022-02-16 op int ctl_sock;
248 3baa2617 2022-02-16 op
249 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON);
250 3baa2617 2022-02-16 op log_setverbose(verbose);
251 3baa2617 2022-02-16 op
252 3baa2617 2022-02-16 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
253 3baa2617 2022-02-16 op fatal("socket");
254 3baa2617 2022-02-16 op
255 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
256 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
257 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
258 3baa2617 2022-02-16 op
259 3baa2617 2022-02-16 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
260 3baa2617 2022-02-16 op fatal("connect %s", csock);
261 3baa2617 2022-02-16 op
262 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
263 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
264 3baa2617 2022-02-16 op
265 3baa2617 2022-02-16 op optreset = 1;
266 3baa2617 2022-02-16 op optind = 1;
267 3baa2617 2022-02-16 op
268 3baa2617 2022-02-16 op exit(parse(argc, argv));
269 3baa2617 2022-02-16 op }