* limitations under the License.
*/
-#include <stdio.h>
-#include <linux/limits.h>
-#include <dlfcn.h>
#include <Ecore.h>
#include <bundle_internal.h>
+#include <dlfcn.h>
+#include <linux/limits.h>
+#include <stdint.h>
+#include <stdio.h>
#include <sys/prctl.h>
#include <bundle_cpp.h>
#include <memory>
#include <vector>
+#include "key.h"
+#include "launchpad.h"
#include "launchpad_common.h"
#include "launchpad_types.h"
-#include "launchpad.h"
-#include "key.h"
-#ifdef LOG_TAG
-#undef LOG_TAG
-#endif
-#define LOG_TAG "APP_DEFINED_LOADER"
+#include "config.hh"
+#include "log-private.hh"
+#include "proc.hh"
#ifndef PR_TASK_PERF_USER_TRACE
#define PR_TASK_PERF_USER_TRACE 666
#define C_EXPORT extern "C" __attribute__((visibility("default")))
#endif
-using namespace tizen_base;
-using namespace std;
namespace launchpad {
+static const char PATH_CONF[] = "/usr/share/aul/app-defined-loader.conf";
+static const char SECTION_MEMORY[] = "Memory";
+static const char KEY_THRESHOLD[] = "Threshold";
+static uint32_t DEFAULT_THRESHOLD = 25600; // kB
+
class AppDefinedLoader {
public:
AppDefinedLoader(int argc, char** argv)
: argc_(argc), argv_(argv) {
- lifecycle_cb_ = shared_ptr<loader_lifecycle_callback_s>(new loader_lifecycle_callback_s());
+ lifecycle_cb_ = std::shared_ptr<loader_lifecycle_callback_s>(
+ new loader_lifecycle_callback_s());
lifecycle_cb_->create = OnCreate;
lifecycle_cb_->launch = OnLaunch;
lifecycle_cb_->terminate = OnTerminate;
- adapter_ = shared_ptr<loader_adapter_s>(new loader_adapter_s());
+ adapter_ = std::shared_ptr<loader_adapter_s>(new loader_adapter_s());
adapter_->loop_begin = OnLoopBegin;
adapter_->loop_quit = OnLoopQuit;
adapter_->add_fd = OnAddFd;
adapter_->remove_fd = OnRemoveFd;
+
+ proc_ = std::unique_ptr<Proc>(new Proc(getpid()));
+
+ Config conf(PATH_CONF);
+ int threshold = conf.GetIntValue(SECTION_MEMORY, KEY_THRESHOLD);
+ if (threshold > 0) {
+ threshold_ = static_cast<uint32_t>(threshold);
+ } else {
+ _W("Failed to get threshold");
+ }
}
~AppDefinedLoader() {
_W("app defined loader destroyed");
}
- shared_ptr<loader_lifecycle_callback_s> GetLifeCycle() {
+ std::shared_ptr<loader_lifecycle_callback_s> GetLifeCycle() {
return lifecycle_cb_;
}
- shared_ptr<loader_adapter_s> GetAdapter() {
+ std::shared_ptr<loader_adapter_s> GetAdapter() {
return adapter_;
}
}
private:
- static void PreloadLib(Bundle data) {
- vector<std::string> so_array = data.GetStringArray("preload");
+ void PreloadLib(tizen_base::Bundle data) {
+ std::vector<std::string> so_array = data.GetStringArray("preload");
if (so_array.size() == 0)
return;
+
for (auto& i : so_array) {
if (i.empty())
continue;
_E("fail to load : %s, err : %s", i.c_str(), dlerror());
else
_D("preload %s# - handle : %p", i.c_str(), handle);
+
+ uint32_t pss = proc_->GetPss();
+ if (pss > threshold_) {
+ _W("Pss(%u) is over threshold(%u)", pss, threshold_);
+ break;
+ }
}
}
static void OnCreate(bundle *extra, int type, void *user_data) {
_I("on create");
AppDefinedLoader* loader = (AppDefinedLoader*)user_data;
- Bundle ex = Bundle(extra, false, false);
- string loader_type = ex.GetString(KEY_LOADER_TYPE);
+ tizen_base::Bundle ex = tizen_base::Bundle(extra, false, false);
+ std::string loader_type = ex.GetString(KEY_LOADER_TYPE);
if (loader_type.empty()) {
_E("No loader type");
return;
}
static int OnLaunch(int argc, char **argv, const char *app_path,
- const char *appid, const char *pkgid, const char *pkg_type, void *user_data) {
+ const char *appid, const char *pkgid, const char *pkg_type,
+ void *user_data) {
_I("on launch");
AppDefinedLoader* loader = (AppDefinedLoader*)user_data;
if (!loader->IsPriorityChangeEnabled())
if (kb == nullptr)
return 0;
- Bundle data(kb, false, false);
- string high_priority = data.GetString(AUL_K_HIGHPRIORITY);
+ tizen_base::Bundle data(kb, false, false);
+ std::string high_priority = data.GetString(AUL_K_HIGHPRIORITY);
if (high_priority == "true")
launchpad_loader_set_priority(-12);
data.Delete(AUL_K_HIGHPRIORITY);
return 0;
}
- void DoExec(string libdir) {
+ void DoExec(std::string libdir) {
_I("do exec");
char err_str[MAX_LOCAL_BUFSZ];
if (access(argv_[LOADER_ARG_PATH], F_OK | R_OK)) {
- SECURE_LOGE("access() failed for file: \"%s\", error: %d (%s)", argv_[LOADER_ARG_PATH], errno,
+ SECURE_LOGE("access() failed for file: \"%s\", error: %d (%s)",
+ argv_[LOADER_ARG_PATH], errno,
strerror_r(errno, err_str, sizeof(err_str)));
} else {
SECURE_LOGD("[candidate] Exec application (%s)",
}
}
- int DoDlOpen(bool restore, string old_cwd, string libdir) {
+ int DoDlOpen(bool restore, std::string old_cwd, std::string libdir) {
_I("do dlopen");
- string hwc_message = "" + to_string(getpid()) + "|lib loading start";
+ std::string hwc_message = "" + std::to_string(getpid()) +
+ "|lib loading start";
prctl(PR_TASK_PERF_USER_TRACE, hwc_message.c_str(), hwc_message.size());
void* handle = dlopen(argv_[LOADER_ARG_PATH],
RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE);
return -1;
}
- hwc_message = "" + to_string(getpid()) + "|lib loading end";
+ hwc_message = "" + std::to_string(getpid()) + "|lib loading end";
prctl(PR_TASK_PERF_USER_TRACE, hwc_message.c_str(), hwc_message.size());
if (restore && chdir(old_cwd.c_str()))
}
static int OnTerminate(int argc, char **argv, void *user_data) {
- SECURE_LOGD("[candidate] Launch real application (%s)", argv[LOADER_ARG_PATH]);
+ SECURE_LOGD("[candidate] Launch real application (%s)",
+ argv[LOADER_ARG_PATH]);
char old_cwd[PATH_MAX] = {0, };
AppDefinedLoader* loader = (AppDefinedLoader*)user_data;
if (getcwd(old_cwd, sizeof(old_cwd)) == nullptr) {
_E("failed to chdir: %d", errno);
else
restore = true;
- string libdir_str = string(libdir);
+ std::string libdir_str = std::string(libdir);
free(libdir);
return loader->DoDlOpen(restore, old_cwd, libdir_str);
}
}
private:
- shared_ptr<loader_lifecycle_callback_s> lifecycle_cb_ = nullptr;
- shared_ptr<loader_adapter_s> adapter_ = nullptr;
+ std::shared_ptr<loader_lifecycle_callback_s> lifecycle_cb_ = nullptr;
+ std::shared_ptr<loader_adapter_s> adapter_ = nullptr;
bool loader_priority_enabled_ = false;
bool priority_change_enabled_ = false;
- loader_receiver_cb receiver_cb_;
+ loader_receiver_cb receiver_cb_ = nullptr;
Ecore_Fd_Handler* fd_handler_ = nullptr;
+ std::unique_ptr<Proc> proc_;
+ uint32_t threshold_ = DEFAULT_THRESHOLD;
int argc_;
char** argv_;
};
}
-C_EXPORT int main(int argc, char **argv)
-{
+C_EXPORT int main(int argc, char **argv) {
launchpad::AppDefinedLoader loader(argc, argv);
#ifdef TIZEN_FEATURE_LOADER_PRIORITY
return launchpad_loader_main(argc, argv,
loader.GetLifeCycle().get(), loader.GetAdapter().get(), &loader);
-}
\ No newline at end of file
+}