apply encoded name to the source file of losetup.
[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 <app2ext_interface.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <dlfcn.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <dirent.h>
32
33 #define APP2EXT_SD_PLUGIN_PATH  LIBPREFIX "/libapp2sd.so"
34
35 int _is_global(uid_t uid)
36 {
37         if (uid == OWNER_ROOT || uid == GLOBAL_USER)
38                 return 1;
39         else
40                 return 0;
41 }
42
43 app2ext_handle *app2ext_init(int storage_type)
44 {
45         void *dl_handle = NULL;
46         int (*dl_on_load)(app2ext_interface *) = NULL;
47
48         /* validate the function parameter recieved */
49         if (storage_type < APP2EXT_INTERNAL_MEM ||
50                 storage_type > APP2EXT_CLOUD) {
51                 _E("invalid function arguments");
52                 return NULL;
53         }
54         if (storage_type != APP2EXT_SD_CARD ) {
55                 _E("storage type currently not supported");
56                 return NULL;
57         }
58
59         /* allocate memory for app2ext handle*/
60         app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
61         if (handle == NULL) {
62                 _E("memory allocation failed");
63                 return NULL;
64         }
65
66         /* load SD plugin*/
67         handle->type = APP2EXT_SD_CARD;
68         dl_handle = dlopen(APP2EXT_SD_PLUGIN_PATH, RTLD_LAZY|RTLD_GLOBAL);
69         if (NULL == dl_handle) {
70                 _E("dlopen(%s) failed", APP2EXT_SD_PLUGIN_PATH);
71                 free(handle);
72                 return NULL;
73         }
74
75         handle->plugin_handle = dl_handle;
76         dl_on_load = dlsym(dl_handle, "app2ext_on_load");
77         if (NULL == dl_on_load) {
78                 _E("cannot find app2ext_on_load symbol in (%s)", APP2EXT_SD_PLUGIN_PATH);
79                 dlclose(dl_handle);
80                 free(handle);
81                 return NULL;
82         }
83
84         /* initialize the SD plugin*/
85         if (!dl_on_load(&(handle->interface))) {
86                 _E("app2ext_on_load() failed in (%s)", APP2EXT_SD_PLUGIN_PATH);
87                 dlclose(dl_handle);
88                 free(handle);
89                 return NULL;
90         }
91
92         _D("plugin(%s) loaded", APP2EXT_SD_PLUGIN_PATH);
93
94         return handle;
95 }
96
97 int app2ext_deinit(app2ext_handle *handle)
98 {
99         /* validate the function parameter recieved */
100         if (handle == NULL || handle->plugin_handle == NULL){
101                 _E("invalid function arguments");
102                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
103         }
104
105         /* close the plugin handle*/
106         dlclose(handle->plugin_handle);
107
108         /* free allocated memory during installtion*/
109         free(handle);
110
111         return APP2EXT_SUCCESS;
112 }
113
114 int app2ext_usr_get_app_location(const char *pkgid, uid_t uid)
115 {
116         FILE *fp = NULL;
117         char loopback_device[FILENAME_MAX] = { 0, };
118         char application_path[FILENAME_MAX] = { 0, };
119         char application_mmc_path[FILENAME_MAX] = { 0, };
120
121         /* validate the function parameter received */
122         if (pkgid == NULL) {
123                 _E("invalid func parameters");
124                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
125         }
126
127         if (_is_global(uid)) {
128                 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
129                         tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
130                 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
131                         tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
132                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
133                         APP2SD_PATH, pkgid);
134         } else {
135                 tzplatform_set_user(uid);
136                 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
137                         tzplatform_getenv(TZ_USER_APP), pkgid);
138                 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
139                         tzplatform_getenv(TZ_USER_APP), pkgid);
140                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
141                         APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
142                 tzplatform_reset_user();
143         }
144         _D("application_path = (%s)", application_path);
145         _D("application_mmc_path = (%s)", application_mmc_path);
146         _D("loopback_device = (%s)", loopback_device);
147
148         /*check whether application is in external memory or not */
149         fp = fopen(loopback_device, "r");
150         if (fp != NULL) {
151                 fclose(fp);
152                 fp = NULL;
153                 _D("sd card");
154                 return APP2EXT_SD_CARD;
155         }
156
157         /*check whether application is in internal or not */
158         fp = fopen(application_path, "r");
159         if (fp == NULL) {
160                 _D("app_dir_path open failed, " \
161                         "package not installed");
162                 return APP2EXT_NOT_INSTALLED;
163         } else {
164                 fclose(fp);
165                 /* check whether the application is installed in SD card
166                  * but SD card is not present
167                  */
168                 fp = fopen(application_mmc_path, "r");
169                 if (fp == NULL) {
170                         _D("internal mem");
171                         return APP2EXT_INTERNAL_MEM;
172                 } else {
173                         fclose(fp);
174                         _E("app_mmc_internal_path exists, " \
175                                 "error mmc status");
176                         return APP2EXT_ERROR_MMC_STATUS;
177                 }
178         }
179 }
180 int app2ext_get_app_location(const char *pkgid)
181 {
182         int ret = 0;
183
184         ret = app2ext_usr_get_app_location(pkgid, getuid());
185
186         return ret;
187 }
188
189 int app2ext_usr_enable_external_pkg(const char *pkgid, uid_t uid)
190 {
191         FILE *fp = NULL;
192         app2ext_handle *app2_handle = NULL;
193         char loopback_device[FILENAME_MAX] = { 0, };
194
195         /* validate the function parameter received */
196         if (pkgid == NULL) {
197                 _E("invalid func parameters");
198                 return -1;
199         }
200
201         if (_is_global(uid)) {
202                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
203                         APP2SD_PATH, pkgid);
204         } else {
205                 tzplatform_set_user(uid);
206                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
207                         APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
208                 tzplatform_reset_user();
209         }
210
211         _D("loopback_device = (%s)", loopback_device);
212
213         /* check whether application is in external memory or not */
214         fp = fopen(loopback_device, "r");
215         if (fp != NULL) {
216                 fclose(fp);
217                 fp = NULL;
218
219                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
220                 if (app2_handle == NULL) {
221                         _E("app2ext init failed");
222                         return -2;
223                 }
224
225                 app2_handle->interface.client_usr_enable(pkgid, uid);
226                 app2ext_deinit(app2_handle);
227         }
228         return 0;
229 }
230 int app2ext_enable_external_pkg(const char *pkgid)
231 {
232         int ret = 0;
233
234         ret = app2ext_usr_enable_external_pkg(pkgid, getuid());
235
236         return ret;
237 }
238
239 int app2ext_usr_disable_external_pkg(const char *pkgid, uid_t uid)
240 {
241         FILE *fp = NULL;
242         app2ext_handle *app2_handle = NULL;
243         char loopback_device[FILENAME_MAX] = { 0, };
244
245         /* validate the function parameter received */
246         if (pkgid == NULL) {
247                 _E("invalid func parameters");
248                 return -1;
249         }
250
251         if (_is_global(uid)) {
252                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
253                         APP2SD_PATH, pkgid);
254         } else {
255                 tzplatform_set_user(uid);
256                 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
257                         APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
258                 tzplatform_reset_user();
259         }
260
261         _D("loopback_device = (%s)", loopback_device);
262
263         /* check whether application is in external memory or not */
264         fp = fopen(loopback_device, "r");
265         if (fp != NULL) {
266                 fclose(fp);
267                 fp = NULL;
268
269                 app2_handle = app2ext_init(APP2EXT_SD_CARD);
270                 if (app2_handle == NULL) {
271                         _E("app2_handle : app2ext init failed");
272                         return -2;
273                 }
274
275                 app2_handle->interface.client_usr_disable(pkgid, uid);
276                 app2ext_deinit(app2_handle);
277         }
278
279         return 0;
280 }
281 int app2ext_disable_external_pkg(const char *pkgid)
282 {
283         int ret = 0;
284
285         ret = app2ext_usr_disable_external_pkg(pkgid, getuid());
286
287         return ret;
288 }
289
290 int app2ext_usr_force_clean_pkg(const char *pkgid, uid_t uid)
291 {
292         FILE *fp = NULL;
293         app2ext_handle *app2_handle = NULL;
294         char application_mmc_path[FILENAME_MAX] = { 0, };
295
296         /* validate the function parameter received */
297         if (pkgid == NULL) {
298                 _E("invalid func parameters");
299                 return 0;
300         }
301
302         if (_is_global(uid)) {
303                 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
304                         tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
305         } else {
306                 tzplatform_set_user(uid);
307                 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
308                         tzplatform_getenv(TZ_USER_APP), pkgid);
309                 tzplatform_reset_user();
310         }
311         _D("application_mmc_path = (%s)", application_mmc_path);
312
313         fp = fopen(application_mmc_path, "r");
314         if (fp == NULL) {
315                 return 0;
316         } else {
317                 fclose(fp);
318         }
319
320         app2_handle = app2ext_init(APP2EXT_SD_CARD);
321         if (app2_handle == NULL) {
322                 _E("app2ext init failed");
323                 return 0;
324         }
325
326         app2_handle->interface.client_usr_force_clean(pkgid, uid);
327         app2ext_deinit(app2_handle);
328
329         return 0;
330 }
331 int app2ext_force_clean_pkg(const char *pkgid)
332 {
333         int ret = 0;
334
335         ret = app2ext_usr_force_clean_pkg(pkgid, getuid());
336
337         return ret;
338 }