2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "launchpad/step_prepare_execution.hh"
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
23 #include <security-manager.h>
24 #include <sys/prctl.h>
25 #include <sys/types.h>
26 #include <trust-anchor.h>
30 #include <app_info.hh>
31 #include <aul_keys.hh>
35 #include "launchpad/log_private.hh"
36 #include "launchpad/thread_control.hh"
40 StepPrepareExecution::StepPrepareExecution() {
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),
69 int StepPrepareExecution::Prepare(AppInfo* app_info) {
70 for (auto& step : steps_) {
71 if (step(app_info) != 0)
78 int StepPrepareExecution::EnableExternalPackage(AppInfo* app_info) {
79 int ret = Util::EnableExternalPackage(app_info);
81 _E("Failed to enable external package. error: %d", ret);
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);
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");
108 int ret = Util::MountResourceDirectories(app_info);
110 _E("Failed to mount resource direstories. error: %d", ret);
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(),
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);
133 int StepPrepareExecution::SetupStdio(AppInfo* app_info) {
138 int StepPrepareExecution::BuxtonUpdateClientLabel(AppInfo* app_info) {
139 struct buxton_client* client = nullptr;
140 int ret = buxton_open(&client, nullptr, nullptr);
142 _E("buxton_open() is failed. errno: %d", errno);
146 ret = buxton_update_client_label_sync(client);
147 buxton_close(client);
149 _E("buxton_update_client_label_sync() is failed. errno: %d", errno);
156 int StepPrepareExecution::SetDumpable(AppInfo* app_info) {
157 prctl(PR_SET_DUMPABLE, 1);
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);
169 int StepPrepareExecution::WaitTepMount(AppInfo* app_info) {
170 int ret = Util::WaitTepMount(app_info);
172 _E("Failed to wait tep mount. error: %d", ret);
179 int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) {
180 int ret = Util::PrepareAppSocket();
182 _E("Failed to prepare app socket. error: %d", ret);
189 int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) {
190 int ret = Util::PrepareAppIdFile(app_info);
192 _E("Failed to prepare id file. error: %d", ret);
199 int StepPrepareExecution::SendStartupSignal(AppInfo* app_info) {
200 if (Util::SendCmdToAmd(AmdCmd::AppStartupSignal) != 0)
201 _W("Failed to send startup signal");
206 } // namespace launchpad