/* * libslp-sysman * * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: DongGi Jang * * 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 #include #include #include #include #include #include #include "sysman.h" #include "sysman-priv.h" #define DEVMAN_PLUGIN_PATH "/usr/lib/libslp_devman_plugin.so" #define PERMANENT_DIR "/tmp/permanent" #define VIP_DIR "/tmp/vip" #define OOMADJ_SET "oomadj_set" static void *dlopen_handle; const OEM_sys_devman_plugin_interface *plugin_intf; enum mp_entry_type { MP_VIP, MP_PERMANENT, MP_NONE }; int util_oomadj_set(int pid, int oomadj_val) { char buf1[255]; char buf2[255]; snprintf(buf1, sizeof(buf1), "%d", pid); snprintf(buf2, sizeof(buf2), "%d", oomadj_val); return sysman_call_predef_action(OOMADJ_SET, 2, buf1, buf2); } API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol) { if (pid < 1) return -1; int oomadj_val = 0; switch (mempol) { case OOM_LIKELY: oomadj_val = 1; break; case OOM_IGNORE: oomadj_val = -17; break; default: return -1; } return util_oomadj_set(pid, oomadj_val); } API int sysconf_set_mempolicy(enum mem_policy mempol) { return sysconf_set_mempolicy_bypid(getpid(), mempol); } static int already_permanent(int pid) { char buf[BUFF_MAX]; snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid); if (access(buf, R_OK) == 0) { DBG("already_permanent process : %d", pid); return 1; } return 0; } static int copy_cmdline(int pid) { char buf[PATH_MAX]; char filepath[PATH_MAX]; int fd; size_t cnt; if (access(PERMANENT_DIR, R_OK) < 0) { DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR); mkdir(PERMANENT_DIR, 0777); } snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid); fd = open(filepath, O_RDONLY); if (fd == -1) { DBG("Failed to open"); return -1; } cnt = read(fd, buf, PATH_MAX); close(fd); if (cnt <= 0) { /* Read /proc//cmdline error */ DBG("Failed to read"); return -1; } snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid); fd = open(filepath, O_CREAT | O_WRONLY, 0644); if (fd == -1) { DBG("Failed to open"); return -1; } if (write(fd, buf, cnt) == -1) { DBG("Failed to write"); close(fd); return -1; } close(fd); return 0; } API int sysconf_set_vip(int pid) { char buf[BUFF_MAX]; int fd; if (pid < 1) return -1; if (access(VIP_DIR, R_OK) < 0) { DBG("no predefined matrix dir = %s, so created", VIP_DIR); mkdir(VIP_DIR, 0777); } snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid); fd = open(buf, O_CREAT | O_RDWR, 0644); close(fd); if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_vip(pid)) { ERR("set vip failed"); return -1; } return 0; } API int sysconf_is_vip(int pid) { if (pid < 1) return -1; char buf[BUFF_MAX]; snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid); if (access(buf, R_OK) == 0) return 1; else return 0; } API int sysconf_set_permanent_bypid(int pid) { int fd; if (already_permanent(pid)) goto MEMPOL_SET; if (copy_cmdline(pid) < 0) return -1; if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(pid)) { ERR("set vip failed"); return -1; } MEMPOL_SET: util_oomadj_set(pid, -17); return 0; } API int sysconf_set_permanent() { pid_t pid = getpid(); return sysconf_set_permanent_bypid(pid); } static void __attribute__ ((constructor)) module_init() { char *error; dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW); if (!dlopen_handle) { ERR("dlopen() failed"); return; } const OEM_sys_devman_plugin_interface *(*OEM_sys_get_devman_plugin_interface) (); OEM_sys_get_devman_plugin_interface = dlsym(dlopen_handle, "OEM_sys_get_devman_plugin_interface"); if ((error = dlerror()) != NULL) { ERR("dlsym() failed: %s", error); dlclose(dlopen_handle); return; } plugin_intf = OEM_sys_get_devman_plugin_interface(); if (!plugin_intf) { ERR("get_devman_plugin_interface() failed"); dlclose(dlopen_handle); return; } } static void __attribute__ ((destructor)) module_fini() { if (dlopen_handle) { dlclose(dlopen_handle); } }