2 * This file is part of stability-monitor
4 * Copyright © 2019 Samsung Electronics
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <json-c/json.h>
29 #include "data_source.h"
32 static struct json_object *config = NULL;
35 int config_is_empty(void) {
36 return config == NULL;
39 struct json_object *config_get(void)
44 int config_load(const char *config_file)
46 config = json_object_from_file(config_file);
50 _D("Loaded config %s\n", config_file);
55 static int config_load_merge(const char *config_file)
57 struct json_object *obj;
60 obj = json_object_from_file(config_file);
62 _E("Unable to parse config %s", config_file);
66 json_object_object_foreach(obj, key, val) {
67 ret = _json_object_object_merge(config, key, val);
74 _D("Loaded config %s\n", config_file);
79 static int is_regular_file(const char *path)
83 if (stat(path, &st) < 0)
86 return S_ISREG(st.st_mode);
89 int config_load_dir(const char *config_dir)
92 struct dirent **filelist;
93 char full_path[PATH_MAX];
95 n = scandir(config_dir, &filelist, NULL, alphasort);
97 _W("Unable to load user config from %s: %m", config_dir);
101 for (int i = 0; i < n; i++) {
102 if (snprintf(full_path, PATH_MAX, "%s/%s", config_dir, filelist[i]->d_name) == -1)
103 _W("Unable to format dir path: %m");
104 else if (is_regular_file(full_path))
105 config_load_merge(full_path);
114 void config_print(void)
116 _D("Config tree:\n\n%s\n",
117 json_object_to_json_string_ext(config, JSON_C_TO_STRING_PRETTY));
120 static void _read_params(json_object *obj, struct proc_info *process)
122 struct data_source *ds;
124 json_object_object_foreach(obj, key, val) {
125 if (strcmp(key, "monitor") == 0) {
126 process->monitor = json_object_get_int(val) ? 1 : 0;
130 if (strcmp(key, "kill") == 0) {
131 process->kill = json_object_get_int(val) ? 1 : 0;
135 if (strcmp(key, "report") == 0) {
136 process->report = json_object_get_int(val) ? 1 : 0;
140 if (strcmp(key, "print_current") == 0) {
141 process->print_current = json_object_get_int(val) ? 1 : 0;
145 if (strcmp(key, "sampling_rate") == 0) {
146 process->sampling_rate = json_object_get_double(val);
150 if (strcmp(key, "apply_to_children") == 0) {
151 process->apply_to_children = json_object_get_int(val) ? 1 : 0;
155 for (int i = 0; i < data_sources_get_n(); i++) {
156 ds = &(process->data_sources[i]);
157 if (ds->apply_config(ds, key, val) == 0)
163 int config_read_per_process(struct proc_info *process)
165 const char *class_str = process_class_to_string(process->class);
166 struct json_object *obj;
169 _E("There is no config");
174 if (!json_object_object_get_ex(config, "global", &obj))
175 _E("There is no 'global' section in config");
177 _read_params(obj, process);
179 /* Class related config */
180 if (!json_object_object_get_ex(config, class_str, &obj))
181 _W("There is no '%s' section in config", class_str);
183 _read_params(obj, process);
185 /* Process by name */
186 if (json_object_object_get_ex(config, process->name, &obj)) {
187 _read_params(obj, process);