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 static struct afs_table afs_tables[] = {
37 {.init = aft_init, .name = "audio_files"},
38 {.init = attribute_init, .name = "attributes"},
39 {.init = score_init, .name = "scores"},
40 {.init = moods_init, .name = "moods"},
41 {.init = lyrics_init, .name = "lyrics"},
42 {.init = images_init, .name = "images"},
43 {.init = playlists_init, .name = "playlists"},
46 #define NUM_AFS_TABLES ARRAY_SIZE(afs_tables)
49 /** The file descriptor for the local socket. */
51 /** The associated task structure. */
56 extern struct misc_meta_data *mmd;
58 static int server_socket;
59 static struct command_task command_task_struct;
60 static struct signal_task *signal_task;
62 static enum play_mode current_play_mode;
63 static char *current_mop; /* mode or playlist specifier. NULL means dummy mood */
65 extern uint32_t afs_socket_cookie;
68 * Passed from command handlers to afs.
70 * Command handlers cannot change the afs database directly because they run in
71 * a separate process. The callback query structure circumvents this
72 * restriction as follows. To instruct the afs process to execute a particular
73 * function, the command hander writes an instance of this structure to a
74 * shared memory area, along with the arguments to the callback function. The
75 * identifier of the shared memory area is transferred to the afs process via
78 * The afs process reads the shared memory id from the command socket, attaches
79 * the corresponding area, and calls the callback function whose address is
82 * The command output, if any, is transferred back to the command handler in
83 * the same way: The afs process writes the output to a second shared memory
84 * area together with a fixed size metadata header whose format corresponds to
85 * the \ref callback_result structure. The identifier of this area is sent back
86 * to the command handler which attaches the area and forwards the output to
89 * \sa \ref struct callback_result.
91 struct callback_query {
92 /** The function to be called. */
93 afs_callback *handler;
94 /** The number of bytes of the query */
99 * Structure embedded in the result of a callback.
101 * If the callback produced a result, an instance of that structure is embedded
102 * into the shared memory area holding the result, mainly to let the command
103 * handler know the size of the result.
105 * \sa \ref struct callback_query.
107 struct callback_result {
108 /** The number of bytes of the result. */
110 /** The band designator (loglevel for the result). */
114 static int dispatch_result(int result_shmid, callback_result_handler *handler,
115 void *private_result_data)
117 struct osl_object result;
119 /* must attach r/w as result.data might get encrypted in-place. */
120 int ret2, ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
121 struct callback_result *cr = result_shm;
124 PARA_ERROR_LOG("attach failed: %s\n", para_strerror(-ret));
127 result.size = cr->result_size;
128 result.data = result_shm + sizeof(*cr);
130 ret = handler(&result, cr->band, private_result_data);
131 ret2 = shm_detach(result_shm);
133 PARA_ERROR_LOG("detach failed: %s\n", para_strerror(-ret2));
141 * Ask the afs process to call a given function.
143 * \param f The function to be called.
144 * \param query Pointer to arbitrary data for the callback.
145 * \param result_handler Called for each shm area sent by the callback.
146 * \param private_result_data Passed verbatim to \a result_handler.
148 * This function creates a socket for communication with the afs process and a
149 * shared memory area (sma) to which the buffer pointed to by \a query is
150 * copied. It then notifies the afs process that the callback function \a f
151 * should be executed by sending the shared memory identifier (shmid) to the
154 * If the callback produces a result, it sends any number of shared memory
155 * identifiers back via the socket. For each such identifier received, \a
156 * result_handler is called. The contents of the sma identified by the received
157 * shmid are passed to that function as an osl object. The private_result_data
158 * pointer is passed as the second argument to \a result_handler.
160 * \return Number of shared memory areas dispatched on success, negative on
163 int send_callback_request(afs_callback *f, struct osl_object *query,
164 callback_result_handler *result_handler,
165 void *private_result_data)
167 struct callback_query *cq;
168 int ret, fd = -1, query_shmid, result_shmid;
170 char buf[sizeof(afs_socket_cookie) + sizeof(int)];
171 size_t query_shm_size = sizeof(*cq);
172 int dispatch_error = 0, num_dispatched = 0;
175 query_shm_size += query->size;
176 ret = shm_new(query_shm_size);
180 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
185 cq->query_size = query_shm_size - sizeof(*cq);
188 memcpy(query_shm + sizeof(*cq), query->data, query->size);
189 ret = shm_detach(query_shm);
193 *(uint32_t *)buf = afs_socket_cookie;
194 *(int *)(buf + sizeof(afs_socket_cookie)) = query_shmid;
196 ret = connect_local_socket(OPT_STRING_VAL(AFS_SOCKET));
200 ret = write_all(fd, buf, sizeof(buf));
204 * Read all shmids from afs.
206 * Even if the dispatcher returns an error we _must_ continue to read
207 * shmids from fd so that we can destroy all shared memory areas that
208 * have been created for us by the afs process.
211 ret = recv_bin_buffer(fd, buf, sizeof(int));
214 assert(ret == sizeof(int));
218 ret = dispatch_result(result_shmid, result_handler,
219 private_result_data);
220 if (ret < 0 && dispatch_error >= 0)
221 dispatch_error = ret;
222 ret = shm_destroy(result_shmid);
224 PARA_CRIT_LOG("destroy result failed: %s\n",
225 para_strerror(-ret));
229 if (shm_destroy(query_shmid) < 0)
230 PARA_CRIT_LOG("shm destroy error\n");
233 if (dispatch_error < 0)
234 return dispatch_error;
237 return num_dispatched;
241 * Wrapper for send_callback_request() which passes a lopsub parse result.
243 * \param f The callback function.
244 * \param cmd Needed for (de-)serialization.
245 * \param lpr Must match cmd.
246 * \param private_result_data Passed to send_callback_request().
248 * This function serializes the parse result given by the lpr pointer into a
249 * buffer. The buffer is sent as the query to the afs process with the callback
252 * \return The return value of the underlying call to send_callback_request().
254 int send_lls_callback_request(afs_callback *f,
255 const struct lls_command * const cmd,
256 struct lls_parse_result *lpr, void *private_result_data)
258 struct osl_object query;
260 int ret = lls_serialize_parse_result(lpr, cmd, &buf, &query.size);
264 ret = send_callback_request(f, &query, afs_cb_result_handler,
265 private_result_data);
270 static int action_if_pattern_matches(struct osl_row *row, void *data)
272 struct pattern_match_data *pmd = data;
273 struct osl_object name_obj;
274 const char *p, *name;
277 ret = osl(osl_get_object(pmd->table, row, pmd->match_col_num,
281 name = (char *)name_obj.data;
282 if ((!name || !*name) && (pmd->pm_flags & PM_SKIP_EMPTY_NAME))
284 if (lls_num_inputs(pmd->lpr) == 0) {
285 if (pmd->pm_flags & PM_NO_PATTERN_MATCHES_EVERYTHING) {
287 return pmd->action(pmd->table, row, name, pmd->data);
292 if (i >= lls_num_inputs(pmd->lpr))
294 p = lls_input(i, pmd->lpr);
295 ret = fnmatch(p, name, pmd->fnmatch_flags);
296 if (ret != FNM_NOMATCH) {
299 ret = pmd->action(pmd->table, row, name, pmd->data);
311 * Execute the given function for each matching row.
313 * \param pmd Describes what to match and how.
317 int for_each_matching_row(struct pattern_match_data *pmd)
319 if (pmd->pm_flags & PM_REVERSE_LOOP)
320 return osl(osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
321 action_if_pattern_matches));
322 return osl(osl_rbtree_loop(pmd->table, pmd->loop_col_num, pmd,
323 action_if_pattern_matches));
327 * Compare two osl objects of string type.
329 * \param obj1 Pointer to the first object.
330 * \param obj2 Pointer to the second object.
332 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
333 * are taken into account.
335 * \return It returns an integer less than, equal to, or greater than zero if
336 * \a obj1 is found, respectively, to be less than, to match, or be greater than
339 * \sa strcmp(3), strncmp(3).
341 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
343 const char *str1 = obj1->data;
344 const char *str2 = obj2->data;
345 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
348 static int pass_afd(int fd, char *buf, size_t size)
350 struct msghdr msg = {.msg_iov = NULL};
351 struct cmsghdr *cmsg;
352 char control[255] __a_aligned(8);
362 msg.msg_control = control;
363 msg.msg_controllen = sizeof(control);
365 cmsg = CMSG_FIRSTHDR(&msg);
366 cmsg->cmsg_level = SOL_SOCKET;
367 cmsg->cmsg_type = SCM_RIGHTS;
368 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
369 *(int *)CMSG_DATA(cmsg) = fd;
371 /* Sum of the length of all control messages in the buffer */
372 msg.msg_controllen = cmsg->cmsg_len;
373 PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size, fd);
374 ret = sendmsg(server_socket, &msg, 0);
376 ret = -ERRNO_TO_PARA_ERROR(errno);
383 * Pass the fd of the next audio file to the server process.
385 * This stores all information for streaming the "best" audio file in a shared
386 * memory area. The id of that area and an open file descriptor for the next
387 * audio file are passed to the server process.
391 * \sa \ref open_and_update_audio_file().
393 static int open_next_audio_file(void)
398 ret = open_and_update_audio_file(&fd);
400 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
401 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
402 goto no_admissible_files;
405 if (!write_ok(server_socket)) {
409 *(uint32_t *)buf = NEXT_AUDIO_FILE;
410 *(uint32_t *)(buf + 4) = (uint32_t)shmid;
411 ret = pass_afd(fd, buf, 8);
419 *(uint32_t *)buf = NO_ADMISSIBLE_FILES;
420 *(uint32_t *)(buf + 4) = (uint32_t)0;
421 return write_all(server_socket, buf, 8);
424 static int activate_mood_or_playlist(const char *arg, int *num_admissible,
431 mode = PLAY_MODE_MOOD;
432 ret = mood_switch(NULL, errmsg);
439 if (!strncmp(arg, "p/", 2)) {
440 ret = playlist_open(arg + 2, errmsg);
441 mode = PLAY_MODE_PLAYLIST;
442 } else if (!strncmp(arg, "m/", 2)) {
443 ret = mood_switch(arg + 2, errmsg);
444 mode = PLAY_MODE_MOOD;
447 *errmsg = make_message("%s: parse error", arg);
448 return -ERRNO_TO_PARA_ERROR(EINVAL);
454 *num_admissible = ret;
455 current_play_mode = mode;
457 * We get called with arg == current_mop from the signal dispatcher
458 * after SIGHUP and from the error path of the select command to
459 * re-select the current mood or playlist. In this case the assignment
460 * to current_mop below would result in a use-after-free condition.
462 if (arg != current_mop) {
464 current_mop = arg? para_strdup(arg) : NULL;
466 /* Notify the server about the mood/playlist change. */
467 mutex_lock(mmd_mutex);
468 strncpy(mmd->afs_mode_string, arg? arg: "dummy",
469 sizeof(mmd->afs_mode_string));
470 mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
472 mutex_unlock(mmd_mutex);
477 * Result handler for sending data to the para_client process.
479 * \param result The data to be sent.
480 * \param band The band designator.
481 * \param private Pointer to the command context.
483 * \return The return value of the underlying call to \ref command.c::send_sb.
485 * \sa \ref callback_result_handler, \ref command.c::send_sb.
487 int afs_cb_result_handler(struct osl_object *result, uint8_t band,
490 struct command_context *cc = private;
498 case SBD_WARNING_LOG:
502 assert(result->size > 0);
503 return send_sb(&cc->scc, result->data, result->size, band, true);
504 case SBD_AFS_CB_FAILURE:
505 return *(int *)(result->data);
511 static void flush_and_free_pb(struct para_buffer *pb)
514 struct afs_max_size_handler_data *amshd = pb->private_data;
516 if (pb->buf && pb->size > 0) {
517 ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
520 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
525 static int com_select_callback(struct afs_callback_arg *aca)
527 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
529 int num_admissible, ret;
532 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
534 arg = lls_input(0, aca->lpr);
535 ret = clear_score_table();
537 para_printf(&aca->pbout, "could not clear score table\n");
540 if (current_play_mode == PLAY_MODE_MOOD)
541 close_current_mood();
544 ret = activate_mood_or_playlist(arg, &num_admissible, &errmsg);
547 /* ignore subsequent errors (but log them) */
548 para_printf(&aca->pbout, "%s\n", errmsg);
550 para_printf(&aca->pbout, "could not activate %s\n", arg);
551 if (current_mop && strcmp(current_mop, arg) != 0) {
553 para_printf(&aca->pbout, "switching back to %s\n", current_mop);
554 ret2 = activate_mood_or_playlist(current_mop, &num_admissible,
558 para_printf(&aca->pbout, "%s\n", errmsg);
560 para_printf(&aca->pbout, "could not reactivate %s: %s\n",
561 current_mop, para_strerror(-ret2));
563 para_printf(&aca->pbout, "activating dummy mood\n");
564 activate_mood_or_playlist(NULL, &num_admissible, NULL);
566 para_printf(&aca->pbout, "activated %s (%d admissible file%s)\n",
567 current_mop? current_mop : "dummy mood", num_admissible,
568 num_admissible == 1? "" : "s");
570 lls_free_parse_result(aca->lpr, cmd);
574 static int com_select(struct command_context *cc, struct lls_parse_result *lpr)
576 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
578 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
581 send_errctx(cc, errctx);
584 return send_lls_callback_request(com_select_callback, cmd, lpr, cc);
586 EXPORT_SERVER_CMD_HANDLER(select);
588 static void init_admissible_files(const char *arg)
590 int ret = activate_mood_or_playlist(arg, NULL, NULL);
592 PARA_WARNING_LOG("could not activate %s: %s\n", arg,
593 para_strerror(-ret));
595 activate_mood_or_playlist(NULL, NULL, NULL);
599 static int setup_command_socket_or_die(void)
602 const char *socket_name = OPT_STRING_VAL(AFS_SOCKET);
605 ret = create_local_socket(socket_name);
607 PARA_EMERG_LOG("%s: %s\n", para_strerror(-ret), socket_name);
611 PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
616 static char *database_dir;
618 static void close_afs_tables(void)
621 PARA_NOTICE_LOG("closing afs tables\n");
622 for (i = 0; i < NUM_AFS_TABLES; i++)
623 afs_tables[i].close();
628 static void get_database_dir(void)
631 if (OPT_GIVEN(AFS_DATABASE_DIR))
632 database_dir = para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR));
634 char *home = para_homedir();
635 database_dir = make_message(
636 "%s/.paraslash/afs_database-0.7", home);
640 PARA_INFO_LOG("afs_database dir %s\n", database_dir);
643 static int make_database_dir(void)
648 ret = para_mkdir(database_dir, 0777);
649 if (ret >= 0 || ret == -ERRNO_TO_PARA_ERROR(EEXIST))
654 static int open_afs_tables(void)
659 PARA_NOTICE_LOG("opening %zu osl tables in %s\n", NUM_AFS_TABLES,
661 for (i = 0; i < NUM_AFS_TABLES; i++) {
662 ret = afs_tables[i].open(database_dir);
665 PARA_ERROR_LOG("could not open %s\n", afs_tables[i].name);
671 afs_tables[--i].close();
675 static int afs_signal_post_monitor(struct sched *s, __a_unused void *context)
679 if (getppid() == 1) {
680 PARA_EMERG_LOG("para_server died\n");
683 signum = para_next_signal();
686 if (signum == SIGHUP) {
688 parse_config_or_die(1);
689 ret = open_afs_tables();
692 init_admissible_files(current_mop);
695 PARA_EMERG_LOG("terminating on signal %d\n", signum);
697 task_notify_all(s, E_AFS_SIGNAL);
698 return -E_AFS_SIGNAL;
701 static void register_signal_task(struct sched *s)
703 para_sigaction(SIGPIPE, SIG_IGN);
704 signal_task = signal_init_or_die();
705 para_install_sighandler(SIGINT);
706 para_install_sighandler(SIGTERM);
707 para_install_sighandler(SIGHUP);
709 signal_task->task = task_register(&(struct task_info) {
711 .pre_monitor = signal_pre_monitor,
712 .post_monitor = afs_signal_post_monitor,
713 .context = signal_task,
718 static struct list_head afs_client_list;
720 /** Describes one connected afs client. */
722 /** Position in the afs client list. */
723 struct list_head node;
724 /** The socket file descriptor for this client. */
726 /** The time the client connected. */
727 struct timeval connect_time;
730 static void command_pre_monitor(struct sched *s, void *context)
732 struct command_task *ct = context;
733 struct afs_client *client;
735 sched_monitor_readfd(server_socket, s);
736 sched_monitor_readfd(ct->fd, s);
737 list_for_each_entry(client, &afs_client_list, node)
738 sched_monitor_readfd(client->fd, s);
742 * Send data as shared memory to a file descriptor.
744 * \param fd File descriptor to send the shmid to.
745 * \param band The band designator for this data.
746 * \param buf The buffer holding the data to be sent.
747 * \param size The size of \a buf.
749 * This function creates a shared memory area large enough to hold
750 * the content given by \a buf and \a size and sends the identifier
751 * of this area to the file descriptor \a fd.
753 * It is called by the AFS max_size handler as well as directly by the AFS
754 * command callbacks to send command output to the command handlers.
756 * \return Zero if \a buf is \p NULL or \a size is zero. Negative on errors,
757 * and positive on success.
759 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size)
763 struct callback_result *cr;
766 assert(band != SBD_OUTPUT);
767 ret = shm_new(size + sizeof(*cr));
771 ret = shm_attach(shmid, ATTACH_RW, &shm);
775 cr->result_size = size;
778 memcpy(shm + sizeof(*cr), buf, size);
779 ret = shm_detach(shm);
782 ret = write_all(fd, (char *)&shmid, sizeof(int));
786 if (shm_destroy(shmid) < 0)
787 PARA_ERROR_LOG("destroy result failed\n");
791 static int call_callback(int fd, int query_shmid)
794 struct callback_query *cq;
796 struct afs_callback_arg aca = {.fd = fd};
798 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
802 aca.query.data = (char *)query_shm + sizeof(*cq);
803 aca.query.size = cq->query_size;
804 aca.pbout.max_size = shm_get_shmmax();
805 aca.pbout.max_size_handler = afs_max_size_handler;
806 aca.pbout.private_data = &(struct afs_max_size_handler_data) {
810 ret = cq->handler(&aca);
811 ret2 = shm_detach(query_shm);
813 if (ret < 0) /* ignore (but log) detach error */
814 PARA_ERROR_LOG("could not detach sma: %s\n",
815 para_strerror(-ret2));
819 flush_and_free_pb(&aca.pbout);
821 ret2 = pass_buffer_as_shm(fd, SBD_AFS_CB_FAILURE,
822 (const char *)&ret, sizeof(ret));
824 PARA_ERROR_LOG("could not pass cb failure packet: %s\n",
825 para_strerror(-ret));
830 static int execute_server_command(void)
834 int ret = read_nonblock(server_socket, buf, sizeof(buf) - 1, &n);
836 if (ret < 0 || n == 0)
839 if (strcmp(buf, "new"))
840 return -ERRNO_TO_PARA_ERROR(EINVAL);
841 return open_next_audio_file();
844 /* returns 0 if no data available, 1 else */
845 static int execute_afs_command(int fd)
849 char buf[sizeof(cookie) + sizeof(query_shmid)];
851 int ret = read_nonblock(fd, buf, sizeof(buf), &n);
857 if (n != sizeof(buf)) {
858 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
859 ret, (long unsigned) sizeof(buf));
862 cookie = *(uint32_t *)buf;
863 if (cookie != afs_socket_cookie) {
864 PARA_NOTICE_LOG("received invalid cookie (got %u, expected %u)\n",
865 (unsigned)cookie, (unsigned)afs_socket_cookie);
868 query_shmid = *(int *)(buf + sizeof(cookie));
869 if (query_shmid < 0) {
870 PARA_WARNING_LOG("received invalid query shmid %d)\n",
874 ret = call_callback(fd, query_shmid);
878 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
882 /** Shutdown connection if query has not arrived until this many seconds. */
883 #define AFS_CLIENT_TIMEOUT 3
885 static int command_post_monitor(struct sched *s, void *context)
887 struct command_task *ct = context;
888 struct sockaddr_un unix_addr;
889 struct afs_client *client, *tmp;
892 ret = task_get_notification(ct->task);
895 ret = execute_server_command();
897 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
898 task_notify_all(s, -ret);
901 /* Check the list of connected clients. */
902 list_for_each_entry_safe(client, tmp, &afs_client_list, node) {
903 ret = execute_afs_command(client->fd);
904 if (ret == 0) { /* prevent bogus connection flooding */
906 tv_diff(now, &client->connect_time, &diff);
907 if (diff.tv_sec < AFS_CLIENT_TIMEOUT)
909 PARA_WARNING_LOG("connection timeout\n");
912 list_del(&client->node);
915 /* Accept connections on the local socket. */
916 ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr), &fd);
918 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
921 ret = mark_fd_nonblocking(fd);
923 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
927 client = alloc(sizeof(*client));
929 client->connect_time = *now;
930 para_list_add(&client->node, &afs_client_list);
934 static void register_command_task(struct sched *s)
936 struct command_task *ct = &command_task_struct;
937 ct->fd = setup_command_socket_or_die();
939 ct->task = task_register(&(struct task_info) {
940 .name = "afs command",
941 .pre_monitor = command_pre_monitor,
942 .post_monitor = command_post_monitor,
947 static int afs_poll(struct pollfd *fds, nfds_t nfds, int timeout)
949 mutex_lock(mmd_mutex);
950 daemon_set_loglevel(mmd->loglevel);
951 mutex_unlock(mmd_mutex);
952 return xpoll(fds, nfds, timeout);
956 * Initialize the audio file selector process.
958 * \param socket_fd File descriptor used for communication with the server.
960 __noreturn void afs_init(int socket_fd)
962 static struct sched s;
965 register_signal_task(&s);
966 init_list_head(&afs_client_list);
967 for (i = 0; i < NUM_AFS_TABLES; i++)
968 afs_tables[i].init(&afs_tables[i]);
969 ret = open_afs_tables();
972 server_socket = socket_fd;
973 ret = mark_fd_nonblocking(server_socket);
976 PARA_INFO_LOG("server_socket: %d\n", server_socket);
977 init_admissible_files(OPT_STRING_VAL(AFS_INITIAL_MODE));
978 register_command_task(&s);
979 s.default_timeout = 1000;
980 s.poll_function = afs_poll;
981 ret = write(socket_fd, "\0", 1);
985 ret = -ERRNO_TO_PARA_ERROR(errno);
990 close_current_mood();
994 signal_shutdown(signal_task);
999 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1003 static int com_init_callback(struct afs_callback_arg *aca)
1005 uint32_t table_mask = *(uint32_t *)aca->query.data;
1010 for (i = 0; i < NUM_AFS_TABLES; i++) {
1011 struct afs_table *t = &afs_tables[i];
1013 if (!(table_mask & (1 << i)))
1017 ret = t->create(database_dir);
1019 para_printf(&aca->pbout, "cannot create table %s\n",
1023 para_printf(&aca->pbout, "successfully created %s table\n",
1026 ret = open_afs_tables();
1028 para_printf(&aca->pbout, "cannot open afs tables: %s\n",
1029 para_strerror(-ret));
1034 static int com_init(struct command_context *cc, struct lls_parse_result *lpr)
1037 uint32_t table_mask = (1 << (NUM_AFS_TABLES + 1)) - 1;
1038 struct osl_object query = {.data = &table_mask,
1039 .size = sizeof(table_mask)};
1040 unsigned num_inputs = lls_num_inputs(lpr);
1042 ret = make_database_dir();
1045 if (num_inputs > 0) {
1047 for (i = 0; i < num_inputs; i++) {
1048 for (j = 0; j < NUM_AFS_TABLES; j++) {
1049 struct afs_table *t = &afs_tables[j];
1051 if (strcmp(lls_input(i, lpr), t->name))
1053 table_mask |= (1 << j);
1056 if (j == NUM_AFS_TABLES)
1057 return -E_BAD_TABLE_NAME;
1060 return send_callback_request(com_init_callback, &query,
1061 afs_cb_result_handler, cc);
1063 EXPORT_SERVER_CMD_HANDLER(init);
1065 static int com_check(struct command_context *cc, struct lls_parse_result *lpr)
1067 const struct lls_opt_result *r_a = SERVER_CMD_OPT_RESULT(CHECK, AFT, lpr);
1068 const struct lls_opt_result *r_A = SERVER_CMD_OPT_RESULT(CHECK, ATTRIBUTE, lpr);
1069 const struct lls_opt_result *r_m = SERVER_CMD_OPT_RESULT(CHECK, MOOD, lpr);
1070 const struct lls_opt_result *r_p = SERVER_CMD_OPT_RESULT(CHECK, PLAYLIST, lpr);
1071 bool noopt = !lls_opt_given(r_a) && !lls_opt_given(r_A)
1072 && !lls_opt_given(r_m) && !lls_opt_given(r_p);
1075 if (noopt || lls_opt_given(r_a)) {
1076 ret = send_callback_request(aft_check_callback, NULL,
1077 afs_cb_result_handler, cc);
1081 if (noopt || lls_opt_given(r_A)) {
1082 ret = send_callback_request(attribute_check_callback, NULL,
1083 afs_cb_result_handler, cc);
1087 if (noopt || lls_opt_given(r_p)) {
1088 ret = send_callback_request(playlist_check_callback,
1089 NULL, afs_cb_result_handler, cc);
1093 if (noopt || lls_opt_given(r_m)) {
1094 ret = send_callback_request(mood_check_callback, NULL,
1095 afs_cb_result_handler, cc);
1101 EXPORT_SERVER_CMD_HANDLER(check);
1104 * The afs event dispatcher.
1106 * \param event Type of the event.
1107 * \param pb May be \p NULL.
1108 * \param data Type depends on \a event.
1110 * This function calls each table event handler, passing \a pb and \a data
1111 * verbatim. It's up to the handlers to interpret the \a data pointer. If a
1112 * handler returns negative, the loop is aborted.
1114 * \return The (negative) error code of the first handler that failed, or non-negative
1115 * if all handlers succeeded.
1117 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
1122 for (i = 0; i < NUM_AFS_TABLES; i++) {
1123 struct afs_table *t = &afs_tables[i];
1124 if (!t->event_handler)
1126 ret = t->event_handler(event, pb, data);
1128 PARA_CRIT_LOG("table %s, event %u: %s\n", t->name,
1129 event, para_strerror(-ret));
1137 * Dummy event handler for the images table.
1139 * \param event Unused.
1141 * \param data Unused.
1143 * \return The images table does not honor events, so this handler always
1146 __a_const int images_event_handler(__a_unused enum afs_events event,
1147 __a_unused struct para_buffer *pb, __a_unused void *data)
1153 * Dummy event handler for the lyrics table.
1155 * \param event Unused.
1157 * \param data Unused.
1159 * \return The lyrics table does not honor events, so this handler always
1162 __a_const int lyrics_event_handler(__a_unused enum afs_events event,
1163 __a_unused struct para_buffer *pb, __a_unused void *data)