+ if (gc->fd < 0) {
+ list_del(&gc->node);
+ free(gc);
+ continue;
+ }
+ 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_select().
+ */
+ close(gc->fd);
+ gc->fd = -1;
+ free(gc->parent);
+ free(gc->name);
+ return 1;
+ }
+ return 0;
+}
+
+static int gc_post_select(__a_unused struct sched *s, struct task *t)
+{
+ struct grab_client *gc = container_of(t, struct grab_client, task);
+ 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(int argc, char **argv, struct grab_client *gc)
+{
+ int i;
+
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+ if (arg[0] != '-')
+ break;
+ if (!strcmp(arg, "--")) {
+ i++;
+ break;
+ }
+ if (!strncmp(arg, "-m", 2)) {
+ if (*(arg + 3))
+ return -E_GC_SYNTAX;
+ switch(*(arg + 2)) {
+ case 's':
+ gc->mode = GM_SLOPPY;
+ continue;
+ case 'a':
+ gc->mode = GM_AGGRESSIVE;
+ continue;
+ case 'p':
+ gc->mode = GM_PEDANTIC;
+ continue;
+ default:
+ return -E_GC_SYNTAX;
+ }
+ }
+ if (!strcmp(arg, "-o")) {
+ gc->flags |= GF_ONE_SHOT;