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 * Paraslash's scoring system is based on Gaussian normal
44 * distributions, and the relevant data is stored in the rbtrees of an
45 * osl table containing only volatile columns. See \ref score.c for
50 * A standard blob table containing the mood definitions. For details
54 /** A blob table containing lyrics on a per-song basis. */
56 /** Another blob table for images (for example album cover art). */
58 /** Yet another blob table for storing standard playlists. */
60 /** How many tables are in use? */
64 static struct afs_table afs_tables[NUM_AFS_TABLES] = {
65 [TBLNUM_AUDIO_FILES] = {.init = aft_init, .name = "audio_files"},
66 [TBLNUM_ATTRIBUTES] = {.init = attribute_init, .name = "attributes"},
67 [TBLNUM_SCORES] = {.init = score_init, .name = "scores"},
68 [TBLNUM_MOODS] = {.init = moods_init, .name = "moods"},
69 [TBLNUM_LYRICS] = {.init = lyrics_init, .name = "lyrics"},
70 [TBLNUM_IMAGES] = {.init = images_init, .name = "images"},
71 [TBLNUM_PLAYLIST] = {.init = playlists_init, .name = "playlists"},
75 /** The file descriptor for the local socket. */
77 /** The associated task structure. */
82 extern struct misc_meta_data *mmd;
84 static int server_socket;
85 static struct command_task command_task_struct;
86 static struct signal_task *signal_task;
88 static enum play_mode current_play_mode;
89 static char *current_mop; /* mode or playlist specifier. NULL means dummy mood */
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 corresponding 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 \ref struct callback_result.
117 struct callback_query {
118 /** The function to be called. */
119 afs_callback *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 embedded
128 * into the shared memory area holding the result, mainly to let the command
129 * handler know the size of the result.
131 * \sa \ref struct callback_query.
133 struct callback_result {
134 /** The number of bytes of the result. */
136 /** The band designator (loglevel for the result). */
140 static int dispatch_result(int result_shmid, callback_result_handler *handler,
141 void *private_result_data)
143 struct osl_object result;
145 /* must attach r/w as result.data might get encrypted in-place. */
146 int ret2, ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
147 struct callback_result *cr = result_shm;
150 PARA_ERROR_LOG("attach failed: %s\n", para_strerror(-ret));
153 result.size = cr->result_size;
154 result.data = result_shm + sizeof(*cr);
156 ret = handler(&result, cr->band, private_result_data);
157 ret2 = shm_detach(result_shm);
159 PARA_ERROR_LOG("detach failed: %s\n", para_strerror(-ret2));
167 * Ask the afs process to call a given function.
169 * \param f The function to be called.
170 * \param query Pointer to arbitrary data for the callback.
171 * \param result_handler Called for each shm area sent by the callback.
172 * \param private_result_data Passed verbatim to \a result_handler.
174 * This function creates a socket for communication with the afs process and a
175 * shared memory area (sma) to which the buffer pointed to by \a query is
176 * copied. It then notifies the afs process that the callback function \a f
177 * should be executed by sending the shared memory identifier (shmid) to the
180 * If the callback produces a result, it sends any number of shared memory
181 * identifiers back via the socket. For each such identifier received, \a
182 * result_handler is called. The contents of the sma identified by the received
183 * shmid are passed to that function as an osl object. The private_result_data
184 * pointer is passed as the second argument to \a result_handler.
186 * \return Number of shared memory areas dispatched on success, negative on
189 int send_callback_request(afs_callback *f, struct osl_object *query,
190 callback_result_handler *result_handler,
191 void *private_result_data)
193 struct callback_query *cq;
194 int ret, fd = -1, query_shmid, result_shmid;
196 char buf[sizeof(afs_socket_cookie) + sizeof(int)];
197 size_t query_shm_size = sizeof(*cq);
198 int dispatch_error = 0, num_dispatched = 0;
201 query_shm_size += query->size;
202 ret = shm_new(query_shm_size);
206 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
211 cq->query_size = query_shm_size - sizeof(*cq);
214 memcpy(query_shm + sizeof(*cq), query->data, query->size);
215 ret = shm_detach(query_shm);
219 *(uint32_t *)buf = afs_socket_cookie;
220 *(int *)(buf + sizeof(afs_socket_cookie)) = query_shmid;
222 ret = connect_local_socket(OPT_STRING_VAL(AFS_SOCKET));
226 ret = write_all(fd, buf, sizeof(buf));
230 * Read all shmids from afs.
232 * Even if the dispatcher returns an error we _must_ continue to read
233 * shmids from fd so that we can destroy all shared memory areas that
234 * have been created for us by the afs process.
237 ret = recv_bin_buffer(fd, buf, sizeof(int));
240 assert(ret == sizeof(int));
244 ret = dispatch_result(result_shmid, result_handler,
245 private_result_data);
246 if (ret < 0 && dispatch_error >= 0)
247 dispatch_error = ret;
248 ret = shm_destroy(result_shmid);
250 PARA_CRIT_LOG("destroy result failed: %s\n",
251 para_strerror(-ret));
255 if (shm_destroy(query_shmid) < 0)
256 PARA_CRIT_LOG("shm destroy error\n");
259 if (dispatch_error < 0)
260 return dispatch_error;
263 return num_dispatched;
267 * Wrapper for send_callback_request() which passes a lopsub parse result.
269 * \param f The callback function.
270 * \param cmd Needed for (de-)serialization.
271 * \param lpr Must match cmd.
272 * \param private_result_data Passed to send_callback_request().
274 * This function serializes the parse result given by the lpr pointer into a
275 * buffer. The buffer is sent as the query to the afs process with the callback
278 * \return The return value of the underlying call to send_callback_request().
280 int send_lls_callback_request(afs_callback *f,
281 const struct lls_command * const cmd,
282 struct lls_parse_result *lpr, void *private_result_data)
284 struct osl_object query;
286 int ret = lls_serialize_parse_result(lpr, cmd, &buf, &query.size);
290 ret = send_callback_request(f, &query, afs_cb_result_handler,
291 private_result_data);
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;
303 ret = osl(osl_get_object(pmd->table, row, pmd->match_col_num,
307 name = (char *)name_obj.data;
308 if ((!name || !*name) && (pmd->pm_flags & PM_SKIP_EMPTY_NAME))
310 if (lls_num_inputs(pmd->lpr) == 0) {
311 if (pmd->pm_flags & PM_NO_PATTERN_MATCHES_EVERYTHING) {
313 return pmd->action(pmd->table, row, name, pmd->data);
318 if (i >= lls_num_inputs(pmd->lpr))
320 p = lls_input(i, pmd->lpr);
321 ret = fnmatch(p, name, pmd->fnmatch_flags);
322 if (ret != FNM_NOMATCH) {
325 ret = pmd->action(pmd->table, row, name, pmd->data);
337 * Execute the given function for each matching row.
339 * \param pmd Describes what to match and how.
343 int for_each_matching_row(struct pattern_match_data *pmd)
345 if (pmd->pm_flags & PM_REVERSE_LOOP)
346 return osl(osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
347 action_if_pattern_matches));
348 return osl(osl_rbtree_loop(pmd->table, pmd->loop_col_num, pmd,
349 action_if_pattern_matches));
353 * Compare two osl objects of string type.
355 * \param obj1 Pointer to the first object.
356 * \param obj2 Pointer to the second object.
358 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
359 * are taken into account.
361 * \return It returns an integer less than, equal to, or greater than zero if
362 * \a obj1 is found, respectively, to be less than, to match, or be greater than
365 * \sa strcmp(3), strncmp(3).
367 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
369 const char *str1 = obj1->data;
370 const char *str2 = obj2->data;
371 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
374 static int pass_afd(int fd, char *buf, size_t size)
376 struct msghdr msg = {.msg_iov = NULL};
377 struct cmsghdr *cmsg;
378 char control[255] __a_aligned(8);
388 msg.msg_control = control;
389 msg.msg_controllen = sizeof(control);
391 cmsg = CMSG_FIRSTHDR(&msg);
392 cmsg->cmsg_level = SOL_SOCKET;
393 cmsg->cmsg_type = SCM_RIGHTS;
394 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
395 *(int *)CMSG_DATA(cmsg) = fd;
397 /* Sum of the length of all control messages in the buffer */
398 msg.msg_controllen = cmsg->cmsg_len;
399 PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size, fd);
400 ret = sendmsg(server_socket, &msg, 0);
402 ret = -ERRNO_TO_PARA_ERROR(errno);
409 * Pass the fd of the next audio file to the server process.
411 * This stores all information for streaming the "best" audio file in a shared
412 * memory area. The id of that area and an open file descriptor for the next
413 * audio file are passed to the server process.
417 * \sa \ref open_and_update_audio_file().
419 static int open_next_audio_file(void)
424 ret = open_and_update_audio_file(&fd);
426 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
428 goto no_admissible_files;
431 if (!write_ok(server_socket)) {
435 *(uint32_t *)buf = NEXT_AUDIO_FILE;
436 *(uint32_t *)(buf + 4) = (uint32_t)shmid;
437 ret = pass_afd(fd, buf, 8);
445 *(uint32_t *)buf = NO_ADMISSIBLE_FILES;
446 *(uint32_t *)(buf + 4) = (uint32_t)0;
447 return write_all(server_socket, buf, 8);
450 static int activate_mood_or_playlist(const char *arg, int *num_admissible,
457 mode = PLAY_MODE_MOOD;
458 ret = change_current_mood(NULL, errmsg);
465 if (!strncmp(arg, "p/", 2)) {
466 ret = playlist_open(arg + 2);
467 if (ret < 0 && errmsg)
468 *errmsg = make_message( "could not open %s",
470 mode = PLAY_MODE_PLAYLIST;
471 } else if (!strncmp(arg, "m/", 2)) {
472 ret = change_current_mood(arg + 2, errmsg);
473 mode = PLAY_MODE_MOOD;
476 *errmsg = make_message("%s: parse error", arg);
477 return -ERRNO_TO_PARA_ERROR(EINVAL);
483 *num_admissible = ret;
484 current_play_mode = mode;
485 if (arg != current_mop) {
488 current_mop = para_strdup(arg);
489 mutex_lock(mmd_mutex);
490 strncpy(mmd->afs_mode_string, arg,
491 sizeof(mmd->afs_mode_string));
492 mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
494 mutex_unlock(mmd_mutex);
496 mutex_lock(mmd_mutex);
497 strcpy(mmd->afs_mode_string, "dummy");
499 mutex_unlock(mmd_mutex);
507 * Result handler for sending data to the para_client process.
509 * \param result The data to be sent.
510 * \param band The band designator.
511 * \param private Pointer to the command context.
513 * \return The return value of the underlying call to \ref command.c::send_sb.
515 * \sa \ref callback_result_handler, \ref command.c::send_sb.
517 int afs_cb_result_handler(struct osl_object *result, uint8_t band,
520 struct command_context *cc = private;
528 case SBD_WARNING_LOG:
532 assert(result->size > 0);
533 return send_sb(&cc->scc, result->data, result->size, band, true);
534 case SBD_AFS_CB_FAILURE:
535 return *(int *)(result->data);
541 static void flush_and_free_pb(struct para_buffer *pb)
544 struct afs_max_size_handler_data *amshd = pb->private_data;
546 if (pb->buf && pb->size > 0) {
547 ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
550 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
555 static int com_select_callback(struct afs_callback_arg *aca)
557 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
559 int num_admissible, ret;
562 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
564 arg = lls_input(0, aca->lpr);
565 ret = clear_score_table();
567 para_printf(&aca->pbout, "could not clear score table\n");
570 if (current_play_mode == PLAY_MODE_MOOD)
571 close_current_mood();
574 ret = activate_mood_or_playlist(arg, &num_admissible, &errmsg);
577 /* ignore subsequent errors (but log them) */
578 para_printf(&aca->pbout, "%s\n", errmsg);
580 para_printf(&aca->pbout, "could not activate %s\n", arg);
581 if (current_mop && strcmp(current_mop, arg) != 0) {
583 para_printf(&aca->pbout, "switching back to %s\n", current_mop);
584 ret2 = activate_mood_or_playlist(current_mop, &num_admissible,
588 para_printf(&aca->pbout, "%s\n", errmsg);
590 para_printf(&aca->pbout, "could not reactivate %s: %s\n",
591 current_mop, para_strerror(-ret2));
593 para_printf(&aca->pbout, "activating dummy mood\n");
594 activate_mood_or_playlist(NULL, &num_admissible, NULL);
596 para_printf(&aca->pbout, "activated %s (%d admissible file%s)\n",
597 current_mop? current_mop : "dummy mood", num_admissible,
598 num_admissible == 1? "" : "s");
600 lls_free_parse_result(aca->lpr, cmd);
604 static int com_select(struct command_context *cc, struct lls_parse_result *lpr)
606 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
608 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
611 send_errctx(cc, errctx);
614 return send_lls_callback_request(com_select_callback, cmd, lpr, cc);
616 EXPORT_SERVER_CMD_HANDLER(select);
618 static void init_admissible_files(const char *arg)
620 int ret = activate_mood_or_playlist(arg, NULL, NULL);
622 PARA_WARNING_LOG("could not activate %s: %s\n", arg,
623 para_strerror(-ret));
625 activate_mood_or_playlist(NULL, NULL, NULL);
629 static int setup_command_socket_or_die(void)
632 const char *socket_name = OPT_STRING_VAL(AFS_SOCKET);
635 ret = create_local_socket(socket_name);
637 PARA_EMERG_LOG("%s: %s\n", para_strerror(-ret), socket_name);
641 PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
646 static char *database_dir;
648 static void close_afs_tables(void)
651 PARA_NOTICE_LOG("closing afs_tables\n");
652 for (i = 0; i < NUM_AFS_TABLES; i++)
653 afs_tables[i].close();
658 static void get_database_dir(void)
661 if (OPT_GIVEN(AFS_DATABASE_DIR))
662 database_dir = para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR));
664 char *home = para_homedir();
665 database_dir = make_message(
666 "%s/.paraslash/afs_database-0.4", home);
670 PARA_INFO_LOG("afs_database dir %s\n", database_dir);
673 static int make_database_dir(void)
678 ret = para_mkdir(database_dir, 0777);
679 if (ret >= 0 || ret == -ERRNO_TO_PARA_ERROR(EEXIST))
684 static int open_afs_tables(void)
689 PARA_NOTICE_LOG("opening %d osl tables in %s\n", NUM_AFS_TABLES,
691 for (i = 0; i < NUM_AFS_TABLES; i++) {
692 ret = afs_tables[i].open(database_dir);
695 PARA_ERROR_LOG("could not open %s\n", afs_tables[i].name);
701 afs_tables[--i].close();
705 static int afs_signal_post_select(struct sched *s, __a_unused void *context)
709 if (getppid() == 1) {
710 PARA_EMERG_LOG("para_server died\n");
713 signum = para_next_signal(&s->rfds);
716 if (signum == SIGHUP) {
718 parse_config_or_die(1);
719 ret = open_afs_tables();
722 init_admissible_files(current_mop);
725 PARA_EMERG_LOG("terminating on signal %d\n", signum);
727 task_notify_all(s, E_AFS_SIGNAL);
728 return -E_AFS_SIGNAL;
731 static void register_signal_task(struct sched *s)
733 para_sigaction(SIGPIPE, SIG_IGN);
734 signal_task = signal_init_or_die();
735 para_install_sighandler(SIGINT);
736 para_install_sighandler(SIGTERM);
737 para_install_sighandler(SIGHUP);
739 signal_task->task = task_register(&(struct task_info) {
741 .pre_select = signal_pre_select,
742 .post_select = afs_signal_post_select,
743 .context = signal_task,
748 static struct list_head afs_client_list;
750 /** Describes one connected afs client. */
752 /** Position in the afs client list. */
753 struct list_head node;
754 /** The socket file descriptor for this client. */
756 /** The time the client connected. */
757 struct timeval connect_time;
760 static void command_pre_select(struct sched *s, void *context)
762 struct command_task *ct = context;
763 struct afs_client *client;
765 para_fd_set(server_socket, &s->rfds, &s->max_fileno);
766 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
767 list_for_each_entry(client, &afs_client_list, node)
768 para_fd_set(client->fd, &s->rfds, &s->max_fileno);
772 * Send data as shared memory to a file descriptor.
774 * \param fd File descriptor to send the shmid to.
775 * \param band The band designator for this data.
776 * \param buf The buffer holding the data to be sent.
777 * \param size The size of \a buf.
779 * This function creates a shared memory area large enough to hold
780 * the content given by \a buf and \a size and sends the identifier
781 * of this area to the file descriptor \a fd.
783 * It is called by the AFS max_size handler as well as directly by the AFS
784 * command callbacks to send command output to the command handlers.
786 * \return Zero if \a buf is \p NULL or \a size is zero. Negative on errors,
787 * and positive on success.
789 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size)
793 struct callback_result *cr;
796 assert(band != SBD_OUTPUT);
797 ret = shm_new(size + sizeof(*cr));
801 ret = shm_attach(shmid, ATTACH_RW, &shm);
805 cr->result_size = size;
808 memcpy(shm + sizeof(*cr), buf, size);
809 ret = shm_detach(shm);
812 ret = write_all(fd, (char *)&shmid, sizeof(int));
816 if (shm_destroy(shmid) < 0)
817 PARA_ERROR_LOG("destroy result failed\n");
821 static int call_callback(int fd, int query_shmid)
824 struct callback_query *cq;
826 struct afs_callback_arg aca = {.fd = fd};
828 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
832 aca.query.data = (char *)query_shm + sizeof(*cq);
833 aca.query.size = cq->query_size;
834 aca.pbout.max_size = shm_get_shmmax();
835 aca.pbout.max_size_handler = afs_max_size_handler;
836 aca.pbout.private_data = &(struct afs_max_size_handler_data) {
840 ret = cq->handler(&aca);
841 ret2 = shm_detach(query_shm);
843 if (ret < 0) /* ignore (but log) detach error */
844 PARA_ERROR_LOG("could not detach sma: %s\n",
845 para_strerror(-ret2));
849 flush_and_free_pb(&aca.pbout);
851 ret2 = pass_buffer_as_shm(fd, SBD_AFS_CB_FAILURE,
852 (const char *)&ret, sizeof(ret));
854 PARA_ERROR_LOG("could not pass cb failure packet: %s\n",
855 para_strerror(-ret));
860 static int execute_server_command(fd_set *rfds)
864 int ret = read_nonblock(server_socket, buf, sizeof(buf) - 1, rfds, &n);
866 if (ret < 0 || n == 0)
869 if (strcmp(buf, "new"))
870 return -ERRNO_TO_PARA_ERROR(EINVAL);
871 return open_next_audio_file();
874 /* returns 0 if no data available, 1 else */
875 static int execute_afs_command(int fd, fd_set *rfds)
879 char buf[sizeof(cookie) + sizeof(query_shmid)];
881 int ret = read_nonblock(fd, buf, sizeof(buf), rfds, &n);
887 if (n != sizeof(buf)) {
888 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
889 ret, (long unsigned) sizeof(buf));
892 cookie = *(uint32_t *)buf;
893 if (cookie != afs_socket_cookie) {
894 PARA_NOTICE_LOG("received invalid cookie (got %u, expected %u)\n",
895 (unsigned)cookie, (unsigned)afs_socket_cookie);
898 query_shmid = *(int *)(buf + sizeof(cookie));
899 if (query_shmid < 0) {
900 PARA_WARNING_LOG("received invalid query shmid %d)\n",
904 ret = call_callback(fd, query_shmid);
908 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
912 /** Shutdown connection if query has not arrived until this many seconds. */
913 #define AFS_CLIENT_TIMEOUT 3
915 static int command_post_select(struct sched *s, void *context)
917 struct command_task *ct = context;
918 struct sockaddr_un unix_addr;
919 struct afs_client *client, *tmp;
922 ret = task_get_notification(ct->task);
925 ret = execute_server_command(&s->rfds);
927 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
928 task_notify_all(s, -ret);
931 /* Check the list of connected clients. */
932 list_for_each_entry_safe(client, tmp, &afs_client_list, node) {
933 ret = execute_afs_command(client->fd, &s->rfds);
934 if (ret == 0) { /* prevent bogus connection flooding */
936 tv_diff(now, &client->connect_time, &diff);
937 if (diff.tv_sec < AFS_CLIENT_TIMEOUT)
939 PARA_WARNING_LOG("connection timeout\n");
942 list_del(&client->node);
945 /* Accept connections on the local socket. */
946 ret = para_accept(ct->fd, &s->rfds, &unix_addr, sizeof(unix_addr), &fd);
948 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
951 ret = mark_fd_nonblocking(fd);
953 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
957 client = para_malloc(sizeof(*client));
959 client->connect_time = *now;
960 para_list_add(&client->node, &afs_client_list);
964 static void register_command_task(struct sched *s)
966 struct command_task *ct = &command_task_struct;
967 ct->fd = setup_command_socket_or_die();
969 ct->task = task_register(&(struct task_info) {
970 .name = "afs command",
971 .pre_select = command_pre_select,
972 .post_select = command_post_select,
978 * Initialize the audio file selector process.
980 * \param socket_fd File descriptor used for communication with the server.
982 __noreturn void afs_init(int socket_fd)
984 static struct sched s;
987 register_signal_task(&s);
988 INIT_LIST_HEAD(&afs_client_list);
989 for (i = 0; i < NUM_AFS_TABLES; i++)
990 afs_tables[i].init(&afs_tables[i]);
991 ret = open_afs_tables();
994 server_socket = socket_fd;
995 ret = mark_fd_nonblocking(server_socket);
998 PARA_INFO_LOG("server_socket: %d\n", server_socket);
999 init_admissible_files(OPT_STRING_VAL(AFS_INITIAL_MODE));
1000 register_command_task(&s);
1001 s.default_timeout.tv_sec = 0;
1002 s.default_timeout.tv_usec = 999 * 1000;
1003 ret = write(socket_fd, "\0", 1);
1007 ret = -ERRNO_TO_PARA_ERROR(errno);
1012 close_current_mood();
1016 signal_shutdown(signal_task);
1017 free_status_items();
1021 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1025 static int com_init_callback(struct afs_callback_arg *aca)
1027 uint32_t table_mask = *(uint32_t *)aca->query.data;
1032 for (i = 0; i < NUM_AFS_TABLES; i++) {
1033 struct afs_table *t = &afs_tables[i];
1035 if (!(table_mask & (1 << i)))
1039 ret = t->create(database_dir);
1041 para_printf(&aca->pbout, "cannot create table %s\n",
1045 para_printf(&aca->pbout, "successfully created %s table\n",
1048 ret = open_afs_tables();
1050 para_printf(&aca->pbout, "cannot open afs tables: %s\n",
1051 para_strerror(-ret));
1056 static int com_init(struct command_context *cc, struct lls_parse_result *lpr)
1059 uint32_t table_mask = (1 << (NUM_AFS_TABLES + 1)) - 1;
1060 struct osl_object query = {.data = &table_mask,
1061 .size = sizeof(table_mask)};
1062 unsigned num_inputs = lls_num_inputs(lpr);
1064 ret = make_database_dir();
1067 if (num_inputs > 0) {
1069 for (i = 0; i < num_inputs; i++) {
1070 for (j = 0; j < NUM_AFS_TABLES; j++) {
1071 struct afs_table *t = &afs_tables[j];
1073 if (strcmp(lls_input(i, lpr), t->name))
1075 table_mask |= (1 << j);
1078 if (j == NUM_AFS_TABLES)
1079 return -E_BAD_TABLE_NAME;
1082 return send_callback_request(com_init_callback, &query,
1083 afs_cb_result_handler, cc);
1085 EXPORT_SERVER_CMD_HANDLER(init);
1087 static int com_check(struct command_context *cc, struct lls_parse_result *lpr)
1089 const struct lls_opt_result *r_a = SERVER_CMD_OPT_RESULT(CHECK, AFT, lpr);
1090 const struct lls_opt_result *r_A = SERVER_CMD_OPT_RESULT(CHECK, ATTRIBUTE, lpr);
1091 const struct lls_opt_result *r_m = SERVER_CMD_OPT_RESULT(CHECK, MOOD, lpr);
1092 const struct lls_opt_result *r_p = SERVER_CMD_OPT_RESULT(CHECK, PLAYLIST, lpr);
1093 bool noopt = !lls_opt_given(r_a) && !lls_opt_given(r_A)
1094 && !lls_opt_given(r_m) && !lls_opt_given(r_p);
1097 if (noopt || lls_opt_given(r_a)) {
1098 ret = send_callback_request(aft_check_callback, NULL,
1099 afs_cb_result_handler, cc);
1103 if (noopt || lls_opt_given(r_A)) {
1104 ret = send_callback_request(attribute_check_callback, NULL,
1105 afs_cb_result_handler, cc);
1109 if (noopt || lls_opt_given(r_p)) {
1110 ret = send_callback_request(playlist_check_callback,
1111 NULL, afs_cb_result_handler, cc);
1115 if (noopt || lls_opt_given(r_m)) {
1116 ret = send_callback_request(mood_check_callback, NULL,
1117 afs_cb_result_handler, cc);
1123 EXPORT_SERVER_CMD_HANDLER(check);
1126 * The afs event dispatcher.
1128 * \param event Type of the event.
1129 * \param pb May be \p NULL.
1130 * \param data Type depends on \a event.
1132 * This function calls each table event handler, passing \a pb and \a data
1133 * verbatim. It's up to the handlers to interpret the \a data pointer. If a
1134 * handler returns negative, the loop is aborted.
1136 * \return The (negative) error code of the first handler that failed, or non-negative
1137 * if all handlers succeeded.
1139 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
1144 for (i = 0; i < NUM_AFS_TABLES; i++) {
1145 struct afs_table *t = &afs_tables[i];
1146 if (!t->event_handler)
1148 ret = t->event_handler(event, pb, data);
1150 PARA_CRIT_LOG("table %s, event %u: %s\n", t->name,
1151 event, para_strerror(-ret));
1159 * Dummy event handler for the images table.
1161 * \param event Unused.
1163 * \param data Unused.
1165 * \return The images table does not honor events, so this handler always
1168 __a_const int images_event_handler(__a_unused enum afs_events event,
1169 __a_unused struct para_buffer *pb, __a_unused void *data)
1175 * Dummy event handler for the lyrics table.
1177 * \param event Unused.
1179 * \param data Unused.
1181 * \return The lyrics table does not honor events, so this handler always
1184 __a_const int lyrics_event_handler(__a_unused enum afs_events event,
1185 __a_unused struct para_buffer *pb, __a_unused void *data)