Add to mount lib directory for rpk
[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     std::bind(&StepPrepareExecution::SendStartupSignal, this,
65         std::placeholders::_1),
66   };
67 }
68
69 int StepPrepareExecution::Prepare(AppInfo* app_info) {
70   for (auto& step : steps_) {
71     if (step(app_info) != 0)
72       return -1;
73   }
74
75   return 0;
76 }
77
78 int StepPrepareExecution::EnableExternalPackage(AppInfo* app_info) {
79   int ret = Util::EnableExternalPackage(app_info);
80   if (ret < 0) {
81     _E("Failed to enable external package. error: %d", ret);
82     return -1;
83   }
84
85   return 0;
86 }
87
88 int StepPrepareExecution::TrustAnchorLaunch(AppInfo* app_info) {
89   int ret = trust_anchor_launch(app_info->GetPkgId().c_str(),
90       app_info->IsGlobal() ? GLOBAL_USER : getuid());
91   if (ret != TRUST_ANCHOR_ERROR_NONE &&
92       ret != TRUST_ANCHOR_ERROR_NOT_INSTALLED) {
93     _E("trust_anchor_launch() is failed. error: %d", ret);
94     return -1;
95   }
96
97   return 0;
98 }
99
100 int StepPrepareExecution::MountResourceDirectories(AppInfo* app_info) {
101   if (getenv("LOADER_MOUNT") == nullptr) {
102     if (Util::MountGadgetDirectories(app_info->GetBundle()) != 0) {
103       _E("Failed to mount gadget resources");
104       return -1;
105     }
106
107     if (Util::MountLibraryDirectories(app_info->GetBundle()) != 0) {
108       _E("Failed to mount library direstories");
109       return -1;
110     }
111   }
112
113   int ret = Util::MountResourceDirectories(app_info);
114   if (ret < 0) {
115     _E("Failed to mount resource direstories. error: %d", ret);
116     return -1;
117   }
118
119   return 0;
120 }
121
122 int StepPrepareExecution::SecurityManagerPrepareApp(AppInfo* app_info) {
123   auto* enabled_light_user = bundle_get_val(app_info->GetBundle().GetHandle(),
124       kAulEnabledLightUser);
125   _W("security_manager_prepare_app2() ++ %s", app_info->GetAppId().c_str());
126   int ret = security_manager_prepare_app2(app_info->GetAppId().c_str(),
127       enabled_light_user);
128   _W("security_manager_prepare_app2() -- %s", app_info->GetAppId().c_str());
129   if (ret != SECURITY_MANAGER_SUCCESS) {
130     _E("security_manager_prepare_app2() is failed. appid: %s, error: %d",
131         app_info->GetAppId().c_str(), ret);
132     return -1;
133   }
134
135   return 0;
136 }
137
138 int StepPrepareExecution::SetupStdio(AppInfo* app_info) {
139   Stdio::Setup();
140   return 0;
141 }
142
143 int StepPrepareExecution::BuxtonUpdateClientLabel(AppInfo* app_info) {
144   struct buxton_client* client = nullptr;
145   int ret = buxton_open(&client, nullptr, nullptr);
146   if (ret != 0) {
147     _E("buxton_open() is failed. errno: %d", errno);
148     return -1;
149   }
150
151   ret = buxton_update_client_label_sync(client);
152   buxton_close(client);
153   if (ret != 0) {
154     _E("buxton_update_client_label_sync() is failed. errno: %d", errno);
155     return -1;
156   }
157
158   return 0;
159 }
160
161 int StepPrepareExecution::SetDumpable(AppInfo* app_info) {
162   prctl(PR_SET_DUMPABLE, 1);
163   return 0;
164 }
165
166 int StepPrepareExecution::SetProcessName(AppInfo* app_info) {
167   char* name = basename(const_cast<char*>(app_info->GetAppPath().c_str()));
168   char process_name[16] = { 0, };
169   snprintf(process_name, sizeof(process_name), "%s", name);
170   prctl(PR_SET_NAME, process_name);
171   return 0;
172 }
173
174 int StepPrepareExecution::WaitTepMount(AppInfo* app_info) {
175   int ret = Util::WaitTepMount(app_info);
176   if (ret < 0) {
177     _E("Failed to wait tep mount. error: %d", ret);
178     return -1;
179   }
180
181   return 0;
182 }
183
184 int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) {
185   int ret = Util::PrepareAppSocket();
186   if (ret < 0) {
187     _E("Failed to prepare app socket. error: %d", ret);
188     return -1;
189   }
190
191   return 0;
192 }
193
194 int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) {
195   int ret = Util::PrepareAppIdFile(app_info);
196   if (ret < 0) {
197     _E("Failed to prepare id file. error: %d", ret);
198     return -1;
199   }
200
201   return 0;
202 }
203
204 int StepPrepareExecution::SendStartupSignal(AppInfo* app_info) {
205   if (Util::SendCmdToAmd(AmdCmd::AppStartupSignal) != 0)
206     _W("Failed to send startup signal");
207
208   return 0;
209 }
210
211 }  // namespace launchpad