Make writer nodes honor notifications.
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client_common.c Common functions of para_client and para_audiod. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "client.cmdline.h"
17 #include "crypt.h"
18 #include "net.h"
19 #include "fd.h"
20 #include "sideband.h"
21 #include "string.h"
22 #include "client.cmdline.h"
23 #include "client.h"
24 #include "buffer_tree.h"
25 #include "version.h"
26
27 /** The size of the receiving buffer. */
28 #define CLIENT_BUFSIZE 4000
29
30 /**
31  * Close the connection to para_server and deallocate per-command ressources.
32  *
33  * \param ct The client task.
34  *
35  * This frees all ressources of the current command but keeps the configuration
36  * in \p ct->conf.
37  *
38  * \sa \ref client_close().
39  */
40 void client_disconnect(struct client_task *ct)
41 {
42         if (!ct)
43                 return;
44         if (ct->scc.fd >= 0)
45                 close(ct->scc.fd);
46         free_argv(ct->features);
47         sc_free(ct->scc.recv);
48         ct->scc.recv = NULL;
49         sc_free(ct->scc.send);
50         ct->scc.send = NULL;
51         btr_remove_node(&ct->btrn);
52 }
53
54 /**
55  * Close the connection to para_server and free all resources.
56  *
57  * \param ct Pointer to the client data.
58  *
59  * \sa \ref client_open(), \ref client_disconnect().
60  */
61 void client_close(struct client_task *ct)
62 {
63         if (!ct)
64                 return;
65         client_disconnect(ct);
66         free(ct->user);
67         free(ct->config_file);
68         free(ct->key_file);
69         client_cmdline_parser_free(&ct->conf);
70         free(ct->challenge_hash);
71         sb_free(ct->sbc);
72         free(ct);
73 }
74
75 /**
76  * The preselect hook for server commands.
77  *
78  * \param s Pointer to the scheduler.
79  * \param t Pointer to the task struct for this command.
80  *
81  * The task pointer must contain a pointer to the initialized client data
82  * structure as it is returned by client_open().
83  *
84  * This function checks the state of the connection and adds the file descriptor
85  * of the connection to the read or write fd set of \a s accordingly.
86  *
87  * \sa register_task() client_open(), struct sched, struct task.
88  */
89 static void client_pre_select(struct sched *s, struct task *t)
90 {
91         int ret;
92         struct client_task *ct = container_of(t, struct client_task, task);
93         struct btr_node *btrn = ct->btrn;
94
95         if (ct->scc.fd < 0)
96                 return;
97         switch (ct->status) {
98         case CL_CONNECTED:
99         case CL_SENT_AUTH:
100         case CL_SENT_CH_RESPONSE:
101         case CL_SENT_COMMAND:
102                 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
103                 return;
104
105         case CL_RECEIVED_WELCOME:
106         case CL_RECEIVED_PROCEED:
107         case CL_RECEIVED_CHALLENGE:
108                 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
109                 return;
110
111         case CL_RECEIVING:
112                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
113                 if (ret != 0) {
114                         if (ret < 0)
115                                 sched_min_delay(s);
116                         else
117                                 para_fd_set(ct->scc.fd, &s->rfds,
118                                         &s->max_fileno);
119                 }
120                 return;
121         case CL_SENDING:
122                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
123                 if (ret != 0) {
124                         if (ret < 0)
125                                 sched_min_delay(s);
126                         else
127                                 para_fd_set(ct->scc.fd, &s->wfds,
128                                         &s->max_fileno);
129                 }
130                 return;
131         }
132 }
133
134 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
135                 char *buf, size_t sz, size_t *n)
136 {
137         int ret;
138
139         if (ct->status < CL_SENT_CH_RESPONSE)
140                 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
141
142         *n = 0;
143         ret = sc_recv_buffer(&ct->scc, buf, sz);
144         /*
145          * sc_recv_buffer is used with blocking fds elsewhere, so it
146          * does not use the nonblock-API. Therefore we need to
147          * check for EOF and EAGAIN.
148          */
149         if (ret == 0)
150                 return -E_SERVER_EOF;
151         if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
152                 return 0;
153         if (ret < 0)
154                 return ret;
155         *n = ret;
156         return 0;
157 }
158
159 static int send_sb(struct client_task *ct, void *buf, size_t numbytes,
160                 enum sb_designator band, bool dont_free)
161 {
162         int ret, fd = ct->scc.fd;
163         struct iovec iov[2];
164
165         if (!ct->sbc) {
166                 struct sb_buffer sbb;
167                 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
168                         NULL : sc_trafo;
169                 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
170                 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
171         }
172         ret = sb_get_send_buffers(ct->sbc, iov);
173         ret = xwritev(fd, iov, ret);
174         if (ret < 0) {
175                 sb_free(ct->sbc);
176                 ct->sbc = NULL;
177                 return ret;
178         }
179         if (sb_sent(ct->sbc, ret)) {
180                 ct->sbc = NULL;
181                 return 1;
182         }
183         return 0;
184 }
185
186 static int recv_sb(struct client_task *ct, fd_set *rfds,
187                 struct sb_buffer *result)
188 {
189         int ret;
190         size_t n;
191         sb_transformation trafo;
192         void *trafo_context;
193         struct iovec iov;
194
195         if (!FD_ISSET(ct->scc.fd, rfds))
196                 return 0;
197         if (ct->status < CL_SENT_CH_RESPONSE)
198                 trafo = trafo_context = NULL;
199         else {
200                 trafo = sc_trafo;
201                 trafo_context = ct->scc.recv;
202         }
203         if (!ct->sbc)
204                 ct->sbc = sb_new_recv(0, trafo, trafo_context);
205 again:
206         sb_get_recv_buffer(ct->sbc, &iov);
207         ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
208         if (ret < 0) {
209                 sb_free(ct->sbc);
210                 ct->sbc = NULL;
211                 return ret;
212         }
213         if (n == 0)
214                 return 0;
215         if (!sb_received(ct->sbc, n, result))
216                 goto again;
217         ct->sbc = NULL;
218         return 1;
219 }
220
221
222 static char **parse_features(char *buf)
223 {
224         int i;
225         const char id[] = "\nFeatures: ";
226         char *p, *q, **features;
227
228         p = strstr(buf, id);
229         if (!p)
230                 return NULL;
231         p += strlen(id);
232         q = strchr(p, '\n');
233         if (!q)
234                 return NULL;
235         *q = '\0';
236         create_argv(p, ",", &features);
237         for (i = 0; features[i]; i++)
238                 PARA_INFO_LOG("server feature: %s\n", features[i]);
239         return features;
240 }
241
242 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
243 {
244         int ret;
245         const char *designator[] = {SB_DESIGNATORS_ARRAY};
246
247         if (!sbb)
248                 return 0;
249         if (sbb->band < NUM_SB_DESIGNATORS)
250                 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
251
252         switch (sbb->band) {
253         case SBD_OUTPUT:
254                 if (iov_valid(&sbb->iov))
255                         btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
256                                 ct->btrn);
257                 ret = 1;
258                 goto out;
259         case SBD_DEBUG_LOG:
260         case SBD_INFO_LOG:
261         case SBD_NOTICE_LOG:
262         case SBD_WARNING_LOG:
263         case SBD_ERROR_LOG:
264         case SBD_CRIT_LOG:
265         case SBD_EMERG_LOG:
266                 if (iov_valid(&sbb->iov)) {
267                         int ll = sbb->band - SBD_DEBUG_LOG;
268                         para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
269                 }
270                 ret = 1;
271                 goto deallocate;
272         case SBD_EXIT__SUCCESS:
273                 ret = -E_SERVER_CMD_SUCCESS;
274                 goto deallocate;
275         case SBD_EXIT__FAILURE:
276                 ret = -E_SERVER_CMD_FAILURE;
277                 goto deallocate;
278         default:
279                 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
280                 ret = -E_BAD_BAND;
281                 goto deallocate;
282         }
283 deallocate:
284         free(sbb->iov.iov_base);
285 out:
286         sbb->iov.iov_base = NULL;
287         return ret;
288 }
289
290 static bool has_feature(const char *feature, struct client_task *ct)
291 {
292         return find_arg(feature, ct->features) >= 0? true : false;
293 }
294
295 static int send_sb_command(struct client_task *ct)
296 {
297         int i;
298         char *command, *p;
299         size_t len = 0;
300
301         if (ct->sbc)
302                 return send_sb(ct, NULL, 0, 0, false);
303
304         for (i = 0; i < ct->conf.inputs_num; i++)
305                 len += strlen(ct->conf.inputs[i]) + 1;
306         p = command = para_malloc(len);
307         for (i = 0; i < ct->conf.inputs_num; i++) {
308                 strcpy(p, ct->conf.inputs[i]);
309                 p += strlen(ct->conf.inputs[i]) + 1;
310         }
311         PARA_DEBUG_LOG("--> %s\n", command);
312         return send_sb(ct, command, len, SBD_COMMAND, false);
313 }
314
315 /**
316  * The post select hook for client commands.
317  *
318  * \param s Pointer to the scheduler.
319  * \param t Pointer to the task struct for this command.
320  *
321  * Depending on the current state of the connection and the status of the read
322  * and write fd sets of \a s, this function performs the necessary steps to
323  * authenticate the connection, to send the command given by \a t->private_data
324  * and to receive para_server's output, if any.
325  *
326  * \sa struct sched, struct task.
327  */
328 static void client_post_select(struct sched *s, struct task *t)
329 {
330         struct client_task *ct = container_of(t, struct client_task, task);
331         struct btr_node *btrn = ct->btrn;
332         int ret = 0;
333         size_t n;
334         char buf[CLIENT_BUFSIZE];
335
336         ret = task_get_notification(t);
337         if (ret < 0)
338                 goto out;
339         if (ct->scc.fd < 0)
340                 return;
341         switch (ct->status) {
342         case CL_CONNECTED: /* receive welcome message */
343                 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
344                 if (ret < 0 || n == 0)
345                         goto out;
346                 ct->features = parse_features(buf);
347                 ct->status = CL_RECEIVED_WELCOME;
348                 return;
349         case CL_RECEIVED_WELCOME: /* send auth command */
350                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
351                         return;
352                 if (has_feature("sideband", ct)) {
353                         ct->use_sideband = true;
354                         sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
355                 } else
356                         sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
357                 PARA_INFO_LOG("--> %s\n", buf);
358                 ret = write_buffer(ct->scc.fd, buf);
359                 if (ret < 0)
360                         goto out;
361                 ct->status = CL_SENT_AUTH;
362                 return;
363         case CL_SENT_AUTH:
364                 /*
365                  * Receive challenge and session keys, decrypt the challenge and
366                  * send back the hash of the decrypted challenge.
367                  */
368                 {
369                 /* decrypted challenge/session key buffer */
370                 unsigned char crypt_buf[1024];
371                 /* the SHA1 of the decrypted challenge */
372
373                 if (ct->use_sideband) {
374                         struct sb_buffer sbb;
375                         ret = recv_sb(ct, &s->rfds, &sbb);
376                         if (ret <= 0)
377                                 goto out;
378                         if (sbb.band != SBD_CHALLENGE) {
379                                 ret = -E_BAD_BAND;
380                                 free(sbb.iov.iov_base);
381                                         goto out;
382                         }
383                         n = sbb.iov.iov_len;
384                         PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
385                         ret = priv_decrypt(ct->key_file, crypt_buf,
386                                 sbb.iov.iov_base, n);
387                         free(sbb.iov.iov_base);
388                         if (ret < 0)
389                                 goto out;
390                 } else {
391                         ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
392                         if (ret < 0 || n == 0)
393                                 goto out;
394                         PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
395                         ret = priv_decrypt(ct->key_file, crypt_buf,
396                                 (unsigned char *)buf, n);
397                         if (ret < 0)
398                                 goto out;
399                 }
400                 ct->challenge_hash = para_malloc(HASH_SIZE);
401                 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
402                 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
403                 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
404                         SESSION_KEY_LEN);
405                 hash_to_asc(ct->challenge_hash, buf);
406                 PARA_INFO_LOG("--> %s\n", buf);
407                 ct->status = CL_RECEIVED_CHALLENGE;
408                 return;
409                 }
410         case CL_RECEIVED_CHALLENGE:
411                 if (ct->use_sideband) {
412                         ret = send_sb(ct, ct->challenge_hash, HASH_SIZE,
413                                 SBD_CHALLENGE_RESPONSE, false);
414                         if (ret != 0)
415                                 ct->challenge_hash = NULL;
416                         if (ret <= 0)
417                                 goto out;
418                 } else {
419                         ret = write_all(ct->scc.fd, (char *)ct->challenge_hash, HASH_SIZE);
420                         if (ret < 0)
421                                 goto out;
422                 }
423                 ct->status = CL_SENT_CH_RESPONSE;
424                 goto out;
425         case CL_SENT_CH_RESPONSE: /* read server response */
426                 {
427                 if (ct->use_sideband) {
428                         struct sb_buffer sbb;
429                         ret = recv_sb(ct, &s->rfds, &sbb);
430                         if (ret <= 0)
431                                 goto out;
432                         free(sbb.iov.iov_base);
433                         if (sbb.band != SBD_PROCEED)
434                                 ret = -E_BAD_BAND;
435                         else
436                                 ct->status = CL_RECEIVED_PROCEED;
437                         goto out;
438                 }
439                 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
440                 if (ret < 0 || n == 0)
441                         goto out;
442                 /* check if server has sent "Proceed" message */
443                 ret = -E_CLIENT_AUTH;
444                 if (n < PROCEED_MSG_LEN)
445                         goto out;
446                 if (!strstr(buf, PROCEED_MSG))
447                         goto out;
448                 ct->status = CL_RECEIVED_PROCEED;
449                 return;
450                 }
451         case CL_RECEIVED_PROCEED: /* concat args and send command */
452                 {
453                 int i;
454                 char *command = NULL;
455                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
456                         return;
457                 if (ct->use_sideband) {
458                         ret = send_sb_command(ct);
459                         if (ret <= 0)
460                                 goto out;
461                         ct->status = CL_SENT_COMMAND;
462                         return;
463                 }
464                 for (i = 0; i < ct->conf.inputs_num; i++) {
465                         char *tmp = command;
466                         command = make_message("%s\n%s", command?
467                                 command : "", ct->conf.inputs[i]);
468                         free(tmp);
469                 }
470                 command = para_strcat(command, EOC_MSG "\n");
471                 PARA_DEBUG_LOG("--> %s\n", command);
472                 ret = sc_send_buffer(&ct->scc, command);
473                 free(command);
474                 if (ret < 0)
475                         goto out;
476                 ct->status = CL_SENT_COMMAND;
477                 return;
478                 }
479         case CL_SENT_COMMAND:
480                 {
481                 char *buf2;
482                 if (ct->use_sideband) {
483                         struct sb_buffer sbb;
484                         ret = recv_sb(ct, &s->rfds, &sbb);
485                         if (ret <= 0)
486                                 goto out;
487                         if (sbb.band == SBD_AWAITING_DATA) {
488                                 ct->status = CL_SENDING;
489                                 free(sbb.iov.iov_base);
490                                 goto out;
491                         }
492                         ct->status = CL_RECEIVING;
493                         ret = dispatch_sbb(ct, &sbb);
494                         goto out;
495                 }
496                 /* can not use "buf" here because we need a malloced buffer */
497                 buf2 = para_malloc(CLIENT_BUFSIZE);
498                 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
499                 if (n > 0) {
500                         if (strstr(buf2, AWAITING_DATA_MSG)) {
501                                 free(buf2);
502                                 ct->status = CL_SENDING;
503                                 return;
504                         }
505                         ct->status = CL_RECEIVING;
506                         btr_add_output(buf2, n, btrn);
507                 } else
508                         free(buf2);
509                 goto out;
510                 }
511         case CL_SENDING:
512                 {
513                 char *buf2;
514                 size_t sz;
515                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
516                 if (ret < 0)
517                         goto out;
518                 if (ret == 0)
519                         return;
520                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
521                         return;
522                 sz = btr_next_buffer(btrn, &buf2);
523                 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
524                 if (ret < 0)
525                         goto out;
526                 btr_consume(btrn, sz);
527                 return;
528                 }
529         case CL_RECEIVING:
530                 {
531                 char *buf2;
532                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
533                 if (ret < 0)
534                         goto out;
535                 if (ret == 0)
536                         return;
537                 /*
538                  * The FD_ISSET() is not strictly necessary, but is allows us
539                  * to skip the malloc below if there is nothing to read anyway.
540                  */
541                 if (!FD_ISSET(ct->scc.fd, &s->rfds))
542                         return;
543                 if (ct->use_sideband) {
544                         struct sb_buffer sbb;
545                         ret = recv_sb(ct, &s->rfds, &sbb);
546                         if (ret > 0)
547                                 ret = dispatch_sbb(ct, &sbb);
548                         goto out;
549                 }
550                 buf2 = para_malloc(CLIENT_BUFSIZE);
551                 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
552                 if (n > 0) {
553                         buf2 = para_realloc(buf2, n);
554                         btr_add_output(buf2, n, btrn);
555                 } else
556                         free(buf2);
557                 goto out;
558                 }
559         }
560 out:
561         t->error = ret;
562         if (ret < 0) {
563                 if (!ct->use_sideband && ret != -E_SERVER_EOF &&
564                                 ret != -E_BTR_EOF && ret != -E_EOF)
565                         PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
566                 btr_remove_node(&ct->btrn);
567         }
568 }
569
570 /**
571  * Connect to para_server and register the client task.
572  *
573  * \param ct The initialized client task structure.
574  * \param s The scheduler instance to register the client task to.
575  * \param parent The parent node of the client btr node.
576  * \param child The child node of the client node.
577  *
578  * The client task structure given by \a ct  must be allocated and initialized
579  * by \ref client_parse_config() before this function is called.
580  *
581  * \return Standard.
582  */
583 int client_connect(struct client_task *ct, struct sched *s,
584                 struct btr_node *parent, struct btr_node *child)
585 {
586         int ret;
587
588         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
589                 ct->conf.server_port_arg);
590         ct->scc.fd = -1;
591         ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
592                                                ct->conf.server_port_arg);
593         if (ret < 0)
594                 return ret;
595         ct->scc.fd = ret;
596         ret = mark_fd_nonblocking(ct->scc.fd);
597         if (ret < 0)
598                 goto err_out;
599         ct->status = CL_CONNECTED;
600         ct->btrn = btr_new_node(&(struct btr_node_description)
601                 EMBRACE(.name = "client", .parent = parent, .child = child));
602         ct->task.pre_select = client_pre_select;
603         ct->task.post_select = client_post_select;
604         ct->task.error = 0;
605         sprintf(ct->task.status, "client");
606         register_task(s, &ct->task);
607         return 1;
608 err_out:
609         close(ct->scc.fd);
610         ct->scc.fd = -1;
611         return ret;
612 }
613
614 /**
615  * Parse a client configuration.
616  *
617  * \param argc Usual argument count.
618  * \param argv Usual argument vector.
619  * \param ct_ptr Filled in by this function.
620  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
621  *
622  * This checks the command line options given by \a argc and \a argv, sets
623  * default values for the user name and the name of the rsa key file and reads
624  * further options from the config file.
625  *
626  * Upon successful return, \a ct_ptr points to a dynamically allocated and
627  * initialized client task struct.
628  *
629  * \return The number of non-option arguments in \a argc/argv on success,
630  * negative on errors.
631  */
632 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
633                 int *loglevel)
634 {
635         char *home = para_homedir();
636         int ret;
637         struct client_task *ct = para_calloc(sizeof(struct client_task));
638
639         *ct_ptr = ct;
640         ct->scc.fd = -1;
641         ret = -E_CLIENT_SYNTAX;
642         if (client_cmdline_parser(argc, argv, &ct->conf))
643                 goto out;
644         HANDLE_VERSION_FLAG("client", ct->conf);
645
646         ct->config_file = ct->conf.config_file_given?
647                 para_strdup(ct->conf.config_file_arg) :
648                 make_message("%s/.paraslash/client.conf", home);
649         ret = file_exists(ct->config_file);
650         if (!ret && ct->conf.config_file_given) {
651                 ret = -E_NO_CONFIG;
652                 goto out;
653         }
654         if (ret) {
655                 struct client_cmdline_parser_params params = {
656                         .override = 0,
657                         .initialize = 0,
658                         .check_required = 0,
659                         .check_ambiguity = 0,
660                         .print_errors = 0
661                 };
662                 ret = -E_BAD_CONFIG;
663                 if (client_cmdline_parser_config_file(ct->config_file,
664                         &ct->conf, &params))
665                         goto out;
666         }
667         ct->user = ct->conf.user_given?
668                 para_strdup(ct->conf.user_arg) : para_logname();
669
670         if (ct->conf.key_file_given)
671                 ct->key_file = para_strdup(ct->conf.key_file_arg);
672         else {
673                 ct->key_file = make_message("%s/.paraslash/key.%s",
674                         home, ct->user);
675                 if (!file_exists(ct->key_file)) {
676                         free(ct->key_file);
677                         ct->key_file = make_message("%s/.ssh/id_rsa", home);
678                 }
679         }
680
681         if (loglevel)
682                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
683         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
684         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
685         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
686         ret = ct->conf.inputs_num;
687 out:
688         free(home);
689         if (ret < 0) {
690                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
691                 client_close(ct);
692                 *ct_ptr = NULL;
693         }
694         return ret;
695 }
696
697 /**
698  * Parse the client configuration and open a connection to para_server.
699  *
700  * \param argc See \ref client_parse_config.
701  * \param argv See \ref client_parse_config.
702  * \param ct_ptr See \ref client_parse_config.
703  * \param loglevel See \ref client_parse_config.
704  * \param parent See \ref client_connect().
705  * \param child See \ref client_connect().
706  * \param sched See \ref client_connect().
707  *
708  * This function combines client_parse_config() and client_connect(). It is
709  * considered a syntax error if no command was given, i.e. if the number
710  * of non-option arguments is zero.
711  *
712  * \return Standard.
713  */
714 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
715                 int *loglevel, struct btr_node *parent, struct btr_node *child,
716                 struct sched *sched)
717 {
718         int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
719
720         if (ret < 0)
721                 return ret;
722         if (ret == 0) {
723                 ret = -E_CLIENT_SYNTAX;
724                 goto fail;
725         }
726         ret = client_connect(*ct_ptr, sched, parent, child);
727         if (ret < 0)
728                 goto fail;
729         return 1;
730 fail:
731         client_close(*ct_ptr);
732         *ct_ptr = NULL;
733         return ret;
734 }