tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / wrt-launchpad-daemon / include / process_pool_launchpad_util.h
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    process_pool_launchpad_util.h
18  * @author  Tae-Jeong Lee (taejeong.lee@samsung.com)
19  * @version 0.1
20  * @brief   Api library to support launchpad operation.
21  */
22
23 #ifndef __PROCESS_POOL_LAUNCHPAD_UTIL_H_
24 #define __PROCESS_POOL_LAUNCHPAD_UTIL_H_
25
26 #include <stdlib.h>
27 #include <launchpad_util.h>
28 #include <smack_labeling_support.h>
29 #include <execute_on_whole_thread_util.h>
30
31 // Prototype
32 _static_ int    __process_pool_prepare_exec(const char *pkg_name, const char *app_path, app_info_from_db * menu_info, bundle * kb);
33 _static_ void   process_pool_launchpad_main_loop(app_pkt_t* pkt, char* out_app_path, int* out_argc, char ***out_argv);
34
35 // Implementation
36 _static_ int __process_pool_prepare_exec(const char *pkg_name,
37                             const char *app_path, app_info_from_db * menu_info,
38                             bundle * kb)
39 {
40     const char *file_name;
41     char process_name[WRT_AUL_PR_NAME];
42
43     /* SET PRIVILEGES*/
44     char pkg_id[PKG_ID_LENGTH];
45     memset(pkg_id, '\0', PKG_ID_LENGTH);
46     snprintf(pkg_id, PKG_ID_LENGTH, "%s", pkg_name);
47
48     if (set_app_smack_label(app_path) != 0)
49     {
50         WrtLogE("set_app_smack_label() failed");
51     }
52
53     if (AccessControl::setPriviledge(pkg_id, menu_info->pkg_type, app_path) < 0) {
54         WrtLogD("fail to set privileges - check your package's credential\n");
55         return -1;
56     }
57
58     /* SET INHERIT BIT FOR CAP_MAC_ADMIN TO WHOLE THREAD */
59     EXECUTE_ON_WHOLE_THREAD(__set_inherit_bit_for_CAP_MAC_ADMIN, SIGUSR1);
60
61     /* SET PROCESS NAME*/
62     if (app_path == NULL) {
63         WrtLogD("app_path should not be NULL - check menu db");
64         return -1;
65     }
66     file_name = strrchr(app_path, '/');
67     if (file_name == NULL) {
68         WrtLogD("can't locate file name to execute");
69         return -1;
70     }
71
72     memset(process_name, '\0', WRT_AUL_PR_NAME);
73     snprintf(process_name, WRT_AUL_PR_NAME, "%s", file_name + 1);
74     prctl(PR_SET_NAME, process_name);
75
76     /* SET ENVIROMENT*/
77     __set_env(menu_info, kb);
78
79     return 0;
80 }
81
82
83 static bundle *_s_bundle = NULL;
84 static void __at_exit_to_release_bundle()
85 {
86     if (_s_bundle) {
87         bundle_free(_s_bundle);
88         _s_bundle = NULL;
89     }
90 }
91
92 _static_ void process_pool_launchpad_main_loop(app_pkt_t* pkt, char* out_app_path, int* out_argc, char ***out_argv)
93
94 {
95     bundle *kb = NULL;
96     app_info_from_db *menu_info = NULL;
97
98     const char *pkg_name = NULL;
99     const char *app_path = NULL;
100
101     kb = bundle_decode(pkt->data, pkt->len);
102     if (!kb) {
103         WrtLogE("bundle decode error");
104         exit(-1);
105     }
106
107     if (_s_bundle != NULL) {
108         bundle_free(_s_bundle);
109     }
110     _s_bundle = kb;
111     atexit(__at_exit_to_release_bundle);
112
113     pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
114     SECURE_LOGD("pkg name : %s", pkg_name);
115
116     menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
117     if (menu_info == NULL) {
118         WrtLogD("such pkg no found");
119         exit(-1);
120     }
121
122     app_path = _get_app_path(menu_info);
123
124     if (app_path == NULL) {
125         WrtLogE("app_path is NULL");
126         exit(-1);
127     }
128
129     if (app_path[0] != '/') {
130         WrtLogE("app_path is not absolute path");
131         exit(-1);
132     }
133
134     __modify_bundle(kb, /*cr.pid - unused parameter*/ 0, menu_info, pkt->cmd);
135     pkg_name = _get_pkgname(menu_info);
136     SECURE_LOGD("pkg name : %s", pkg_name);
137
138     __process_pool_prepare_exec(pkg_name, app_path, menu_info, kb);
139
140     if (out_app_path != NULL && out_argc != NULL && out_argv != NULL)
141     {
142         int i;
143
144         sprintf(out_app_path, "%s", app_path);
145
146         *out_argv = __create_argc_argv(kb, out_argc);
147         (*out_argv)[0] = out_app_path;
148
149         for (i = 0; i < *out_argc; i++)
150         {
151             WrtLogD("input argument %d : %s##", i, (*out_argv)[i]);
152         }
153     }
154     else
155     {
156         exit(-1);
157     }
158
159     if (menu_info != NULL) {
160         _free_app_info_from_db(menu_info);
161     }
162 }
163
164 #endif // __PROCESS_POOL_LAUNCHPAD_UTIL_H_