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"
37 static struct afs_table afs_tables[] = {
38 {.init = aft_init, .name = "audio_files"},
39 {.init = attribute_init, .name = "attributes"},
40 {.init = score_init, .name = "scores"},
41 {.init = moods_init, .name = "moods"},
42 {.init = lyrics_init, .name = "lyrics"},
43 {.init = images_init, .name = "images"},
44 {.init = playlists_init, .name = "playlists"},
47 #define NUM_AFS_TABLES ARRAY_SIZE(afs_tables)
50 /** The file descriptor for the local socket. */
52 /** The associated task structure. */
57 extern struct misc_meta_data *mmd;
59 static int server_socket;
60 static struct command_task command_task_struct;
61 static struct signal_task *signal_task;
63 static enum play_mode current_play_mode;
64 static char *current_mop; /* mode or playlist specifier. NULL means dummy mood */
66 extern uint32_t afs_socket_cookie;
69 * Passed from command handlers to afs.
71 * Command handlers cannot change the afs database directly because they run in
72 * a separate process. The callback query structure circumvents this
73 * restriction as follows. To instruct the afs process to execute a particular
74 * function, the command hander writes an instance of this structure to a
75 * shared memory area, along with the arguments to the callback function. The
76 * identifier of the shared memory area is transferred to the afs process via
79 * The afs process reads the shared memory id from the command socket, attaches
80 * the corresponding area, and calls the callback function whose address is
83 * The command output, if any, is transferred back to the command handler in
84 * the same way: The afs process writes the output to a second shared memory
85 * area together with a fixed size metadata header whose format corresponds to
86 * the \ref callback_result structure. The identifier of this area is sent back
87 * to the command handler which attaches the area and forwards the output to
90 * \sa \ref struct callback_result.
92 struct callback_query {
93 /** The function to be called. */
94 afs_callback *handler;
95 /** The number of bytes of the query */
100 * Structure embedded in the result of a callback.
102 * If the callback produced a result, an instance of that structure is embedded
103 * into the shared memory area holding the result, mainly to let the command
104 * handler know the size of the result.
106 * \sa \ref struct callback_query.
108 struct callback_result {
109 /** The number of bytes of the result. */
111 /** The band designator (loglevel for the result). */
115 static int dispatch_result(int result_shmid, callback_result_handler *handler,
116 void *private_result_data)
118 struct osl_object result;
120 /* must attach r/w as result.data might get encrypted in-place. */
121 int ret2, ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
122 struct callback_result *cr = result_shm;
125 PARA_ERROR_LOG("attach failed: %s\n", para_strerror(-ret));
128 result.size = cr->result_size;
129 result.data = result_shm + sizeof(*cr);
131 ret = handler(&result, cr->band, private_result_data);
132 ret2 = shm_detach(result_shm);
134 PARA_ERROR_LOG("detach failed: %s\n", para_strerror(-ret2));
142 * Ask the afs process to call a given function.
144 * \param f The function to be called.
145 * \param query Pointer to arbitrary data for the callback.
146 * \param result_handler Called for each shm area sent by the callback.
147 * \param private_result_data Passed verbatim to \a result_handler.
149 * This function creates a socket for communication with the afs process and a
150 * shared memory area (sma) to which the buffer pointed to by \a query is
151 * copied. It then notifies the afs process that the callback function \a f
152 * should be executed by sending the shared memory identifier (shmid) to the
155 * If the callback produces a result, it sends any number of shared memory
156 * identifiers back via the socket. For each such identifier received, \a
157 * result_handler is called. The contents of the sma identified by the received
158 * shmid are passed to that function as an osl object. The private_result_data
159 * pointer is passed as the second argument to \a result_handler.
161 * \return Number of shared memory areas dispatched on success, negative on
164 int send_callback_request(afs_callback *f, struct osl_object *query,
165 callback_result_handler *result_handler,
166 void *private_result_data)
168 struct callback_query *cq;
169 int ret, fd = -1, query_shmid, result_shmid;
171 char buf[sizeof(afs_socket_cookie) + sizeof(int)];
172 size_t query_shm_size = sizeof(*cq);
173 int dispatch_error = 0, num_dispatched = 0;
176 query_shm_size += query->size;
177 ret = shm_new(query_shm_size);
181 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
186 cq->query_size = query_shm_size - sizeof(*cq);
189 memcpy(query_shm + sizeof(*cq), query->data, query->size);
190 ret = shm_detach(query_shm);
194 *(uint32_t *)buf = afs_socket_cookie;
195 *(int *)(buf + sizeof(afs_socket_cookie)) = query_shmid;
197 ret = connect_local_socket(OPT_STRING_VAL(AFS_SOCKET));
201 ret = write_all(fd, buf, sizeof(buf));
205 * Read all shmids from afs.
207 * Even if the dispatcher returns an error we _must_ continue to read
208 * shmids from fd so that we can destroy all shared memory areas that
209 * have been created for us by the afs process.
212 ret = recv_bin_buffer(fd, buf, sizeof(int));
215 assert(ret == sizeof(int));
219 ret = dispatch_result(result_shmid, result_handler,
220 private_result_data);
221 if (ret < 0 && dispatch_error >= 0)
222 dispatch_error = ret;
223 ret = shm_destroy(result_shmid);
225 PARA_CRIT_LOG("destroy result failed: %s\n",
226 para_strerror(-ret));
230 if (shm_destroy(query_shmid) < 0)
231 PARA_CRIT_LOG("shm destroy error\n");
234 if (dispatch_error < 0)
235 return dispatch_error;
238 return num_dispatched;
242 * Wrapper for send_callback_request() which passes a lopsub parse result.
244 * \param f The callback function.
245 * \param cmd Needed for (de-)serialization.
246 * \param lpr Must match cmd.
247 * \param private_result_data Passed to send_callback_request().
249 * This function serializes the parse result given by the lpr pointer into a
250 * buffer. The buffer is sent as the query to the afs process with the callback
253 * \return The return value of the underlying call to send_callback_request().
255 int send_lls_callback_request(afs_callback *f,
256 const struct lls_command * const cmd,
257 struct lls_parse_result *lpr, void *private_result_data)
259 struct osl_object query;
261 int ret = lls_serialize_parse_result(lpr, cmd, &buf, &query.size);
265 ret = send_callback_request(f, &query, afs_cb_result_handler,
266 private_result_data);
271 static int action_if_pattern_matches(struct osl_row *row, void *data)
273 struct pattern_match_data *pmd = data;
274 struct osl_object name_obj;
275 const char *p, *name;
278 ret = osl(osl_get_object(pmd->table, row, pmd->match_col_num,
282 name = (char *)name_obj.data;
283 if ((!name || !*name) && (pmd->pm_flags & PM_SKIP_EMPTY_NAME))
285 if (lls_num_inputs(pmd->lpr) == 0) {
286 if (pmd->pm_flags & PM_NO_PATTERN_MATCHES_EVERYTHING) {
288 return pmd->action(pmd->table, row, name, pmd->data);
293 if (i >= lls_num_inputs(pmd->lpr))
295 p = lls_input(i, pmd->lpr);
296 ret = fnmatch(p, name, pmd->fnmatch_flags);
297 if (ret != FNM_NOMATCH) {
300 ret = pmd->action(pmd->table, row, name, pmd->data);
312 * Execute the given function for each matching row.
314 * \param pmd Describes what to match and how.
318 int for_each_matching_row(struct pattern_match_data *pmd)
320 if (pmd->pm_flags & PM_REVERSE_LOOP)
321 return osl(osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
322 action_if_pattern_matches));
323 return osl(osl_rbtree_loop(pmd->table, pmd->loop_col_num, pmd,
324 action_if_pattern_matches));
328 * Compare two osl objects of string type.
330 * \param obj1 Pointer to the first object.
331 * \param obj2 Pointer to the second object.
333 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
334 * are taken into account.
336 * \return It returns an integer less than, equal to, or greater than zero if
337 * \a obj1 is found, respectively, to be less than, to match, or be greater than
340 * \sa strcmp(3), strncmp(3).
342 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
344 const char *str1 = obj1->data;
345 const char *str2 = obj2->data;
346 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
349 static int pass_afd(int fd, char *buf, size_t size)
351 struct msghdr msg = {.msg_iov = NULL};
352 struct cmsghdr *cmsg;
353 char control[255] __a_aligned(8);
363 msg.msg_control = control;
364 msg.msg_controllen = sizeof(control);
366 cmsg = CMSG_FIRSTHDR(&msg);
367 cmsg->cmsg_level = SOL_SOCKET;
368 cmsg->cmsg_type = SCM_RIGHTS;
369 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
370 *(int *)CMSG_DATA(cmsg) = fd;
372 /* Sum of the length of all control messages in the buffer */
373 msg.msg_controllen = cmsg->cmsg_len;
374 PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size, fd);
375 ret = sendmsg(server_socket, &msg, 0);
377 ret = -ERRNO_TO_PARA_ERROR(errno);
384 * Pass the fd of the next audio file to the server process.
386 * This stores all information for streaming the "best" audio file in a shared
387 * memory area. The id of that area and an open file descriptor for the next
388 * audio file are passed to the server process.
392 * \sa \ref open_and_update_audio_file().
394 static int open_next_audio_file(void)
399 ret = open_and_update_audio_file(&fd);
401 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
402 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
403 goto no_admissible_files;
406 if (!write_ok(server_socket)) {
410 *(uint32_t *)buf = NEXT_AUDIO_FILE;
411 *(uint32_t *)(buf + 4) = (uint32_t)shmid;
412 ret = pass_afd(fd, buf, 8);
420 *(uint32_t *)buf = NO_ADMISSIBLE_FILES;
421 *(uint32_t *)(buf + 4) = (uint32_t)0;
422 return write_all(server_socket, buf, 8);
425 static int activate_mood_or_playlist(const char *arg, int *num_admissible,
432 mode = PLAY_MODE_MOOD;
433 ret = change_current_mood(NULL, errmsg);
440 if (!strncmp(arg, "p/", 2)) {
441 ret = playlist_open(arg + 2);
442 if (ret < 0 && errmsg)
443 *errmsg = make_message( "could not open %s",
445 mode = PLAY_MODE_PLAYLIST;
446 } else if (!strncmp(arg, "m/", 2)) {
447 ret = change_current_mood(arg + 2, errmsg);
448 mode = PLAY_MODE_MOOD;
451 *errmsg = make_message("%s: parse error", arg);
452 return -ERRNO_TO_PARA_ERROR(EINVAL);
458 *num_admissible = ret;
459 current_play_mode = mode;
461 * We get called with arg == current_mop from the signal dispatcher
462 * after SIGHUP and from the error path of the select command to
463 * re-select the current mood or playlist. In this case the assignment
464 * to current_mop below would result in a use-after-free condition.
466 if (arg != current_mop) {
469 current_mop = para_strdup(arg);
470 mutex_lock(mmd_mutex);
471 strncpy(mmd->afs_mode_string, arg,
472 sizeof(mmd->afs_mode_string));
473 mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
475 mutex_unlock(mmd_mutex);
477 mutex_lock(mmd_mutex);
478 strcpy(mmd->afs_mode_string, "dummy");
480 mutex_unlock(mmd_mutex);
488 * Result handler for sending data to the para_client process.
490 * \param result The data to be sent.
491 * \param band The band designator.
492 * \param private Pointer to the command context.
494 * \return The return value of the underlying call to \ref command.c::send_sb.
496 * \sa \ref callback_result_handler, \ref command.c::send_sb.
498 int afs_cb_result_handler(struct osl_object *result, uint8_t band,
501 struct command_context *cc = private;
509 case SBD_WARNING_LOG:
513 assert(result->size > 0);
514 return send_sb(&cc->scc, result->data, result->size, band, true);
515 case SBD_AFS_CB_FAILURE:
516 return *(int *)(result->data);
522 static void flush_and_free_pb(struct para_buffer *pb)
525 struct afs_max_size_handler_data *amshd = pb->private_data;
527 if (pb->buf && pb->size > 0) {
528 ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
531 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
536 static int com_select_callback(struct afs_callback_arg *aca)
538 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
540 int num_admissible, ret;
543 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
545 arg = lls_input(0, aca->lpr);
546 ret = clear_score_table();
548 para_printf(&aca->pbout, "could not clear score table\n");
551 if (current_play_mode == PLAY_MODE_MOOD)
552 close_current_mood();
555 ret = activate_mood_or_playlist(arg, &num_admissible, &errmsg);
558 /* ignore subsequent errors (but log them) */
559 para_printf(&aca->pbout, "%s\n", errmsg);
561 para_printf(&aca->pbout, "could not activate %s\n", arg);
562 if (current_mop && strcmp(current_mop, arg) != 0) {
564 para_printf(&aca->pbout, "switching back to %s\n", current_mop);
565 ret2 = activate_mood_or_playlist(current_mop, &num_admissible,
569 para_printf(&aca->pbout, "%s\n", errmsg);
571 para_printf(&aca->pbout, "could not reactivate %s: %s\n",
572 current_mop, para_strerror(-ret2));
574 para_printf(&aca->pbout, "activating dummy mood\n");
575 activate_mood_or_playlist(NULL, &num_admissible, NULL);
577 para_printf(&aca->pbout, "activated %s (%d admissible file%s)\n",
578 current_mop? current_mop : "dummy mood", num_admissible,
579 num_admissible == 1? "" : "s");
581 lls_free_parse_result(aca->lpr, cmd);
585 static int com_select(struct command_context *cc, struct lls_parse_result *lpr)
587 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
589 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
592 send_errctx(cc, errctx);
595 return send_lls_callback_request(com_select_callback, cmd, lpr, cc);
597 EXPORT_SERVER_CMD_HANDLER(select);
599 static void init_admissible_files(const char *arg)
601 int ret = activate_mood_or_playlist(arg, NULL, NULL);
603 PARA_WARNING_LOG("could not activate %s: %s\n", arg,
604 para_strerror(-ret));
606 activate_mood_or_playlist(NULL, NULL, NULL);
610 static int setup_command_socket_or_die(void)
613 const char *socket_name = OPT_STRING_VAL(AFS_SOCKET);
616 ret = create_local_socket(socket_name);
618 PARA_EMERG_LOG("%s: %s\n", para_strerror(-ret), socket_name);
622 PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
627 static char *database_dir;
629 static void close_afs_tables(void)
632 PARA_NOTICE_LOG("closing afs tables\n");
633 for (i = 0; i < NUM_AFS_TABLES; i++)
634 afs_tables[i].close();
639 static void get_database_dir(void)
642 if (OPT_GIVEN(AFS_DATABASE_DIR))
643 database_dir = para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR));
645 char *home = para_homedir();
646 database_dir = make_message(
647 "%s/.paraslash/afs_database-0.7", home);
651 PARA_INFO_LOG("afs_database dir %s\n", database_dir);
654 static int make_database_dir(void)
659 ret = para_mkdir(database_dir, 0777);
660 if (ret >= 0 || ret == -ERRNO_TO_PARA_ERROR(EEXIST))
665 static int open_afs_tables(void)
670 PARA_NOTICE_LOG("opening %zu osl tables in %s\n", NUM_AFS_TABLES,
672 for (i = 0; i < NUM_AFS_TABLES; i++) {
673 ret = afs_tables[i].open(database_dir);
676 PARA_ERROR_LOG("could not open %s\n", afs_tables[i].name);
682 afs_tables[--i].close();
686 static int afs_signal_post_monitor(struct sched *s, __a_unused void *context)
690 if (getppid() == 1) {
691 PARA_EMERG_LOG("para_server died\n");
694 signum = para_next_signal();
697 if (signum == SIGHUP) {
699 parse_config_or_die(1);
700 ret = open_afs_tables();
703 init_admissible_files(current_mop);
706 PARA_EMERG_LOG("terminating on signal %d\n", signum);
708 task_notify_all(s, E_AFS_SIGNAL);
709 return -E_AFS_SIGNAL;
712 static void register_signal_task(struct sched *s)
714 para_sigaction(SIGPIPE, SIG_IGN);
715 signal_task = signal_init_or_die();
716 para_install_sighandler(SIGINT);
717 para_install_sighandler(SIGTERM);
718 para_install_sighandler(SIGHUP);
720 signal_task->task = task_register(&(struct task_info) {
722 .pre_monitor = signal_pre_monitor,
723 .post_monitor = afs_signal_post_monitor,
724 .context = signal_task,
729 static struct list_head afs_client_list;
731 /** Describes one connected afs client. */
733 /** Position in the afs client list. */
734 struct list_head node;
735 /** The socket file descriptor for this client. */
737 /** The time the client connected. */
738 struct timeval connect_time;
741 static void command_pre_monitor(struct sched *s, void *context)
743 struct command_task *ct = context;
744 struct afs_client *client;
746 sched_monitor_readfd(server_socket, s);
747 sched_monitor_readfd(ct->fd, s);
748 list_for_each_entry(client, &afs_client_list, node)
749 sched_monitor_readfd(client->fd, s);
753 * Send data as shared memory to a file descriptor.
755 * \param fd File descriptor to send the shmid to.
756 * \param band The band designator for this data.
757 * \param buf The buffer holding the data to be sent.
758 * \param size The size of \a buf.
760 * This function creates a shared memory area large enough to hold
761 * the content given by \a buf and \a size and sends the identifier
762 * of this area to the file descriptor \a fd.
764 * It is called by the AFS max_size handler as well as directly by the AFS
765 * command callbacks to send command output to the command handlers.
767 * \return Zero if \a buf is \p NULL or \a size is zero. Negative on errors,
768 * and positive on success.
770 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size)
774 struct callback_result *cr;
777 assert(band != SBD_OUTPUT);
778 ret = shm_new(size + sizeof(*cr));
782 ret = shm_attach(shmid, ATTACH_RW, &shm);
786 cr->result_size = size;
789 memcpy(shm + sizeof(*cr), buf, size);
790 ret = shm_detach(shm);
793 ret = write_all(fd, (char *)&shmid, sizeof(int));
797 if (shm_destroy(shmid) < 0)
798 PARA_ERROR_LOG("destroy result failed\n");
802 static int call_callback(int fd, int query_shmid)
805 struct callback_query *cq;
807 struct afs_callback_arg aca = {.fd = fd};
809 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
813 aca.query.data = (char *)query_shm + sizeof(*cq);
814 aca.query.size = cq->query_size;
815 aca.pbout.max_size = shm_get_shmmax();
816 aca.pbout.max_size_handler = afs_max_size_handler;
817 aca.pbout.private_data = &(struct afs_max_size_handler_data) {
821 ret = cq->handler(&aca);
822 ret2 = shm_detach(query_shm);
824 if (ret < 0) /* ignore (but log) detach error */
825 PARA_ERROR_LOG("could not detach sma: %s\n",
826 para_strerror(-ret2));
830 flush_and_free_pb(&aca.pbout);
832 ret2 = pass_buffer_as_shm(fd, SBD_AFS_CB_FAILURE,
833 (const char *)&ret, sizeof(ret));
835 PARA_ERROR_LOG("could not pass cb failure packet: %s\n",
836 para_strerror(-ret));
841 static int execute_server_command(void)
845 int ret = read_nonblock(server_socket, buf, sizeof(buf) - 1, &n);
847 if (ret < 0 || n == 0)
850 if (strcmp(buf, "new"))
851 return -ERRNO_TO_PARA_ERROR(EINVAL);
852 return open_next_audio_file();
855 /* returns 0 if no data available, 1 else */
856 static int execute_afs_command(int fd)
860 char buf[sizeof(cookie) + sizeof(query_shmid)];
862 int ret = read_nonblock(fd, buf, sizeof(buf), &n);
868 if (n != sizeof(buf)) {
869 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
870 ret, (long unsigned) sizeof(buf));
873 cookie = *(uint32_t *)buf;
874 if (cookie != afs_socket_cookie) {
875 PARA_NOTICE_LOG("received invalid cookie (got %u, expected %u)\n",
876 (unsigned)cookie, (unsigned)afs_socket_cookie);
879 query_shmid = *(int *)(buf + sizeof(cookie));
880 if (query_shmid < 0) {
881 PARA_WARNING_LOG("received invalid query shmid %d)\n",
885 ret = call_callback(fd, query_shmid);
889 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
893 /** Shutdown connection if query has not arrived until this many seconds. */
894 #define AFS_CLIENT_TIMEOUT 3
896 static int command_post_monitor(struct sched *s, void *context)
898 struct command_task *ct = context;
899 struct sockaddr_un unix_addr;
900 struct afs_client *client, *tmp;
903 ret = task_get_notification(ct->task);
906 ret = execute_server_command();
908 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
909 task_notify_all(s, -ret);
912 /* Check the list of connected clients. */
913 list_for_each_entry_safe(client, tmp, &afs_client_list, node) {
914 ret = execute_afs_command(client->fd);
915 if (ret == 0) { /* prevent bogus connection flooding */
917 tv_diff(now, &client->connect_time, &diff);
918 if (diff.tv_sec < AFS_CLIENT_TIMEOUT)
920 PARA_WARNING_LOG("connection timeout\n");
923 list_del(&client->node);
926 /* Accept connections on the local socket. */
927 ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr), &fd);
929 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
932 ret = mark_fd_nonblocking(fd);
934 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
938 client = alloc(sizeof(*client));
940 client->connect_time = *now;
941 para_list_add(&client->node, &afs_client_list);
945 static void register_command_task(struct sched *s)
947 struct command_task *ct = &command_task_struct;
948 ct->fd = setup_command_socket_or_die();
950 ct->task = task_register(&(struct task_info) {
951 .name = "afs command",
952 .pre_monitor = command_pre_monitor,
953 .post_monitor = command_post_monitor,
958 static int afs_poll(struct pollfd *fds, nfds_t nfds, int timeout)
960 mutex_lock(mmd_mutex);
961 daemon_set_loglevel(mmd->loglevel);
962 mutex_unlock(mmd_mutex);
963 return xpoll(fds, nfds, timeout);
967 * Initialize the audio file selector process.
969 * \param socket_fd File descriptor used for communication with the server.
971 __noreturn void afs_init(int socket_fd)
973 static struct sched s;
976 register_signal_task(&s);
977 init_list_head(&afs_client_list);
978 for (i = 0; i < NUM_AFS_TABLES; i++)
979 afs_tables[i].init(&afs_tables[i]);
980 ret = open_afs_tables();
983 server_socket = socket_fd;
984 ret = mark_fd_nonblocking(server_socket);
987 PARA_INFO_LOG("server_socket: %d\n", server_socket);
988 init_admissible_files(OPT_STRING_VAL(AFS_INITIAL_MODE));
989 register_command_task(&s);
990 s.default_timeout = 1000;
991 s.poll_function = afs_poll;
992 ret = write(socket_fd, "\0", 1);
996 ret = -ERRNO_TO_PARA_ERROR(errno);
1001 close_current_mood();
1005 signal_shutdown(signal_task);
1006 free_status_items();
1010 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1014 static int com_init_callback(struct afs_callback_arg *aca)
1016 uint32_t table_mask = *(uint32_t *)aca->query.data;
1021 for (i = 0; i < NUM_AFS_TABLES; i++) {
1022 struct afs_table *t = &afs_tables[i];
1024 if (!(table_mask & (1 << i)))
1028 ret = t->create(database_dir);
1030 para_printf(&aca->pbout, "cannot create table %s\n",
1034 para_printf(&aca->pbout, "successfully created %s table\n",
1037 ret = open_afs_tables();
1039 para_printf(&aca->pbout, "cannot open afs tables: %s\n",
1040 para_strerror(-ret));
1045 static int com_init(struct command_context *cc, struct lls_parse_result *lpr)
1048 uint32_t table_mask = (1 << (NUM_AFS_TABLES + 1)) - 1;
1049 struct osl_object query = {.data = &table_mask,
1050 .size = sizeof(table_mask)};
1051 unsigned num_inputs = lls_num_inputs(lpr);
1053 ret = make_database_dir();
1056 if (num_inputs > 0) {
1058 for (i = 0; i < num_inputs; i++) {
1059 for (j = 0; j < NUM_AFS_TABLES; j++) {
1060 struct afs_table *t = &afs_tables[j];
1062 if (strcmp(lls_input(i, lpr), t->name))
1064 table_mask |= (1 << j);
1067 if (j == NUM_AFS_TABLES)
1068 return -E_BAD_TABLE_NAME;
1071 return send_callback_request(com_init_callback, &query,
1072 afs_cb_result_handler, cc);
1074 EXPORT_SERVER_CMD_HANDLER(init);
1076 static int com_check(struct command_context *cc, struct lls_parse_result *lpr)
1078 const struct lls_opt_result *r_a = SERVER_CMD_OPT_RESULT(CHECK, AFT, lpr);
1079 const struct lls_opt_result *r_A = SERVER_CMD_OPT_RESULT(CHECK, ATTRIBUTE, lpr);
1080 const struct lls_opt_result *r_m = SERVER_CMD_OPT_RESULT(CHECK, MOOD, lpr);
1081 const struct lls_opt_result *r_p = SERVER_CMD_OPT_RESULT(CHECK, PLAYLIST, lpr);
1082 bool noopt = !lls_opt_given(r_a) && !lls_opt_given(r_A)
1083 && !lls_opt_given(r_m) && !lls_opt_given(r_p);
1086 if (noopt || lls_opt_given(r_a)) {
1087 ret = send_callback_request(aft_check_callback, NULL,
1088 afs_cb_result_handler, cc);
1092 if (noopt || lls_opt_given(r_A)) {
1093 ret = send_callback_request(attribute_check_callback, NULL,
1094 afs_cb_result_handler, cc);
1098 if (noopt || lls_opt_given(r_p)) {
1099 ret = send_callback_request(playlist_check_callback,
1100 NULL, afs_cb_result_handler, cc);
1104 if (noopt || lls_opt_given(r_m)) {
1105 ret = send_callback_request(mood_check_callback, NULL,
1106 afs_cb_result_handler, cc);
1112 EXPORT_SERVER_CMD_HANDLER(check);
1115 * The afs event dispatcher.
1117 * \param event Type of the event.
1118 * \param pb May be \p NULL.
1119 * \param data Type depends on \a event.
1121 * This function calls each table event handler, passing \a pb and \a data
1122 * verbatim. It's up to the handlers to interpret the \a data pointer. If a
1123 * handler returns negative, the loop is aborted.
1125 * \return The (negative) error code of the first handler that failed, or non-negative
1126 * if all handlers succeeded.
1128 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
1133 for (i = 0; i < NUM_AFS_TABLES; i++) {
1134 struct afs_table *t = &afs_tables[i];
1135 if (!t->event_handler)
1137 ret = t->event_handler(event, pb, data);
1139 PARA_CRIT_LOG("table %s, event %u: %s\n", t->name,
1140 event, para_strerror(-ret));
1148 * Dummy event handler for the images table.
1150 * \param event Unused.
1152 * \param data Unused.
1154 * \return The images table does not honor events, so this handler always
1157 __a_const int images_event_handler(__a_unused enum afs_events event,
1158 __a_unused struct para_buffer *pb, __a_unused void *data)
1164 * Dummy event handler for the lyrics table.
1166 * \param event Unused.
1168 * \param data Unused.
1170 * \return The lyrics table does not honor events, so this handler always
1173 __a_const int lyrics_event_handler(__a_unused enum afs_events event,
1174 __a_unused struct para_buffer *pb, __a_unused void *data)