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