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