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