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-16
op
#include <sys/uio.h>
021
2022-02-16
op
#include <sys/wait.h>
023
2022-02-16
op
#include <event.h>
024
2022-02-16
op
#include <errno.h>
025
2022-02-16
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 <signal.h>
029
2022-02-16
op
#include <stdio.h>
030
2022-02-16
op
#include <stdlib.h>
031
2022-02-16
op
#include <stdint.h>
032
2022-02-16
op
#include <string.h>
033
2022-02-16
op
#include <syslog.h>
034
2022-02-16
op
#include <unistd.h>
035
2022-02-16
op
#include <imsg.h>
037
2022-02-16
op
#include "amused.h"
038
2022-02-16
op
#include "control.h"
039
2022-02-16
op
#include "log.h"
040
2022-02-16
op
#include "playlist.h"
041
2022-02-16
op
#include "xmalloc.h"
043
2022-02-16
op
struct sio_hdl *hdl;
044
2022-02-16
op
char *csock = NULL;
045
2022-02-16
op
int debug;
046
2022-02-16
op
int verbose;
047
2022-02-16
op
struct imsgev *iev_player;
049
2022-02-16
op
const char *argv0;
050
2022-02-16
op
pid_t player_pid;
051
2022-02-16
op
struct event ev_sigint;
052
2022-02-16
op
struct event ev_sigterm;
053
2022-02-16
op
struct event ev_siginfo;
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_status(void)
089
2022-02-16
op
const char *cur;
091
2022-02-16
op
switch (play_state) {
092
2022-02-16
op
case STATE_STOPPED:
093
2022-02-16
op
log_info("status: stopped");
095
2022-02-16
op
case STATE_PLAYING:
096
2022-02-16
op
log_info("status: playing");
098
2022-02-16
op
case STATE_PAUSED:
099
2022-02-16
op
log_info("status: paused");
102
2022-02-16
op
log_info("status: unknown");
106
2022-02-16
op
if ((cur = playlist_current()) != NULL)
107
2022-02-16
op
log_info("playing %s", cur);
109
2022-02-16
op
log_info("not playing anything");
112
2022-02-16
op
static void
113
2022-02-16
op
main_sig_handler(int sig, short event, void *arg)
116
2022-02-16
op
* Normal signal handler rules don't apply because libevent
117
2022-02-16
op
* decouples for us.
120
2022-02-16
op
switch (sig) {
121
2022-02-16
op
case SIGTERM:
122
2022-02-16
op
case SIGINT:
123
2022-02-16
op
main_shutdown();
125
2022-02-16
op
case SIGINFO:
126
2022-02-16
op
main_status();
129
2022-02-16
op
fatalx("unexpected signal %d", sig);
133
2022-02-16
op
static void
134
2022-02-16
op
main_dispatch_player(int sig, short event, void *d)
136
2022-02-16
op
struct imsgev *iev = d;
137
2022-02-16
op
struct imsgbuf *ibuf = &iev->ibuf;
138
2022-02-16
op
struct imsg imsg;
139
2022-02-16
op
ssize_t n;
140
2022-02-16
op
int shut = 0;
142
2022-02-16
op
if (event & EV_READ) {
143
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
144
2022-02-16
op
fatal("imsg_read error");
145
2022-02-16
op
if (n == 0) /* Connection closed */
146
2022-02-16
op
shut = 1;
148
2022-02-16
op
if (event & EV_WRITE) {
149
2022-02-16
op
if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
150
2022-02-16
op
fatal("msgbuf_write");
151
2022-02-16
op
if (n == 0) /* Connection closed */
152
2022-02-16
op
shut = 1;
155
2022-02-16
op
for (;;) {
156
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
157
2022-02-16
op
fatal("imsg_get");
158
2022-02-16
op
if (n == 0) /* No more messages. */
161
2022-02-16
op
switch (imsg.hdr.type) {
162
2022-02-16
op
case IMSG_ERR:
163
2022-02-16
op
playlist_dropcurrent();
164
2022-02-16
op
/* fallthrough */
165
2022-02-16
op
case IMSG_EOF:
166
2022-02-16
op
main_playlist_advance();
170
2022-02-16
op
log_debug("%s: error handling imsg %d", __func__,
171
2022-02-16
op
imsg.hdr.type);
174
2022-02-16
op
imsg_free(&imsg);
177
2022-02-16
op
if (!shut)
178
2022-02-16
op
imsg_event_add(iev);
180
2022-02-16
op
/* This pipe is dead. Remove its event handler. */
181
2022-02-16
op
event_del(&iev->ev);
182
2022-02-16
op
event_loopexit(NULL);
186
2022-02-16
op
static pid_t
187
2022-02-16
op
start_child(enum amused_process proc, int fd)
189
2022-02-17
op
const char *argv[6];
190
2022-02-16
op
int argc = 0;
191
2022-02-16
op
pid_t pid;
193
2022-02-16
op
switch (pid = fork()) {
195
2022-02-16
op
fatal("cannot fork");
199
2022-02-16
op
close(fd);
200
2022-02-16
op
return pid;
203
2022-02-16
op
if (fd != 3) {
204
2022-02-16
op
if (fd != -1 && dup2(fd, 3) == -1)
205
2022-02-16
op
fatal("cannot setup imsg fd");
206
2022-02-16
op
} else if (fcntl(F_SETFD, 0) == -1)
207
2022-02-16
op
fatal("cannot setup imsg fd");
209
2022-02-16
op
argv[argc++] = argv0;
211
2022-02-16
op
switch (proc) {
212
2022-02-16
op
case PROC_MAIN:
213
2022-02-17
op
argv[argc++] = "-s";
214
2022-02-17
op
argv[argc++] = csock;
216
2022-02-16
op
case PROC_PLAYER:
217
2022-02-16
op
argv[argc++] = "-Tp";
221
2022-02-16
op
if (debug)
222
2022-02-16
op
argv[argc++] = "-d";
223
2022-02-16
op
if (verbose)
224
2022-02-16
op
argv[argc++] = "-v";
225
2022-02-16
op
argv[argc++] = NULL;
227
2022-02-16
op
/* obnoxious casts */
228
2022-02-16
op
execvp(argv0, (char *const *)argv);
229
2022-02-16
op
fatal("execvp %s", argv0);
232
2022-02-16
op
/* daemon main routine */
233
2022-02-16
op
static __dead int
234
2022-02-16
op
amused_main(void)
236
2022-02-16
op
int pipe_main2player[2];
237
2022-02-16
op
int control_fd;
239
2022-02-16
op
log_init(debug, LOG_DAEMON);
240
2022-02-16
op
log_setverbose(verbose);
241
2022-02-16
op
log_procinit("main");
243
2022-02-16
op
if (!debug)
244
2022-02-16
op
daemon(1, 0);
246
2022-02-16
op
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
247
2022-02-16
op
PF_UNSPEC, pipe_main2player) == -1)
248
2022-02-16
op
fatal("socketpair");
250
2022-02-16
op
player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
252
2022-02-16
op
event_init();
254
2022-02-16
op
signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
255
2022-02-16
op
signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
256
2022-02-16
op
signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
258
2022-02-16
op
signal_add(&ev_sigint, NULL);
259
2022-02-16
op
signal_add(&ev_sigterm, NULL);
260
2022-02-16
op
signal_add(&ev_siginfo, NULL);
262
2022-02-16
op
signal(SIGHUP, SIG_IGN);
263
2022-02-16
op
signal(SIGCHLD, SIG_IGN);
264
2022-02-16
op
signal(SIGPIPE, SIG_IGN);
266
2022-02-16
op
iev_player = xmalloc(sizeof(*iev_player));
267
2022-02-16
op
imsg_init(&iev_player->ibuf, pipe_main2player[0]);
268
2022-02-16
op
iev_player->handler = main_dispatch_player;
269
2022-02-16
op
iev_player->events = EV_READ;
270
2022-02-16
op
event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
271
2022-02-16
op
iev_player->handler, iev_player);
272
2022-02-16
op
event_add(&iev_player->ev, NULL);
274
2022-02-16
op
if ((control_fd = control_init(csock)) == -1)
275
2022-02-16
op
fatal("control socket setup failed %s", csock);
276
2022-02-16
op
control_listen(control_fd);
278
2022-02-16
op
if (pledge("stdio rpath unix sendfd", NULL) == -1)
279
2022-02-16
op
fatal("pledge");
281
2022-02-16
op
log_info("startup");
282
2022-02-16
op
event_dispatch();
283
2022-02-16
op
main_shutdown();
287
2022-02-16
op
main(int argc, char **argv)
289
2022-02-16
op
int ch, proc = PROC_MAIN;
291
2022-02-16
op
log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
292
2022-02-16
op
log_setverbose(1);
294
2022-02-16
op
argv0 = argv[0];
295
2022-02-16
op
if (argv0 == NULL)
296
2022-02-16
op
argv0 = "amused";
298
2022-02-16
op
while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
299
2022-02-16
op
switch (ch) {
300
2022-02-16
op
case 'd':
301
2022-02-16
op
debug = 1;
303
2022-02-16
op
case 's':
304
2022-02-16
op
free(csock);
305
2022-02-16
op
csock = xstrdup(optarg);
307
2022-02-16
op
case 'T':
308
2022-02-16
op
switch (*optarg) {
309
2022-02-16
op
case 'p':
310
2022-02-16
op
proc = PROC_PLAYER;
316
2022-02-16
op
case 'v':
317
2022-02-16
op
verbose++;
319
2022-02-16
op
case 'V':
320
2022-02-16
op
printf("%s version %s\n", getprogname(),
321
2022-02-16
op
AMUSED_VERSION);
327
2022-02-16
op
argv += optind;
328
2022-02-16
op
argc -= optind;
330
2022-02-16
op
if (proc == PROC_PLAYER)
331
2022-02-16
op
exit(player(debug, verbose));
333
2022-02-16
op
if (csock == NULL)
334
2022-02-16
op
xasprintf(&csock, "/tmp/amused-%d", getuid());
336
2022-02-16
op
if (argc == 0)
337
2022-02-16
op
amused_main();
339
2022-02-16
op
ctl(argc, argv);
340
2022-02-16
op
return 0;
344
2022-02-16
op
spawn_daemon(void)
346
2022-02-16
op
debug = 0;
347
2022-02-16
op
start_child(PROC_MAIN, -1);
351
2022-02-16
op
imsg_event_add(struct imsgev *iev)
353
2022-02-16
op
iev->events = EV_READ;
354
2022-02-16
op
if (iev->ibuf.w.queued)
355
2022-02-16
op
iev->events |= EV_WRITE;
357
2022-02-16
op
event_del(&iev->ev);
358
2022-02-16
op
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
359
2022-02-16
op
event_add(&iev->ev, NULL);
363
2022-02-16
op
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
364
2022-02-16
op
pid_t pid, int fd, const void *data, uint16_t datalen)
368
2022-02-16
op
if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
369
2022-02-16
op
datalen)) != -1)
370
2022-02-16
op
imsg_event_add(iev);
372
2022-02-16
op
return ret;
376
2022-02-16
op
main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
378
2022-02-16
op
return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
382
2022-02-16
op
main_play_song(const char *song)
384
2022-02-16
op
char path[PATH_MAX] = { 0 };
387
2022-02-16
op
strlcpy(path, song, sizeof(path));
388
2022-02-16
op
if ((fd = open(path, O_RDONLY)) == -1) {
389
2022-02-16
op
log_warn("open %s", path);
390
2022-02-16
op
return 0;
393
2022-02-16
op
play_state = STATE_PLAYING;
394
2022-02-16
op
imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
395
2022-02-16
op
path, sizeof(path));
396
2022-02-16
op
return 1;
400
2022-02-17
op
main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
402
2022-02-17
op
size_t datalen;
403
2022-02-17
op
char arg[PATH_MAX];
404
2022-02-17
op
const char *song;
406
2022-02-17
op
datalen = IMSG_DATA_SIZE(*imsg);
407
2022-02-17
op
if (datalen != sizeof(arg)) {
408
2022-02-17
op
main_senderr(iev, "wrong size");
412
2022-02-17
op
memcpy(arg, imsg->data, sizeof(arg));
413
2022-02-17
op
if (arg[sizeof(arg)-1] != '\0') {
414
2022-02-17
op
main_senderr(iev, "data corrupted");
418
2022-02-17
op
song = playlist_jump(arg);
419
2022-02-17
op
if (song == NULL) {
420
2022-02-17
op
main_senderr(iev, "not found");
424
2022-02-17
op
main_send_player(IMSG_STOP, -1, NULL, 0);
425
2022-02-17
op
if (!main_play_song(song)) {
426
2022-02-17
op
main_senderr(iev, "can't play");
427
2022-02-17
op
playlist_dropcurrent();
428
2022-02-17
op
main_playlist_advance();
432
2022-02-17
op
main_send_status(iev);
436
2022-02-17
op
main_playlist_resume(void)
438
2022-02-17
op
const char *song;
440
2022-02-17
op
if ((song = playlist_current()) == NULL)
441
2022-02-17
op
song = playlist_advance();
443
2022-02-17
op
for (; song != NULL; song = playlist_advance()) {
444
2022-02-17
op
if (main_play_song(song))
447
2022-02-17
op
playlist_dropcurrent();
452
2022-02-16
op
main_playlist_advance(void)
454
2022-02-16
op
const char *song;
456
2022-02-16
op
for (;;) {
457
2022-02-16
op
song = playlist_advance();
458
2022-02-16
op
if (song == NULL)
461
2022-02-16
op
if (main_play_song(song))
464
2022-02-16
op
playlist_dropcurrent();
469
2022-02-17
op
main_playlist_previous(void)
471
2022-02-17
op
const char *song;
473
2022-02-17
op
for (;;) {
474
2022-02-17
op
song = playlist_previous();
475
2022-02-17
op
if (song == NULL)
478
2022-02-17
op
if (main_play_song(song))
481
2022-02-17
op
playlist_dropcurrent();
486
2022-02-16
op
main_restart_track(void)
488
2022-02-16
op
const char *song;
490
2022-02-16
op
song = playlist_current();
491
2022-02-16
op
if (song == NULL)
494
2022-02-16
op
if (main_play_song(song))
497
2022-02-16
op
playlist_dropcurrent();
498
2022-02-16
op
main_playlist_advance();
502
2022-02-17
op
main_senderr(struct imsgev *iev, const char *msg)
504
2022-02-17
op
imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
505
2022-02-17
op
msg, strlen(msg)+1);
509
2022-02-17
op
main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
510
2022-02-17
op
struct imsg *imsg)
512
2022-02-16
op
size_t datalen;
513
2022-02-16
op
char path[PATH_MAX] = { 0 };
514
2022-02-16
op
const char *err = NULL;
516
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
517
2022-02-16
op
if (datalen != sizeof(path)) {
518
2022-02-16
op
err = "data size mismatch";
519
2022-02-16
op
goto err;
522
2022-02-16
op
memcpy(path, imsg->data, sizeof(path));
523
2022-02-16
op
if (path[datalen-1] != '\0') {
524
2022-02-16
op
err = "malformed data";
525
2022-02-16
op
goto err;
529
2022-02-17
op
playlist_push(px, path);
531
2022-02-17
op
playlist_enqueue(path);
532
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
535
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
539
2022-02-16
op
main_send_playlist(struct imsgev *iev)
541
2022-02-17
op
struct player_status s;
542
2022-02-16
op
size_t i;
544
2022-02-16
op
for (i = 0; i < playlist.len; ++i) {
545
2022-02-17
op
memset(&s, 0, sizeof(s));
546
2022-02-17
op
strlcpy(s.path, playlist.songs[i], sizeof(s.path));
547
2022-02-17
op
s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
548
2022-02-17
op
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
549
2022-02-17
op
sizeof(s));
552
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
556
2022-02-16
op
main_send_status(struct imsgev *iev)
558
2022-02-16
op
struct player_status s;
559
2022-02-16
op
const char *song;
561
2022-02-16
op
memset(&s, 0, sizeof(s));
563
2022-02-16
op
song = playlist_current();
564
2022-02-16
op
if (song != NULL)
565
2022-02-16
op
strlcpy(s.path, song, sizeof(s.path));
566
2022-02-16
op
s.status = play_state;
568
2022-02-16
op
imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));