Add packaging/libslp-sysman.changes file
[platform/core/system/libslp-sysman.git] / sysconf.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <dlfcn.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <devman_plugin_intf.h>
25
26 #include "sysman.h"
27 #include "sysman-priv.h"
28
29 #define DEVMAN_PLUGIN_PATH      "/usr/lib/libslp_devman_plugin.so"
30 #define PERMANENT_DIR           "/tmp/permanent"
31 #define VIP_DIR                 "/tmp/vip"
32
33 #define OOMADJ_SET              "oomadj_set"
34
35 static void *dlopen_handle;
36
37 const OEM_sys_devman_plugin_interface *plugin_intf;
38
39 enum mp_entry_type {
40         MP_VIP,
41         MP_PERMANENT,
42         MP_NONE
43 };
44
45 int util_oomadj_set(int pid, int oomadj_val)
46 {
47         char buf1[255];
48         char buf2[255];
49
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);
53 }
54
55 API int sysconf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
56 {
57         if (pid < 1)
58                 return -1;
59
60         int oomadj_val = 0;
61
62         switch (mempol) {
63         case OOM_LIKELY:
64                 oomadj_val = 1;
65                 break;
66         case OOM_IGNORE:
67                 oomadj_val = -17;
68                 break;
69         default:
70                 return -1;
71         }
72
73         return util_oomadj_set(pid, oomadj_val);
74 }
75
76 API int sysconf_set_mempolicy(enum mem_policy mempol)
77 {
78         return sysconf_set_mempolicy_bypid(getpid(), mempol);
79 }
80
81 static int already_permanent(int pid)
82 {
83         char buf[BUFF_MAX];
84
85         snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
86
87         if (access(buf, R_OK) == 0) {
88                 DBG("already_permanent process : %d", pid);
89                 return 1;
90         }
91         return 0;
92 }
93
94 static int copy_cmdline(int pid)
95 {
96         char buf[PATH_MAX];
97         char filepath[PATH_MAX];
98         int fd;
99         int cnt;
100         int r;
101
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);
105                 if(r < 0) {
106                         ERR("permanent directory mkdir is failed");
107                         return -1;
108                 }
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         int r;
151
152         if (pid < 1)
153                 return -1;
154
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);
158                 if(r < 0) {
159                         ERR("sysconf_set_vip vip mkdir is failed");
160                         return -1;
161                 }
162         }
163
164         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
165         fd = open(buf, O_CREAT | O_RDWR, 0644);
166         if (fd < 0) {
167                 ERR("sysconf_set_vip fd open failed");
168                 return -1;
169         }
170         close(fd);
171
172         if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_vip(pid)) {
173                 ERR("set vip failed");
174                 return -1;
175         }
176
177         return 0;
178 }
179
180 API int sysconf_is_vip(int pid)
181 {
182         if (pid < 1)
183                 return -1;
184
185         char buf[BUFF_MAX];
186
187         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
188
189         if (access(buf, R_OK) == 0)
190                 return 1;
191         else
192                 return 0;
193 }
194
195 API int sysconf_set_permanent_bypid(int pid)
196 {
197         int fd;
198         if (already_permanent(pid))
199                 goto MEMPOL_SET;
200
201         if (copy_cmdline(pid) < 0)
202                 return -1;
203
204         if (0 > plugin_intf->OEM_sys_set_process_monitor_mp_pnp(pid)) {
205                 ERR("set vip failed");
206                 return -1;
207         }
208
209  MEMPOL_SET:
210         util_oomadj_set(pid, -17);
211
212         return 0;
213 }
214
215 API int sysconf_set_permanent()
216 {
217         pid_t pid = getpid();
218         return sysconf_set_permanent_bypid(pid);
219 }
220
221 static void __attribute__ ((constructor)) module_init()
222 {
223         char *error;
224
225         dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
226         if (!dlopen_handle) {
227                 ERR("dlopen() failed");
228                 return;
229         }
230
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);
236                 return;
237         }
238
239         plugin_intf = OEM_sys_get_devman_plugin_interface();
240         if (!plugin_intf) {
241                 ERR("get_devman_plugin_interface() failed");
242                 dlclose(dlopen_handle);
243                 return;
244         }
245 }
246
247
248 static void __attribute__ ((destructor)) module_fini()
249 {
250         if (dlopen_handle) {
251                 dlclose(dlopen_handle);
252         }
253
254 }