-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamused.c
592 lines (498 loc) · 11.5 KB
/
amused.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* Copyright (c) 2022 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <imsg.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "amused.h"
#include "control.h"
#include "ev.h"
#include "log.h"
#include "player.h"
#include "playlist.h"
#include "xmalloc.h"
char *csock = NULL;
int debug;
int verbose;
struct imsgev *iev_player;
const char *argv0;
pid_t player_pid;
enum amused_process {
PROC_MAIN,
PROC_PLAYER,
};
static __dead void
main_shutdown(void)
{
pid_t pid;
int status;
/* close pipes. */
msgbuf_clear(&iev_player->imsgbuf.w);
close(iev_player->imsgbuf.fd);
free(iev_player);
log_debug("waiting for children to terminate");
do {
pid = wait(&status);
if (pid == -1) {
if (errno != EINTR && errno != ECHILD)
fatal("wait");
} else if (WIFSIGNALED(status))
log_warnx("player terminated; signal %d",
WTERMSIG(status));
} while (pid != -1 || (pid == -1 && errno == EINTR));
log_info("terminating");
exit(0);
}
static void
main_sig_handler(int sig, int event, void *arg)
{
/*
* Normal signal handler rules don't apply because ev.c
* decouples for us.
*/
switch (sig) {
case SIGTERM:
case SIGINT:
main_shutdown();
break;
default:
fatalx("unexpected signal %d", sig);
}
}
static void
main_dispatch_player(int sig, int event, void *d)
{
char *errstr;
struct imsgev *iev = d;
struct imsgbuf *imsgbuf = &iev->imsgbuf;
struct imsg imsg;
struct ibuf ibuf;
size_t datalen;
ssize_t n;
int shut = 0;
if (event & EV_READ) {
if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
fatal("imsg_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
fatal("msgbuf_write");
if (n == 0) /* Connection closed */
shut = 1;
}
for (;;) {
if ((n = imsg_get(imsgbuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg_get_type(&imsg)) {
case IMSG_POS:
if (imsg_get_data(&imsg, ¤t_position,
sizeof(current_position)) == -1)
fatalx("IMSG_POS: got wrong size");
if (current_position < 0)
current_position = -1;
control_notify(IMSG_CTL_SEEK);
break;
case IMSG_LEN:
if (imsg_get_data(&imsg, ¤t_duration,
sizeof(current_duration)) == -1)
fatalx("IMSG_LEN: got wrong size");
if (current_duration < 0)
current_duration = -1;
break;
case IMSG_ERR:
if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
(datalen = ibuf_size(&ibuf)) == 0)
errstr = "unknown error";
else {
errstr = ibuf_data(&ibuf);
errstr[datalen-1] = '\0';
}
log_warnx("%s; skipping %s", errstr, current_song);
playlist_dropcurrent();
main_playlist_advance();
if (play_state == STATE_PLAYING)
control_notify(IMSG_CTL_NEXT);
else
control_notify(IMSG_CTL_STOP);
break;
case IMSG_EOF:
if (repeat_one && main_play_song(current_song))
break;
else if (repeat_one || consume)
playlist_dropcurrent();
main_playlist_advance();
if (play_state == STATE_PLAYING)
control_notify(IMSG_CTL_NEXT);
else
control_notify(IMSG_CTL_STOP);
break;
default:
log_debug("%s: error handling imsg %d", __func__,
imsg_get_type(&imsg));
break;
}
imsg_free(&imsg);
}
if (shut)
ev_break();
else
imsg_event_add(iev);
}
static pid_t
start_child(enum amused_process proc, int fd)
{
const char *argv[7];
int argc = 0;
pid_t pid;
if (fd == -1 && debug)
goto exec;
switch (pid = fork()) {
case -1:
fatal("cannot fork");
case 0:
break;
default:
close(fd);
return pid;
}
if (fd != 3) {
if (fd != -1 && dup2(fd, 3) == -1)
fatal("cannot setup imsg fd");
} else if (fcntl(F_SETFD, 0) == -1)
fatal("cannot setup imsg fd");
exec:
argv[argc++] = argv0;
switch (proc) {
case PROC_MAIN:
argv[argc++] = "-s";
argv[argc++] = csock;
argv[argc++] = "-Tm";
break;
case PROC_PLAYER:
argv[argc++] = "-Tp";
break;
}
if (debug)
argv[argc++] = "-d";
if (verbose)
argv[argc++] = "-v";
argv[argc++] = NULL;
/* obnoxious casts */
execvp(argv0, (char *const *)argv);
fatal("execvp %s", argv0);
}
/* daemon main routine */
static __dead void
amused_main(void)
{
int pipe_main2player[2];
int control_fd, flags;
log_init(debug, LOG_DAEMON);
log_setverbose(verbose);
log_procinit("main");
if (!debug && daemon(1, 0) == -1)
fatal("daemon");
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_main2player) == -1)
fatal("socketpair");
if ((flags = fcntl(pipe_main2player[0], F_GETFL)) == -1 ||
fcntl(pipe_main2player[0], F_SETFL, flags | O_NONBLOCK) == -1 ||
(flags = fcntl(pipe_main2player[1], F_GETFL)) == -1 ||
fcntl(pipe_main2player[1], F_SETFL, flags | O_NONBLOCK) == -1)
fatal("fcntl(O_NONBLOCK)");
if (fcntl(pipe_main2player[0], F_SETFD, FD_CLOEXEC) == -1 ||
fcntl(pipe_main2player[1], F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl(CLOEXEC)");
player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
if (ev_init() == -1)
fatal("ev_init");
signal(SIGHUP, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
ev_signal(SIGINT, main_sig_handler, NULL);
ev_signal(SIGTERM, main_sig_handler, NULL);
iev_player = xmalloc(sizeof(*iev_player));
imsg_init(&iev_player->imsgbuf, pipe_main2player[0]);
iev_player->handler = main_dispatch_player;
iev_player->events = EV_READ;
ev_add(iev_player->imsgbuf.fd, iev_player->events,
iev_player->handler, iev_player);
if ((control_fd = control_init(csock)) == -1)
fatal("control socket setup failed %s", csock);
control_listen(control_fd);
if (pledge("stdio rpath unix sendfd", NULL) == -1)
fatal("pledge");
log_info("startup");
ev_loop();
main_shutdown();
}
int
main(int argc, char **argv)
{
int ch, proc = -1;
log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
log_setverbose(1);
argv0 = argv[0];
if (argv0 == NULL)
argv0 = "amused";
while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 's':
free(csock);
csock = xstrdup(optarg);
break;
case 'T':
switch (*optarg) {
case 'm':
proc = PROC_MAIN;
break;
case 'p':
proc = PROC_PLAYER;
break;
default:
usage();
}
break;
case 'v':
verbose++;
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (proc == PROC_MAIN)
amused_main();
if (proc == PROC_PLAYER)
exit(player(debug, verbose));
if (csock == NULL) {
const char *tmpdir;
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = "/tmp";
xasprintf(&csock, "%s/amused-%d", tmpdir, getuid());
}
if (argc > 0)
debug = 0;
ctl(argc, argv);
}
void
spawn_daemon(void)
{
start_child(PROC_MAIN, -1);
}
void
imsg_event_add(struct imsgev *iev)
{
iev->events = EV_READ;
if (iev->imsgbuf.w.queued)
iev->events |= EV_WRITE;
ev_add(iev->imsgbuf.fd, iev->events, iev->handler, iev);
}
int
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
pid_t pid, int fd, const void *data, uint16_t datalen)
{
int ret;
if ((ret = imsg_compose(&iev->imsgbuf, type, peerid, pid, fd, data,
datalen)) != -1)
imsg_event_add(iev);
return ret;
}
int
main_send_player(uint16_t type, int fd, const void *data, size_t len)
{
return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
}
int
main_play_song(const char *path)
{
struct stat sb;
int fd;
if ((fd = open(path, O_RDONLY)) == -1) {
log_warn("open %s", path);
return 0;
}
if (fstat(fd, &sb) == -1) {
log_warn("failed to stat %s", path);
close(fd);
return 0;
}
if (!S_ISREG(sb.st_mode)) {
log_info("skipping non-regular file: %s", path);
close(fd);
return 0;
}
play_state = STATE_PLAYING;
main_send_player(IMSG_PLAY, fd, NULL, 0);
return 1;
}
void
main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
{
char arg[PATH_MAX];
const char *song;
if (imsg_get_data(imsg, arg, sizeof(arg)) == -1) {
main_senderr(iev, "wrong size");
return;
}
if (arg[sizeof(arg)-1] != '\0') {
main_senderr(iev, "data corrupted");
return;
}
song = playlist_jump(arg);
if (song == NULL) {
main_senderr(iev, "not found");
return;
}
control_notify(IMSG_CTL_JUMP);
main_send_player(IMSG_STOP, -1, NULL, 0);
if (!main_play_song(song)) {
main_senderr(iev, "can't play");
playlist_dropcurrent();
main_playlist_advance();
return;
}
main_send_status(iev);
}
void
main_playlist_resume(void)
{
const char *song;
if ((song = current_song) == NULL)
song = playlist_advance();
for (; song != NULL; song = playlist_advance()) {
if (main_play_song(song))
return;
playlist_dropcurrent();
}
}
void
main_playlist_advance(void)
{
const char *song;
for (;;) {
song = playlist_advance();
if (song == NULL)
return;
if (main_play_song(song))
break;
playlist_dropcurrent();
}
}
void
main_playlist_previous(void)
{
const char *song;
for (;;) {
song = playlist_previous();
if (song == NULL)
return;
if (main_play_song(song))
break;
playlist_dropcurrent();
}
}
void
main_senderr(struct imsgev *iev, const char *msg)
{
imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
msg, strlen(msg)+1);
}
void
main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
struct imsg *imsg)
{
char path[PATH_MAX];
const char *err = NULL;
if (imsg_get_data(imsg, path, sizeof(path)) == -1) {
err = "data size mismatch";
goto err;
}
if (path[sizeof(path)-1] != '\0') {
err = "malformed data";
goto err;
}
if (tx)
playlist_push(px, path);
else
playlist_enqueue(path);
imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
return;
err:
main_senderr(iev, err);
}
void
main_send_playlist(struct imsgev *iev)
{
struct player_status s;
size_t i;
for (i = 0; i < playlist.len; ++i) {
memset(&s, 0, sizeof(s));
strlcpy(s.path, playlist.songs[i], sizeof(s.path));
s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
sizeof(s));
}
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
}
void
main_send_status(struct imsgev *iev)
{
struct player_status s;
memset(&s, 0, sizeof(s));
if (current_song != NULL)
strlcpy(s.path, current_song, sizeof(s.path));
s.status = play_state;
s.position = current_position;
s.duration = current_duration;
s.mode.repeat_all = repeat_all;
s.mode.repeat_one = repeat_one;
s.mode.consume = consume;
imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
}
void
main_seek(struct player_seek *s)
{
switch (play_state) {
case STATE_STOPPED:
main_playlist_resume();
break;
case STATE_PLAYING:
break;
case STATE_PAUSED:
play_state = STATE_PLAYING;
break;
}
main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));
}