4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jyotsna Dhumale <jyotsna.a@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/types.h>
32 #include "app2ext_interface.h"
33 #include "app2ext_utils.h"
35 #define APP2EXT_SD_PLUGIN_PATH LIBPREFIX "/libapp2sd.so"
37 app2ext_handle *app2ext_init(int storage_type)
39 void *dl_handle = NULL;
40 int (*dl_on_load)(app2ext_interface *) = NULL;
42 /* validate the function parameter recieved */
43 if (storage_type < APP2EXT_INTERNAL_MEM ||
44 storage_type > APP2EXT_CLOUD) {
45 _E("invalid function arguments");
48 if (storage_type != APP2EXT_SD_CARD) {
49 _E("storage type currently not supported");
53 /* allocate memory for app2ext handle*/
54 app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
56 _E("memory allocation failed");
61 handle->type = APP2EXT_SD_CARD;
62 dl_handle = dlopen(APP2EXT_SD_PLUGIN_PATH, RTLD_LAZY|RTLD_GLOBAL);
63 if (NULL == dl_handle) {
64 _E("dlopen(%s) failed", APP2EXT_SD_PLUGIN_PATH);
69 handle->plugin_handle = dl_handle;
70 dl_on_load = dlsym(dl_handle, "app2ext_on_load");
71 if (NULL == dl_on_load) {
72 _E("cannot find app2ext_on_load symbol in (%s)", APP2EXT_SD_PLUGIN_PATH);
78 /* initialize the SD plugin*/
79 if (!dl_on_load(&(handle->interface))) {
80 _E("app2ext_on_load() failed in (%s)", APP2EXT_SD_PLUGIN_PATH);
86 _D("plugin(%s) loaded", APP2EXT_SD_PLUGIN_PATH);
91 int app2ext_deinit(app2ext_handle *handle)
93 /* validate the function parameter recieved */
94 if (handle == NULL || handle->plugin_handle == NULL) {
95 _E("invalid function arguments");
99 /* close the plugin handle*/
100 dlclose(handle->plugin_handle);
102 /* free allocated memory during installtion*/
108 /* use this api only to check external related file existing,
109 * such as .mmc directory and image file under /sdcard/app2sd.
111 int app2ext_usr_get_app_location(const char *pkgid, uid_t uid)
114 char loopback_device[FILENAME_MAX] = { 0, };
115 char application_path[FILENAME_MAX] = { 0, };
116 char application_mmc_path[FILENAME_MAX] = { 0, };
117 char *encoded_id = NULL;
119 /* validate the function parameter received */
121 _E("invalid func parameters");
125 if (_is_global(uid)) {
126 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
127 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
128 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
129 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
131 tzplatform_set_user(uid);
132 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
133 tzplatform_getenv(TZ_USER_APP), pkgid);
134 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
135 tzplatform_getenv(TZ_USER_APP), pkgid);
136 tzplatform_reset_user();
138 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
139 if (encoded_id == NULL)
142 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
143 APP2SD_PATH, encoded_id);
146 /* check whether application is in external memory or not */
147 fp = fopen(loopback_device, "r");
152 return APP2EXT_SD_CARD;
155 /* check whether application is in internal or not */
156 fp = fopen(application_path, "r");
158 _D("app_dir_path open failed, " \
159 "package not installed");
160 return APP2EXT_NOT_INSTALLED;
163 /* check whether the application is installed in SD card
164 * but SD card is not present
166 fp = fopen(application_mmc_path, "r");
169 return APP2EXT_INTERNAL_MEM;
172 _E("app_mmc_internal_path exists, " \
179 int app2ext_get_app_location(const char *pkgid)
183 ret = app2ext_usr_get_app_location(pkgid, getuid());
188 int app2ext_usr_enable_external_pkg(const char *pkgid, uid_t uid)
191 app2ext_handle *handle = NULL;
192 char loopback_device[FILENAME_MAX] = { 0, };
193 char *encoded_id = NULL;
195 /* validate the function parameter received */
197 _E("invalid func parameters");
201 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
202 if (encoded_id == NULL)
205 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
206 APP2SD_PATH, encoded_id);
209 /* check whether application is in external memory or not */
210 fp = fopen(loopback_device, "r");
215 handle = app2ext_init(APP2EXT_SD_CARD);
216 if (handle == NULL) {
217 _E("app2ext init failed");
221 handle->interface.client_usr_enable(pkgid, uid);
222 app2ext_deinit(handle);
228 int app2ext_enable_external_pkg(const char *pkgid)
232 ret = app2ext_usr_enable_external_pkg(pkgid, getuid());
237 int app2ext_usr_disable_external_pkg(const char *pkgid, uid_t uid)
240 app2ext_handle *handle = NULL;
241 char loopback_device[FILENAME_MAX] = { 0, };
242 char *encoded_id = NULL;
244 /* validate the function parameter received */
246 _E("invalid func parameters");
250 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
251 if (encoded_id == NULL)
254 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
255 APP2SD_PATH, encoded_id);
258 /* check whether application is in external memory or not */
259 fp = fopen(loopback_device, "r");
264 handle = app2ext_init(APP2EXT_SD_CARD);
265 if (handle == NULL) {
266 _E("app2ext init failed");
270 handle->interface.client_usr_disable(pkgid, uid);
271 app2ext_deinit(handle);
277 int app2ext_disable_external_pkg(const char *pkgid)
281 ret = app2ext_usr_disable_external_pkg(pkgid, getuid());
286 int app2ext_enable_all_external_pkgs(void)
289 app2ext_handle *handle = NULL;
291 if (getuid() >= REGULAR_USER)
294 handle = app2ext_init(APP2EXT_SD_CARD);
295 if (handle == NULL) {
296 _E("app2ext init failed");
300 ret = handle->interface.client_enable_full_pkg();
302 _E("failed to enable entire pkgs");
304 app2ext_deinit(handle);
309 int app2ext_disable_all_external_pkgs(void)
312 app2ext_handle *handle = NULL;
314 if (getuid() >= REGULAR_USER)
317 handle = app2ext_init(APP2EXT_SD_CARD);
318 if (handle == NULL) {
319 _E("app2ext init failed");
323 ret = handle->interface.client_disable_full_pkg();
325 _E("failed to disable entire pkgs");
327 app2ext_deinit(handle);
332 int app2ext_usr_force_clean_pkg(const char *pkgid, uid_t uid)
334 app2ext_handle *handle = NULL;
336 /* validate the function parameter received */
338 _E("invalid func parameters");
342 handle = app2ext_init(APP2EXT_SD_CARD);
343 if (handle == NULL) {
344 _E("app2ext init failed");
348 handle->interface.client_usr_force_clean(pkgid, uid);
349 app2ext_deinit(handle);
354 int app2ext_force_clean_pkg(const char *pkgid)
358 ret = app2ext_usr_force_clean_pkg(pkgid, getuid());