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 e5d4e9f7 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_RESTART:
259 fad0fb69 2022-05-28 op return "restart";
260 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
261 fad0fb69 2022-05-28 op return "flush";
262 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
263 fad0fb69 2022-05-28 op return "next";
264 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
265 fad0fb69 2022-05-28 op return "prev";
266 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
267 fad0fb69 2022-05-28 op return "jump";
268 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
269 fad0fb69 2022-05-28 op return "repeat";
270 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
271 fad0fb69 2022-05-28 op return "add";
272 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
273 fad0fb69 2022-05-28 op return "load";
274 6b47a39f 2022-02-21 op default:
275 fad0fb69 2022-05-28 op return "unknown";
276 6b47a39f 2022-02-21 op }
277 bdc8c4e0 2022-07-08 op }
278 bdc8c4e0 2022-07-08 op
279 bdc8c4e0 2022-07-08 op static void
280 bdc8c4e0 2022-07-08 op print_time(const char *label, int64_t seconds)
281 bdc8c4e0 2022-07-08 op {
282 bdc8c4e0 2022-07-08 op int hours, minutes;
283 bdc8c4e0 2022-07-08 op
284 bdc8c4e0 2022-07-08 op if (seconds < 0)
285 bdc8c4e0 2022-07-08 op seconds = 0;
286 bdc8c4e0 2022-07-08 op
287 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
288 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
289 bdc8c4e0 2022-07-08 op
290 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
291 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
292 bdc8c4e0 2022-07-08 op
293 bdc8c4e0 2022-07-08 op printf("%s ", label);
294 bdc8c4e0 2022-07-08 op if (hours != 0)
295 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
296 bdc8c4e0 2022-07-08 op printf("%02d:%02lld\n", minutes, (long long)seconds);
297 6b47a39f 2022-02-21 op }
298 6b47a39f 2022-02-21 op
299 6b47a39f 2022-02-21 op static int
300 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
301 3baa2617 2022-02-16 op {
302 fad0fb69 2022-05-28 op char path[PATH_MAX];
303 3baa2617 2022-02-16 op struct imsg imsg;
304 fad0fb69 2022-05-28 op struct player_status ps;
305 fad0fb69 2022-05-28 op size_t datalen;
306 3baa2617 2022-02-16 op ssize_t n;
307 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
308 3baa2617 2022-02-16 op
309 3baa2617 2022-02-16 op switch (res->action) {
310 3baa2617 2022-02-16 op case PLAY:
311 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
312 00f500ff 2022-02-19 op if (verbose) {
313 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
314 00f500ff 2022-02-19 op NULL, 0);
315 00f500ff 2022-02-19 op done = 0;
316 00f500ff 2022-02-19 op }
317 3baa2617 2022-02-16 op break;
318 3baa2617 2022-02-16 op case PAUSE:
319 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
320 3baa2617 2022-02-16 op break;
321 3baa2617 2022-02-16 op case TOGGLE:
322 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
323 00f500ff 2022-02-19 op if (verbose) {
324 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
325 00f500ff 2022-02-19 op NULL, 0);
326 00f500ff 2022-02-19 op done = 0;
327 00f500ff 2022-02-19 op }
328 3baa2617 2022-02-16 op break;
329 3baa2617 2022-02-16 op case STOP:
330 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
331 3baa2617 2022-02-16 op break;
332 3baa2617 2022-02-16 op case RESTART:
333 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
334 00f500ff 2022-02-19 op if (verbose) {
335 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
336 00f500ff 2022-02-19 op NULL, 0);
337 00f500ff 2022-02-19 op done = 0;
338 00f500ff 2022-02-19 op }
339 3baa2617 2022-02-16 op break;
340 3baa2617 2022-02-16 op case ADD:
341 8ff9231f 2022-02-16 op done = 0;
342 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
343 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
344 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
345 ef593b43 2022-06-09 op == -1) {
346 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
347 fad0fb69 2022-05-28 op continue;
348 fad0fb69 2022-05-28 op }
349 fad0fb69 2022-05-28 op
350 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
351 fad0fb69 2022-05-28 op path, sizeof(path));
352 fad0fb69 2022-05-28 op }
353 fad0fb69 2022-05-28 op ret = i == 0;
354 3baa2617 2022-02-16 op break;
355 3baa2617 2022-02-16 op case FLUSH:
356 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
357 3baa2617 2022-02-16 op break;
358 3baa2617 2022-02-16 op case SHOW:
359 3baa2617 2022-02-16 op done = 0;
360 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
361 3baa2617 2022-02-16 op break;
362 bb3f279f 2022-02-16 op case STATUS:
363 bb3f279f 2022-02-16 op done = 0;
364 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
365 af27e631 2022-02-17 op break;
366 af27e631 2022-02-17 op case NEXT:
367 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 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 bb3f279f 2022-02-16 op break;
374 af27e631 2022-02-17 op case PREV:
375 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
376 00f500ff 2022-02-19 op if (verbose) {
377 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
378 00f500ff 2022-02-19 op NULL, 0);
379 00f500ff 2022-02-19 op done = 0;
380 00f500ff 2022-02-19 op }
381 af27e631 2022-02-17 op break;
382 14be015f 2022-02-17 op case LOAD:
383 7a427ecd 2022-02-17 op done = 0;
384 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
385 a913de21 2022-02-17 op break;
386 a913de21 2022-02-17 op case JUMP:
387 a913de21 2022-02-17 op done = 0;
388 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
389 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
390 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
391 fad0fb69 2022-05-28 op path, sizeof(path));
392 7a427ecd 2022-02-17 op break;
393 310ef57c 2022-02-19 op case REPEAT:
394 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
395 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
396 310ef57c 2022-02-19 op break;
397 6b47a39f 2022-02-21 op case MONITOR:
398 6b47a39f 2022-02-21 op done = 0;
399 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
400 6b47a39f 2022-02-21 op NULL, 0);
401 6b47a39f 2022-02-21 op break;
402 e5d4e9f7 2022-07-09 op case SEEK:
403 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
404 e5d4e9f7 2022-07-09 op sizeof(res->seek));
405 e5d4e9f7 2022-07-09 op break;
406 3baa2617 2022-02-16 op case NONE:
407 3baa2617 2022-02-16 op /* action not expected */
408 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
409 3baa2617 2022-02-16 op break;
410 3baa2617 2022-02-16 op }
411 3baa2617 2022-02-16 op
412 3baa2617 2022-02-16 op if (ret != 0)
413 3baa2617 2022-02-16 op goto end;
414 3baa2617 2022-02-16 op
415 3baa2617 2022-02-16 op imsg_flush(ibuf);
416 3baa2617 2022-02-16 op
417 fad0fb69 2022-05-28 op i = 0;
418 3baa2617 2022-02-16 op while (!done) {
419 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
420 3baa2617 2022-02-16 op fatalx("imsg_read error");
421 3baa2617 2022-02-16 op if (n == 0)
422 3baa2617 2022-02-16 op fatalx("pipe closed");
423 3baa2617 2022-02-16 op
424 3baa2617 2022-02-16 op while (!done) {
425 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
426 3baa2617 2022-02-16 op fatalx("imsg_get error");
427 3baa2617 2022-02-16 op if (n == 0)
428 3baa2617 2022-02-16 op break;
429 3baa2617 2022-02-16 op
430 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
431 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
432 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
433 fad0fb69 2022-05-28 op ret = 1;
434 fad0fb69 2022-05-28 op done = 1;
435 fad0fb69 2022-05-28 op break;
436 fad0fb69 2022-05-28 op }
437 fad0fb69 2022-05-28 op
438 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
439 fad0fb69 2022-05-28 op
440 3baa2617 2022-02-16 op switch (res->action) {
441 8ff9231f 2022-02-16 op case ADD:
442 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
443 fad0fb69 2022-05-28 op fatalx("received more replies than "
444 fad0fb69 2022-05-28 op "files enqueued.");
445 fad0fb69 2022-05-28 op
446 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
447 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
448 fad0fb69 2022-05-28 op else
449 fad0fb69 2022-05-28 op fatalx("invalid message %d",
450 fad0fb69 2022-05-28 op imsg.hdr.type);
451 fad0fb69 2022-05-28 op i++;
452 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
453 8ff9231f 2022-02-16 op break;
454 3baa2617 2022-02-16 op case SHOW:
455 fad0fb69 2022-05-28 op if (datalen == 0) {
456 fad0fb69 2022-05-28 op done = 1;
457 fad0fb69 2022-05-28 op break;
458 fad0fb69 2022-05-28 op }
459 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
460 fad0fb69 2022-05-28 op fatalx("data size mismatch");
461 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
462 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
463 fad0fb69 2022-05-28 op fatalx("received corrupted data");
464 fad0fb69 2022-05-28 op if (res->pretty) {
465 fad0fb69 2022-05-28 op char c = ' ';
466 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
467 fad0fb69 2022-05-28 op c = '>';
468 fad0fb69 2022-05-28 op printf("%c ", c);
469 fad0fb69 2022-05-28 op }
470 fad0fb69 2022-05-28 op puts(ps.path);
471 3baa2617 2022-02-16 op break;
472 146307bd 2022-02-17 op case PLAY:
473 146307bd 2022-02-17 op case TOGGLE:
474 146307bd 2022-02-17 op case RESTART:
475 bb3f279f 2022-02-16 op case STATUS:
476 146307bd 2022-02-17 op case NEXT:
477 146307bd 2022-02-17 op case PREV:
478 a913de21 2022-02-17 op case JUMP:
479 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
480 fad0fb69 2022-05-28 op fatalx("invalid message %d",
481 fad0fb69 2022-05-28 op imsg.hdr.type);
482 fad0fb69 2022-05-28 op
483 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
484 fad0fb69 2022-05-28 op fatalx("data size mismatch");
485 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
486 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
487 fad0fb69 2022-05-28 op fatalx("received corrupted data");
488 fad0fb69 2022-05-28 op
489 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
490 fad0fb69 2022-05-28 op printf("stopped ");
491 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
492 fad0fb69 2022-05-28 op printf("playing ");
493 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
494 fad0fb69 2022-05-28 op printf("paused ");
495 fad0fb69 2022-05-28 op else
496 fad0fb69 2022-05-28 op printf("unknown ");
497 fad0fb69 2022-05-28 op
498 fad0fb69 2022-05-28 op puts(ps.path);
499 bdc8c4e0 2022-07-08 op
500 bdc8c4e0 2022-07-08 op print_time("position", ps.position);
501 bdc8c4e0 2022-07-08 op print_time("duration", ps.duration);
502 bdc8c4e0 2022-07-08 op
503 dc12484b 2022-07-08 op printf("repeat one %s\nrepeat all %s\n",
504 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
505 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
506 fad0fb69 2022-05-28 op
507 fad0fb69 2022-05-28 op done = 1;
508 bb3f279f 2022-02-16 op break;
509 7a427ecd 2022-02-17 op case LOAD:
510 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
511 fad0fb69 2022-05-28 op break;
512 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
513 fad0fb69 2022-05-28 op done = 1;
514 fad0fb69 2022-05-28 op break;
515 fad0fb69 2022-05-28 op }
516 fad0fb69 2022-05-28 op
517 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
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 load_files(res, &ret);
522 7a427ecd 2022-02-17 op break;
523 6b47a39f 2022-02-21 op case MONITOR:
524 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
525 fad0fb69 2022-05-28 op fatalx("invalid message %d",
526 fad0fb69 2022-05-28 op imsg.hdr.type);
527 fad0fb69 2022-05-28 op
528 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
529 fad0fb69 2022-05-28 op fatalx("data size mismatch");
530 fad0fb69 2022-05-28 op
531 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
532 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
533 fad0fb69 2022-05-28 op fatalx("received corrupted data");
534 fad0fb69 2022-05-28 op
535 fad0fb69 2022-05-28 op if (!res->monitor[type])
536 fad0fb69 2022-05-28 op break;
537 fad0fb69 2022-05-28 op
538 fad0fb69 2022-05-28 op puts(imsg_name(type));
539 fad0fb69 2022-05-28 op fflush(stdout);
540 6b47a39f 2022-02-21 op break;
541 3baa2617 2022-02-16 op default:
542 3baa2617 2022-02-16 op done = 1;
543 3baa2617 2022-02-16 op break;
544 3baa2617 2022-02-16 op }
545 3baa2617 2022-02-16 op
546 3baa2617 2022-02-16 op imsg_free(&imsg);
547 3baa2617 2022-02-16 op }
548 3baa2617 2022-02-16 op }
549 3baa2617 2022-02-16 op
550 3baa2617 2022-02-16 op end:
551 3baa2617 2022-02-16 op return ret;
552 3baa2617 2022-02-16 op }
553 3baa2617 2022-02-16 op
554 2ddcfa40 2022-07-08 op static int
555 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
556 3baa2617 2022-02-16 op {
557 7e29fc4a 2022-03-03 op int ch;
558 7e29fc4a 2022-03-03 op
559 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
560 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
561 7e29fc4a 2022-03-03 op argc -= optind;
562 7e29fc4a 2022-03-03 op argv += optind;
563 7e29fc4a 2022-03-03 op
564 acaf7eb2 2022-03-05 op if (argc > 0)
565 3baa2617 2022-02-16 op ctl_usage(res->ctl);
566 7e29fc4a 2022-03-03 op
567 3baa2617 2022-02-16 op return ctlaction(res);
568 3baa2617 2022-02-16 op }
569 3baa2617 2022-02-16 op
570 2ddcfa40 2022-07-08 op static int
571 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
572 3baa2617 2022-02-16 op {
573 7e29fc4a 2022-03-03 op int ch;
574 7e29fc4a 2022-03-03 op
575 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
576 3baa2617 2022-02-16 op ctl_usage(res->ctl);
577 7e29fc4a 2022-03-03 op argc -= optind;
578 7e29fc4a 2022-03-03 op argv += optind;
579 7e29fc4a 2022-03-03 op
580 7e29fc4a 2022-03-03 op if (argc == 0)
581 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
582 7e29fc4a 2022-03-03 op res->files = argv;
583 14be015f 2022-02-17 op
584 3baa2617 2022-02-16 op return ctlaction(res);
585 3baa2617 2022-02-16 op }
586 14be015f 2022-02-17 op
587 2ddcfa40 2022-07-08 op static int
588 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
589 8e916714 2022-02-17 op {
590 8e916714 2022-02-17 op int ch;
591 8e916714 2022-02-17 op
592 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
593 8e916714 2022-02-17 op switch (ch) {
594 8e916714 2022-02-17 op case 'p':
595 8e916714 2022-02-17 op res->pretty = 1;
596 8e916714 2022-02-17 op break;
597 8e916714 2022-02-17 op default:
598 8e916714 2022-02-17 op ctl_usage(res->ctl);
599 8e916714 2022-02-17 op }
600 8e916714 2022-02-17 op }
601 8e916714 2022-02-17 op
602 8e916714 2022-02-17 op return ctlaction(res);
603 8e916714 2022-02-17 op }
604 8e916714 2022-02-17 op
605 2ddcfa40 2022-07-08 op static int
606 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
607 14be015f 2022-02-17 op {
608 7e29fc4a 2022-03-03 op int ch;
609 7e29fc4a 2022-03-03 op
610 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
611 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
612 7e29fc4a 2022-03-03 op argc -= optind;
613 7e29fc4a 2022-03-03 op argv += optind;
614 7e29fc4a 2022-03-03 op
615 7e29fc4a 2022-03-03 op if (argc == 0)
616 ec1fb0c7 2022-02-17 op res->file = NULL;
617 7e29fc4a 2022-03-03 op else if (argc == 1)
618 7e29fc4a 2022-03-03 op res->file = argv[0];
619 ec1fb0c7 2022-02-17 op else
620 14be015f 2022-02-17 op ctl_usage(res->ctl);
621 14be015f 2022-02-17 op
622 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
623 14be015f 2022-02-17 op fatal("pledge");
624 14be015f 2022-02-17 op
625 7a427ecd 2022-02-17 op return ctlaction(res);
626 14be015f 2022-02-17 op }
627 14be015f 2022-02-17 op
628 2ddcfa40 2022-07-08 op static int
629 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
630 a913de21 2022-02-17 op {
631 a913de21 2022-02-17 op int ch;
632 a913de21 2022-02-17 op
633 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
634 a913de21 2022-02-17 op ctl_usage(res->ctl);
635 a913de21 2022-02-17 op argc -= optind;
636 a913de21 2022-02-17 op argv += optind;
637 a913de21 2022-02-17 op
638 a913de21 2022-02-17 op if (argc != 1)
639 a913de21 2022-02-17 op ctl_usage(res->ctl);
640 a913de21 2022-02-17 op
641 a913de21 2022-02-17 op res->file = argv[0];
642 310ef57c 2022-02-19 op return ctlaction(res);
643 310ef57c 2022-02-19 op }
644 310ef57c 2022-02-19 op
645 2ddcfa40 2022-07-08 op static int
646 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
647 310ef57c 2022-02-19 op {
648 310ef57c 2022-02-19 op int ch, b;
649 310ef57c 2022-02-19 op
650 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
651 310ef57c 2022-02-19 op ctl_usage(res->ctl);
652 310ef57c 2022-02-19 op argc -= optind;
653 310ef57c 2022-02-19 op argv += optind;
654 310ef57c 2022-02-19 op
655 310ef57c 2022-02-19 op if (argc != 2)
656 310ef57c 2022-02-19 op ctl_usage(res->ctl);
657 310ef57c 2022-02-19 op
658 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
659 310ef57c 2022-02-19 op b = 1;
660 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
661 310ef57c 2022-02-19 op b = 0;
662 310ef57c 2022-02-19 op else
663 310ef57c 2022-02-19 op ctl_usage(res->ctl);
664 310ef57c 2022-02-19 op
665 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
666 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
667 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
668 310ef57c 2022-02-19 op res->rep.repeat_one = b;
669 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
670 310ef57c 2022-02-19 op res->rep.repeat_all = b;
671 310ef57c 2022-02-19 op else
672 310ef57c 2022-02-19 op ctl_usage(res->ctl);
673 90122a37 2022-05-14 op
674 90122a37 2022-05-14 op return ctlaction(res);
675 90122a37 2022-05-14 op }
676 90122a37 2022-05-14 op
677 2ddcfa40 2022-07-08 op static int
678 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
679 90122a37 2022-05-14 op {
680 90122a37 2022-05-14 op int ch;
681 90122a37 2022-05-14 op const char *events;
682 90122a37 2022-05-14 op char *dup, *tmp, *tok;
683 90122a37 2022-05-14 op
684 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
685 90122a37 2022-05-14 op ctl_usage(res->ctl);
686 90122a37 2022-05-14 op argc -= optind;
687 90122a37 2022-05-14 op argv += optind;
688 90122a37 2022-05-14 op
689 90122a37 2022-05-14 op if (argc > 1)
690 90122a37 2022-05-14 op ctl_usage(res->ctl);
691 90122a37 2022-05-14 op
692 90122a37 2022-05-14 op if (argc == 1)
693 90122a37 2022-05-14 op events = *argv;
694 90122a37 2022-05-14 op else
695 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
696 90122a37 2022-05-14 op "jump,repeat,add,load";
697 310ef57c 2022-02-19 op
698 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
699 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
700 90122a37 2022-05-14 op if (*tok == '\0')
701 90122a37 2022-05-14 op continue;
702 90122a37 2022-05-14 op
703 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
704 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
705 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
706 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
707 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
708 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
709 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
710 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
711 90122a37 2022-05-14 op else if (!strcmp(tok, "restart"))
712 90122a37 2022-05-14 op res->monitor[IMSG_CTL_RESTART] = 1;
713 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
714 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
715 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
716 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
717 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
718 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
719 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
720 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
721 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
722 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
723 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
724 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
725 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
726 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
727 90122a37 2022-05-14 op else
728 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
729 90122a37 2022-05-14 op }
730 90122a37 2022-05-14 op
731 90122a37 2022-05-14 op free(dup);
732 e5d4e9f7 2022-07-09 op return ctlaction(res);
733 e5d4e9f7 2022-07-09 op }
734 e5d4e9f7 2022-07-09 op
735 e5d4e9f7 2022-07-09 op static int
736 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
737 e5d4e9f7 2022-07-09 op {
738 e5d4e9f7 2022-07-09 op const char *n, *errstr;
739 e5d4e9f7 2022-07-09 op
740 e5d4e9f7 2022-07-09 op if (argc > 0) {
741 e5d4e9f7 2022-07-09 op /* skip the command name */
742 e5d4e9f7 2022-07-09 op argc--;
743 e5d4e9f7 2022-07-09 op argv++;
744 e5d4e9f7 2022-07-09 op }
745 e5d4e9f7 2022-07-09 op
746 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
747 e5d4e9f7 2022-07-09 op argc--;
748 e5d4e9f7 2022-07-09 op argv++;
749 e5d4e9f7 2022-07-09 op }
750 e5d4e9f7 2022-07-09 op
751 e5d4e9f7 2022-07-09 op if (argc != 1)
752 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
753 e5d4e9f7 2022-07-09 op
754 e5d4e9f7 2022-07-09 op n = *argv;
755 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
756 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
757 e5d4e9f7 2022-07-09 op
758 e5d4e9f7 2022-07-09 op res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
759 e5d4e9f7 2022-07-09 op if (errstr != NULL)
760 e5d4e9f7 2022-07-09 op fatalx("offset is %s: %s", errstr, n);
761 e5d4e9f7 2022-07-09 op
762 a913de21 2022-02-17 op return ctlaction(res);
763 a913de21 2022-02-17 op }
764 a913de21 2022-02-17 op
765 fea541a8 2022-02-16 op static int
766 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
767 3baa2617 2022-02-16 op {
768 1d673950 2022-03-03 op int lockfd;
769 3baa2617 2022-02-16 op
770 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
771 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
772 1d673950 2022-03-03 op return -1;
773 1d673950 2022-03-03 op }
774 3baa2617 2022-02-16 op
775 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
776 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
777 1d673950 2022-03-03 op if (errno != EAGAIN)
778 1d673950 2022-03-03 op return -1;
779 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
780 1d673950 2022-03-03 op /* nop */;
781 1d673950 2022-03-03 op close(lockfd);
782 1d673950 2022-03-03 op return -2;
783 1d673950 2022-03-03 op }
784 1d673950 2022-03-03 op log_debug("flock succeeded");
785 1d673950 2022-03-03 op
786 1d673950 2022-03-03 op return lockfd;
787 1d673950 2022-03-03 op }
788 1d673950 2022-03-03 op
789 1d673950 2022-03-03 op static int
790 1d673950 2022-03-03 op ctl_connect(void)
791 1d673950 2022-03-03 op {
792 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
793 1d673950 2022-03-03 op struct sockaddr_un sa;
794 1d673950 2022-03-03 op size_t size;
795 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
796 1b77ad48 2022-07-08 op int attempt = 0;
797 1d673950 2022-03-03 op char *lockfile = NULL;
798 1d673950 2022-03-03 op
799 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
800 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
801 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
802 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
803 1d673950 2022-03-03 op errno = ENAMETOOLONG;
804 fea541a8 2022-02-16 op return -1;
805 fea541a8 2022-02-16 op }
806 3baa2617 2022-02-16 op
807 1d673950 2022-03-03 op retry:
808 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
809 1d673950 2022-03-03 op return -1;
810 1d673950 2022-03-03 op
811 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
812 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
813 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
814 1d673950 2022-03-03 op goto failed;
815 1b77ad48 2022-07-08 op if (attempt++ == 100)
816 1b77ad48 2022-07-08 op goto failed;
817 1d673950 2022-03-03 op close(fd);
818 1d673950 2022-03-03 op
819 1d673950 2022-03-03 op if (!locked) {
820 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
821 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
822 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
823 1d673950 2022-03-03 op
824 1d673950 2022-03-03 op free(lockfile);
825 1d673950 2022-03-03 op lockfile = NULL;
826 1d673950 2022-03-03 op
827 1d673950 2022-03-03 op if (lockfd == -1)
828 1d673950 2022-03-03 op goto retry;
829 1d673950 2022-03-03 op }
830 1d673950 2022-03-03 op
831 1d673950 2022-03-03 op /*
832 1d673950 2022-03-03 op * Always retry at least once, even if we got
833 1d673950 2022-03-03 op * the lock, because another client could have
834 1d673950 2022-03-03 op * taken the lock, started the server and released
835 1d673950 2022-03-03 op * the lock between our connect() and flock()
836 1d673950 2022-03-03 op */
837 1d673950 2022-03-03 op locked = 1;
838 1d673950 2022-03-03 op goto retry;
839 1d673950 2022-03-03 op }
840 1d673950 2022-03-03 op
841 1d673950 2022-03-03 op if (!spawned) {
842 1d673950 2022-03-03 op log_debug("spawning the daemon");
843 1d673950 2022-03-03 op spawn_daemon();
844 1d673950 2022-03-03 op spawned = 1;
845 1d673950 2022-03-03 op }
846 1d673950 2022-03-03 op
847 1d673950 2022-03-03 op nanosleep(&ts, NULL);
848 1d673950 2022-03-03 op goto retry;
849 1d673950 2022-03-03 op }
850 1d673950 2022-03-03 op
851 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
852 1d673950 2022-03-03 op unlink(lockfile);
853 1d673950 2022-03-03 op free(lockfile);
854 1d673950 2022-03-03 op close(lockfd);
855 1d673950 2022-03-03 op }
856 1d673950 2022-03-03 op return fd;
857 1d673950 2022-03-03 op
858 1d673950 2022-03-03 op failed:
859 1d673950 2022-03-03 op if (locked) {
860 1d673950 2022-03-03 op free(lockfile);
861 1d673950 2022-03-03 op close(lockfd);
862 1d673950 2022-03-03 op }
863 1d673950 2022-03-03 op close(fd);
864 1d673950 2022-03-03 op return -1;
865 fea541a8 2022-02-16 op }
866 fea541a8 2022-02-16 op
867 fea541a8 2022-02-16 op __dead void
868 fea541a8 2022-02-16 op ctl(int argc, char **argv)
869 fea541a8 2022-02-16 op {
870 1d673950 2022-03-03 op int ctl_sock;
871 3baa2617 2022-02-16 op
872 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
873 fea541a8 2022-02-16 op log_setverbose(verbose);
874 fea541a8 2022-02-16 op
875 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
876 96233476 2022-06-09 op fatal("getcwd");
877 96233476 2022-06-09 op
878 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
879 1d673950 2022-03-03 op fatal("can't connect");
880 fea541a8 2022-02-16 op
881 fea541a8 2022-02-16 op if (ctl_sock == -1)
882 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
883 fea541a8 2022-02-16 op
884 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
885 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
886 3baa2617 2022-02-16 op
887 3baa2617 2022-02-16 op optreset = 1;
888 3baa2617 2022-02-16 op optind = 1;
889 3baa2617 2022-02-16 op
890 3baa2617 2022-02-16 op exit(parse(argc, argv));
891 3baa2617 2022-02-16 op }