+ for (;;) {
+ ret = recv_bin_buffer(fd, buf, sizeof(int));
+ if (ret <= 0)
+ goto out;
+ assert(ret == sizeof(int));
+ ret = *(int *) buf;
+ assert(ret > 0);
+ result_shmid = ret;
+ ret = dispatch_result(result_shmid, result_handler,
+ private_result_data);
+ if (ret < 0 && dispatch_error >= 0)
+ dispatch_error = ret;
+ ret = shm_destroy(result_shmid);
+ if (ret < 0)
+ PARA_CRIT_LOG("destroy result failed: %s\n",
+ para_strerror(-ret));
+ num_dispatched++;
+ }
+out:
+ if (shm_destroy(query_shmid) < 0)
+ PARA_CRIT_LOG("shm destroy error\n");
+ if (fd >= 0)
+ close(fd);
+ if (dispatch_error < 0)
+ return dispatch_error;
+ if (ret < 0)
+ return ret;
+ return num_dispatched;
+}
+
+/**
+ * Wrapper for send_callback_request() which passes a lopsub parse result.
+ *
+ * \param f The callback function.
+ * \param cmd Needed for (de-)serialization.
+ * \param lpr Must match cmd.
+ * \param private_result_data Passed to send_callback_request().
+ *
+ * This function serializes the parse result given by the lpr pointer into a
+ * buffer. The buffer is sent as the query to the afs process with the callback
+ * mechanism.
+ *
+ * \return The return value of the underlying call to send_callback_request().
+ */
+int send_lls_callback_request(afs_callback *f,
+ const struct lls_command * const cmd,
+ struct lls_parse_result *lpr, void *private_result_data)
+{
+ struct osl_object query;
+ char *buf = NULL;
+ int ret = lls_serialize_parse_result(lpr, cmd, &buf, &query.size);
+
+ assert(ret >= 0);
+ query.data = buf;
+ ret = send_callback_request(f, &query, afs_cb_result_handler,
+ private_result_data);
+ free(buf);
+ return ret;
+}
+
+/**
+ * Send a callback request passing an options structure and an argument vector.
+ *
+ * \param options pointer to an arbitrary data structure.
+ * \param argc Argument count.
+ * \param argv Standard argument vector.
+ * \param f The callback function.
+ * \param result_handler See \ref send_callback_request.
+ * \param private_result_data See \ref send_callback_request.
+ *
+ * Some command handlers pass command-specific options to a callback, together
+ * with a list of further arguments (often a list of audio files). This
+ * function allows to pass an arbitrary structure (given as an osl object) and
+ * a usual argument vector to the specified callback.
+ *
+ * \return The return value of the underlying call to \ref
+ * send_callback_request().
+ *
+ * \sa send_standard_callback_request(), send_callback_request().
+ */
+int send_option_arg_callback_request(struct osl_object *options,
+ int argc, char * const * const argv, afs_callback *f,
+ callback_result_handler *result_handler,
+ void *private_result_data)
+{
+ char *p;
+ int i, ret;
+ struct osl_object query = {.size = options? options->size : 0};
+
+ for (i = 0; i < argc; i++)
+ query.size += strlen(argv[i]) + 1;
+ query.data = para_malloc(query.size);
+ p = query.data;
+ if (options) {
+ memcpy(query.data, options->data, options->size);
+ p += options->size;
+ }
+ for (i = 0; i < argc; i++) {
+ strcpy(p, argv[i]); /* OK */
+ p += strlen(argv[i]) + 1;
+ }
+ ret = send_callback_request(f, &query, result_handler,
+ private_result_data);
+ free(query.data);
+ return ret;
+}
+
+/**
+ * Send a callback request with an argument vector only.
+ *
+ * \param argc The same meaning as in send_option_arg_callback_request().
+ * \param argv The same meaning as in send_option_arg_callback_request().
+ * \param f The same meaning as in send_option_arg_callback_request().
+ * \param result_handler See \ref send_callback_request.
+ * \param private_result_data See \ref send_callback_request.
+ *
+ * This is similar to send_option_arg_callback_request(), but no options buffer
+ * is passed to the parent process.
+ *
+ * \return The return value of the underlying call to
+ * send_option_arg_callback_request().
+ */
+int send_standard_callback_request(int argc, char * const * const argv,
+ afs_callback *f, callback_result_handler *result_handler,
+ void *private_result_data)
+{
+ return send_option_arg_callback_request(NULL, argc, argv, f, result_handler,
+ private_result_data);
+}
+
+static int action_if_pattern_matches(struct osl_row *row, void *data)
+{
+ struct pattern_match_data *pmd = data;
+ struct osl_object name_obj;
+ const char *p, *name;
+ int i, ret = osl(osl_get_object(pmd->table, row, pmd->match_col_num, &name_obj));
+ const char *pattern_txt = (const char *)pmd->patterns.data;
+
+ if (ret < 0)
+ return ret;
+ name = (char *)name_obj.data;
+ if ((!name || !*name) && (pmd->pm_flags & PM_SKIP_EMPTY_NAME))
+ return 1;
+ if ((pmd->lpr && lls_num_inputs(pmd->lpr) == 0) || pmd->patterns.size == 0) {
+ if (pmd->pm_flags & PM_NO_PATTERN_MATCHES_EVERYTHING) {
+ pmd->num_matches++;
+ return pmd->action(pmd->table, row, name, pmd->data);
+ }
+ }
+ p = pattern_txt;
+ i = pmd->input_skip;
+ for (;;) {
+ if (pmd->lpr) {
+ if (i >= lls_num_inputs(pmd->lpr))
+ break;
+ p = lls_input(i, pmd->lpr);
+ } else {
+ if (p >= pattern_txt + pmd->patterns.size)
+ break;
+ }
+ ret = fnmatch(p, name, pmd->fnmatch_flags);
+ if (ret != FNM_NOMATCH) {
+ if (ret != 0)
+ return -E_FNMATCH;
+ ret = pmd->action(pmd->table, row, name, pmd->data);
+ if (ret >= 0)
+ pmd->num_matches++;
+ return ret;
+
+ }
+ if (pmd->lpr)
+ i++;
+ else
+ p += strlen(p) + 1;
+ }
+ return 1;
+}
+
+/**
+ * Execute the given function for each matching row.
+ *
+ * \param pmd Describes what to match and how.
+ *
+ * \return Standard.
+ */
+int for_each_matching_row(struct pattern_match_data *pmd)
+{
+ if (pmd->pm_flags & PM_REVERSE_LOOP)
+ return osl(osl_rbtree_loop_reverse(pmd->table, pmd->loop_col_num, pmd,
+ action_if_pattern_matches));
+ return osl(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));
+}
+
+static int pass_afd(int fd, char *buf, size_t size)
+{
+ struct msghdr msg = {.msg_iov = NULL};
+ struct cmsghdr *cmsg;
+ char control[255] __a_aligned(8);
+ 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;
+}
+
+/**
+ * Pass the fd of the next audio file to the server process.
+ *
+ * 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().
+ */
+static int open_next_audio_file(void)
+{
+ struct audio_file_data afd;
+ int ret, shmid;
+ char buf[8];
+
+ ret = open_and_update_audio_file(&afd);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ goto no_admissible_files;
+ }
+ 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 write_all(server_socket, buf, 8);
+}
+
+/* Never fails if arg == NULL */
+static int activate_mood_or_playlist(const 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);
+ mutex_lock(mmd_mutex);
+ strncpy(mmd->afs_mode_string, arg,
+ sizeof(mmd->afs_mode_string));
+ mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
+ mutex_unlock(mmd_mutex);
+ } else {
+ mutex_lock(mmd_mutex);
+ strcpy(mmd->afs_mode_string, "dummy");
+ mutex_unlock(mmd_mutex);
+ current_mop = NULL;
+ }
+ }
+ return 1;
+}
+
+/**
+ * Result handler for sending data to the para_client process.
+ *
+ * \param result The data to be sent.
+ * \param band The band designator.
+ * \param private Pointer to the command context.
+ *
+ * \return The return value of the underlying call to \ref command.c::send_sb.
+ *
+ * \sa \ref callback_result_handler, \ref command.c::send_sb.
+ */
+int afs_cb_result_handler(struct osl_object *result, uint8_t band,
+ void *private)
+{
+ struct command_context *cc = private;
+
+ assert(cc);
+ switch (band) {
+ case SBD_OUTPUT:
+ case SBD_DEBUG_LOG:
+ case SBD_INFO_LOG:
+ case SBD_NOTICE_LOG:
+ case SBD_WARNING_LOG:
+ case SBD_ERROR_LOG:
+ case SBD_CRIT_LOG:
+ case SBD_EMERG_LOG:
+ assert(result->size > 0);
+ return send_sb(&cc->scc, result->data, result->size, band, true);
+ case SBD_AFS_CB_FAILURE:
+ return *(int *)(result->data);
+ default:
+ return -E_BAD_BAND;
+ }
+}
+
+static void flush_and_free_pb(struct para_buffer *pb)
+{
+ int ret;
+ struct afs_max_size_handler_data *amshd = pb->private_data;
+
+ if (pb->buf && pb->size > 0) {
+ ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
+ pb->offset);
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ }
+ free(pb->buf);
+}
+
+static int com_select_callback(struct afs_callback_arg *aca)
+{
+ const struct lls_command *cmd = SERVER_CMD_CMD_PTR(SELECT);
+ const char *arg;
+ int num_admissible, ret;
+
+ ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
+ assert(ret >= 0);
+ arg = lls_input(0, aca->lpr);
+ ret = clear_score_table();
+ if (ret < 0) {
+ para_printf(&aca->pbout, "could not clear score table\n");
+ goto free_lpr;
+ }
+ if (current_play_mode == PLAY_MODE_MOOD)
+ close_current_mood();
+ else
+ playlist_close();
+ ret = activate_mood_or_playlist(arg, &num_admissible);
+ if (ret >= 0)