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[] =
132 { "./opt/var/kdb/db" },
133 { "./opt/storage/sdcard" },
139 { "./sys/kernel/debug" },
150 { "./Storagecard/Media" },
153 { "./HomeExt/Share" },
154 { "./HomeExt/Share2" },
158 std::unique_ptr<char[]> pAppRootPath(_StringConverter::CopyToCharArrayN(rootPath));
159 TryReturn(pAppRootPath, false, "The memory is insufficient.");
161 res = chdir(pAppRootPath.get());
162 TryReturn(res == 0, false, "chdir() failed. (%d, %s), path = [%ls]", errno, strerror(errno), rootPath.GetPointer());
164 for (i = 0; i < sizeof(mountPath) / sizeof(struct _PathInfo); ++i)
166 res = umount2(mountPath[i].destPath, MNT_DETACH);
167 TryReturn((res == 0) || (errno == EINVAL) || (errno == ENOENT), false, "umount2() failed. (%d, %s), path = [%s]", errno, strerror(errno), mountPath[i].destPath);
170 char* pPackageId = strrchr(pAppRootPath.get(), '/');
171 char mountFlag[PATH_MAX] = {0, };
173 sprintf(mountFlag, "/tmp/osp-compat/mount/internal/%s", ++pPackageId);
174 res = unlink(mountFlag);
175 if ((res == -1) && (errno != ENOENT))
177 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
181 memset(mountFlag, 0, PATH_MAX);
182 sprintf(mountFlag, "/tmp/osp-compat/mount/external/%s", pPackageId);
183 res = unlink(mountFlag);
184 if ((res == -1) && (errno != ENOENT))
186 AppLog("unlink(%s) failed, errno: %d (%s)", mountFlag, errno, strerror(errno));
190 AppLog("[Tizen::Io] FinalizeDataCaging() succeeded, rootPath: %ls", rootPath.GetPointer());
195 CompatibilityManager::CleanDirectories(const String& rootPath, const PackageId& packageId)
198 char removeCmd[PATH_MAX] = {0, };
200 String ospSharePkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
201 ospSharePkgIdPath.Append(L"share/");
202 ospSharePkgIdPath.Append(packageId);
204 String ospShare2PkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
205 ospShare2PkgIdPath.Append(L"share2/");
206 ospShare2PkgIdPath.Append(packageId);
208 sprintf(removeCmd, "rm -rf %ls", ospSharePkgIdPath.GetPointer());
209 AppLog("removeCmd = [%s]", removeCmd);
211 ret = system(removeCmd);
212 TryReturn(ret != -1, false, "system(%s) failed.", removeCmd);
214 memset(removeCmd, 0, PATH_MAX);
216 sprintf(removeCmd, "rm -rf %ls", ospShare2PkgIdPath.GetPointer());
217 AppLog("removeCmd = [%s]", removeCmd);
219 ret = system(removeCmd);
220 TryReturn(ret != -1, false, "system(%s) failed.", removeCmd);
222 AppLog("[Tizen::Io] CleanDirectories() succeeded, rootPath: %ls", rootPath.GetPointer());
227 CompatibilityManager::LinkOspSharePath(const String& rootPath, const PackageId& packageId)
230 const char* pOspCompatSharedPath = "/opt/usr/share/.osp-compat/\0";
232 std::unique_ptr<char[]> pRootPath(_StringConverter::CopyToCharArrayN(rootPath));
233 TryReturn(pRootPath, false, "The memory is insufficient.");
235 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
236 TryReturn(pPackageId, false, "The memory is insufficient.");
238 char ospAppSharePath[PATH_MAX] = {0, };
239 char ospAppShare2Path[PATH_MAX] = {0, };
240 char ospSharePkgIdPath[PATH_MAX] = {0, };
241 char ospShare2PkgIdPath[PATH_MAX] = {0, };
243 strncpy(ospAppSharePath, pRootPath.get(), strlen(pRootPath.get()));
244 strncat(ospAppSharePath, "/shared/data", 12);
246 strncpy(ospSharePkgIdPath, pOspCompatSharedPath, strlen(pOspCompatSharedPath));
247 strncat(ospSharePkgIdPath, "share/", 6);
248 strncat(ospSharePkgIdPath, pPackageId.get(), strlen(pPackageId.get()));
250 strncpy(ospAppShare2Path, pRootPath.get(), strlen(pRootPath.get()));
251 strncat(ospAppShare2Path, "/shared/trusted", 15);
253 strncpy(ospShare2PkgIdPath, pOspCompatSharedPath, strlen(pOspCompatSharedPath));
254 strncat(ospShare2PkgIdPath, "share2/", 7);
255 strncat(ospShare2PkgIdPath, pPackageId.get(), strlen(pPackageId.get()));
257 unlink(ospSharePkgIdPath);
258 unlink(ospShare2PkgIdPath);
260 SmackManager smackManager;
262 ret = symlink(ospAppSharePath, ospSharePkgIdPath);
263 smackManager.AddLabelDir(L"_", ospSharePkgIdPath);
265 ret = symlink(ospAppShare2Path, ospShare2PkgIdPath);
266 smackManager.AddLabelDir(L"_", ospShare2PkgIdPath);
272 CompatibilityManager::PrepareVirtualRoot(const Tizen::Base::String& rootPath, const Tizen::App::PackageId& packageId)
274 result r = E_SUCCESS;
278 String destPath = rootPath + DIR_VIRTUAL_ROOT;
279 SmackManager smackManager;
281 std::unique_ptr<char[]> pVirtualRootPath(_StringConverter::CopyToCharArrayN(destPath));
282 TryCatch(pVirtualRootPath != null, res = false, "The memory is insufficient.");
284 pCwd = get_current_dir_name();
285 TryCatch(pCwd != null, res = false, "get_current_dir_name() failed. [errno = %d(%s)]", errno, strerror(errno));
287 // appRoot/virtual-root
288 if (File::IsFileExist(destPath) == false)
290 r = Directory::Create(destPath, false);
291 TryCatch(!IsFailed(r), res = false, "Directory::Create() failed");
294 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
296 result = chdir(pVirtualRootPath.get());
297 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pVirtualRootPath.get(), errno, strerror(errno));
299 res = CreateVirtualRootDirectories();
300 TryCatch(res == true, , "CreateVirtualRootDirectories() failed.");
302 smackManager.SetupPath(packageId, destPath, SMACK_DIR_TYPE_PRIVATE);
304 res = CreateSymbolicLink();
305 TryCatch(res == true, , "CreateSymbolicLink() failed.");
307 result = chdir(pCwd);
308 TryCatch(result == 0, res = false, "chdir(%s) failed. [errno = %d(%s)]", pCwd, errno, strerror(errno));
316 AppLog("[Tizen::Io] result = [%s]", res?"true":"false");
322 CompatibilityManager::FinalizeVirtualRoot(const String& rootPath)
326 String destPath = rootPath + DIR_VIRTUAL_ROOT;
327 static const struct _PathInfo mountPath[] =
351 std::unique_ptr<char[]> pVirtualRootPath(_StringConverter::CopyToCharArrayN(destPath));
352 TryReturn(pVirtualRootPath, false, "The memory is insufficient.");
354 res = chdir(pVirtualRootPath.get());
355 TryReturn(res == 0, false, "chdir() failed. (%d, %s), path = [%ls]", errno, strerror(errno), destPath.GetPointer());
357 for (i = 0; i < sizeof(mountPath) / sizeof(struct _PathInfo); ++i)
359 res = umount2(mountPath[i].destPath, MNT_DETACH);
360 TryReturn((res == 0) || (errno == EINVAL) || (errno == ENOENT), false, "umount2() failed. (%d, %s), path = [%s]", errno, strerror(errno), mountPath[i].destPath);
363 AppLog("[Tizen::Io] FinalizeVirtualRoot() succeeded, path: %ls", destPath.GetPointer());
368 CompatibilityManager::CreateOspInternalDirectories(const String& rootPath, const PackageId& packageId)
370 result r = E_SUCCESS;
375 struct _OspDir appSubDir[] =
377 { "./shared", 0755, false },
378 { "./data/Share", 0000, true }, // mount from /opt/usr/share/.osp-compat/share/{pkgId}
379 { "./data/Share2", 0000, true }, // mount from /opt/usr/share/.osp-compat/share2/{pkgId}
380 { "./Share", 0000, false }, // mount from /opt/usr/share/.osp-compat/share
381 { "./Share2", 0000, false }, // mount from /opt/usr/share/.osp-compat/share2
382 { "./Media", 0000, false }, // mount from /opt/usr/media
383 { "./Storagecard", 0705, false },
384 { "./Storagecard/Media", 0000, false }, // mount from /opt/storage/sdcard
386 struct _OspDir mediaDir[] =
388 { "/opt/usr/media/Images", 0777, false },
389 { "/opt/usr/media/Sounds", 0777, false },
390 { "/opt/usr/media/Videos", 0777, false },
391 { "/opt/usr/media/Others", 0777, false }
394 String ospCompatSharedPath = _EnvironmentImpl::GetOspCompatSharedPath();
395 String ospShareAppIdPath(L"share/");
396 String ospShare2AppIdPath(L"share2/");
398 r = ospShareAppIdPath.Insert(ospCompatSharedPath, 0);
399 TryReturn(!IsFailed(r), false, "String::Insert() failed. [error: %s]", GetErrorMessage(r));
401 r = ospShare2AppIdPath.Insert(ospCompatSharedPath, 0);
402 TryReturn(!IsFailed(r), false, "String::Insert() failed. [error: %s]", GetErrorMessage(r));
404 for (i = 0; i < sizeof(appSubDir) / sizeof(struct _OspDir); i++)
406 ret = mkdir(appSubDir[i].path, appSubDir[i].mode);
407 if (ret == -1 && errno != 17) // EEXIST
409 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), appSubDir[i].path, appSubDir[i].mode);
413 if (appSubDir[i].appPrivilege)
415 ret = chown(appSubDir[i].path, uid, uid);
418 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), appSubDir[i].path, uid);
424 SmackManager smackManager;
425 smackManager.SetupPath(packageId, L"./Storagecard", SMACK_DIR_TYPE_ANY_LABEL, L"_");
427 ospShareAppIdPath.Append(packageId);
429 std::unique_ptr<char[]> pOspShareAppIdPath(_StringConverter::CopyToCharArrayN(ospShareAppIdPath));
430 TryReturn(pOspShareAppIdPath, false, "The memory is insufficient.");
432 ret = mkdir(pOspShareAppIdPath.get(), 0705);
433 if (ret == -1 && errno != 17) // EEXIST
435 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), pOspShareAppIdPath.get(), 0705);
439 ret = chown(pOspShareAppIdPath.get(), uid, uid);
442 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), pOspShareAppIdPath.get(), uid);
446 ospShare2AppIdPath.Append(packageId);
448 std::unique_ptr<char[]> pOspShare2AppIdPath(_StringConverter::CopyToCharArrayN(ospShare2AppIdPath));
449 TryReturn(pOspShare2AppIdPath, false, "The memory is insufficient.");
451 ret = mkdir(pOspShare2AppIdPath.get(), 0705);
452 if (ret == -1 && errno != 17) // EEXIST
454 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), pOspShare2AppIdPath.get(), 0705);
458 ret = chown(pOspShare2AppIdPath.get(), uid, uid);
461 AppLog("chown() failed (%s), path: %s, uid: %d", strerror(errno), pOspShare2AppIdPath.get(), uid);
465 for (i = 0; i < sizeof(mediaDir) / sizeof(struct _OspDir); ++i)
467 ret = mkdir(mediaDir[i].path, mediaDir[i].mode);
468 if (ret == -1 && errno != 17) // EEXIST
470 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), mediaDir[i].path, mediaDir[i].mode);
474 ret = chown(mediaDir[i].path, 5000, 5000);
475 TryReturn(ret == 0, false, "chown() failed (%d, %s), path: %s, uid: 5000, gid: 5000", errno, strerror(errno), mediaDir[i].path);
478 String appSharedDataPath(rootPath);
479 appSharedDataPath.Append(L"/shared/data");
481 String appSharedTrustedPath(rootPath);
482 appSharedTrustedPath.Append(L"/shared/trusted");
484 r = Directory::Remove(appSharedDataPath, true);
485 TryReturn(r == E_SUCCESS, false, "Directory::Remove(%ls) failed.", appSharedDataPath.GetPointer());
487 r = Directory::Remove(appSharedTrustedPath, true);
488 TryReturn(r == E_SUCCESS, false, "Directory::Remove(%ls) failed.", appSharedTrustedPath.GetPointer());
490 res = InstallerUtil::CreateSymlink(ospShareAppIdPath, L"./shared/data");
491 TryReturn(res == true, false, "CreateSymlink() failed, errno: %d (%s), link: ./shared/data -> %s", errno, strerror(errno), pOspShareAppIdPath.get());
493 res = InstallerUtil::CreateSymlink(ospShare2AppIdPath, L"./shared/trusted");
494 TryReturn(res == true, false, " CreateSymlink() failed, errno: %d (%s), link: ./shared/trusted -> %s", errno, strerror(errno), pOspShare2AppIdPath.get());
496 AppLog("[Tizen::Io] CreateOspInternalDirectories() succeeded, rootPath: %ls", rootPath.GetPointer());
501 CompatibilityManager::CreateSlpDirectories(void)
505 struct _OspDir slpDir[] =
507 { "./csa", 0000, false },
508 { "./dev", 0000, false },
509 { "./etc", 0000, false },
510 { "./lib", 0000, false },
511 { "./media", 0000, false },
512 { "./mnt", 0000, false },
513 { "./opt", 0000, false },
514 { "./proc", 0000, false },
515 { "./sbin", 0000, false },
516 { "./smack", 0000, false },
517 { "./srv", 0000, false },
518 { "./sys", 0000, false },
519 { "./tmp", 0000, false },
520 { "./usr", 0000, false },
521 { "./var", 0000, false }
524 for (i = 0; i < sizeof(slpDir) / sizeof(struct _OspDir); i++)
526 ret = mkdir(slpDir[i].path, slpDir[i].mode);
527 if (ret == -1 && errno != 17) // EEXIST
529 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), slpDir[i].path, slpDir[i].mode);
534 AppLog("[Tizen::Io] CreateSlpDirectories() succeeded.");
539 CompatibilityManager::CreateSymbolicLink(void)
541 struct _LinkDir linkDirList[] =
543 { "/opt/home", "./home" },
544 { "/opt/home/root", "./root" },
545 { "/mnt/mmc", "./sdcard" }
548 for (unsigned int i = 0; i < sizeof(linkDirList) / sizeof(struct _LinkDir); ++i)
550 InstallerUtil::CreateSymlink(linkDirList[i].srcPath, linkDirList[i].destPath);
553 AppLog("[Tizen::Io] CreateSymbolicLink() succeeded.");
558 CompatibilityManager::CreateVirtualRootDirectories(void)
562 struct _OspDir virtualRootDir[] =
564 { "./bin", 0000, false },
565 { "./cache", 0000, false },
566 { "./csa", 0000, false },
567 { "./data", 0000, false },
568 { "./dev", 0000, false },
569 { "./etc", 0000, false },
570 { "./lib", 0000, false },
571 { "./media", 0000, false },
572 { "./mnt", 0000, false },
573 { "./opt", 0000, false },
574 { "./proc", 0000, false },
575 { "./run", 0000, false },
576 { "./sbin", 0000, false },
577 { "./smack", 0000, false },
578 { "./srv", 0000, false },
579 { "./sys", 0000, false },
580 { "./system", 0000, false },
581 { "./tmp", 0000, false },
582 { "./usr", 0000, false },
583 { "./var", 0000, false }
586 for (i = 0; i < sizeof(virtualRootDir) / sizeof(struct _OspDir); i++)
588 ret = mkdir(virtualRootDir[i].path, virtualRootDir[i].mode);
589 if (ret == -1 && errno != 17) // EEXIST
591 AppLog("mkdir() failed (%s), path: %s, mode: 0%o", strerror(errno), virtualRootDir[i].path, virtualRootDir[i].mode);
596 AppLog("[Tizen::Io] CreateVirtualRootDirectories() succeeded.");