+ /* fall though */
+ case CL_EXECUTING:
+ if (ct->btrn[0]) {
+ ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
+ if (ret < 0)
+ goto close0;
+ if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
+ struct sb_buffer sbb;
+ ret = recv_sb(ct, &s->rfds, &sbb);
+ if (ret < 0)
+ goto close0;
+ if (ret > 0) {
+ ret = dispatch_sbb(ct, &sbb);
+ if (ret < 0)
+ goto close0;
+ }
+ }
+ }
+ ret = 0;
+ goto out;
+ }
+close1:
+ PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
+ btr_remove_node(&ct->btrn[1]);
+ if (ct->btrn[0])
+ return 0;
+ goto out;
+close0:
+ PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
+ btr_remove_node(&ct->btrn[0]);
+ if (ct->btrn[1] && ct->status == CL_SENDING)
+ return 0;
+out:
+ if (ret >= 0)
+ return 0;
+ btr_remove_node(&ct->btrn[0]);
+ btr_remove_node(&ct->btrn[1]);
+ if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ if (ct->scc.fd >= 0) {
+ close(ct->scc.fd);
+ ct->scc.fd = -1;
+ }
+ free_argv(ct->features);
+ ct->features = NULL;
+ sc_free(ct->scc.recv);
+ ct->scc.recv = NULL;
+ sc_free(ct->scc.send);
+ ct->scc.send = NULL;
+ 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[0] = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client recv", .parent = NULL, .child = child));
+ ct->btrn[1] = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client send", .parent = parent, .child = NULL));
+ 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;
+}
+
+__noreturn static void print_help_and_die(struct client_task *ct)
+{
+ struct ggo_help h = DEFINE_GGO_HELP(client);
+ bool d = ct->conf.detailed_help_given;
+
+ ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+ exit(0);
+}
+
+/**
+ * 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;
+ version_handle_flag("client", ct->conf.version_given);
+ if (ct->conf.help_given || ct->conf.detailed_help_given)
+ print_help_and_die(ct);
+
+ 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;