Revert "[Fix] Fetch proper app_id for creation of cgroup."
[platform/core/connectivity/stc-manager.git] / src / stc-manager-util.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "stc-manager-util.h"
18
19 static int g_debuglog = -1;
20
21 static void __sync_file_to_disk(const char *path)
22 {
23         FILE *fp = NULL;
24         fp = fopen(path, "a+");
25         if (fp) {
26                 fflush(fp);
27                 fsync(fp->_fileno);
28                 fclose(fp);
29                 STC_LOGD("Sync the file to disk");
30         }
31 }
32
33 static GKeyFile *__load_key_file(const char *path)
34 {
35         GKeyFile *keyfile = NULL;
36         GError *error = NULL;
37
38         STC_LOGD("Loading [%s]", path);
39
40         keyfile = g_key_file_new();
41
42         if (!g_key_file_load_from_file(keyfile, path, 0, &error)) {
43                 STC_LOGD("Unable to load [%s] : %s", path, error->message);
44                 g_clear_error(&error);
45                 g_key_file_free(keyfile);
46                 keyfile = NULL;
47         }
48
49         return keyfile;
50 }
51
52 static int __save_key_file(GKeyFile *keyfile, char *path)
53 {
54         gchar *data = NULL;
55         gsize length = 0;
56         GError *error = NULL;
57         int ret = 0;
58
59         data = g_key_file_to_data(keyfile, &length, NULL);
60
61         if (!g_file_set_contents(path, data, length, &error)) {
62                 STC_LOGD("Failed to save information : %s", error->message);
63                 g_error_free(error);
64                 ret = -EIO;
65         }
66
67         __sync_file_to_disk(path);
68
69         g_free(data);
70         return ret;
71 }
72
73 gboolean stc_util_get_config_bool(char *key)
74 {
75         char path[MAX_PATH_LENGTH];
76         GKeyFile *keyfile;
77
78         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
79
80         keyfile = __load_key_file(path);
81         if (!keyfile)
82                 keyfile = g_key_file_new();
83
84         return g_key_file_get_boolean(keyfile, path, key, NULL);
85 }
86
87 gchar * stc_util_get_config_str(char *key)
88 {
89         char path[MAX_PATH_LENGTH];
90         GKeyFile *keyfile;
91
92         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
93
94         keyfile = __load_key_file(path);
95         if (!keyfile)
96                 keyfile = g_key_file_new();
97
98         return g_key_file_get_string(keyfile, path, key, NULL);
99 }
100
101 int stc_util_get_config_int(char *key)
102 {
103         char path[MAX_PATH_LENGTH];
104         GKeyFile *keyfile;
105
106         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
107
108         keyfile = __load_key_file(path);
109         if (!keyfile)
110                 keyfile = g_key_file_new();
111
112         return g_key_file_get_integer(keyfile, path, key, NULL);
113 }
114
115 API void stc_util_set_debuglog(int debuglog)
116 {
117         g_debuglog = debuglog;
118 }
119
120 API int stc_util_get_debuglog(void)
121 {
122         if (g_debuglog == -1)
123                 g_debuglog = stc_util_get_config_int(INFO_DEBUGLOG);
124
125         return g_debuglog;
126 }
127
128 void stc_util_initialize_config(void)
129 {
130         char path[MAX_PATH_LENGTH];
131         GKeyFile *keyfile;
132
133         snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
134
135         keyfile = __load_key_file(path);
136         if (!keyfile)
137                 keyfile = g_key_file_new();
138
139         g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
140
141         __save_key_file(keyfile, path);
142 }