Blame


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