X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Foserver%2Foserver.c;h=50b49bd5f94746c8259fb3c609c285c5d68684c9;hb=ce689e269acaabd77dcb82166340441a4a00f9a1;hp=ad8f31538ec31663c90624490217b1c6936f5294;hpb=5cde12ca405f988a8a35b4281c90fbaf53f5464f;p=ccan-lca-2011.git diff --git a/ccan/oserver/oserver.c b/ccan/oserver/oserver.c index ad8f315..50b49bd 100644 --- a/ccan/oserver/oserver.c +++ b/ccan/oserver/oserver.c @@ -1,8 +1,11 @@ #include +#include +#include #include #include #include #include +#include #include #include #include @@ -16,17 +19,9 @@ #include #include #include - -enum state { - SENDING_GREETING, - RECEIVING_USER_QUESTION, - SENDING_OTHER_QUESTION_PREFIX, - SENDING_OTHER_QUESTION, - RECEIVING_OTHER_ANSWER, - SENDING_ANSWER_PREFIX, - SENDING_ANSWER, - FINISHED -}; +#include +#include +#include static uint16_t state_flag_map[] = { [SENDING_GREETING] = TEVENT_FD_WRITE, @@ -39,33 +34,6 @@ static uint16_t state_flag_map[] = { [FINISHED] = 0 }; -struct client { - /* What are we doing today, brain? */ - enum state state; - /* Our event info, and the file descriptor. */ - struct tevent_fd *fde; - int fd; - /* The question we read from client. */ - char *question; - /* The answer to the client. */ - char *answer; - /* How many bytes of the reply we sent so far. */ - size_t bytes_sent; - /* Our server. */ - struct oserver *oserver; - /* Whose question this client is answering. */ - struct client *subclient; - /* Who is answering our question. */ - struct client *oracle; -}; - -struct oserver { - /* 5 clients should be enough for anybody! */ - struct client *clients[5]; - int fd; - struct tevent_fd *fde; -}; - static ssize_t write_string(int fd, const char *str) { return write(fd, str, strlen(str)); @@ -123,9 +91,9 @@ static bool get_subclient(struct client *me) struct client *c = me->oserver->clients[i]; if (!c || c == me) continue; - if (c->oracle == NULL && input_finished(c->question)) { - me->subclient = c; - c->oracle = me; + if (c->oracle == -1 && input_finished(c->question)) { + me->subclient = c->id; + c->oracle = me->id; return true; } } @@ -140,9 +108,9 @@ static bool get_oracle(struct client *me) struct client *c = me->oserver->clients[i]; if (!c || c == me) continue; - if (c->subclient == NULL && input_finished(c->question)) { - me->oracle = c; - c->subclient = me; + if (c->subclient == -1 && input_finished(c->question)) { + me->oracle = c->id; + c->subclient = me->id; return true; } } @@ -173,27 +141,29 @@ static void service_client(struct tevent_context *ev, set_state(c, SENDING_OTHER_QUESTION_PREFIX); break; case SENDING_OTHER_QUESTION_PREFIX: - if (!c->subclient) + 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) + if (c->subclient == -1) goto need_subclient; - if (!send_string(c, c->subclient->question)) + if (!send_string(c, + c->oserver->clients[c->subclient]->question)) goto fail; break; case RECEIVING_OTHER_ANSWER: - if (!c->subclient) + if (c->subclient == -1) goto need_subclient; - len = read_string(c->fd, &c->subclient->answer); + len = read_string(c->fd, + &c->oserver->clients[c->subclient]->answer); if (len <= 0) goto fail; - if (input_finished(c->subclient->answer)) { + if (input_finished(c->oserver->clients[c->subclient]->answer)) { set_state(c, SENDING_ANSWER_PREFIX); - wakeup(c->subclient); + wakeup(c->oserver->clients[c->subclient]); } break; case SENDING_ANSWER_PREFIX: @@ -222,14 +192,14 @@ need_subclient: tevent_fd_set_flags(c->fde, 0); } else /* In case they are waiting... */ - wakeup(c->subclient); + 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 && get_oracle(c)) { + if (c->oracle == -1 && get_oracle(c)) { /* In case they are waiting... */ - wakeup(c->oracle); + wakeup(c->oserver->clients[c->oracle]); return; } @@ -239,25 +209,18 @@ need_answer: static int cleanup_client(struct client *client) { - unsigned int i; /* We were an oracle? */ - if (client->subclient) - client->subclient->oracle = NULL; + if (client->subclient >= 0) + client->oserver->clients[client->subclient]->oracle = -1; /* We had an oracle? */ - if (client->oracle) - client->oracle->subclient = NULL; - - for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) { - if (client->oserver->clients[i] == client) { - client->oserver->clients[i] = NULL; - tevent_fd_set_flags(client->oserver->fde, - TEVENT_FD_READ); - return 0; - } - } - abort(); + 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, @@ -265,7 +228,6 @@ static void add_client(struct tevent_context *ev, { struct oserver *oserver = _oserver; struct client *client; - unsigned int i; client = talloc(oserver, struct client); client->fd = accept(oserver->fd, NULL, 0); @@ -276,8 +238,8 @@ static void add_client(struct tevent_context *ev, client->bytes_sent = 0; client->question = talloc_strdup(client, ""); client->oserver = oserver; - client->oracle = NULL; - client->subclient = NULL; + 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], @@ -285,12 +247,12 @@ static void add_client(struct tevent_context *ev, tevent_fd_set_auto_close(client->fde); /* Find empty slot in array for this client. */ - for (i = 0; oserver->clients[i]; i++); - oserver->clients[i] = 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 (i == ARRAY_SIZE(oserver->clients)-1) + if (client->id == ARRAY_SIZE(oserver->clients)-1) tevent_fd_set_flags(oserver->fde, 0); } @@ -327,7 +289,96 @@ static void talloc_dump(struct tevent_context *ev, } } -struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port) +static void dump(struct tevent_context *ev, + struct tevent_signal *se, + int signum, + int count, + void *siginfo, + void *_oserver) +{ + struct oserver *oserver = _oserver; + char *str; + int fd; + + str = cdump_bundle(ev, cdump_struct_oserver, oserver); + fd = open(oserver->dumpfile, O_CREAT|O_TRUNC|O_WRONLY, 0600); + write(fd, str, strlen(str)); + close(fd); + talloc_free(str); +} + +static bool load_file(struct oserver *oserver, const char *file) +{ + char *str; + + if (!file) + return false; + + str = grab_file(oserver, file, NULL); + if (!str) + return false; + + if (!cdump_unbundle(oserver, cdump_struct_oserver, oserver, str)) { + talloc_free(str); + return false; + } + talloc_free(str); + return true; +} + +static bool complete_server(struct tevent_context *ev, + struct oserver *oserver, const char *dumpfile) +{ + /* Re-set this even if restored from file, in case it changed. */ + oserver->dumpfile = dumpfile; + if (oserver->dumpfile) + tevent_add_signal(ev, oserver, SIGHUP, SA_RESTART, + dump, oserver); + + /* Don't kill us if client dies. */ + signal(SIGPIPE, SIG_IGN); + + /* Show talloc tree on SIGUSR1. */ + tevent_add_signal(ev, oserver, SIGUSR1, SA_RESTART, + talloc_dump, oserver); + + oserver->fde = tevent_add_fd(ev, oserver, oserver->fd, + TEVENT_FD_READ, add_client, oserver); + if (!oserver->fde) + return false; + return true; +} + +struct oserver *oserver_restore(struct tevent_context *ev, const char *dumpfile) +{ + unsigned int i; + struct oserver *oserver = talloc(ev, struct oserver); + if (!load_file(oserver, dumpfile)) { + talloc_free(oserver); + return NULL; + } + + /* Restore ignored fields in clients (fde and oserver). */ + for (i = 0; i < ARRAY_SIZE(oserver->clients); i++) { + struct client *client = oserver->clients[i]; + if (!client) + continue; + client->oserver = oserver; + client->fde = tevent_add_fd(ev, client, client->fd, + state_flag_map[client->state], + service_client, client); + tevent_fd_set_auto_close(client->fde); + } + + if (!complete_server(ev, oserver, dumpfile)) { + talloc_free(oserver); + return NULL; + } + return oserver; +} + +struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port, + const char *dumpfile) { struct oserver *oserver; int one = 1; @@ -363,19 +414,10 @@ struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port) return NULL; } - oserver->fde = tevent_add_fd(ev, oserver, oserver->fd, - TEVENT_FD_READ, add_client, oserver); - if (!oserver->fde) { + if (!complete_server(ev, oserver, dumpfile)) { talloc_free(oserver); return NULL; } - /* Don't kill us if client dies. */ - signal(SIGPIPE, SIG_IGN); - - /* Show talloc tree on SIGUSR1. */ - tevent_add_signal(ev, oserver, SIGUSR1, SA_RESTART, - talloc_dump, oserver); - return oserver; }