Mount gadget resource paths for NUIGadget
[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
108   int ret = Util::MountResourceDirectories(app_info);
109   if (ret < 0) {
110     _E("Failed to mount resource direstories. error: %d", ret);
111     return -1;
112   }
113
114   return 0;
115 }
116
117 int StepPrepareExecution::SecurityManagerPrepareApp(AppInfo* app_info) {
118   auto* enabled_light_user = bundle_get_val(app_info->GetBundle().GetHandle(),
119       kAulEnabledLightUser);
120   _W("security_manager_prepare_app2() ++ %s", app_info->GetAppId().c_str());
121   int ret = security_manager_prepare_app2(app_info->GetAppId().c_str(),
122       enabled_light_user);
123   _W("security_manager_prepare_app2() -- %s", app_info->GetAppId().c_str());
124   if (ret != SECURITY_MANAGER_SUCCESS) {
125     _E("security_manager_prepare_app2() is failed. appid: %s, error: %d",
126         app_info->GetAppId().c_str(), ret);
127     return -1;
128   }
129
130   return 0;
131 }
132
133 int StepPrepareExecution::SetupStdio(AppInfo* app_info) {
134   Stdio::Setup();
135   return 0;
136 }
137
138 int StepPrepareExecution::BuxtonUpdateClientLabel(AppInfo* app_info) {
139   struct buxton_client* client = nullptr;
140   int ret = buxton_open(&client, nullptr, nullptr);
141   if (ret != 0) {
142     _E("buxton_open() is failed. errno: %d", errno);
143     return -1;
144   }
145
146   ret = buxton_update_client_label_sync(client);
147   buxton_close(client);
148   if (ret != 0) {
149     _E("buxton_update_client_label_sync() is failed. errno: %d", errno);
150     return -1;
151   }
152
153   return 0;
154 }
155
156 int StepPrepareExecution::SetDumpable(AppInfo* app_info) {
157   prctl(PR_SET_DUMPABLE, 1);
158   return 0;
159 }
160
161 int StepPrepareExecution::SetProcessName(AppInfo* app_info) {
162   char* name = basename(const_cast<char*>(app_info->GetAppPath().c_str()));
163   char process_name[16] = { 0, };
164   snprintf(process_name, sizeof(process_name), "%s", name);
165   prctl(PR_SET_NAME, process_name);
166   return 0;
167 }
168
169 int StepPrepareExecution::WaitTepMount(AppInfo* app_info) {
170   int ret = Util::WaitTepMount(app_info);
171   if (ret < 0) {
172     _E("Failed to wait tep mount. error: %d", ret);
173     return -1;
174   }
175
176   return 0;
177 }
178
179 int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) {
180   int ret = Util::PrepareAppSocket();
181   if (ret < 0) {
182     _E("Failed to prepare app socket. error: %d", ret);
183     return -1;
184   }
185
186   return 0;
187 }
188
189 int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) {
190   int ret = Util::PrepareAppIdFile(app_info);
191   if (ret < 0) {
192     _E("Failed to prepare id file. error: %d", ret);
193     return -1;
194   }
195
196   return 0;
197 }
198
199 int StepPrepareExecution::SendStartupSignal(AppInfo* app_info) {
200   if (Util::SendCmdToAmd(AmdCmd::AppStartupSignal) != 0)
201     _W("Failed to send startup signal");
202
203   return 0;
204 }
205
206 }  // namespace launchpad