Move parse_service_config() from service.c to new json-config.c file 67/153867/2
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Mon, 2 Oct 2017 11:39:14 +0000 (13:39 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 11 Oct 2017 07:36:59 +0000 (09:36 +0200)
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 <k.kuchciak@samsung.com>
Makefile.am
src/core/service.c
src/util/json-config.c [new file with mode: 0644]
src/util/json-config.h [new file with mode: 0644]

index 284d1e1499bf6c2b3f77dfca6ed03705144863d2..1b84322fd2a1ffee6b6274830b41afde0253046f 100644 (file)
@@ -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 \
index 98fea5f2f5597e9bc2c5f58a72da4b3403e47abb..77ad814f67076f21dc46399a519a7573e4bdf6bd 100644 (file)
@@ -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 (file)
index 0000000..ace02bd
--- /dev/null
@@ -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 <string.h>
+#include <stdio.h>
+#include <json-c/json.h>
+
+#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 (file)
index 0000000..06fa98c
--- /dev/null
@@ -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 */