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.
22 #include <app2ext_interface.h>
25 #include <sys/types.h>
33 #define APP2EXT_SD_PLUGIN_PATH LIBPREFIX "/libapp2sd.so"
35 int _is_global(uid_t uid)
37 if (uid == OWNER_ROOT || uid == GLOBAL_USER)
43 app2ext_handle *app2ext_init(int storage_type)
45 void *dl_handle = NULL;
46 int (*dl_on_load)(app2ext_interface *) = NULL;
48 /* validate the function parameter recieved */
49 if (storage_type < APP2EXT_INTERNAL_MEM ||
50 storage_type > APP2EXT_CLOUD) {
51 _E("invalid function arguments");
54 if (storage_type != APP2EXT_SD_CARD ) {
55 _E("storage type currently not supported");
59 /* allocate memory for app2ext handle*/
60 app2ext_handle *handle = (app2ext_handle *)calloc(1, sizeof(app2ext_handle));
62 _E("memory allocation failed");
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);
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);
84 /* initialize the SD plugin*/
85 if (!dl_on_load(&(handle->interface))) {
86 _E("app2ext_on_load() failed in (%s)", APP2EXT_SD_PLUGIN_PATH);
92 _D("plugin(%s) loaded", APP2EXT_SD_PLUGIN_PATH);
97 int app2ext_deinit(app2ext_handle *handle)
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;
105 /* close the plugin handle*/
106 dlclose(handle->plugin_handle);
108 /* free allocated memory during installtion*/
111 return APP2EXT_SUCCESS;
114 int app2ext_usr_get_app_location(const char *pkgid, uid_t uid)
117 char loopback_device[FILENAME_MAX] = { 0, };
118 char application_path[FILENAME_MAX] = { 0, };
119 char application_mmc_path[FILENAME_MAX] = { 0, };
121 /* validate the function parameter received */
123 _E("invalid func parameters");
124 return APP2EXT_ERROR_INVALID_ARGUMENTS;
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",
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();
144 _D("application_path = (%s)", application_path);
145 _D("application_mmc_path = (%s)", application_mmc_path);
146 _D("loopback_device = (%s)", loopback_device);
148 /*check whether application is in external memory or not */
149 fp = fopen(loopback_device, "r");
154 return APP2EXT_SD_CARD;
157 /*check whether application is in internal or not */
158 fp = fopen(application_path, "r");
160 _D("app_dir_path open failed, " \
161 "package not installed");
162 return APP2EXT_NOT_INSTALLED;
165 /* check whether the application is installed in SD card
166 * but SD card is not present
168 fp = fopen(application_mmc_path, "r");
171 return APP2EXT_INTERNAL_MEM;
174 _E("app_mmc_internal_path exists, " \
176 return APP2EXT_ERROR_MMC_STATUS;
180 int app2ext_get_app_location(const char *pkgid)
184 ret = app2ext_usr_get_app_location(pkgid, getuid());
189 int app2ext_usr_enable_external_pkg(const char *pkgid, uid_t uid)
192 app2ext_handle *app2_handle = NULL;
193 char loopback_device[FILENAME_MAX] = { 0, };
195 /* validate the function parameter received */
197 _E("invalid func parameters");
201 if (_is_global(uid)) {
202 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
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();
211 _D("loopback_device = (%s)", loopback_device);
213 /* check whether application is in external memory or not */
214 fp = fopen(loopback_device, "r");
219 app2_handle = app2ext_init(APP2EXT_SD_CARD);
220 if (app2_handle == NULL) {
221 _E("app2ext init failed");
225 app2_handle->interface.client_usr_enable(pkgid, uid);
226 app2ext_deinit(app2_handle);
230 int app2ext_enable_external_pkg(const char *pkgid)
234 ret = app2ext_usr_enable_external_pkg(pkgid, getuid());
239 int app2ext_usr_disable_external_pkg(const char *pkgid, uid_t uid)
242 app2ext_handle *app2_handle = NULL;
243 char loopback_device[FILENAME_MAX] = { 0, };
245 /* validate the function parameter received */
247 _E("invalid func parameters");
251 if (_is_global(uid)) {
252 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
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();
261 _D("loopback_device = (%s)", loopback_device);
263 /* check whether application is in external memory or not */
264 fp = fopen(loopback_device, "r");
269 app2_handle = app2ext_init(APP2EXT_SD_CARD);
270 if (app2_handle == NULL) {
271 _E("app2_handle : app2ext init failed");
275 app2_handle->interface.client_usr_disable(pkgid, uid);
276 app2ext_deinit(app2_handle);
281 int app2ext_disable_external_pkg(const char *pkgid)
285 ret = app2ext_usr_disable_external_pkg(pkgid, getuid());
290 int app2ext_usr_force_clean_pkg(const char *pkgid, uid_t uid)
293 app2ext_handle *app2_handle = NULL;
294 char application_mmc_path[FILENAME_MAX] = { 0, };
296 /* validate the function parameter received */
298 _E("invalid func parameters");
302 if (_is_global(uid)) {
303 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/%s/.mmc",
304 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
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();
311 _D("application_mmc_path = (%s)", application_mmc_path);
313 fp = fopen(application_mmc_path, "r");
320 app2_handle = app2ext_init(APP2EXT_SD_CARD);
321 if (app2_handle == NULL) {
322 _E("app2ext init failed");
326 app2_handle->interface.client_usr_force_clean(pkgid, uid);
327 app2ext_deinit(app2_handle);
331 int app2ext_force_clean_pkg(const char *pkgid)
335 ret = app2ext_usr_force_clean_pkg(pkgid, getuid());