Prep gmock tests
[platform/core/appfw/app2sd.git] / src / app2ext_interface.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jyotsna Dhumale <jyotsna.a@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <errno.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <dlfcn.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <dirent.h>
31
32 #include "app2ext_interface.h"
33 #include "app2ext_debug.h"
34
35 #define APP2EXT_SD_PLUGIN_PATH  LIBPREFIX "/libapp2sd.so"
36 #define REGULAR_USER 5000
37
38 app2ext_handle *app2ext_init(int storage_type)
39 {
40         void *dl_handle = NULL;
41         int (*dl_on_load)(app2ext_interface *) = NULL;
42
43         /* validate the function parameter recieved */
44         if (storage_type < APP2EXT_INTERNAL_MEM ||
45                 storage_type > APP2EXT_CLOUD) {
46                 _E("invalid function arguments");
47                 return NULL;
48         }
49         if (storage_type != APP2EXT_SD_CARD) {
50                 _E("storage type currently not supported");
51                 return NULL;
52         }
53
54         /* allocate memory for app2ext handle*/
55         app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
56         if (handle == NULL) {
57                 _E("memory allocation failed");
58                 return NULL;
59         }
60
61         /* load SD plugin*/
62         handle->type = APP2EXT_SD_CARD;
63         dl_handle = dlopen(APP2EXT_SD_PLUGIN_PATH, RTLD_LAZY|RTLD_GLOBAL);
64         if (NULL == dl_handle) {
65                 _E("dlopen(%s) failed", APP2EXT_SD_PLUGIN_PATH);
66                 free(handle);
67                 return NULL;
68         }
69
70         handle->plugin_handle = dl_handle;
71         dl_on_load = dlsym(dl_handle, "app2ext_on_load");
72         if (NULL == dl_on_load) {
73                 _E("cannot find app2ext_on_load symbol in (%s)", APP2EXT_SD_PLUGIN_PATH);
74                 dlclose(dl_handle);
75                 free(handle);
76                 return NULL;
77         }
78
79         /* initialize the SD plugin*/
80         if (!dl_on_load(&(handle->interface))) {
81                 _E("app2ext_on_load() failed in (%s)", APP2EXT_SD_PLUGIN_PATH);
82                 dlclose(dl_handle);
83                 free(handle);
84                 return NULL;
85         }
86
87         _D("plugin(%s) loaded", APP2EXT_SD_PLUGIN_PATH);
88
89         return handle;
90 }
91
92 int app2ext_deinit(app2ext_handle *handle)
93 {
94         /* validate the function parameter recieved */
95         if (handle == NULL || handle->plugin_handle == NULL) {
96                 _E("invalid function arguments");
97                 return -1;
98         }
99
100         /* close the plugin handle*/
101         dlclose(handle->plugin_handle);
102
103         /* free allocated memory during installation*/
104         free(handle);
105
106         return 0;
107 }
108
109 int app2ext_usr_enable_external_pkg(const char *pkgid, uid_t uid)
110 {
111         app2ext_handle *handle = NULL;
112
113         /* validate the function parameter received */
114         if (pkgid == NULL) {
115                 _E("invalid func parameters");
116                 return -1;
117         }
118
119         handle = app2ext_init(APP2EXT_SD_CARD);
120         if (handle == NULL) {
121                 _E("app2ext init failed");
122                 return -1;
123         }
124
125         handle->interface.client_usr_enable(pkgid, uid);
126         app2ext_deinit(handle);
127
128         return 0;
129 }
130
131 int app2ext_enable_external_pkg(const char *pkgid)
132 {
133         int ret = 0;
134
135         ret = app2ext_usr_enable_external_pkg(pkgid, getuid());
136
137         return ret;
138 }
139
140 int app2ext_usr_disable_external_pkg(const char *pkgid, uid_t uid)
141 {
142         app2ext_handle *handle = NULL;
143
144         /* validate the function parameter received */
145         if (pkgid == NULL) {
146                 _E("invalid func parameters");
147                 return -1;
148         }
149
150         handle = app2ext_init(APP2EXT_SD_CARD);
151         if (handle == NULL) {
152                 _E("app2ext init failed");
153                 return -1;
154         }
155         handle->interface.client_usr_disable(pkgid, uid);
156         app2ext_deinit(handle);
157
158         return 0;
159 }
160
161 int app2ext_disable_external_pkg(const char *pkgid)
162 {
163         int ret = 0;
164
165         ret = app2ext_usr_disable_external_pkg(pkgid, getuid());
166
167         return ret;
168 }
169
170 int app2ext_enable_all_external_pkgs(void)
171 {
172         int ret = 0;
173         app2ext_handle *handle = NULL;
174
175         if (getuid() >= REGULAR_USER)
176                 return 0;
177
178         handle = app2ext_init(APP2EXT_SD_CARD);
179         if (handle == NULL) {
180                 _E("app2ext init failed");
181                 return -1;
182         }
183
184         ret = handle->interface.client_enable_full_pkg();
185         if (ret != 0)
186                 _E("failed to enable entire pkgs");
187
188         app2ext_deinit(handle);
189
190         return 0;
191 }
192
193 int app2ext_disable_all_external_pkgs(void)
194 {
195         int ret = 0;
196         app2ext_handle *handle = NULL;
197
198         if (getuid() >= REGULAR_USER)
199                 return 0;
200
201         handle = app2ext_init(APP2EXT_SD_CARD);
202         if (handle == NULL) {
203                 _E("app2ext init failed");
204                 return -1;
205         }
206
207         ret = handle->interface.client_disable_full_pkg();
208         if (ret != 0)
209                 _E("failed to disable entire pkgs");
210
211         app2ext_deinit(handle);
212
213         return 0;
214 }
215
216 int app2ext_usr_force_clean_pkg(const char *pkgid, uid_t uid)
217 {
218         app2ext_handle *handle = NULL;
219
220         /* validate the function parameter received */
221         if (pkgid == NULL) {
222                 _E("invalid func parameters");
223                 return -1;
224         }
225
226         handle = app2ext_init(APP2EXT_SD_CARD);
227         if (handle == NULL) {
228                 _E("app2ext init failed");
229                 return -1;
230         }
231
232         handle->interface.client_usr_force_clean(pkgid, uid);
233         app2ext_deinit(handle);
234
235         return 0;
236 }
237
238 int app2ext_force_clean_pkg(const char *pkgid)
239 {
240         int ret = 0;
241
242         ret = app2ext_usr_force_clean_pkg(pkgid, getuid());
243
244         return ret;
245 }
246
247 int app2ext_migrate_legacy_all(void)
248 {
249         app2ext_handle *handle = NULL;
250
251         handle = app2ext_init(APP2EXT_SD_CARD);
252         if (handle == NULL) {
253                 _E("app2ext init failed");
254                 return -1;
255         }
256
257         handle->interface.client_migrate_legacy_all();
258         app2ext_deinit(handle);
259
260         return 0;
261 }
262
263 char *app2ext_usr_getname_image(const char *pkgid, uid_t uid)
264 {
265         app2ext_handle *handle = NULL;
266         char *image_name = NULL;
267
268         /* validate the function parameter received */
269         if (pkgid == NULL) {
270                 _E("invalid func parameters");
271                 return NULL;
272         }
273
274         handle = app2ext_init(APP2EXT_SD_CARD);
275         if (handle == NULL) {
276                 _E("app2ext init failed");
277                 return NULL;
278         }
279
280         image_name = handle->interface.client_usr_getname_image(pkgid, uid);
281         app2ext_deinit(handle);
282
283         return image_name;
284 }
285
286 char *app2ext_usr_get_image_path(const char *pkgid, uid_t uid)
287 {
288         app2ext_handle *handle;
289         char *image_path;
290
291         /* validate the function parameter received */
292         if (pkgid == NULL) {
293                 _E("invalid func parameters");
294                 return NULL;
295         }
296
297         handle = app2ext_init(APP2EXT_SD_CARD);
298         if (handle == NULL) {
299                 _E("app2ext init failed");
300                 return NULL;
301         }
302
303         image_path = handle->interface.client_usr_get_image_path(pkgid, uid);
304         app2ext_deinit(handle);
305
306         return image_path;
307 }