]> git.ozlabs.org Git - ccan-lca-2011.git/blobdiff - ccan/oserver/oserver.c
lca2011: mark types in header with CDUMP annotations.
[ccan-lca-2011.git] / ccan / oserver / oserver.c
index c293dd5178ed40651ab3b3b1d6269c2577af1e5e..317a01cc5e2f177bcf7b9337c88560958b85d035 100644 (file)
@@ -1,4 +1,5 @@
 #include <ccan/oserver/oserver.h>
+#include <ccan/oserver/oserver_types.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/opt/opt.h>
 #include <ccan/tevent/tevent.h>
 #include <signal.h>
 #include <assert.h>
 
-enum state {
-       SENDING_GREETING,
-       RECEIVING_USER_QUESTION,
-       SENDING_OTHER_QUESTION_PREFIX,
-       SENDING_OTHER_QUESTION,
-       RECEIVING_OTHER_ANSWER,
-       SENDING_ANSWER_PREFIX,
-       SENDING_ANSWER,
-       FINISHED
-};
-
 static uint16_t state_flag_map[] = {
        [SENDING_GREETING]              = TEVENT_FD_WRITE,
        [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
@@ -39,33 +29,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));
@@ -306,6 +269,27 @@ static int destroy_oserver(struct oserver *oserver)
        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);
+               }
+               _exit(0);
+       }
+}
+
 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
 {
        struct oserver *oserver;
@@ -352,5 +336,9 @@ struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
        /* 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;
 }