Remove compilation warning
[platform/framework/native/env-config.git] / osp-env-config.c
index 33759a5..f259cdf 100644 (file)
@@ -30,6 +30,7 @@
 #include <limits.h>
 #include <sys/prctl.h>
 #include <sys/vfs.h>
+#include <fcntl.h>
 
 #include <dlog.h>
 #include <vconf.h>
@@ -48,7 +49,6 @@
 #define _SECURE_LOGE(...)
 #endif
 
-static const char* _OSP_HOME_PATH = "/opt/osp/\0"; // /opt/apps/com.samsung.osp
 static const char* _OSP_COMPAT_SHARED_PATH = "/opt/usr/share/.osp-compat/\0";
 static const char* _EXT_OSP_HOME_PATH = "/opt/storage/sdcard/osp/\0";
 
@@ -112,6 +112,7 @@ get_package_id_from_app_rootpath(const char* app_rootpath, char* package_id)
        _SECURE_LOGI("package id: %s", package_id);
 }
 
+#if 0
 static void
 get_package_id_from_package_name(const char* package_name, char* package_id)
 {
@@ -154,6 +155,7 @@ get_package_id_from_package_name(const char* package_name, char* package_id)
        package_id[_MAX_PACKAGEID_LENGTH] = '\0';
        _SECURE_LOGI("package_id: %s", package_id);
 }
+#endif
 
 static int
 internal_is_mounted(const char* pkgid)
@@ -260,18 +262,13 @@ mount_native_paths(const char* app_rootpath)
        static const struct _path_info mount_info[] =
        {
                //{ "/bin",                             "./bin" },
-               //{ "/boot",                    "./boot" },
-               //{ "/cache",                   "./cache" },
                { "/csa",                               "./csa" },
                { "/dev",                               "./dev" },
                { "/dev/pts",                   "./dev/pts" },
                { "/dev/shm",                   "./dev/shm" },
                { "/etc",                               "./etc" },
                { "/lib",                               "./lib" },
-               //{ "/lost+found",              "./lost+found" },
-               { "/media",                             "./media" },
                { "/mnt",                               "./mnt" },
-               //{ "/packaging",               "./packaging" },
                { "/proc",                              "./proc" },
                { "/sbin",                              "./sbin" },
                { "/smack",                             "./smack" },
@@ -285,7 +282,7 @@ mount_native_paths(const char* app_rootpath)
                { "/opt",                               "./opt" },
                { "/opt/usr",                   "./opt/usr" },
                { "/opt/var/kdb/db",    "./opt/var/kdb/db" },
-//             { "/opt/storage/sdcard","./opt/storage/sdcard" }
+               //{ "/opt/storage/sdcard","./opt/storage/sdcard" }
        };
 
        if (chdir(app_rootpath) != 0)
@@ -325,10 +322,6 @@ mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
                { "\0", "./data/Share2" },
                { "/opt/usr/share/.osp-compat/share",   "./Share" },
                { "/opt/usr/share/.osp-compat/share2",  "./Share2" },
-               //{ "/opt/osp/clipboard",   "./Clipboard" },
-               //{ "/opt/osp/partner/npki","./NPKI" },
-               //{ "/opt/osp/system",          "./System" },
-               //{ "/opt/osp/Tmp",                     "./Tmp" },
                { "/opt/usr/media",                     "./Media" }
        };
 
@@ -369,6 +362,83 @@ mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
 }
 
 static int
+mount_linux_paths(const char* app_rootpath)
+{
+       int i = 0;
+       static const struct _path_info mount_info[] =
+       {
+               { "/bin",                               "./bin" },
+               { "/cache",                             "./cache" },
+               { "/csa",                               "./csa" },
+               { "/data",                              "./data" },
+               { "/dev",                               "./dev" },
+               { "/dev/pts",                   "./dev/pts" },
+               { "/dev/shm",                   "./dev/shm" },
+               { "/etc",                               "./etc" },
+               { "/lib",                               "./lib" },
+               { "/lib/modules",               "./lib/modules" },
+               { "/media",                             "./media" },
+               { "/mnt",                               "./mnt" },
+               { "/proc",                              "./proc" },
+               { "/run",                               "./run" },
+               { "/sbin",                              "./sbin" },
+               { "/smack",                             "./smack" },
+               { "/srv",                               "./srv" },
+               { "/sys",                               "./sys" },
+               { "/sys/fs/cgroup",             "./sys/fs/cgroup" },
+               { "/sys/fs/cgroup/systemd",     "./sys/fs/cgroup/systemd" },
+               { "/system",                    "./system" },
+               { "/tmp",                               "./tmp" },
+               { "/usr",                               "./usr" },
+               { "/var",                               "./var" },
+               { "/var/run",                   "./var/run" },
+               { "/opt",                               "./opt" },
+               { "/opt/usr",                   "./opt/usr" },
+               //{ "/opt/var/run",             "./opt/var/run" },
+               { "/opt/storage/sdcard","./opt/storage/sdcard" },
+       };
+
+       if (chdir(app_rootpath) != 0)
+       {
+               LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
+               return -1;
+       }
+
+       for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
+       {
+               if (i == 27) // /opt/storage/sdcard
+               {
+                       int mmc_mounted = 0;
+                       int ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
+                       if (ret < 0)
+                       {
+                               LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
+                       }
+                       if (mmc_mounted == 0)
+                       {
+                               continue;
+                       }
+               }
+
+               LOGI("src path: %s, dest path: %s", mount_info[i].src_path, mount_info[i].dest_path);
+               if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
+               {
+                       LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
+                                       mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
+
+                       int j = 0;
+                       for (j = i; j > 0; --j)
+                       {
+                               umount2(mount_info[j-1].dest_path, MNT_DETACH);
+                       }
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static int
 create_osp_external_paths(const char* app_rootpath, const char* pkgid)
 {
        char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
@@ -503,10 +573,7 @@ int
 do_pre_exe(const char* package_name, const char* bin_path, const char* package_id)
 {
        char* app_rootpath = NULL;
-       char osp_app_data_path[PATH_MAX] = {0, };
-       //char internal_installed = 1; // true
        int mmc_mounted = 0;
-       struct statfs fs;
 
        /* e.g., app_rootdir is "/opt/usr/apps/[pkgId] */
        app_rootpath = get_app_rootpath_from_path(bin_path);
@@ -516,11 +583,6 @@ do_pre_exe(const char* package_name, const char* bin_path, const char* package_i
 
        umask(0000);
 
-       // TODO: Check whether the application is installed in internal or external storage.
-       // Check installation position using the input path.
-       //if (internal_installed)
-       //{
-
        if (!internal_is_mounted(package_id))
        {
                if (mount_native_paths(app_rootpath) != 0)
@@ -554,12 +616,6 @@ do_pre_exe(const char* package_name, const char* bin_path, const char* package_i
                        }
                }
        }
-
-       /*}
-         else
-         {
-       // TODO mount_external_paths(app_rootpath);
-       }*/
        LOGI("mount() succeeded.");
 
        if (chroot(app_rootpath) != 0)
@@ -574,12 +630,6 @@ do_pre_exe(const char* package_name, const char* bin_path, const char* package_i
        }
        LOGI("chroot() succeeded.");
 
-       // set current working dir to "/opt/apps/{packageId}/data"
-#if 0
-       strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
-       strncat(osp_app_data_path, "/data", strlen("/data"));
-#endif
-
        if (chdir("/data") != 0)
        {
                LOGE("chdir() failed, path: /data, errno: %d (%s)", errno, strerror(errno));
@@ -600,13 +650,53 @@ ERROR:
        return -1;
 }
 
+static int
+do_virtual_root(const char* virtual_root_path, const char* package_id)
+{
+       _SECURE_LOGI("[virtual_root] do_virtual_root() was called, virtual root path: %s, package id: %s",
+                       virtual_root_path, package_id);
+
+       umask(0000);
+
+       if (!internal_is_mounted(package_id))
+       {
+               if (mount_linux_paths(virtual_root_path) != 0)
+               {
+                       goto ERROR;
+               }
+       }
+       LOGI("mount() succeeded.");
+
+       if (chroot(virtual_root_path) != 0)
+       {
+               LOGE("chroot() failed. path: %s, errno: %d (%s)", virtual_root_path, errno, strerror(errno));
+               goto ERROR;
+       }
+       if (chdir("/") != 0)
+       {
+               LOGE("chdir() failed. path: /data, errno: %d (%s)", errno, strerror(errno));
+               goto ERROR;
+       }
+       LOGI("chroot() succeeded.");
+
+       umask(0022);
+
+       LOGI("[virtual_root] do_virtual_root() succeeded.");
+       return 0;
+
+ERROR:
+       umask(0022);
+
+       LOGI("[virtual_root] do_virtual_root() failed.");
+       return -1;
+}
+
 int
 do_pre_exec(const char* package_name, const char* bin_path)
 {
        char* app_rootpath = NULL;
        char app_compat_path[PATH_MAX] = { 0, };
        const char app_compat_file[] = "/info/compat.info\0";
-       int pathlen = 0;
        char package_id[_MAX_PACKAGEID_LENGTH + 1] = { 0, };
        char osp_app_data_path[PATH_MAX] = { 0, };
        int osp_compat = 0;
@@ -648,6 +738,38 @@ do_pre_exec(const char* package_name, const char* bin_path)
                return do_pre_exe(package_name, bin_path, package_id);
        }
 
+       char virtual_root_file[PATH_MAX] = { 0, };
+       const char virtual_root_info[] = "/info/virtualroot.info";
+       strncpy(virtual_root_file, app_rootpath, strlen(app_rootpath));
+       strncat(virtual_root_file, virtual_root_info, strlen(virtual_root_info));
+       if (access(virtual_root_file, F_OK) == 0)
+       {
+               LOGI("This is virtual root application.");
+               int fd = open(virtual_root_file, O_RDONLY);
+               if (fd < 0)
+               {
+                       LOGE("failed to open %s, errno: %d (%s)", virtual_root_file, errno, strerror(errno));
+                       goto ERROR;
+               }
+
+               char user_path[PATH_MAX] = { 0, };
+               int read_bytes = read(fd, user_path, PATH_MAX);
+               if (read_bytes < 0)
+               {
+                       LOGE("failed to read %s, errno: %d (%s)", virtual_root_file, errno, strerror(errno));
+                       close(fd);
+                       goto ERROR;
+               }
+               close(fd);
+
+               char virtual_root_path[PATH_MAX] = { 0, };
+               sprintf(virtual_root_path, "%s/data/%s", app_rootpath, user_path);
+
+               int res = do_virtual_root(virtual_root_path, package_id);
+               free(app_rootpath);
+               return res;
+       }
+
        // API version is equal to or greater than Tizen 2.0
        // Set current working dir to "/opt/apps/{pkgId}/data"
        strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));