From: Neil Brown Date: Wed, 7 Jun 2006 11:14:26 +0000 (+1000) Subject: Split init_state out of copy_state X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=55f98c450fdf01be6e09a2c87c3d4b1f41dc01c4;p=metad.git Split init_state out of copy_state 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. --- diff --git a/daemon.c b/daemon.c index 02c7c98..7692c49 100644 --- 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 033d4b2..7c4f885 100644 --- 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 */ diff --git a/service.c b/service.c index 3ce6828..9850921 100644 --- 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; } diff --git a/stream.c b/stream.c index d350499..9e2c58d 100644 --- 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,