]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/oserver.c
317a01cc5e2f177bcf7b9337c88560958b85d035
[ccan-lca-2011.git] / ccan / oserver / oserver.c
1 #include <ccan/oserver/oserver.h>
2 #include <ccan/oserver/oserver_types.h>
3 #include <ccan/read_write_all/read_write_all.h>
4 #include <ccan/opt/opt.h>
5 #include <ccan/tevent/tevent.h>
6 #include <ccan/array_size/array_size.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netinet/tcp.h>
11 #include <err.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <assert.h>
20
21 static uint16_t state_flag_map[] = {
22         [SENDING_GREETING]              = TEVENT_FD_WRITE,
23         [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
24         [SENDING_OTHER_QUESTION_PREFIX] = TEVENT_FD_WRITE,
25         [SENDING_OTHER_QUESTION]        = TEVENT_FD_WRITE,
26         [RECEIVING_OTHER_ANSWER]        = TEVENT_FD_READ,
27         [SENDING_ANSWER_PREFIX]         = TEVENT_FD_WRITE,
28         [SENDING_ANSWER]                = TEVENT_FD_WRITE,
29         [FINISHED]                      = 0
30 };
31
32 static ssize_t write_string(int fd, const char *str)
33 {
34         return write(fd, str, strlen(str));
35 }
36
37 static ssize_t read_string(int fd, char **buf)
38 {
39         ssize_t ret, len, maxlen;
40
41         len = strlen(*buf);
42         maxlen = talloc_array_length(*buf);
43
44         if (maxlen < len + 100) {
45                 maxlen += 100;
46                 *buf = talloc_realloc(NULL, *buf, char, maxlen);
47         }
48
49         ret = read(fd, *buf + len, maxlen - len - 1);
50         if (ret >= 0)
51                 (*buf)[len + ret] = '\0';
52         return ret;
53 }
54
55 static bool input_finished(const char *str)
56 {
57         return strchr(str, '\n');
58 }
59
60 /* Update state, and set our READ/WRITE flags appropriately. */
61 static void set_state(struct client *c, enum state state)
62 {
63         c->state = state;
64         tevent_fd_set_flags(c->fde, state_flag_map[state]);
65 }
66
67 /* Returns false on error, increments state on finishing string. */
68 static bool send_string(struct client *c, const char *str)
69 {
70         ssize_t len = write_string(c->fd, str + c->bytes_sent);
71         if (len < 0)
72                 return false;
73         c->bytes_sent += len;
74         if (c->bytes_sent == strlen(str)) {
75                 c->bytes_sent = 0;
76                 set_state(c, c->state+1);
77         }
78         return true;
79 }
80
81 static bool get_subclient(struct client *me)
82 {
83         unsigned int i;
84
85         for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
86                 struct client *c = me->oserver->clients[i];
87                 if (!c || c == me)
88                         continue;
89                 if (c->oracle == NULL && input_finished(c->question)) {
90                         me->subclient = c;
91                         c->oracle = me;
92                         return true;
93                 }
94         }
95         return false;
96 }
97
98 static bool get_oracle(struct client *me)
99 {
100         unsigned int i;
101
102         for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
103                 struct client *c = me->oserver->clients[i];
104                 if (!c || c == me)
105                         continue;
106                 if (c->subclient == NULL && input_finished(c->question)) {
107                         me->oracle = c;
108                         c->subclient = me;
109                         return true;
110                 }
111         }
112         return false;
113 }
114
115 static void wakeup(struct client *c)
116 {
117         tevent_fd_set_flags(c->fde, state_flag_map[c->state]);
118 }
119
120 static void service_client(struct tevent_context *ev,
121                            struct tevent_fd *fde, uint16_t flags, void *_c)
122 {
123         struct client *c = _c;
124         ssize_t len;
125
126         switch (c->state) {
127         case SENDING_GREETING:
128                 if (!send_string(c, "Welcome.  Please ask your question.\n"))
129                         goto fail;
130                 break;
131         case RECEIVING_USER_QUESTION:
132                 len = read_string(c->fd, &c->question);
133                 if (len <= 0)
134                         goto fail;
135                 if (input_finished(c->question))
136                         set_state(c, SENDING_OTHER_QUESTION_PREFIX);
137                 break;
138         case SENDING_OTHER_QUESTION_PREFIX:
139                 if (!c->subclient)
140                         goto need_subclient;
141                 if (!send_string(c, "While the Oracle ponders,"
142                                  " please answer the following question:\n"))
143                         goto fail;
144                 break;
145         case SENDING_OTHER_QUESTION:
146                 if (!c->subclient)
147                         goto need_subclient;
148                 if (!send_string(c, c->subclient->question))
149                         goto fail;
150                 break;
151         case RECEIVING_OTHER_ANSWER:
152                 if (!c->subclient)
153                         goto need_subclient;
154                 len = read_string(c->fd, &c->subclient->answer);
155                 if (len <= 0)
156                         goto fail;
157                 if (input_finished(c->subclient->answer)) {
158                         set_state(c, SENDING_ANSWER_PREFIX);
159                         wakeup(c->subclient);
160                 }
161                 break;
162         case SENDING_ANSWER_PREFIX:
163                 if (!input_finished(c->answer))
164                         goto need_answer;
165                 if (!send_string(c, "The Oracle spake thus:\n"))
166                         goto fail;
167                 break;
168         case SENDING_ANSWER:
169                 if (!send_string(c, c->answer))
170                         goto fail;
171                 break;
172         default:
173                 goto fail;
174         }
175
176         if (c->state != FINISHED)
177                 return;
178 fail:
179         talloc_free(c);
180         return;
181
182 need_subclient:
183         if (!get_subclient(c)) {
184                 /* We can't get one: go to sleep until someone find_oracle() */
185                 tevent_fd_set_flags(c->fde, 0);
186         } else
187                 /* In case they are waiting... */
188                 wakeup(c->subclient);
189         return;
190
191 need_answer:
192         /* If we don't have an oracle and find one, that's OK. */
193         if (!c->oracle && get_oracle(c)) {
194                 /* In case they are waiting... */
195                 wakeup(c->oracle);
196                 return;
197         }
198
199         /* Either our oracle is not finished, or we don't have one: sleep. */
200         tevent_fd_set_flags(c->fde, 0);
201 }
202
203 static int cleanup_client(struct client *client)
204 {
205         unsigned int i;
206
207         /* We were an oracle? */
208         if (client->subclient)
209                 client->subclient->oracle = NULL;
210
211         /* We had an oracle? */
212         if (client->oracle)
213                 client->oracle->subclient = NULL;
214
215         for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) {
216                 if (client->oserver->clients[i] == client) {
217                         client->oserver->clients[i] = NULL;
218                         tevent_fd_set_flags(client->oserver->fde,
219                                             TEVENT_FD_READ);
220                         return 0;
221                 }
222         }
223         abort();
224 }
225
226 static void add_client(struct tevent_context *ev,
227                        struct tevent_fd *fde, uint16_t flags, void *_oserver)
228 {
229         struct oserver *oserver = _oserver;
230         struct client *client;
231         unsigned int i;
232
233         client = talloc(oserver, struct client);
234         client->fd = accept(oserver->fd, NULL, 0);
235         if (client->fd < 0)
236                 err(1, "Accepting client connection");
237
238         client->state = SENDING_GREETING;
239         client->bytes_sent = 0;
240         client->question = talloc_strdup(client, "");
241         client->oserver = oserver;
242         client->oracle = NULL;
243         client->subclient = NULL;
244         client->answer = talloc_strdup(client, "");
245         client->fde = tevent_add_fd(ev, client, client->fd,
246                                     state_flag_map[client->state],
247                                     service_client, client);
248         tevent_fd_set_auto_close(client->fde);
249
250         /* Find empty slot in array for this client. */
251         for (i = 0; oserver->clients[i]; i++);
252         oserver->clients[i] = client;
253         talloc_set_destructor(client, cleanup_client);
254
255         /* Full?  Stop listening... */
256         if (i == ARRAY_SIZE(oserver->clients)-1)
257                 tevent_fd_set_flags(oserver->fde, 0);
258 }
259
260 static void clear_clients(struct oserver *oserver)
261 {
262         memset(oserver->clients, 0,
263                ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
264 }
265
266 static int destroy_oserver(struct oserver *oserver)
267 {
268         close(oserver->fd);
269         return 0;
270 }
271
272 static void talloc_dump(struct tevent_context *ev,
273                         struct tevent_signal *se,
274                         int signum,
275                         int count,
276                         void *siginfo,
277                         void *_oserver)
278 {
279         struct oserver *oserver = _oserver;
280         FILE *f;
281
282         /* Fork off a child for the report, so we aren't stopped. */
283         if (fork() == 0) {
284                 f = fopen("/var/run/oserver/talloc.dump", "w");
285                 if (f) {
286                         talloc_report_full(oserver, f);
287                         fclose(f);
288                 }
289                 _exit(0);
290         }
291 }
292
293 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
294 {
295         struct oserver *oserver;
296         int one = 1;
297         union {
298                 struct sockaddr addr;
299                 struct sockaddr_in in;
300         } u;
301
302         oserver = talloc(ev, struct oserver);
303         clear_clients(oserver);
304         oserver->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
305         if (oserver->fd < 0) {
306                 talloc_free(oserver);
307                 return NULL;
308         }
309
310         talloc_set_destructor(oserver, destroy_oserver);
311
312         if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
313                        &one, sizeof(one)))
314                 warn("Setting socket reuse");
315
316         u.in.sin_family = AF_INET;
317         u.in.sin_port = htons(port);
318         u.in.sin_addr.s_addr = INADDR_ANY;
319         if (bind(oserver->fd, &u.addr, sizeof(u.in)) == -1) {
320                 talloc_free(oserver);
321                 return NULL;
322         }
323
324         if (listen(oserver->fd, 0) != 0) {
325                 talloc_free(oserver);
326                 return NULL;
327         }
328
329         oserver->fde = tevent_add_fd(ev, oserver, oserver->fd,
330                                      TEVENT_FD_READ, add_client, oserver);
331         if (!oserver->fde) {
332                 talloc_free(oserver);
333                 return NULL;
334         }
335
336         /* Don't kill us if client dies. */
337         signal(SIGPIPE, SIG_IGN);
338
339         /* Show talloc tree on SIGUSR1. */
340         tevent_add_signal(ev, oserver, SIGUSR1, SA_RESTART,
341                           talloc_dump, oserver);
342
343         return oserver;
344 }