X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=client_common.c;h=eea7510fd0589e86fb4058dd7f4361f3c79901aa;hp=900d3653893dea7067330d60a929ce0a4b13d902;hb=534a94f441767947874cb15d18211edf758e9277;hpb=a731a7c6e78fb137268b42062d9fb08b4e49a2c8 diff --git a/client_common.c b/client_common.c index 900d3653..eea7510f 100644 --- a/client_common.c +++ b/client_common.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2014 Andre Noll + * Copyright (C) 1997 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -32,44 +32,17 @@ /** The size of the receiving buffer. */ #define CLIENT_BUFSIZE 4000 -/** - * Close the connection to para_server and deallocate per-command resources. - * - * \param ct The client task. - * - * This frees all resources of the current command but keeps the configuration - * in \p ct->conf. - * - * \sa \ref client_close(). - */ -void client_disconnect(struct client_task *ct) -{ - if (!ct) - return; - if (ct->scc.fd >= 0) - close(ct->scc.fd); - 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; - btr_remove_node(&ct->btrn[0]); - btr_remove_node(&ct->btrn[1]); -} - /** * Close the connection to para_server and free all resources. * * \param ct Pointer to the client data. * - * \sa \ref client_open(), \ref client_disconnect(). + * \sa \ref client_open(). */ void client_close(struct client_task *ct) { if (!ct) return; - client_disconnect(ct); free(ct->user); free(ct->config_file); free(ct->key_file); @@ -80,24 +53,19 @@ void client_close(struct client_task *ct) free(ct); } -/** +/* * The preselect hook for server commands. * - * \param s Pointer to the scheduler. - * \param t Pointer to the task struct for this command. - * * The task pointer must contain a pointer to the initialized client data * structure as it is returned by client_open(). * * This function checks the state of the connection and adds the file descriptor - * of the connection to the read or write fd set of \a s accordingly. - * - * \sa register_task() client_open(), struct sched, struct task. + * of the connection to the read or write fd set of s accordingly. */ -static void client_pre_select(struct sched *s, struct task *t) +static void client_pre_select(struct sched *s, void *context) { int ret; - struct client_task *ct = container_of(t, struct client_task, task); + struct client_task *ct = context; if (ct->scc.fd < 0) return; @@ -191,7 +159,10 @@ again: } if (n == 0) return 0; - if (!sb_received(ct->sbc[0], n, result)) + ret = sb_received(ct->sbc[0], n, result); + if (ret < 0) + return ret; + if (ret == 0) goto again; ct->sbc[0] = NULL; return 1; @@ -295,27 +266,22 @@ static int send_sb_command(struct client_task *ct) return send_sb(ct, 0, command, len, SBD_COMMAND, false); } -/** +/* * The post select hook for client commands. * - * \param s Pointer to the scheduler. - * \param t Pointer to the task struct for this command. - * * Depending on the current state of the connection and the status of the read - * and write fd sets of \a s, this function performs the necessary steps to - * authenticate the connection, to send the command given by \a t->private_data + * and write fd sets of s, this function performs the necessary steps to + * authenticate the connection, to send the command given by t->private_data * and to receive para_server's output, if any. - * - * \sa struct sched, struct task. */ -static int client_post_select(struct sched *s, struct task *t) +static int client_post_select(struct sched *s, void *context) { - struct client_task *ct = container_of(t, struct client_task, task); + struct client_task *ct = context; int ret = 0; size_t n; char buf[CLIENT_BUFSIZE]; - ret = task_get_notification(t); + ret = task_get_notification(ct->task); if (ret < 0) goto out; if (ct->scc.fd < 0) @@ -326,11 +292,6 @@ static int client_post_select(struct sched *s, struct task *t) if (ret < 0 || n == 0) goto out; ct->features = parse_features(buf); - if (!has_feature("sideband", ct)) { - PARA_ERROR_LOG("server has no sideband support\n"); - ret = -E_INCOMPAT_FEAT; - goto out; - } ct->status = CL_RECEIVED_WELCOME; return 0; case CL_RECEIVED_WELCOME: /* send auth command */ @@ -437,7 +398,7 @@ static int client_post_select(struct sched *s, struct task *t) btr_consume(ct->btrn[1], sz); } } - /* fall though */ + /* fall through */ case CL_EXECUTING: if (ct->btrn[0]) { ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT); @@ -476,6 +437,16 @@ out: 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; } @@ -513,11 +484,13 @@ int client_connect(struct client_task *ct, struct sched *s, 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); + + ct->task = task_register(&(struct task_info) { + .name = "client", + .pre_select = client_pre_select, + .post_select = client_post_select, + .context = ct, + }, s); return 1; err_out: close(ct->scc.fd);