From 84c2e44d4c1ae46418cc8f7891b31e9df4131d96 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 7 Jun 2012 18:58:42 +0200 Subject: [PATCH] move instance code to a separate source file --- CMakeLists.txt | 2 +- instance.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ instance.h | 10 ++++++ service.c | 93 ++++---------------------------------------------- 4 files changed, 103 insertions(+), 87 deletions(-) create mode 100644 instance.c create mode 100644 instance.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4666b39..a903ac8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ IF(APPLE) LINK_DIRECTORIES(/opt/local/lib) ENDIF() -SET(SOURCES main.c ubus.c service.c) +SET(SOURCES main.c ubus.c service.c instance.c) SET(LIBS ubox ubus) diff --git a/instance.c b/instance.c new file mode 100644 index 0000000..e2c8b28 --- /dev/null +++ b/instance.c @@ -0,0 +1,85 @@ +#include "procd.h" +#include "service.h" +#include "instance.h" + +void +instance_start(struct service_instance *in) +{ + in->restart = false; +} + +static void +instance_timeout(struct uloop_timeout *t) +{ + struct service_instance *in; + + in = container_of(t, struct service_instance, timeout); + kill(in->proc.pid, SIGKILL); + uloop_process_delete(&in->proc); + in->proc.cb(&in->proc, -1); +} + +static void +instance_exit(struct uloop_process *p, int ret) +{ + struct service_instance *in; + + in = container_of(p, struct service_instance, proc); + uloop_timeout_cancel(&in->timeout); + if (in->restart) + instance_start(in); +} + +void +instance_stop(struct service_instance *in, bool restart) +{ + if (!in->proc.pending) + return; + + kill(in->proc.pid, SIGTERM); +} + +static bool +instance_config_changed(struct service_instance *in, struct service_instance *in_new) +{ + int len = blob_pad_len(in->config); + + if (len != blob_pad_len(in_new->config)) + return true; + + if (memcmp(in->config, in_new->config, blob_pad_len(in->config)) != 0) + return true; + + return false; +} + +bool +instance_update(struct service_instance *in, struct service_instance *in_new) +{ + bool changed = instance_config_changed(in, in_new); + + in->config = in_new->config; + if (!changed) + return false; + + instance_stop(in, true); + return true; +} + +void +instance_free(struct service_instance *in) +{ + uloop_process_delete(&in->proc); + uloop_timeout_cancel(&in->timeout); + free(in); +} + +void +instance_init(struct service_instance *in, struct blob_attr *config) +{ + in->config = config; + in->timeout.cb = instance_timeout; + in->proc.cb = instance_exit; +} + + diff --git a/instance.h b/instance.h new file mode 100644 index 0000000..8016a44 --- /dev/null +++ b/instance.h @@ -0,0 +1,10 @@ +#ifndef __PROCD_INSTANCE_H +#define __PROCD_INSTANCE_H + +void instance_start(struct service_instance *in); +void instance_stop(struct service_instance *in, bool restart); +bool instance_update(struct service_instance *in, struct service_instance *in_new); +void instance_init(struct service_instance *in, struct blob_attr *config); +void instance_free(struct service_instance *in); + +#endif diff --git a/service.c b/service.c index 360810e..46f846d 100644 --- a/service.c +++ b/service.c @@ -1,90 +1,11 @@ #include #include "procd.h" #include "service.h" +#include "instance.h" struct avl_tree services; static struct blob_buf b; -static void -start_instance(struct service_instance *in) -{ - in->restart = false; -} - -static void -instance_timeout(struct uloop_timeout *t) -{ - struct service_instance *in; - - in = container_of(t, struct service_instance, timeout); - kill(in->proc.pid, SIGKILL); - uloop_process_delete(&in->proc); - in->proc.cb(&in->proc, -1); -} - -static void -instance_exit(struct uloop_process *p, int ret) -{ - struct service_instance *in; - - in = container_of(p, struct service_instance, proc); - uloop_timeout_cancel(&in->timeout); - if (in->restart) - start_instance(in); -} - -static void -stop_instance(struct service_instance *in, bool restart) -{ - if (!in->proc.pending) - return; - - kill(in->proc.pid, SIGTERM); -} - -static bool -instance_config_changed(struct service_instance *in, struct service_instance *in_new) -{ - int len = blob_pad_len(in->config); - - if (len != blob_pad_len(in_new->config)) - return true; - - if (memcmp(in->config, in_new->config, blob_pad_len(in->config)) != 0) - return true; - - return false; -} - -static bool -update_instance(struct service_instance *in, struct service_instance *in_new) -{ - bool changed = instance_config_changed(in, in_new); - - in->config = in_new->config; - if (!changed) - return false; - - stop_instance(in, true); - return true; -} - -static void -free_instance(struct service_instance *in) -{ - uloop_process_delete(&in->proc); - uloop_timeout_cancel(&in->timeout); - free(in); -} - -static void -init_instance(struct service_instance *in, struct blob_attr *config) -{ - in->config = config; - in->timeout.cb = instance_timeout; - in->proc.cb = instance_exit; -} - static void service_instance_add(struct service *s, struct blob_attr *attr) { @@ -98,7 +19,7 @@ service_instance_add(struct service *s, struct blob_attr *attr) if (!in) return; - init_instance(in, attr); + instance_init(in, attr); vlist_add(&s->instances, &in->node, (void *) name); } @@ -115,13 +36,13 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new, in_n = container_of(node_new, struct service_instance, node); if (in_o && in_n) { - update_instance(in_o, in_n); - free_instance(in_n); + instance_update(in_o, in_n); + instance_free(in_n); } else if (in_o) { - stop_instance(in_o, false); - free_instance(in_o); + instance_stop(in_o, false); + instance_free(in_o); } else if (in_n) { - start_instance(in_n); + instance_start(in_n); } } -- 2.30.2