2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <sys/types.h>
24 #include <devman_plugin_intf.h>
27 #include "sysman-priv.h"
29 #define DEVMAN_PLUGIN_PATH "/usr/lib/libslp_devman_plugin.so"
30 #define PERMANENT_DIR "/tmp/permanent"
31 #define VIP_DIR "/tmp/vip"
33 #define OOMADJ_SET "oomadj_set"
35 static void *dlopen_handle;
37 const OEM_sys_devman_plugin_interface *plugin_intf;
45 int util_oomadj_set(int pid, int oomadj_val)
50 snprintf(buf1, sizeof(buf1), "%d", pid);
51 snprintf(buf2, sizeof(buf2), "%d", oomadj_val);
52 return sysman_call_predef_action(OOMADJ_SET, 2, buf1, buf2);
55 API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
73 return util_oomadj_set(pid, oomadj_val);
76 API int sysconf_set_mempolicy(enum mem_policy mempol)
78 return sysconf_set_mempolicy_bypid(getpid(), mempol);
81 static int already_permanent(int pid)
85 snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
87 if (access(buf, R_OK) == 0) {
88 DBG("already_permanent process : %d", pid);
94 static int copy_cmdline(int pid)
97 char filepath[PATH_MAX];
102 if (access(PERMANENT_DIR, R_OK) < 0) {
103 DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR);
104 r = mkdir(PERMANENT_DIR, 0777);
106 ERR("permanent directory mkdir is failed");
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)
155 if (access(VIP_DIR, R_OK) < 0) {
156 DBG("no predefined matrix dir = %s, so created", VIP_DIR);
157 r = mkdir(VIP_DIR, 0777);
159 ERR("sysconf_set_vip vip mkdir is failed");
164 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
165 fd = open(buf, O_CREAT | O_RDWR, 0644);
167 ERR("sysconf_set_vip fd open failed");
172 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_vip(pid)) {
173 ERR("set vip failed");
180 API int sysconf_is_vip(int pid)
187 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
189 if (access(buf, R_OK) == 0)
195 API int sysconf_set_permanent_bypid(int pid)
198 if (already_permanent(pid))
201 if (copy_cmdline(pid) < 0)
204 if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(pid)) {
205 ERR("set vip failed");
210 util_oomadj_set(pid, -17);
215 API int sysconf_set_permanent()
217 pid_t pid = getpid();
218 return sysconf_set_permanent_bypid(pid);
221 static void __attribute__ ((constructor)) module_init()
225 dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
226 if (!dlopen_handle) {
227 ERR("dlopen() failed");
231 const OEM_sys_devman_plugin_interface *(*OEM_sys_get_devman_plugin_interface) ();
232 OEM_sys_get_devman_plugin_interface = dlsym(dlopen_handle, "OEM_sys_get_devman_plugin_interface");
233 if ((error = dlerror()) != NULL) {
234 ERR("dlsym() failed: %s", error);
235 dlclose(dlopen_handle);
239 plugin_intf = OEM_sys_get_devman_plugin_interface();
241 ERR("get_devman_plugin_interface() failed");
242 dlclose(dlopen_handle);
248 static void __attribute__ ((destructor)) module_fini()
251 dlclose(dlopen_handle);