fd.c: Simplify para_select().
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client.c the client program used to connect to para_server */
8
9 #include <openssl/rc4.h>
10
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "crypt.h"
15 #include "client.cmdline.h"
16 #include "string.h"
17 #include "stdin.h"
18 #include "stdout.h"
19 #include "client.h"
20 #include "error.h"
21
22 INIT_CLIENT_ERRLISTS;
23
24 static struct client_task *ct;
25 static struct stdin_task sit;
26 static struct stdout_task sot;
27
28 static void supervisor_post_select(__a_unused struct sched *s, struct task *t)
29 {
30         if (ct->task.error < 0) {
31                 t->error = ct->task.error;
32                 return;
33         }
34         if (ct->status == CL_SENDING) {
35                 stdin_set_defaults(&sit);
36                 sit.buf = para_malloc(sit.bufsize),
37                 register_task(&sit.task);
38                 ct->inbuf = sit.buf;
39                 ct->in_loaded = &sit.loaded;
40                 ct->in_error = &sit.task.error;
41                 t->error = -E_TASK_STARTED;
42                 return;
43         }
44         if (ct->status == CL_RECEIVING) {
45                 stdout_set_defaults(&sot);
46                 sot.bufp = &ct->buf;
47                 sot.loaded = &ct->loaded;
48                 sot.input_error = &ct->task.error;
49                 register_task(&sot.task);
50                 t->error = -E_TASK_STARTED;
51                 return;
52         }
53 }
54
55 static struct task svt = {
56         .post_select = supervisor_post_select,
57         .status = "supervisor task"
58 };
59
60 static int client_loglevel; /* loglevel */
61 INIT_STDERR_LOGGING(client_loglevel);
62
63
64 /**
65  * The client program to connect to para_server.
66  *
67  * \param argc Usual argument count.
68  * \param argv Usual argument vector.
69  *
70  * It registers two tasks: The client task that communicates with para_server
71  * and the standard out task that writes any output produced by the client task
72  * to standard out.
73  *
74  * \return EXIT_SUCCESS or EXIT_FAILURE
75  *
76  * \sa client_open(), stdout.c, stdout.h, para_client(1), para_server(1)
77  */
78 int main(int argc, char *argv[])
79 {
80
81         int ret;
82         static struct sched s;
83
84         s.default_timeout.tv_sec = 1;
85         s.default_timeout.tv_usec = 0;
86         ret = client_open(argc, argv, &ct, &client_loglevel);
87         if (ret < 0) /* can not use PARA_LOG here because ct is NULL */
88                 exit(EXIT_FAILURE);
89         register_task(&svt);
90         ret = schedule(&s);
91         if (ret < 0)
92                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
93         client_close(ct);
94         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
95 }