002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
004
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
008
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2022-02-16
op
#include <sys/types.h>
018
2022-02-16
op
#include <sys/socket.h>
019
2022-02-16
op
#include <sys/queue.h>
020
2022-02-16
op
#include <sys/uio.h>
021
2022-02-16
op
#include <sys/un.h>
023
2022-02-16
op
#include <errno.h>
024
2022-02-16
op
#include <event.h>
025
2022-03-03
op
#include <fcntl.h>
026
2022-02-16
op
#include <limits.h>
027
2022-02-16
op
#include <sndio.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-02-16
op
static struct imsgbuf *ibuf;
043
2022-02-16
op
int ctl_noarg(struct parse_result *, int, char **);
044
2022-02-16
op
int ctl_add(struct parse_result *, int, char **);
045
2022-02-17
op
int ctl_show(struct parse_result *, int, char **);
046
2022-02-17
op
int ctl_load(struct parse_result *, int, char **);
047
2022-02-17
op
int ctl_jump(struct parse_result *, int, char **);
048
2022-02-19
op
int ctl_repeat(struct parse_result *, int, char **);
050
2022-02-16
op
struct ctl_command ctl_commands[] = {
051
2022-02-16
op
{ "play", PLAY, ctl_noarg, "" },
052
2022-02-16
op
{ "pause", PAUSE, ctl_noarg, "" },
053
2022-02-16
op
{ "toggle", TOGGLE, ctl_noarg, "" },
054
2022-02-16
op
{ "stop", STOP, ctl_noarg, "" },
055
2022-02-16
op
{ "restart", RESTART, ctl_noarg, "" },
056
2022-02-16
op
{ "add", ADD, ctl_add, "files...", 1 },
057
2022-02-16
op
{ "flush", FLUSH, ctl_noarg, "" },
058
2022-02-19
op
{ "show", SHOW, ctl_show, "[-p]" },
059
2022-02-16
op
{ "status", STATUS, ctl_noarg, "" },
060
2022-02-17
op
{ "next", NEXT, ctl_noarg, "" },
061
2022-02-17
op
{ "prev", PREV, ctl_noarg, "" },
062
2022-02-17
op
{ "load", LOAD, ctl_load, "[file]", 1 },
063
2022-02-17
op
{ "jump", JUMP, ctl_jump, "pattern" },
064
2022-02-19
op
{ "repeat", REPEAT, ctl_repeat, "one|all on|off" },
065
2022-02-21
op
{ "monitor", MONITOR, ctl_noarg, "" },
066
2022-02-16
op
{ NULL },
069
2022-02-16
op
__dead void
070
2022-02-16
op
usage(void)
072
2022-02-16
op
fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
076
2022-02-16
op
static __dead void
077
2022-02-16
op
ctl_usage(struct ctl_command *ctl)
079
2022-02-16
op
fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
080
2022-02-16
op
ctl->name, ctl->usage);
084
2022-02-16
op
static int
085
2022-02-16
op
parse(int argc, char **argv)
087
2022-02-16
op
struct ctl_command *ctl = NULL;
088
2022-02-16
op
struct parse_result res;
089
2022-03-03
op
const char *argv0;
090
2022-02-16
op
int i, status;
092
2022-02-16
op
memset(&res, 0, sizeof(res));
094
2022-03-03
op
if ((argv0 = argv[0]) == NULL)
095
2022-03-03
op
argv0 = "status";
097
2022-02-16
op
for (i = 0; ctl_commands[i].name != NULL; ++i) {
098
2022-03-03
op
if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
100
2022-02-16
op
if (ctl != NULL) {
101
2022-02-16
op
fprintf(stderr,
102
2022-03-03
op
"ambiguous argument: %s\n", argv0);
105
2022-02-16
op
ctl = &ctl_commands[i];
109
2022-02-16
op
if (ctl == NULL) {
110
2022-02-16
op
fprintf(stderr, "unknown argument: %s\n", argv[0]);
114
2022-02-16
op
res.action = ctl->action;
115
2022-02-16
op
res.ctl = ctl;
117
2022-02-16
op
if (!ctl->has_pledge) {
118
2022-02-16
op
/* pledge(2) default if command doesn't have its own */
119
2022-02-16
op
if (pledge("stdio", NULL) == -1)
120
2022-02-16
op
fatal("pledge");
123
2022-02-16
op
status = ctl->main(&res, argc, argv);
124
2022-02-16
op
close(ibuf->fd);
125
2022-02-16
op
free(ibuf);
126
2022-02-16
op
return status;
129
2022-02-16
op
static int
130
2022-02-16
op
enqueue_tracks(char **files)
132
2022-02-16
op
char res[PATH_MAX];
133
2022-02-16
op
int enq = 0;
135
2022-02-16
op
for (; *files != NULL; ++files) {
136
2022-02-16
op
memset(&res, 0, sizeof(res));
137
2022-02-16
op
if (realpath(*files, res) == NULL) {
138
2022-02-16
op
log_warn("realpath %s", *files);
139
2022-02-16
op
continue;
142
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
143
2022-02-16
op
res, sizeof(res));
147
2022-02-16
op
return enq == 0;
150
2022-02-17
op
static int
151
2022-02-17
op
jump_req(const char *arg)
153
2022-02-17
op
char path[PATH_MAX];
155
2022-02-17
op
memset(path, 0, sizeof(path));
156
2022-02-17
op
strlcpy(path, arg, sizeof(path));
157
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
158
2022-02-17
op
return 0;
161
2022-02-16
op
static void
162
2022-02-16
op
print_error_message(const char *prfx, struct imsg *imsg)
164
2022-02-16
op
size_t datalen;
165
2022-02-16
op
char *msg;
167
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
168
2022-02-16
op
if ((msg = calloc(1, datalen)) == NULL)
169
2022-02-16
op
fatal("calloc %zu", datalen);
170
2022-02-16
op
memcpy(msg, imsg->data, datalen);
171
2022-02-16
op
if (datalen == 0 || msg[datalen-1] != '\0')
172
2022-02-16
op
fatalx("malformed error message");
174
2022-02-16
op
log_warnx("%s: %s", prfx, msg);
175
2022-02-16
op
free(msg);
178
2022-02-16
op
static int
179
2022-02-16
op
show_add(struct imsg *imsg, int *ret, char ***files)
181
2022-02-16
op
if (**files == NULL) {
182
2022-02-16
op
log_warnx("received more replies than file sent");
183
2022-02-16
op
*ret = 1;
184
2022-02-16
op
return 1;
187
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR)
188
2022-02-16
op
print_error_message(**files, imsg);
189
2022-02-16
op
else if (imsg->hdr.type == IMSG_CTL_ADD)
190
2022-02-16
op
log_debug("enqueued %s", **files);
192
2022-02-16
op
fatalx("got invalid message %d", imsg->hdr.type);
194
2022-02-16
op
(*files)++;
195
2022-02-16
op
return (**files) == NULL;
198
2022-02-16
op
static int
199
2022-02-17
op
show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
201
2022-02-17
op
struct player_status s;
202
2022-02-16
op
size_t datalen;
204
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
205
2022-02-16
op
print_error_message("show failed", imsg);
206
2022-02-16
op
*ret = 1;
207
2022-02-16
op
return 1;
210
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
211
2022-02-16
op
if (datalen == 0)
212
2022-02-16
op
return 1;
214
2022-02-17
op
if (datalen != sizeof(s))
215
2022-02-16
op
fatalx("%s: data size mismatch", __func__);
216
2022-02-17
op
memcpy(&s, imsg->data, sizeof(s));
217
2022-02-17
op
if (s.path[sizeof(s.path)-1] != '\0')
218
2022-02-16
op
fatalx("%s: data corrupted?", __func__);
220
2022-02-17
op
if (res->pretty)
221
2022-02-17
op
printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
222
2022-02-17
op
printf("%s\n", s.path);
223
2022-02-16
op
return 0;
226
2022-02-16
op
static int
227
2022-02-16
op
show_status(struct imsg *imsg, int *ret)
229
2022-02-16
op
struct player_status s;
230
2022-02-16
op
size_t datalen;
232
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
233
2022-02-16
op
print_error_message("show failed", imsg);
234
2022-02-16
op
*ret = 1;
235
2022-02-16
op
return 1;
238
2022-02-16
op
if (imsg->hdr.type != IMSG_CTL_STATUS)
239
2022-02-16
op
fatalx("%s: got wrong reply", __func__);
241
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
242
2022-02-16
op
if (datalen != sizeof(s))
243
2022-02-16
op
fatalx("%s: data size mismatch", __func__);
244
2022-02-16
op
memcpy(&s, imsg->data, sizeof(s));
245
2022-02-16
op
if (s.path[sizeof(s.path)-1] != '\0')
246
2022-02-16
op
fatalx("%s: data corrupted?", __func__);
248
2022-02-16
op
switch (s.status) {
249
2022-02-16
op
case STATE_STOPPED:
250
2022-02-16
op
printf("stopped ");
252
2022-02-16
op
case STATE_PLAYING:
253
2022-02-16
op
printf("playing ");
255
2022-02-16
op
case STATE_PAUSED:
256
2022-02-16
op
printf("paused ");
259
2022-02-16
op
printf("unknown ");
263
2022-02-16
op
printf("%s\n", s.path);
264
2022-03-03
op
printf("repeat one %s\nrepeat all %s\n",
265
2022-02-19
op
s.rp.repeat_one ? "on" : "off",
266
2022-02-19
op
s.rp.repeat_all ? "on" : "off");
267
2022-02-16
op
return 1;
270
2022-02-17
op
static int
271
2022-02-17
op
show_load(struct parse_result *res, struct imsg *imsg, int *ret)
274
2022-02-17
op
const char *file;
275
2022-02-17
op
char *line = NULL;
276
2022-02-17
op
char path[PATH_MAX];
277
2022-03-02
op
size_t linesize = 0, i = 0;
278
2022-03-02
op
ssize_t linelen, curr = -1;
280
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
281
2022-02-17
op
print_error_message("load failed", imsg);
282
2022-02-17
op
*ret = 1;
283
2022-02-17
op
return 1;
286
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_ADD)
287
2022-02-17
op
return 0;
289
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_COMMIT)
290
2022-02-17
op
return 1;
292
2022-02-17
op
if (imsg->hdr.type != IMSG_CTL_BEGIN)
293
2022-02-17
op
fatalx("got unexpected message %d", imsg->hdr.type);
295
2022-02-17
op
if (res->file == NULL)
296
2022-02-17
op
f = stdin;
297
2022-02-17
op
else if ((f = fopen(res->file, "r")) == NULL) {
298
2022-02-17
op
log_warn("can't open %s", res->file);
299
2022-02-17
op
*ret = 1;
300
2022-02-17
op
return 1;
303
2022-02-17
op
while ((linelen = getline(&line, &linesize, f)) != -1) {
304
2022-02-17
op
if (linelen == 0)
305
2022-02-17
op
continue;
306
2022-02-17
op
line[linelen-1] = '\0';
307
2022-02-17
op
file = line;
308
2022-03-02
op
if (file[0] == '>' && file[1] == ' ') {
309
2022-02-17
op
file += 2;
310
2022-03-02
op
curr = i;
312
2022-02-17
op
if (file[0] == ' ' && file[1] == ' ')
313
2022-02-17
op
file += 2;
315
2022-02-17
op
memset(&path, 0, sizeof(path));
316
2022-02-17
op
if (realpath(file, path) == NULL) {
317
2022-02-17
op
log_warn("realpath %s", file);
318
2022-02-17
op
continue;
322
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
323
2022-02-17
op
path, sizeof(path));
326
2022-02-17
op
free(line);
327
2022-02-17
op
if (ferror(f))
328
2022-02-17
op
fatal("getline");
329
2022-02-17
op
fclose(f);
331
2022-03-02
op
if (i == 0) {
332
2022-02-17
op
*ret = 1;
333
2022-02-17
op
return 1;
336
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
337
2022-03-02
op
&curr, sizeof(curr));
338
2022-02-17
op
imsg_flush(ibuf);
339
2022-02-17
op
return 0;
342
2022-02-16
op
static int
343
2022-02-21
op
show_monitor(struct imsg *imsg, int *ret)
345
2022-02-21
op
int type;
347
2022-02-21
op
if (imsg->hdr.type != IMSG_CTL_MONITOR) {
348
2022-02-21
op
log_warnx("wrong message type received: %d",
349
2022-02-21
op
imsg->hdr.type);
350
2022-02-21
op
*ret = 1;
351
2022-02-21
op
return 1;
354
2022-02-21
op
if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
355
2022-02-21
op
log_warnx("size mismatch");
356
2022-02-21
op
*ret = 1;
357
2022-02-21
op
return 1;
360
2022-02-21
op
memcpy(&type, imsg->data, sizeof(type));
361
2022-02-21
op
switch (type) {
362
2022-02-21
op
case IMSG_CTL_PLAY:
363
2022-02-21
op
puts("play");
365
2022-02-21
op
case IMSG_CTL_TOGGLE_PLAY:
366
2022-02-21
op
puts("toggle");
368
2022-02-21
op
case IMSG_CTL_PAUSE:
369
2022-02-21
op
puts("pause");
371
2022-02-21
op
case IMSG_CTL_STOP:
372
2022-02-21
op
puts("stop");
374
2022-02-21
op
case IMSG_CTL_RESTART:
375
2022-02-21
op
puts("restart");
377
2022-02-21
op
case IMSG_CTL_FLUSH:
378
2022-02-21
op
puts("flush");
380
2022-02-21
op
case IMSG_CTL_NEXT:
381
2022-02-21
op
puts("next");
383
2022-02-21
op
case IMSG_CTL_PREV:
384
2022-02-21
op
puts("prev");
386
2022-02-21
op
case IMSG_CTL_JUMP:
387
2022-02-21
op
puts("jump");
389
2022-02-21
op
case IMSG_CTL_REPEAT:
390
2022-02-21
op
puts("repeat");
392
2022-02-21
op
case IMSG_CTL_ADD:
393
2022-02-21
op
puts("add");
395
2022-02-21
op
case IMSG_CTL_COMMIT:
396
2022-02-21
op
puts("load");
399
2022-02-21
op
puts("unknown");
403
2022-02-22
op
fflush(stdout);
404
2022-02-21
op
return 0;
407
2022-02-21
op
static int
408
2022-02-16
op
ctlaction(struct parse_result *res)
410
2022-02-16
op
struct imsg imsg;
411
2022-02-16
op
ssize_t n;
412
2022-02-16
op
int ret = 0, done = 1;
413
2022-02-16
op
char **files;
415
2022-02-16
op
switch (res->action) {
416
2022-02-16
op
case PLAY:
417
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
418
2022-02-19
op
if (verbose) {
419
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
420
2022-02-19
op
NULL, 0);
421
2022-02-19
op
done = 0;
424
2022-02-16
op
case PAUSE:
425
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
427
2022-02-16
op
case TOGGLE:
428
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
429
2022-02-19
op
if (verbose) {
430
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
431
2022-02-19
op
NULL, 0);
432
2022-02-19
op
done = 0;
435
2022-02-16
op
case STOP:
436
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
438
2022-02-16
op
case RESTART:
439
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
440
2022-02-19
op
if (verbose) {
441
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
442
2022-02-19
op
NULL, 0);
443
2022-02-19
op
done = 0;
446
2022-02-16
op
case ADD:
447
2022-02-16
op
done = 0;
448
2022-02-16
op
files = res->files;
449
2022-02-16
op
ret = enqueue_tracks(res->files);
451
2022-02-16
op
case FLUSH:
452
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
454
2022-02-16
op
case SHOW:
455
2022-02-16
op
done = 0;
456
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
458
2022-02-16
op
case STATUS:
459
2022-02-16
op
done = 0;
460
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
462
2022-02-17
op
case NEXT:
463
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
464
2022-02-19
op
if (verbose) {
465
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
466
2022-02-19
op
NULL, 0);
467
2022-02-19
op
done = 0;
470
2022-02-17
op
case PREV:
471
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
472
2022-02-19
op
if (verbose) {
473
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
474
2022-02-19
op
NULL, 0);
475
2022-02-19
op
done = 0;
478
2022-02-17
op
case LOAD:
479
2022-02-17
op
done = 0;
480
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
482
2022-02-17
op
case JUMP:
483
2022-02-17
op
done = 0;
484
2022-02-17
op
ret = jump_req(res->file);
486
2022-02-19
op
case REPEAT:
487
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
488
2022-02-19
op
&res->rep, sizeof(res->rep));
490
2022-02-21
op
case MONITOR:
491
2022-02-21
op
done = 0;
492
2022-02-21
op
imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
493
2022-02-21
op
NULL, 0);
495
2022-02-16
op
case NONE:
496
2022-02-16
op
/* action not expected */
497
2022-02-16
op
fatalx("invalid action %u", res->action);
501
2022-02-16
op
if (ret != 0)
502
2022-02-16
op
goto end;
504
2022-02-16
op
imsg_flush(ibuf);
506
2022-02-16
op
while (!done) {
507
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
508
2022-02-16
op
fatalx("imsg_read error");
509
2022-02-16
op
if (n == 0)
510
2022-02-16
op
fatalx("pipe closed");
512
2022-02-16
op
while (!done) {
513
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
514
2022-02-16
op
fatalx("imsg_get error");
515
2022-02-16
op
if (n == 0)
518
2022-02-16
op
switch (res->action) {
519
2022-02-16
op
case ADD:
520
2022-02-16
op
done = show_add(&imsg, &ret, &files);
522
2022-02-16
op
case SHOW:
523
2022-02-17
op
done = show_complete(res, &imsg, &ret);
525
2022-02-17
op
case PLAY:
526
2022-02-17
op
case TOGGLE:
527
2022-02-17
op
case RESTART:
528
2022-02-16
op
case STATUS:
529
2022-02-17
op
case NEXT:
530
2022-02-17
op
case PREV:
531
2022-02-17
op
case JUMP:
532
2022-02-16
op
done = show_status(&imsg, &ret);
534
2022-02-17
op
case LOAD:
535
2022-02-17
op
done = show_load(res, &imsg, &ret);
537
2022-02-21
op
case MONITOR:
538
2022-02-21
op
done = show_monitor(&imsg, &ret);
541
2022-02-16
op
done = 1;
545
2022-02-16
op
imsg_free(&imsg);
550
2022-02-16
op
return ret;
554
2022-02-16
op
ctl_noarg(struct parse_result *res, int argc, char **argv)
558
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
559
2022-03-03
op
ctl_usage(res->ctl);
560
2022-03-03
op
argc -= optind;
561
2022-03-03
op
argv += optind;
563
2022-03-05
op
if (argc > 0)
564
2022-02-16
op
ctl_usage(res->ctl);
566
2022-02-16
op
return ctlaction(res);
570
2022-02-16
op
ctl_add(struct parse_result *res, int argc, char **argv)
574
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
575
2022-02-16
op
ctl_usage(res->ctl);
576
2022-03-03
op
argc -= optind;
577
2022-03-03
op
argv += optind;
579
2022-03-03
op
if (argc == 0)
580
2022-03-03
op
ctl_usage(res->ctl);
581
2022-03-03
op
res->files = argv;
583
2022-02-17
op
if (pledge("stdio rpath", NULL) == -1)
584
2022-02-17
op
fatal("pledge");
586
2022-02-16
op
return ctlaction(res);
590
2022-02-17
op
ctl_show(struct parse_result *res, int argc, char **argv)
594
2022-02-17
op
while ((ch = getopt(argc, argv, "p")) != -1) {
595
2022-02-17
op
switch (ch) {
596
2022-02-17
op
case 'p':
597
2022-02-17
op
res->pretty = 1;
600
2022-02-17
op
ctl_usage(res->ctl);
604
2022-02-17
op
return ctlaction(res);
608
2022-02-17
op
ctl_load(struct parse_result *res, int argc, char **argv)
612
2022-03-03
op
while ((ch = getopt(argc, argv, "")) != -1)
613
2022-03-03
op
ctl_usage(res->ctl);
614
2022-03-03
op
argc -= optind;
615
2022-03-03
op
argv += optind;
617
2022-03-03
op
if (argc == 0)
618
2022-02-17
op
res->file = NULL;
619
2022-03-03
op
else if (argc == 1)
620
2022-03-03
op
res->file = argv[0];
622
2022-02-17
op
ctl_usage(res->ctl);
624
2022-02-17
op
if (pledge("stdio rpath", NULL) == -1)
625
2022-02-17
op
fatal("pledge");
627
2022-02-17
op
return ctlaction(res);
631
2022-02-17
op
ctl_jump(struct parse_result *res, int argc, char **argv)
635
2022-02-17
op
while ((ch = getopt(argc, argv, "")) != -1)
636
2022-02-17
op
ctl_usage(res->ctl);
637
2022-02-17
op
argc -= optind;
638
2022-02-17
op
argv += optind;
640
2022-02-17
op
if (argc != 1)
641
2022-02-17
op
ctl_usage(res->ctl);
643
2022-02-17
op
res->file = argv[0];
644
2022-02-19
op
return ctlaction(res);
648
2022-02-19
op
ctl_repeat(struct parse_result *res, int argc, char **argv)
650
2022-02-19
op
int ch, b;
652
2022-02-19
op
while ((ch = getopt(argc, argv, "")) != -1)
653
2022-02-19
op
ctl_usage(res->ctl);
654
2022-02-19
op
argc -= optind;
655
2022-02-19
op
argv += optind;
657
2022-02-19
op
if (argc != 2)
658
2022-02-19
op
ctl_usage(res->ctl);
660
2022-02-19
op
if (!strcmp(argv[1], "on"))
662
2022-02-19
op
else if (!strcmp(argv[1], "off"))
665
2022-02-19
op
ctl_usage(res->ctl);
667
2022-02-19
op
res->rep.repeat_one = -1;
668
2022-02-19
op
res->rep.repeat_all = -1;
669
2022-02-19
op
if (!strcmp(argv[0], "one"))
670
2022-02-19
op
res->rep.repeat_one = b;
671
2022-02-19
op
else if (!strcmp(argv[0], "all"))
672
2022-02-19
op
res->rep.repeat_all = b;
674
2022-02-19
op
ctl_usage(res->ctl);
676
2022-02-17
op
return ctlaction(res);
679
2022-02-16
op
static int
680
2022-03-03
op
ctl_get_lock(const char *lockfile)
682
2022-03-03
op
int lockfd;
684
2022-03-03
op
if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
685
2022-03-03
op
log_debug("open failed: %s", strerror(errno));
686
2022-03-03
op
return -1;
689
2022-03-03
op
if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
690
2022-03-03
op
log_debug("flock failed: %s", strerror(errno));
691
2022-03-03
op
if (errno != EAGAIN)
692
2022-03-03
op
return -1;
693
2022-03-03
op
while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
694
2022-03-03
op
/* nop */;
695
2022-03-03
op
close(lockfd);
696
2022-03-03
op
return -2;
698
2022-03-03
op
log_debug("flock succeeded");
700
2022-03-03
op
return lockfd;
703
2022-03-03
op
static int
704
2022-03-03
op
ctl_connect(void)
706
2022-03-03
op
struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
707
2022-03-03
op
struct sockaddr_un sa;
708
2022-03-03
op
size_t size;
709
2022-03-03
op
int fd, lockfd = -1, locked = 0, spawned = 0;
710
2022-03-03
op
char *lockfile = NULL;
712
2022-03-03
op
memset(&sa, 0, sizeof(sa));
713
2022-03-03
op
sa.sun_family = AF_UNIX;
714
2022-03-03
op
size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
715
2022-03-03
op
if (size >= sizeof(sa.sun_path)) {
716
2022-03-03
op
errno = ENAMETOOLONG;
717
2022-02-16
op
return -1;
721
2022-03-03
op
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
722
2022-03-03
op
return -1;
724
2022-03-03
op
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
725
2022-03-03
op
log_debug("connection failed: %s", strerror(errno));
726
2022-03-03
op
if (errno != ECONNREFUSED && errno != ENOENT)
727
2022-03-03
op
goto failed;
728
2022-03-03
op
close(fd);
730
2022-03-03
op
if (!locked) {
731
2022-03-03
op
xasprintf(&lockfile, "%s.lock", csock);
732
2022-03-03
op
if ((lockfd = ctl_get_lock(lockfile)) < 0) {
733
2022-03-03
op
log_debug("didn't get the lock (%d)", lockfd);
735
2022-03-03
op
free(lockfile);
736
2022-03-03
op
lockfile = NULL;
738
2022-03-03
op
if (lockfd == -1)
739
2022-03-03
op
goto retry;
743
2022-03-03
op
* Always retry at least once, even if we got
744
2022-03-03
op
* the lock, because another client could have
745
2022-03-03
op
* taken the lock, started the server and released
746
2022-03-03
op
* the lock between our connect() and flock()
748
2022-03-03
op
locked = 1;
749
2022-03-03
op
goto retry;
752
2022-03-03
op
if (!spawned) {
753
2022-03-03
op
log_debug("spawning the daemon");
754
2022-03-03
op
spawn_daemon();
755
2022-03-03
op
spawned = 1;
758
2022-03-03
op
nanosleep(&ts, NULL);
759
2022-03-03
op
goto retry;
762
2022-03-03
op
if (locked && lockfd >= 0) {
763
2022-03-03
op
unlink(lockfile);
764
2022-03-03
op
free(lockfile);
765
2022-03-03
op
close(lockfd);
767
2022-03-03
op
return fd;
770
2022-03-03
op
if (locked) {
771
2022-03-03
op
free(lockfile);
772
2022-03-03
op
close(lockfd);
774
2022-03-03
op
close(fd);
775
2022-03-03
op
return -1;
778
2022-02-16
op
__dead void
779
2022-02-16
op
ctl(int argc, char **argv)
781
2022-03-03
op
int ctl_sock;
783
2022-02-16
op
log_init(1, LOG_DAEMON);
784
2022-02-16
op
log_setverbose(verbose);
786
2022-03-03
op
if ((ctl_sock = ctl_connect()) == -1)
787
2022-03-03
op
fatal("can't connect");
789
2022-02-16
op
if (ctl_sock == -1)
790
2022-02-16
op
fatalx("failed to connect to the daemon");
792
2022-02-16
op
ibuf = xmalloc(sizeof(*ibuf));
793
2022-02-16
op
imsg_init(ibuf, ctl_sock);
795
2022-02-16
op
optreset = 1;
796
2022-02-16
op
optind = 1;
798
2022-02-16
op
exit(parse(argc, argv));