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