+ }
+ gc_activate(gc, s);
+ }
+}
+
+static int gc_close(struct grab_client *gc, int err)
+{
+ btr_remove_node(&gc->btrn);
+ PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
+ list_move(&gc->node, &inactive_grab_client_list);
+ if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
+ /*
+ * We must not free the gc structure here as it contains ->task
+ * which is still used because this function is called from
+ * post_monitor().
+ */
+ close(gc->fd);
+ gc->fd = -1;
+ free(gc->parent);
+ free(gc->name);
+ return 1;
+ }
+ return 0;
+}
+
+static int gc_post_monitor(__a_unused struct sched *s, void *context)
+{
+ struct grab_client *gc = context;
+ struct btr_node *btrn = gc->btrn;
+ int ret;
+ size_t sz;
+ char *buf;
+
+ ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+ if (ret == 0)
+ return 0;
+ if (ret < 0)
+ goto err;
+ sz = btr_next_buffer(btrn, &buf);
+ assert(sz != 0);
+ ret = gc_write(gc, buf, sz);
+ if (ret < 0)
+ goto err;
+ if (ret > 0)
+ btr_consume(btrn, ret);
+ return 0;
+err:
+ gc_close(gc, ret);
+ return ret;
+}
+
+static int gc_check_args(struct lls_parse_result *lpr, struct grab_client *gc)
+{
+ const struct lls_opt_result *r;
+
+ r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_MODE, lpr);
+ if (lls_opt_given(r) > 0) {
+ const char *arg = lls_string_val(0, r);
+ if (strcmp(arg, "s") == 0)
+ gc->mode = GM_SLOPPY;
+ else if (strcmp(arg, "a") == 0)
+ gc->mode = GM_AGGRESSIVE;
+ else if (strcmp(arg, "p") == 0)
+ gc->mode = GM_PEDANTIC;
+ else
+ return -E_GC_SYNTAX;
+ }
+
+ r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_ONE_SHOT, lpr);
+ if (lls_opt_given(r) > 0)
+ gc->flags |= GF_ONE_SHOT;
+
+ r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_PARENT, lpr);
+ if (lls_opt_given(r) > 0) {
+ const char *arg = lls_string_val(0, r);
+ gc->parent = para_strdup(arg);
+ }
+
+ r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_NAME, lpr);
+ if (lls_opt_given(r) > 0) {
+ const char *arg = lls_string_val(0, r);
+ gc->name = para_strdup(arg);