248b947b66faaef2a1945f7444b30f1e78f4a4fe
[platform/core/appfw/launchpad.git] / src / lib / launchpad / step_prepare_execution.cc
1 /*
2  * Copyright (c) 2023 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 #include "launchpad/step_prepare_execution.hh"
18
19 #include <aul.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
22 #include <buxton2.h>
23 #include <security-manager.h>
24 #include <sys/prctl.h>
25 #include <sys/types.h>
26 #include <trust-anchor.h>
27 #include <unistd.h>
28 #include <vconf.h>
29
30 #include <app_info.hh>
31 #include <aul_keys.hh>
32 #include <stdio.hh>
33 #include <util.hh>
34
35 #include "launchpad/log_private.hh"
36 #include "launchpad/thread_control.hh"
37
38 namespace launchpad {
39
40 StepPrepareExecution::StepPrepareExecution() {
41   steps_ = {
42     std::bind(&StepPrepareExecution::EnableExternalPackage, this,
43         std::placeholders::_1),
44     std::bind(&StepPrepareExecution::TrustAnchorLaunch, this,
45         std::placeholders::_1),
46     std::bind(&StepPrepareExecution::MountResourceDirectories, this,
47         std::placeholders::_1),
48     std::bind(&StepPrepareExecution::SecurityManagerPrepareApp, this,
49         std::placeholders::_1),
50     std::bind(&StepPrepareExecution::SetupStdio, this,
51         std::placeholders::_1),
52     std::bind(&StepPrepareExecution::BuxtonUpdateClientLabel, this,
53         std::placeholders::_1),
54     std::bind(&StepPrepareExecution::SetDumpable, this,
55         std::placeholders::_1),
56     std::bind(&StepPrepareExecution::SetProcessName, this,
57         std::placeholders::_1),
58     std::bind(&StepPrepareExecution::WaitTepMount, this,
59         std::placeholders::_1),
60     std::bind(&StepPrepareExecution::PrepareAppSocket, this,
61         std::placeholders::_1),
62     std::bind(&StepPrepareExecution::PrepareIdFile, this,
63         std::placeholders::_1),
64   };
65 }
66
67 int StepPrepareExecution::Prepare(AppInfo* app_info) {
68   for (auto& step : steps_) {
69     if (step(app_info) != 0)
70       return -1;
71   }
72
73   return 0;
74 }
75
76 int StepPrepareExecution::EnableExternalPackage(AppInfo* app_info) {
77   int ret = Util::EnableExternalPackage(app_info);
78   if (ret < 0) {
79     _E("Failed to enable external package. error: %d", ret);
80     return -1;
81   }
82
83   return 0;
84 }
85
86 int StepPrepareExecution::TrustAnchorLaunch(AppInfo* app_info) {
87   int ret = trust_anchor_launch(app_info->GetPkgId().c_str(),
88       app_info->IsGlobal() ? GLOBAL_USER : getuid());
89   if (ret != TRUST_ANCHOR_ERROR_NONE &&
90       ret != TRUST_ANCHOR_ERROR_NOT_INSTALLED) {
91     _E("trust_anchor_launch() is failed. error: %d", ret);
92     return -1;
93   }
94
95   return 0;
96 }
97
98 int StepPrepareExecution::MountResourceDirectories(AppInfo* app_info) {
99   int ret = Util::MountResourceDirectories(app_info);
100   if (ret < 0) {
101     _E("Failed to mount resource direstories. error: %d", ret);
102     return -1;
103   }
104
105   return 0;
106 }
107
108 int StepPrepareExecution::SecurityManagerPrepareApp(AppInfo* app_info) {
109   auto* enabled_light_user = bundle_get_val(app_info->GetBundle().GetHandle(),
110       kAulEnabledLightUser);
111   _W("security_manager_prepare_app2() ++ %s", app_info->GetAppId().c_str());
112   int ret = security_manager_prepare_app2(app_info->GetAppId().c_str(),
113       enabled_light_user);
114   _W("security_manager_prepare_app2() -- %s", app_info->GetAppId().c_str());
115   if (ret != SECURITY_MANAGER_SUCCESS) {
116     _E("security_manager_prepare_app2() is failed. appid: %s, error: %d",
117         app_info->GetAppId().c_str(), ret);
118     return -1;
119   }
120
121   return 0;
122 }
123
124 int StepPrepareExecution::SetupStdio(AppInfo* app_info) {
125   Stdio::Setup();
126   return 0;
127 }
128
129 int StepPrepareExecution::BuxtonUpdateClientLabel(AppInfo* app_info) {
130   struct buxton_client* client = nullptr;
131   int ret = buxton_open(&client, nullptr, nullptr);
132   if (ret != 0) {
133     _E("buxton_open() is failed. errno: %d", errno);
134     return -1;
135   }
136
137   ret = buxton_update_client_label_sync(client);
138   buxton_close(client);
139   if (ret != 0) {
140     _E("buxton_update_client_label_sync() is failed. errno: %d", errno);
141     return -1;
142   }
143
144   return 0;
145 }
146
147 int StepPrepareExecution::SetDumpable(AppInfo* app_info) {
148   prctl(PR_SET_DUMPABLE, 1);
149   return 0;
150 }
151
152 int StepPrepareExecution::SetProcessName(AppInfo* app_info) {
153   char* name = basename(const_cast<char*>(app_info->GetAppPath().c_str()));
154   char process_name[16] = { 0, };
155   snprintf(process_name, sizeof(process_name), "%s", name);
156   prctl(PR_SET_NAME, process_name);
157   return 0;
158 }
159
160 int StepPrepareExecution::WaitTepMount(AppInfo* app_info) {
161   int ret = Util::WaitTepMount(app_info);
162   if (ret < 0) {
163     _E("Failed to wait tep mount. error: %d", ret);
164     return -1;
165   }
166
167   return 0;
168 }
169
170 int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) {
171   int ret = Util::PrepareAppSocket();
172   if (ret < 0) {
173     _E("Failed to prepare app socket. error: %d", ret);
174     return -1;
175   }
176
177   return 0;
178 }
179
180 int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) {
181   int ret = Util::PrepareAppIdFile(app_info);
182   if (ret < 0) {
183     _E("Failed to prepare id file. error: %d", ret);
184     return -1;
185   }
186
187   return 0;
188 }
189
190 }  // namespace launchpad