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);
79 LOGI("input bin_path: %s", app_rootpath);
81 delimiter = strrchr(app_rootpath, '/');
84 delimiter = strrchr(app_rootpath, '/');
91 get_package_id_from_app_rootpath(const char* app_rootpath, char* package_id)
94 if (strncmp(app_rootpath, "/opt/apps/org.tizen.", 19) == 0)
96 p = strrchr(app_rootpath, '.') + 1;
100 p = strrchr(app_rootpath, '/') + 1;
102 strncpy(package_id, p, _MAX_PACKAGEID_LENGTH);
103 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
104 LOGI("package id: %s", package_id);
108 get_package_id_from_package_name(const char* package_name, char* package_id)
112 if (strncmp(package_name, "com", 3) == 0)
113 { // in case of com.samsung.#osp#[package_id]#[serviceid]
114 tmpbuf = strstr(package_name, "#osp#");
117 strncpy(package_id, tmpbuf + 5, _MAX_PACKAGEID_LENGTH);
120 else if (strncmp(package_name, "osp", 3) == 0)
121 { // in case of osp.[package_id].#osp#[serviceid]
122 tmpbuf = strstr(package_name, "osp.");
125 strncpy(package_id, tmpbuf + 4, _MAX_PACKAGEID_LENGTH);
128 else if (strncmp(package_name, "org.tizen", 9) == 0)
130 // in case of org.tizen.[package_id]#[serviceid]
131 tmpbuf = strstr(package_name, "org.tizen.");
134 strncpy(package_id, tmpbuf + 10, _MAX_PACKAGEID_LENGTH);
137 else if (strlen(package_name) == 10)
139 strncpy(package_id, package_name, _MAX_PACKAGEID_LENGTH);
143 LOGE("package name is invalid (%s)", package_name);
146 package_id[_MAX_PACKAGEID_LENGTH] = '\0';
147 LOGI("package_id: %s", package_id);
151 internal_is_mounted(const char* pkgid)
153 char mount_flag[64] = { 0, };
154 static const char dir[][64] =
156 { "/tmp/osp-compat" },
157 { "/tmp/osp-compat/mount" },
158 { "/tmp/osp-compat/mount/internal" }
161 sprintf(mount_flag, "/tmp/osp-compat/mount/internal/%s", pkgid);
162 int res = access(mount_flag, F_OK);
165 LOGI("Intenal path is already mounted.");
168 else if (res == -1 && errno == ENOENT)
171 for (i = 0; i < sizeof(dir)/64; ++i)
173 int res = mkdir(dir[i], 0755);
174 if (res == -1 && errno != EEXIST)
176 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
181 int fd = creat(mount_flag, 0644);
184 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
191 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
195 LOGI("Intenal path mount succeeded.");
200 external_is_mounted(const char* pkgid)
202 char mount_flag[64] = { 0, };
203 static const char dir[][64] =
205 { "/tmp/osp-compat" },
206 { "/tmp/osp-compat/mount" },
207 { "/tmp/osp-compat/mount/external" }
210 sprintf(mount_flag, "/tmp/osp-compat/mount/external/%s", pkgid);
211 int res = access(mount_flag, F_OK);
214 LOGI("Extenal path is already mounted.");
217 else if (res == -1 && errno == ENOENT)
220 for (i = 0; i < sizeof(dir)/64; ++i)
222 int res = mkdir(dir[i], 0755);
223 if (res == -1 && errno != EEXIST)
225 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
230 int fd = creat(mount_flag, 0644);
233 LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
240 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
244 LOGI("Extenal path mount succeeded.");
249 mount_slp_paths(const char* app_rootpath)
252 static const struct _path_info mount_info[] = {
253 //{ "/bin", "./bin" },
254 //{ "/boot", "./boot" },
255 //{ "/cache", "./cache" },
258 { "/dev/pts", "./dev/pts" },
259 { "/dev/shm", "./dev/shm" },
262 //{ "/lost+found", "./lost+found" },
263 { "/media", "./media" },
266 { "/opt/usr", "./opt/usr" },
267 { "/opt/var/kdb/db", "./opt/var/kdb/db" },
268 { "/opt/storage/sdcard","./opt/storage/sdcard" },
269 //{ "/packaging", "./packaging" },
270 { "/proc", "./proc" },
271 { "/sbin", "./sbin" },
274 { "/sys/kernel/debug", "./sys/kernel/debug" },
278 { "/var/run", "./var/run" }
281 if (chdir(app_rootpath) != 0)
283 LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
287 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
289 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
291 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
292 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
295 for (j = i; j > 0; --j)
297 umount2(mount_info[j-1].dest_path, MNT_DETACH);
307 mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
310 char osp_share_pkgid_path[PATH_MAX] = {0, };
311 char osp_share2_pkgid_path[PATH_MAX] = {0, };
312 struct _path_info mount_info[] = {
313 { "\0", "./data/Share" },
314 { "\0", "./data/Share2" },
315 { "/opt/usr/share/.osp-compat/share", "./Share" },
316 { "/opt/usr/share/.osp-compat/share2", "./Share2" },
317 //{ "/opt/osp/clipboard", "./Clipboard" },
318 //{ "/opt/osp/partner/npki", "./NPKI" },
319 //{ "/opt/osp/system", "./System" },
320 //{ "/opt/osp/Tmp", "./Tmp" },
321 { "/opt/usr/media", "./Media" }
324 strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
325 strncat(osp_share_pkgid_path, "share/", 6);
326 strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
328 strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
329 strncat(osp_share2_pkgid_path, "share2/", 7);
330 strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
332 strncpy(mount_info[0].src_path, osp_share_pkgid_path, strlen(osp_share_pkgid_path));
333 strncpy(mount_info[1].src_path, osp_share2_pkgid_path, strlen(osp_share2_pkgid_path));
335 if (chdir(app_rootpath) != 0)
337 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
341 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
343 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
345 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
346 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
349 for (j = i; j > 0; --j)
351 umount2(mount_info[j-1].dest_path, MNT_DETACH);
361 link_osp_share_path(const char* app_rootpath, const char* pkgid)
363 char osp_app_share_path[PATH_MAX] = { 0, };
364 char osp_app_share2_path[PATH_MAX] = { 0, };
365 char osp_share_pkgid_path[PATH_MAX] = { 0, };
366 char osp_share2_pkgid_path[PATH_MAX] = { 0, };
368 strncpy(osp_app_share_path, app_rootpath, strlen(app_rootpath));
369 strncat(osp_app_share_path, "/shared/data", 12);
371 strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
372 strncat(osp_share_pkgid_path, "share/", 6);
373 strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
375 strncpy(osp_app_share2_path, app_rootpath, strlen(app_rootpath));
376 strncat(osp_app_share2_path, "/shared/trusted", 15);
378 strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
379 strncat(osp_share2_pkgid_path, "share2/", 7);
380 strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
382 unlink(osp_share_pkgid_path);
383 unlink(osp_share2_pkgid_path);
385 int ret = symlink(osp_app_share_path, osp_share_pkgid_path);
386 if (ret == -1 && errno != EEXIST)
388 LOGE("symlink() failed, src path: %s, dest path: %s, errno: %d (%s)",
389 osp_app_share_path, osp_share_pkgid_path, errno, strerror(errno));
393 ret = symlink(osp_app_share2_path, osp_share2_pkgid_path);
394 if (ret == -1 && errno != EEXIST)
396 LOGE("symlink() failed, src path: %s, dest path: %s, errno: %d (%s)",
397 osp_app_share2_path, osp_share2_pkgid_path, errno, strerror(errno));
405 create_osp_external_paths(const char* app_rootpath, const char* pkgid)
407 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
408 char osp_ext_apps_pkgid_share_path[PATH_MAX] = {0, };
409 char osp_ext_apps_pkgid_share2_path[PATH_MAX] = {0, };
410 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
411 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
412 struct _dir_info external_dirs[] = {
413 { "./HomeExt", 0000, 0},
414 { "./ShareExt", 0000, 0},
415 { "./Share2Ext", 0000, 0},
416 { "/opt/storage/sdcard/osp", 0777, 0 },
417 { "/opt/storage/sdcard/osp/apps", 0777, 0 },
418 { "/opt/storage/sdcard/osp/share", 0777, 0 },
419 { "/opt/storage/sdcard/osp/share2", 0777, 0 },
425 { "/opt/storage/sdcard/Images", 0777, 0 },
426 { "/opt/storage/sdcard/Sounds", 0777, 0 },
427 { "/opt/storage/sdcard/Videos", 0777, 0 },
428 //{ "/opt/storage/sdcard/Themes", 0777, 0 },
429 { "/opt/storage/sdcard/Others", 0777, 0 }
433 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
434 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
435 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
437 strncpy(osp_ext_apps_pkgid_share_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
438 strncat(osp_ext_apps_pkgid_share_path, "/Share", 6);
440 strncpy(osp_ext_apps_pkgid_share2_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
441 strncat(osp_ext_apps_pkgid_share2_path, "/Share2", 7);
443 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
444 strncat(osp_ext_share_pkgid_path, "share/", 6);
445 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
447 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
448 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
449 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
451 strncpy(external_dirs[7].path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
452 strncpy(external_dirs[8].path, osp_ext_apps_pkgid_share_path, strlen(osp_ext_apps_pkgid_share_path));
453 strncpy(external_dirs[9].path, osp_ext_apps_pkgid_share2_path, strlen(osp_ext_apps_pkgid_share2_path));
454 strncpy(external_dirs[10].path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
455 strncpy(external_dirs[11].path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
457 if (chdir(app_rootpath) != 0)
459 LOGE("chdir() failed (%s), path: %s", strerror(errno), app_rootpath);
463 for (i = 0; i < sizeof(external_dirs)/sizeof(struct _dir_info); i++)
465 int ret = mkdir(external_dirs[i].path, external_dirs[i].mode);
466 if (ret == -1 && errno != 17) // EEXIST
468 LOGE("mkdir() failed, path: %s, errno: %d (%s)", external_dirs[i].path, errno, strerror(errno));
477 mount_osp_external_paths(const char* app_rootpath, const char* pkgid)
479 char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
480 char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
481 char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
482 struct _path_info mount_info[] = {
483 { "/opt/storage/sdcard", "./Storagecard/Media" },
484 { "/opt/storage/sdcard/osp/share", "./ShareExt" },
485 { "/opt/storage/sdcard/osp/share2", "./Share2Ext" },
486 { "\0", "./HomeExt" },
487 { "\0", "./HomeExt/Share" },
488 { "\0", "./HomeExt/Share2" },
492 strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
493 strncat(osp_ext_apps_pkgid_path, "apps/", 5);
494 strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
496 strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
497 strncat(osp_ext_share_pkgid_path, "share/", 6);
498 strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
500 strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
501 strncat(osp_ext_share2_pkgid_path, "share2/", 7);
502 strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
504 strncpy(mount_info[3].src_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
505 strncpy(mount_info[4].src_path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
506 strncpy(mount_info[5].src_path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
508 if (chdir(app_rootpath) != 0)
510 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
513 LOGI("app_rootpath: %s", app_rootpath);
515 for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); i++)
517 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
519 LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
520 mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
523 for (j = i; j > 0; --j)
525 umount2(mount_info[j-1].dest_path, MNT_DETACH);
535 do_pre_exe(const char* package_name, const char* bin_path, const char* package_id)
537 char* app_rootpath = NULL;
538 char osp_app_data_path[PATH_MAX] = {0, };
539 //char internal_installed = 1; // true
543 /* e.g., app_rootdir is "/opt/apps/com.samsung.basicapp or /opt/osp/applications/[appId] */
544 app_rootpath = get_app_rootpath_from_path(bin_path);
546 LOGI("[data_caging] do_pre_exe() was called, package name: %s, \
547 binary path: %s, application root(home) path: %s, package id: %s",
548 package_name, bin_path, app_rootpath, package_id);
552 // TODO: Check whether the application is installed in internal or external storage.
553 // Check installation position using the input path.
554 //if (internal_installed)
557 if (!internal_is_mounted(package_id))
559 if (mount_slp_paths(app_rootpath) != 0)
562 if (mount_osp_internal_paths(app_rootpath, package_id) != 0)
567 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
570 LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
572 if (mmc_mounted == 1)
574 LOGI("MMC is mounted.");
575 if (create_osp_external_paths(app_rootpath, package_id) != 0)
580 if (!external_is_mounted(package_id))
582 if (mount_osp_external_paths(app_rootpath, package_id) != 0)
591 // TODO mount_external_paths(app_rootpath);
593 LOGI("mount() succeeded.");
595 if (chroot(app_rootpath) != 0)
597 LOGE("chroot() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
602 LOGE("chdir() failed, path: /, errno: %d (%s)", errno, strerror(errno));
605 LOGI("chroot() succeeded.");
607 // set current working dir to "/opt/apps/{packageId}/data"
609 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
610 strncat(osp_app_data_path, "/data", strlen("/data"));
613 if (chdir("/data") != 0)
615 LOGE("chdir() failed, path: /data, errno: %d (%s)", errno, strerror(errno));
622 LOGI("[data_caging] do_pre_exec() succeeded.");
629 LOGI("[data_caging] do_pre_exec() failed.");
634 do_pre_exec(const char* package_name, const char* bin_path)
636 char* app_rootpath = NULL;
637 char app_compat_path[PATH_MAX] = { 0, };
638 const char app_compat_file[] = "/info/compat.info\0";
640 char package_id[_MAX_PACKAGEID_LENGTH + 1] = { 0, };
641 char osp_app_data_path[PATH_MAX] = { 0, };
644 LOGI("do_pre_exec() is called, package_name: %s, bin_path: %s", package_name, bin_path);
646 app_rootpath = get_app_rootpath_from_path(bin_path);
648 strncpy(app_compat_path, app_rootpath, strlen(app_rootpath));
649 strncat(app_compat_path, app_compat_file, strlen(app_compat_file));
650 if (access(app_compat_path, F_OK) == 0)
656 //if (package_name == NULL)
658 //LOGI("The package name is empty.");
659 get_package_id_from_app_rootpath(app_rootpath, package_id);
664 get_package_id_from_package_name(package_name, package_id);
669 LOGI("package name: %s, binary path: %s, package id: %s, OSP compatibility: %d",
670 package_name, bin_path, package_id, osp_compat);
672 // FIXME: Temporary code with security risk
673 prctl(PR_SET_KEEPCAPS, 1);
678 //unshare(CLONE_NEWNS);
679 return do_pre_exe(package_name, bin_path, package_id);
682 // API version is equal to or greater than Tizen 2.0
683 // Set current working dir to "/opt/apps/{pkgId}/data"
684 strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
685 strncat(osp_app_data_path, "/data", strlen("/data"));
687 if (chdir(osp_app_data_path) != 0)
689 LOGE("chdir() failed, path: %s, errno: %d (%s)", osp_app_data_path, errno, strerror(errno));
693 if (link_osp_share_path(app_rootpath, package_id) != 0)
698 LOGI("[data_caging] do_pre_exec() succeeded.");