Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 96233476 2022-06-09 op * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 3baa2617 2022-02-16 op *
5 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
6 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
7 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op *
9 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 3baa2617 2022-02-16 op */
17 3baa2617 2022-02-16 op
18 f36fd90a 2022-07-09 op #include "config.h"
19 f36fd90a 2022-07-09 op
20 3baa2617 2022-02-16 op #include <sys/socket.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 1d673950 2022-03-03 op #include <fcntl.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <stdio.h>
27 3baa2617 2022-02-16 op #include <stdlib.h>
28 3baa2617 2022-02-16 op #include <stdint.h>
29 3baa2617 2022-02-16 op #include <string.h>
30 3baa2617 2022-02-16 op #include <syslog.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
32 3baa2617 2022-02-16 op
33 3baa2617 2022-02-16 op #include "amused.h"
34 3baa2617 2022-02-16 op #include "log.h"
35 bb3f279f 2022-02-16 op #include "playlist.h"
36 3baa2617 2022-02-16 op #include "xmalloc.h"
37 3baa2617 2022-02-16 op
38 96233476 2022-06-09 op static struct imsgbuf *ibuf;
39 96233476 2022-06-09 op char cwd[PATH_MAX];
40 3baa2617 2022-02-16 op
41 2ddcfa40 2022-07-08 op static int ctl_noarg(struct parse_result *, int, char **);
42 2ddcfa40 2022-07-08 op static int ctl_add(struct parse_result *, int, char **);
43 2ddcfa40 2022-07-08 op static int ctl_show(struct parse_result *, int, char **);
44 2ddcfa40 2022-07-08 op static int ctl_load(struct parse_result *, int, char **);
45 2ddcfa40 2022-07-08 op static int ctl_jump(struct parse_result *, int, char **);
46 2ddcfa40 2022-07-08 op static int ctl_repeat(struct parse_result *, int, char **);
47 2ddcfa40 2022-07-08 op static int ctl_monitor(struct parse_result *, int, char **);
48 e5d4e9f7 2022-07-09 op static int ctl_seek(struct parse_result *, int, char **);
49 68e4f29e 2022-07-10 op static int ctl_status(struct parse_result *, int, char **);
50 3baa2617 2022-02-16 op
51 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
52 d488e5a5 2022-07-08 op { "add", ADD, ctl_add, "files...", 0 },
53 d488e5a5 2022-07-08 op { "flush", FLUSH, ctl_noarg, "", 0 },
54 d488e5a5 2022-07-08 op { "jump", JUMP, ctl_jump, "pattern", 0 },
55 6a128dce 2022-07-08 op { "load", LOAD, ctl_load, "[file]", 1 },
56 d488e5a5 2022-07-08 op { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
57 6a128dce 2022-07-08 op { "next", NEXT, ctl_noarg, "", 0 },
58 6a128dce 2022-07-08 op { "pause", PAUSE, ctl_noarg, "", 0 },
59 6a128dce 2022-07-08 op { "play", PLAY, ctl_noarg, "", 0 },
60 6a128dce 2022-07-08 op { "prev", PREV, ctl_noarg, "", 0 },
61 6a128dce 2022-07-08 op { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
62 6a128dce 2022-07-08 op { "restart", RESTART, ctl_noarg, "", 0 },
63 071167ed 2022-07-09 op { "seek", SEEK, ctl_seek, "[+-]time[%]", 0 },
64 6a128dce 2022-07-08 op { "show", SHOW, ctl_show, "[-p]", 0 },
65 68e4f29e 2022-07-10 op { "status", STATUS, ctl_status, "[-f fmt]", 0 },
66 6a128dce 2022-07-08 op { "stop", STOP, ctl_noarg, "", 0 },
67 6a128dce 2022-07-08 op { "toggle", TOGGLE, ctl_noarg, "", 0 },
68 d488e5a5 2022-07-08 op { NULL, 0, NULL, NULL, 0 },
69 3baa2617 2022-02-16 op };
70 3baa2617 2022-02-16 op
71 3baa2617 2022-02-16 op __dead void
72 3baa2617 2022-02-16 op usage(void)
73 3baa2617 2022-02-16 op {
74 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
75 3baa2617 2022-02-16 op exit(1);
76 3baa2617 2022-02-16 op }
77 3baa2617 2022-02-16 op
78 3baa2617 2022-02-16 op static __dead void
79 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
80 3baa2617 2022-02-16 op {
81 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
82 3baa2617 2022-02-16 op ctl->name, ctl->usage);
83 3baa2617 2022-02-16 op exit(1);
84 3baa2617 2022-02-16 op }
85 3baa2617 2022-02-16 op
86 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
87 3baa2617 2022-02-16 op static int
88 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
89 96233476 2022-06-09 op {
90 96233476 2022-06-09 op const char *p;
91 96233476 2022-06-09 op char *q, path[PATH_MAX];
92 96233476 2022-06-09 op
93 96233476 2022-06-09 op if (input[0] != '/') {
94 96233476 2022-06-09 op if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
95 96233476 2022-06-09 op >= sizeof(path)) {
96 96233476 2022-06-09 op errno = ENAMETOOLONG;
97 96233476 2022-06-09 op return -1;
98 96233476 2022-06-09 op }
99 96233476 2022-06-09 op input = path;
100 96233476 2022-06-09 op }
101 96233476 2022-06-09 op
102 96233476 2022-06-09 op p = input;
103 96233476 2022-06-09 op q = buf;
104 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
105 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
106 96233476 2022-06-09 op p += 1;
107 96233476 2022-06-09 op
108 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
109 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
110 96233476 2022-06-09 op p += 2;
111 96233476 2022-06-09 op
112 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
113 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
114 96233476 2022-06-09 op p += 3;
115 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
116 96233476 2022-06-09 op while (*--q != '/')
117 96233476 2022-06-09 op continue;
118 96233476 2022-06-09 op
119 96233476 2022-06-09 op } else {
120 96233476 2022-06-09 op *q++ = *p++;
121 96233476 2022-06-09 op }
122 96233476 2022-06-09 op }
123 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
124 96233476 2022-06-09 op *q = 0;
125 96233476 2022-06-09 op return 0;
126 96233476 2022-06-09 op } else {
127 96233476 2022-06-09 op errno = ENAMETOOLONG;
128 96233476 2022-06-09 op return -1;
129 96233476 2022-06-09 op }
130 96233476 2022-06-09 op }
131 96233476 2022-06-09 op
132 96233476 2022-06-09 op static int
133 6576093a 2022-07-11 op parse(struct parse_result *res, int argc, char **argv)
134 3baa2617 2022-02-16 op {
135 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
136 1d673950 2022-03-03 op const char *argv0;
137 3baa2617 2022-02-16 op int i, status;
138 3baa2617 2022-02-16 op
139 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
140 1d673950 2022-03-03 op argv0 = "status";
141 1d673950 2022-03-03 op
142 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
143 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
144 3baa2617 2022-02-16 op == 0) {
145 3baa2617 2022-02-16 op if (ctl != NULL) {
146 3baa2617 2022-02-16 op fprintf(stderr,
147 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
148 3baa2617 2022-02-16 op usage();
149 3baa2617 2022-02-16 op }
150 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op }
153 3baa2617 2022-02-16 op
154 3baa2617 2022-02-16 op if (ctl == NULL) {
155 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
156 3baa2617 2022-02-16 op usage();
157 3baa2617 2022-02-16 op }
158 3baa2617 2022-02-16 op
159 6576093a 2022-07-11 op res->action = ctl->action;
160 6576093a 2022-07-11 op res->ctl = ctl;
161 3baa2617 2022-02-16 op
162 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
163 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
164 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
165 3baa2617 2022-02-16 op fatal("pledge");
166 3baa2617 2022-02-16 op }
167 3baa2617 2022-02-16 op
168 6576093a 2022-07-11 op status = ctl->main(res, argc, argv);
169 3baa2617 2022-02-16 op close(ibuf->fd);
170 3baa2617 2022-02-16 op free(ibuf);
171 3baa2617 2022-02-16 op return status;
172 3baa2617 2022-02-16 op }
173 3baa2617 2022-02-16 op
174 3baa2617 2022-02-16 op static int
175 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
176 7a427ecd 2022-02-17 op {
177 ec1fb0c7 2022-02-17 op FILE *f;
178 7a427ecd 2022-02-17 op const char *file;
179 7a427ecd 2022-02-17 op char *line = NULL;
180 96233476 2022-06-09 op char path[PATH_MAX];
181 96233476 2022-06-09 op size_t linesize = 0, i = 0;
182 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
183 7a427ecd 2022-02-17 op
184 ec1fb0c7 2022-02-17 op if (res->file == NULL)
185 ec1fb0c7 2022-02-17 op f = stdin;
186 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
187 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
188 ec1fb0c7 2022-02-17 op *ret = 1;
189 ec1fb0c7 2022-02-17 op return 1;
190 ec1fb0c7 2022-02-17 op }
191 c00c1428 2022-03-25 op
192 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
193 7a427ecd 2022-02-17 op if (linelen == 0)
194 7a427ecd 2022-02-17 op continue;
195 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
196 7a427ecd 2022-02-17 op file = line;
197 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
198 7a427ecd 2022-02-17 op file += 2;
199 3af93963 2022-03-02 op curr = i;
200 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
201 7a427ecd 2022-02-17 op file += 2;
202 1dd3b054 2022-03-25 op
203 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
204 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
205 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
206 7a427ecd 2022-02-17 op continue;
207 7a427ecd 2022-02-17 op }
208 7a427ecd 2022-02-17 op
209 3af93963 2022-03-02 op i++;
210 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
211 7a427ecd 2022-02-17 op path, sizeof(path));
212 7a427ecd 2022-02-17 op }
213 7a427ecd 2022-02-17 op
214 7a427ecd 2022-02-17 op free(line);
215 ec1fb0c7 2022-02-17 op if (ferror(f))
216 7a427ecd 2022-02-17 op fatal("getline");
217 ec1fb0c7 2022-02-17 op fclose(f);
218 7a427ecd 2022-02-17 op
219 3af93963 2022-03-02 op if (i == 0) {
220 7a427ecd 2022-02-17 op *ret = 1;
221 7a427ecd 2022-02-17 op return 1;
222 7a427ecd 2022-02-17 op }
223 7a427ecd 2022-02-17 op
224 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
225 3af93963 2022-03-02 op &curr, sizeof(curr));
226 7a427ecd 2022-02-17 op imsg_flush(ibuf);
227 7a427ecd 2022-02-17 op return 0;
228 3baa2617 2022-02-16 op }
229 3baa2617 2022-02-16 op
230 fad0fb69 2022-05-28 op static const char *
231 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
232 6b47a39f 2022-02-21 op {
233 fad0fb69 2022-05-28 op size_t datalen;
234 fad0fb69 2022-05-28 op const char *msg;
235 6b47a39f 2022-02-21 op
236 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
237 fad0fb69 2022-05-28 op msg = imsg->data;
238 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
239 fad0fb69 2022-05-28 op fatalx("malformed error message");
240 6b47a39f 2022-02-21 op
241 fad0fb69 2022-05-28 op return msg;
242 fad0fb69 2022-05-28 op }
243 90122a37 2022-05-14 op
244 fad0fb69 2022-05-28 op static const char *
245 232e682b 2022-07-12 op event_name(int type)
246 fad0fb69 2022-05-28 op {
247 6b47a39f 2022-02-21 op switch (type) {
248 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
249 fad0fb69 2022-05-28 op return "play";
250 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
251 fad0fb69 2022-05-28 op return "pause";
252 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
253 fad0fb69 2022-05-28 op return "stop";
254 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
255 fad0fb69 2022-05-28 op return "next";
256 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
257 fad0fb69 2022-05-28 op return "prev";
258 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
259 fad0fb69 2022-05-28 op return "jump";
260 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
261 fad0fb69 2022-05-28 op return "add";
262 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
263 fad0fb69 2022-05-28 op return "load";
264 15aecb89 2022-07-12 op case IMSG_CTL_SEEK:
265 15aecb89 2022-07-12 op return "seek";
266 6b47a39f 2022-02-21 op default:
267 fad0fb69 2022-05-28 op return "unknown";
268 6b47a39f 2022-02-21 op }
269 bdc8c4e0 2022-07-08 op }
270 bdc8c4e0 2022-07-08 op
271 bdc8c4e0 2022-07-08 op static void
272 68e4f29e 2022-07-10 op print_time(const char *label, int64_t seconds, const char *suffx)
273 bdc8c4e0 2022-07-08 op {
274 bdc8c4e0 2022-07-08 op int hours, minutes;
275 bdc8c4e0 2022-07-08 op
276 bdc8c4e0 2022-07-08 op if (seconds < 0)
277 bdc8c4e0 2022-07-08 op seconds = 0;
278 bdc8c4e0 2022-07-08 op
279 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
280 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
281 bdc8c4e0 2022-07-08 op
282 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
283 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
284 bdc8c4e0 2022-07-08 op
285 68e4f29e 2022-07-10 op printf("%s", label);
286 bdc8c4e0 2022-07-08 op if (hours != 0)
287 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
288 68e4f29e 2022-07-10 op printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
289 68e4f29e 2022-07-10 op }
290 68e4f29e 2022-07-10 op
291 68e4f29e 2022-07-10 op static void
292 68e4f29e 2022-07-10 op print_status(struct player_status *ps, const char *spec)
293 68e4f29e 2022-07-10 op {
294 68e4f29e 2022-07-10 op const char *status;
295 68e4f29e 2022-07-10 op double percent;
296 68e4f29e 2022-07-10 op char *dup, *tmp, *tok;
297 68e4f29e 2022-07-10 op
298 68e4f29e 2022-07-10 op if (ps->status == STATE_STOPPED)
299 68e4f29e 2022-07-10 op status = "stopped";
300 68e4f29e 2022-07-10 op else if (ps->status == STATE_PLAYING)
301 68e4f29e 2022-07-10 op status = "playing";
302 68e4f29e 2022-07-10 op else if (ps->status == STATE_PAUSED)
303 68e4f29e 2022-07-10 op status = "paused";
304 68e4f29e 2022-07-10 op else
305 68e4f29e 2022-07-10 op status = "unknown";
306 68e4f29e 2022-07-10 op
307 68e4f29e 2022-07-10 op percent = 100.0 * (double)ps->position / (double)ps->duration;
308 68e4f29e 2022-07-10 op
309 68e4f29e 2022-07-10 op tmp = dup = xstrdup(spec);
310 68e4f29e 2022-07-10 op while ((tok = strsep(&tmp, ",")) != NULL) {
311 68e4f29e 2022-07-10 op if (*tok == '\0')
312 68e4f29e 2022-07-10 op continue;
313 68e4f29e 2022-07-10 op
314 68e4f29e 2022-07-10 op if (!strcmp(tok, "path")) {
315 68e4f29e 2022-07-10 op puts(ps->path);
316 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "repeat:oneline")) {
317 68e4f29e 2022-07-10 op printf("repeat one:%s ",
318 68e4f29e 2022-07-10 op ps->rp.repeat_one ? "on" : "off");
319 68e4f29e 2022-07-10 op printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
320 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "repeat")) {
321 68e4f29e 2022-07-10 op printf("repeat one %s\n",
322 68e4f29e 2022-07-10 op ps->rp.repeat_one ? "on" : "off");
323 68e4f29e 2022-07-10 op printf("repeat all %s\n",
324 68e4f29e 2022-07-10 op ps->rp.repeat_all ? "on" : "off");
325 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "status")) {
326 68e4f29e 2022-07-10 op printf("%s %s\n", status, ps->path);
327 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:oneline")) {
328 68e4f29e 2022-07-10 op print_time("time ", ps->position, " / ");
329 68e4f29e 2022-07-10 op print_time("", ps->duration, "\n");
330 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:percentage")) {
331 68e4f29e 2022-07-10 op printf("position %.2f%%\n", percent);
332 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:raw")) {
333 68e4f29e 2022-07-10 op printf("position %lld\n", (long long)ps->position);
334 68e4f29e 2022-07-10 op printf("duration %lld\n", (long long)ps->duration);
335 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time")) {
336 68e4f29e 2022-07-10 op print_time("position ", ps->position, "\n");
337 68e4f29e 2022-07-10 op print_time("duration ", ps->duration, "\n");
338 68e4f29e 2022-07-10 op }
339 68e4f29e 2022-07-10 op }
340 68e4f29e 2022-07-10 op
341 68e4f29e 2022-07-10 op free(dup);
342 6b47a39f 2022-02-21 op }
343 6b47a39f 2022-02-21 op
344 6b47a39f 2022-02-21 op static int
345 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
346 3baa2617 2022-02-16 op {
347 fad0fb69 2022-05-28 op char path[PATH_MAX];
348 3baa2617 2022-02-16 op struct imsg imsg;
349 fad0fb69 2022-05-28 op struct player_status ps;
350 fad0fb69 2022-05-28 op size_t datalen;
351 3baa2617 2022-02-16 op ssize_t n;
352 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
353 3baa2617 2022-02-16 op
354 3baa2617 2022-02-16 op switch (res->action) {
355 3baa2617 2022-02-16 op case PLAY:
356 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
357 00f500ff 2022-02-19 op if (verbose) {
358 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
359 00f500ff 2022-02-19 op NULL, 0);
360 00f500ff 2022-02-19 op done = 0;
361 00f500ff 2022-02-19 op }
362 3baa2617 2022-02-16 op break;
363 3baa2617 2022-02-16 op case PAUSE:
364 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
365 3baa2617 2022-02-16 op break;
366 3baa2617 2022-02-16 op case TOGGLE:
367 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
368 00f500ff 2022-02-19 op if (verbose) {
369 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
370 00f500ff 2022-02-19 op NULL, 0);
371 00f500ff 2022-02-19 op done = 0;
372 00f500ff 2022-02-19 op }
373 3baa2617 2022-02-16 op break;
374 3baa2617 2022-02-16 op case STOP:
375 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
376 3baa2617 2022-02-16 op break;
377 3baa2617 2022-02-16 op case ADD:
378 8ff9231f 2022-02-16 op done = 0;
379 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
380 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
381 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
382 ef593b43 2022-06-09 op == -1) {
383 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
384 fad0fb69 2022-05-28 op continue;
385 fad0fb69 2022-05-28 op }
386 fad0fb69 2022-05-28 op
387 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
388 fad0fb69 2022-05-28 op path, sizeof(path));
389 fad0fb69 2022-05-28 op }
390 fad0fb69 2022-05-28 op ret = i == 0;
391 3baa2617 2022-02-16 op break;
392 3baa2617 2022-02-16 op case FLUSH:
393 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
394 3baa2617 2022-02-16 op break;
395 3baa2617 2022-02-16 op case SHOW:
396 3baa2617 2022-02-16 op done = 0;
397 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
398 3baa2617 2022-02-16 op break;
399 bb3f279f 2022-02-16 op case STATUS:
400 bb3f279f 2022-02-16 op done = 0;
401 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
402 af27e631 2022-02-17 op break;
403 af27e631 2022-02-17 op case NEXT:
404 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
405 00f500ff 2022-02-19 op if (verbose) {
406 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
407 00f500ff 2022-02-19 op NULL, 0);
408 00f500ff 2022-02-19 op done = 0;
409 00f500ff 2022-02-19 op }
410 bb3f279f 2022-02-16 op break;
411 af27e631 2022-02-17 op case PREV:
412 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
413 00f500ff 2022-02-19 op if (verbose) {
414 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
415 00f500ff 2022-02-19 op NULL, 0);
416 00f500ff 2022-02-19 op done = 0;
417 00f500ff 2022-02-19 op }
418 af27e631 2022-02-17 op break;
419 14be015f 2022-02-17 op case LOAD:
420 7a427ecd 2022-02-17 op done = 0;
421 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
422 a913de21 2022-02-17 op break;
423 a913de21 2022-02-17 op case JUMP:
424 a913de21 2022-02-17 op done = 0;
425 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
426 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
427 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
428 fad0fb69 2022-05-28 op path, sizeof(path));
429 7a427ecd 2022-02-17 op break;
430 310ef57c 2022-02-19 op case REPEAT:
431 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
432 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
433 310ef57c 2022-02-19 op break;
434 6b47a39f 2022-02-21 op case MONITOR:
435 6b47a39f 2022-02-21 op done = 0;
436 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
437 6b47a39f 2022-02-21 op NULL, 0);
438 6b47a39f 2022-02-21 op break;
439 8d087670 2022-07-09 op case RESTART:
440 8d087670 2022-07-09 op memset(&res->seek, 0, sizeof(res->seek));
441 8d087670 2022-07-09 op /* fallthrough */
442 e5d4e9f7 2022-07-09 op case SEEK:
443 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
444 e5d4e9f7 2022-07-09 op sizeof(res->seek));
445 e5d4e9f7 2022-07-09 op break;
446 3baa2617 2022-02-16 op case NONE:
447 3baa2617 2022-02-16 op /* action not expected */
448 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
449 3baa2617 2022-02-16 op break;
450 3baa2617 2022-02-16 op }
451 3baa2617 2022-02-16 op
452 3baa2617 2022-02-16 op if (ret != 0)
453 3baa2617 2022-02-16 op goto end;
454 3baa2617 2022-02-16 op
455 3baa2617 2022-02-16 op imsg_flush(ibuf);
456 3baa2617 2022-02-16 op
457 fad0fb69 2022-05-28 op i = 0;
458 3baa2617 2022-02-16 op while (!done) {
459 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
460 3baa2617 2022-02-16 op fatalx("imsg_read error");
461 3baa2617 2022-02-16 op if (n == 0)
462 3baa2617 2022-02-16 op fatalx("pipe closed");
463 3baa2617 2022-02-16 op
464 3baa2617 2022-02-16 op while (!done) {
465 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
466 3baa2617 2022-02-16 op fatalx("imsg_get error");
467 3baa2617 2022-02-16 op if (n == 0)
468 3baa2617 2022-02-16 op break;
469 3baa2617 2022-02-16 op
470 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
471 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
472 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
473 fad0fb69 2022-05-28 op ret = 1;
474 fad0fb69 2022-05-28 op done = 1;
475 fad0fb69 2022-05-28 op break;
476 fad0fb69 2022-05-28 op }
477 fad0fb69 2022-05-28 op
478 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
479 fad0fb69 2022-05-28 op
480 3baa2617 2022-02-16 op switch (res->action) {
481 8ff9231f 2022-02-16 op case ADD:
482 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
483 fad0fb69 2022-05-28 op fatalx("received more replies than "
484 fad0fb69 2022-05-28 op "files enqueued.");
485 fad0fb69 2022-05-28 op
486 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
487 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
488 fad0fb69 2022-05-28 op else
489 fad0fb69 2022-05-28 op fatalx("invalid message %d",
490 fad0fb69 2022-05-28 op imsg.hdr.type);
491 fad0fb69 2022-05-28 op i++;
492 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
493 8ff9231f 2022-02-16 op break;
494 3baa2617 2022-02-16 op case SHOW:
495 fad0fb69 2022-05-28 op if (datalen == 0) {
496 fad0fb69 2022-05-28 op done = 1;
497 fad0fb69 2022-05-28 op break;
498 fad0fb69 2022-05-28 op }
499 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
500 fad0fb69 2022-05-28 op fatalx("data size mismatch");
501 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
502 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
503 fad0fb69 2022-05-28 op fatalx("received corrupted data");
504 fad0fb69 2022-05-28 op if (res->pretty) {
505 fad0fb69 2022-05-28 op char c = ' ';
506 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
507 fad0fb69 2022-05-28 op c = '>';
508 fad0fb69 2022-05-28 op printf("%c ", c);
509 fad0fb69 2022-05-28 op }
510 fad0fb69 2022-05-28 op puts(ps.path);
511 3baa2617 2022-02-16 op break;
512 146307bd 2022-02-17 op case PLAY:
513 146307bd 2022-02-17 op case TOGGLE:
514 146307bd 2022-02-17 op case RESTART:
515 bb3f279f 2022-02-16 op case STATUS:
516 146307bd 2022-02-17 op case NEXT:
517 146307bd 2022-02-17 op case PREV:
518 a913de21 2022-02-17 op case JUMP:
519 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
520 fad0fb69 2022-05-28 op fatalx("invalid message %d",
521 fad0fb69 2022-05-28 op imsg.hdr.type);
522 fad0fb69 2022-05-28 op
523 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
524 fad0fb69 2022-05-28 op fatalx("data size mismatch");
525 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
526 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
527 fad0fb69 2022-05-28 op fatalx("received corrupted data");
528 fad0fb69 2022-05-28 op
529 68e4f29e 2022-07-10 op print_status(&ps, res->status_format);
530 fad0fb69 2022-05-28 op done = 1;
531 bb3f279f 2022-02-16 op break;
532 7a427ecd 2022-02-17 op case LOAD:
533 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
534 fad0fb69 2022-05-28 op break;
535 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
536 fad0fb69 2022-05-28 op done = 1;
537 fad0fb69 2022-05-28 op break;
538 fad0fb69 2022-05-28 op }
539 fad0fb69 2022-05-28 op
540 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
541 fad0fb69 2022-05-28 op fatalx("invalid message %d",
542 fad0fb69 2022-05-28 op imsg.hdr.type);
543 fad0fb69 2022-05-28 op
544 fad0fb69 2022-05-28 op load_files(res, &ret);
545 7a427ecd 2022-02-17 op break;
546 6b47a39f 2022-02-21 op case MONITOR:
547 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
548 fad0fb69 2022-05-28 op fatalx("invalid message %d",
549 fad0fb69 2022-05-28 op imsg.hdr.type);
550 fad0fb69 2022-05-28 op
551 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
552 fad0fb69 2022-05-28 op fatalx("data size mismatch");
553 fad0fb69 2022-05-28 op
554 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
555 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
556 fad0fb69 2022-05-28 op fatalx("received corrupted data");
557 fad0fb69 2022-05-28 op
558 fad0fb69 2022-05-28 op if (!res->monitor[type])
559 fad0fb69 2022-05-28 op break;
560 fad0fb69 2022-05-28 op
561 232e682b 2022-07-12 op puts(event_name(type));
562 fad0fb69 2022-05-28 op fflush(stdout);
563 6b47a39f 2022-02-21 op break;
564 3baa2617 2022-02-16 op default:
565 3baa2617 2022-02-16 op done = 1;
566 3baa2617 2022-02-16 op break;
567 3baa2617 2022-02-16 op }
568 3baa2617 2022-02-16 op
569 3baa2617 2022-02-16 op imsg_free(&imsg);
570 3baa2617 2022-02-16 op }
571 3baa2617 2022-02-16 op }
572 3baa2617 2022-02-16 op
573 3baa2617 2022-02-16 op end:
574 3baa2617 2022-02-16 op return ret;
575 3baa2617 2022-02-16 op }
576 3baa2617 2022-02-16 op
577 2ddcfa40 2022-07-08 op static int
578 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
579 3baa2617 2022-02-16 op {
580 7e29fc4a 2022-03-03 op int ch;
581 7e29fc4a 2022-03-03 op
582 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
583 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
584 7e29fc4a 2022-03-03 op argc -= optind;
585 7e29fc4a 2022-03-03 op argv += optind;
586 7e29fc4a 2022-03-03 op
587 acaf7eb2 2022-03-05 op if (argc > 0)
588 3baa2617 2022-02-16 op ctl_usage(res->ctl);
589 7e29fc4a 2022-03-03 op
590 3baa2617 2022-02-16 op return ctlaction(res);
591 3baa2617 2022-02-16 op }
592 3baa2617 2022-02-16 op
593 2ddcfa40 2022-07-08 op static int
594 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
595 3baa2617 2022-02-16 op {
596 7e29fc4a 2022-03-03 op int ch;
597 7e29fc4a 2022-03-03 op
598 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
599 3baa2617 2022-02-16 op ctl_usage(res->ctl);
600 7e29fc4a 2022-03-03 op argc -= optind;
601 7e29fc4a 2022-03-03 op argv += optind;
602 7e29fc4a 2022-03-03 op
603 7e29fc4a 2022-03-03 op if (argc == 0)
604 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
605 7e29fc4a 2022-03-03 op res->files = argv;
606 14be015f 2022-02-17 op
607 3baa2617 2022-02-16 op return ctlaction(res);
608 3baa2617 2022-02-16 op }
609 14be015f 2022-02-17 op
610 2ddcfa40 2022-07-08 op static int
611 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
612 8e916714 2022-02-17 op {
613 8e916714 2022-02-17 op int ch;
614 8e916714 2022-02-17 op
615 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
616 8e916714 2022-02-17 op switch (ch) {
617 8e916714 2022-02-17 op case 'p':
618 8e916714 2022-02-17 op res->pretty = 1;
619 8e916714 2022-02-17 op break;
620 8e916714 2022-02-17 op default:
621 8e916714 2022-02-17 op ctl_usage(res->ctl);
622 8e916714 2022-02-17 op }
623 8e916714 2022-02-17 op }
624 8e916714 2022-02-17 op
625 8e916714 2022-02-17 op return ctlaction(res);
626 8e916714 2022-02-17 op }
627 8e916714 2022-02-17 op
628 2ddcfa40 2022-07-08 op static int
629 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
630 14be015f 2022-02-17 op {
631 7e29fc4a 2022-03-03 op int ch;
632 7e29fc4a 2022-03-03 op
633 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
634 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
635 7e29fc4a 2022-03-03 op argc -= optind;
636 7e29fc4a 2022-03-03 op argv += optind;
637 7e29fc4a 2022-03-03 op
638 7e29fc4a 2022-03-03 op if (argc == 0)
639 ec1fb0c7 2022-02-17 op res->file = NULL;
640 7e29fc4a 2022-03-03 op else if (argc == 1)
641 7e29fc4a 2022-03-03 op res->file = argv[0];
642 ec1fb0c7 2022-02-17 op else
643 14be015f 2022-02-17 op ctl_usage(res->ctl);
644 14be015f 2022-02-17 op
645 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
646 14be015f 2022-02-17 op fatal("pledge");
647 14be015f 2022-02-17 op
648 7a427ecd 2022-02-17 op return ctlaction(res);
649 14be015f 2022-02-17 op }
650 14be015f 2022-02-17 op
651 2ddcfa40 2022-07-08 op static int
652 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
653 a913de21 2022-02-17 op {
654 a913de21 2022-02-17 op int ch;
655 a913de21 2022-02-17 op
656 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
657 a913de21 2022-02-17 op ctl_usage(res->ctl);
658 a913de21 2022-02-17 op argc -= optind;
659 a913de21 2022-02-17 op argv += optind;
660 a913de21 2022-02-17 op
661 a913de21 2022-02-17 op if (argc != 1)
662 a913de21 2022-02-17 op ctl_usage(res->ctl);
663 a913de21 2022-02-17 op
664 a913de21 2022-02-17 op res->file = argv[0];
665 310ef57c 2022-02-19 op return ctlaction(res);
666 310ef57c 2022-02-19 op }
667 310ef57c 2022-02-19 op
668 2ddcfa40 2022-07-08 op static int
669 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
670 310ef57c 2022-02-19 op {
671 310ef57c 2022-02-19 op int ch, b;
672 310ef57c 2022-02-19 op
673 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
674 310ef57c 2022-02-19 op ctl_usage(res->ctl);
675 310ef57c 2022-02-19 op argc -= optind;
676 310ef57c 2022-02-19 op argv += optind;
677 310ef57c 2022-02-19 op
678 310ef57c 2022-02-19 op if (argc != 2)
679 310ef57c 2022-02-19 op ctl_usage(res->ctl);
680 310ef57c 2022-02-19 op
681 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
682 310ef57c 2022-02-19 op b = 1;
683 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
684 310ef57c 2022-02-19 op b = 0;
685 310ef57c 2022-02-19 op else
686 310ef57c 2022-02-19 op ctl_usage(res->ctl);
687 310ef57c 2022-02-19 op
688 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
689 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
690 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
691 310ef57c 2022-02-19 op res->rep.repeat_one = b;
692 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
693 310ef57c 2022-02-19 op res->rep.repeat_all = b;
694 310ef57c 2022-02-19 op else
695 310ef57c 2022-02-19 op ctl_usage(res->ctl);
696 90122a37 2022-05-14 op
697 90122a37 2022-05-14 op return ctlaction(res);
698 90122a37 2022-05-14 op }
699 90122a37 2022-05-14 op
700 2ddcfa40 2022-07-08 op static int
701 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
702 90122a37 2022-05-14 op {
703 15aecb89 2022-07-12 op int ch, n = 0;
704 90122a37 2022-05-14 op const char *events;
705 90122a37 2022-05-14 op char *dup, *tmp, *tok;
706 90122a37 2022-05-14 op
707 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
708 90122a37 2022-05-14 op ctl_usage(res->ctl);
709 90122a37 2022-05-14 op argc -= optind;
710 90122a37 2022-05-14 op argv += optind;
711 90122a37 2022-05-14 op
712 90122a37 2022-05-14 op if (argc > 1)
713 90122a37 2022-05-14 op ctl_usage(res->ctl);
714 90122a37 2022-05-14 op
715 15aecb89 2022-07-12 op events = "play,pause,stop,next,prev,jump,repeat,add,load,seek";
716 90122a37 2022-05-14 op if (argc == 1)
717 90122a37 2022-05-14 op events = *argv;
718 310ef57c 2022-02-19 op
719 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
720 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
721 90122a37 2022-05-14 op if (*tok == '\0')
722 90122a37 2022-05-14 op continue;
723 90122a37 2022-05-14 op
724 15aecb89 2022-07-12 op n++;
725 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
726 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
727 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
728 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
729 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
730 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
731 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
732 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
733 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
734 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
735 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
736 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
737 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
738 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
739 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
740 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
741 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
742 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
743 88519c7b 2022-07-09 op else if (!strcmp(tok, "seek"))
744 88519c7b 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
745 15aecb89 2022-07-12 op else {
746 15aecb89 2022-07-12 op log_warnx("unknown event \"%s\"", tok);
747 15aecb89 2022-07-12 op n--;
748 15aecb89 2022-07-12 op }
749 90122a37 2022-05-14 op }
750 90122a37 2022-05-14 op
751 90122a37 2022-05-14 op free(dup);
752 15aecb89 2022-07-12 op if (n == 0)
753 15aecb89 2022-07-12 op ctl_usage(res->ctl);
754 e5d4e9f7 2022-07-09 op return ctlaction(res);
755 e5d4e9f7 2022-07-09 op }
756 e5d4e9f7 2022-07-09 op
757 e5d4e9f7 2022-07-09 op static int
758 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
759 e5d4e9f7 2022-07-09 op {
760 071167ed 2022-07-09 op const char *n;
761 071167ed 2022-07-09 op char *ep;
762 071167ed 2022-07-09 op int hours = 0, minutes = 0, seconds = 0;
763 071167ed 2022-07-09 op int sign = 1;
764 e5d4e9f7 2022-07-09 op
765 e5d4e9f7 2022-07-09 op if (argc > 0) {
766 e5d4e9f7 2022-07-09 op /* skip the command name */
767 e5d4e9f7 2022-07-09 op argc--;
768 e5d4e9f7 2022-07-09 op argv++;
769 e5d4e9f7 2022-07-09 op }
770 e5d4e9f7 2022-07-09 op
771 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
772 e5d4e9f7 2022-07-09 op argc--;
773 e5d4e9f7 2022-07-09 op argv++;
774 e5d4e9f7 2022-07-09 op }
775 e5d4e9f7 2022-07-09 op
776 e5d4e9f7 2022-07-09 op if (argc != 1)
777 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
778 e5d4e9f7 2022-07-09 op
779 e5d4e9f7 2022-07-09 op n = *argv;
780 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
781 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
782 071167ed 2022-07-09 op if (*n == '-') {
783 071167ed 2022-07-09 op n++;
784 071167ed 2022-07-09 op sign = -1;
785 071167ed 2022-07-09 op }
786 e5d4e9f7 2022-07-09 op
787 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
788 071167ed 2022-07-09 op if (n[0] == '\0' ||
789 071167ed 2022-07-09 op (*ep != '\0' && *ep != ':' && *ep != '%') ||
790 071167ed 2022-07-09 op (*ep == '%' && ep[1] != '\0'))
791 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
792 071167ed 2022-07-09 op if (*ep == '\0' || *ep == '%') {
793 071167ed 2022-07-09 op res->seek.percent = *ep == '%';
794 071167ed 2022-07-09 op goto done;
795 071167ed 2022-07-09 op }
796 e5d4e9f7 2022-07-09 op
797 071167ed 2022-07-09 op n = ++ep;
798 071167ed 2022-07-09 op minutes = seconds;
799 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
800 071167ed 2022-07-09 op if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
801 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
802 071167ed 2022-07-09 op if (*ep == '\0')
803 071167ed 2022-07-09 op goto done;
804 071167ed 2022-07-09 op
805 071167ed 2022-07-09 op n = ++ep;
806 071167ed 2022-07-09 op hours = minutes;
807 071167ed 2022-07-09 op minutes = seconds;
808 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
809 071167ed 2022-07-09 op if (n[0] == '\0' || *ep != '\0')
810 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
811 071167ed 2022-07-09 op
812 071167ed 2022-07-09 op done:
813 071167ed 2022-07-09 op res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
814 a913de21 2022-02-17 op return ctlaction(res);
815 a913de21 2022-02-17 op }
816 a913de21 2022-02-17 op
817 fea541a8 2022-02-16 op static int
818 68e4f29e 2022-07-10 op ctl_status(struct parse_result *res, int argc, char **argv)
819 68e4f29e 2022-07-10 op {
820 68e4f29e 2022-07-10 op int ch;
821 68e4f29e 2022-07-10 op
822 68e4f29e 2022-07-10 op while ((ch = getopt(argc, argv, "f:")) != -1) {
823 68e4f29e 2022-07-10 op switch (ch) {
824 68e4f29e 2022-07-10 op case 'f':
825 6576093a 2022-07-11 op res->status_format = optarg;
826 68e4f29e 2022-07-10 op break;
827 68e4f29e 2022-07-10 op default:
828 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
829 68e4f29e 2022-07-10 op }
830 68e4f29e 2022-07-10 op }
831 68e4f29e 2022-07-10 op argc -= optind;
832 68e4f29e 2022-07-10 op argv += optind;
833 68e4f29e 2022-07-10 op
834 68e4f29e 2022-07-10 op if (argc > 0)
835 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
836 68e4f29e 2022-07-10 op
837 68e4f29e 2022-07-10 op return ctlaction(res);
838 68e4f29e 2022-07-10 op }
839 68e4f29e 2022-07-10 op
840 68e4f29e 2022-07-10 op static int
841 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
842 3baa2617 2022-02-16 op {
843 1d673950 2022-03-03 op int lockfd;
844 3baa2617 2022-02-16 op
845 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
846 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
847 1d673950 2022-03-03 op return -1;
848 1d673950 2022-03-03 op }
849 3baa2617 2022-02-16 op
850 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
851 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
852 1d673950 2022-03-03 op if (errno != EAGAIN)
853 1d673950 2022-03-03 op return -1;
854 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
855 1d673950 2022-03-03 op /* nop */;
856 1d673950 2022-03-03 op close(lockfd);
857 1d673950 2022-03-03 op return -2;
858 1d673950 2022-03-03 op }
859 1d673950 2022-03-03 op log_debug("flock succeeded");
860 1d673950 2022-03-03 op
861 1d673950 2022-03-03 op return lockfd;
862 1d673950 2022-03-03 op }
863 1d673950 2022-03-03 op
864 1d673950 2022-03-03 op static int
865 1d673950 2022-03-03 op ctl_connect(void)
866 1d673950 2022-03-03 op {
867 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
868 1d673950 2022-03-03 op struct sockaddr_un sa;
869 1d673950 2022-03-03 op size_t size;
870 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
871 1b77ad48 2022-07-08 op int attempt = 0;
872 1d673950 2022-03-03 op char *lockfile = NULL;
873 1d673950 2022-03-03 op
874 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
875 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
876 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
877 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
878 1d673950 2022-03-03 op errno = ENAMETOOLONG;
879 fea541a8 2022-02-16 op return -1;
880 fea541a8 2022-02-16 op }
881 3baa2617 2022-02-16 op
882 1d673950 2022-03-03 op retry:
883 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
884 1d673950 2022-03-03 op return -1;
885 1d673950 2022-03-03 op
886 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
887 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
888 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
889 1d673950 2022-03-03 op goto failed;
890 1b77ad48 2022-07-08 op if (attempt++ == 100)
891 1b77ad48 2022-07-08 op goto failed;
892 1d673950 2022-03-03 op close(fd);
893 1d673950 2022-03-03 op
894 1d673950 2022-03-03 op if (!locked) {
895 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
896 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
897 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
898 1d673950 2022-03-03 op
899 1d673950 2022-03-03 op free(lockfile);
900 1d673950 2022-03-03 op lockfile = NULL;
901 1d673950 2022-03-03 op
902 1d673950 2022-03-03 op if (lockfd == -1)
903 1d673950 2022-03-03 op goto retry;
904 1d673950 2022-03-03 op }
905 1d673950 2022-03-03 op
906 1d673950 2022-03-03 op /*
907 1d673950 2022-03-03 op * Always retry at least once, even if we got
908 1d673950 2022-03-03 op * the lock, because another client could have
909 1d673950 2022-03-03 op * taken the lock, started the server and released
910 1d673950 2022-03-03 op * the lock between our connect() and flock()
911 1d673950 2022-03-03 op */
912 1d673950 2022-03-03 op locked = 1;
913 1d673950 2022-03-03 op goto retry;
914 1d673950 2022-03-03 op }
915 1d673950 2022-03-03 op
916 1d673950 2022-03-03 op if (!spawned) {
917 1d673950 2022-03-03 op log_debug("spawning the daemon");
918 1d673950 2022-03-03 op spawn_daemon();
919 1d673950 2022-03-03 op spawned = 1;
920 1d673950 2022-03-03 op }
921 1d673950 2022-03-03 op
922 1d673950 2022-03-03 op nanosleep(&ts, NULL);
923 1d673950 2022-03-03 op goto retry;
924 1d673950 2022-03-03 op }
925 1d673950 2022-03-03 op
926 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
927 1d673950 2022-03-03 op unlink(lockfile);
928 1d673950 2022-03-03 op free(lockfile);
929 1d673950 2022-03-03 op close(lockfd);
930 1d673950 2022-03-03 op }
931 1d673950 2022-03-03 op return fd;
932 1d673950 2022-03-03 op
933 1d673950 2022-03-03 op failed:
934 1d673950 2022-03-03 op if (locked) {
935 1d673950 2022-03-03 op free(lockfile);
936 1d673950 2022-03-03 op close(lockfd);
937 1d673950 2022-03-03 op }
938 1d673950 2022-03-03 op close(fd);
939 1d673950 2022-03-03 op return -1;
940 fea541a8 2022-02-16 op }
941 fea541a8 2022-02-16 op
942 fea541a8 2022-02-16 op __dead void
943 fea541a8 2022-02-16 op ctl(int argc, char **argv)
944 fea541a8 2022-02-16 op {
945 6576093a 2022-07-11 op struct parse_result res;
946 6576093a 2022-07-11 op const char *fmt;
947 1d673950 2022-03-03 op int ctl_sock;
948 3baa2617 2022-02-16 op
949 6576093a 2022-07-11 op memset(&res, 0, sizeof(res));
950 6576093a 2022-07-11 op if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
951 6576093a 2022-07-11 op fmt = "status,time,repeat";
952 6576093a 2022-07-11 op res.status_format = fmt;
953 6576093a 2022-07-11 op
954 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
955 fea541a8 2022-02-16 op log_setverbose(verbose);
956 fea541a8 2022-02-16 op
957 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
958 96233476 2022-06-09 op fatal("getcwd");
959 96233476 2022-06-09 op
960 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
961 1d673950 2022-03-03 op fatal("can't connect");
962 fea541a8 2022-02-16 op
963 fea541a8 2022-02-16 op if (ctl_sock == -1)
964 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
965 fea541a8 2022-02-16 op
966 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
967 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
968 3baa2617 2022-02-16 op
969 3baa2617 2022-02-16 op optreset = 1;
970 3baa2617 2022-02-16 op optind = 1;
971 3baa2617 2022-02-16 op
972 6576093a 2022-07-11 op exit(parse(&res, argc, argv));
973 3baa2617 2022-02-16 op }