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/queue.h>
019
2022-02-16
op
#include <sys/socket.h>
020
2022-02-24
op
#include <sys/stat.h>
021
2022-02-16
op
#include <sys/uio.h>
022
2022-02-16
op
#include <sys/wait.h>
024
2022-02-16
op
#include <event.h>
025
2022-02-16
op
#include <errno.h>
026
2022-02-16
op
#include <fcntl.h>
027
2022-02-16
op
#include <limits.h>
028
2022-02-16
op
#include <sndio.h>
029
2022-02-16
op
#include <signal.h>
030
2022-02-16
op
#include <stdio.h>
031
2022-02-16
op
#include <stdlib.h>
032
2022-02-16
op
#include <stdint.h>
033
2022-02-16
op
#include <string.h>
034
2022-02-16
op
#include <syslog.h>
035
2022-02-16
op
#include <unistd.h>
036
2022-02-16
op
#include <imsg.h>
038
2022-02-16
op
#include "amused.h"
039
2022-02-16
op
#include "control.h"
040
2022-02-16
op
#include "log.h"
041
2022-02-16
op
#include "playlist.h"
042
2022-02-16
op
#include "xmalloc.h"
044
2022-02-16
op
struct sio_hdl *hdl;
045
2022-02-16
op
char *csock = NULL;
046
2022-02-16
op
int debug;
047
2022-02-16
op
int verbose;
048
2022-02-16
op
struct imsgev *iev_player;
050
2022-02-16
op
const char *argv0;
051
2022-02-16
op
pid_t player_pid;
052
2022-02-16
op
struct event ev_sigint;
053
2022-02-16
op
struct event ev_sigterm;
055
2022-02-16
op
enum amused_process {
056
2022-02-16
op
PROC_MAIN,
057
2022-02-16
op
PROC_PLAYER,
060
2022-02-16
op
__dead void
061
2022-02-16
op
main_shutdown(void)
063
2022-02-16
op
pid_t pid;
064
2022-02-16
op
int status;
066
2022-02-16
op
/* close pipes. */
067
2022-02-16
op
msgbuf_clear(&iev_player->ibuf.w);
068
2022-02-16
op
close(iev_player->ibuf.fd);
069
2022-02-16
op
free(iev_player);
071
2022-02-16
op
log_debug("waiting for children to terminate");
073
2022-02-16
op
pid = wait(&status);
074
2022-02-16
op
if (pid == -1) {
075
2022-02-16
op
if (errno != EINTR && errno != ECHILD)
076
2022-02-16
op
fatal("wait");
077
2022-02-16
op
} else if (WIFSIGNALED(status))
078
2022-02-16
op
log_warnx("player terminated; signal %d",
079
2022-02-16
op
WTERMSIG(status));
080
2022-02-16
op
} while (pid != -1 || (pid == -1 && errno == EINTR));
082
2022-02-16
op
log_info("terminating");
086
2022-02-16
op
static void
087
2022-02-16
op
main_sig_handler(int sig, short event, void *arg)
090
2022-02-16
op
* Normal signal handler rules don't apply because libevent
091
2022-02-16
op
* decouples for us.
094
2022-02-16
op
switch (sig) {
095
2022-02-16
op
case SIGTERM:
096
2022-02-16
op
case SIGINT:
097
2022-02-16
op
main_shutdown();
100
2022-02-16
op
fatalx("unexpected signal %d", sig);
104
2022-02-16
op
static void
105
2022-02-16
op
main_dispatch_player(int sig, short event, void *d)
107
2022-02-16
op
struct imsgev *iev = d;
108
2022-02-16
op
struct imsgbuf *ibuf = &iev->ibuf;
109
2022-02-16
op
struct imsg imsg;
110
2022-02-16
op
ssize_t n;
111
2022-02-16
op
int shut = 0;
113
2022-02-16
op
if (event & EV_READ) {
114
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
115
2022-02-16
op
fatal("imsg_read error");
116
2022-02-16
op
if (n == 0) /* Connection closed */
117
2022-02-16
op
shut = 1;
119
2022-02-16
op
if (event & EV_WRITE) {
120
2022-02-16
op
if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
121
2022-02-16
op
fatal("msgbuf_write");
122
2022-02-16
op
if (n == 0) /* Connection closed */
123
2022-02-16
op
shut = 1;
126
2022-02-16
op
for (;;) {
127
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
128
2022-02-16
op
fatal("imsg_get");
129
2022-02-16
op
if (n == 0) /* No more messages. */
132
2022-02-16
op
switch (imsg.hdr.type) {
133
2022-02-16
op
case IMSG_ERR:
134
2022-02-16
op
playlist_dropcurrent();
135
2022-02-22
op
/* fallthrough */
136
2022-02-16
op
case IMSG_EOF:
137
2022-02-22
op
if (repeat_one && current_song != NULL) {
138
2022-02-19
op
if (main_play_song(current_song))
140
2022-02-22
op
playlist_dropcurrent();
142
2022-02-16
op
main_playlist_advance();
143
2022-02-21
op
if (play_state == STATE_PLAYING)
144
2022-02-21
op
control_notify(NULL, IMSG_CTL_NEXT);
146
2022-02-21
op
control_notify(NULL, IMSG_CTL_STOP);
149
2022-02-16
op
log_debug("%s: error handling imsg %d", __func__,
150
2022-02-16
op
imsg.hdr.type);
153
2022-02-16
op
imsg_free(&imsg);
156
2022-02-16
op
if (!shut)
157
2022-02-16
op
imsg_event_add(iev);
159
2022-02-16
op
/* This pipe is dead. Remove its event handler. */
160
2022-02-16
op
event_del(&iev->ev);
161
2022-02-16
op
event_loopexit(NULL);
165
2022-02-16
op
static pid_t
166
2022-02-16
op
start_child(enum amused_process proc, int fd)
168
2022-03-03
op
const char *argv[7];
169
2022-02-16
op
int argc = 0;
170
2022-02-16
op
pid_t pid;
172
2022-03-03
op
if (fd == -1 && debug)
173
2022-03-03
op
goto exec;
175
2022-02-16
op
switch (pid = fork()) {
177
2022-02-16
op
fatal("cannot fork");
181
2022-02-16
op
close(fd);
182
2022-02-16
op
return pid;
185
2022-02-16
op
if (fd != 3) {
186
2022-02-16
op
if (fd != -1 && dup2(fd, 3) == -1)
187
2022-02-16
op
fatal("cannot setup imsg fd");
188
2022-02-16
op
} else if (fcntl(F_SETFD, 0) == -1)
189
2022-02-16
op
fatal("cannot setup imsg fd");
192
2022-02-16
op
argv[argc++] = argv0;
194
2022-02-16
op
switch (proc) {
195
2022-02-16
op
case PROC_MAIN:
196
2022-02-17
op
argv[argc++] = "-s";
197
2022-02-17
op
argv[argc++] = csock;
198
2022-03-03
op
argv[argc++] = "-Tm";
200
2022-02-16
op
case PROC_PLAYER:
201
2022-02-16
op
argv[argc++] = "-Tp";
205
2022-02-16
op
if (debug)
206
2022-02-16
op
argv[argc++] = "-d";
207
2022-02-16
op
if (verbose)
208
2022-02-16
op
argv[argc++] = "-v";
209
2022-02-16
op
argv[argc++] = NULL;
211
2022-02-16
op
/* obnoxious casts */
212
2022-02-16
op
execvp(argv0, (char *const *)argv);
213
2022-02-16
op
fatal("execvp %s", argv0);
216
2022-02-16
op
/* daemon main routine */
217
2022-02-16
op
static __dead int
218
2022-02-16
op
amused_main(void)
220
2022-02-16
op
int pipe_main2player[2];
221
2022-02-16
op
int control_fd;
223
2022-02-16
op
log_init(debug, LOG_DAEMON);
224
2022-02-16
op
log_setverbose(verbose);
225
2022-02-16
op
log_procinit("main");
227
2022-02-16
op
if (!debug)
228
2022-02-16
op
daemon(1, 0);
230
2022-02-16
op
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
231
2022-02-16
op
PF_UNSPEC, pipe_main2player) == -1)
232
2022-02-16
op
fatal("socketpair");
234
2022-02-16
op
player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
236
2022-02-16
op
event_init();
238
2022-02-16
op
signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
239
2022-02-16
op
signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
241
2022-02-16
op
signal_add(&ev_sigint, NULL);
242
2022-02-16
op
signal_add(&ev_sigterm, NULL);
244
2022-02-16
op
signal(SIGHUP, SIG_IGN);
245
2022-02-16
op
signal(SIGCHLD, SIG_IGN);
246
2022-02-16
op
signal(SIGPIPE, SIG_IGN);
248
2022-02-16
op
iev_player = xmalloc(sizeof(*iev_player));
249
2022-02-16
op
imsg_init(&iev_player->ibuf, pipe_main2player[0]);
250
2022-02-16
op
iev_player->handler = main_dispatch_player;
251
2022-02-16
op
iev_player->events = EV_READ;
252
2022-02-16
op
event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
253
2022-02-16
op
iev_player->handler, iev_player);
254
2022-02-16
op
event_add(&iev_player->ev, NULL);
256
2022-02-16
op
if ((control_fd = control_init(csock)) == -1)
257
2022-02-16
op
fatal("control socket setup failed %s", csock);
258
2022-02-16
op
control_listen(control_fd);
260
2022-02-16
op
if (pledge("stdio rpath unix sendfd", NULL) == -1)
261
2022-02-16
op
fatal("pledge");
263
2022-02-16
op
log_info("startup");
264
2022-02-16
op
event_dispatch();
265
2022-02-16
op
main_shutdown();
269
2022-02-16
op
main(int argc, char **argv)
271
2022-03-03
op
int ch, proc = -1;
273
2022-02-16
op
log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
274
2022-02-16
op
log_setverbose(1);
276
2022-02-16
op
argv0 = argv[0];
277
2022-02-16
op
if (argv0 == NULL)
278
2022-02-16
op
argv0 = "amused";
280
2022-02-19
op
while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
281
2022-02-16
op
switch (ch) {
282
2022-02-16
op
case 'd':
283
2022-02-16
op
debug = 1;
285
2022-02-16
op
case 's':
286
2022-02-16
op
free(csock);
287
2022-02-16
op
csock = xstrdup(optarg);
289
2022-02-16
op
case 'T':
290
2022-02-16
op
switch (*optarg) {
291
2022-03-03
op
case 'm':
292
2022-03-03
op
proc = PROC_MAIN;
294
2022-02-16
op
case 'p':
295
2022-02-16
op
proc = PROC_PLAYER;
301
2022-02-16
op
case 'v':
302
2022-02-16
op
verbose++;
308
2022-02-16
op
argv += optind;
309
2022-02-16
op
argc -= optind;
311
2022-03-03
op
if (proc == PROC_MAIN)
312
2022-03-03
op
amused_main();
313
2022-02-16
op
if (proc == PROC_PLAYER)
314
2022-02-16
op
exit(player(debug, verbose));
316
2022-02-16
op
if (csock == NULL)
317
2022-02-16
op
xasprintf(&csock, "/tmp/amused-%d", getuid());
319
2022-03-03
op
if (argc > 0)
320
2022-03-03
op
debug = 0;
322
2022-03-03
op
ctl(argc, argv);
326
2022-02-16
op
spawn_daemon(void)
328
2022-02-16
op
start_child(PROC_MAIN, -1);
332
2022-02-16
op
imsg_event_add(struct imsgev *iev)
334
2022-02-16
op
iev->events = EV_READ;
335
2022-02-16
op
if (iev->ibuf.w.queued)
336
2022-02-16
op
iev->events |= EV_WRITE;
338
2022-02-16
op
event_del(&iev->ev);
339
2022-02-16
op
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
340
2022-02-16
op
event_add(&iev->ev, NULL);
344
2022-02-16
op
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
345
2022-02-16
op
pid_t pid, int fd, const void *data, uint16_t datalen)
349
2022-02-16
op
if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
350
2022-02-16
op
datalen)) != -1)
351
2022-02-16
op
imsg_event_add(iev);
353
2022-02-16
op
return ret;
357
2022-02-16
op
main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
359
2022-02-16
op
return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
363
2022-02-16
op
main_play_song(const char *song)
365
2022-02-24
op
struct stat sb;
366
2022-02-16
op
char path[PATH_MAX] = { 0 };
369
2022-02-16
op
strlcpy(path, song, sizeof(path));
370
2022-02-16
op
if ((fd = open(path, O_RDONLY)) == -1) {
371
2022-02-16
op
log_warn("open %s", path);
372
2022-02-24
op
return 0;
375
2022-02-24
op
if (fstat(fd, &sb) == -1) {
376
2022-02-24
op
log_warn("failed to stat %s", path);
377
2022-02-24
op
close(fd);
378
2022-02-16
op
return 0;
381
2022-02-24
op
if (S_ISDIR(sb.st_mode)) {
382
2022-02-24
op
log_info("skipping a directory: %s", path);
383
2022-02-24
op
close(fd);
384
2022-02-24
op
return 0;
387
2022-02-16
op
play_state = STATE_PLAYING;
388
2022-02-16
op
imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
389
2022-02-16
op
path, sizeof(path));
390
2022-02-16
op
return 1;
394
2022-02-17
op
main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
396
2022-02-17
op
size_t datalen;
397
2022-02-17
op
char arg[PATH_MAX];
398
2022-02-17
op
const char *song;
400
2022-02-17
op
datalen = IMSG_DATA_SIZE(*imsg);
401
2022-02-17
op
if (datalen != sizeof(arg)) {
402
2022-02-17
op
main_senderr(iev, "wrong size");
406
2022-02-17
op
memcpy(arg, imsg->data, sizeof(arg));
407
2022-02-17
op
if (arg[sizeof(arg)-1] != '\0') {
408
2022-02-17
op
main_senderr(iev, "data corrupted");
412
2022-02-17
op
song = playlist_jump(arg);
413
2022-02-17
op
if (song == NULL) {
414
2022-02-17
op
main_senderr(iev, "not found");
418
2022-02-17
op
main_send_player(IMSG_STOP, -1, NULL, 0);
419
2022-02-17
op
if (!main_play_song(song)) {
420
2022-02-17
op
main_senderr(iev, "can't play");
421
2022-02-17
op
playlist_dropcurrent();
422
2022-02-17
op
main_playlist_advance();
426
2022-02-17
op
main_send_status(iev);
430
2022-02-17
op
main_playlist_resume(void)
432
2022-02-17
op
const char *song;
434
2022-02-19
op
if ((song = current_song) == NULL)
435
2022-02-17
op
song = playlist_advance();
437
2022-02-17
op
for (; song != NULL; song = playlist_advance()) {
438
2022-02-17
op
if (main_play_song(song))
441
2022-02-17
op
playlist_dropcurrent();
446
2022-02-16
op
main_playlist_advance(void)
448
2022-02-16
op
const char *song;
450
2022-02-16
op
for (;;) {
451
2022-02-16
op
song = playlist_advance();
452
2022-02-16
op
if (song == NULL)
455
2022-02-16
op
if (main_play_song(song))
458
2022-02-16
op
playlist_dropcurrent();
463
2022-02-17
op
main_playlist_previous(void)
465
2022-02-17
op
const char *song;
467
2022-02-17
op
for (;;) {
468
2022-02-17
op
song = playlist_previous();
469
2022-02-17
op
if (song == NULL)
472
2022-02-17
op
if (main_play_song(song))
475
2022-02-17
op
playlist_dropcurrent();
480
2022-02-16
op
main_restart_track(void)
482
2022-02-16
op
const char *song;
484
2022-02-19
op
song = current_song;
485
2022-02-16
op
if (song == NULL)
488
2022-02-16
op
if (main_play_song(song))
491
2022-02-16
op
playlist_dropcurrent();
492
2022-02-16
op
main_playlist_advance();
496
2022-02-17
op
main_senderr(struct imsgev *iev, const char *msg)
498
2022-02-17
op
imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
499
2022-02-17
op
msg, strlen(msg)+1);
503
2022-02-17
op
main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
504
2022-02-17
op
struct imsg *imsg)
506
2022-02-16
op
size_t datalen;
507
2022-02-16
op
char path[PATH_MAX] = { 0 };
508
2022-02-16
op
const char *err = NULL;
510
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
511
2022-02-16
op
if (datalen != sizeof(path)) {
512
2022-02-16
op
err = "data size mismatch";
513
2022-02-16
op
goto err;
516
2022-02-16
op
memcpy(path, imsg->data, sizeof(path));
517
2022-02-16
op
if (path[datalen-1] != '\0') {
518
2022-02-16
op
err = "malformed data";
519
2022-02-16
op
goto err;
523
2022-02-17
op
playlist_push(px, path);
525
2022-02-17
op
playlist_enqueue(path);
526
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
529
2022-03-02
op
main_senderr(iev, err);
533
2022-02-16
op
main_send_playlist(struct imsgev *iev)
535
2022-02-17
op
struct player_status s;
536
2022-02-16
op
size_t i;
538
2022-02-16
op
for (i = 0; i < playlist.len; ++i) {
539
2022-02-17
op
memset(&s, 0, sizeof(s));
540
2022-02-17
op
strlcpy(s.path, playlist.songs[i], sizeof(s.path));
541
2022-02-17
op
s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
542
2022-02-17
op
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
543
2022-02-17
op
sizeof(s));
546
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
550
2022-02-16
op
main_send_status(struct imsgev *iev)
552
2022-02-16
op
struct player_status s;
554
2022-02-16
op
memset(&s, 0, sizeof(s));
556
2022-02-19
op
if (current_song != NULL)
557
2022-02-19
op
strlcpy(s.path, current_song, sizeof(s.path));
558
2022-02-16
op
s.status = play_state;
559
2022-02-19
op
s.rp.repeat_all = repeat_all;
560
2022-02-19
op
s.rp.repeat_one = repeat_one;
562
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));