Return errors to caller
[platform/core/connectivity/stc-manager.git] / src / stc-manager-plugin-monitor.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 <dlfcn.h>
18
19 #include "stc-manager.h"
20 #include "stc-manager-plugin-monitor.h"
21
22 static gboolean stc_plugin_enabled = FALSE;
23 static void *handle_plugin;
24 static stc_plugin_monitor_s *stc_plugin;
25
26 //LCOV_EXCL_START
27 API int stc_plugin_monitor_init(stc_manager_stop_cb stop_cb)
28 {
29         __STC_LOG_FUNC_ENTER__;
30
31         handle_plugin = dlopen(STC_PLUGIN_MONITOR_FILEPATH, RTLD_NOW);
32         if (!handle_plugin) {
33                 STC_LOGE("Can't load %s: %s", STC_PLUGIN_MONITOR_FILEPATH, dlerror());
34                 __STC_LOG_FUNC_EXIT__;
35                 return STC_ERROR_UNINITIALIZED;
36         }
37
38         stc_plugin = dlsym(handle_plugin, "stc_plugin_monitor");
39         if (!stc_plugin) {
40                 STC_LOGE("Can't load symbol: %s", dlerror());
41                 dlclose(handle_plugin);
42                 __STC_LOG_FUNC_EXIT__;
43                 return STC_ERROR_UNINITIALIZED;
44         }
45
46         stc_plugin->initialize_plugin(stop_cb);
47         stc_plugin_enabled = TRUE;
48
49         __STC_LOG_FUNC_EXIT__;
50         return STC_ERROR_NONE;
51 }
52
53 API int stc_plugin_monitor_deinit(void)
54 {
55         __STC_LOG_FUNC_ENTER__;
56
57         if (!stc_plugin_enabled)
58                 return STC_ERROR_UNINITIALIZED;
59
60         stc_plugin->deinitialize_plugin();
61         stc_plugin_enabled = FALSE;
62         dlclose(handle_plugin);
63
64         __STC_LOG_FUNC_EXIT__;
65         return STC_ERROR_NONE;
66 }
67
68 API int stc_plugin_monitor_add_app(uint32_t classid,
69                                 const char *app_id,
70                                 const char *pkg_id,
71                                 const stc_app_value_s value)
72 {
73         if (!stc_plugin_enabled) {
74                 if (STC_DEBUG_LOG)
75                         STC_LOGE("Plugin wasn't enabled");
76                 return STC_ERROR_UNINITIALIZED;
77         }
78
79         if (!stc_plugin) {
80                 if (STC_DEBUG_LOG)
81                         STC_LOGE("Plugin wasn't loaded");
82                 return STC_ERROR_UNINITIALIZED;
83         }
84
85         return stc_plugin->add_application(classid, app_id, pkg_id, value);
86 }
87
88 API int stc_plugin_monitor_remove_app(uint32_t classid,
89                                 const char *app_id)
90 {
91         if (!stc_plugin_enabled) {
92                 if (STC_DEBUG_LOG)
93                         STC_LOGE("Plugin wasn't enabled");
94                 return STC_ERROR_UNINITIALIZED;
95         }
96
97         if (!stc_plugin) {
98                 if (STC_DEBUG_LOG)
99                         STC_LOGE("Plugin wasn't loaded");
100                 return STC_ERROR_UNINITIALIZED;
101         }
102
103         return stc_plugin->remove_application(classid, app_id);
104 }
105
106 API int stc_plugin_monitor_lookup_app(uint32_t classid)
107 {
108         if (!stc_plugin_enabled) {
109                 if (STC_DEBUG_LOG)
110                         STC_LOGE("Plugin wasn't enabled");
111                 return STC_ERROR_UNINITIALIZED;
112         }
113
114         if (!stc_plugin) {
115                 if (STC_DEBUG_LOG)
116                         STC_LOGE("Plugin wasn't loaded");
117                 return STC_ERROR_UNINITIALIZED;
118         }
119
120         return stc_plugin->lookup_application(classid);
121 }
122
123 API int stc_plugin_monitor_add_rstn(table_restrictions_info *info)
124 {
125         if (!stc_plugin_enabled) {
126                 if (STC_DEBUG_LOG)
127                         STC_LOGE("Plugin wasn't enabled");
128                 return STC_ERROR_UNINITIALIZED;
129         }
130
131         if (!stc_plugin) {
132                 if (STC_DEBUG_LOG)
133                         STC_LOGE("Plugin wasn't loaded");
134                 return STC_ERROR_UNINITIALIZED;
135         }
136
137         return stc_plugin->add_restriction(info);
138 }
139
140 API int stc_plugin_monitor_remove_rstn(table_restrictions_info *info)
141 {
142         if (!stc_plugin_enabled) {
143                 if (STC_DEBUG_LOG)
144                         STC_LOGE("Plugin wasn't enabled");
145                 return STC_ERROR_UNINITIALIZED;
146         }
147
148         if (!stc_plugin) {
149                 if (STC_DEBUG_LOG)
150                         STC_LOGE("Plugin wasn't loaded");
151                 return STC_ERROR_UNINITIALIZED;
152         }
153
154         return stc_plugin->remove_restriction(info);
155 }
156
157 API int stc_plugin_monitor_init_connection(stc_s *stc)
158 {
159         if (!stc_plugin_enabled) {
160                 if (STC_DEBUG_LOG)
161                         STC_LOGE("Plugin wasn't enabled");
162                 return STC_ERROR_UNINITIALIZED;
163         }
164
165         if (!stc_plugin) {
166                 if (STC_DEBUG_LOG)
167                         STC_LOGE("Plugin wasn't loaded");
168                 return STC_ERROR_UNINITIALIZED;
169         }
170
171         return stc_plugin->init_connection(stc);
172 }
173
174 API int stc_plugin_monitor_deinit_connection(stc_s *stc)
175 {
176         if (!stc_plugin_enabled) {
177                 if (STC_DEBUG_LOG)
178                         STC_LOGE("Plugin wasn't enabled");
179                 return STC_ERROR_UNINITIALIZED;
180         }
181
182         if (!stc_plugin) {
183                 STC_LOGE("Plugin wasn't loaded");
184                 return STC_ERROR_UNINITIALIZED;
185         }
186
187         return stc_plugin->deinit_connection(stc);
188 }
189
190 API int stc_plugin_monitor_add_proc(uint32_t classid,
191                         const char *app_id, const stc_proc_value_s value)
192 {
193         if (!stc_plugin_enabled) {
194                 if (STC_DEBUG_LOG)
195                         STC_LOGE("Plugin wasn't enabled");
196                 return STC_ERROR_UNINITIALIZED;
197         }
198
199         if (!stc_plugin) {
200                 if (STC_DEBUG_LOG)
201                         STC_LOGE("Plugin wasn't loaded");
202                 return STC_ERROR_UNINITIALIZED;
203         }
204
205         return stc_plugin->add_process(classid, app_id, value);
206 }
207
208 API int stc_plugin_monitor_remove_proc(uint32_t classid, pid_t pid)
209 {
210         if (!stc_plugin_enabled) {
211                 if (STC_DEBUG_LOG)
212                         STC_LOGE("Plugin wasn't enabled");
213                 return STC_ERROR_UNINITIALIZED;
214         }
215
216         if (!stc_plugin) {
217                 if (STC_DEBUG_LOG)
218                         STC_LOGE("Plugin wasn't loaded");
219                 return STC_ERROR_UNINITIALIZED;
220         }
221
222         return stc_plugin->remove_process(classid, pid);
223 }
224
225 API int stc_plugin_monitor_move_proc(uint32_t from, uint32_t to)
226 {
227         if (!stc_plugin_enabled) {
228                 if (STC_DEBUG_LOG)
229                         STC_LOGE("Plugin wasn't enabled");
230                 return STC_ERROR_UNINITIALIZED;
231         }
232
233         if (!stc_plugin) {
234                 if (STC_DEBUG_LOG)
235                         STC_LOGE("Plugin wasn't loaded");
236                 return STC_ERROR_UNINITIALIZED;
237         }
238
239         return stc_plugin->move_process(from, to);
240 }
241
242 API int stc_plugin_monitor_update_proc_ground(uint32_t classid,
243                         const char *app_id, const stc_proc_value_s value)
244 {
245         if (!stc_plugin_enabled) {
246                 if (STC_DEBUG_LOG)
247                         STC_LOGE("Plugin wasn't enabled");
248                 return STC_ERROR_UNINITIALIZED;
249         }
250
251         if (!stc_plugin) {
252                 if (STC_DEBUG_LOG)
253                         STC_LOGE("Plugin wasn't loaded");
254                 return STC_ERROR_UNINITIALIZED;
255         }
256
257         return stc_plugin->update_process_ground(classid, app_id, value);
258 }
259 //LCOV_EXCL_STOP