2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
26 #include <sys/mount.h>
29 #include <sys/types.h>
31 #include <sys/prctl.h>
38 #define LOG_TAG "ENV_CONFIG"
40 #define _MAX_PACKAGEID_LENGTH 10
41 #define _MAX_APIVERSION_LENGTH 3
44 #define _SECURE_LOGI LOGI
45 #define _SECURE_LOGE LOGE
47 #define _SECURE_LOGI(...)
48 #define _SECURE_LOGE(...)
51 static const char* _OSP_HOME_PATH = "/opt/osp/\0"; // /opt/apps/com.samsung.osp
52 static const char* _OSP_COMPAT_SHARED_PATH = "/opt/usr/share/.osp-compat/\0";
53 static const char* _EXT_OSP_HOME_PATH = "/opt/storage/sdcard/osp/\0";
57 char src_path[PATH_MAX];
58 char dest_path[PATH_MAX];
65 char app_privilege; // 0: root privilege
69 * XXX: The returned app_roodir must be freed in caller.
72 get_app_rootpath_from_path(const char* bin_path)
74 char* app_rootpath = NULL;
75 char* delimiter = NULL;
77 /* e.g., The specified bin_path is "/opt/apps/com.samsung.basicapp/bin/basicapp" */
79 length = strlen(bin_path);
80 app_rootpath = (char *)malloc(length + 1);
81 if(app_rootpath == NULL)
84 memset(app_rootpath, '\0', length + 1);
85 strncpy(app_rootpath, bin_path, length);
87 _SECURE_LOGI("input bin_path: %s", app_rootpath);
89 delimiter = strrchr(app_rootpath, '/');
92 delimiter = strrchr(app_rootpath, '/');
99 get_package_id_from_app_rootpath(const char* app_rootpath, char* package_id)
101 const char* p = NULL;
102 if (strncmp(app_rootpath, "/opt/apps/org.tizen.", 19) == 0)
104 p = strrchr(app_rootpath, '.') + 1;
108 p = strrchr(app_rootpath, '/') + 1;
110 strncpy(package_id, p, _MAX_PACKAGEID_LENGTH);
111 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
112 _SECURE_LOGI("package id: %s", package_id);
116 get_package_id_from_package_name(const char* package_name, char* package_id)
120 if (strncmp(package_name, "com", 3) == 0)
121 { // in case of com.samsung.#osp#[package_id]#[serviceid]
122 tmpbuf = strstr(package_name, "#osp#");
125 strncpy(package_id, tmpbuf + 5, _MAX_PACKAGEID_LENGTH);
128 else if (strncmp(package_name, "osp", 3) == 0)
129 { // in case of osp.[package_id].#osp#[serviceid]
130 tmpbuf = strstr(package_name, "osp.");
133 strncpy(package_id, tmpbuf + 4, _MAX_PACKAGEID_LENGTH);
136 else if (strncmp(package_name, "org.tizen", 9) == 0)
138 // in case of org.tizen.[package_id]#[serviceid]
139 tmpbuf = strstr(package_name, "org.tizen.");
142 strncpy(package_id, tmpbuf + 10, _MAX_PACKAGEID_LENGTH);
145 else if (strlen(package_name) == 10)
147 strncpy(package_id, package_name, _MAX_PACKAGEID_LENGTH);
151 LOGE("package name is invalid (%s)", package_name);
154 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
155 _SECURE_LOGI("package_id: %s", package_id);
159 internal_is_mounted(const char* pkgid)
161 char mount_flag[64] = { 0, };
162 static const char dir[][64] =
164 { "/tmp/osp-compat" },
165 { "/tmp/osp-compat/mount" },
166 { "/tmp/osp-compat/mount/internal" }
169 sprintf(mount_flag, "/tmp/osp-compat/mount/internal/%s", pkgid);
170 int res = access(mount_flag, F_OK);
173 LOGI("Intenal path is already mounted.");
176 else if (res == -1 && errno == ENOENT)
179 for (i = 0; i < sizeof(dir)/64; ++i)
181 int res = mkdir(dir[i], 0755);
182 if (res == -1 && errno != EEXIST)
184 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
189 int fd = creat(mount_flag, 0644);
192 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
199 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
203 LOGI("Intenal path mount succeeded.");
208 external_is_mounted(const char* pkgid)
210 char mount_flag[64] = { 0, };
211 static const char dir[][64] =
213 { "/tmp/osp-compat" },
214 { "/tmp/osp-compat/mount" },
215 { "/tmp/osp-compat/mount/external" }
218 sprintf(mount_flag, "/tmp/osp-compat/mount/external/%s", pkgid);
219 int res = access(mount_flag, F_OK);
222 LOGI("Extenal path is already mounted.");
225 else if (res == -1 && errno == ENOENT)
228 for (i = 0; i < sizeof(dir)/64; ++i)
230 int res = mkdir(dir[i], 0755);
231 if (res == -1 && errno != EEXIST)
233 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
238 int fd = creat(mount_flag, 0644);
241 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
248 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
252 LOGI("Extenal path mount succeeded.");
257 mount_native_paths(const char* app_rootpath)
260 static const struct _path_info mount_info[] =
262 //{ "/bin", "./bin" },
263 //{ "/boot", "./boot" },
264 //{ "/cache", "./cache" },
267 { "/dev/pts", "./dev/pts" },
268 { "/dev/shm", "./dev/shm" },
271 //{ "/lost+found", "./lost+found" },
272 { "/media", "./media" },
274 //{ "/packaging", "./packaging" },
275 { "/proc", "./proc" },
276 { "/sbin", "./sbin" },
277 { "/smack", "./smack" },
280 { "/sys/kernel/debug", "./sys/kernel/debug" },
284 { "/var/run", "./var/run" },
286 { "/opt/usr", "./opt/usr" },
287 { "/opt/var/kdb/db", "./opt/var/kdb/db" },
288 // { "/opt/storage/sdcard","./opt/storage/sdcard" }
291 if (chdir(app_rootpath) != 0)
293 LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
297 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
299 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
301 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
302 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
305 for (j = i; j > 0; --j)
307 umount2(mount_info[j-1].dest_path, MNT_DETACH);
317 mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
320 char osp_share_pkgid_path[PATH_MAX] = {0, };
321 char osp_share2_pkgid_path[PATH_MAX] = {0, };
322 struct _path_info mount_info[] =
324 { "\0", "./data/Share" },
325 { "\0", "./data/Share2" },
326 { "/opt/usr/share/.osp-compat/share", "./Share" },
327 { "/opt/usr/share/.osp-compat/share2", "./Share2" },
328 //{ "/opt/osp/clipboard", "./Clipboard" },
329 //{ "/opt/osp/partner/npki","./NPKI" },
330 //{ "/opt/osp/system", "./System" },
331 //{ "/opt/osp/Tmp", "./Tmp" },
332 { "/opt/usr/media", "./Media" }
335 strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
336 strncat(osp_share_pkgid_path, "share/", 6);
337 strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
339 strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
340 strncat(osp_share2_pkgid_path, "share2/", 7);
341 strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
343 strncpy(mount_info[0].src_path, osp_share_pkgid_path, strlen(osp_share_pkgid_path));
344 strncpy(mount_info[1].src_path, osp_share2_pkgid_path, strlen(osp_share2_pkgid_path));
346 if (chdir(app_rootpath) != 0)
348 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
352 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
354 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
356 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
357 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
360 for (j = i; j > 0; --j)
362 umount2(mount_info[j-1].dest_path, MNT_DETACH);
372 create_osp_external_paths(const char* app_rootpath, const char* pkgid)
374 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
375 char osp_ext_apps_pkgid_share_path[PATH_MAX] = {0, };
376 char osp_ext_apps_pkgid_share2_path[PATH_MAX] = {0, };
377 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
378 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
379 struct _dir_info external_dirs[] =
381 { "./HomeExt", 0000, 0 },
382 { "./ShareExt", 0000, 0 },
383 { "./Share2Ext", 0000, 0 },
384 { "/opt/storage/sdcard/osp", 0777, 0 },
385 { "/opt/storage/sdcard/osp/apps", 0777, 0 },
386 { "/opt/storage/sdcard/osp/share", 0777, 0 },
387 { "/opt/storage/sdcard/osp/share2", 0777, 0 },
393 { "/opt/storage/sdcard/Images", 0777, 0 },
394 { "/opt/storage/sdcard/Sounds", 0777, 0 },
395 { "/opt/storage/sdcard/Videos", 0777, 0 },
396 { "/opt/storage/sdcard/Others", 0777, 0 }
400 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
401 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
402 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
404 strncpy(osp_ext_apps_pkgid_share_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
405 strncat(osp_ext_apps_pkgid_share_path, "/Share", 6);
407 strncpy(osp_ext_apps_pkgid_share2_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
408 strncat(osp_ext_apps_pkgid_share2_path, "/Share2", 7);
410 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
411 strncat(osp_ext_share_pkgid_path, "share/", 6);
412 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
414 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
415 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
416 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
418 strncpy(external_dirs[7].path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
419 strncpy(external_dirs[8].path, osp_ext_apps_pkgid_share_path, strlen(osp_ext_apps_pkgid_share_path));
420 strncpy(external_dirs[9].path, osp_ext_apps_pkgid_share2_path, strlen(osp_ext_apps_pkgid_share2_path));
421 strncpy(external_dirs[10].path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
422 strncpy(external_dirs[11].path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
424 if (chdir(app_rootpath) != 0)
426 LOGE("chdir() failed (%s), path: %s", strerror(errno), app_rootpath);
430 for (i = 0; i < sizeof(external_dirs)/sizeof(struct _dir_info); i++)
432 int ret = mkdir(external_dirs[i].path, external_dirs[i].mode);
433 if (ret == -1 && errno != 17) // EEXIST
435 LOGE("mkdir() failed, path: %s, errno: %d (%s)", external_dirs[i].path, errno, strerror(errno));
444 mount_osp_external_paths(const char* app_rootpath, const char* pkgid)
446 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
447 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
448 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
449 struct _path_info mount_info[] =
451 { "/opt/storage/sdcard", "./Storagecard/Media" },
452 { "/opt/storage/sdcard/osp/share", "./ShareExt" },
453 { "/opt/storage/sdcard/osp/share2", "./Share2Ext" },
454 { "\0", "./HomeExt" },
455 { "\0", "./HomeExt/Share" },
456 { "\0", "./HomeExt/Share2" }
460 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
461 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
462 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
464 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
465 strncat(osp_ext_share_pkgid_path, "share/", 6);
466 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
468 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
469 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
470 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
472 strncpy(mount_info[3].src_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
473 strncpy(mount_info[4].src_path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
474 strncpy(mount_info[5].src_path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
476 if (chdir(app_rootpath) != 0)
478 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
481 LOGI("app_rootpath: %s", app_rootpath);
483 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); i++)
485 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
487 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
488 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
491 for (j = i; j > 0; --j)
493 umount2(mount_info[j-1].dest_path, MNT_DETACH);
503 do_pre_exe(const char* package_name, const char* bin_path, const char* package_id)
505 char* app_rootpath = NULL;
506 char osp_app_data_path[PATH_MAX] = {0, };
507 //char internal_installed = 1; // true
511 /* e.g., app_rootdir is "/opt/usr/apps/[pkgId] */
512 app_rootpath = get_app_rootpath_from_path(bin_path);
514 _SECURE_LOGI("[data_caging] do_pre_exe() was called, package name: %s, package id: %s, binary: %s, app root: %s",
515 package_name, package_id, bin_path, app_rootpath);
519 // TODO: Check whether the application is installed in internal or external storage.
520 // Check installation position using the input path.
521 //if (internal_installed)
524 if (!internal_is_mounted(package_id))
526 if (mount_native_paths(app_rootpath) != 0)
530 if (mount_osp_internal_paths(app_rootpath, package_id) != 0)
536 int ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
539 LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
541 if (mmc_mounted == 1)
543 LOGI("MMC is mounted.");
544 if (create_osp_external_paths(app_rootpath, package_id) != 0)
549 if (!external_is_mounted(package_id))
551 if (mount_osp_external_paths(app_rootpath, package_id) != 0)
561 // TODO mount_external_paths(app_rootpath);
563 LOGI("mount() succeeded.");
565 if (chroot(app_rootpath) != 0)
567 LOGE("chroot() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
572 LOGE("chdir() failed, path: /, errno: %d (%s)", errno, strerror(errno));
575 LOGI("chroot() succeeded.");
577 // set current working dir to "/opt/apps/{packageId}/data"
579 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
580 strncat(osp_app_data_path, "/data", strlen("/data"));
583 if (chdir("/data") != 0)
585 LOGE("chdir() failed, path: /data, errno: %d (%s)", errno, strerror(errno));
592 LOGI("[data_caging] do_pre_exec() succeeded.");
599 LOGI("[data_caging] do_pre_exec() failed.");
604 do_pre_exec(const char* package_name, const char* bin_path)
606 char* app_rootpath = NULL;
607 char app_compat_path[PATH_MAX] = { 0, };
608 const char app_compat_file[] = "/info/compat.info\0";
610 char package_id[_MAX_PACKAGEID_LENGTH + 1] = { 0, };
611 char osp_app_data_path[PATH_MAX] = { 0, };
614 _SECURE_LOGI("do_pre_exec() is called, package name: %s, binary path: %s", package_name, bin_path);
616 app_rootpath = get_app_rootpath_from_path(bin_path);
618 strncpy(app_compat_path, app_rootpath, strlen(app_rootpath));
619 strncat(app_compat_path, app_compat_file, strlen(app_compat_file));
620 if (access(app_compat_path, F_OK) == 0)
626 //if (package_name == NULL)
628 //LOGI("The package name is empty.");
629 get_package_id_from_app_rootpath(app_rootpath, package_id);
634 get_package_id_from_package_name(package_name, package_id);
639 _SECURE_LOGI("package: %s (%s), binary: %s, OSP compat: %d", package_name, package_id, bin_path, osp_compat);
641 // FIXME: Temporary code with security risk
642 prctl(PR_SET_KEEPCAPS, 1);
647 //unshare(CLONE_NEWNS);
648 return do_pre_exe(package_name, bin_path, package_id);
651 // API version is equal to or greater than Tizen 2.0
652 // Set current working dir to "/opt/apps/{pkgId}/data"
653 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
654 strncat(osp_app_data_path, "/data", strlen("/data"));
656 if (chdir(osp_app_data_path) != 0)
658 LOGE("chdir() failed, path: %s, errno: %d (%s)", osp_app_data_path, errno, strerror(errno));
662 LOGI("[data_caging] do_pre_exec() succeeded.");