+ if (ret)
+ return -E_FNMATCH;
+ return pmd->action(pmd->table, row, name, pmd->data);
+ }
+ return 1;
+}
+
+/**
+ * Execute the given function for each matching row.
+ *
+ * \param pmd Describes what to match and how.
+ *
+ * \return The return value of the underlying call to osl_rbtree_loop()
+ * or osl_rbtree_loop_reverse().
+ */
+int for_each_matching_row(struct pattern_match_data *pmd)
+{
+ if (pmd->pm_flags & PM_REVERSE_LOOP)
+ return osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
+ action_if_pattern_matches);
+ return osl_rbtree_loop(pmd->table, pmd->loop_col_num, pmd,
+ action_if_pattern_matches);
+}
+
+/**
+ * Compare two osl objects of string type.
+ *
+ * \param obj1 Pointer to the first object.
+ * \param obj2 Pointer to the second object.
+ *
+ * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
+ * are taken into account.
+ *
+ * \return It returns an integer less than, equal to, or greater than zero if
+ * \a obj1 is found, respectively, to be less than, to match, or be greater than
+ * obj2.
+ *
+ * \sa strcmp(3), strncmp(3), osl_compare_func.
+ */
+int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
+{
+ const char *str1 = (const char *)obj1->data;
+ const char *str2 = (const char *)obj2->data;
+ return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
+}
+
+/*
+ * write input from fd to dynamically allocated buffer,
+ * but maximal max_size byte.
+ */
+static int fd2buf(int fd, unsigned max_size, struct osl_object *obj)
+{
+ const size_t chunk_size = 1024;
+ size_t size = 2048, received = 0;
+ int ret;
+ char *buf = para_malloc(size);
+
+ for (;;) {
+ ret = recv_bin_buffer(fd, buf + received, chunk_size);
+ if (ret <= 0)
+ break;
+ received += ret;
+ if (received + chunk_size >= size) {
+ size *= 2;
+ ret = -E_INPUT_TOO_LARGE;
+ if (size > max_size)
+ break;
+ buf = para_realloc(buf, size);
+ }
+ }
+ obj->data = buf;
+ obj->size = received;
+ if (ret < 0)
+ free(buf);
+ return ret;
+}
+
+/**
+ * Read data from a file descriptor, and send it to the afs process.
+ *
+ * \param fd File descriptor to read data from.
+ * \param arg_obj Pointer to the arguments to \a f.
+ * \param f The callback function.
+ * \param max_len Don't read more than that many bytes from stdin.
+ * \param result_handler See \ref send_callback_request.
+ * \param private_result_data See \ref send_callback_request.
+ *
+ * This function is used by commands that wish to let para_server store
+ * arbitrary data specified by the user (for instance the add_blob family of
+ * commands). First, at most \a max_len bytes are read from \a fd, the result
+ * is concatenated with the buffer given by \a arg_obj, and the combined buffer
+ * is made available to the afs process via the callback method. See \ref
+ * send_callback_request for details.
+ *
+ * \return Negative on errors, the return value of the underlying call to
+ * send_callback_request() otherwise.
+ */
+int stdin_command(int fd, struct osl_object *arg_obj, callback_function *f,
+ unsigned max_len, callback_result_handler *result_handler,
+ void *private_result_data)
+{
+ struct osl_object query, stdin_obj;
+ int ret;
+
+ ret = send_buffer(fd, AWAITING_DATA_MSG);
+ if (ret < 0)
+ return ret;
+ ret = fd2buf(fd, max_len, &stdin_obj);
+ if (ret < 0)
+ return ret;
+ query.size = arg_obj->size + stdin_obj.size;
+ query.data = para_malloc(query.size);
+ memcpy(query.data, arg_obj->data, arg_obj->size);
+ memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
+ free(stdin_obj.data);
+ ret = send_callback_request(f, &query, result_handler, private_result_data);
+ free(query.data);
+ return ret;
+}
+
+static int pass_afd(int fd, char *buf, size_t size)
+{
+ struct msghdr msg = {.msg_iov = NULL};
+ struct cmsghdr *cmsg;
+ char control[255];
+ int ret;
+ struct iovec iov;
+
+ iov.iov_base = buf;
+ iov.iov_len = size;
+
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ *(int *)CMSG_DATA(cmsg) = fd;
+
+ /* Sum of the length of all control messages in the buffer */
+ msg.msg_controllen = cmsg->cmsg_len;
+ PARA_DEBUG_LOG("passing %zu bytes and fd %d\n", size, fd);
+ ret = sendmsg(server_socket, &msg, 0);
+ if (ret < 0) {
+ ret = -ERRNO_TO_PARA_ERROR(errno);
+ return ret;
+ }
+ return 1;
+}
+
+/**
+ * Open the audio file with highest score.
+ *
+ * This stores all information for streaming the "best" audio file in a shared
+ * memory area. The id of that area and an open file descriptor for the next
+ * audio file are passed to the server process.
+ *
+ * \return Standard.
+ *
+ * \sa open_and_update_audio_file().
+ */
+int open_next_audio_file(void)
+{
+ struct osl_row *aft_row;
+ struct audio_file_data afd;
+ int ret, shmid;
+ char buf[8];
+ long score;
+again:
+ PARA_NOTICE_LOG("getting next audio file\n");
+ ret = score_get_best(&aft_row, &score);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ goto no_admissible_files;
+ }
+ ret = open_and_update_audio_file(aft_row, score, &afd);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ ret = score_delete(aft_row);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ goto no_admissible_files;
+ }
+ goto again;
+ }
+ shmid = ret;
+ if (!write_ok(server_socket)) {
+ ret = -E_AFS_SOCKET;
+ goto destroy;
+ }
+ *(uint32_t *)buf = NEXT_AUDIO_FILE;
+ *(uint32_t *)(buf + 4) = (uint32_t)shmid;
+ ret = pass_afd(afd.fd, buf, 8);
+ close(afd.fd);
+ if (ret >= 0)
+ return ret;
+destroy:
+ shm_destroy(shmid);
+ return ret;
+no_admissible_files:
+ *(uint32_t *)buf = NO_ADMISSIBLE_FILES;
+ *(uint32_t *)(buf + 4) = (uint32_t)0;
+ return send_bin_buffer(server_socket, buf, 8);
+}
+
+/* Never fails if arg == NULL */
+static int activate_mood_or_playlist(char *arg, int *num_admissible)
+{
+ enum play_mode mode;
+ int ret;
+
+ if (!arg) {
+ ret = change_current_mood(NULL); /* always successful */
+ mode = PLAY_MODE_MOOD;
+ } else {
+ if (!strncmp(arg, "p/", 2)) {
+ ret = playlist_open(arg + 2);
+ mode = PLAY_MODE_PLAYLIST;
+ } else if (!strncmp(arg, "m/", 2)) {
+ ret = change_current_mood(arg + 2);
+ mode = PLAY_MODE_MOOD;
+ } else
+ return -E_AFS_SYNTAX;
+ if (ret < 0)
+ return ret;
+ }
+ if (num_admissible)
+ *num_admissible = ret;
+ current_play_mode = mode;
+ if (arg != current_mop) {
+ free(current_mop);
+ if (arg) {
+ current_mop = para_strdup(arg);
+ mmd_lock();
+ strncpy(mmd->afs_mode_string, arg,
+ sizeof(mmd->afs_mode_string));
+ mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
+ mmd_unlock();
+ } else {
+ mmd_lock();
+ strcpy(mmd->afs_mode_string, "dummy");
+ mmd_unlock();
+ current_mop = NULL;
+ }
+ }
+ return 1;
+}
+
+static void com_select_callback(int fd, const struct osl_object *query)
+{
+ struct para_buffer pb = {
+ .max_size = SHMMAX,
+ .private_data = &fd,
+ .max_size_handler = pass_buffer_as_shm
+ };
+ char *arg = query->data;
+ int num_admissible, ret, ret2;
+
+ ret = clear_score_table();
+ if (ret < 0) {
+ ret2 = para_printf(&pb, "%s\n", para_strerror(-ret));
+ goto out;
+ }
+ if (current_play_mode == PLAY_MODE_MOOD)
+ close_current_mood();
+ else
+ playlist_close();
+ ret = activate_mood_or_playlist(arg, &num_admissible);
+ if (ret < 0) {
+ ret2 = para_printf(&pb, "%s\nswitching back to %s\n",
+ para_strerror(-ret), current_mop?
+ current_mop : "dummy");
+ ret = activate_mood_or_playlist(current_mop, &num_admissible);
+ if (ret < 0) {
+ if (ret2 >= 0)
+ ret2 = para_printf(&pb, "failed, switching to dummy\n");
+ activate_mood_or_playlist(NULL, &num_admissible);
+ }
+ } else
+ ret2 = para_printf(&pb, "activated %s (%d admissible files)\n", current_mop?
+ current_mop : "dummy mood", num_admissible);
+out:
+ if (ret2 >= 0 && pb.offset)
+ pass_buffer_as_shm(pb.buf, pb.offset, &fd);
+ free(pb.buf);
+}
+
+/**
+ * Result handler for sending data to the para_client process.
+ *
+ * \param result The data to be sent.
+ * \param fd_ptr Pointer to the file descriptor.
+ *
+ * \return The return value of the underlying call to send_bin_buffer().
+ *
+ * \sa \ref callback_result_handler.
+ */
+int send_result(struct osl_object *result, void *fd_ptr)
+{
+ int fd = *(int *)fd_ptr;
+ if (!result->size)
+ return 1;
+ return send_bin_buffer(fd, result->data, result->size);
+}
+
+int com_select(int fd, int argc, char * const * const argv)
+{
+ struct osl_object query;
+
+ if (argc != 2)
+ return -E_AFS_SYNTAX;
+ query.data = argv[1];
+ query.size = strlen(argv[1]) + 1;
+ return send_callback_request(com_select_callback, &query,
+ &send_result, &fd);
+}
+
+static void init_admissible_files(char *arg)
+{
+ if (activate_mood_or_playlist(arg, NULL) < 0)
+ activate_mood_or_playlist(NULL, NULL); /* always successful */
+}
+
+static int setup_command_socket_or_die(void)
+{
+ int ret, socket_fd;
+ char *socket_name = conf.afs_socket_arg;
+ struct sockaddr_un unix_addr;
+
+ unlink(socket_name);
+ ret = create_local_socket(socket_name, &unix_addr,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
+ if (ret < 0) {
+ PARA_EMERG_LOG("%s: %s\n", para_strerror(-ret), socket_name);
+ exit(EXIT_FAILURE);
+ }
+ socket_fd = ret;
+ if (listen(socket_fd , 5) < 0) {
+ PARA_EMERG_LOG("can not listen on socket\n");
+ exit(EXIT_FAILURE);
+ }
+ ret = mark_fd_nonblocking(socket_fd);
+ if (ret < 0) {
+ close(socket_fd);
+ return ret;
+ }
+ PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
+ socket_fd);
+ return socket_fd;
+}
+
+static void close_afs_tables(void)
+{
+ int i;
+ PARA_NOTICE_LOG("closing afs_tables\n");
+ for (i = 0; i < NUM_AFS_TABLES; i++)
+ afs_tables[i].close();
+}
+
+static char *database_dir;
+
+static void get_database_dir(void)
+{
+ if (!database_dir) {
+ if (conf.afs_database_dir_given)
+ database_dir = para_strdup(conf.afs_database_dir_arg);
+ else {
+ char *home = para_homedir();
+ database_dir = make_message(
+ "%s/.paraslash/afs_database", home);
+ free(home);
+ }
+ }
+ PARA_INFO_LOG("afs_database dir %s\n", database_dir);
+}
+
+static int make_database_dir(void)
+{
+ int ret;
+
+ get_database_dir();
+ ret = para_mkdir(database_dir, 0777);
+ if (ret >= 0 || is_errno(-ret, EEXIST))
+ return 1;
+ return ret;
+}
+
+static int open_afs_tables(void)
+{
+ int i, ret;
+
+ get_database_dir();
+ PARA_NOTICE_LOG("opening %u osl tables in %s\n", NUM_AFS_TABLES,
+ database_dir);
+ for (i = 0; i < NUM_AFS_TABLES; i++) {
+ ret = afs_tables[i].open(database_dir);
+ if (ret >= 0)