002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
003
2022-06-09
op
* Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
005
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
006
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
007
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
009
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
010
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
011
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
012
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
013
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
014
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
015
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
018
2022-02-16
op
#include <sys/types.h>
019
2022-02-16
op
#include <sys/socket.h>
020
2022-02-16
op
#include <sys/queue.h>
021
2022-02-16
op
#include <sys/uio.h>
022
2022-02-16
op
#include <sys/un.h>
024
2022-02-16
op
#include <errno.h>
025
2022-02-16
op
#include <event.h>
026
2022-03-03
op
#include <fcntl.h>
027
2022-02-16
op
#include <limits.h>
028
2022-02-16
op
#include <stdio.h>
029
2022-02-16
op
#include <stdlib.h>
030
2022-02-16
op
#include <stdint.h>
031
2022-02-16
op
#include <string.h>
032
2022-02-16
op
#include <syslog.h>
033
2022-02-16
op
#include <unistd.h>
034
2022-02-16
op
#include <imsg.h>
036
2022-02-16
op
#include "amused.h"
037
2022-02-16
op
#include "log.h"
038
2022-02-16
op
#include "playlist.h"
039
2022-02-16
op
#include "xmalloc.h"
041
2022-06-09
op
static struct imsgbuf *ibuf;
042
2022-06-09
op
char cwd[PATH_MAX];
044
2022-02-16
op
int ctl_noarg(struct parse_result *, int, char **);
045
2022-02-16
op
int ctl_add(struct parse_result *, int, char **);
046
2022-02-17
op
int ctl_show(struct parse_result *, int, char **);
047
2022-02-17
op
int ctl_load(struct parse_result *, int, char **);
048
2022-02-17
op
int ctl_jump(struct parse_result *, int, char **);
049
2022-02-19
op
int ctl_repeat(struct parse_result *, int, char **);
050
2022-05-14
op
int ctl_monitor(struct parse_result *, int, char **);
052
2022-02-16
op
struct ctl_command ctl_commands[] = {
053
2022-02-16
op
{ "play", PLAY, ctl_noarg, "" },
054
2022-02-16
op
{ "pause", PAUSE, ctl_noarg, "" },
055
2022-02-16
op
{ "toggle", TOGGLE, ctl_noarg, "" },
056
2022-02-16
op
{ "stop", STOP, ctl_noarg, "" },
057
2022-02-16
op
{ "restart", RESTART, ctl_noarg, "" },
058
2022-06-09
op
{ "add", ADD, ctl_add, "files..." },
059
2022-02-16
op
{ "flush", FLUSH, ctl_noarg, "" },
060
2022-02-19
op
{ "show", SHOW, ctl_show, "[-p]" },
061
2022-02-16
op
{ "status", STATUS, ctl_noarg, "" },
062
2022-02-17
op
{ "next", NEXT, ctl_noarg, "" },
063
2022-02-17
op
{ "prev", PREV, ctl_noarg, "" },
064
2022-02-17
op
{ "load", LOAD, ctl_load, "[file]", 1 },
065
2022-02-17
op
{ "jump", JUMP, ctl_jump, "pattern" },
066
2022-02-19
op
{ "repeat", REPEAT, ctl_repeat, "one|all on|off" },
067
2022-05-14
op
{ "monitor", MONITOR, ctl_monitor, "[events]" },
068
2022-02-16
op
{ NULL },
071
2022-02-16
op
__dead void
072
2022-02-16
op
usage(void)
074
2022-02-16
op
fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
078
2022-02-16
op
static __dead void
079
2022-02-16
op
ctl_usage(struct ctl_command *ctl)
081
2022-02-16
op
fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
082
2022-02-16
op
ctl->name, ctl->usage);
086
2022-06-09
op
/* based on canonpath from kern_pledge.c */
087
2022-02-16
op
static int
088
2022-06-09
op
canonpath(const char *input, char *buf, size_t bufsize)
090
2022-06-09
op
const char *p;
091
2022-06-09
op
char *q, path[PATH_MAX];
093
2022-06-09
op
if (input[0] != '/') {
094
2022-06-09
op
if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
095
2022-06-09
op
>= sizeof(path)) {
096
2022-06-09
op
errno = ENAMETOOLONG;
097
2022-06-09
op
return -1;
099
2022-06-09
op
input = path;
102
2022-06-09
op
p = input;
104
2022-06-09
op
while (*p && (q - buf < bufsize)) {
105
2022-06-09
op
if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
108
2022-06-09
op
} else if (p[0] == '/' && p[1] == '.' &&
109
2022-06-09
op
(p[2] == '/' || p[2] == '\0')) {
112
2022-06-09
op
} else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
113
2022-06-09
op
(p[3] == '/' || p[3] == '\0')) {
115
2022-06-09
op
if (q != buf) /* "/../" at start of buf */
116
2022-06-09
op
while (*--q != '/')
117
2022-06-09
op
continue;
120
2022-06-09
op
*q++ = *p++;
123
2022-06-09
op
if ((*p == '\0') && (q - buf < bufsize)) {
125
2022-06-09
op
return 0;
127
2022-06-09
op
errno = ENAMETOOLONG;
128
2022-06-09
op
return -1;
132
2022-06-09
op
static int
133
2022-02-16
op
parse(int argc, char **argv)
135
2022-02-16
op
struct ctl_command *ctl = NULL;
136
2022-02-16
op
struct parse_result res;
137
2022-03-03
op
const char *argv0;
138
2022-02-16
op
int i, status;
140
2022-02-16
op
memset(&res, 0, sizeof(res));
142
2022-03-03
op
if ((argv0 = argv[0]) == NULL)
143
2022-03-03
op
argv0 = "status";
145
2022-02-16
op
for (i = 0; ctl_commands[i].name != NULL; ++i) {
146
2022-03-03
op
if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
148
2022-02-16
op
if (ctl != NULL) {
149
2022-02-16
op
fprintf(stderr,
150
2022-03-03
op
"ambiguous argument: %s\n", argv0);
153
2022-02-16
op
ctl = &ctl_commands[i];
157
2022-02-16
op
if (ctl == NULL) {
158
2022-02-16
op
fprintf(stderr, "unknown argument: %s\n", argv[0]);
162
2022-02-16
op
res.action = ctl->action;
163
2022-02-16
op
res.ctl = ctl;
165
2022-02-16
op
if (!ctl->has_pledge) {
166
2022-02-16
op
/* pledge(2) default if command doesn't have its own */
167
2022-02-16
op
if (pledge("stdio", NULL) == -1)
168
2022-02-16
op
fatal("pledge");
171
2022-02-16
op
status = ctl->main(&res, argc, argv);
172
2022-02-16
op
close(ibuf->fd);
173
2022-02-16
op
free(ibuf);
174
2022-02-16
op
return status;
177
2022-02-16
op
static int
178
2022-05-28
op
load_files(struct parse_result *res, int *ret)
181
2022-02-17
op
const char *file;
182
2022-02-17
op
char *line = NULL;
183
2022-06-09
op
char path[PATH_MAX];
184
2022-06-09
op
size_t linesize = 0, i = 0;
185
2022-03-02
op
ssize_t linelen, curr = -1;
187
2022-02-17
op
if (res->file == NULL)
188
2022-02-17
op
f = stdin;
189
2022-02-17
op
else if ((f = fopen(res->file, "r")) == NULL) {
190
2022-02-17
op
log_warn("can't open %s", res->file);
191
2022-02-17
op
*ret = 1;
192
2022-02-17
op
return 1;
195
2022-02-17
op
while ((linelen = getline(&line, &linesize, f)) != -1) {
196
2022-02-17
op
if (linelen == 0)
197
2022-02-17
op
continue;
198
2022-02-17
op
line[linelen-1] = '\0';
199
2022-02-17
op
file = line;
200
2022-03-25
op
if (!strncmp(file, "> ", 2)) {
201
2022-02-17
op
file += 2;
202
2022-03-02
op
curr = i;
203
2022-03-25
op
} else if (!strncmp(file, " ", 2))
204
2022-02-17
op
file += 2;
206
2022-06-13
op
memset(path, 0, sizeof(path));
207
2022-06-09
op
if (canonpath(file, path, sizeof(path)) == -1) {
208
2022-06-13
op
log_warn("canonpath %s", file);
209
2022-02-17
op
continue;
213
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
214
2022-02-17
op
path, sizeof(path));
217
2022-02-17
op
free(line);
218
2022-02-17
op
if (ferror(f))
219
2022-02-17
op
fatal("getline");
220
2022-02-17
op
fclose(f);
222
2022-03-02
op
if (i == 0) {
223
2022-02-17
op
*ret = 1;
224
2022-02-17
op
return 1;
227
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
228
2022-03-02
op
&curr, sizeof(curr));
229
2022-02-17
op
imsg_flush(ibuf);
230
2022-02-17
op
return 0;
233
2022-05-28
op
static const char *
234
2022-05-28
op
imsg_strerror(struct imsg *imsg)
236
2022-05-28
op
size_t datalen;
237
2022-05-28
op
const char *msg;
239
2022-05-28
op
datalen = IMSG_DATA_SIZE(*imsg);
240
2022-05-28
op
msg = imsg->data;
241
2022-05-28
op
if (datalen == 0 || msg[datalen-1] != '\0')
242
2022-05-28
op
fatalx("malformed error message");
244
2022-05-28
op
return msg;
247
2022-05-28
op
static const char *
248
2022-05-28
op
imsg_name(int type)
250
2022-02-21
op
switch (type) {
251
2022-02-21
op
case IMSG_CTL_PLAY:
252
2022-05-28
op
return "play";
253
2022-02-21
op
case IMSG_CTL_TOGGLE_PLAY:
254
2022-05-28
op
return "toggle";
255
2022-02-21
op
case IMSG_CTL_PAUSE:
256
2022-05-28
op
return "pause";
257
2022-02-21
op
case IMSG_CTL_STOP:
258
2022-05-28
op
return "stop";
259
2022-02-21
op
case IMSG_CTL_RESTART:
260
2022-05-28
op
return "restart";
261
2022-02-21
op
case IMSG_CTL_FLUSH:
262
2022-05-28
op
return "flush";
263
2022-02-21
op
case IMSG_CTL_NEXT:
264
2022-05-28
op
return "next";
265
2022-02-21
op
case IMSG_CTL_PREV:
266
2022-05-28
op
return "prev";
267
2022-02-21
op
case IMSG_CTL_JUMP:
268
2022-05-28
op
return "jump";
269
2022-02-21
op
case IMSG_CTL_REPEAT:
270
2022-05-28
op
return "repeat";
271
2022-02-21
op
case IMSG_CTL_ADD:
272
2022-05-28
op
return "add";
273
2022-02-21
op
case IMSG_CTL_COMMIT:
274
2022-05-28
op
return "load";
276
2022-05-28
op
return "unknown";
280
2022-02-21
op
static int
281
2022-02-16
op
ctlaction(struct parse_result *res)
283
2022-05-28
op
char path[PATH_MAX];
284
2022-02-16
op
struct imsg imsg;
285
2022-05-28
op
struct player_status ps;
286
2022-05-28
op
size_t datalen;
287
2022-02-16
op
ssize_t n;
288
2022-05-28
op
int i, type, ret = 0, done = 1;
290
2022-02-16
op
switch (res->action) {
291
2022-02-16
op
case PLAY:
292
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
293
2022-02-19
op
if (verbose) {
294
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
295
2022-02-19
op
NULL, 0);
296
2022-02-19
op
done = 0;
299
2022-02-16
op
case PAUSE:
300
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
302
2022-02-16
op
case TOGGLE:
303
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
304
2022-02-19
op
if (verbose) {
305
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
306
2022-02-19
op
NULL, 0);
307
2022-02-19
op
done = 0;
310
2022-02-16
op
case STOP:
311
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
313
2022-02-16
op
case RESTART:
314
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
315
2022-02-19
op
if (verbose) {
316
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
317
2022-02-19
op
NULL, 0);
318
2022-02-19
op
done = 0;
321
2022-02-16
op
case ADD:
322
2022-02-16
op
done = 0;
323
2022-05-29
op
for (i = 0; res->files[i] != NULL; ++i) {
324
2022-06-13
op
memset(path, 0, sizeof(path));
325
2022-06-09
op
if (canonpath(res->files[i], path, sizeof(path))
327
2022-06-09
op
log_warn("canonpath %s", res->files[i]);
328
2022-05-28
op
continue;
331
2022-05-28
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
332
2022-05-28
op
path, sizeof(path));
334
2022-05-28
op
ret = i == 0;
336
2022-02-16
op
case FLUSH:
337
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
339
2022-02-16
op
case SHOW:
340
2022-02-16
op
done = 0;
341
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
343
2022-02-16
op
case STATUS:
344
2022-02-16
op
done = 0;
345
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
347
2022-02-17
op
case NEXT:
348
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
349
2022-02-19
op
if (verbose) {
350
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
351
2022-02-19
op
NULL, 0);
352
2022-02-19
op
done = 0;
355
2022-02-17
op
case PREV:
356
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
357
2022-02-19
op
if (verbose) {
358
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
359
2022-02-19
op
NULL, 0);
360
2022-02-19
op
done = 0;
363
2022-02-17
op
case LOAD:
364
2022-02-17
op
done = 0;
365
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
367
2022-02-17
op
case JUMP:
368
2022-02-17
op
done = 0;
369
2022-05-28
op
memset(path, 0, sizeof(path));
370
2022-05-28
op
strlcpy(path, res->file, sizeof(path));
371
2022-05-28
op
imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
372
2022-05-28
op
path, sizeof(path));
374
2022-02-19
op
case REPEAT:
375
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
376
2022-02-19
op
&res->rep, sizeof(res->rep));
378
2022-02-21
op
case MONITOR:
379
2022-02-21
op
done = 0;
380
2022-02-21
op
imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
381
2022-02-21
op
NULL, 0);
383
2022-02-16
op
case NONE:
384
2022-02-16
op
/* action not expected */
385
2022-02-16
op
fatalx("invalid action %u", res->action);
389
2022-02-16
op
if (ret != 0)
390
2022-02-16
op
goto end;
392
2022-02-16
op
imsg_flush(ibuf);
395
2022-02-16
op
while (!done) {
396
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
397
2022-02-16
op
fatalx("imsg_read error");
398
2022-02-16
op
if (n == 0)
399
2022-02-16
op
fatalx("pipe closed");
401
2022-02-16
op
while (!done) {
402
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
403
2022-02-16
op
fatalx("imsg_get error");
404
2022-02-16
op
if (n == 0)
407
2022-05-28
op
if (imsg.hdr.type == IMSG_CTL_ERR) {
408
2022-05-28
op
log_warnx("%s: %s", res->ctl->name,
409
2022-05-28
op
imsg_strerror(&imsg));
411
2022-05-28
op
done = 1;
415
2022-05-28
op
datalen = IMSG_DATA_SIZE(imsg);
417
2022-02-16
op
switch (res->action) {
418
2022-02-16
op
case ADD:
419
2022-05-29
op
if (res->files[i] == NULL)
420
2022-05-28
op
fatalx("received more replies than "
421
2022-05-28
op
"files enqueued.");
423
2022-05-28
op
if (imsg.hdr.type == IMSG_CTL_ADD)
424
2022-05-29
op
log_debug("enqueued %s", res->files[i]);
426
2022-05-28
op
fatalx("invalid message %d",
427
2022-05-28
op
imsg.hdr.type);
429
2022-05-29
op
done = res->files[i] == NULL;
431
2022-02-16
op
case SHOW:
432
2022-05-28
op
if (datalen == 0) {
433
2022-05-28
op
done = 1;
436
2022-05-28
op
if (datalen != sizeof(ps))
437
2022-05-28
op
fatalx("data size mismatch");
438
2022-05-28
op
memcpy(&ps, imsg.data, sizeof(ps));
439
2022-05-28
op
if (ps.path[sizeof(ps.path) - 1] != '\0')
440
2022-05-28
op
fatalx("received corrupted data");
441
2022-05-28
op
if (res->pretty) {
442
2022-05-28
op
char c = ' ';
443
2022-05-28
op
if (ps.status == STATE_PLAYING)
445
2022-05-28
op
printf("%c ", c);
447
2022-05-28
op
puts(ps.path);
449
2022-02-17
op
case PLAY:
450
2022-02-17
op
case TOGGLE:
451
2022-02-17
op
case RESTART:
452
2022-02-16
op
case STATUS:
453
2022-02-17
op
case NEXT:
454
2022-02-17
op
case PREV:
455
2022-02-17
op
case JUMP:
456
2022-05-28
op
if (imsg.hdr.type != IMSG_CTL_STATUS)
457
2022-05-28
op
fatalx("invalid message %d",
458
2022-05-28
op
imsg.hdr.type);
460
2022-05-28
op
if (datalen != sizeof(ps))
461
2022-05-28
op
fatalx("data size mismatch");
462
2022-05-28
op
memcpy(&ps, imsg.data, sizeof(ps));
463
2022-05-28
op
if (ps.path[sizeof(ps.path) - 1] != '\0')
464
2022-05-28
op
fatalx("received corrupted data");
466
2022-05-28
op
if (ps.status == STATE_STOPPED)
467
2022-05-28
op
printf("stopped ");
468
2022-05-28
op
else if (ps.status == STATE_PLAYING)
469
2022-05-28
op
printf("playing ");
470
2022-05-28
op
else if (ps.status == STATE_PAUSED)
471
2022-05-28
op
printf("paused ");
473
2022-05-28
op
printf("unknown ");
475
2022-05-28
op
puts(ps.path);
476
2022-05-28
op
printf("repat one %s\nrepeat all %s\n",
477
2022-05-28
op
ps.rp.repeat_one ? "on" : "off",
478
2022-05-28
op
ps.rp.repeat_all ? "on" : "off");
480
2022-05-28
op
done = 1;
482
2022-02-17
op
case LOAD:
483
2022-05-28
op
if (imsg.hdr.type == IMSG_CTL_ADD)
485
2022-05-28
op
if (imsg.hdr.type == IMSG_CTL_COMMIT) {
486
2022-05-28
op
done = 1;
490
2022-05-28
op
if (imsg.hdr.type != IMSG_CTL_BEGIN)
491
2022-05-28
op
fatalx("invalid message %d",
492
2022-05-28
op
imsg.hdr.type);
494
2022-05-28
op
load_files(res, &ret);
496
2022-02-21
op
case MONITOR:
497
2022-05-28
op
if (imsg.hdr.type != IMSG_CTL_MONITOR)
498
2022-05-28
op
fatalx("invalid message %d",
499
2022-05-28
op
imsg.hdr.type);
501
2022-05-28
op
if (datalen != sizeof(type))
502
2022-05-28
op
fatalx("data size mismatch");
504
2022-05-28
op
memcpy(&type, imsg.data, sizeof(type));
505
2022-05-28
op
if (type < 0 || type > IMSG__LAST)
506
2022-05-28
op
fatalx("received corrupted data");
508
2022-05-28
op
if (!res->monitor[type])
511
2022-05-28
op
puts(imsg_name(type));
512
2022-05-28
op
fflush(stdout);
515
2022-02-16
op
done = 1;
519
2022-02-16
op
imsg_free(&imsg);
524
2022-02-16
op
return ret;
528
2022-02-16
op
ctl_noarg(struct parse_result *res, int argc, char **argv)
532
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
533
2022-03-03
op
ctl_usage(res->ctl);
534
2022-03-03
op
argc -= optind;
535
2022-03-03
op
argv += optind;
537
2022-03-05
op
if (argc > 0)
538
2022-02-16
op
ctl_usage(res->ctl);
540
2022-02-16
op
return ctlaction(res);
544
2022-02-16
op
ctl_add(struct parse_result *res, int argc, char **argv)
548
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
549
2022-02-16
op
ctl_usage(res->ctl);
550
2022-03-03
op
argc -= optind;
551
2022-03-03
op
argv += optind;
553
2022-03-03
op
if (argc == 0)
554
2022-03-03
op
ctl_usage(res->ctl);
555
2022-03-03
op
res->files = argv;
557
2022-02-16
op
return ctlaction(res);
561
2022-02-17
op
ctl_show(struct parse_result *res, int argc, char **argv)
565
2022-02-17
op
while ((ch = getopt(argc, argv, "p")) != -1) {
566
2022-02-17
op
switch (ch) {
567
2022-02-17
op
case 'p':
568
2022-02-17
op
res->pretty = 1;
571
2022-02-17
op
ctl_usage(res->ctl);
575
2022-02-17
op
return ctlaction(res);
579
2022-02-17
op
ctl_load(struct parse_result *res, int argc, char **argv)
583
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
584
2022-03-03
op
ctl_usage(res->ctl);
585
2022-03-03
op
argc -= optind;
586
2022-03-03
op
argv += optind;
588
2022-03-03
op
if (argc == 0)
589
2022-02-17
op
res->file = NULL;
590
2022-03-03
op
else if (argc == 1)
591
2022-03-03
op
res->file = argv[0];
593
2022-02-17
op
ctl_usage(res->ctl);
595
2022-02-17
op
if (pledge("stdio rpath", NULL) == -1)
596
2022-02-17
op
fatal("pledge");
598
2022-02-17
op
return ctlaction(res);
602
2022-02-17
op
ctl_jump(struct parse_result *res, int argc, char **argv)
606
2022-02-17
op
while ((ch = getopt(argc, argv, "")) != -1)
607
2022-02-17
op
ctl_usage(res->ctl);
608
2022-02-17
op
argc -= optind;
609
2022-02-17
op
argv += optind;
611
2022-02-17
op
if (argc != 1)
612
2022-02-17
op
ctl_usage(res->ctl);
614
2022-02-17
op
res->file = argv[0];
615
2022-02-19
op
return ctlaction(res);
619
2022-02-19
op
ctl_repeat(struct parse_result *res, int argc, char **argv)
621
2022-02-19
op
int ch, b;
623
2022-02-19
op
while ((ch = getopt(argc, argv, "")) != -1)
624
2022-02-19
op
ctl_usage(res->ctl);
625
2022-02-19
op
argc -= optind;
626
2022-02-19
op
argv += optind;
628
2022-02-19
op
if (argc != 2)
629
2022-02-19
op
ctl_usage(res->ctl);
631
2022-02-19
op
if (!strcmp(argv[1], "on"))
633
2022-02-19
op
else if (!strcmp(argv[1], "off"))
636
2022-02-19
op
ctl_usage(res->ctl);
638
2022-02-19
op
res->rep.repeat_one = -1;
639
2022-02-19
op
res->rep.repeat_all = -1;
640
2022-02-19
op
if (!strcmp(argv[0], "one"))
641
2022-02-19
op
res->rep.repeat_one = b;
642
2022-02-19
op
else if (!strcmp(argv[0], "all"))
643
2022-02-19
op
res->rep.repeat_all = b;
645
2022-02-19
op
ctl_usage(res->ctl);
647
2022-05-14
op
return ctlaction(res);
651
2022-05-14
op
ctl_monitor(struct parse_result *res, int argc, char **argv)
654
2022-05-14
op
const char *events;
655
2022-05-14
op
char *dup, *tmp, *tok;
657
2022-05-14
op
while ((ch = getopt(argc, argv, "")) != -1)
658
2022-05-14
op
ctl_usage(res->ctl);
659
2022-05-14
op
argc -= optind;
660
2022-05-14
op
argv += optind;
662
2022-05-14
op
if (argc > 1)
663
2022-05-14
op
ctl_usage(res->ctl);
665
2022-05-14
op
if (argc == 1)
666
2022-05-14
op
events = *argv;
668
2022-05-14
op
events = "play,toggle,pause,stop,restart,flush,next,prev,"
669
2022-05-14
op
"jump,repeat,add,load";
671
2022-05-14
op
tmp = dup = xstrdup(events);
672
2022-05-14
op
while ((tok = strsep(&tmp, ",")) != NULL) {
673
2022-05-14
op
if (*tok == '\0')
674
2022-05-14
op
continue;
676
2022-05-14
op
if (!strcmp(tok, "play"))
677
2022-05-14
op
res->monitor[IMSG_CTL_PLAY] = 1;
678
2022-05-14
op
else if (!strcmp(tok, "toggle"))
679
2022-05-14
op
res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
680
2022-05-14
op
else if (!strcmp(tok, "pause"))
681
2022-05-14
op
res->monitor[IMSG_CTL_PAUSE] = 1;
682
2022-05-14
op
else if (!strcmp(tok, "stop"))
683
2022-05-14
op
res->monitor[IMSG_CTL_STOP] = 1;
684
2022-05-14
op
else if (!strcmp(tok, "restart"))
685
2022-05-14
op
res->monitor[IMSG_CTL_RESTART] = 1;
686
2022-05-14
op
else if (!strcmp(tok, "flush"))
687
2022-05-14
op
res->monitor[IMSG_CTL_FLUSH] = 1;
688
2022-05-14
op
else if (!strcmp(tok, "next"))
689
2022-05-14
op
res->monitor[IMSG_CTL_NEXT] = 1;
690
2022-05-14
op
else if (!strcmp(tok, "prev"))
691
2022-05-14
op
res->monitor[IMSG_CTL_PREV] = 1;
692
2022-05-14
op
else if (!strcmp(tok, "jump"))
693
2022-05-14
op
res->monitor[IMSG_CTL_JUMP] = 1;
694
2022-05-14
op
else if (!strcmp(tok, "repeat"))
695
2022-05-14
op
res->monitor[IMSG_CTL_REPEAT] = 1;
696
2022-05-14
op
else if (!strcmp(tok, "add"))
697
2022-05-14
op
res->monitor[IMSG_CTL_ADD] = 1;
698
2022-05-14
op
else if (!strcmp(tok, "load"))
699
2022-05-14
op
res->monitor[IMSG_CTL_COMMIT] = 1;
701
2022-05-14
op
fatalx("unknown event \"%s\"", tok);
704
2022-05-14
op
free(dup);
705
2022-02-17
op
return ctlaction(res);
708
2022-02-16
op
static int
709
2022-03-03
op
ctl_get_lock(const char *lockfile)
711
2022-03-03
op
int lockfd;
713
2022-03-03
op
if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
714
2022-03-03
op
log_debug("open failed: %s", strerror(errno));
715
2022-03-03
op
return -1;
718
2022-03-03
op
if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
719
2022-03-03
op
log_debug("flock failed: %s", strerror(errno));
720
2022-03-03
op
if (errno != EAGAIN)
721
2022-03-03
op
return -1;
722
2022-03-03
op
while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
723
2022-03-03
op
/* nop */;
724
2022-03-03
op
close(lockfd);
725
2022-03-03
op
return -2;
727
2022-03-03
op
log_debug("flock succeeded");
729
2022-03-03
op
return lockfd;
732
2022-03-03
op
static int
733
2022-03-03
op
ctl_connect(void)
735
2022-03-03
op
struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
736
2022-03-03
op
struct sockaddr_un sa;
737
2022-03-03
op
size_t size;
738
2022-03-03
op
int fd, lockfd = -1, locked = 0, spawned = 0;
739
2022-03-03
op
char *lockfile = NULL;
741
2022-03-03
op
memset(&sa, 0, sizeof(sa));
742
2022-03-03
op
sa.sun_family = AF_UNIX;
743
2022-03-03
op
size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
744
2022-03-03
op
if (size >= sizeof(sa.sun_path)) {
745
2022-03-03
op
errno = ENAMETOOLONG;
746
2022-02-16
op
return -1;
750
2022-03-03
op
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
751
2022-03-03
op
return -1;
753
2022-03-03
op
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
754
2022-03-03
op
log_debug("connection failed: %s", strerror(errno));
755
2022-03-03
op
if (errno != ECONNREFUSED && errno != ENOENT)
756
2022-03-03
op
goto failed;
757
2022-03-03
op
close(fd);
759
2022-03-03
op
if (!locked) {
760
2022-03-03
op
xasprintf(&lockfile, "%s.lock", csock);
761
2022-03-03
op
if ((lockfd = ctl_get_lock(lockfile)) < 0) {
762
2022-03-03
op
log_debug("didn't get the lock (%d)", lockfd);
764
2022-03-03
op
free(lockfile);
765
2022-03-03
op
lockfile = NULL;
767
2022-03-03
op
if (lockfd == -1)
768
2022-03-03
op
goto retry;
772
2022-03-03
op
* Always retry at least once, even if we got
773
2022-03-03
op
* the lock, because another client could have
774
2022-03-03
op
* taken the lock, started the server and released
775
2022-03-03
op
* the lock between our connect() and flock()
777
2022-03-03
op
locked = 1;
778
2022-03-03
op
goto retry;
781
2022-03-03
op
if (!spawned) {
782
2022-03-03
op
log_debug("spawning the daemon");
783
2022-03-03
op
spawn_daemon();
784
2022-03-03
op
spawned = 1;
787
2022-03-03
op
nanosleep(&ts, NULL);
788
2022-03-03
op
goto retry;
791
2022-03-03
op
if (locked && lockfd >= 0) {
792
2022-03-03
op
unlink(lockfile);
793
2022-03-03
op
free(lockfile);
794
2022-03-03
op
close(lockfd);
796
2022-03-03
op
return fd;
799
2022-03-03
op
if (locked) {
800
2022-03-03
op
free(lockfile);
801
2022-03-03
op
close(lockfd);
803
2022-03-03
op
close(fd);
804
2022-03-03
op
return -1;
807
2022-02-16
op
__dead void
808
2022-02-16
op
ctl(int argc, char **argv)
810
2022-03-03
op
int ctl_sock;
812
2022-02-16
op
log_init(1, LOG_DAEMON);
813
2022-02-16
op
log_setverbose(verbose);
815
2022-06-09
op
if (getcwd(cwd, sizeof(cwd)) == NULL)
816
2022-06-09
op
fatal("getcwd");
818
2022-03-03
op
if ((ctl_sock = ctl_connect()) == -1)
819
2022-03-03
op
fatal("can't connect");
821
2022-02-16
op
if (ctl_sock == -1)
822
2022-02-16
op
fatalx("failed to connect to the daemon");
824
2022-02-16
op
ibuf = xmalloc(sizeof(*ibuf));
825
2022-02-16
op
imsg_init(ibuf, ctl_sock);
827
2022-02-16
op
optreset = 1;
828
2022-02-16
op
optind = 1;
830
2022-02-16
op
exit(parse(argc, argv));