Refactor code
[framework/osp/env-config.git] / src / osp-env-config.c
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sched.h>
26 #include <sys/mount.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <limits.h>
31 #include <sys/prctl.h>
32 #include <sys/vfs.h>
33 #include <fcntl.h>
34 #include <dlfcn.h>
35
36 #include <dlog.h>
37 #include <vconf.h>
38
39 #undef LOG_TAG
40 #define LOG_TAG "ENV_CONFIG"
41
42 #define _MAX_PACKAGEID_LENGTH 10
43 #define _MAX_APIVERSION_LENGTH 3
44
45 #ifdef _SECURE_LOG
46 #define _SECURE_LOGI LOGI
47 #define _SECURE_LOGE LOGE
48 #else
49 #define _SECURE_LOGI(...)
50 #define _SECURE_LOGE(...)
51 #endif
52
53 static const char _OSP_COMPAT_SHARED_PATH[] = "/opt/usr/share/.osp-compat/\0";
54 static const char _EXT_OSP_HOME_PATH[] = "/opt/storage/sdcard/osp/\0";
55 static const char OSP_COMPAT_LIB[] = "/usr/lib/osp/libosp-compat.so.1";
56
57 struct _path_info
58 {
59         char src_path[PATH_MAX];
60         char dest_path[PATH_MAX];
61 };
62
63 struct _dir_info
64 {
65         char path[PATH_MAX];
66         mode_t mode;
67         char app_privilege; // 0: root privilege
68 };
69
70 /*
71  * XXX: The returned app_roodir must be freed in caller.
72  */
73 static char*
74 get_app_rootpath_from_path(const char* bin_path)
75 {
76         char* app_rootpath = NULL;
77         char* delimiter = NULL;
78         size_t length = 0;
79
80         /* e.g., The specified bin_path is "/opt/apps/com.samsung.basicapp/bin/basicapp" */
81         length = strlen(bin_path);
82         app_rootpath = (char *)malloc(length + 1);
83         if(app_rootpath == NULL)
84                 return NULL;
85
86         memset(app_rootpath, '\0', length + 1);
87         strncpy(app_rootpath, bin_path, length);
88
89         _SECURE_LOGI("input bin_path: %s", app_rootpath);
90
91         delimiter = strrchr(app_rootpath, '/');
92         *delimiter = '\0';
93
94         delimiter  = strrchr(app_rootpath, '/');
95         *delimiter = '\0';
96
97         return app_rootpath;
98 }
99
100 static void
101 get_package_id_from_app_rootpath(const char* app_rootpath, char* package_id)
102 {
103         const char* p = NULL;
104         if (strncmp(app_rootpath, "/opt/apps/org.tizen.", 19) == 0)
105         {
106                 p = strrchr(app_rootpath, '.') + 1;
107         }
108         else
109         {
110                 p = strrchr(app_rootpath, '/') + 1;
111         }
112         strncpy(package_id, p, _MAX_PACKAGEID_LENGTH);
113         package_id[_MAX_PACKAGEID_LENGTH] = '\0';
114         _SECURE_LOGI("package id: %s", package_id);
115 }
116
117 #if 0
118 static void
119 get_package_id_from_package_name(const char* package_name, char* package_id)
120 {
121         char* tmpbuf = NULL;
122
123         if (strncmp(package_name, "com", 3) == 0)
124         {   // in case of com.samsung.#osp#[package_id]#[serviceid]
125                 tmpbuf = strstr(package_name, "#osp#");
126                 if (tmpbuf != NULL)
127                 {
128                         strncpy(package_id, tmpbuf + 5, _MAX_PACKAGEID_LENGTH);
129                 }
130         }
131         else if (strncmp(package_name, "osp", 3) == 0)
132         {   // in case of osp.[package_id].#osp#[serviceid]
133                 tmpbuf = strstr(package_name, "osp.");
134                 if (tmpbuf != NULL)
135                 {
136                         strncpy(package_id, tmpbuf + 4, _MAX_PACKAGEID_LENGTH);
137                 }
138         }
139         else if (strncmp(package_name, "org.tizen", 9) == 0)
140         {
141                 // in case of org.tizen.[package_id]#[serviceid]
142                 tmpbuf = strstr(package_name, "org.tizen.");
143                 if (tmpbuf != NULL)
144                 {
145                         strncpy(package_id, tmpbuf + 10, _MAX_PACKAGEID_LENGTH);
146                 }
147         }
148         else if (strlen(package_name) == 10)
149         {
150                 strncpy(package_id, package_name, _MAX_PACKAGEID_LENGTH);
151         }
152         else
153         {
154                 LOGE("package name is invalid (%s)", package_name);
155         }
156
157         package_id[_MAX_PACKAGEID_LENGTH] = '\0';
158         _SECURE_LOGI("package_id: %s", package_id);
159 }
160 #endif
161
162 static int
163 internal_is_mounted(const char* pkgid)
164 {
165         char mount_flag[64] = { 0, };
166         static const char dir[][64] =
167         {
168                 { "/tmp/osp-compat" },
169                 { "/tmp/osp-compat/mount" },
170                 { "/tmp/osp-compat/mount/internal" }
171         };
172
173         sprintf(mount_flag, "/tmp/osp-compat/mount/internal/%s", pkgid);
174         int res = access(mount_flag, F_OK);
175         if (res == 0)
176         {
177                 LOGI("Intenal path is already mounted.");
178                 return 1;
179         }
180         else if (res == -1 && errno == ENOENT)
181         {
182                 int i = 0;
183                 for (i = 0; i < sizeof(dir)/64; ++i)
184                 {
185                         int res = mkdir(dir[i], 0755);
186                         if (res == -1 && errno != EEXIST)
187                         {
188                                 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
189                                 return 1;
190                         }
191                 }
192
193                 int fd = creat(mount_flag, 0644);
194                 if (fd == -1)
195                 {
196                         LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
197                         return 1;
198                 }
199                 close(fd);
200         }
201         else
202         {
203                 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
204                 return 1;
205         }
206
207         LOGI("Intenal path mount succeeded.");
208         return 0;
209 }
210
211 static int
212 external_is_mounted(const char* pkgid)
213 {
214         char mount_flag[64] = { 0, };
215         static const char dir[][64] =
216         {
217                 { "/tmp/osp-compat" },
218                 { "/tmp/osp-compat/mount" },
219                 { "/tmp/osp-compat/mount/external" }
220         };
221
222         sprintf(mount_flag, "/tmp/osp-compat/mount/external/%s", pkgid);
223         int res = access(mount_flag, F_OK);
224         if (res == 0)
225         {
226                 LOGI("Extenal path is already mounted.");
227                 return 1;
228         }
229         else if (res == -1 && errno == ENOENT)
230         {
231                 int i = 0;
232                 for (i = 0; i < sizeof(dir)/64; ++i)
233                 {
234                         int res = mkdir(dir[i], 0755);
235                         if (res == -1 && errno != EEXIST)
236                         {
237                                 LOGE("Failed to create directory (%s), errno: %d (%s)", dir[i], errno, strerror(errno));
238                                 return 1;
239                         }
240                 }
241
242                 int fd = creat(mount_flag, 0644);
243                 if (fd == -1)
244                 {
245                         LOGE("Failed to create mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
246                         return 1;
247                 }
248                 close(fd);
249         }
250         else
251         {
252                 LOGE("Failed to access mount flag (%s), errno: %d (%s)", mount_flag, errno, strerror(errno));
253                 return 1;
254         }
255
256         LOGI("Extenal path mount succeeded.");
257         return 0;
258 }
259
260 static int
261 mount_native_paths(const char* app_rootpath)
262 {
263         int i = 0;
264         static const struct _path_info mount_info[] =
265         {
266                 //{ "/bin",                             "./bin" },
267                 { "/csa",                               "./csa" },
268                 { "/dev",                               "./dev" },
269                 { "/dev/pts",                   "./dev/pts" },
270                 { "/dev/shm",                   "./dev/shm" },
271                 { "/etc",                               "./etc" },
272                 { "/lib",                               "./lib" },
273                 { "/mnt",                               "./mnt" },
274                 { "/proc",                              "./proc" },
275                 { "/sbin",                              "./sbin" },
276                 { "/smack",                             "./smack" },
277                 { "/srv",                               "./srv" },
278                 { "/sys",                               "./sys" },
279                 { "/sys/kernel/debug",  "./sys/kernel/debug" },
280                 { "/tmp",                               "./tmp" },
281                 { "/usr",                               "./usr" },
282                 { "/var",                               "./var" },
283                 { "/var/run",                   "./var/run" },
284                 { "/opt",                               "./opt" },
285                 { "/opt/usr",                   "./opt/usr" },
286                 { "/opt/var/kdb/db",    "./opt/var/kdb/db" },
287                 //{ "/opt/storage/sdcard","./opt/storage/sdcard" }
288         };
289
290         if (chdir(app_rootpath) != 0)
291         {
292                 LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
293                 return -1;
294         }
295
296         for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
297         {
298                 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
299                 {
300                         LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
301                                         mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
302
303                         int j = 0;
304                         for (j = i; j > 0; --j)
305                         {
306                                 umount2(mount_info[j-1].dest_path, MNT_DETACH);
307                         }
308                         return -1;
309                 }
310         }
311
312         return 0;
313 }
314
315 static int
316 mount_osp_internal_paths(const char* app_rootpath, const char* pkgid)
317 {
318         int i = 0;
319         char osp_share_pkgid_path[PATH_MAX] = {0, };
320         char osp_share2_pkgid_path[PATH_MAX] = {0, };
321         struct _path_info mount_info[] =
322         {
323                 { "\0", "./data/Share" },
324                 { "\0", "./data/Share2" },
325                 { "/opt/usr/share/.osp-compat/share",   "./Share" },
326                 { "/opt/usr/share/.osp-compat/share2",  "./Share2" },
327                 { "/opt/usr/media",                     "./Media" }
328         };
329
330         strncpy(osp_share_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
331         strncat(osp_share_pkgid_path, "share/", 6);
332         strncat(osp_share_pkgid_path, pkgid, strlen(pkgid));
333
334         strncpy(osp_share2_pkgid_path, _OSP_COMPAT_SHARED_PATH, strlen(_OSP_COMPAT_SHARED_PATH));
335         strncat(osp_share2_pkgid_path, "share2/", 7);
336         strncat(osp_share2_pkgid_path, pkgid, strlen(pkgid));
337
338         strncpy(mount_info[0].src_path, osp_share_pkgid_path, strlen(osp_share_pkgid_path));
339         strncpy(mount_info[1].src_path, osp_share2_pkgid_path, strlen(osp_share2_pkgid_path));
340
341         if (chdir(app_rootpath) != 0)
342         {
343                 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
344                 return -1;
345         }
346
347         for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
348         {
349                 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
350                 {
351                         LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
352                                         mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
353
354                         int j = 0;
355                         for (j = i; j > 0; --j)
356                         {
357                                 umount2(mount_info[j-1].dest_path, MNT_DETACH);
358                         }
359                         return -1;
360                 }
361         }
362
363         return 0;
364 }
365
366 static int
367 mount_linux_paths(const char* app_rootpath)
368 {
369         int i = 0;
370         static const struct _path_info mount_info[] =
371         {
372                 { "/bin",                               "./bin" },
373                 { "/cache",                             "./cache" },
374                 { "/csa",                               "./csa" },
375                 { "/data",                              "./data" },
376                 { "/dev",                               "./dev" },
377                 { "/dev/pts",                   "./dev/pts" },
378                 { "/dev/shm",                   "./dev/shm" },
379                 { "/etc",                               "./etc" },
380                 { "/lib",                               "./lib" },
381                 { "/lib/modules",               "./lib/modules" },
382                 { "/media",                             "./media" },
383                 { "/mnt",                               "./mnt" },
384                 { "/proc",                              "./proc" },
385                 { "/run",                               "./run" },
386                 { "/sbin",                              "./sbin" },
387                 { "/smack",                             "./smack" },
388                 { "/srv",                               "./srv" },
389                 { "/sys",                               "./sys" },
390                 { "/sys/fs/cgroup",             "./sys/fs/cgroup" },
391                 { "/sys/fs/cgroup/systemd",     "./sys/fs/cgroup/systemd" },
392                 { "/system",                    "./system" },
393                 { "/tmp",                               "./tmp" },
394                 { "/usr",                               "./usr" },
395                 { "/var",                               "./var" },
396                 { "/var/run",                   "./var/run" },
397                 { "/opt",                               "./opt" },
398                 { "/opt/usr",                   "./opt/usr" },
399                 //{ "/opt/var/run",             "./opt/var/run" },
400                 { "/opt/storage/sdcard","./opt/storage/sdcard" },
401         };
402
403         if (chdir(app_rootpath) != 0)
404         {
405                 LOGE("chdir() failed path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
406                 return -1;
407         }
408
409         for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); ++i)
410         {
411                 if (i == 27) // /opt/storage/sdcard
412                 {
413                         int mmc_mounted = 0;
414                         int ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
415                         if (ret < 0)
416                         {
417                                 LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
418                         }
419                         if (mmc_mounted == 0)
420                         {
421                                 continue;
422                         }
423                 }
424
425                 LOGI("src path: %s, dest path: %s", mount_info[i].src_path, mount_info[i].dest_path);
426                 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
427                 {
428                         LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
429                                         mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
430
431                         int j = 0;
432                         for (j = i; j > 0; --j)
433                         {
434                                 umount2(mount_info[j-1].dest_path, MNT_DETACH);
435                         }
436                         return -1;
437                 }
438         }
439
440         return 0;
441 }
442
443 static int
444 create_osp_external_paths(const char* app_rootpath, const char* pkgid)
445 {
446         char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
447         char osp_ext_apps_pkgid_share_path[PATH_MAX] = {0, };
448         char osp_ext_apps_pkgid_share2_path[PATH_MAX] = {0, };
449         char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
450         char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
451         struct _dir_info external_dirs[] =
452         {
453                 { "./HomeExt",          0000,   0 },
454                 { "./ShareExt",         0000,   0 },
455                 { "./Share2Ext",        0000,   0 },
456                 { "/opt/storage/sdcard/osp",            0777,   0 },
457                 { "/opt/storage/sdcard/osp/apps",       0777,   0 },
458                 { "/opt/storage/sdcard/osp/share",      0777,   0 },
459                 { "/opt/storage/sdcard/osp/share2",     0777,   0 },
460                 { "\0", 0777,   0},
461                 { "\0", 0777,   0},
462                 { "\0", 0777,   0},
463                 { "\0", 0777,   0},
464                 { "\0", 0777,   0},
465                 { "/opt/storage/sdcard/Images", 0777,   0 },
466                 { "/opt/storage/sdcard/Sounds", 0777,   0 },
467                 { "/opt/storage/sdcard/Videos", 0777,   0 },
468                 { "/opt/storage/sdcard/Others", 0777,   0 }
469         };
470         int i = 0;
471
472         strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
473         strncat(osp_ext_apps_pkgid_path, "apps/", 5);
474         strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
475
476         strncpy(osp_ext_apps_pkgid_share_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
477         strncat(osp_ext_apps_pkgid_share_path, "/Share", 6);
478
479         strncpy(osp_ext_apps_pkgid_share2_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
480         strncat(osp_ext_apps_pkgid_share2_path, "/Share2", 7);
481
482         strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
483         strncat(osp_ext_share_pkgid_path, "share/", 6);
484         strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
485
486         strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
487         strncat(osp_ext_share2_pkgid_path, "share2/", 7);
488         strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
489
490         strncpy(external_dirs[7].path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
491         strncpy(external_dirs[8].path, osp_ext_apps_pkgid_share_path, strlen(osp_ext_apps_pkgid_share_path));
492         strncpy(external_dirs[9].path, osp_ext_apps_pkgid_share2_path, strlen(osp_ext_apps_pkgid_share2_path));
493         strncpy(external_dirs[10].path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
494         strncpy(external_dirs[11].path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
495
496         if (chdir(app_rootpath) != 0)
497         {
498                 LOGE("chdir() failed (%s), path: %s", strerror(errno), app_rootpath);
499                 return -1;
500         }
501
502         for (i = 0; i < sizeof(external_dirs)/sizeof(struct _dir_info); i++)
503         {
504                 int ret = mkdir(external_dirs[i].path, external_dirs[i].mode);
505                 if (ret == -1 && errno != 17) // EEXIST
506                 {
507                         LOGE("mkdir() failed, path: %s, errno: %d (%s)", external_dirs[i].path, errno, strerror(errno));
508                         return -1;
509                 }
510         }
511
512         return 0;
513 }
514
515 static int
516 mount_osp_external_paths(const char* app_rootpath, const char* pkgid)
517 {
518         char osp_ext_apps_pkgid_path[PATH_MAX] = {0, };
519         char osp_ext_share_pkgid_path[PATH_MAX] = {0, };
520         char osp_ext_share2_pkgid_path[PATH_MAX] = {0, };
521         struct _path_info mount_info[] =
522         {
523                 { "/opt/storage/sdcard",                        "./Storagecard/Media"   },
524                 { "/opt/storage/sdcard/osp/share",      "./ShareExt"                    },
525                 { "/opt/storage/sdcard/osp/share2",     "./Share2Ext"                   },
526                 { "\0", "./HomeExt"                     },
527                 { "\0", "./HomeExt/Share"       },
528                 { "\0", "./HomeExt/Share2"      }
529         };
530         int i = 0;
531
532         strncpy(osp_ext_apps_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
533         strncat(osp_ext_apps_pkgid_path, "apps/", 5);
534         strncat(osp_ext_apps_pkgid_path, pkgid, strlen(pkgid));
535
536         strncpy(osp_ext_share_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
537         strncat(osp_ext_share_pkgid_path, "share/", 6);
538         strncat(osp_ext_share_pkgid_path, pkgid, strlen(pkgid));
539
540         strncpy(osp_ext_share2_pkgid_path, _EXT_OSP_HOME_PATH, strlen(_EXT_OSP_HOME_PATH));
541         strncat(osp_ext_share2_pkgid_path, "share2/", 7);
542         strncat(osp_ext_share2_pkgid_path, pkgid, strlen(pkgid));
543
544         strncpy(mount_info[3].src_path, osp_ext_apps_pkgid_path, strlen(osp_ext_apps_pkgid_path));
545         strncpy(mount_info[4].src_path, osp_ext_share_pkgid_path, strlen(osp_ext_share_pkgid_path));
546         strncpy(mount_info[5].src_path, osp_ext_share2_pkgid_path, strlen(osp_ext_share2_pkgid_path));
547
548         if (chdir(app_rootpath) != 0)
549         {
550                 LOGE("chdir() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
551                 return -1;
552         }
553         LOGI("app_rootpath: %s", app_rootpath);
554
555         for (i = 0; i < sizeof(mount_info)/sizeof(struct _path_info); i++)
556         {
557                 if (mount(mount_info[i].src_path, mount_info[i].dest_path, NULL, MS_BIND, NULL) != 0)
558                 {
559                         LOGE("mount() failed, src path: %s, dest path: %s, errno: %d (%s)",
560                                         mount_info[i].src_path, mount_info[i].dest_path, errno, strerror(errno));
561
562                         int j = 0;
563                         for (j = i; j > 0; --j)
564                         {
565                                 umount2(mount_info[j-1].dest_path, MNT_DETACH);
566                         }
567                         return -1;
568                 }
569         }
570
571         return 0;
572 }
573
574 int
575 do_pre_exe(const char* bin_path, const char* package_id)
576 {
577         char* app_rootpath = NULL;
578         int mmc_mounted = 0;
579
580         // preload libosp-compat.so.1 for compatibility
581         void* handle = dlopen(OSP_COMPAT_LIB, RTLD_GLOBAL);
582         if (handle)
583         {
584                 LOGI("%s is preloaded.", OSP_COMPAT_LIB);
585         }
586
587         /* e.g., app_rootdir is "/opt/usr/apps/[pkgId] */
588         app_rootpath = get_app_rootpath_from_path(bin_path);
589
590         _SECURE_LOGI("[data_caging] do_pre_exe() was called, package id: %s, binary path: %s, app root: %s",
591                         package_id, bin_path, app_rootpath);
592
593         umask(0000);
594
595         if (!internal_is_mounted(package_id))
596         {
597                 if (mount_native_paths(app_rootpath) != 0)
598                 {
599                         goto ERROR;
600                 }
601                 if (mount_osp_internal_paths(app_rootpath, package_id) != 0)
602                 {
603                         goto ERROR;
604                 }
605         }
606
607         int ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mounted);
608         if (ret < 0)
609         {
610                 LOGE("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
611         }
612         if (mmc_mounted == 1)
613         {
614                 LOGI("MMC is mounted.");
615                 if (create_osp_external_paths(app_rootpath, package_id) != 0)
616                 {
617                         goto ERROR;
618                 }
619
620                 if (!external_is_mounted(package_id))
621                 {
622                         if (mount_osp_external_paths(app_rootpath, package_id) != 0)
623                         {
624                                 goto ERROR;
625                         }
626                 }
627         }
628         LOGI("mount() succeeded.");
629
630         if (chroot(app_rootpath) != 0)
631         {
632                 LOGE("chroot() failed, path: %s, errno: %d (%s)", app_rootpath, errno, strerror(errno));
633                 goto ERROR;
634         }
635         if (chdir("/") != 0)
636         {
637                 LOGE("chdir() failed, path: /, errno: %d (%s)", errno, strerror(errno));
638                 goto ERROR;
639         }
640         LOGI("chroot() succeeded.");
641
642         if (chdir("/data") != 0)
643         {
644                 LOGE("chdir() failed, path: /data, errno: %d (%s)", errno, strerror(errno));
645                 goto ERROR;
646         }
647
648         free(app_rootpath);
649         umask(0022);
650
651         LOGI("[data_caging] do_pre_exe() succeeded.");
652         return 0;
653
654 ERROR:
655         free(app_rootpath);
656         umask(0022);
657
658         LOGI("[data_caging] do_pre_exe() failed.");
659         return -1;
660 }
661
662 static int
663 do_virtual_root(const char* virtual_root_path, const char* package_id)
664 {
665         _SECURE_LOGI("[virtual_root] do_virtual_root() was called, virtual root path: %s, package id: %s",
666                         virtual_root_path, package_id);
667
668         umask(0000);
669
670         if (!internal_is_mounted(package_id))
671         {
672                 if (mount_linux_paths(virtual_root_path) != 0)
673                 {
674                         goto ERROR;
675                 }
676         }
677         LOGI("mount() succeeded.");
678
679         if (chroot(virtual_root_path) != 0)
680         {
681                 LOGE("chroot() failed. path: %s, errno: %d (%s)", virtual_root_path, errno, strerror(errno));
682                 goto ERROR;
683         }
684         if (chdir("/") != 0)
685         {
686                 LOGE("chdir() failed. path: /data, errno: %d (%s)", errno, strerror(errno));
687                 goto ERROR;
688         }
689         LOGI("chroot() succeeded.");
690
691         umask(0022);
692
693         LOGI("[virtual_root] do_virtual_root() succeeded.");
694         return 0;
695
696 ERROR:
697         umask(0022);
698
699         LOGI("[virtual_root] do_virtual_root() failed.");
700         return -1;
701 }
702
703 int
704 do_pre_exec(const char* package_name, const char* bin_path)
705 {
706         char* app_rootpath = NULL;
707         char app_compat_path[PATH_MAX] = { 0, };
708         const char app_compat_file[] = "/info/compat.info\0";
709         char package_id[_MAX_PACKAGEID_LENGTH + 1] = { 0, };
710         char osp_app_data_path[PATH_MAX] = { 0, };
711         int osp_compat = 0;
712
713         _SECURE_LOGI("do_pre_exec() is called, app name: %s, binary path: %s", package_name, bin_path);
714
715         app_rootpath = get_app_rootpath_from_path(bin_path);
716
717         strncpy(app_compat_path, app_rootpath, strlen(app_rootpath));
718         strncat(app_compat_path, app_compat_file, strlen(app_compat_file));
719         if (access(app_compat_path, F_OK) == 0)
720         {
721                 osp_compat = 1;
722         }
723
724         get_package_id_from_app_rootpath(app_rootpath, package_id);
725
726         _SECURE_LOGI("package id: %s, binary path: %s, OSP compat: %d", package_id, bin_path, osp_compat);
727
728         // FIXME: Temporary code with security risk
729         prctl(PR_SET_KEEPCAPS, 1);
730
731         if (osp_compat == 1)
732         {
733                 free(app_rootpath);
734                 //unshare(CLONE_NEWNS);
735                 return do_pre_exe(bin_path, package_id);
736         }
737
738         char virtual_root_file[PATH_MAX] = { 0, };
739         const char virtual_root_info[] = "/info/virtualroot.info";
740         strncpy(virtual_root_file, app_rootpath, strlen(app_rootpath));
741         strncat(virtual_root_file, virtual_root_info, strlen(virtual_root_info));
742         if (access(virtual_root_file, F_OK) == 0)
743         {
744                 LOGI("This is virtual root application.");
745                 int fd = open(virtual_root_file, O_RDONLY);
746                 if (fd < 0)
747                 {
748                         LOGE("failed to open %s, errno: %d (%s)", virtual_root_file, errno, strerror(errno));
749                         goto ERROR;
750                 }
751
752                 char user_path[PATH_MAX] = { 0, };
753                 int read_bytes = read(fd, user_path, PATH_MAX);
754                 if (read_bytes < 0)
755                 {
756                         LOGE("failed to read %s, errno: %d (%s)", virtual_root_file, errno, strerror(errno));
757                         close(fd);
758                         goto ERROR;
759                 }
760                 close(fd);
761
762                 char virtual_root_path[PATH_MAX] = { 0, };
763                 sprintf(virtual_root_path, "%s/data/%s", app_rootpath, user_path);
764
765                 int res = do_virtual_root(virtual_root_path, package_id);
766                 free(app_rootpath);
767                 return res;
768         }
769
770         // API version is equal to or greater than Tizen 2.0
771         // Set current working dir to "/opt/apps/{pkgId}/data"
772         strncpy(osp_app_data_path, app_rootpath, strlen(app_rootpath));
773         strncat(osp_app_data_path, "/data", strlen("/data"));
774
775         if (chdir(osp_app_data_path) != 0)
776         {
777                 LOGE("chdir() failed, path: %s, errno: %d (%s)", osp_app_data_path, errno, strerror(errno));
778                 goto ERROR;
779         }
780
781         LOGI("[data_caging] do_pre_exec() succeeded.");
782         free(app_rootpath);
783         return 0;
784
785 ERROR:
786         free(app_rootpath);
787         return -1;
788 }