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.
18 * @file CompatibilityManager.cpp
19 * @brief This is the implementation file for %CompatibilityManager class.
23 #include <sys/mount.h>
26 #include <unique_ptr.h>
29 #include <FIoDirectory.h>
30 #include <FBase_StringConverter.h>
31 #include <FSys_EnvironmentImpl.h>
33 #include "InstallerDefs.h"
34 #include "CompatibilityManager.h"
35 #include "SmackManager.h"
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40 using namespace Tizen::System;
51 char srcPath[PATH_MAX];
52 char destPath[PATH_MAX];
57 char destPath[PATH_MAX];
61 CompatibilityManager::CompatibilityManager(void)
65 CompatibilityManager::~CompatibilityManager(void)
70 CompatibilityManager::PrepareDataCaging(const String& rootPath, const PackageId& packageId)
76 std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(rootPath));
77 TryCatch(pRootPath != null, res = false, "The memory is insufficient.");
79 res = CleanDirectories(rootPath, packageId);
80 TryCatch(res == true, , "CleanDirectories(%ls, %ls) failed.", rootPath.GetPointer(), packageId.GetPointer());
82 pCwd = get_current_dir_name();
83 TryCatch(pCwd != null, res = false, "get_current_dir_name() failed. [errno = %d(%s)]", errno, strerror(errno));
87 result = chdir(pRootPath.get());
88 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pRootPath.get(), errno, strerror(errno));
90 res = CreateOspInternalDirectories(rootPath, packageId);
91 TryCatch(res == true, , "CreateOspInternalDirectories(%ls, %ls) failed.", rootPath.GetPointer(), packageId.GetPointer());
93 res = CreateSlpDirectories();
94 TryCatch(res == true, , "CreateSlpDirectories() failed.");
96 res = CreateSymbolicLink();
97 TryCatch(res == true, , "CreateSymbolicLink() failed.");
100 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pCwd, errno, strerror(errno));
110 AppLog("[Tizen::Io] result = [%s]", res?"true":"false");
116 CompatibilityManager::FinalizeDataCaging(const String& rootPath)
121 static const struct _PathInfo mountPath[] =
131 { "./opt/var/kdb/db" },
132 { "./opt/storage/sdcard" },
138 { "./sys/kernel/debug" },
149 { "./Storagecard/Media" },
152 { "./HomeExt/Share" },
153 { "./HomeExt/Share2" },
157 std::unique_ptr<char[]> pAppRootPath(_StringConverter::CopyToCharArrayN(rootPath));
158 TryReturn(pAppRootPath, false, "The memory is insufficient.");
160 res = chdir(pAppRootPath.get());
161 TryReturn(res == 0, false, "chdir() failed. (%d, %s), path = [%ls]", errno, strerror(errno), rootPath.GetPointer());
163 for (i = 0; i < sizeof(mountPath) / sizeof(struct _PathInfo); ++i)
165 res = umount2(mountPath[i].destPath, MNT_DETACH);
166 TryReturn((res == 0) || (errno == EINVAL) || (errno == ENOENT), false, "umount2() failed. (%d, %s), path = [%s]", errno, strerror(errno), mountPath[i].destPath);
169 char* pPackageId = strrchr(pAppRootPath.get(), '/');
170 char mountFlag[PATH_MAX] = {0, };
172 sprintf(mountFlag, "/tmp/osp-compat/mount/internal/%s", ++pPackageId);
173 res = unlink(mountFlag);
174 if ((res == -1) && (errno != ENOENT))
176 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
180 memset(mountFlag, 0, PATH_MAX);
181 sprintf(mountFlag, "/tmp/osp-compat/mount/external/%s", pPackageId);
182 res = unlink(mountFlag);
183 if ((res == -1) && (errno != ENOENT))
185 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
189 AppLog("[Tizen::Io] FinalizeDataCaging() succeeded, rootPath: %ls", rootPath.GetPointer());
194 CompatibilityManager::CleanDirectories(const String& rootPath, const PackageId& packageId)
197 char removeCmd[PATH_MAX] = {0, };
199 String ospSharePkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
200 ospSharePkgIdPath.Append(L"share/");
201 ospSharePkgIdPath.Append(packageId);
203 String ospShare2PkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
204 ospShare2PkgIdPath.Append(L"share2/");
205 ospShare2PkgIdPath.Append(packageId);
207 sprintf(removeCmd, "rm -rf %ls", ospSharePkgIdPath.GetPointer());
208 AppLog("removeCmd = [%s]", removeCmd);
210 ret = system(removeCmd);
211 TryReturn(ret != -1, false, "system(%s) failed.", removeCmd);
213 memset(removeCmd, 0, PATH_MAX);
215 sprintf(removeCmd, "rm -rf %ls", ospShare2PkgIdPath.GetPointer());
216 AppLog("removeCmd = [%s]", removeCmd);
218 ret = system(removeCmd);
219 TryReturn(ret != -1, false, "system(%s) failed.", removeCmd);
221 AppLog("[Tizen::Io] CleanDirectories() succeeded, rootPath: %ls", rootPath.GetPointer());
226 CompatibilityManager::LinkOspSharePath(const String& rootPath, const PackageId& packageId)
228 const char* pOspCompatSharedPath = "/opt/usr/share/.osp-compat/\0";
230 std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(rootPath));
231 TryReturn(pRootPath, false, "The memory is insufficient.");
233 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
234 TryReturn(pPackageId, false, "The memory is insufficient.");
236 char ospAppSharePath[PATH_MAX] = {0, };
237 char ospAppShare2Path[PATH_MAX] = {0, };
238 char ospSharePkgIdPath[PATH_MAX] = {0, };
239 char ospShare2PkgIdPath[PATH_MAX] = {0, };
241 strncpy(ospAppSharePath, pRootPath.get(), strlen(pRootPath.get()));
242 strncat(ospAppSharePath, "/shared/data", 12);
244 strncpy(ospSharePkgIdPath, pOspCompatSharedPath, strlen(pOspCompatSharedPath));
245 strncat(ospSharePkgIdPath, "share/", 6);
246 strncat(ospSharePkgIdPath, pPackageId.get(), strlen(pPackageId.get()));
248 strncpy(ospAppShare2Path, pRootPath.get(), strlen(pRootPath.get()));
249 strncat(ospAppShare2Path, "/shared/trusted", 15);
251 strncpy(ospShare2PkgIdPath, pOspCompatSharedPath, strlen(pOspCompatSharedPath));
252 strncat(ospShare2PkgIdPath, "share2/", 7);
253 strncat(ospShare2PkgIdPath, pPackageId.get(), strlen(pPackageId.get()));
255 unlink(ospSharePkgIdPath);
256 unlink(ospShare2PkgIdPath);
258 InstallerUtil::CreateSymlink(ospAppSharePath, ospSharePkgIdPath, false);
259 InstallerUtil::CreateSymlink(ospAppShare2Path, ospShare2PkgIdPath, false);
265 CompatibilityManager::PrepareVirtualRoot(const Tizen::Base::String& rootPath, const Tizen::App::PackageId& packageId)
267 result r = E_SUCCESS;
271 String destPath = rootPath + DIR_VIRTUAL_ROOT;
272 SmackManager smackManager;
274 std::unique_ptr<char[]> pVirtualRootPath(_StringConverter::CopyToCharArrayN(destPath));
275 TryCatch(pVirtualRootPath != null, res = false, "The memory is insufficient.");
277 pCwd = get_current_dir_name();
278 TryCatch(pCwd != null, res = false, "get_current_dir_name() failed. [errno = %d(%s)]", errno, strerror(errno));
280 // appRoot/virtual-root
281 if (File::IsFileExist(destPath) == false)
283 r = Directory::Create(destPath, false);
284 TryCatch(!IsFailed(r), res = false, "Directory::Create() failed");
287 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
289 result = chdir(pVirtualRootPath.get());
290 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pVirtualRootPath.get(), errno, strerror(errno));
292 res = CreateVirtualRootDirectories();
293 TryCatch(res == true, , "CreateVirtualRootDirectories() failed.");
295 smackManager.AddLabelDir(packageId, destPath);
297 res = CreateSymbolicLink();
298 TryCatch(res == true, , "CreateSymbolicLink() failed.");
300 result = chdir(pCwd);
301 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pCwd, errno, strerror(errno));
309 AppLog("[Tizen::Io] result = [%s]", res?"true":"false");
315 CompatibilityManager::FinalizeVirtualRoot(const String& rootPath, const PackageId& packageId)
319 String destPath = rootPath + DIR_VIRTUAL_ROOT;
320 static const struct _PathInfo mountPath[] =
339 { "./sys/fs/cgroup/systemd" },
340 { "./sys/fs/cgroup" },
348 //{ "./opt/var/run" },
349 { "./opt/storage/sdcard" },
353 std::unique_ptr<char[]> pVirtualRootPath(_StringConverter::CopyToCharArrayN(destPath));
354 TryReturn(pVirtualRootPath, false, "The memory is insufficient.");
356 res = chdir(pVirtualRootPath.get());
357 TryReturn(res == 0, false, "chdir() failed. (%d, %s), path = [%ls]", errno, strerror(errno), destPath.GetPointer());
359 for (i = 0; i < sizeof(mountPath) / sizeof(struct _PathInfo); ++i)
361 res = umount2(mountPath[i].destPath, MNT_DETACH);
362 TryReturn((res == 0) || (errno == EINVAL) || (errno == ENOENT), false, "umount2() failed. (%d, %s), path = [%s]", errno, strerror(errno), mountPath[i].destPath);
365 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
366 TryReturn(pPackageId, false, "The memory is insufficient.");
368 char mountFlag[PATH_MAX] = {0, };
369 sprintf(mountFlag, "/tmp/osp-compat/mount/internal/%s", pPackageId.get());
370 res = unlink(mountFlag);
371 if ((res == -1) && (errno != ENOENT))
373 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
377 memset(mountFlag, 0, PATH_MAX);
378 sprintf(mountFlag, "/tmp/osp-compat/mount/external/%s", pPackageId.get());
379 res = unlink(mountFlag);
380 if ((res == -1) && (errno != ENOENT))
382 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
386 AppLog("[Tizen::Io] FinalizeVirtualRoot() succeeded, path: %ls", destPath.GetPointer());
391 CompatibilityManager::CreateOspInternalDirectories(const String& rootPath, const PackageId& packageId)
393 result r = E_SUCCESS;
398 struct _OspDir appSubDir[] =
400 { "./shared", 0755, false },
401 { "./data/Share", 0000, true }, // mount from /opt/usr/share/.osp-compat/share/{pkgId}
402 { "./data/Share2", 0000, true }, // mount from /opt/usr/share/.osp-compat/share2/{pkgId}
403 { "./Share", 0000, false }, // mount from /opt/usr/share/.osp-compat/share
404 { "./Share2", 0000, false }, // mount from /opt/usr/share/.osp-compat/share2
405 { "./Media", 0000, false }, // mount from /opt/usr/media
406 { "./Storagecard", 0705, false },
407 { "./Storagecard/Media", 0000, false }, // mount from /opt/storage/sdcard
410 String ospCompatSharedPath = _EnvironmentImpl::GetOspCompatSharedPath();
411 String ospShareAppIdPath(L"share/");
412 String ospShare2AppIdPath(L"share2/");
414 r = ospShareAppIdPath.Insert(ospCompatSharedPath, 0);
415 TryReturn(!IsFailed(r), false, "String::Insert() failed. [error: %s]", GetErrorMessage(r));
417 r = ospShare2AppIdPath.Insert(ospCompatSharedPath, 0);
418 TryReturn(!IsFailed(r), false, "String::Insert() failed. [error: %s]", GetErrorMessage(r));
420 for (i = 0; i < sizeof(appSubDir) / sizeof(struct _OspDir); i++)
422 ret = mkdir(appSubDir[i].path, appSubDir[i].mode);
423 if (ret == -1 && errno != 17) // EEXIST
425 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), appSubDir[i].path, appSubDir[i].mode);
429 if (appSubDir[i].appPrivilege)
431 ret = chown(appSubDir[i].path, uid, uid);
434 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), appSubDir[i].path, uid);
440 SmackManager smackManager;
441 smackManager.AddLabelDir(L"_", L"./Storagecard");
443 ospShareAppIdPath.Append(packageId);
445 std::unique_ptr<char[]> pOspShareAppIdPath(_StringConverter::CopyToCharArrayN(ospShareAppIdPath));
446 TryReturn(pOspShareAppIdPath, false, "The memory is insufficient.");
448 ret = mkdir(pOspShareAppIdPath.get(), 0705);
449 if (ret == -1 && errno != 17) // EEXIST
451 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), pOspShareAppIdPath.get(), 0705);
455 ret = chown(pOspShareAppIdPath.get(), uid, uid);
458 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), pOspShareAppIdPath.get(), uid);
462 ospShare2AppIdPath.Append(packageId);
464 std::unique_ptr<char[]> pOspShare2AppIdPath(_StringConverter::CopyToCharArrayN(ospShare2AppIdPath));
465 TryReturn(pOspShare2AppIdPath, false, "The memory is insufficient.");
467 ret = mkdir(pOspShare2AppIdPath.get(), 0705);
468 if (ret == -1 && errno != 17) // EEXIST
470 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), pOspShare2AppIdPath.get(), 0705);
474 ret = chown(pOspShare2AppIdPath.get(), uid, uid);
477 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), pOspShare2AppIdPath.get(), uid);
481 String appSharedDataPath(rootPath);
482 appSharedDataPath.Append(L"/shared/data");
484 String appSharedTrustedPath(rootPath);
485 appSharedTrustedPath.Append(L"/shared/trusted");
487 r = Directory::Remove(appSharedDataPath, true);
488 TryReturn(r == E_SUCCESS, false, "Directory::Remove(%ls) failed.", appSharedDataPath.GetPointer());
490 r = Directory::Remove(appSharedTrustedPath, true);
491 TryReturn(r == E_SUCCESS, false, "Directory::Remove(%ls) failed.", appSharedTrustedPath.GetPointer());
493 res = InstallerUtil::CreateSymlink(ospShareAppIdPath, L"./shared/data");
494 TryReturn(res == true, false, "CreateSymlink() failed, errno: %d (%s), link: ./shared/data -> %s", errno, strerror(errno), pOspShareAppIdPath.get());
496 res = InstallerUtil::CreateSymlink(ospShare2AppIdPath, L"./shared/trusted");
497 TryReturn(res == true, false, " CreateSymlink() failed, errno: %d (%s), link: ./shared/trusted -> %s", errno, strerror(errno), pOspShare2AppIdPath.get());
499 AppLog("[Tizen::Io] CreateOspInternalDirectories() succeeded, rootPath: %ls", rootPath.GetPointer());
504 CompatibilityManager::CreateSlpDirectories(void)
508 struct _OspDir slpDir[] =
510 { "./csa", 0000, false },
511 { "./dev", 0000, false },
512 { "./etc", 0000, false },
513 { "./lib", 0000, false },
514 { "./media", 0000, false },
515 { "./mnt", 0000, false },
516 { "./opt", 0000, false },
517 { "./proc", 0000, false },
518 { "./sbin", 0000, false },
519 { "./smack", 0000, false },
520 { "./srv", 0000, false },
521 { "./sys", 0000, false },
522 { "./tmp", 0000, false },
523 { "./usr", 0000, false },
524 { "./var", 0000, false }
527 for (i = 0; i < sizeof(slpDir) / sizeof(struct _OspDir); i++)
529 ret = mkdir(slpDir[i].path, slpDir[i].mode);
530 if (ret == -1 && errno != 17) // EEXIST
532 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), slpDir[i].path, slpDir[i].mode);
537 AppLog("[Tizen::Io] CreateSlpDirectories() succeeded.");
542 CompatibilityManager::CreateSymbolicLink(void)
544 struct _LinkDir linkDirList[] =
546 { "/opt/home", "./home" },
547 { "/opt/home/root", "./root" },
548 { "/mnt/mmc", "./sdcard" }
551 for (unsigned int i = 0; i < sizeof(linkDirList) / sizeof(struct _LinkDir); ++i)
553 InstallerUtil::CreateSymlink(linkDirList[i].srcPath, linkDirList[i].destPath, false);
556 AppLog("[Tizen::Io] CreateSymbolicLink() succeeded.");
561 CompatibilityManager::CreateVirtualRootDirectories(void)
565 struct _OspDir virtualRootDir[] =
567 { "./bin", 0000, false },
568 { "./cache", 0000, false },
569 { "./csa", 0000, false },
570 { "./data", 0000, false },
571 { "./dev", 0000, false },
572 { "./etc", 0000, false },
573 { "./lib", 0000, false },
574 { "./media", 0000, false },
575 { "./mnt", 0000, false },
576 { "./opt", 0000, false },
577 { "./proc", 0000, false },
578 { "./run", 0000, false },
579 { "./sbin", 0000, false },
580 { "./smack", 0000, false },
581 { "./srv", 0000, false },
582 { "./sys", 0000, false },
583 { "./system", 0000, false },
584 { "./tmp", 0000, false },
585 { "./usr", 0000, false },
586 { "./var", 0000, false }
589 for (i = 0; i < sizeof(virtualRootDir) / sizeof(struct _OspDir); i++)
591 ret = mkdir(virtualRootDir[i].path, virtualRootDir[i].mode);
592 if (ret == -1 && errno != 17) // EEXIST
594 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), virtualRootDir[i].path, virtualRootDir[i].mode);
599 AppLog("[Tizen::Io] CreateVirtualRootDirectories() succeeded.");