From 25e7e297e36e05514b718de88b95b5b3512aaec4 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Tue, 9 May 2017 11:42:17 +0200 Subject: [PATCH] Add a decision maker to restart a service when it leaks resources This is a very simple decision maker which always restarts a service if it's leaking resources. Signed-off-by: Krzysztof Opasiak --- Makefile.am | 2 + src/decision_makers/rv_dm.c | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/decision_makers/rv_dm.c diff --git a/Makefile.am b/Makefile.am index 5e01537..6f953d8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,6 +8,7 @@ AM_CPPFLAGS = \ -DSYSCONFDIR=\""$(sysconfdir)"\" \ -I${top_srcdir}/src \ -I${top_srcdir}/src/core \ + -I${top_srcdir}/src/decision_makers \ -I${top_srcdir}/src/event_types \ -I${top_srcdir}/src/listeners \ -I${top_srcdir}/src/util @@ -37,6 +38,7 @@ faultd_SOURCES = \ src/core/event_processor.c \ src/core/service.c \ src/core/module.c \ + src/decision_makers/rv_dm.c \ src/event_types/decision_made_event.c \ src/event_types/resource_violation_event.c \ src/faultd.c \ diff --git a/src/decision_makers/rv_dm.c b/src/decision_makers/rv_dm.c new file mode 100644 index 0000000..8596fb0 --- /dev/null +++ b/src/decision_makers/rv_dm.c @@ -0,0 +1,93 @@ +/* + * faultd + * + * Copyright © 2017 Samsung Electronics + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include + +#include "resource_violation_event.h" +#include "decision_made_event.h" +#include "action.h" +#include "event_processor.h" +#include "log.h" + +#define MODULE_NAME "resource_violation_decision_maker" + +static int rv_event_match(struct faultd_event_handler *handler, + struct faultd_event *ev) +{ + /* For now we handle here only resource violation events */ + return faultd_event_is_of_type(ev, RESOURCE_VIOLATION_EVENT_ID); +} + +static int rv_make_decision(struct faultd_event_handler *handler) +{ + struct faultd_event *ev = nqueue_pop(&handler->event_queue, + struct faultd_event, + nq_node); + struct resource_violation_event *rv_ev = to_resource_violation_event(ev); + struct faultd_event *new_ev; + /* For now this is just hardcoded to restart the service */ + struct dm_event_data ev_data = { + .reason = ev, + .who_made = MODULE_NAME, + .action = FAULTD_ACTION_SERVICE_RESTART_ID, + .action_data_release = free, + }; + int ret; + + ev_data.action_data = strdup(rv_ev->service.name); + if (!ev_data.action_data) { + log_error("Unable to duplicate service name"); + faultd_event_unref(ev); + return -ENOMEM; + } + + ret = faultd_event_create(DECISION_MADE_EVENT_ID, &ev_data, &new_ev); + faultd_event_unref(ev); + if (ret) { + log_error("Unable to create event"); + goto free_action_data; + } + + ret = event_processor_report_event(new_ev); + if (ret) { + log_error("Unable to report event"); + goto put_new_event; + } + + return 0; + +put_new_event: + faultd_event_unref(new_ev); + return 0; + +free_action_data: + free(ev_data.action_data); + return 0; +} + +static struct faultd_event_handler resource_violation_event_handler = { + .name = MODULE_NAME, + .event_match = rv_event_match, + .handle_event = rv_make_decision, + + .node = LIST_HEAD_INIT(resource_violation_event_handler.node), +}; + +FAULTD_EVENT_HANDLER_REGISTER(resource_violation_event_handler, + resource_violation_eh, + FAULTD_MODULE_TYPE_DECISION_MAKER) -- 2.34.1