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