From 13dbc37266dd908b257ffd1b7d412ceaf4550ea1 Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Wed, 30 Sep 2015 19:51:47 +0300 Subject: [PATCH] [IMPROVE] add application to ANR exclude list Change-Id: I7228f50b4c24c04231aa7847f8ca88a31d69915e Signed-off-by: Vyacheslav Cherkashin --- daemon/Makefile | 1 + daemon/cpp/inst/Anr.cpp | 109 ++++++++++++++++++++++++++++++++++++++++ daemon/cpp/inst/Anr.h | 38 ++++++++++++++ daemon/cpp/inst/AppInst.cpp | 21 +++++++- daemon/cpp/inst/AppInst.h | 7 ++- daemon/cpp/inst/AppInstCont.cpp | 4 +- packaging/swap-manager.spec | 4 ++ 7 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 daemon/cpp/inst/Anr.cpp create mode 100644 daemon/cpp/inst/Anr.h diff --git a/daemon/Makefile b/daemon/Makefile index a586a83..dd1322f 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -120,6 +120,7 @@ SRC_CPP := \ \ cpp/inst/AppInst.cpp \ cpp/inst/AppInstCont.cpp \ + cpp/inst/Anr.cpp \ \ cpp/elf/File.cpp \ cpp/elf/FileElf.cpp diff --git a/daemon/cpp/inst/Anr.cpp b/daemon/cpp/inst/Anr.cpp new file mode 100644 index 0000000..df9ce37 --- /dev/null +++ b/daemon/cpp/inst/Anr.cpp @@ -0,0 +1,109 @@ +/* + * DA manager + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Vyacheslav Cherkashin + * + * 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. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + + +#include +#include +#include +#include "Anr.h" +#include "swap_debug.h" + + +static const char EXCLUDE_PATH[] = "/opt/usr/etc/resourced_proc_exclude.ini"; + +static void rm_endline_symbols(char *buf, size_t len) +{ + char *p, *buf_end; + + buf_end = buf + len; + for (p = buf; p != buf_end; ++p) + if (*p == '\n' || *p == '\r') + *p = '\0'; +} + + +namespace Anr { + +int add(const std::string &name) +{ + int ret = 0; + + FILE *f = fopen(EXCLUDE_PATH, "a+"); + if (f == NULL) + return -errno; + + std::string line(name + "\n"); + if (fwrite(line.c_str(), line.length(), 1, f) != 1) + ret = -errno; + + fclose(f); + return ret; +} + +int del(const std::string &name) +{ + FILE *f = fopen(EXCLUDE_PATH, "r+"); + if (f == NULL) + return -errno; + + typedef std::list StrList; + + char *line = NULL; + size_t len = 0; + ssize_t ret; + StrList strList; + + // read all names + while ((ret = getline(&line, &len, f)) != -1) { + rm_endline_symbols(line, ret); + strList.push_back(line); + } + if (line) + free(line); + + // clean file + fseek(f, 0L, SEEK_SET); + ftruncate(fileno(f), 0); + + // write new names list without 'name' + bool delFlag = false; + for (StrList::const_iterator it = strList.begin(), + itEnd = strList.end(); it != itEnd; ++it) { + if (delFlag == false && *it == name) { + delFlag = true; + continue; + } + + std::string new_line(*it + '\n'); + if (fwrite(new_line.c_str(), new_line.length(), 1, f) != 1) + LOGE("failed write '%s' to file '%s'\n", new_line.c_str(), EXCLUDE_PATH); + } + + fclose(f); + + return 0; +} + +} // namespace Anr diff --git a/daemon/cpp/inst/Anr.h b/daemon/cpp/inst/Anr.h new file mode 100644 index 0000000..3f4b2af --- /dev/null +++ b/daemon/cpp/inst/Anr.h @@ -0,0 +1,38 @@ +/* + * DA manager + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Vyacheslav Cherkashin + * + * 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. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + +#ifndef ANR_H +#define ANR_H + +#include + +namespace Anr { + +int add(const std::string &name); +int del(const std::string &name); + +} // namespace Anr + +#endif // ANR_H diff --git a/daemon/cpp/inst/AppInst.cpp b/daemon/cpp/inst/AppInst.cpp index b379295..66c391f 100644 --- a/daemon/cpp/inst/AppInst.cpp +++ b/daemon/cpp/inst/AppInst.cpp @@ -24,6 +24,7 @@ */ +#include "Anr.h" #include "AppInst.h" #include "AppInstTizen.h" #include "AppInstRunning.h" @@ -32,9 +33,17 @@ #include "swap_debug.h" -AppInst *AppInst::create(AppType type, const AppInstInfo &info) +static std::string getAnrName(const AppInstInfo &info) +{ + return info.type() == AT_WEB ? + info.id() : + basename(info.path().c_str()); +} + +AppInst *AppInst::create(const AppInstInfo &info) { AppInst *app(0); + AppType type = info.type(); switch (type) { case AT_TIZEN: @@ -63,11 +72,21 @@ AppInst *AppInst::create(AppType type, const AppInstInfo &info) } } + if (app) { + int ret = Anr::add(getAnrName(info)); + if (ret) + LOGE("failed Anr::add: ret=%d\n", ret); + } + return app; } void AppInst::destroy(AppInst *app) { + int ret = Anr::del(getAnrName(app->info())); + if (ret) + LOGE("failed Anr::del: ret=%d\n", ret); + app->uninit(); delete app; } diff --git a/daemon/cpp/inst/AppInst.h b/daemon/cpp/inst/AppInst.h index 1a022d5..2318aa8 100644 --- a/daemon/cpp/inst/AppInst.h +++ b/daemon/cpp/inst/AppInst.h @@ -64,18 +64,21 @@ static inline const char *type2str(AppType type) class AppInstInfo { public: - AppInstInfo(const std::string &id, const std::string &path, + AppInstInfo(AppType type, const std::string &id, const std::string &path, const ByteArray &data) : + _type(type), _id(id), _path(path), _data(data) {} + const AppType type() const { return _type ; } const std::string &id() const { return _id; } const std::string &path() const { return _path; } const ByteArray &data() const { return _data; } private: + enum AppType _type; const std::string _id; const std::string _path; const ByteArray _data; @@ -85,7 +88,7 @@ private: class AppInst { public: - static AppInst *create(AppType type, const AppInstInfo &info); + static AppInst *create(const AppInstInfo &info); static void destroy(AppInst *app); const AppInstInfo &info() const { return *_info; } diff --git a/daemon/cpp/inst/AppInstCont.cpp b/daemon/cpp/inst/AppInstCont.cpp index 537b9fc..279bbca 100644 --- a/daemon/cpp/inst/AppInstCont.cpp +++ b/daemon/cpp/inst/AppInstCont.cpp @@ -47,9 +47,9 @@ AppInstCont::~AppInstCont() int AppInstCont::add(AppType type, const std::string &id, const std::string &path, const ByteArray &data) { - AppInstInfo info(id, path, data); + AppInstInfo info(type, id, path, data); - AppInst *app = AppInst::create(type, info); + AppInst *app = AppInst::create(info); if (app == 0) return -EINVAL; diff --git a/packaging/swap-manager.spec b/packaging/swap-manager.spec index 3bf5adf..6e8621a 100644 --- a/packaging/swap-manager.spec +++ b/packaging/swap-manager.spec @@ -91,6 +91,10 @@ cp LICENSE %{buildroot}/usr/share/license/%{name} cd daemon %make_install +%post +mkdir -p /opt/usr/etc +touch /opt/usr/etc/resourced_proc_exclude.ini + %files /usr/share/license/%{name} %manifest swap-manager.manifest -- 2.7.4