2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afs.c Paraslash's audio file selector. */
11 #include "server.cmdline.h"
18 #include <dirent.h> /* readdir() */
28 /** The osl tables used by afs. \sa blob.c. */
30 /** Contains audio file information. See aft.c. */
32 /** The table for the paraslash attributes. See attribute.c. */
35 * Paraslash's scoring system is based on Gaussian normal
36 * distributions, and the relevant data is stored in the rbtrees of an
37 * osl table containing only volatile columns. See score.c for
42 * A standard blob table containing the mood definitions. For details
46 /** A blob table containing lyrics on a per-song basis. */
48 /** Another blob table for images (for example album cover art). */
50 /** Yet another blob table for storing standard playlists. */
52 /** How many tables are in use? */
56 static struct afs_table afs_tables
[NUM_AFS_TABLES
] = {
57 [TBLNUM_AUDIO_FILES
] = {.init
= aft_init
},
58 [TBLNUM_ATTRIBUTES
] = {.init
= attribute_init
},
59 [TBLNUM_SCORES
] = {.init
= score_init
},
60 [TBLNUM_MOODS
] = {.init
= moods_init
},
61 [TBLNUM_LYRICS
] = {.init
= lyrics_init
},
62 [TBLNUM_IMAGES
] = {.init
= images_init
},
63 [TBLNUM_PLAYLIST
] = {.init
= playlists_init
},
67 /** The file descriptor for the local socket. */
70 * Value sent by the command handlers to identify themselves as
71 * children of the running para_server.
74 /** The associated task structure. */
78 static int server_socket
;
79 static struct command_task command_task_struct
;
80 static struct signal_task signal_task_struct
;
84 * A random number used to "authenticate" the connection.
86 * para_server picks this number by random before forking the afs process. The
87 * command handlers write this number together with the id of the shared memory
88 * area containing the query. This way, a malicious local user has to know this
89 * number to be able to cause the afs process to crash by sending fake queries.
91 extern uint32_t afs_socket_cookie
;
94 * Struct to let command handlers execute a callback in afs context.
96 * Commands that need to change the state of afs can't change the relevant data
97 * structures directly because commands are executed in a child process, i.e.
98 * they get their own virtual address space.
100 * This structure is used by \p send_callback_request() (executed from handler
101 * context) in order to let the afs process call the specified function. An
102 * instance of that structure is written to a shared memory area together with
103 * the arguments to the callback function. The identifier of the shared memory
104 * area is written to the command socket.
106 * The afs process accepts connections on the command socket and reads the
107 * shared memory id, attaches the corresponing area, calls the given handler to
108 * perform the desired action and to optionally compute a result.
110 * The result and a \p callback_result structure is then written to another
111 * shared memory area. The identifier for that area is written to the handler's
112 * command socket, so that the handler process can read the id, attach the
113 * shared memory area and use the result.
115 * \sa struct callback_result.
117 struct callback_query
{
118 /** The function to be called. */
119 callback_function
*handler
;
120 /** The number of bytes of the query */
125 * Structure embedded in the result of a callback.
127 * If the callback produced a result, an instance of that structure is embeeded
128 * into the shared memory area holding the result, mainly to let the command
129 * handler know the size of the result.
131 * \sa struct callback_query.
133 struct callback_result
{
134 /** The number of bytes of the result. */
139 * Ask the afs process to call a given function.
141 * \param f The function to be called.
142 * \param query Pointer to arbitrary data for the callback.
143 * \param result Callback result will be stored here.
145 * This function creates a shared memory area, copies the buffer pointed to by
146 * \a buf to that area and notifies the afs process that \a f should be
149 * \return Negative, on errors, the return value of the callback function
152 * \sa send_option_arg_callback_request(), send_standard_callback_request().
154 int send_callback_request(callback_function
*f
, struct osl_object
*query
,
155 struct osl_object
*result
)
157 struct callback_query
*cq
;
158 struct callback_result
*cr
;
159 int ret
, fd
= -1, query_shmid
, result_shmid
;
160 void *query_shm
, *result_shm
;
161 char buf
[sizeof(afs_socket_cookie
) + sizeof(int)];
162 struct sockaddr_un unix_addr
;
163 size_t query_shm_size
= sizeof(*cq
);
166 query_shm_size
+= query
->size
;
167 ret
= shm_new(query_shm_size
);
171 ret
= shm_attach(query_shmid
, ATTACH_RW
, &query_shm
);
176 cq
->query_size
= query_shm_size
- sizeof(*cq
);
179 memcpy(query_shm
+ sizeof(*cq
), query
->data
, query
->size
);
180 ret
= shm_detach(query_shm
);
184 *(uint32_t *) buf
= afs_socket_cookie
;
185 *(int *) (buf
+ sizeof(afs_socket_cookie
)) = query_shmid
;
187 ret
= get_stream_socket(PF_UNIX
);
191 ret
= init_unix_addr(&unix_addr
, conf
.afs_socket_arg
);
194 ret
= PARA_CONNECT(fd
, &unix_addr
);
197 ret
= send_bin_buffer(fd
, buf
, sizeof(buf
));
200 ret
= recv_bin_buffer(fd
, buf
, sizeof(buf
));
203 if (ret
!= sizeof(int)) {
211 ret
= shm_attach(result_shmid
, ATTACH_RO
, &result_shm
);
215 result
->size
= cr
->result_size
;
216 result
->data
= para_malloc(result
->size
);
217 memcpy(result
->data
, result_shm
+ sizeof(*cr
), result
->size
);
218 ret
= shm_detach(result_shm
);
220 PARA_ERROR_LOG("can not detach result\n");
222 PARA_ERROR_LOG("attach result failed: %d\n", ret
);
223 if (shm_destroy(result_shmid
) < 0)
224 PARA_ERROR_LOG("destroy result failed\n");
227 if (shm_destroy(query_shmid
) < 0)
228 PARA_ERROR_LOG("%s\n", "shm destroy error");
231 // PARA_DEBUG_LOG("callback_ret: %d\n", ret);
236 * Send a callback request passing an options structure and an argument vector.
238 * \param options pointer to an arbitrary data structure.
239 * \param argc Argument count.
240 * \param argv Standard argument vector.
241 * \param f The callback function.
242 * \param result The result of the query is stored here.
244 * Some commands have a couple of options that are parsed in child context for
245 * syntactic correctness and are stored in a special options structure for that
246 * command. This function allows to pass such a structure together with a list
247 * of further arguments (often a list of audio files) to the parent process.
249 * \sa send_standard_callback_request(), send_callback_request().
251 int send_option_arg_callback_request(struct osl_object
*options
,
252 int argc
, char * const * const argv
, callback_function
*f
,
253 struct osl_object
*result
)
257 struct osl_object query
= {.size
= options
? options
->size
: 0};
259 for (i
= 0; i
< argc
; i
++)
260 query
.size
+= strlen(argv
[i
]) + 1;
261 query
.data
= para_malloc(query
.size
);
264 memcpy(query
.data
, options
->data
, options
->size
);
267 for (i
= 0; i
< argc
; i
++) {
268 strcpy(p
, argv
[i
]); /* OK */
269 p
+= strlen(argv
[i
]) + 1;
271 ret
= send_callback_request(f
, &query
, result
);
277 * Send a callback request with an argument vector only.
279 * \param argc The same meaning as in send_option_arg_callback_request().
280 * \param argv The same meaning as in send_option_arg_callback_request().
281 * \param f The same meaning as in send_option_arg_callback_request().
282 * \param result The same meaning as in send_option_arg_callback_request().
284 * This is similar to send_option_arg_callback_request(), but no options buffer
285 * is passed to the parent process.
287 * \return The return value of the underlying call to
288 * send_option_arg_callback_request().
290 int send_standard_callback_request(int argc
, char * const * const argv
,
291 callback_function
*f
, struct osl_object
*result
)
293 return send_option_arg_callback_request(NULL
, argc
, argv
, f
, result
);
296 static int action_if_pattern_matches(struct osl_row
*row
, void *data
)
298 struct pattern_match_data
*pmd
= data
;
299 struct osl_object name_obj
;
300 const char *p
, *name
;
301 int ret
= osl_get_object(pmd
->table
, row
, pmd
->match_col_num
, &name_obj
);
302 const char *pattern_txt
= (const char *)pmd
->patterns
.data
;
306 name
= (char *)name_obj
.data
;
307 if ((!name
|| !*name
) && (pmd
->pm_flags
& PM_SKIP_EMPTY_NAME
))
309 if (!pmd
->patterns
.size
&& (pmd
->pm_flags
& PM_NO_PATTERN_MATCHES_EVERYTHING
))
310 return pmd
->action(pmd
->table
, row
, name
, pmd
->data
);
311 for (p
= pattern_txt
; p
< pattern_txt
+ pmd
->patterns
.size
;
312 p
+= strlen(p
) + 1) {
313 ret
= fnmatch(p
, name
, pmd
->fnmatch_flags
);
314 if (ret
== FNM_NOMATCH
)
318 return pmd
->action(pmd
->table
, row
, name
, pmd
->data
);
324 * Execute the given function for each matching row.
326 * \param pmd Describes what to match and how.
328 * \return The return value of the underlying call to osl_rbtree_loop()
329 * or osl_rbtree_loop_reverse().
331 int for_each_matching_row(struct pattern_match_data
*pmd
)
333 if (pmd
->pm_flags
& PM_REVERSE_LOOP
)
334 return osl_rbtree_loop_reverse(pmd
->table
, pmd
->loop_col_num
, pmd
,
335 action_if_pattern_matches
);
336 return osl_rbtree_loop(pmd
->table
, pmd
->loop_col_num
, pmd
,
337 action_if_pattern_matches
);
341 * Compare two osl objects of string type.
343 * \param obj1 Pointer to the first object.
344 * \param obj2 Pointer to the second object.
346 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
347 * are taken into account.
349 * \return It returns an integer less than, equal to, or greater than zero if
350 * \a obj1 is found, respectively, to be less than, to match, or be greater than
353 * \sa strcmp(3), strncmp(3), osl_compare_func.
355 int string_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
357 const char *str1
= (const char *)obj1
->data
;
358 const char *str2
= (const char *)obj2
->data
;
359 return strncmp(str1
, str2
, PARA_MIN(obj1
->size
, obj2
->size
));
363 * write input from fd to dynamically allocated buffer,
364 * but maximal max_size byte.
366 static int fd2buf(int fd
, unsigned max_size
, struct osl_object
*obj
)
368 const size_t chunk_size
= 1024;
369 size_t size
= 2048, received
= 0;
371 char *buf
= para_malloc(size
);
374 ret
= recv_bin_buffer(fd
, buf
+ received
, chunk_size
);
378 if (received
+ chunk_size
>= size
) {
380 ret
= -E_INPUT_TOO_LARGE
;
383 buf
= para_realloc(buf
, size
);
387 obj
->size
= received
;
394 * Read data from a file descriptor, and send it to the afs process.
396 * \param fd File descriptor to read data from.
397 * \param arg_obj Pointer to the arguments to \a f.
398 * \param f The callback function.
399 * \param max_len Don't read more than that many bytes from stdin.
400 * \param result The result of the query is stored here.
402 * This function is used by commands that wish to let para_server store
403 * arbitrary data specified by the user (for instance the add_blob family of
404 * commands). First, at most \a max_len bytes are read from \a fd, the result
405 * is concatenated with the buffer given by \a arg_obj, and the combined buffer
406 * is made available to the parent process via shared memory.
408 * \return Negative on errors, the return value of the underlying call to
409 * send_callback_request() otherwise.
411 int stdin_command(int fd
, struct osl_object
*arg_obj
, callback_function
*f
,
412 unsigned max_len
, struct osl_object
*result
)
414 struct osl_object query
, stdin_obj
;
417 ret
= send_buffer(fd
, AWAITING_DATA_MSG
);
420 ret
= fd2buf(fd
, max_len
, &stdin_obj
);
423 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
424 query
.data
= para_malloc(query
.size
);
425 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
426 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
427 free(stdin_obj
.data
);
428 ret
= send_callback_request(f
, &query
, result
);
433 int pass_afd(int fd
, char *buf
, size_t size
)
435 struct msghdr msg
= {.msg_iov
= NULL
};
436 struct cmsghdr
*cmsg
;
447 msg
.msg_control
= control
;
448 msg
.msg_controllen
= sizeof(control
);
450 cmsg
= CMSG_FIRSTHDR(&msg
);
451 cmsg
->cmsg_level
= SOL_SOCKET
;
452 cmsg
->cmsg_type
= SCM_RIGHTS
;
453 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
454 *(int *)CMSG_DATA(cmsg
) = fd
;
456 /* Sum of the length of all control messages in the buffer */
457 msg
.msg_controllen
= cmsg
->cmsg_len
;
458 PARA_NOTICE_LOG("passing %zu bytes and fd %d\n", size
, fd
);
459 ret
= sendmsg(server_socket
, &msg
, 0);
461 ret
= -ERRNO_TO_PARA_ERROR(errno
);
468 * Open the audio file with highest score.
470 * \param afd Audio file data is returned here.
472 * This stores all information for streaming the "best" audio file
473 * in the \a afd structure.
475 * \return Positive on success, negative on errors.
477 * \sa close_audio_file(), open_and_update_audio_file().
479 int open_next_audio_file(void)
481 struct osl_row
*aft_row
;
482 struct audio_file_data afd
;
486 PARA_NOTICE_LOG("getting next af\n");
487 ret
= score_get_best(&aft_row
, &afd
.score
);
490 ret
= open_and_update_audio_file(aft_row
, &afd
);
494 PARA_NOTICE_LOG("shmid: %u\n", shmid
);
495 if (!write_ok(server_socket
)) {
496 PARA_EMERG_LOG("afs_socket not writable\n");
499 *(uint32_t *)buf
= NEXT_AUDIO_FILE
;
500 *(uint32_t *)(buf
+ 4) = (uint32_t)shmid
;
501 ret
= pass_afd(afd
.fd
, buf
, 8);
504 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
510 static enum play_mode
init_admissible_files(void)
513 char *arg
= conf
.afs_initial_mode_arg
;
515 if (conf
.afs_initial_mode_given
) {
516 if (!strncmp(arg
, "p:", 2)) {
517 ret
= playlist_open(arg
+ 2);
519 return PLAY_MODE_PLAYLIST
;
522 if (!strncmp(arg
, "m:", 2)) {
523 ret
= change_current_mood(arg
+ 2);
525 return PLAY_MODE_MOOD
;
528 PARA_ERROR_LOG("bad afs initial mode arg: %s\n", arg
);
532 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
533 PARA_NOTICE_LOG("defaulting to dummy mood\n");
534 change_current_mood(""); /* always successful */
535 return PLAY_MODE_MOOD
;
538 static int setup_command_socket_or_die(void)
541 char *socket_name
= conf
.afs_socket_arg
;
542 struct sockaddr_un unix_addr
;
545 ret
= create_local_socket(socket_name
, &unix_addr
,
546 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
548 PARA_EMERG_LOG("%s: %s\n", PARA_STRERROR(-ret
), socket_name
);
551 if (listen(ret
, 5) < 0) {
552 PARA_EMERG_LOG("%s", "can not listen on socket\n");
555 PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name
,
560 static void close_afs_tables(void)
563 PARA_NOTICE_LOG("closing afs_tables\n");
564 for (i
= 0; i
< NUM_AFS_TABLES
; i
++)
565 afs_tables
[i
].close();
568 static char *database_dir
;
570 static void get_database_dir(void)
573 if (conf
.afs_database_dir_given
)
574 database_dir
= para_strdup(conf
.afs_database_dir_arg
);
576 char *home
= para_homedir();
577 database_dir
= make_message(
578 "%s/.paraslash/afs_database", home
);
582 PARA_INFO_LOG("afs_database dir %s\n", database_dir
);
585 static int make_database_dir(void)
590 ret
= para_mkdir(database_dir
, 0777);
591 if (ret
>= 0 || is_errno(-ret
, EEXIST
))
596 static int open_afs_tables(void)
601 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
602 ret
= afs_tables
[i
].open(database_dir
);
605 PARA_ERROR_LOG("%s init: %s\n", afs_tables
[i
].name
,
606 PARA_STRERROR(-ret
));
611 afs_tables
[i
].close();
616 static void unregister_tasks(void)
618 unregister_task(&command_task_struct
.task
);
619 unregister_task(&signal_task_struct
.task
);
622 static void signal_pre_select(struct sched
*s
, struct task
*t
)
624 struct signal_task
*st
= t
->private_data
;
626 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
629 static void signal_post_select(struct sched
*s
, struct task
*t
)
631 struct signal_task
*st
= t
->private_data
;
633 if (!FD_ISSET(st
->fd
, &s
->rfds
))
635 st
->signum
= para_next_signal();
637 if (st
->signum
== SIGUSR1
)
638 return; /* ignore SIGUSR1 */
639 if (st
->signum
== SIGHUP
) {
641 t
->ret
= open_afs_tables();
644 PARA_NOTICE_LOG("caught signal %d\n", st
->signum
);
645 t
->ret
= -E_AFS_SIGNAL
;
649 static void register_signal_task(void)
651 struct signal_task
*st
= &signal_task_struct
;
652 st
->fd
= para_signal_init();
653 PARA_INFO_LOG("signal pipe: fd %d\n", st
->fd
);
654 para_install_sighandler(SIGINT
);
655 para_install_sighandler(SIGTERM
);
656 para_install_sighandler(SIGPIPE
);
657 para_install_sighandler(SIGHUP
);
659 st
->task
.pre_select
= signal_pre_select
;
660 st
->task
.post_select
= signal_post_select
;
661 st
->task
.private_data
= st
;
662 sprintf(st
->task
.status
, "signal task");
663 register_task(&st
->task
);
666 static struct list_head afs_client_list
;
669 struct list_head node
;
671 struct timeval connect_time
;
674 static void command_pre_select(struct sched
*s
, struct task
*t
)
676 struct command_task
*ct
= t
->private_data
;
677 struct afs_client
*client
;
679 para_fd_set(server_socket
, &s
->rfds
, &s
->max_fileno
);
680 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
681 list_for_each_entry(client
, &afs_client_list
, node
)
682 para_fd_set(client
->fd
, &s
->rfds
, &s
->max_fileno
);
687 * On errors, negative value is written to fd.
688 * On success: If query produced a result, the result_shmid is written to fd.
689 * Otherwise, zero is written.
691 static int call_callback(int fd
, int query_shmid
)
693 void *query_shm
, *result_shm
;
694 struct callback_query
*cq
;
695 struct callback_result
*cr
;
696 struct osl_object query
, result
= {.data
= NULL
};
697 int result_shmid
= -1, ret
, ret2
;
699 ret
= shm_attach(query_shmid
, ATTACH_RW
, &query_shm
);
703 query
.data
= (char *)query_shm
+ sizeof(*cq
);
704 query
.size
= cq
->query_size
;
705 ret
= cq
->handler(&query
, &result
);
706 ret2
= shm_detach(query_shm
);
707 if (ret2
< 0 && ret
>= 0)
712 if (!result
.data
|| !result
.size
)
714 ret
= shm_new(result
.size
+ sizeof(struct callback_result
));
718 ret
= shm_attach(result_shmid
, ATTACH_RW
, &result_shm
);
722 cr
->result_size
= result
.size
;
723 memcpy(result_shm
+ sizeof(*cr
), result
.data
, result
.size
);
724 ret
= shm_detach(result_shm
);
730 ret2
= send_bin_buffer(fd
, (char *)&ret
, sizeof(int));
731 if (ret
< 0 || ret2
< 0) {
732 if (result_shmid
>= 0)
733 if (shm_destroy(result_shmid
) < 0)
734 PARA_ERROR_LOG("destroy result failed\n");
741 static void execute_server_command(void)
744 int ret
= recv_bin_buffer(server_socket
, buf
, sizeof(buf
) - 1);
748 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
752 PARA_NOTICE_LOG("received: %s\n", buf
);
753 if (!strcmp(buf
, "new")) {
754 ret
= open_next_audio_file();
755 PARA_NOTICE_LOG("ret: %d\n", ret
);
758 PARA_ERROR_LOG("unknown command\n");
762 static void execute_afs_command(int fd
, uint32_t expected_cookie
)
766 char buf
[sizeof(cookie
) + sizeof(query_shmid
)];
767 int ret
= recv_bin_buffer(fd
, buf
, sizeof(buf
));
770 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret
));
773 if (ret
!= sizeof(buf
)) {
774 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
775 ret
, (long unsigned) sizeof(buf
));
778 cookie
= *(uint32_t *)buf
;
779 if (cookie
!= expected_cookie
) {
780 PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
781 (unsigned)cookie
, (unsigned)expected_cookie
);
784 query_shmid
= *(int *)(buf
+ sizeof(cookie
));
785 if (query_shmid
< 0) {
786 PARA_WARNING_LOG("received invalid query shmid %d)\n",
790 /* Ignore return value: Errors might be OK here. */
791 call_callback(fd
, query_shmid
);
794 /** Shutdown connection if query has not arrived until this many seconds. */
795 #define AFS_CLIENT_TIMEOUT 3
797 static void command_post_select(struct sched
*s
, struct task
*t
)
799 struct command_task
*ct
= t
->private_data
;
800 struct sockaddr_un unix_addr
;
801 struct afs_client
*client
, *tmp
;
803 if (FD_ISSET(server_socket
, &s
->rfds
))
804 execute_server_command();
806 /* Check the list of connected clients. */
807 list_for_each_entry_safe(client
, tmp
, &afs_client_list
, node
) {
808 if (FD_ISSET(client
->fd
, &s
->rfds
))
809 execute_afs_command(client
->fd
, ct
->cookie
);
810 else { /* prevent bogus connection flooding */
812 tv_diff(now
, &client
->connect_time
, &diff
);
813 if (diff
.tv_sec
< AFS_CLIENT_TIMEOUT
)
815 PARA_WARNING_LOG("connection timeout\n");
818 list_del(&client
->node
);
821 /* Accept connections on the local socket. */
822 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
824 t
->ret
= para_accept(ct
->fd
, &unix_addr
, sizeof(unix_addr
));
826 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
829 client
= para_malloc(sizeof(*client
));
831 client
->connect_time
= *now
;
832 para_list_add(&client
->node
, &afs_client_list
);
837 static void register_command_task(uint32_t cookie
)
839 struct command_task
*ct
= &command_task_struct
;
840 ct
->fd
= setup_command_socket_or_die();
843 ct
->task
.pre_select
= command_pre_select
;
844 ct
->task
.post_select
= command_post_select
;
845 ct
->task
.private_data
= ct
;
846 sprintf(ct
->task
.status
, "command task");
847 register_task(&ct
->task
);
850 static void register_tasks(uint32_t cookie
)
852 register_signal_task();
853 register_command_task(cookie
);
857 * Initialize the audio file selector process.
859 * \param cookie The value used for "authentication".
860 * \param socket_fd File descriptor used for communication with the server.
862 __noreturn
void afs_init(uint32_t cookie
, int socket_fd
)
864 enum play_mode current_play_mode
;
868 INIT_LIST_HEAD(&afs_client_list
);
869 for (i
= 0; i
< NUM_AFS_TABLES
; i
++)
870 afs_tables
[i
].init(&afs_tables
[i
]);
871 ret
= open_afs_tables();
874 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
877 server_socket
= socket_fd
;
878 ret
= mark_fd_nonblock(server_socket
);
881 PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
882 server_socket
, (unsigned) cookie
);
883 current_play_mode
= init_admissible_files();
884 register_tasks(cookie
);
885 s
.default_timeout
.tv_sec
= 0;
886 s
.default_timeout
.tv_usec
= 99 * 1000;
889 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
894 static int create_tables_callback(const struct osl_object
*query
,
895 __a_unused
struct osl_object
*result
)
897 uint32_t table_mask
= *(uint32_t *)query
->data
;
901 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
902 struct afs_table
*t
= &afs_tables
[i
];
904 if (!(table_mask
& (1 << i
)))
908 ret
= t
->create(database_dir
);
912 ret
= open_afs_tables();
913 return ret
< 0? ret
: 0;
916 int com_init(int fd
, int argc
, char * const * const argv
)
919 uint32_t table_mask
= (1 << (NUM_AFS_TABLES
+ 1)) - 1;
920 struct osl_object query
= {.data
= &table_mask
,
921 .size
= sizeof(table_mask
)};
923 ret
= make_database_dir();
928 for (i
= 1; i
< argc
; i
++) {
929 for (j
= 0; j
< NUM_AFS_TABLES
; j
++) {
930 struct afs_table
*t
= &afs_tables
[j
];
932 if (strcmp(argv
[i
], t
->name
))
934 table_mask
|= (1 << j
);
937 if (j
== NUM_AFS_TABLES
)
938 return -E_BAD_TABLE_NAME
;
941 ret
= send_callback_request(create_tables_callback
, &query
, NULL
);
944 return send_va_buffer(fd
, "successfully created afs table(s)\n");
948 * Flags for the check command.
952 enum com_check_flags
{
953 /** Check the audio file table. */
955 /** Check the mood table. */
957 /** Check the playlist table. */
961 int com_check(int fd
, int argc
, char * const * const argv
)
965 struct osl_object result
;
967 for (i
= 1; i
< argc
; i
++) {
968 const char *arg
= argv
[i
];
971 if (!strcmp(arg
, "--")) {
975 if (!strcmp(arg
, "-a")) {
979 if (!strcmp(arg
, "-p")) {
980 flags
|= CHECK_PLAYLISTS
;
983 if (!strcmp(arg
, "-m")) {
984 flags
|= CHECK_MOODS
;
987 return -E_AFS_SYNTAX
;
990 return -E_AFS_SYNTAX
;
993 if (flags
& CHECK_AFT
) {
994 ret
= send_callback_request(aft_check_callback
, NULL
, &result
);
998 ret
= send_buffer(fd
, (char *) result
.data
);
1004 if (flags
& CHECK_PLAYLISTS
) {
1005 ret
= send_callback_request(playlist_check_callback
, NULL
, &result
);
1009 ret
= send_buffer(fd
, (char *) result
.data
);
1015 if (flags
& CHECK_MOODS
) {
1016 ret
= send_callback_request(mood_check_callback
, NULL
, &result
);
1020 ret
= send_buffer(fd
, (char *) result
.data
);
1029 void afs_event(enum afs_events event
, struct para_buffer
*pb
,
1034 for (i
= 0; i
< NUM_AFS_TABLES
; i
++) {
1035 struct afs_table
*t
= &afs_tables
[i
];
1036 if (!t
->event_handler
)
1038 ret
= t
->event_handler(event
, pb
, data
);
1040 PARA_CRIT_LOG("%s\n", PARA_STRERROR(-ret
));
1044 int images_event_handler(__a_unused
enum afs_events event
,
1045 __a_unused
struct para_buffer
*pb
, __a_unused
void *data
)
1050 int lyrics_event_handler(__a_unused
enum afs_events event
,
1051 __a_unused
struct para_buffer
*pb
, __a_unused
void *data
)