+ for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
+ struct client *c = me->oserver->clients[i];
+ if (!c || c == me)
+ continue;
+ if (c->subclient == -1 && input_finished(c->question)) {
+ me->oracle = c->id;
+ c->subclient = me->id;
+ return true;
+ }
+ }
+ return false;
+}
+
+static void wakeup(struct client *c)
+{
+ tevent_fd_set_flags(c->fde, state_flag_map[c->state]);
+}
+
+static void service_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags, void *_c)
+{
+ struct client *c = _c;
+ ssize_t len;
+
+ switch (c->state) {
+ case SENDING_GREETING:
+ if (!send_string(c, "Welcome. Please ask your question.\n"))
+ goto fail;
+ break;
+ case RECEIVING_USER_QUESTION:
+ len = read_string(c->fd, &c->question);
+ if (len <= 0)
+ goto fail;
+ if (input_finished(c->question))
+ set_state(c, SENDING_OTHER_QUESTION_PREFIX);
+ break;
+ case SENDING_OTHER_QUESTION_PREFIX:
+ if (c->subclient == -1)
+ goto need_subclient;
+ if (!send_string(c, "While the Oracle ponders,"
+ " please answer the following question:\n"))
+ goto fail;
+ break;
+ case SENDING_OTHER_QUESTION:
+ if (c->subclient == -1)
+ goto need_subclient;
+ if (!send_string(c,
+ c->oserver->clients[c->subclient]->question))
+ goto fail;
+ break;
+ case RECEIVING_OTHER_ANSWER:
+ if (c->subclient == -1)
+ goto need_subclient;
+ len = read_string(c->fd,
+ &c->oserver->clients[c->subclient]->answer);
+ if (len <= 0)
+ goto fail;
+ if (input_finished(c->oserver->clients[c->subclient]->answer)) {
+ set_state(c, SENDING_ANSWER_PREFIX);
+ wakeup(c->oserver->clients[c->subclient]);
+ }
+ break;
+ case SENDING_ANSWER_PREFIX:
+ if (!input_finished(c->answer))
+ goto need_answer;
+ if (!send_string(c, "The Oracle spake thus:\n"))
+ goto fail;
+ break;
+ case SENDING_ANSWER:
+ if (!send_string(c, c->answer))
+ goto fail;
+ break;
+ default:
+ goto fail;
+ }
+
+ if (c->state != FINISHED)
+ return;
+fail:
+ talloc_free(c);
+ return;
+
+need_subclient:
+ if (!get_subclient(c)) {
+ /* We can't get one: go to sleep until someone find_oracle() */
+ tevent_fd_set_flags(c->fde, 0);
+ } else
+ /* In case they are waiting... */
+ wakeup(c->oserver->clients[c->subclient]);
+ return;
+
+need_answer:
+ /* If we don't have an oracle and find one, that's OK. */
+ if (c->oracle == -1 && get_oracle(c)) {
+ /* In case they are waiting... */
+ wakeup(c->oserver->clients[c->oracle]);
+ return;
+ }
+
+ /* Either our oracle is not finished, or we don't have one: sleep. */
+ tevent_fd_set_flags(c->fde, 0);
+}
+
+static int cleanup_client(struct client *client)
+{
+
+ /* We were an oracle? */
+ if (client->subclient >= 0)
+ client->oserver->clients[client->subclient]->oracle = -1;
+
+ /* We had an oracle? */
+ if (client->oracle >= 0)
+ client->oserver->clients[client->oracle]->subclient = -1;
+
+ assert(client->oserver->clients[client->id] == client);
+ client->oserver->clients[client->id] = NULL;
+ return 0;
+}
+
+static void add_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags, void *_oserver)
+{
+ struct oserver *oserver = _oserver;
+ struct client *client;
+
+ client = talloc(oserver, struct client);
+ client->fd = accept(oserver->fd, NULL, 0);
+ if (client->fd < 0)
+ err(1, "Accepting client connection");
+
+ client->state = SENDING_GREETING;
+ client->bytes_sent = 0;
+ client->question = talloc_strdup(client, "");
+ client->oserver = oserver;
+ client->oracle = -1;
+ client->subclient = -1;
+ client->answer = talloc_strdup(client, "");
+ client->fde = tevent_add_fd(ev, client, client->fd,
+ state_flag_map[client->state],
+ service_client, client);
+ tevent_fd_set_auto_close(client->fde);
+
+ /* Find empty slot in array for this client. */
+ for (client->id = 0; oserver->clients[client->id]; client->id++);
+ oserver->clients[client->id] = client;
+ talloc_set_destructor(client, cleanup_client);
+
+ /* Full? Stop listening... */
+ if (client->id == ARRAY_SIZE(oserver->clients)-1)
+ tevent_fd_set_flags(oserver->fde, 0);
+}
+
+static void clear_clients(struct oserver *oserver)
+{
+ memset(oserver->clients, 0,
+ ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
+}
+
+static int destroy_oserver(struct oserver *oserver)
+{
+ close(oserver->fd);
+ return 0;
+}
+
+static void talloc_dump(struct tevent_context *ev,
+ struct tevent_signal *se,
+ int signum,
+ int count,
+ void *siginfo,
+ void *_oserver)
+{
+ struct oserver *oserver = _oserver;
+ FILE *f;
+
+ /* Fork off a child for the report, so we aren't stopped. */
+ if (fork() == 0) {
+ f = fopen("/var/run/oserver/talloc.dump", "w");
+ if (f) {
+ talloc_report_full(oserver, f);
+ fclose(f);