set context in updater mode

Signed-off-by: steven_q <qichanggui@huawei.com>
Change-Id: I8cb4cc6672a9c018ee3c0a6e17dae2fc4f8743cf
This commit is contained in:
steven_q 2023-02-25 12:17:54 +00:00
parent ff134e0a37
commit 53a77eca03
4 changed files with 162 additions and 76 deletions

189
BUILD.gn
View File

@ -62,91 +62,129 @@ config("hdc_config") {
} }
} }
ohos_executable("hdcd") { template("hdcd_source_set") {
use_exceptions = true forward_variables_from(invoker, "*")
sources = [
"src/daemon/daemon.cpp",
"src/daemon/daemon_app.cpp",
"src/daemon/daemon_forward.cpp",
"src/daemon/daemon_tcp.cpp",
"src/daemon/daemon_unity.cpp",
"src/daemon/daemon_usb.cpp",
"src/daemon/jdwp.cpp",
"src/daemon/main.cpp",
"src/daemon/shell.cpp",
"src/daemon/system_depend.cpp",
]
sources += hdc_common_sources
defines = [ ohos_source_set(target_name) {
"HARMONY_PROJECT", use_exceptions = true
"USE_CONFIG_UV_THREADS", sources = [
"SIZE_THREAD_POOL=$hdcd_uv_thread_size", "src/daemon/daemon.cpp",
] "src/daemon/daemon_app.cpp",
"src/daemon/daemon_forward.cpp",
"src/daemon/daemon_tcp.cpp",
"src/daemon/daemon_unity.cpp",
"src/daemon/daemon_usb.cpp",
"src/daemon/jdwp.cpp",
"src/daemon/main.cpp",
"src/daemon/shell.cpp",
"src/daemon/system_depend.cpp",
]
sources += hdc_common_sources
if (hdc_debug) { defines = [
defines += [ "HDC_DEBUG" ] "HARMONY_PROJECT",
"USE_CONFIG_UV_THREADS",
"SIZE_THREAD_POOL=$hdcd_uv_thread_size",
]
if (hdc_debug) {
defines += [ "HDC_DEBUG" ]
}
if (hdc_support_uart) {
defines += [ "HDC_SUPPORT_UART" ]
sources += [ "src/daemon/daemon_uart.cpp" ]
}
if (js_jdwp_connect) {
defines += [ "JS_JDWP_CONNECT" ]
}
if (build_variant == "user") {
defines += [ "HDC_BUILD_VARIANT_USER" ]
}
configs = [ ":hdc_config" ]
deps = [
"//third_party/libuv:uv",
"//third_party/lz4:liblz4_static",
"//third_party/openssl:libcrypto_shared",
]
deps += [
":hdc_hash_gen",
"${HDC_PATH}/src/daemon/etc:daemon_etc",
]
if (hdc_jdwp_test) {
defines += [ "SIMULATE_JDWP" ]
deps += [ "${HDC_PATH}/src/test/jdwp:jdwp_test" ]
}
external_deps = [
"c_utils:utilsbase",
"init:libbegetutil",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//third_party/lz4/lib",
"//third_party/openssl/include",
"//third_party/libuv",
]
if (build_selinux) {
deps += [ "//third_party/selinux:libselinux" ]
include_dirs += [ "//third_party/selinux/libselinux/include" ]
defines += [ "SURPPORT_SELINUX" ]
if (image_name == "updater") {
defines += [ "UPDATER_MODE" ]
}
}
if (hdc_version_check) {
defines += [ "HDC_VERSION_CHECK" ]
}
ldflags = [ "-rdynamic" ]
} }
if (hdc_support_uart) { }
defines += [ "HDC_SUPPORT_UART" ]
sources += [ "src/daemon/daemon_uart.cpp" ]
}
if (js_jdwp_connect) {
defines += [ "JS_JDWP_CONNECT" ]
}
if (build_variant == "user") {
defines += [ "HDC_BUILD_VARIANT_USER" ]
}
configs = [ ":hdc_config" ]
deps = [ template("build_hdc") {
"//third_party/libuv:uv", forward_variables_from(invoker, "*")
"//third_party/lz4:liblz4_static", image_name = target_name
"//third_party/openssl:libcrypto_shared", hdcd_source_set("hdcd_${image_name}_source") {
] if (build_selinux) {
image_name = image_name
deps += [ }
":hdc_hash_gen",
"${HDC_PATH}/src/daemon/etc:daemon_etc",
]
if (hdc_jdwp_test) {
defines += [ "SIMULATE_JDWP" ]
deps += [ "${HDC_PATH}/src/test/jdwp:jdwp_test" ]
} }
external_deps = [ ohos_executable("hdcd_${image_name}_exe") {
"c_utils:utilsbase", deps = [ ":hdcd_${image_name}_source" ]
"init:libbegetutil", output_name = "hdcd_${image_name}"
] install_enable = false
subsystem_name = "developtools"
include_dirs = [ part_name = "hdc"
"//third_party/bounds_checking_function/include",
"//third_party/lz4/lib",
"//third_party/openssl/include",
"//third_party/libuv",
]
if (build_selinux) {
deps += [ "//third_party/selinux:libselinux" ]
include_dirs += [ "//third_party/selinux/libselinux/include" ]
defines += [ "SURPPORT_SELINUX" ]
} }
if (hdc_version_check) { ohos_prebuilt_executable("hdcd_${image_name}") {
defines += [ "HDC_VERSION_CHECK" ] deps = [ ":hdcd_${image_name}_exe" ]
output = "${image_name}/hdcd"
install_enable = true
subsystem_name = "developtools"
part_name = "hdc"
source = "${root_out_dir}/${subsystem_name}/${part_name}/hdcd_${image_name}"
module_install_dir = "bin"
install_images = []
if (image_name == "updater") {
install_images = [ "updater" ]
} else {
install_images = [ "system" ]
}
} }
}
install_images = [ build_hdc("system") {
"system", }
"updater",
]
ldflags = [ "-rdynamic" ] build_hdc("updater") {
install_enable = true
subsystem_name = "developtools"
part_name = "hdc"
} }
ohos_executable("hdc") { ohos_executable("hdc") {
@ -247,7 +285,8 @@ ohos_executable("hdc") {
group("hdc_target") { group("hdc_target") {
deps = [ deps = [
":hdc", ":hdc",
":hdcd", ":hdcd_system",
":hdcd_updater",
] ]
} }

View File

@ -33,7 +33,8 @@
}, },
"build": { "build": {
"sub_component": [ "sub_component": [
"//developtools/hdc:hdcd" "//developtools/hdc:hdcd_system",
"//developtools/hdc:hdcd_updater"
] ]
} }
} }

View File

@ -13,6 +13,11 @@
* limitations under the License. * limitations under the License.
*/ */
#include "async_cmd.h" #include "async_cmd.h"
#if !defined(_WIN32) && !defined(HDC_HOST)
#if defined(SURPPORT_SELINUX) && defined(UPDATER_MODE)
#include "selinux/selinux.h"
#endif
#endif
namespace Hdc { namespace Hdc {
// Do not add thread-specific init op in the following methods as it's running in child thread. // Do not add thread-specific init op in the following methods as it's running in child thread.
@ -89,6 +94,24 @@ bool AsyncCmd::ChildReadCallback(const void *context, uint8_t *buf, const int si
return thisClass->resultCallback(false, 0, s); return thisClass->resultCallback(false, 0, s);
}; };
#if !defined(_WIN32) && !defined(HDC_HOST)
static void SetSelinuxLabel()
{
#if defined(SURPPORT_SELINUX) && defined(UPDATER_MODE)
char *con = nullptr;
if (getcon(&con) != 0) {
return;
}
if (strcmp(con, "u:r:hdcd:s0") != 0) {
freecon(con);
return;
}
setcon("u:r:sh:s0");
freecon(con);
#endif
}
#endif
int AsyncCmd::Popen(string command, bool readWrite, int &cpid) int AsyncCmd::Popen(string command, bool readWrite, int &cpid)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -116,6 +139,9 @@ int AsyncCmd::Popen(string command, bool readWrite, int &cpid)
setsid(); setsid();
setpgid(childPid, childPid); setpgid(childPid, childPid);
#if !defined(HDC_HOST)
SetSelinuxLabel();
#endif
string shellPath = Base::GetShellPath(); string shellPath = Base::GetShellPath();
execl(shellPath.c_str(), shellPath.c_str(), "-c", command.c_str(), NULL); execl(shellPath.c_str(), shellPath.c_str(), "-c", command.c_str(), NULL);
exit(0); exit(0);

View File

@ -24,6 +24,9 @@
#include "unistd.h" #include "unistd.h"
#include "base.h" #include "base.h"
#include "file_descriptor.h" #include "file_descriptor.h"
#if defined(SURPPORT_SELINUX) && defined(UPDATER_MODE)
#include "selinux/selinux.h"
#endif
namespace Hdc { namespace Hdc {
std::mutex HdcShell::mutexPty; std::mutex HdcShell::mutexPty;
@ -143,6 +146,22 @@ int HdcShell::ChildForkDo(int pts, const char *cmd, const char *arg0, const char
return 0; return 0;
} }
static void SetSelinuxLabel()
{
#if defined(SURPPORT_SELINUX) && defined(UPDATER_MODE)
char *con = nullptr;
if (getcon(&con) != 0) {
return;
}
if (strcmp(con, "u:r:hdcd:s0") != 0) {
freecon(con);
return;
}
setcon("u:r:sh:s0");
freecon(con);
#endif
}
int HdcShell::ShellFork(const char *cmd, const char *arg0, const char *arg1) int HdcShell::ShellFork(const char *cmd, const char *arg0, const char *arg1)
{ {
pid_t pid; pid_t pid;
@ -158,6 +177,7 @@ int HdcShell::ShellFork(const char *cmd, const char *arg0, const char *arg1)
Base::DeInitProcess(); Base::DeInitProcess();
HdcShell::mutexPty.unlock(); HdcShell::mutexPty.unlock();
setsid(); setsid();
SetSelinuxLabel();
Base::CloseFd(ptm); Base::CloseFd(ptm);
int pts = 0; int pts = 0;
if ((pts = open(devname, O_RDWR | O_CLOEXEC)) < 0) { if ((pts = open(devname, O_RDWR | O_CLOEXEC)) < 0) {