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 int app2ext_usr_get_app_location(const char *pkgid, uid_t uid)
111 char loopback_device[FILENAME_MAX] = { 0, };
112 char application_path[FILENAME_MAX] = { 0, };
113 char application_mmc_path[FILENAME_MAX] = { 0, };
114 char *encoded_id = NULL;
116 /* validate the function parameter received */
118 _E("invalid func parameters");
122 if (_is_global(uid)) {
123 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
124 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
125 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
126 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
128 tzplatform_set_user(uid);
129 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
130 tzplatform_getenv(TZ_USER_APP), pkgid);
131 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
132 tzplatform_getenv(TZ_USER_APP), pkgid);
133 tzplatform_reset_user();
135 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
136 if (encoded_id == NULL) {
139 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
140 APP2SD_PATH, encoded_id);
143 /* check whether application is in external memory or not */
144 fp = fopen(loopback_device, "r");
149 return APP2EXT_SD_CARD;
152 /* check whether application is in internal or not */
153 fp = fopen(application_path, "r");
155 _D("app_dir_path open failed, " \
156 "package not installed");
157 return APP2EXT_NOT_INSTALLED;
160 /* check whether the application is installed in SD card
161 * but SD card is not present
163 fp = fopen(application_mmc_path, "r");
166 return APP2EXT_INTERNAL_MEM;
169 _E("app_mmc_internal_path exists, " \
176 int app2ext_get_app_location(const char *pkgid)
180 ret = app2ext_usr_get_app_location(pkgid, getuid());
185 int app2ext_usr_enable_external_pkg(const char *pkgid, uid_t uid)
188 app2ext_handle *app2_handle = NULL;
189 char loopback_device[FILENAME_MAX] = { 0, };
190 char *encoded_id = NULL;
192 /* validate the function parameter received */
194 _E("invalid func parameters");
198 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
199 if (encoded_id == NULL) {
202 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
203 APP2SD_PATH, encoded_id);
206 /* check whether application is in external memory or not */
207 fp = fopen(loopback_device, "r");
212 app2_handle = app2ext_init(APP2EXT_SD_CARD);
213 if (app2_handle == NULL) {
214 _E("app2ext init failed");
218 app2_handle->interface.client_usr_enable(pkgid, uid);
219 app2ext_deinit(app2_handle);
225 int app2ext_enable_external_pkg(const char *pkgid)
229 ret = app2ext_usr_enable_external_pkg(pkgid, getuid());
234 int app2ext_usr_disable_external_pkg(const char *pkgid, uid_t uid)
237 app2ext_handle *app2_handle = NULL;
238 char loopback_device[FILENAME_MAX] = { 0, };
239 char *encoded_id = NULL;
241 /* validate the function parameter received */
242 if (pkgid == NULL || uid < 0) {
243 _E("invalid func parameters");
247 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
248 if (encoded_id == NULL) {
251 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
252 APP2SD_PATH, encoded_id);
255 /* check whether application is in external memory or not */
256 fp = fopen(loopback_device, "r");
261 app2_handle = app2ext_init(APP2EXT_SD_CARD);
262 if (app2_handle == NULL) {
263 _E("app2ext init failed");
267 app2_handle->interface.client_usr_disable(pkgid, uid);
268 app2ext_deinit(app2_handle);
274 int app2ext_disable_external_pkg(const char *pkgid)
278 ret = app2ext_usr_disable_external_pkg(pkgid, getuid());
283 int app2ext_usr_force_clean_pkg(const char *pkgid, uid_t uid)
286 app2ext_handle *app2_handle = NULL;
288 /* validate the function parameter received */
289 if (pkgid == NULL || uid < 0) {
290 _E("invalid func parameters");
294 app2_handle = app2ext_init(APP2EXT_SD_CARD);
295 if (app2_handle == NULL) {
296 _E("app2ext init failed");
300 app2_handle->interface.client_usr_force_clean(pkgid, uid);
301 app2ext_deinit(app2_handle);
306 int app2ext_force_clean_pkg(const char *pkgid)
310 ret = app2ext_usr_force_clean_pkg(pkgid, getuid());