4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongGi Jang <dg0402.jang@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <sys/types.h>
29 #include <devman_plugin_intf.h>
32 #include "sysman-priv.h"
34 #define DEVMAN_PLUGIN_PATH "/usr/lib/libslp_devman_plugin.so"
35 #define PERMANENT_DIR "/tmp/permanent"
36 #define VIP_DIR "/tmp/vip"
38 #define OOMADJ_SET "oomadj_set"
40 static void *dlopen_handle;
42 const OEM_sys_devman_plugin_interface *plugin_intf;
50 int util_oomadj_set(int pid, int oomadj_val)
55 snprintf(buf1, sizeof(buf1), "%d", pid);
56 snprintf(buf2, sizeof(buf2), "%d", oomadj_val);
57 return sysman_call_predef_action(OOMADJ_SET, 2, buf1, buf2);
60 API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
78 return util_oomadj_set(pid, oomadj_val);
81 API int sysconf_set_mempolicy(enum mem_policy mempol)
83 return sysconf_set_mempolicy_bypid(getpid(), mempol);
86 static int already_permanent(int pid)
90 snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
92 if (access(buf, R_OK) == 0) {
93 DBG("already_permanent process : %d", pid);
99 static int copy_cmdline(int pid)
102 char filepath[PATH_MAX];
106 if (access(PERMANENT_DIR, R_OK) < 0) {
107 DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR);
108 mkdir(PERMANENT_DIR, 0777);
111 snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
113 fd = open(filepath, O_RDONLY);
115 DBG("Failed to open");
119 cnt = read(fd, buf, PATH_MAX);
123 /* Read /proc/<pid>/cmdline error */
124 DBG("Failed to read");
128 snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
130 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
132 DBG("Failed to open");
136 if (write(fd, buf, cnt) == -1) {
137 DBG("Failed to write");
146 API int sysconf_set_vip(int pid)
154 if (access(VIP_DIR, R_OK) < 0) {
155 DBG("no predefined matrix dir = %s, so created", VIP_DIR);
156 mkdir(VIP_DIR, 0777);
159 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
160 fd = open(buf, O_CREAT | O_RDWR, 0644);
163 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_vip(pid)) {
164 ERR("set vip failed");
171 API int sysconf_is_vip(int pid)
178 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
180 if (access(buf, R_OK) == 0)
186 API int sysconf_set_permanent_bypid(int pid)
189 if (already_permanent(pid))
192 if (copy_cmdline(pid) < 0)
195 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(pid)) {
196 ERR("set vip failed");
201 util_oomadj_set(pid, -17);
206 API int sysconf_set_permanent()
208 pid_t pid = getpid();
209 return sysconf_set_permanent_bypid(pid);
212 static void __attribute__ ((constructor)) module_init()
216 dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
217 if (!dlopen_handle) {
218 ERR("dlopen() failed");
222 const OEM_sys_devman_plugin_interface *(*OEM_sys_get_devman_plugin_interface) ();
223 OEM_sys_get_devman_plugin_interface = dlsym(dlopen_handle, "OEM_sys_get_devman_plugin_interface");
224 if ((error = dlerror()) != NULL) {
225 ERR("dlsym() failed: %s", error);
226 dlclose(dlopen_handle);
230 plugin_intf = OEM_sys_get_devman_plugin_interface();
232 ERR("get_devman_plugin_interface() failed");
233 dlclose(dlopen_handle);
239 static void __attribute__ ((destructor)) module_fini()
242 dlclose(dlopen_handle);