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