]> git.neil.brown.name Git - metad.git/commitdiff
Split init_state out of copy_state
authorNeil Brown <neilb@notabene.(none)>
Wed, 7 Jun 2006 11:14:26 +0000 (21:14 +1000)
committerNeil Brown <neilb@notabene.(none)>
Wed, 7 Jun 2006 11:14:26 +0000 (21:14 +1000)
copy_state was used to initial class-specific data (from==NULL)
and to copy state data from a previous instance.
These are quite different, and the later was implemented badly.

So split this into two separate methods and get it right.

daemon.c
metad.h
service.c
stream.c

index 02c7c98560e875a3fbb5749011b801f687244afa..7692c496b7c3ad3af2645447ab0f913c0a9dc87a 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -78,29 +78,23 @@ static void daemon_check(service_t sv)
                waituntil(c(sv)->last_start + c(sv)->period);
 }
 
+static void daemon_init(service_t to)
+{
+       /* create ->classinfo with defaults */
+       daemon_t n;
+
+       n = (daemon_t)malloc(sizeof(struct daemon_opts));
+       n->min = 0;
+       n->period = 0;
+       n->last_start = 0;
+       to->classinfo = n;
+}
+
 static void daemon_copy(service_t from, service_t to)
 {
-       /* copy the classinfo - min and period */
-       daemon_t n; // o;
-       if (from)
-       {
-               /* no special state to copy
-                * the new service should have parsed its own args
-                * - simonb 11nov2003
-                */
-               //o = from->classinfo;
-               //n->min = o->min;
-               //n->period = o->period;
-               //n->last_start = o->last_start;
-       }
-       else
-       {
-               n = (daemon_t)malloc(sizeof(struct daemon_opts));
-               n->min = 0;
-               n->period = 0;
-               n->last_start = 0;
-               to->classinfo = n;
-       }
+       /* copy the 'state' classinfo - last_start */
+
+       c(to)->last_start = c(from)->last_start;
 }
 
 static void daemon_freestate(service_t sv)
@@ -133,6 +127,7 @@ struct class daemon_class = {
        .c_process_opt  = daemon_opt,
        .register_service = daemon_register,
        .c_check_service= daemon_check,
+       .init_state     = daemon_init,
        .copy_state     = daemon_copy,
        .free_state     = daemon_freestate,
        .send_class     = daemon_send,
diff --git a/metad.h b/metad.h
index 033d4b2bc1053cb4cb29fa2eaaeae318b005a16d..7c4f88512a385c02c5e8281a9ef53954b1bce26f 100644 (file)
--- a/metad.h
+++ b/metad.h
@@ -38,6 +38,7 @@ typedef struct class {
        int  (*c_process_opt)();        /* function to processes options */
        void (*register_service)();     /* register the service if necessary */
        void (*c_check_service)();      /* check if anything needs to be done for a service */
+       void (*init_state)();           /* copy state from old service struct to new */
        void (*copy_state)();           /* copy state from old service struct to new */
        void (*free_state)();           /* free class dependant state */
        void (*send_class)();           /* send class info */
index 3ce682893acdd9f49ae08eb8488e924d867ba334..98509218ecb2c2bb1dfee691dbbc67d9fcd5ce10 100644 (file)
--- a/service.c
+++ b/service.c
@@ -325,7 +325,7 @@ service_t new_service(char *name, class_t class)
        sv->pending = 0;
        sv->proc_list = skip_new(proc_cmp, NULL, NULL);
        sv->next_hold = 2;
-       (*class->copy_state)(NULL, sv);
+       (*class->init_state)(sv);
        return sv;
 }
 
index d3504991127031547d7f1221edc5c72b9134d66f..9e2c58da3f52e80951670c449081cbe6df6f7000 100644 (file)
--- a/stream.c
+++ b/stream.c
@@ -127,28 +127,29 @@ static int stream_prefork(service_t sv)
        return 0;
 }
 
-static void stream_copy(service_t from, service_t to)
+static void stream_init(service_t to)
 {
-       stream_t n,o;
+       /* set up defaults */
+       stream_t n;
        n = (stream_t)malloc(sizeof(struct stream_opts));
-       if (from) {
-               o = from->classinfo;
-               n->proto = o->proto;
-               n->port = o->port;
-               n->sock = o->sock;
-               n->backlog = o->backlog;
-               o->sock = -1;
-               n->newsock = -1;
-       } else {
-               n->proto = IPPROTO_TCP;
-               n->backlog = 10;
-               n->port = 0;
-               n->sock = -1;
-               n->newsock = -1;
-       }
+
+       n->proto = IPPROTO_TCP;
+       n->backlog = 10;
+       n->port = 0;
+       n->sock = -1;
+       n->newsock = -1;
+
        to->classinfo = n;
 }
 
+static void stream_copy(service_t from, service_t to)
+{
+       /* copy state */
+
+       c(to)->sock = c(from)->sock;
+       c(from)->sock = -1;
+}
+
 static void stream_freestate(service_t sv)
 {
        stream_t s = sv->classinfo;
@@ -191,6 +192,7 @@ struct class stream_class = {
        .c_process_opt  = stream_opt,
        .register_service= stream_register,
        .c_check_service= stream_check,
+       .init_state     = stream_init,
        .copy_state     = stream_copy,
        .free_state     = stream_freestate,
        .send_class     = stream_send,