+ struct callback_query *cq;
+ struct callback_result *cr;
+ int ret, fd = -1, query_shmid, result_shmid;
+ void *query_shm, *result_shm;
+ char buf[sizeof(afs_socket_cookie) + sizeof(int)];
+// char *tmpsocket_name;
+ struct sockaddr_un unix_addr;
+
+ assert(query->data && query->size);
+ ret = shm_new(query->size + sizeof(*cq));
+ if (ret < 0)
+ return ret;
+ query_shmid = ret;
+ ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
+ if (ret < 0)
+ goto out;
+ cq = query_shm;
+ cq->handler = f;
+ cq->query_size = query->size;
+
+ memcpy(query_shm + sizeof(*cq), query->data, query->size);
+ ret = shm_detach(query_shm);
+ if (ret < 0)
+ goto out;
+
+ *(uint32_t *) buf = afs_socket_cookie;
+ *(int *) (buf + sizeof(afs_socket_cookie)) = query_shmid;
+
+ ret = get_stream_socket(PF_UNIX);
+ if (ret < 0)
+ goto out;
+ fd = ret;
+ ret = init_unix_addr(&unix_addr, conf.afs_socket_arg);
+ if (ret < 0)
+ goto out;
+ ret = PARA_CONNECT(fd, &unix_addr);
+ if (ret < 0)
+ goto out;
+ ret = send_bin_buffer(fd, buf, sizeof(buf));
+ if (ret < 0)
+ goto out;
+ ret = recv_bin_buffer(fd, buf, sizeof(buf));
+ if (ret < 0)
+ goto out;
+ if (ret != sizeof(int)) {
+ ret = -E_RECV;
+ goto out;
+ }
+ ret = *(int *) buf;
+ if (ret <= 0)
+ goto out;
+ result_shmid = ret;
+ ret = shm_attach(result_shmid, ATTACH_RO, &result_shm);
+ if (ret >= 0) {
+ assert(result);
+ cr = result_shm;
+ result->size = cr->result_size;
+ result->data = para_malloc(result->size);
+ memcpy(result->data, result_shm + sizeof(*cr), result->size);
+ ret = shm_detach(result_shm);
+ if (ret < 0)
+ PARA_ERROR_LOG("can not detach result\n");
+ } else
+ PARA_ERROR_LOG("attach result failed: %d\n", ret);
+ if (shm_destroy(result_shmid) < 0)
+ PARA_ERROR_LOG("destroy result failed\n");
+ ret = 1;
+out:
+ if (shm_destroy(query_shmid) < 0)
+ PARA_ERROR_LOG("%s\n", "shm destroy error");
+ if (fd >= 0)
+ close(fd);
+// PARA_DEBUG_LOG("callback_ret: %d\n", ret);
+ 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 The result of the query is stored here.
+ *
+ * Some commands have a couple of options that are parsed in child context for
+ * syntactic correctness and are stored in a special options structure for that
+ * command. This function allows to pass such a structure together with a list
+ * of further arguments (often a list of audio files) to the parent process.
+ *
+ * \sa send_standard_callback_request(), send_callback_request().
+ */
+int send_option_arg_callback_request(struct osl_object *options,
+ int argc, char * const * const argv, callback_function *f,
+ struct osl_object *result)
+{
+ 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);
+ 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 The same meaning as in send_option_arg_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,
+ callback_function *f, struct osl_object *result)
+{
+ return send_option_arg_callback_request(NULL, argc, argv, f, result);
+}
+
+/**
+ * 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));
+}
+
+/**
+ * A wrapper for strtol(3).
+ *
+ * \param str The string to be converted to a long integer.
+ * \param result The converted value is stored here.
+ *
+ * \return Positive on success, -E_ATOL on errors.
+ *
+ * \sa strtol(3), atoi(3).
+ */
+int para_atol(const char *str, long *result)
+{
+ char *endptr;
+ long val;
+ int ret, base = 10;
+
+ errno = 0; /* To distinguish success/failure after call */
+ val = strtol(str, &endptr, base);
+ ret = -E_ATOL;
+ if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+ goto out; /* overflow */
+ if (errno != 0 && val == 0)
+ goto out; /* other error */
+ if (endptr == str)
+ goto out; /* No digits were found */
+ if (*endptr != '\0')
+ goto out; /* Further characters after number */
+ *result = val;
+ ret = 1;
+out:
+ return ret;
+}
+
+
+/*
+ * write input from fd to dynamically allocated char array,
+ * but maximal max_size byte. Return size.
+ */
+static int fd2buf(int fd, char **buf, int max_size)
+{
+ const size_t chunk_size = 1024;
+ size_t size = 2048;
+ char *p;
+ int ret;
+
+ *buf = para_malloc(size * sizeof(char));
+ p = *buf;
+ while ((ret = read(fd, p, chunk_size)) > 0) {
+ p += ret;
+ if ((p - *buf) + chunk_size >= size) {
+ char *tmp;
+
+ size *= 2;
+ if (size > max_size) {
+ ret = -E_INPUT_TOO_LARGE;