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
43 static const char* _OSP_HOME_PATH = "/opt/osp/\0"; // /opt/apps/com.samsung.osp
44 static const char* _OSP_COMPAT_SHARED_PATH = "/opt/usr/share/.osp-compat/\0";
45 static const char* _EXT_OSP_HOME_PATH = "/opt/storage/sdcard/osp/\0";
49 char src_path[PATH_MAX];
50 char dest_path[PATH_MAX];
57 char app_privilege; // 0: root privilege
61 * XXX: The returned app_roodir must be freed in caller.
64 get_app_rootpath_from_path(const char* bin_path)
66 char* app_rootpath = NULL;
67 char* delimiter = NULL;
69 /* e.g., The specified bin_path is "/opt/apps/com.samsung.basicapp/bin/basicapp" */
71 length = strlen(bin_path);
72 app_rootpath = (char *)malloc(length + 1);
73 if(app_rootpath == NULL)
76 memset(app_rootpath, '\0', length + 1);
77 strncpy(app_rootpath, bin_path, length);
80 LOGI("input bin_path: %s", app_rootpath);
83 delimiter = strrchr(app_rootpath, '/');
86 delimiter = strrchr(app_rootpath, '/');
93 get_package_id_from_app_rootpath(const char* app_rootpath, char* package_id)
96 if (strncmp(app_rootpath, "/opt/apps/org.tizen.", 19) == 0)
98 p = strrchr(app_rootpath, '.') + 1;
102 p = strrchr(app_rootpath, '/') + 1;
104 strncpy(package_id, p, _MAX_PACKAGEID_LENGTH);
105 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
107 LOGI("package id: %s", package_id);
112 get_package_id_from_package_name(const char* package_name, char* package_id)
116 if (strncmp(package_name, "com", 3) == 0)
117 { // in case of com.samsung.#osp#[package_id]#[serviceid]
118 tmpbuf = strstr(package_name, "#osp#");
121 strncpy(package_id, tmpbuf + 5, _MAX_PACKAGEID_LENGTH);
124 else if (strncmp(package_name, "osp", 3) == 0)
125 { // in case of osp.[package_id].#osp#[serviceid]
126 tmpbuf = strstr(package_name, "osp.");
129 strncpy(package_id, tmpbuf + 4, _MAX_PACKAGEID_LENGTH);
132 else if (strncmp(package_name, "org.tizen", 9) == 0)
134 // in case of org.tizen.[package_id]#[serviceid]
135 tmpbuf = strstr(package_name, "org.tizen.");
138 strncpy(package_id, tmpbuf + 10, _MAX_PACKAGEID_LENGTH);
141 else if (strlen(package_name) == 10)
143 strncpy(package_id, package_name, _MAX_PACKAGEID_LENGTH);
147 LOGE("package name is invalid (%s)", package_name);
150 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
152 LOGI("package_id: %s", package_id);
157 internal_is_mounted(const char* pkgid)
159 char mount_flag[64] = { 0, };
160 static const char dir[][64] =
162 { "/tmp/osp-compat" },
163 { "/tmp/osp-compat/mount" },
164 { "/tmp/osp-compat/mount/internal" }
167 sprintf(mount_flag, "/tmp/osp-compat/mount/internal/%s", pkgid);
168 int res = access(mount_flag, F_OK);
171 LOGI("Intenal path is already mounted.");
174 else if (res == -1 && errno == ENOENT)
177 for (i = 0; i < sizeof(dir)/64; ++i)
179 int res = mkdir(dir[i], 0755);
180 if (res == -1 && errno != EEXIST)
182 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
187 int fd = creat(mount_flag, 0644);
190 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
197 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
201 LOGI("Intenal path mount succeeded.");
206 external_is_mounted(const char* pkgid)
208 char mount_flag[64] = { 0, };
209 static const char dir[][64] =
211 { "/tmp/osp-compat" },
212 { "/tmp/osp-compat/mount" },
213 { "/tmp/osp-compat/mount/external" }
216 sprintf(mount_flag, "/tmp/osp-compat/mount/external/%s", pkgid);
217 int res = access(mount_flag, F_OK);
220 LOGI("Extenal path is already mounted.");
223 else if (res == -1 && errno == ENOENT)
226 for (i = 0; i < sizeof(dir)/64; ++i)
228 int res = mkdir(dir[i], 0755);
229 if (res == -1 && errno != EEXIST)
231 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
236 int fd = creat(mount_flag, 0644);
239 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
246 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
250 LOGI("Extenal path mount succeeded.");
255 mount_slp_paths(const char* app_rootpath)
258 static const struct _path_info mount_info[] = {
259 //{ "/bin", "./bin" },
260 //{ "/boot", "./boot" },
261 //{ "/cache", "./cache" },
264 { "/dev/pts", "./dev/pts" },
265 { "/dev/shm", "./dev/shm" },
268 //{ "/lost+found", "./lost+found" },
269 { "/media", "./media" },
272 { "/opt/usr", "./opt/usr" },
273 { "/opt/var/kdb/db", "./opt/var/kdb/db" },
274 { "/opt/storage/sdcard","./opt/storage/sdcard" },
275 //{ "/packaging", "./packaging" },
276 { "/proc", "./proc" },
277 { "/sbin", "./sbin" },
278 { "/smack", "./smack" },
281 { "/sys/kernel/debug", "./sys/kernel/debug" },
285 { "/var/run", "./var/run" }
288 if (chdir(app_rootpath) != 0)
290 LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
294 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
296 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
298 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
299 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
302 for (j = i; j > 0; --j)
304 umount2(mount_info[j-1].dest_path, MNT_DETACH);
314 mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
317 char osp_share_pkgid_path[PATH_MAX] = {0, };
318 char osp_share2_pkgid_path[PATH_MAX] = {0, };
319 struct _path_info mount_info[] = {
320 { "\0", "./data/Share" },
321 { "\0", "./data/Share2" },
322 { "/opt/usr/share/.osp-compat/share", "./Share" },
323 { "/opt/usr/share/.osp-compat/share2", "./Share2" },
324 //{ "/opt/osp/clipboard", "./Clipboard" },
325 //{ "/opt/osp/partner/npki", "./NPKI" },
326 //{ "/opt/osp/system", "./System" },
327 //{ "/opt/osp/Tmp", "./Tmp" },
328 { "/opt/usr/media", "./Media" }
331 strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
332 strncat(osp_share_pkgid_path, "share/", 6);
333 strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
335 strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
336 strncat(osp_share2_pkgid_path, "share2/", 7);
337 strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
339 strncpy(mount_info[0].src_path, osp_share_pkgid_path, strlen(osp_share_pkgid_path));
340 strncpy(mount_info[1].src_path, osp_share2_pkgid_path, strlen(osp_share2_pkgid_path));
342 if (chdir(app_rootpath) != 0)
344 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
348 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
350 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
352 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
353 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
356 for (j = i; j > 0; --j)
358 umount2(mount_info[j-1].dest_path, MNT_DETACH);
368 link_osp_share_path(const char* app_rootpath, const char* pkgid)
370 char osp_app_share_path[PATH_MAX] = { 0, };
371 char osp_app_share2_path[PATH_MAX] = { 0, };
372 char osp_share_pkgid_path[PATH_MAX] = { 0, };
373 char osp_share2_pkgid_path[PATH_MAX] = { 0, };
375 strncpy(osp_app_share_path, app_rootpath, strlen(app_rootpath));
376 strncat(osp_app_share_path, "/shared/data", 12);
378 strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
379 strncat(osp_share_pkgid_path, "share/", 6);
380 strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
382 strncpy(osp_app_share2_path, app_rootpath, strlen(app_rootpath));
383 strncat(osp_app_share2_path, "/shared/trusted", 15);
385 strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
386 strncat(osp_share2_pkgid_path, "share2/", 7);
387 strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
389 unlink(osp_share_pkgid_path);
390 unlink(osp_share2_pkgid_path);
392 int ret = symlink(osp_app_share_path, osp_share_pkgid_path);
393 if (ret == -1 && errno != EEXIST)
395 LOGE("symlink() failed, src path: %s, dest path: %s, errno: %d (%s)",
396 osp_app_share_path, osp_share_pkgid_path, errno, strerror(errno));
400 ret = symlink(osp_app_share2_path, osp_share2_pkgid_path);
401 if (ret == -1 && errno != EEXIST)
403 LOGE("symlink() failed, src path: %s, dest path: %s, errno: %d (%s)",
404 osp_app_share2_path, osp_share2_pkgid_path, errno, strerror(errno));
412 create_osp_external_paths(const char* app_rootpath, const char* pkgid)
414 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
415 char osp_ext_apps_pkgid_share_path[PATH_MAX] = {0, };
416 char osp_ext_apps_pkgid_share2_path[PATH_MAX] = {0, };
417 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
418 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
419 struct _dir_info external_dirs[] = {
420 { "./HomeExt", 0000, 0},
421 { "./ShareExt", 0000, 0},
422 { "./Share2Ext", 0000, 0},
423 { "/opt/storage/sdcard/osp", 0777, 0 },
424 { "/opt/storage/sdcard/osp/apps", 0777, 0 },
425 { "/opt/storage/sdcard/osp/share", 0777, 0 },
426 { "/opt/storage/sdcard/osp/share2", 0777, 0 },
432 { "/opt/storage/sdcard/Images", 0777, 0 },
433 { "/opt/storage/sdcard/Sounds", 0777, 0 },
434 { "/opt/storage/sdcard/Videos", 0777, 0 },
435 //{ "/opt/storage/sdcard/Themes", 0777, 0 },
436 { "/opt/storage/sdcard/Others", 0777, 0 }
440 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
441 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
442 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
444 strncpy(osp_ext_apps_pkgid_share_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
445 strncat(osp_ext_apps_pkgid_share_path, "/Share", 6);
447 strncpy(osp_ext_apps_pkgid_share2_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
448 strncat(osp_ext_apps_pkgid_share2_path, "/Share2", 7);
450 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
451 strncat(osp_ext_share_pkgid_path, "share/", 6);
452 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
454 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
455 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
456 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
458 strncpy(external_dirs[7].path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
459 strncpy(external_dirs[8].path, osp_ext_apps_pkgid_share_path, strlen(osp_ext_apps_pkgid_share_path));
460 strncpy(external_dirs[9].path, osp_ext_apps_pkgid_share2_path, strlen(osp_ext_apps_pkgid_share2_path));
461 strncpy(external_dirs[10].path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
462 strncpy(external_dirs[11].path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
464 if (chdir(app_rootpath) != 0)
466 LOGE("chdir() failed (%s), path: %s", strerror(errno), app_rootpath);
470 for (i = 0; i < sizeof(external_dirs)/sizeof(struct _dir_info); i++)
472 int ret = mkdir(external_dirs[i].path, external_dirs[i].mode);
473 if (ret == -1 && errno != 17) // EEXIST
475 LOGE("mkdir() failed, path: %s, errno: %d (%s)", external_dirs[i].path, errno, strerror(errno));
484 mount_osp_external_paths(const char* app_rootpath, const char* pkgid)
486 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
487 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
488 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
489 struct _path_info mount_info[] = {
490 { "/opt/storage/sdcard", "./Storagecard/Media" },
491 { "/opt/storage/sdcard/osp/share", "./ShareExt" },
492 { "/opt/storage/sdcard/osp/share2", "./Share2Ext" },
493 { "\0", "./HomeExt" },
494 { "\0", "./HomeExt/Share" },
495 { "\0", "./HomeExt/Share2" },
499 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
500 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
501 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
503 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
504 strncat(osp_ext_share_pkgid_path, "share/", 6);
505 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
507 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
508 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
509 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
511 strncpy(mount_info[3].src_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
512 strncpy(mount_info[4].src_path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
513 strncpy(mount_info[5].src_path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
515 if (chdir(app_rootpath) != 0)
517 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
520 LOGI("app_rootpath: %s", app_rootpath);
522 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); i++)
524 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
526 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
527 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
530 for (j = i; j > 0; --j)
532 umount2(mount_info[j-1].dest_path, MNT_DETACH);
542 do_pre_exe(const char* package_name, const char* bin_path, const char* package_id)
544 char* app_rootpath = NULL;
545 char osp_app_data_path[PATH_MAX] = {0, };
546 //char internal_installed = 1; // true
550 /* e.g., app_rootdir is "/opt/apps/com.samsung.basicapp or /opt/osp/applications/[appId] */
551 app_rootpath = get_app_rootpath_from_path(bin_path);
553 LOGI("[data_caging] do_pre_exe() was called, package: %s (%s), binary: %s, app root: %s",
554 package_name, package_id, bin_path, app_rootpath);
558 // TODO: Check whether the application is installed in internal or external storage.
559 // Check installation position using the input path.
560 //if (internal_installed)
563 if (!internal_is_mounted(package_id))
565 if (mount_slp_paths(app_rootpath) != 0)
568 if (mount_osp_internal_paths(app_rootpath, package_id) != 0)
573 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
576 LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
578 if (mmc_mounted == 1)
580 LOGI("MMC is mounted.");
581 if (create_osp_external_paths(app_rootpath, package_id) != 0)
586 if (!external_is_mounted(package_id))
588 if (mount_osp_external_paths(app_rootpath, package_id) != 0)
597 // TODO mount_external_paths(app_rootpath);
599 LOGI("mount() succeeded.");
601 if (chroot(app_rootpath) != 0)
603 LOGE("chroot() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
608 LOGE("chdir() failed, path: /, errno: %d (%s)", errno, strerror(errno));
611 LOGI("chroot() succeeded.");
613 // set current working dir to "/opt/apps/{packageId}/data"
615 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
616 strncat(osp_app_data_path, "/data", strlen("/data"));
619 if (chdir("/data") != 0)
621 LOGE("chdir() failed, path: /data, errno: %d (%s)", errno, strerror(errno));
628 LOGI("[data_caging] do_pre_exec() succeeded.");
635 LOGI("[data_caging] do_pre_exec() failed.");
640 do_pre_exec(const char* package_name, const char* bin_path)
642 char* app_rootpath = NULL;
643 char app_compat_path[PATH_MAX] = { 0, };
644 const char app_compat_file[] = "/info/compat.info\0";
646 char package_id[_MAX_PACKAGEID_LENGTH + 1] = { 0, };
647 char osp_app_data_path[PATH_MAX] = { 0, };
651 LOGI("do_pre_exec() is called, package name: %s, binary path: %s", package_name, bin_path);
654 app_rootpath = get_app_rootpath_from_path(bin_path);
656 strncpy(app_compat_path, app_rootpath, strlen(app_rootpath));
657 strncat(app_compat_path, app_compat_file, strlen(app_compat_file));
658 if (access(app_compat_path, F_OK) == 0)
664 //if (package_name == NULL)
666 //LOGI("The package name is empty.");
667 get_package_id_from_app_rootpath(app_rootpath, package_id);
672 get_package_id_from_package_name(package_name, package_id);
677 LOGI("package: %s (%s), binary: %s, OSP compat: %d", package_name, package_id, bin_path, osp_compat);
679 // FIXME: Temporary code with security risk
680 prctl(PR_SET_KEEPCAPS, 1);
685 //unshare(CLONE_NEWNS);
686 return do_pre_exe(package_name, bin_path, package_id);
689 // API version is equal to or greater than Tizen 2.0
690 // Set current working dir to "/opt/apps/{pkgId}/data"
691 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
692 strncat(osp_app_data_path, "/data", strlen("/data"));
694 if (chdir(osp_app_data_path) != 0)
696 LOGE("chdir() failed, path: %s, errno: %d (%s)", osp_app_data_path, errno, strerror(errno));
700 if (link_osp_share_path(app_rootpath, package_id) != 0)
705 LOGI("[data_caging] do_pre_exec() succeeded.");