From: Konrad Kuchciak Date: Mon, 2 Oct 2017 11:39:14 +0000 (+0200) Subject: Move parse_service_config() from service.c to new json-config.c file X-Git-Tag: submit/tizen/20180222.151354~43 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e62339c41579ee94cf30f96947a3dfd6e36d485d;p=platform%2Fcore%2Fsystem%2Ffaultd.git Move parse_service_config() from service.c to new json-config.c file As other program like vip-generator need access to this functionality, it should belong to util, not core. Change-Id: I0b4b5c1e1aeec06720892fad279e808913d0c1a0 Signed-off-by: Konrad Kuchciak --- diff --git a/Makefile.am b/Makefile.am index 284d1e1..1b84322 100644 --- a/Makefile.am +++ b/Makefile.am @@ -91,6 +91,7 @@ faultd_SOURCES = \ src/util/common.c \ src/util/log.c \ src/util/notify_queue.c \ + src/util/json-config.c \ src/database/database_nop.c \ src/util/systemd_dbus.c @@ -207,6 +208,7 @@ test_SOURCES = \ src/util/systemd_dbus.c \ src/util/log.c \ src/util/common.c \ + src/util/json-config.c \ src/database/ejdb.c \ src/event_types/action_executed_event.c \ src/event_types/decision_made_event.c \ diff --git a/src/core/service.c b/src/core/service.c index 98fea5f..77ad814 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -31,6 +31,7 @@ #include "service.h" #include "common.h" #include "systemd_dbus.h" +#include "json-config.h" int systemd_service_init_by_pid(pid_t pid, struct systemd_service *s) { @@ -46,58 +47,6 @@ int systemd_service_init_by_pid(pid_t pid, struct systemd_service *s) return systemd_service_init(n, s); } -static int get_config_field(struct json_object *root, - const char *key, char **value) -{ - struct json_object *node; - const char *node_value; - json_bool rc; - int ret = 0; - - /* if there is no such node let's skip it */ - rc = json_object_object_get_ex(root, key, &node); - if (!rc) - return 0; - - if (!json_object_is_type(node, json_type_string)) { - log_error("Option %s has to be a string", key); - return -EINVAL; - } - - node_value = json_object_get_string(node); - - *value = strdup(node_value); - if (*value == NULL) { - log_error("Failed to duplicate string"); - ret = -ENOMEM; - } - - return ret; -} - -static int parse_service_config(const char *config_path, - struct systemd_service *s) -{ - struct json_object *root; - int ret; - - root = json_object_from_file(config_path); - if (is_error(root)) { - log_error("Could not create json object from file %s", config_path); - return -1; - } - - ret = get_config_field(root, "RecoveryUnit", &s->recovery_unit); - if (ret < 0) - goto out; - - ret = get_config_field(root, "ServiceType", &s->service_type); - -out: - json_object_put(root); - return ret; -} - static int get_service_config_path(const char *dbus_path, char **config_path) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; diff --git a/src/util/json-config.c b/src/util/json-config.c new file mode 100644 index 0000000..ace02bd --- /dev/null +++ b/src/util/json-config.c @@ -0,0 +1,76 @@ +/* + * This file is part of 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 + +#include "json-config.h" +#include "log.h" + +static int get_config_field(struct json_object *root, + const char *key, char **value) +{ + struct json_object *node; + const char *node_value; + json_bool rc; + int ret = 0; + + /* if there is no such node let's skip it */ + rc = json_object_object_get_ex(root, key, &node); + if (!rc) + return 0; + + if (!json_object_is_type(node, json_type_string)) { + log_error("Option %s has to be a string", key); + return -EINVAL; + } + + node_value = json_object_get_string(node); + + *value = strdup(node_value); + if (*value == NULL) { + log_error("Failed to duplicate string"); + ret = -ENOMEM; + } + + return ret; +} + +int parse_service_config(const char *config_path, + struct systemd_service *s) +{ + struct json_object *root; + int ret; + + root = json_object_from_file(config_path); + if (is_error(root)) { + log_error("Could not create json object from file %s", config_path); + return -1; + } + + ret = get_config_field(root, "RecoveryUnit", &s->recovery_unit); + if (ret < 0) + goto out; + + ret = get_config_field(root, "ServiceType", &s->service_type); + +out: + json_object_put(root); + return ret; +} diff --git a/src/util/json-config.h b/src/util/json-config.h new file mode 100644 index 0000000..06fa98c --- /dev/null +++ b/src/util/json-config.h @@ -0,0 +1,26 @@ +/* + * This file is part of 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. + */ + +#ifndef FAULTD_JSON_CONFIG_H +#define FAULTD_JSON_CONFIG_H + +#include "service.h" + +int parse_service_config(const char *config_path, struct systemd_service *s); + +#endif /* FAULTD_JSON_CONFIG_H */