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