+ {
+ char *buf2;
+ ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+ if (ret < 0)
+ goto out;
+ if (ret == 0)
+ return 0;
+ /*
+ * The FD_ISSET() is not strictly necessary, but is allows us
+ * to skip the malloc below if there is nothing to read anyway.
+ */
+ if (!FD_ISSET(ct->scc.fd, &s->rfds))
+ return 0;
+ if (ct->use_sideband) {
+ struct sb_buffer sbb;
+ ret = recv_sb(ct, &s->rfds, &sbb);
+ if (ret > 0)
+ ret = dispatch_sbb(ct, &sbb);
+ goto out;
+ }
+ buf2 = para_malloc(CLIENT_BUFSIZE);
+ ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
+ if (n > 0) {
+ buf2 = para_realloc(buf2, n);
+ btr_add_output(buf2, n, btrn);
+ } else
+ free(buf2);
+ goto out;
+ }
+ }
+out:
+ if (ret < 0) {
+ if (!ct->use_sideband && ret != -E_SERVER_EOF &&
+ ret != -E_BTR_EOF && ret != -E_EOF)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ btr_remove_node(&ct->btrn);
+ }
+ return ret;
+}
+
+/**
+ * Connect to para_server and register the client task.
+ *
+ * \param ct The initialized client task structure.
+ * \param s The scheduler instance to register the client task to.
+ * \param parent The parent node of the client btr node.
+ * \param child The child node of the client node.
+ *
+ * The client task structure given by \a ct must be allocated and initialized
+ * by \ref client_parse_config() before this function is called.
+ *
+ * \return Standard.
+ */
+int client_connect(struct client_task *ct, struct sched *s,
+ struct btr_node *parent, struct btr_node *child)
+{
+ int ret;
+
+ PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
+ ct->conf.server_port_arg);
+ ct->scc.fd = -1;
+ ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
+ ct->conf.server_port_arg);
+ if (ret < 0)
+ return ret;
+ ct->scc.fd = ret;
+ ret = mark_fd_nonblocking(ct->scc.fd);
+ if (ret < 0)
+ goto err_out;
+ ct->status = CL_CONNECTED;
+ ct->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client", .parent = parent, .child = child));
+ ct->task.pre_select = client_pre_select;
+ ct->task.post_select = client_post_select;
+ ct->task.error = 0;
+ sprintf(ct->task.status, "client");
+ register_task(s, &ct->task);
+ return 1;
+err_out:
+ close(ct->scc.fd);
+ ct->scc.fd = -1;
+ return ret;
+}
+
+/**
+ * Parse a client configuration.
+ *
+ * \param argc Usual argument count.
+ * \param argv Usual argument vector.
+ * \param ct_ptr Filled in by this function.
+ * \param loglevel If not \p NULL, the number of the loglevel is stored here.
+ *
+ * This checks the command line options given by \a argc and \a argv, sets
+ * default values for the user name and the name of the rsa key file and reads
+ * further options from the config file.
+ *
+ * Upon successful return, \a ct_ptr points to a dynamically allocated and
+ * initialized client task struct.
+ *
+ * \return The number of non-option arguments in \a argc/argv on success,
+ * negative on errors.
+ */
+int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
+ int *loglevel)
+{
+ char *home = para_homedir();
+ int ret;
+ struct client_task *ct = para_calloc(sizeof(struct client_task));
+
+ *ct_ptr = ct;
+ ct->scc.fd = -1;
+ ret = -E_CLIENT_SYNTAX;
+ if (client_cmdline_parser(argc, argv, &ct->conf))
+ goto out;
+ HANDLE_VERSION_FLAG("client", ct->conf);
+
+ ct->config_file = ct->conf.config_file_given?
+ para_strdup(ct->conf.config_file_arg) :
+ make_message("%s/.paraslash/client.conf", home);
+ ret = file_exists(ct->config_file);
+ if (!ret && ct->conf.config_file_given) {
+ ret = -E_NO_CONFIG;
+ goto out;