diff --git a/BUILD.gn b/BUILD.gn index 5c7e3bacbca1f4e03b16ea70a8f60302c02c36f1..0bdd0d9d58a522ba5c3852b19c3a4f336362d93b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -103,6 +103,11 @@ ohos_static_library("appspawn_server") { external_deps += [ "selinux:libhap_restorecon" ] } + if (appspawn_report_event) { + cflags = [ "-DREPORT_EVENT" ] + deps += [ "adapter/sysevent:event_reporter" ] + } + subsystem_name = "${subsystem_name}" part_name = "${part_name}" } diff --git a/adapter/sysevent/BUILD.gn b/adapter/sysevent/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c2d75079b85ef3183de5e68287399f4b22b8d1e2 --- /dev/null +++ b/adapter/sysevent/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//build/ohos.gni") +config("event_reporter_config") { + visibility = [ "*:*" ] + include_dirs = [ "." ] +} + +ohos_source_set("event_reporter") { + sources = [ "event_reporter.cpp" ] + public_configs = [ ":event_reporter_config" ] + external_deps = [ "hisysevent_native:libhisysevent" ] +} diff --git a/adapter/sysevent/event_reporter.cpp b/adapter/sysevent/event_reporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c00be6aa2c04a99a8dd02d40b91f69b2a0195d8b --- /dev/null +++ b/adapter/sysevent/event_reporter.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "event_reporter.h" + +#include + +using namespace OHOS::HiviewDFX; +namespace OHOS { +namespace system { +constexpr char KEY_PROCESS_EXIT[] = "PROCESS_EXIT"; +constexpr char KEY_NAME[] = "PROCESS_NAME"; +constexpr char KEY_PID[] = "PID"; +constexpr char KEY_UID[] = "UID"; +constexpr char KEY_STATUS[] = "STATUS"; +constexpr int32_t MAX_NAME_LENGTH = 1024; +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status) +{ + std::string pname = "Unknown"; + if ((processName != NULL) && (strlen(processName) <= MAX_NAME_LENGTH)) { + pname = std::string(processName, strlen(processName)); + } + + HiSysEvent::Write(HiSysEvent::Domain::STARTUP, KEY_PROCESS_EXIT, HiSysEvent::EventType::BEHAVIOR, + KEY_NAME, pname, KEY_PID, pid, KEY_UID, uid, KEY_STATUS, status); +} +} // namespace system +} // namespace OHOS + +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status) +{ + OHOS::system::ReportProcessExitInfo(processName, pid, uid, status); +} diff --git a/adapter/sysevent/event_reporter.h b/adapter/sysevent/event_reporter.h new file mode 100644 index 0000000000000000000000000000000000000000..f96bac1cd3ee0b70e07f501040cf82bd60e55096 --- /dev/null +++ b/adapter/sysevent/event_reporter.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef STARTUP_EVENT_REPORT_H +#define STARTUP_EVENT_REPORT_H +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +void ReportProcessExitInfo(const char* processName, int pid, int uid, int status); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif \ No newline at end of file diff --git a/appspawn.gni b/appspawn.gni index 24ada92960d06f724ac8d4471f8f3dd518ca8ee7..369b7fdfacc70bff03269e4277373b16a4a7776f 100644 --- a/appspawn.gni +++ b/appspawn.gni @@ -19,4 +19,5 @@ module_output_path = "${part_name}/appspawn_l2" declare_args() { appspawn_support_nweb = true + appspawn_report_event = true } diff --git a/bundle.json b/bundle.json index 18ac474a73798f099e3a752c6353496e46439619..616ce543e05da76944c632195d785102eb7b035a 100644 --- a/bundle.json +++ b/bundle.json @@ -22,9 +22,13 @@ ], "rom": "", "ram": "", + "hisysevent_config": [ + "//base/startup/appspawn_standard/startup_events.yaml" + ], "deps": { "components": [ "ability_base", + "hisysevent_native", "hiviewdfx_hilog_native", "ipc", "safwk", diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index de903307d972e8d01eef8baf145fb3617b18fa68..dfd7f994e86077830ff3c8b4e03a141f151c8fb5 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -33,6 +33,10 @@ #include "parameter.h" #include "securec.h" +#ifdef REPORT_EVENT +#include "event_reporter.h" +#endif + static AppSpawnContentExt *g_appSpawnContent = NULL; static const int TV_SEC = 60; @@ -131,6 +135,24 @@ static int SendResponse(AppSpawnClientExt *client, const char *buff, size_t buff return LE_Send(LE_GetDefaultLoop(), client->stream, handle, buffSize); } +#ifdef REPORT_EVENT +static void PrintProcessExitInfo(pid_t pid, uid_t uid, int status) +{ + HashNode *node = HashMapGet(g_appSpawnContent->appMap, (const void *)&pid); + APPSPAWN_CHECK(node != NULL, return, "Handle SIGCHLD from pid:%d status:%d", pid, status); + AppInfo *appInfo = HASHMAP_ENTRY(node, AppInfo, node); + APPSPAWN_CHECK(appInfo != NULL, return, "Handle SIGCHLD from pid:%d status:%d", pid, status); + if (WIFSIGNALED(status)) { + APPSPAWN_LOGW("%s with pid %d exit with signal:%d", appInfo->name, pid, WTERMSIG(status)); + } + if (WIFEXITED(status)) { + APPSPAWN_LOGW("%s with pid %d exit with code:%d", appInfo->name, pid, WEXITSTATUS(status)); + } + + ReportProcessExitInfo(appInfo->name, pid, uid, status); +} +#endif + static void SignalHandler(const struct signalfd_siginfo *siginfo) { APPSPAWN_LOGI("SignalHandler signum %d", siginfo->ssi_signo); @@ -142,6 +164,9 @@ static void SignalHandler(const struct signalfd_siginfo *siginfo) int status; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { APPSPAWN_LOGI("SignalHandler pid %d status %d", pid, status); +#ifdef REPORT_EVENT + PrintProcessExitInfo(pid, siginfo->ssi_uid, status); +#endif RemoveAppInfo(pid); } #endif diff --git a/startup_events.yaml b/startup_events.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4bf186289f4303624350b90cf128d98d3de4bdb0 --- /dev/null +++ b/startup_events.yaml @@ -0,0 +1,21 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +domain: STARTUP + +PROCESS_EXIT: + __BASE: {type: BEHAVIOR, level: CRITICAL, tag: Stability, desc: process exit reason} + PROCESS_NAME: {type: STRING, desc: process name} + PID: {type: UINT32, desc: process id} + UID: {type: UINT32, desc: user id} + STATUS: {type: INT32, desc: exit code or signal number}