tizen_2.0_build
[platform/core/system/libslp-sysman.git] / sysconf.c
1 /*
2  *  libslp-sysman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  * 
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20 */ 
21
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dlfcn.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <devman_plugin_intf.h>
30
31 #include "sysman.h"
32 #include "sysman-priv.h"
33
34 #define DEVMAN_PLUGIN_PATH      "/usr/lib/libslp_devman_plugin.so"
35 #define PERMANENT_DIR           "/tmp/permanent"
36 #define VIP_DIR                 "/tmp/vip"
37
38 #define OOMADJ_SET              "oomadj_set"
39
40 static void *dlopen_handle;
41
42 const OEM_sys_devman_plugin_interface *plugin_intf;
43
44 enum mp_entry_type {
45         MP_VIP,
46         MP_PERMANENT,
47         MP_NONE
48 };
49
50 int util_oomadj_set(int pid, int oomadj_val)
51 {
52         char buf1[255];
53         char buf2[255];
54
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);
58 }
59
60 API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
61 {
62         if (pid < 1)
63                 return -1;
64
65         int oomadj_val = 0;
66
67         switch (mempol) {
68         case OOM_LIKELY:
69                 oomadj_val = 1;
70                 break;
71         case OOM_IGNORE:
72                 oomadj_val = -17;
73                 break;
74         default:
75                 return -1;
76         }
77
78         return util_oomadj_set(pid, oomadj_val);
79 }
80
81 API int sysconf_set_mempolicy(enum mem_policy mempol)
82 {
83         return sysconf_set_mempolicy_bypid(getpid(), mempol);
84 }
85
86 static int already_permanent(int pid)
87 {
88         char buf[BUFF_MAX];
89
90         snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
91
92         if (access(buf, R_OK) == 0) {
93                 DBG("already_permanent process : %d", pid);
94                 return 1;
95         }
96         return 0;
97 }
98
99 static int copy_cmdline(int pid)
100 {
101         char buf[PATH_MAX];
102         char filepath[PATH_MAX];
103         int fd;
104         size_t cnt;
105
106         if (access(PERMANENT_DIR, R_OK) < 0) {
107                 DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR);
108                 mkdir(PERMANENT_DIR, 0777);
109         }
110
111         snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
112
113         fd = open(filepath, O_RDONLY);
114         if (fd == -1) {
115                 DBG("Failed to open");
116                 return -1;
117         }
118
119         cnt = read(fd, buf, PATH_MAX);
120         close(fd);
121
122         if (cnt <= 0) {
123                 /* Read /proc/<pid>/cmdline error */
124                 DBG("Failed to read");
125                 return -1;
126         }
127
128         snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
129
130         fd = open(filepath, O_CREAT | O_WRONLY, 0644);
131         if (fd == -1) {
132                 DBG("Failed to open");
133                 return -1;
134         }
135
136         if (write(fd, buf, cnt) == -1) {
137                 DBG("Failed to write");
138                 close(fd);
139                 return -1;
140         }
141         close(fd);
142
143         return 0;
144 }
145
146 API int sysconf_set_vip(int pid)
147 {
148         char buf[BUFF_MAX];
149         int fd;
150
151         if (pid < 1)
152                 return -1;
153
154         if (access(VIP_DIR, R_OK) < 0) {
155                 DBG("no predefined matrix dir = %s, so created", VIP_DIR);
156                 mkdir(VIP_DIR, 0777);
157         }
158
159         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
160         fd = open(buf, O_CREAT | O_RDWR, 0644);
161         close(fd);
162
163         if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_vip(pid)) {
164                 ERR("set vip failed");
165                 return -1;
166         }
167
168         return 0;
169 }
170
171 API int sysconf_is_vip(int pid)
172 {
173         if (pid < 1)
174                 return -1;
175
176         char buf[BUFF_MAX];
177
178         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
179
180         if (access(buf, R_OK) == 0)
181                 return 1;
182         else
183                 return 0;
184 }
185
186 API int sysconf_set_permanent_bypid(int pid)
187 {
188         int fd;
189         if (already_permanent(pid))
190                 goto MEMPOL_SET;
191
192         if (copy_cmdline(pid) < 0)
193                 return -1;
194
195         if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(pid)) {
196                 ERR("set vip failed");
197                 return -1;
198         }
199
200  MEMPOL_SET:
201         util_oomadj_set(pid, -17);
202
203         return 0;
204 }
205
206 API int sysconf_set_permanent()
207 {
208         pid_t pid = getpid();
209         return sysconf_set_permanent_bypid(pid);
210 }
211
212 static void __attribute__ ((constructor)) module_init()
213 {
214         char *error;
215
216         dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
217         if (!dlopen_handle) {
218                 ERR("dlopen() failed");
219                 return;
220         }
221
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);
227                 return;
228         }
229
230         plugin_intf = OEM_sys_get_devman_plugin_interface();
231         if (!plugin_intf) {
232                 ERR("get_devman_plugin_interface() failed");
233                 dlclose(dlopen_handle);
234                 return;
235         }
236 }
237
238
239 static void __attribute__ ((destructor)) module_fini()
240 {
241         if (dlopen_handle) {
242                 dlclose(dlopen_handle);
243         }
244
245 }