1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file afs.c Paraslash's audio file selector. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
12 #include <arpa/inet.h>
17 #include "server.lsg.h"
18 #include "server_cmd.lsg.h"
36 /** The osl tables used by afs. \sa \ref blob.c. */
38 /** Contains audio file information. See \ref aft.c. */
40 /** The table for the paraslash attributes. See \ref attribute.c. */
43 * Moods and playlists organize the current set of admissible files in
44 * an osl table which contains only volatile columns. Each row consists
45 * of a pointer to an audio file and the score value of this file.
49 * A standard blob table containing the mood definitions. For details
53 /** A blob table containing lyrics on a per-song basis. */
55 /** Another blob table for images (for example album cover art). */
57 /** Yet another blob table for storing standard playlists. */
59 /** How many tables are in use? */
63 static struct afs_table afs_tables
[NUM_AFS_TABLES
] = {
64 [TBLNUM_AUDIO_FILES
] = {.init
= aft_init
, .name
= "audio_files"},
65 [TBLNUM_ATTRIBUTES
] = {.init
= attribute_init
, .name
= "attributes"},
66 [TBLNUM_SCORES
] = {.init
= score_init
, .name
= "scores"},
67 [TBLNUM_MOODS
] = {.init
= moods_init
, .name
= "moods"},
68 [TBLNUM_LYRICS
] = {.init
= lyrics_init
, .name
= "lyrics"},
69 [TBLNUM_IMAGES
] = {.init
= images_init
, .name
= "images"},
70 [TBLNUM_PLAYLIST
] = {.init
= playlists_init
, .name
= "playlists"},
74 /** The file descriptor for the local socket. */
76 /** The associated task structure. */
81 extern struct misc_meta_data
*mmd
;
83 static int server_socket
;
84 static struct command_task command_task_struct
;
85 static struct signal_task
*signal_task
;
87 static enum play_mode current_play_mode
;
88 static char *current_mop
; /* mode or playlist specifier. NULL means dummy mood */
90 extern uint32_t afs_socket_cookie
;
93 * Passed from command handlers to afs.
95 * Command handlers cannot change the afs database directly because they run in
96 * a separate process. The callback query structure circumvents this
97 * restriction as follows. To instruct the afs process to execute a particular
98 * function, the command hander writes an instance of this structure to a
99 * shared memory area, along with the arguments to the callback function. The
100 * identifier of the shared memory area is transferred to the afs process via
101 * the command socket.
103 * The afs process reads the shared memory id from the command socket, attaches
104 * the corresponding area, and calls the callback function whose address is
105 * stored in the area.
107 * The command output, if any, is transferred back to the command handler in
108 * the same way: The afs process writes the output to a second shared memory
109 * area together with a fixed size metadata header whose format corresponds to
110 * the \ref callback_result structure. The identifier of this area is sent back
111 * to the command handler which attaches the area and forwards the output to
114 * \sa \ref struct callback_result.
116 struct callback_query
{
117 /** The function to be called. */
118 afs_callback
*handler
;
119 /** The number of bytes of the query */
124 * Structure embedded in the result of a callback.
126 * If the callback produced a result, an instance of that structure is embedded
127 * into the shared memory area holding the result, mainly to let the command
128 * handler know the size of the result.
130 * \sa \ref struct callback_query.
132 struct callback_result
{
133 /** The number of bytes of the result. */
135 /** The band designator (loglevel for the result). */
139 static int dispatch_result(int result_shmid
, callback_result_handler
*handler
,
140 void *private_result_data
)
142 struct osl_object result
;
144 /* must attach r/w as result.data might get encrypted in-place. */
145 int ret2
, ret
= shm_attach(result_shmid
, ATTACH_RW
, &result_shm
);
146 struct callback_result
*cr
= result_shm
;
149 PARA_ERROR_LOG("attach failed: %s\n", para_strerror(-ret
));
152 result
.size
= cr
->result_size
;
153 result
.data
= result_shm
+ sizeof(*cr
);
155 ret
= handler(&result
, cr
->band
, private_result_data
);
156 ret2
= shm_detach(result_shm
);
158 PARA_ERROR_LOG("detach failed: %s\n", para_strerror(-ret2
));
166 * Ask the afs process to call a given function.
168 * \param f The function to be called.
169 * \param query Pointer to arbitrary data for the callback.
170 * \param result_handler Called for each shm area sent by the callback.
171 * \param private_result_data Passed verbatim to \a result_handler.
173 * This function creates a socket for communication with the afs process and a
174 * shared memory area (sma) to which the buffer pointed to by \a query is
175 * copied. It then notifies the afs process that the callback function \a f
176 * should be executed by sending the shared memory identifier (shmid) to the
179 * If the callback produces a result, it sends any number of shared memory
180 * identifiers back via the socket. For each such identifier received, \a
181 * result_handler is called. The contents of the sma identified by the received
182 * shmid are passed to that function as an osl object. The private_result_data
183 * pointer is passed as the second argument to \a result_handler.
185 * \return Number of shared memory areas dispatched on success, negative on
188 int send_callback_request(afs_callback
*f
, struct osl_object
*query
,
189 callback_result_handler
*result_handler
,
190 void *private_result_data
)
192 struct callback_query
*cq
;
193 int ret
, fd
= -1, query_shmid
, result_shmid
;
195 char buf
[sizeof(afs_socket_cookie
) + sizeof(int)];
196 size_t query_shm_size
= sizeof(*cq
);
197 int dispatch_error
= 0, num_dispatched
= 0;
200 query_shm_size
+= query
->size
;
201 ret
= shm_new(query_shm_size
);
205 ret
= shm_attach(query_shmid
, ATTACH_RW
, &query_shm
);
210 cq
->query_size
= query_shm_size
- sizeof(*cq
);
213 memcpy(query_shm
+ sizeof(*cq
), query
->data
, query
->size
);
214 ret
= shm_detach(query_shm
);
218 *(uint32_t *)buf
= afs_socket_cookie
;
219 *(int *)(buf
+ sizeof(afs_socket_cookie
)) = query_shmid
;
221 ret
= connect_local_socket(OPT_STRING_VAL(AFS_SOCKET
));
225 ret
= write_all(fd
, buf
, sizeof(buf
));
229 * Read all shmids from afs.
231 * Even if the dispatcher returns an error we _must_ continue to read
232 * shmids from fd so that we can destroy all shared memory areas that
233 * have been created for us by the afs process.
236 ret
= recv_bin_buffer(fd
, buf
, sizeof(int));
239 assert(ret
== sizeof(int));
243 ret
= dispatch_result(result_shmid
, result_handler
,
244 private_result_data
);
245 if (ret
< 0 && dispatch_error
>= 0)
246 dispatch_error
= ret
;
247 ret
= shm_destroy(result_shmid
);
249 PARA_CRIT_LOG("destroy result failed: %s\n",
250 para_strerror(-ret
));
254 if (shm_destroy(query_shmid
) < 0)
255 PARA_CRIT_LOG("shm destroy error\n");
258 if (dispatch_error
< 0)
259 return dispatch_error
;
262 return num_dispatched
;
266 * Wrapper for send_callback_request() which passes a lopsub parse result.
268 * \param f The callback function.
269 * \param cmd Needed for (de-)serialization.
270 * \param lpr Must match cmd.
271 * \param private_result_data Passed to send_callback_request().
273 * This function serializes the parse result given by the lpr pointer into a
274 * buffer. The buffer is sent as the query to the afs process with the callback
277 * \return The return value of the underlying call to send_callback_request().
279 int send_lls_callback_request(afs_callback
*f
,
280 const struct lls_command
* const cmd
,
281 struct lls_parse_result
*lpr
, void *private_result_data
)
283 struct osl_object query
;
285 int ret
= lls_serialize_parse_result(lpr
, cmd
, &buf
, &query
.size
);
289 ret
= send_callback_request(f
, &query
, afs_cb_result_handler
,
290 private_result_data
);
295 static int action_if_pattern_matches(struct osl_row
*row
, void *data
)
297 struct pattern_match_data
*pmd
= data
;
298 struct osl_object name_obj
;
299 const char *p
, *name
;
302 ret
= osl(osl_get_object(pmd
->table
, row
, pmd
->match_col_num
,
306 name
= (char *)name_obj
.data
;
307 if ((!name
|| !*name
) && (pmd
->pm_flags
& PM_SKIP_EMPTY_NAME
))
309 if (lls_num_inputs(pmd
->lpr
) == 0) {
310 if (pmd
->pm_flags
& PM_NO_PATTERN_MATCHES_EVERYTHING
) {
312 return pmd
->action(pmd
->table
, row
, name
, pmd
->data
);
317 if (i
>= lls_num_inputs(pmd
->lpr
))
319 p
= lls_input(i
, pmd
->lpr
);
320 ret
= fnmatch(p
, name
, pmd
->fnmatch_flags
);
321 if (ret
!= FNM_NOMATCH
) {
324 ret
= pmd
->action(pmd
->table
, row
, name
, pmd
->data
);
336 * Execute the given function for each matching row.
338 * \param pmd Describes what to match and how.
342 int for_each_matching_row(struct pattern_match_data
*pmd
)
344 if (pmd
->pm_flags
& PM_REVERSE_LOOP
)
345 return osl(osl_rbtree_loop_reverse(pmd
->table
, pmd
->loop_col_num
, pmd
,
346 action_if_pattern_matches
));
347 return osl(osl_rbtree_loop(pmd
->table
, pmd
->loop_col_num
, pmd
,
348 action_if_pattern_matches
));
352 * Compare two osl objects of string type.
354 * \param obj1 Pointer to the first object.
355 * \param obj2 Pointer to the second object.
357 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
358 * are taken into account.
360 * \return It returns an integer less than, equal to, or greater than zero if
361 * \a obj1 is found, respectively, to be less than, to match, or be greater than
364 * \sa strcmp(3), strncmp(3).
366 int string_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
368 const char *str1
= obj1
->data
;
369 const char *str2
= obj2
->data
;
370 return strncmp(str1
, str2
, PARA_MIN(obj1
->size
, obj2
->size
));
373 static int pass_afd(int fd
, char *buf
, size_t size
)
375 struct msghdr msg
= {.msg_iov
= NULL
};
376 struct cmsghdr
*cmsg
;
377 char control
[255] __a_aligned(8);
387 msg
.msg_control
= control
;
388 msg
.msg_controllen
= sizeof(control
);
390 cmsg
= CMSG_FIRSTHDR(&msg
);
391 cmsg
->cmsg_level
= SOL_SOCKET
;
392 cmsg
->cmsg_type
= SCM_RIGHTS
;
393 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
394 *(int *)CMSG_DATA(cmsg
) = fd
;
396 /* Sum of the length of all control messages in the buffer */
397 msg
.msg_controllen
= cmsg
->cmsg_len
;
398 PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size
, fd
);
399 ret
= sendmsg(server_socket
, &msg
, 0);
401 ret
= -ERRNO_TO_PARA_ERROR(errno
);
408 * Pass the fd of the next audio file to the server process.
410 * This stores all information for streaming the "best" audio file in a shared
411 * memory area. The id of that area and an open file descriptor for the next
412 * audio file are passed to the server process.
416 * \sa \ref open_and_update_audio_file().
418 static int open_next_audio_file(void)
423 ret
= open_and_update_audio_file(&fd
);
425 if (ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
426 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
427 goto no_admissible_files
;
430 if (!write_ok(server_socket
)) {
434 *(uint32_t *)buf
= NEXT_AUDIO_FILE
;
435 *(uint32_t *)(buf
+ 4) = (uint32_t)shmid
;
436 ret
= pass_afd(fd
, buf
, 8);
444 *(uint32_t *)buf
= NO_ADMISSIBLE_FILES
;
445 *(uint32_t *)(buf
+ 4) = (uint32_t)0;
446 return write_all(server_socket
, buf
, 8);
449 static int activate_mood_or_playlist(const char *arg
, int *num_admissible
,
456 mode
= PLAY_MODE_MOOD
;
457 ret
= change_current_mood(NULL
, errmsg
);
464 if (!strncmp(arg
, "p/", 2)) {
465 ret
= playlist_open(arg
+ 2);
466 if (ret
< 0 && errmsg
)
467 *errmsg
= make_message( "could not open %s",
469 mode
= PLAY_MODE_PLAYLIST
;
470 } else if (!strncmp(arg
, "m/", 2)) {
471 ret
= change_current_mood(arg
+ 2, errmsg
);
472 mode
= PLAY_MODE_MOOD
;
475 *errmsg
= make_message("%s: parse error", arg
);
476 return -ERRNO_TO_PARA_ERROR(EINVAL
);
482 *num_admissible
= ret
;
483 current_play_mode
= mode
;
485 * We get called with arg == current_mop from the signal dispatcher
486 * after SIGHUP and from the error path of the select command to
487 * re-select the current mood or playlist. In this case the assignment
488 * to current_mop below would result in a use-after-free condition.
490 if (arg
!= current_mop
) {
493 current_mop
= para_strdup(arg
);
494 mutex_lock(mmd_mutex
);
495 strncpy(mmd
->afs_mode_string
, arg
,
496 sizeof(mmd
->afs_mode_string
));
497 mmd
->afs_mode_string
[sizeof(mmd
->afs_mode_string
) - 1] = '\0';
499 mutex_unlock(mmd_mutex
);
501 mutex_lock(mmd_mutex
);
502 strcpy(mmd
->afs_mode_string
, "dummy");
504 mutex_unlock(mmd_mutex
);
512 * Result handler for sending data to the para_client process.
514 * \param result The data to be sent.
515 * \param band The band designator.
516 * \param private Pointer to the command context.
518 * \return The return value of the underlying call to \ref command.c::send_sb.
520 * \sa \ref callback_result_handler, \ref command.c::send_sb.
522 int afs_cb_result_handler(struct osl_object
*result
, uint8_t band
,
525 struct command_context
*cc
= private;
533 case SBD_WARNING_LOG
:
537 assert(result
->size
> 0);
538 return send_sb(&cc
->scc
, result
->data
, result
->size
, band
, true);
539 case SBD_AFS_CB_FAILURE
:
540 return *(int *)(result
->data
);
546 static void flush_and_free_pb(struct para_buffer
*pb
)
549 struct afs_max_size_handler_data
*amshd
= pb
->private_data
;
551 if (pb
->buf
&& pb
->size
> 0) {
552 ret
= pass_buffer_as_shm(amshd
->fd
, amshd
->band
, pb
->buf
,
555 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
560 static int com_select_callback(struct afs_callback_arg
*aca
)
562 const struct lls_command
*cmd
= SERVER_CMD_CMD_PTR(SELECT
);
564 int num_admissible
, ret
;
567 ret
= lls_deserialize_parse_result(aca
->query
.data
, cmd
, &aca
->lpr
);
569 arg
= lls_input(0, aca
->lpr
);
570 ret
= clear_score_table();
572 para_printf(&aca
->pbout
, "could not clear score table\n");
575 if (current_play_mode
== PLAY_MODE_MOOD
)
576 close_current_mood();
579 ret
= activate_mood_or_playlist(arg
, &num_admissible
, &errmsg
);
582 /* ignore subsequent errors (but log them) */
583 para_printf(&aca
->pbout
, "%s\n", errmsg
);
585 para_printf(&aca
->pbout
, "could not activate %s\n", arg
);
586 if (current_mop
&& strcmp(current_mop
, arg
) != 0) {
588 para_printf(&aca
->pbout
, "switching back to %s\n", current_mop
);
589 ret2
= activate_mood_or_playlist(current_mop
, &num_admissible
,
593 para_printf(&aca
->pbout
, "%s\n", errmsg
);
595 para_printf(&aca
->pbout
, "could not reactivate %s: %s\n",
596 current_mop
, para_strerror(-ret2
));
598 para_printf(&aca
->pbout
, "activating dummy mood\n");
599 activate_mood_or_playlist(NULL
, &num_admissible
, NULL
);
601 para_printf(&aca
->pbout
, "activated %s (%d admissible file%s)\n",
602 current_mop
? current_mop
: "dummy mood", num_admissible
,
603 num_admissible
== 1? "" : "s");
605 lls_free_parse_result(aca
->lpr
, cmd
);
609 static int com_select(struct command_context
*cc
, struct lls_parse_result
*lpr
)
611 const struct lls_command
*cmd
= SERVER_CMD_CMD_PTR(SELECT
);
613 int ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
616 send_errctx(cc
, errctx
);
619 return send_lls_callback_request(com_select_callback
, cmd
, lpr
, cc
);
621 EXPORT_SERVER_CMD_HANDLER(select
);
623 static void init_admissible_files(const char *arg
)
625 int ret
= activate_mood_or_playlist(arg
, NULL
, NULL
);
627 PARA_WARNING_LOG("could not activate %s: %s\n", arg
,
628 para_strerror(-ret
));
630 activate_mood_or_playlist(NULL
, NULL
, NULL
);
634 static int setup_command_socket_or_die(void)
637 const char *socket_name
= OPT_STRING_VAL(AFS_SOCKET
);
640 ret
= create_local_socket(socket_name
);
642 PARA_EMERG_LOG("%s: %s\n", para_strerror(-ret
), socket_name
);
646 PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name
,
651 static char *database_dir
;
653 static void close_afs_tables(void)
656 PARA_NOTICE_LOG("closing afs tables\n");
657 for (i
= 0; i
< NUM_AFS_TABLES
; i
++)
658 afs_tables
[i
].close();
663 static void get_database_dir(void)
666 if (OPT_GIVEN(AFS_DATABASE_DIR
))
667 database_dir
= para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR
));
669 char *home
= para_homedir();
670 database_dir
= make_message(
671 "%s/.paraslash/afs_database-0.7", home
);
675 PARA_INFO_LOG("afs_database dir %s\n", database_dir
);
678 static int make_database_dir(void)
683 ret
= para_mkdir(database_dir
, 0777);
684 if (ret
>= 0 || ret
== -ERRNO_TO_PARA_ERROR(EEXIST
))
689 static int open_afs_tables(void)
694 PARA_NOTICE_LOG("opening %d osl tables in %s\n", NUM_AFS_TABLES
,
696 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
697 ret
= afs_tables
[i
].open(database_dir
);
700 PARA_ERROR_LOG("could not open %s\n", afs_tables
[i
].name
);
706 afs_tables
[--i
].close();
710 static int afs_signal_post_select(struct sched
*s
, __a_unused
void *context
)
714 if (getppid() == 1) {
715 PARA_EMERG_LOG("para_server died\n");
718 signum
= para_next_signal(&s
->rfds
);
721 if (signum
== SIGHUP
) {
723 parse_config_or_die(1);
724 ret
= open_afs_tables();
727 init_admissible_files(current_mop
);
730 PARA_EMERG_LOG("terminating on signal %d\n", signum
);
732 task_notify_all(s
, E_AFS_SIGNAL
);
733 return -E_AFS_SIGNAL
;
736 static void register_signal_task(struct sched
*s
)
738 para_sigaction(SIGPIPE
, SIG_IGN
);
739 signal_task
= signal_init_or_die();
740 para_install_sighandler(SIGINT
);
741 para_install_sighandler(SIGTERM
);
742 para_install_sighandler(SIGHUP
);
744 signal_task
->task
= task_register(&(struct task_info
) {
746 .pre_select
= signal_pre_select
,
747 .post_select
= afs_signal_post_select
,
748 .context
= signal_task
,
753 static struct list_head afs_client_list
;
755 /** Describes one connected afs client. */
757 /** Position in the afs client list. */
758 struct list_head node
;
759 /** The socket file descriptor for this client. */
761 /** The time the client connected. */
762 struct timeval connect_time
;
765 static void command_pre_select(struct sched
*s
, void *context
)
767 struct command_task
*ct
= context
;
768 struct afs_client
*client
;
770 para_fd_set(server_socket
, &s
->rfds
, &s
->max_fileno
);
771 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
772 list_for_each_entry(client
, &afs_client_list
, node
)
773 para_fd_set(client
->fd
, &s
->rfds
, &s
->max_fileno
);
777 * Send data as shared memory to a file descriptor.
779 * \param fd File descriptor to send the shmid to.
780 * \param band The band designator for this data.
781 * \param buf The buffer holding the data to be sent.
782 * \param size The size of \a buf.
784 * This function creates a shared memory area large enough to hold
785 * the content given by \a buf and \a size and sends the identifier
786 * of this area to the file descriptor \a fd.
788 * It is called by the AFS max_size handler as well as directly by the AFS
789 * command callbacks to send command output to the command handlers.
791 * \return Zero if \a buf is \p NULL or \a size is zero. Negative on errors,
792 * and positive on success.
794 int pass_buffer_as_shm(int fd
, uint8_t band
, const char *buf
, size_t size
)
798 struct callback_result
*cr
;
801 assert(band
!= SBD_OUTPUT
);
802 ret
= shm_new(size
+ sizeof(*cr
));
806 ret
= shm_attach(shmid
, ATTACH_RW
, &shm
);
810 cr
->result_size
= size
;
813 memcpy(shm
+ sizeof(*cr
), buf
, size
);
814 ret
= shm_detach(shm
);
817 ret
= write_all(fd
, (char *)&shmid
, sizeof(int));
821 if (shm_destroy(shmid
) < 0)
822 PARA_ERROR_LOG("destroy result failed\n");
826 static int call_callback(int fd
, int query_shmid
)
829 struct callback_query
*cq
;
831 struct afs_callback_arg aca
= {.fd
= fd
};
833 ret
= shm_attach(query_shmid
, ATTACH_RW
, &query_shm
);
837 aca
.query
.data
= (char *)query_shm
+ sizeof(*cq
);
838 aca
.query
.size
= cq
->query_size
;
839 aca
.pbout
.max_size
= shm_get_shmmax();
840 aca
.pbout
.max_size_handler
= afs_max_size_handler
;
841 aca
.pbout
.private_data
= &(struct afs_max_size_handler_data
) {
845 ret
= cq
->handler(&aca
);
846 ret2
= shm_detach(query_shm
);
848 if (ret
< 0) /* ignore (but log) detach error */
849 PARA_ERROR_LOG("could not detach sma: %s\n",
850 para_strerror(-ret2
));
854 flush_and_free_pb(&aca
.pbout
);
856 ret2
= pass_buffer_as_shm(fd
, SBD_AFS_CB_FAILURE
,
857 (const char *)&ret
, sizeof(ret
));
859 PARA_ERROR_LOG("could not pass cb failure packet: %s\n",
860 para_strerror(-ret
));
865 static int execute_server_command(fd_set
*rfds
)
869 int ret
= read_nonblock(server_socket
, buf
, sizeof(buf
) - 1, rfds
, &n
);
871 if (ret
< 0 || n
== 0)
874 if (strcmp(buf
, "new"))
875 return -ERRNO_TO_PARA_ERROR(EINVAL
);
876 return open_next_audio_file();
879 /* returns 0 if no data available, 1 else */
880 static int execute_afs_command(int fd
, fd_set
*rfds
)
884 char buf
[sizeof(cookie
) + sizeof(query_shmid
)];
886 int ret
= read_nonblock(fd
, buf
, sizeof(buf
), rfds
, &n
);
892 if (n
!= sizeof(buf
)) {
893 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
894 ret
, (long unsigned) sizeof(buf
));
897 cookie
= *(uint32_t *)buf
;
898 if (cookie
!= afs_socket_cookie
) {
899 PARA_NOTICE_LOG("received invalid cookie (got %u, expected %u)\n",
900 (unsigned)cookie
, (unsigned)afs_socket_cookie
);
903 query_shmid
= *(int *)(buf
+ sizeof(cookie
));
904 if (query_shmid
< 0) {
905 PARA_WARNING_LOG("received invalid query shmid %d)\n",
909 ret
= call_callback(fd
, query_shmid
);
913 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
917 /** Shutdown connection if query has not arrived until this many seconds. */
918 #define AFS_CLIENT_TIMEOUT 3
920 static int command_post_select(struct sched
*s
, void *context
)
922 struct command_task
*ct
= context
;
923 struct sockaddr_un unix_addr
;
924 struct afs_client
*client
, *tmp
;
927 ret
= task_get_notification(ct
->task
);
930 ret
= execute_server_command(&s
->rfds
);
932 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
933 task_notify_all(s
, -ret
);
936 /* Check the list of connected clients. */
937 list_for_each_entry_safe(client
, tmp
, &afs_client_list
, node
) {
938 ret
= execute_afs_command(client
->fd
, &s
->rfds
);
939 if (ret
== 0) { /* prevent bogus connection flooding */
941 tv_diff(now
, &client
->connect_time
, &diff
);
942 if (diff
.tv_sec
< AFS_CLIENT_TIMEOUT
)
944 PARA_WARNING_LOG("connection timeout\n");
947 list_del(&client
->node
);
950 /* Accept connections on the local socket. */
951 ret
= para_accept(ct
->fd
, &s
->rfds
, &unix_addr
, sizeof(unix_addr
), &fd
);
953 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
956 ret
= mark_fd_nonblocking(fd
);
958 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
962 client
= para_malloc(sizeof(*client
));
964 client
->connect_time
= *now
;
965 para_list_add(&client
->node
, &afs_client_list
);
969 static void register_command_task(struct sched
*s
)
971 struct command_task
*ct
= &command_task_struct
;
972 ct
->fd
= setup_command_socket_or_die();
974 ct
->task
= task_register(&(struct task_info
) {
975 .name
= "afs command",
976 .pre_select
= command_pre_select
,
977 .post_select
= command_post_select
,
983 * Initialize the audio file selector process.
985 * \param socket_fd File descriptor used for communication with the server.
987 __noreturn
void afs_init(int socket_fd
)
989 static struct sched s
;
992 register_signal_task(&s
);
993 init_list_head(&afs_client_list
);
994 for (i
= 0; i
< NUM_AFS_TABLES
; i
++)
995 afs_tables
[i
].init(&afs_tables
[i
]);
996 ret
= open_afs_tables();
999 server_socket
= socket_fd
;
1000 ret
= mark_fd_nonblocking(server_socket
);
1003 PARA_INFO_LOG("server_socket: %d\n", server_socket
);
1004 init_admissible_files(OPT_STRING_VAL(AFS_INITIAL_MODE
));
1005 register_command_task(&s
);
1006 s
.default_timeout
.tv_sec
= 0;
1007 s
.default_timeout
.tv_usec
= 999 * 1000;
1008 ret
= write(socket_fd
, "\0", 1);
1012 ret
= -ERRNO_TO_PARA_ERROR(errno
);
1017 close_current_mood();
1021 signal_shutdown(signal_task
);
1022 free_status_items();
1026 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
1030 static int com_init_callback(struct afs_callback_arg
*aca
)
1032 uint32_t table_mask
= *(uint32_t *)aca
->query
.data
;
1037 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
1038 struct afs_table
*t
= &afs_tables
[i
];
1040 if (!(table_mask
& (1 << i
)))
1044 ret
= t
->create(database_dir
);
1046 para_printf(&aca
->pbout
, "cannot create table %s\n",
1050 para_printf(&aca
->pbout
, "successfully created %s table\n",
1053 ret
= open_afs_tables();
1055 para_printf(&aca
->pbout
, "cannot open afs tables: %s\n",
1056 para_strerror(-ret
));
1061 static int com_init(struct command_context
*cc
, struct lls_parse_result
*lpr
)
1064 uint32_t table_mask
= (1 << (NUM_AFS_TABLES
+ 1)) - 1;
1065 struct osl_object query
= {.data
= &table_mask
,
1066 .size
= sizeof(table_mask
)};
1067 unsigned num_inputs
= lls_num_inputs(lpr
);
1069 ret
= make_database_dir();
1072 if (num_inputs
> 0) {
1074 for (i
= 0; i
< num_inputs
; i
++) {
1075 for (j
= 0; j
< NUM_AFS_TABLES
; j
++) {
1076 struct afs_table
*t
= &afs_tables
[j
];
1078 if (strcmp(lls_input(i
, lpr
), t
->name
))
1080 table_mask
|= (1 << j
);
1083 if (j
== NUM_AFS_TABLES
)
1084 return -E_BAD_TABLE_NAME
;
1087 return send_callback_request(com_init_callback
, &query
,
1088 afs_cb_result_handler
, cc
);
1090 EXPORT_SERVER_CMD_HANDLER(init
);
1092 static int com_check(struct command_context
*cc
, struct lls_parse_result
*lpr
)
1094 const struct lls_opt_result
*r_a
= SERVER_CMD_OPT_RESULT(CHECK
, AFT
, lpr
);
1095 const struct lls_opt_result
*r_A
= SERVER_CMD_OPT_RESULT(CHECK
, ATTRIBUTE
, lpr
);
1096 const struct lls_opt_result
*r_m
= SERVER_CMD_OPT_RESULT(CHECK
, MOOD
, lpr
);
1097 const struct lls_opt_result
*r_p
= SERVER_CMD_OPT_RESULT(CHECK
, PLAYLIST
, lpr
);
1098 bool noopt
= !lls_opt_given(r_a
) && !lls_opt_given(r_A
)
1099 && !lls_opt_given(r_m
) && !lls_opt_given(r_p
);
1102 if (noopt
|| lls_opt_given(r_a
)) {
1103 ret
= send_callback_request(aft_check_callback
, NULL
,
1104 afs_cb_result_handler
, cc
);
1108 if (noopt
|| lls_opt_given(r_A
)) {
1109 ret
= send_callback_request(attribute_check_callback
, NULL
,
1110 afs_cb_result_handler
, cc
);
1114 if (noopt
|| lls_opt_given(r_p
)) {
1115 ret
= send_callback_request(playlist_check_callback
,
1116 NULL
, afs_cb_result_handler
, cc
);
1120 if (noopt
|| lls_opt_given(r_m
)) {
1121 ret
= send_callback_request(mood_check_callback
, NULL
,
1122 afs_cb_result_handler
, cc
);
1128 EXPORT_SERVER_CMD_HANDLER(check
);
1131 * The afs event dispatcher.
1133 * \param event Type of the event.
1134 * \param pb May be \p NULL.
1135 * \param data Type depends on \a event.
1137 * This function calls each table event handler, passing \a pb and \a data
1138 * verbatim. It's up to the handlers to interpret the \a data pointer. If a
1139 * handler returns negative, the loop is aborted.
1141 * \return The (negative) error code of the first handler that failed, or non-negative
1142 * if all handlers succeeded.
1144 __must_check
int afs_event(enum afs_events event
, struct para_buffer
*pb
,
1149 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
1150 struct afs_table
*t
= &afs_tables
[i
];
1151 if (!t
->event_handler
)
1153 ret
= t
->event_handler(event
, pb
, data
);
1155 PARA_CRIT_LOG("table %s, event %u: %s\n", t
->name
,
1156 event
, para_strerror(-ret
));
1164 * Dummy event handler for the images table.
1166 * \param event Unused.
1168 * \param data Unused.
1170 * \return The images table does not honor events, so this handler always
1173 __a_const
int images_event_handler(__a_unused
enum afs_events event
,
1174 __a_unused
struct para_buffer
*pb
, __a_unused
void *data
)
1180 * Dummy event handler for the lyrics table.
1182 * \param event Unused.
1184 * \param data Unused.
1186 * \return The lyrics table does not honor events, so this handler always
1189 __a_const
int lyrics_event_handler(__a_unused
enum afs_events event
,
1190 __a_unused
struct para_buffer
*pb
, __a_unused
void *data
)