forked from jonsafari/mocp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
1879 lines (1554 loc) · 39.1 KB
/
server.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MOC - music on console
* Copyright (C) 2003 - 2005 Damian Pietras <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifdef HAVE_GETRLIMIT
# include <sys/resource.h>
#endif
#include <sys/un.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
#include <pthread.h>
#include <assert.h>
#define DEBUG
#include "common.h"
#include "log.h"
#include "protocol.h"
#include "audio.h"
#include "oss.h"
#include "options.h"
#include "server.h"
#include "playlist.h"
#include "tags_cache.h"
#include "files.h"
#include "softmixer.h"
#include "equalizer.h"
#define SERVER_LOG "mocp_server_log"
#define PID_FILE "pid"
struct client
{
int socket; /* -1 if inactive */
int wants_plist_events; /* requested playlist events? */
struct event_queue events;
pthread_mutex_t events_mtx;
int requests_plist; /* is the client waiting for the playlist? */
int can_send_plist; /* can this client send a playlist? */
int lock; /* is this client locking us? */
int serial; /* used for generating unique serial numbers */
};
static struct client clients[CLIENTS_MAX];
/* Thread ID of the server thread. */
static pthread_t server_tid;
/* Pipe used to wake up the server from select() from another thread. */
static int wake_up_pipe[2];
/* Socket used to accept incoming client connections. */
static int server_sock = -1;
/* Set to 1 when a signal arrived causing the program to exit. */
static volatile int server_quit = 0;
/* Information about currently played file */
static struct {
int avg_bitrate;
int bitrate;
int rate;
int channels;
} sound_info = {
-1,
-1,
-1,
-1
};
static struct tags_cache *tags_cache;
extern char **environ;
static void write_pid_file ()
{
char *fname = create_file_name (PID_FILE);
FILE *file;
if ((file = fopen(fname, "w")) == NULL)
fatal ("Can't open pid file for writing: %s", xstrerror (errno));
fprintf (file, "%d\n", getpid());
fclose (file);
}
/* Check if there is a pid file and if it is valid, return the pid, else 0 */
static pid_t check_pid_file ()
{
FILE *file;
pid_t pid;
char *fname = create_file_name (PID_FILE);
/* Read the pid file */
if ((file = fopen(fname, "r")) == NULL)
return 0;
if (fscanf(file, "%d", &pid) != 1) {
fclose (file);
return 0;
}
fclose (file);
return pid;
}
static void sig_chld (int sig LOGIT_ONLY)
{
int saved_errno;
pid_t rc;
log_signal (sig);
saved_errno = errno;
do {
rc = waitpid (-1, NULL, WNOHANG);
} while (rc > 0);
errno = saved_errno;
}
static void sig_exit (int sig)
{
log_signal (sig);
server_quit = 1;
// FIXME (JCF): pthread_*() are not async-signal-safe and
// should not be used within signal handlers.
if (!pthread_equal (server_tid, pthread_self()))
pthread_kill (server_tid, sig);
}
static void clients_init ()
{
int i;
for (i = 0; i < CLIENTS_MAX; i++) {
clients[i].socket = -1;
pthread_mutex_init (&clients[i].events_mtx, NULL);
}
}
static void clients_cleanup ()
{
int i, rc;
for (i = 0; i < CLIENTS_MAX; i++) {
clients[i].socket = -1;
rc = pthread_mutex_destroy (&clients[i].events_mtx);
if (rc != 0)
log_errno ("Can't destroy events mutex", rc);
}
}
/* Add a client to the list, return 1 if ok, 0 on error (max clients exceeded) */
static int add_client (int sock)
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].socket == -1) {
clients[i].wants_plist_events = 0;
LOCK (clients[i].events_mtx);
event_queue_free (&clients[i].events);
event_queue_init (&clients[i].events);
UNLOCK (clients[i].events_mtx);
clients[i].socket = sock;
clients[i].requests_plist = 0;
clients[i].can_send_plist = 0;
clients[i].lock = 0;
tags_cache_clear_queue (tags_cache, i);
return 1;
}
return 0;
}
/* Return index of a client that has a lock acquired. Return -1 if there is no
* lock. */
static int locking_client ()
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].socket != -1 && clients[i].lock)
return i;
return -1;
}
/* Acquire a lock for this client. Return 0 on error. */
static int client_lock (struct client *cli)
{
if (cli->lock) {
logit ("Client wants deadlock");
return 0;
}
assert (locking_client() == -1);
cli->lock = 1;
logit ("Lock acquired for client with fd %d", cli->socket);
return 1;
}
/* Return != 0 if this client holds a lock. */
static int is_locking (const struct client *cli)
{
return cli->lock;
}
/* Release the lock hold by the client. Return 0 on error. */
static int client_unlock (struct client *cli)
{
if (!cli->lock) {
logit ("Client wants to unlock when there is no lock");
return 0;
}
cli->lock = 0;
logit ("Lock released by client with fd %d", cli->socket);
return 1;
}
/* Return the client index from the clients table. */
static int client_index (const struct client *cli)
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].socket == cli->socket)
return i;
return -1;
}
static void del_client (struct client *cli)
{
cli->socket = -1;
LOCK (cli->events_mtx);
event_queue_free (&cli->events);
tags_cache_clear_queue (tags_cache, client_index(cli));
UNLOCK (cli->events_mtx);
}
/* Check if the process with given PID exists. Return != 0 if so. */
static int valid_pid (const pid_t pid)
{
return kill(pid, 0) == 0 ? 1 : 0;
}
static void wake_up_server ()
{
int w = 1;
debug ("Waking up the server");
if (write(wake_up_pipe[1], &w, sizeof(w)) < 0)
log_errno ("Can't wake up the server: (write() failed)", errno);
}
static void redirect_output (FILE *stream)
{
FILE *rc;
if (stream == stdin)
rc = freopen ("/dev/null", "r", stream);
else
rc = freopen ("/dev/null", "w", stream);
if (!rc)
fatal ("Can't open /dev/null: %s", xstrerror (errno));
}
static void log_process_stack_size ()
{
#if !defined(NDEBUG) && defined(HAVE_GETRLIMIT)
int rc;
struct rlimit limits;
rc = getrlimit (RLIMIT_STACK, &limits);
if (rc == 0)
logit ("Process's stack size: %u", (unsigned int)limits.rlim_cur);
#endif
}
static void log_pthread_stack_size ()
{
#if !defined(NDEBUG) && defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE)
int rc;
size_t stack_size;
pthread_attr_t attr;
rc = pthread_attr_init (&attr);
if (rc)
return;
rc = pthread_attr_getstacksize (&attr, &stack_size);
if (rc == 0)
logit ("PThread's stack size: %u", (unsigned int)stack_size);
pthread_attr_destroy (&attr);
#endif
}
/* Handle running external command on requested event. */
static void run_extern_cmd (const char *event)
{
char *command;
command = xstrdup (options_get_str (event));
if (command) {
char *args[2], *err;
args[0] = xstrdup (command);
args[1] = NULL;
switch (fork ()) {
case 0:
execve (command, args, environ);
fatal ("Error when running %s command '%s': %s",
event, command, xstrerror (errno));
case -1:
err = xstrerror (errno);
logit ("Error when running %s command '%s': %s",
event, command, err);
free (err);
break;
}
free (command);
free (args[0]);
}
}
/* Initialize the server - return fd of the listening socket or -1 on error */
void server_init (int debugging, int foreground)
{
struct sockaddr_un sock_name;
pid_t pid;
logit ("Starting MOC Server");
assert (server_sock == -1);
pid = check_pid_file ();
if (pid && valid_pid(pid)) {
fprintf (stderr, "\nIt seems that the server is already running"
" with pid %d.\n", pid);
fprintf (stderr, "If it is not true, remove the pid file (%s)"
" and try again.\n",
create_file_name(PID_FILE));
fatal ("Exiting!");
}
if (foreground)
log_init_stream (stdout, "stdout");
else {
FILE *logfp;
logfp = NULL;
if (debugging) {
logfp = fopen (SERVER_LOG, "a");
if (!logfp)
fatal ("Can't open server log file: %s", xstrerror (errno));
}
log_init_stream (logfp, SERVER_LOG);
}
if (pipe(wake_up_pipe) < 0)
fatal ("pipe() failed: %s", xstrerror (errno));
unlink (socket_name());
/* Create a socket.
* For reasons why AF_UNIX is the correct constant to use in both
* cases, see the commentary the SVN log for commit r9999. */
server_sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1)
fatal ("Can't create socket: %s", xstrerror (errno));
sock_name.sun_family = AF_UNIX;
strcpy (sock_name.sun_path, socket_name());
/* Bind to socket */
if (bind(server_sock, (struct sockaddr *)&sock_name, SUN_LEN(&sock_name)) == -1)
fatal ("Can't bind() to the socket: %s", xstrerror (errno));
if (listen(server_sock, 1) == -1)
fatal ("listen() failed: %s", xstrerror (errno));
/* Log stack sizes so stack overflows can be debugged. */
log_process_stack_size ();
log_pthread_stack_size ();
clients_init ();
audio_initialize ();
tags_cache = tags_cache_new (options_get_int("TagsCacheSize"));
tags_cache_load (tags_cache, create_file_name("cache"));
server_tid = pthread_self ();
xsignal (SIGTERM, sig_exit);
xsignal (SIGINT, foreground ? sig_exit : SIG_IGN);
xsignal (SIGHUP, SIG_IGN);
xsignal (SIGQUIT, sig_exit);
xsignal (SIGPIPE, SIG_IGN);
xsignal (SIGCHLD, sig_chld);
write_pid_file ();
if (!foreground) {
setsid ();
redirect_output (stdin);
redirect_output (stdout);
redirect_output (stderr);
}
logit ("Running OnServerStart");
run_extern_cmd ("OnServerStart");
return;
}
/* Send EV_DATA and the integer value. Return 0 on error. */
static int send_data_int (const struct client *cli, const int data)
{
assert (cli->socket != -1);
if (!send_int(cli->socket, EV_DATA) || !send_int(cli->socket, data))
return 0;
return 1;
}
/* Send EV_DATA and the boolean value. Return 0 on error. */
static int send_data_bool (const struct client *cli, const bool data)
{
assert (cli->socket != -1);
if (!send_int(cli->socket, EV_DATA) ||
!send_int(cli->socket, data ? 1 : 0))
return 0;
return 1;
}
/* Send EV_DATA and the string value. Return 0 on error. */
static int send_data_str (const struct client *cli, const char *str) {
if (!send_int(cli->socket, EV_DATA) || !send_str(cli->socket, str))
return 0;
return 1;
}
/* Add event to the client's queue */
static void add_event (struct client *cli, const int event, void *data)
{
LOCK (cli->events_mtx);
event_push (&cli->events, event, data);
UNLOCK (cli->events_mtx);
}
static void on_song_change ()
{
static char *last_file = NULL;
static lists_t_strs *on_song_change = NULL;
int ix;
bool same_file, unpaused;
char *curr_file, **args;
struct file_tags *curr_tags;
lists_t_strs *arg_list;
/* We only need to do OnSongChange tokenisation once. */
if (on_song_change == NULL) {
char *command;
on_song_change = lists_strs_new (4);
command = options_get_str ("OnSongChange");
if (command)
lists_strs_tokenise (on_song_change, command);
}
if (lists_strs_empty (on_song_change))
return;
curr_file = audio_get_sname ();
if (curr_file == NULL)
return;
same_file = (last_file && !strcmp (last_file, curr_file));
unpaused = (audio_get_prev_state () == STATE_PAUSE);
if (same_file && (unpaused || !options_get_bool ("RepeatSongChange"))) {
free (curr_file);
return;
}
curr_tags = tags_cache_get_immediate (tags_cache, curr_file,
TAGS_COMMENTS | TAGS_TIME);
arg_list = lists_strs_new (lists_strs_size (on_song_change));
for (ix = 0; ix < lists_strs_size (on_song_change); ix += 1) {
char *arg, *str;
arg = lists_strs_at (on_song_change, ix);
if (arg[0] != '%')
lists_strs_append (arg_list, arg);
else if (!curr_tags)
lists_strs_append (arg_list, "");
else {
switch (arg[1]) {
case 'a':
str = curr_tags->artist ? curr_tags->artist : "";
lists_strs_append (arg_list, str);
break;
case 'r':
str = curr_tags->album ? curr_tags->album : "";
lists_strs_append (arg_list, str);
break;
case 't':
str = curr_tags->title ? curr_tags->title : "";
lists_strs_append (arg_list, str);
break;
case 'n':
if (curr_tags->track >= 0) {
str = (char *) xmalloc (sizeof (char) * 16);
snprintf (str, 16, "%d", curr_tags->track);
lists_strs_push (arg_list, str);
}
else
lists_strs_append (arg_list, "");
break;
case 'f':
lists_strs_append (arg_list, curr_file);
break;
case 'D':
if (curr_tags->time >= 0) {
str = (char *) xmalloc (sizeof (char) * 16);
snprintf (str, 16, "%d", curr_tags->time);
lists_strs_push (arg_list, str);
}
else
lists_strs_append (arg_list, "");
break;
case 'd':
if (curr_tags->time >= 0) {
str = (char *) xmalloc (sizeof (char) * 32);
sec_to_min (str, curr_tags->time);
lists_strs_push (arg_list, str);
}
else
lists_strs_append (arg_list, "");
break;
default:
lists_strs_append (arg_list, arg);
}
}
}
tags_free (curr_tags);
#ifndef NDEBUG
{
char *cmd;
cmd = lists_strs_fmt (arg_list, " %s");
debug ("Running command: %s", cmd);
free (cmd);
}
#endif
switch (fork ()) {
case 0:
args = lists_strs_save (arg_list);
execve (args[0], args, environ);
fatal ("Error when running OnSongChange command '%s': %s",
args[0], xstrerror (errno));
case -1:
log_errno ("Failed to fork()", errno);
}
lists_strs_free (arg_list);
free (last_file);
last_file = curr_file;
}
/* Return true iff 'event' is a playlist event. */
static inline bool is_plist_event (const int event)
{
bool result = false;
switch (event) {
case EV_PLIST_ADD:
case EV_PLIST_DEL:
case EV_PLIST_MOVE:
case EV_PLIST_CLEAR:
result = true;
}
return result;
}
static void add_event_all (const int event, const void *data)
{
int i;
int added = 0;
if (event == EV_STATE) {
switch (audio_get_state()) {
case STATE_PLAY:
on_song_change ();
break;
case STATE_STOP:
run_extern_cmd ("OnStop");
break;
}
}
for (i = 0; i < CLIENTS_MAX; i++) {
void *data_copy = NULL;
if (clients[i].socket == -1)
continue;
if (!clients[i].wants_plist_events && is_plist_event (event))
continue;
if (data) {
if (event == EV_PLIST_ADD
|| event == EV_QUEUE_ADD) {
data_copy = plist_new_item ();
plist_item_copy (data_copy, data);
}
else if (event == EV_PLIST_DEL
|| event == EV_QUEUE_DEL
|| event == EV_STATUS_MSG
|| event == EV_SRV_ERROR) {
data_copy = xstrdup (data);
}
else if (event == EV_PLIST_MOVE
|| event == EV_QUEUE_MOVE)
data_copy = move_ev_data_dup (
(struct move_ev_data *)
data);
else
logit ("Unhandled data!");
}
add_event (&clients[i], event, data_copy);
added++;
}
if (added)
wake_up_server ();
else
debug ("No events have been added because there are no clients");
}
/* Send events from the queue. Return 0 on error. */
static int flush_events (struct client *cli)
{
enum noblock_io_status st = NB_IO_OK;
LOCK (cli->events_mtx);
while (!event_queue_empty(&cli->events)
&& (st = event_send_noblock(cli->socket, &cli->events))
== NB_IO_OK)
;
UNLOCK (cli->events_mtx);
return st != NB_IO_ERR ? 1 : 0;
}
/* Send events to clients whose sockets are ready to write. */
static void send_events (fd_set *fds)
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].socket != -1
&& FD_ISSET(clients[i].socket, fds)) {
debug ("Flushing events for client %d", i);
if (!flush_events (&clients[i])) {
close (clients[i].socket);
del_client (&clients[i]);
}
}
}
/* End playing and cleanup. */
static void server_shutdown ()
{
logit ("Server exiting...");
audio_exit ();
tags_cache_free (tags_cache);
tags_cache = NULL;
logit ("Running OnServerStop");
run_extern_cmd ("OnServerStop");
unlink (socket_name());
unlink (create_file_name(PID_FILE));
close (wake_up_pipe[0]);
close (wake_up_pipe[1]);
logit ("Server exited");
log_close ();
}
/* Send EV_BUSY message and close the connection. */
static void busy (int sock)
{
logit ("Closing connection due to maximum number of clients reached");
send_int (sock, EV_BUSY);
close (sock);
}
/* Handle CMD_LIST_ADD, return 1 if ok or 0 on error. */
static int req_list_add (struct client *cli)
{
char *file;
file = get_str (cli->socket);
if (!file)
return 0;
logit ("Adding '%s' to the list", file);
audio_plist_add (file);
free (file);
return 1;
}
/* Handle CMD_QUEUE_ADD, return 1 if ok or 0 on error. */
static int req_queue_add (const struct client *cli)
{
char *file;
struct plist_item *item;
file = get_str (cli->socket);
if (!file)
return 0;
logit ("Adding '%s' to the queue", file);
audio_queue_add (file);
/* Wrap the filename in struct plist_item.
* We don't need tags, because the player gets them
* when playing the file. This may change if there is
* support for viewing/reordering the queue and here
* is the place to read the tags and fill them into
* the item. */
item = plist_new_item ();
item->file = xstrdup (file);
item->type = file_type (file);
item->mtime = get_mtime (file);
add_event_all (EV_QUEUE_ADD, item);
plist_free_item_fields (item);
free (item);
free (file);
return 1;
}
/* Handle CMD_PLAY, return 1 if ok or 0 on error. */
static int req_play (struct client *cli)
{
char *file;
if (!(file = get_str(cli->socket)))
return 0;
logit ("Playing %s", *file ? file : "first element on the list");
audio_play (file);
free (file);
return 1;
}
/* Handle CMD_SEEK, return 1 if ok or 0 on error */
static int req_seek (struct client *cli)
{
int sec;
if (!get_int(cli->socket, &sec))
return 0;
logit ("Seeking %ds", sec);
audio_seek (sec);
return 1;
}
/* Handle CMD_JUMP_TO, return 1 if ok or 0 on error */
static int req_jump_to (struct client *cli)
{
int sec;
if (!get_int(cli->socket, &sec))
return 0;
logit ("Jumping to %ds", sec);
audio_jump_to (sec);
return 1;
}
/* Report an error logging it and sending a message to the client. */
void server_error (const char *file, int line, const char *function,
const char *msg)
{
internal_logit (file, line, function, "ERROR: %s", msg);
add_event_all (EV_SRV_ERROR, msg);
}
/* Send the song name to the client. Return 0 on error. */
static int send_sname (struct client *cli)
{
int status = 1;
char *sname = audio_get_sname ();
if (!send_data_str(cli, sname ? sname : ""))
status = 0;
free (sname);
return status;
}
/* Return 0 if an option is valid when getting/setting with the client. */
static int valid_sync_option (const char *name)
{
return !strcasecmp(name, "ShowStreamErrors")
|| !strcasecmp(name, "Repeat")
|| !strcasecmp(name, "Shuffle")
|| !strcasecmp(name, "AutoNext");
}
/* Send requested option value to the client. Return 1 if OK. */
static int send_option (struct client *cli)
{
char *name;
if (!(name = get_str(cli->socket)))
return 0;
/* We can send only a few options, others make no sense here. */
if (!valid_sync_option(name)) {
logit ("Client wanted to get invalid option '%s'", name);
free (name);
return 0;
}
/* All supported options are boolean type. */
if (!send_data_bool(cli, options_get_bool(name))) {
free (name);
return 0;
}
free (name);
return 1;
}
/* Get and set an option from the client. Return 1 on error. */
static int get_set_option (struct client *cli)
{
char *name;
int val;
if (!(name = get_str (cli->socket)))
return 0;
if (!valid_sync_option (name)) {
logit ("Client requested setting invalid option '%s'", name);
return 0;
}
if (!get_int (cli->socket, &val)) {
free (name);
return 0;
}
options_set_bool (name, val ? true : false);
free (name);
add_event_all (EV_OPTIONS, NULL);
return 1;
}
/* Set the mixer to the value provided by the client. Return 0 on error. */
static int set_mixer (struct client *cli)
{
int val;
if (!get_int(cli->socket, &val))
return 0;
audio_set_mixer (val);
return 1;
}
/* Delete an item from the playlist. Return 0 on error. */
static int delete_item (struct client *cli)
{
char *file;
if (!(file = get_str(cli->socket)))
return 0;
debug ("Request for deleting %s", file);
audio_plist_delete (file);
free (file);
return 1;
}
static int req_queue_del (const struct client *cli)
{
char *file;
if (!(file = get_str(cli->socket)))
return 0;
debug ("Deleting '%s' from queue", file);
audio_queue_delete (file);
add_event_all (EV_QUEUE_DEL, file);
free (file);
return 1;
}
/* Return the index of the first client able to send the playlist or -1 if
* there isn't any. */
static int find_sending_plist ()
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].socket != -1 && clients[i].can_send_plist)
return i;
return -1;
}
/* Handle CMD_GET_PLIST. Return 0 on error. */
static int get_client_plist (struct client *cli)
{
int first;
debug ("Client with fd %d requests the playlist", cli->socket);
/* Find the first connected client, and ask it to send the playlist.
* Here, send 1 if there is a client with the playlist, or 0 if there
* isn't. */
cli->requests_plist = 1;
first = find_sending_plist ();
if (first == -1) {
debug ("No clients with the playlist");
cli->requests_plist = 0;
if (!send_data_int(cli, 0))
return 0;
return 1;
}
if (!send_data_int(cli, 1))
return 0;
if (!send_int(clients[first].socket, EV_SEND_PLIST))
return 0;
return 1;
}
/* Find the client requesting the playlist. */
static int find_cli_requesting_plist ()
{
int i;
for (i = 0; i < CLIENTS_MAX; i++)
if (clients[i].requests_plist)
return i;
return -1;
}