Git init
[framework/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 <fcntl.h>
27 #include <limits.h>
28
29 #include "sysman.h"
30 #include "sysman-priv.h"
31
32 #define PERMANENT_DIR           "/tmp/permanent"
33 #define VIP_DIR                 "/tmp/vip"
34 #define PMON_VIP                "/sys/class/pmon/mp_vip"
35 #define PMON_PNP                "/sys/class/pmon/mp_pnp"
36
37 enum mp_entry_type {
38         MP_VIP,
39         MP_PERMANENT,
40         MP_NONE
41 };
42
43 #define OOMADJ_SET              "oomadj_set"
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         size_t cnt;
100
101         if (access(PERMANENT_DIR, R_OK) < 0) {
102                 DBG("no predefined matrix dir = %s, so created", PERMANENT_DIR);
103                 mkdir(PERMANENT_DIR, 0777);
104         }
105
106         snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
107
108         fd = open(filepath, O_RDONLY);
109         if (fd == -1) {
110                 DBG("Failed to open");
111                 return -1;
112         }
113
114         cnt = read(fd, buf, PATH_MAX);
115         close(fd);
116
117         if (cnt <= 0) {
118                 /* Read /proc/<pid>/cmdline error */
119                 DBG("Failed to read");
120                 return -1;
121         }
122
123         snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
124
125         fd = open(filepath, O_CREAT | O_WRONLY, 0644);
126         if (fd == -1) {
127                 DBG("Failed to open");
128                 return -1;
129         }
130
131         if (write(fd, buf, cnt) == -1) {
132                 DBG("Failed to write");
133                 close(fd);
134                 return -1;
135         }
136         close(fd);
137
138         return 0;
139 }
140
141 API int sysconf_set_vip(int pid)
142 {
143         char buf[BUFF_MAX];
144         int fd;
145
146         if (pid < 1)
147                 return -1;
148
149         if (access(VIP_DIR, R_OK) < 0) {
150                 DBG("no predefined matrix dir = %s, so created", VIP_DIR);
151                 mkdir(VIP_DIR, 0777);
152         }
153
154         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
155         fd = open(buf, O_CREAT | O_RDWR, 0644);
156         close(fd);
157
158         fd = open(PMON_VIP, O_WRONLY);
159
160         if (fd == -1) {
161                 DBG("File open error");
162                 return -1;
163         }
164
165         write(fd, &pid, sizeof(int));
166         close(fd);
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         fd = open(PMON_PNP, O_WRONLY);
196
197         if (fd == -1) {
198                 DBG("File open error");
199                 return -1;
200         }
201
202         write(fd, &pid, sizeof(int));
203         close(fd);
204
205  MEMPOL_SET:
206         util_oomadj_set(pid, -17);
207
208         return 0;
209 }
210
211 API int sysconf_set_permanent()
212 {
213         pid_t pid = getpid();
214         return sysconf_set_permanent_bypid(pid);
215 }