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. */
10 #include "server.cmdline.h"
15 #include <dirent.h> /* readdir() */
27 /** The osl tables used by afs. \sa blob.c. */
29 /** Contains audio file information. See aft.c. */
31 /** The table for the paraslash attributes. See attribute.c. */
34 * Paraslash's scoring system is based on Gaussian normal
35 * distributions, and the relevant data is stored in the rbtrees of an
36 * osl table containing only volatile columns. See score.c for
41 * A standard blob table containing the mood definitions. For details
45 /** A blob table containing lyrics on a per-song basis. */
47 /** Another blob table for images (for example album cover art). */
49 /** Yet another blob table for storing standard playlists. */
51 /** How many tables are in use? */
55 static struct table_info afs_tables[NUM_AFS_TABLES];
58 /** The file descriptor for the local socket. */
61 * Value sent by the command handlers to identify themselves as
62 * children of the running para_server.
65 /** The associated task structure. */
70 * A random number used to "authenticate" the connection.
72 * para_server picks this number by random before forking the afs process. The
73 * command handlers write this number together with the id of the shared memory
74 * area containing the query. This way, a malicious local user has to know this
75 * number to be able to cause the afs process to crash by sending fake queries.
77 extern uint32_t afs_socket_cookie;
80 * Struct to let command handlers execute a callback in afs context.
82 * Commands that need to change the state of afs can't change the relevant data
83 * structures directly because commands are executed in a child process, i.e.
84 * they get their own virtual address space.
86 * This structure is used by \p send_callback_request() (executed from handler
87 * context) in order to let the afs process call the specified function. An
88 * instance of that structure is written to a shared memory area together with
89 * the arguments to the callback function. The identifier of the shared memory
90 * area is written to the command socket.
92 * The afs process accepts connections on the command socket and reads the
93 * shared memory id, attaches the corresponing area, calls the given handler to
94 * perform the desired action and to optionally compute a result.
96 * The result and a \p callback_result structure is then written to another
97 * shared memory area. The identifier for that area is written to the handler's
98 * command socket, so that the handler process can read the id, attach the
99 * shared memory area and use the result.
101 * \sa struct callback_result.
103 struct callback_query {
104 /** The function to be called. */
105 callback_function *handler;
106 /** The number of bytes of the query */
111 * Structure embedded in the result of a callback.
113 * If the callback produced a result, an instance of that structure is embeeded
114 * into the shared memory area holding the result, mainly to let the command
115 * handler know the size of the result.
117 * \sa struct callback_query.
119 struct callback_result {
120 /** The number of bytes of the result. */
125 * Ask the parent process to call a given function.
127 * \param f The function to be called.
128 * \param query Pointer to arbitrary data for the callback.
129 * \param result Callback result will be stored here.
131 * This function creates a shared memory area, copies the buffer pointed to by
132 * \a buf to that area and notifies the afs process that \a f should be
135 * \return Negative, on errors, the return value of the callback function
138 * \sa send_option_arg_callback_request(), send_standard_callback_request().
140 int send_callback_request(callback_function *f, struct osl_object *query,
141 struct osl_object *result)
143 struct callback_query *cq;
144 struct callback_result *cr;
145 int ret, fd = -1, query_shmid, result_shmid;
146 void *query_shm, *result_shm;
147 char buf[sizeof(afs_socket_cookie) + sizeof(int)];
148 struct sockaddr_un unix_addr;
149 size_t query_shm_size = sizeof(*cq);
152 query_shm_size += query->size;
153 ret = shm_new(query_shm_size);
157 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
162 cq->query_size = query_shm_size - sizeof(*cq);
165 memcpy(query_shm + sizeof(*cq), query->data, query->size);
166 ret = shm_detach(query_shm);
170 *(uint32_t *) buf = afs_socket_cookie;
171 *(int *) (buf + sizeof(afs_socket_cookie)) = query_shmid;
173 ret = get_stream_socket(PF_UNIX);
177 ret = init_unix_addr(&unix_addr, conf.afs_socket_arg);
180 ret = PARA_CONNECT(fd, &unix_addr);
183 ret = send_bin_buffer(fd, buf, sizeof(buf));
186 ret = recv_bin_buffer(fd, buf, sizeof(buf));
189 if (ret != sizeof(int)) {
197 ret = shm_attach(result_shmid, ATTACH_RO, &result_shm);
201 result->size = cr->result_size;
202 result->data = para_malloc(result->size);
203 memcpy(result->data, result_shm + sizeof(*cr), result->size);
204 ret = shm_detach(result_shm);
206 PARA_ERROR_LOG("can not detach result\n");
208 PARA_ERROR_LOG("attach result failed: %d\n", ret);
209 if (shm_destroy(result_shmid) < 0)
210 PARA_ERROR_LOG("destroy result failed\n");
213 if (shm_destroy(query_shmid) < 0)
214 PARA_ERROR_LOG("%s\n", "shm destroy error");
217 // PARA_DEBUG_LOG("callback_ret: %d\n", ret);
222 * Send a callback request passing an options structure and an argument vector.
224 * \param options pointer to an arbitrary data structure.
225 * \param argc Argument count.
226 * \param argv Standard argument vector.
227 * \param f The callback function.
228 * \param result The result of the query is stored here.
230 * Some commands have a couple of options that are parsed in child context for
231 * syntactic correctness and are stored in a special options structure for that
232 * command. This function allows to pass such a structure together with a list
233 * of further arguments (often a list of audio files) to the parent process.
235 * \sa send_standard_callback_request(), send_callback_request().
237 int send_option_arg_callback_request(struct osl_object *options,
238 int argc, char * const * const argv, callback_function *f,
239 struct osl_object *result)
243 struct osl_object query = {.size = options? options->size : 0};
245 for (i = 0; i < argc; i++)
246 query.size += strlen(argv[i]) + 1;
247 query.data = para_malloc(query.size);
250 memcpy(query.data, options->data, options->size);
253 for (i = 0; i < argc; i++) {
254 strcpy(p, argv[i]); /* OK */
255 p += strlen(argv[i]) + 1;
257 ret = send_callback_request(f, &query, result);
263 * Send a callback request with an argument vector only.
265 * \param argc The same meaning as in send_option_arg_callback_request().
266 * \param argv The same meaning as in send_option_arg_callback_request().
267 * \param f The same meaning as in send_option_arg_callback_request().
268 * \param result The same meaning as in send_option_arg_callback_request().
270 * This is similar to send_option_arg_callback_request(), but no options buffer
271 * is passed to the parent process.
273 * \return The return value of the underlying call to
274 * send_option_arg_callback_request().
276 int send_standard_callback_request(int argc, char * const * const argv,
277 callback_function *f, struct osl_object *result)
279 return send_option_arg_callback_request(NULL, argc, argv, f, result);
282 static int action_if_pattern_matches(struct osl_row *row, void *data)
284 struct pattern_match_data *pmd = data;
285 struct osl_object name_obj;
286 const char *p, *name;
287 int ret = osl_get_object(pmd->table, row, pmd->match_col_num, &name_obj);
288 const char *pattern_txt = (const char *)pmd->patterns.data;
292 name = (char *)name_obj.data;
293 if ((!name || !*name) && (pmd->pm_flags & PM_SKIP_EMPTY_NAME))
295 if (!pmd->patterns.size && (pmd->pm_flags & PM_NO_PATTERN_MATCHES_EVERYTHING))
296 return pmd->action(pmd->table, row, name, pmd->data);
297 for (p = pattern_txt; p < pattern_txt + pmd->patterns.size;
298 p += strlen(p) + 1) {
299 ret = fnmatch(p, name, pmd->fnmatch_flags);
300 if (ret == FNM_NOMATCH)
304 return pmd->action(pmd->table, row, name, pmd->data);
309 int for_each_matching_row(struct pattern_match_data *pmd)
311 if (pmd->pm_flags & PM_REVERSE_LOOP)
312 return osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
313 action_if_pattern_matches);
314 return osl_rbtree_loop(pmd->table, pmd->loop_col_num, pmd,
315 action_if_pattern_matches);
319 * Compare two osl objects of string type.
321 * \param obj1 Pointer to the first object.
322 * \param obj2 Pointer to the second object.
324 * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
325 * are taken into account.
327 * \return It returns an integer less than, equal to, or greater than zero if
328 * \a obj1 is found, respectively, to be less than, to match, or be greater than
331 * \sa strcmp(3), strncmp(3), osl_compare_func.
333 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
335 const char *str1 = (const char *)obj1->data;
336 const char *str2 = (const char *)obj2->data;
337 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
341 * write input from fd to dynamically allocated buffer,
342 * but maximal max_size byte.
344 static int fd2buf(int fd, unsigned max_size, struct osl_object *obj)
346 const size_t chunk_size = 1024;
347 size_t size = 2048, received = 0;
349 char *buf = para_malloc(size);
352 ret = recv_bin_buffer(fd, buf + received, chunk_size);
356 if (received + chunk_size >= size) {
358 ret = -E_INPUT_TOO_LARGE;
361 buf = para_realloc(buf, size);
365 obj->size = received;
372 * Read data from a file descriptor, and send it to the afs process.
374 * \param fd File descriptor to read data from.
375 * \param arg_obj Pointer to the arguments to \a f.
376 * \param f The callback function.
377 * \param max_len Don't read more than that many bytes from stdin.
378 * \param result The result of the query is stored here.
380 * This function is used by commands that wish to let para_server store
381 * arbitrary data specified by the user (for instance the add_blob family of
382 * commands). First, at most \a max_len bytes are read from \a fd, the result
383 * is concatenated with the buffer given by \a arg_obj, and the combined buffer
384 * is made available to the parent process via shared memory.
386 * \return Negative on errors, the return value of the underlying call to
387 * send_callback_request() otherwise.
389 int stdin_command(int fd, struct osl_object *arg_obj, callback_function *f,
390 unsigned max_len, struct osl_object *result)
392 struct osl_object query, stdin_obj;
395 ret = send_buffer(fd, AWAITING_DATA_MSG);
398 ret = fd2buf(fd, max_len, &stdin_obj);
401 query.size = arg_obj->size + stdin_obj.size;
402 query.data = para_malloc(query.size);
403 memcpy(query.data, arg_obj->data, arg_obj->size);
404 memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
405 free(stdin_obj.data);
406 ret = send_callback_request(f, &query, result);
412 * Open the audio file with highest score.
414 * \param afd Audio file data is returned here.
416 * This stores all information for streaming the "best" audio file
417 * in the \a afd structure.
419 * \return Positive on success, negative on errors.
421 * \sa close_audio_file(), open_and_update_audio_file().
423 int open_next_audio_file(struct audio_file_data *afd)
425 struct osl_row *aft_row;
428 ret = score_get_best(&aft_row, &afd->score);
431 ret = open_and_update_audio_file(aft_row, afd);
438 * Free all resources which were allocated by open_next_audio_file().
440 * \param afd The structure previously filled in by open_next_audio_file().
442 * \return The return value of the underlying call to para_munmap().
444 * \sa open_next_audio_file().
446 int close_audio_file(struct audio_file_data *afd)
448 free(afd->afhi.chunk_table);
449 return para_munmap(afd->map.data, afd->map.size);
453 static void play_loop(enum play_mode current_play_mode)
456 struct audio_file_data afd;
458 afd.current_play_mode = current_play_mode;
459 for (i = 0; i < 0; i++) {
460 ret = open_next_audio_file(&afd);
462 PARA_ERROR_LOG("failed to open next audio file: %d\n", ret);
465 PARA_NOTICE_LOG("next audio file: %s, score: %li\n", afd.path, afd.score);
467 close_audio_file(&afd);
473 static enum play_mode init_admissible_files(void)
476 char *given_mood, *given_playlist;
478 given_mood = "mood_that_was_given_at_the_command_line";
479 given_playlist = "given_playlist";
482 ret = change_current_mood(given_mood);
485 PARA_WARNING_LOG("ignoring playlist %s\n",
487 return PLAY_MODE_MOOD;
490 if (given_playlist) {
491 ret = playlist_open(given_playlist);
493 return PLAY_MODE_PLAYLIST;
495 ret = change_current_mood(NULL); /* open first available mood */
497 return PLAY_MODE_MOOD;
498 change_current_mood(""); /* open dummy mood, always successful */
499 return PLAY_MODE_MOOD;
502 static int setup_command_socket_or_die(void)
505 char *socket_name = conf.afs_socket_arg;
506 struct sockaddr_un unix_addr;
509 ret = create_local_socket(socket_name, &unix_addr,
510 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
512 PARA_EMERG_LOG("%s: %s\n", PARA_STRERROR(-ret), socket_name);
515 if (listen(ret , 5) < 0) {
516 PARA_EMERG_LOG("%s", "can not listen on socket\n");
519 PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name,
524 static int server_socket;
525 static struct command_task command_task_struct;
526 static struct signal_task signal_task_struct;
528 static void unregister_tasks(void)
530 unregister_task(&command_task_struct.task);
531 unregister_task(&signal_task_struct.task);
534 static void close_afs_tables(enum osl_close_flags flags)
536 PARA_NOTICE_LOG("closing afs_tables\n");
537 score_shutdown(flags);
538 attribute_shutdown(flags);
539 close_current_mood();
541 moods_shutdown(flags);
542 playlists_shutdown(flags);
543 lyrics_shutdown(flags);
544 images_shutdown(flags);
548 static void signal_pre_select(struct sched *s, struct task *t)
550 struct signal_task *st = t->private_data;
552 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
555 static void signal_post_select(struct sched *s, struct task *t)
557 struct signal_task *st = t->private_data;
559 if (!FD_ISSET(st->fd, &s->rfds))
561 st->signum = para_next_signal();
563 if (st->signum == SIGUSR1)
564 return; /* ignore SIGUSR1 */
565 PARA_NOTICE_LOG("caught signal %d\n", st->signum);
566 t->ret = -E_SIGNAL_CAUGHT;
570 static void register_signal_task(void)
572 struct signal_task *st = &signal_task_struct;
573 st->fd = para_signal_init();
574 PARA_INFO_LOG("signal pipe: fd %d\n", st->fd);
575 para_install_sighandler(SIGINT);
576 para_install_sighandler(SIGTERM);
577 para_install_sighandler(SIGPIPE);
579 st->task.pre_select = signal_pre_select;
580 st->task.post_select = signal_post_select;
581 st->task.private_data = st;
582 sprintf(st->task.status, "signal task");
583 register_task(&st->task);
586 static void command_pre_select(struct sched *s, struct task *t)
588 struct command_task *ct = t->private_data;
590 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
594 * On errors, negative value is written to fd.
595 * On success: If query produced a result, the result_shmid is written to fd.
596 * Otherwise, zero is written.
598 static int call_callback(int fd, int query_shmid)
600 void *query_shm, *result_shm;
601 struct callback_query *cq;
602 struct callback_result *cr;
603 struct osl_object query, result = {.data = NULL};
604 int result_shmid = -1, ret, ret2;
606 ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
610 query.data = (char *)query_shm + sizeof(*cq);
611 query.size = cq->query_size;
612 ret = cq->handler(&query, &result);
613 ret2 = shm_detach(query_shm);
614 if (ret2 < 0 && ret >= 0)
619 if (!result.data || !result.size)
621 ret = shm_new(result.size + sizeof(struct callback_result));
625 ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
629 cr->result_size = result.size;
630 memcpy(result_shm + sizeof(*cr), result.data, result.size);
631 ret = shm_detach(result_shm);
637 ret2 = send_bin_buffer(fd, (char *)&ret, sizeof(int));
638 if (ret < 0 || ret2 < 0) {
639 if (result_shmid >= 0)
640 if (shm_destroy(result_shmid) < 0)
641 PARA_ERROR_LOG("destroy result failed\n");
648 static void command_post_select(struct sched *s, struct task *t)
650 struct command_task *ct = t->private_data;
651 struct sockaddr_un unix_addr;
652 char buf[sizeof(uint32_t) + sizeof(int)];
657 if (!FD_ISSET(ct->fd, &s->rfds))
659 t->ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
663 * The following errors may be caused by a malicious local user. So do
664 * not return an error in this case as this would terminate para_afs
668 /* FIXME: This is easily dosable (peer doesn't send data) */
669 t->ret = recv_bin_buffer(fd, buf, sizeof(buf));
671 PARA_NOTICE_LOG("%s (%d)\n", PARA_STRERROR(-t->ret), t->ret);
674 if (t->ret != sizeof(buf)) {
675 PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
676 t->ret, (long unsigned) sizeof(buf));
679 cookie = *(uint32_t *)buf;
680 if (cookie != ct->cookie) {
681 PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
682 (unsigned)cookie, (unsigned)ct->cookie);
685 query_shmid = *(int *)(buf + sizeof(cookie));
686 if (query_shmid < 0) {
687 PARA_WARNING_LOG("received invalid query shmid %d)\n",
691 /* Ignore return value: Errors might be ok here. */
692 call_callback(fd, query_shmid);
698 static void register_command_task(uint32_t cookie)
700 struct command_task *ct = &command_task_struct;
701 ct->fd = setup_command_socket_or_die();
704 ct->task.pre_select = command_pre_select;
705 ct->task.post_select = command_post_select;
706 ct->task.private_data = ct;
707 sprintf(ct->task.status, "command task");
708 register_task(&ct->task);
711 void register_tasks(uint32_t cookie)
713 register_signal_task();
714 register_command_task(cookie);
717 static char *database_dir;
719 static int make_database_dir(void)
724 if (conf.afs_database_dir_given)
725 database_dir = para_strdup(conf.afs_database_dir_arg);
727 char *home = para_homedir();
728 database_dir = make_message(
729 "%s/.paraslash/afs_database", home);
733 PARA_INFO_LOG("afs_database dir %s\n", database_dir);
734 ret = para_mkdir(database_dir, 0777);
735 if (ret >= 0 || ret == -E_EXIST)
742 static int open_afs_tables(void)
744 int ret = make_database_dir();
748 ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], database_dir);
751 ret = moods_init(&afs_tables[TBLNUM_MOODS], database_dir);
753 goto moods_init_error;
754 ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], database_dir);
756 goto playlists_init_error;
757 ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], database_dir);
759 goto lyrics_init_error;
760 ret = images_init(&afs_tables[TBLNUM_IMAGES], database_dir);
762 goto images_init_error;
763 ret = score_init(&afs_tables[TBLNUM_SCORES], database_dir);
765 goto score_init_error;
766 ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], database_dir);
772 score_shutdown(OSL_MARK_CLEAN);
774 images_shutdown(OSL_MARK_CLEAN);
776 lyrics_shutdown(OSL_MARK_CLEAN);
778 playlists_shutdown(OSL_MARK_CLEAN);
779 playlists_init_error:
780 moods_shutdown(OSL_MARK_CLEAN);
782 attribute_shutdown(OSL_MARK_CLEAN);
786 __noreturn int afs_init(uint32_t cookie, int socket_fd)
788 enum play_mode current_play_mode;
790 int ret = open_afs_tables();
793 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
796 server_socket = socket_fd;
797 ret = mark_fd_nonblock(server_socket);
800 PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
801 server_socket, (unsigned) cookie);
802 current_play_mode = init_admissible_files();
803 register_tasks(cookie);
804 s.default_timeout.tv_sec = 0;
805 s.default_timeout.tv_usec = 99 * 1000;
808 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
809 close_afs_tables(OSL_MARK_CLEAN);
813 static int create_tables_callback(const struct osl_object *query,
814 __a_unused struct osl_object *result)
816 uint32_t table_mask = *(uint32_t *)query->data;
819 close_afs_tables(OSL_MARK_CLEAN);
820 for (i = 0; i < NUM_AFS_TABLES; i++) {
821 struct table_info *ti = afs_tables + i;
823 if (ti->flags & TBLFLAG_SKIP_CREATE)
825 if (!(table_mask & (1 << i)))
827 ret = osl_create_table(ti->desc);
831 ret = open_afs_tables();
832 return ret < 0? ret: 0;
835 int com_init(int fd, int argc, char * const * const argv)
838 uint32_t table_mask = (1 << (NUM_AFS_TABLES + 1)) - 1;
839 struct osl_object query = {.data = &table_mask,
840 .size = sizeof(table_mask)};
844 for (i = 1; i < argc; i++) {
845 for (j = 0; j < NUM_AFS_TABLES; j++) {
846 struct table_info *ti = afs_tables + j;
848 if (ti->flags & TBLFLAG_SKIP_CREATE)
850 if (strcmp(argv[i], ti->desc->name))
852 table_mask |= (1 << j);
855 if (j == NUM_AFS_TABLES)
856 return -E_BAD_TABLE_NAME;
859 ret = send_callback_request(create_tables_callback, &query, NULL);
862 return send_va_buffer(fd, "successfully created afs table(s)\n");
865 enum com_check_flags {
871 int com_check(int fd, int argc, char * const * const argv)
875 struct osl_object result;
877 for (i = 1; i < argc; i++) {
878 const char *arg = argv[i];
881 if (!strcmp(arg, "--")) {
885 if (!strcmp(arg, "-a")) {
889 if (!strcmp(arg, "-p")) {
890 flags |= CHECK_PLAYLISTS;
893 if (!strcmp(arg, "-m")) {
894 flags |= CHECK_MOODS;
897 return -E_AFS_SYNTAX;
900 return -E_AFS_SYNTAX;
903 if (flags & CHECK_AFT) {
904 ret = send_callback_request(aft_check_callback, NULL, &result);
908 ret = send_buffer(fd, (char *) result.data);
914 if (flags & CHECK_PLAYLISTS) {
915 ret = send_callback_request(playlist_check_callback, NULL, &result);
919 ret = send_buffer(fd, (char *) result.data);
925 if (flags & CHECK_MOODS) {
926 ret = send_callback_request(mood_check_callback, NULL, &result);
930 ret = send_buffer(fd, (char *) result.data);