support led indicator func
[platform/core/system/system-server.git] / src / shared / system-conf.c
1 /*
2  *  libdeviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dlfcn.h>
25 #include <fcntl.h>
26 #include <limits.h>
27
28 #include "log.h"
29 #include "system-priv.h"
30 #include "dd-system.h"
31
32 #define PERMANENT_DIR           "/tmp/permanent"
33 #define VIP_DIR                 "/tmp/vip"
34
35 #define OOMADJ_SET              "oomadj_set"
36 #define PROCESS_GROUP_SET       "process_group_set"
37 #define PROCESS_VIP             "process_vip"
38 #define PROCESS_PERMANENT       "process_permanent"
39
40 enum mp_entry_type {
41         MP_VIP,
42         MP_PERMANENT,
43         MP_NONE
44 };
45
46 int util_oomadj_set(int pid, int oomadj_val)
47 {
48         char buf1[255];
49         char buf2[255];
50
51         snprintf(buf1, sizeof(buf1), "%d", pid);
52         snprintf(buf2, sizeof(buf2), "%d", oomadj_val);
53         return system_call_predef_action(OOMADJ_SET, 2, buf1, buf2);
54 }
55
56 int util_process_group_set(const char* name, int pid)
57 {
58         char buf[255];
59
60         if (strncmp(PROCESS_VIP, name, strlen(name)) != 0 &&
61             strncmp(PROCESS_PERMANENT, name, strlen(name)) != 0) {
62                 _E("fail to insert at %s group", name);
63                 return -1;
64         }
65
66         snprintf(buf, sizeof(buf), "%d", pid);
67         _E("pid(%d) is inserted at vip", pid);
68
69         return system_call_predef_action(PROCESS_GROUP_SET, 2, buf, name);
70 }
71
72 API int system_conf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
73 {
74         if (pid < 1)
75                 return -1;
76
77         int oomadj_val = 0;
78
79         switch (mempol) {
80         case OOM_LIKELY:
81                 oomadj_val = 1;
82                 break;
83         case OOM_IGNORE:
84                 oomadj_val = -17;
85                 break;
86         default:
87                 return -1;
88         }
89
90         return util_oomadj_set(pid, oomadj_val);
91 }
92
93 API int system_conf_set_mempolicy(enum mem_policy mempol)
94 {
95         return system_conf_set_mempolicy_bypid(getpid(), mempol);
96 }
97
98 static int already_permanent(int pid)
99 {
100         char buf[BUFF_MAX];
101
102         snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
103
104         if (access(buf, R_OK) == 0) {
105                 _D("already_permanent process : %d", pid);
106                 return 1;
107         }
108         return 0;
109 }
110
111 static int copy_cmdline(int pid)
112 {
113         char buf[PATH_MAX];
114         char filepath[PATH_MAX];
115         int fd;
116         int cnt;
117         int r;
118
119         if (access(PERMANENT_DIR, R_OK) < 0) {
120                 _D("no predefined matrix dir = %s, so created", PERMANENT_DIR);
121                 r = mkdir(PERMANENT_DIR, 0777);
122                 if(r < 0) {
123                         _E("permanent directory mkdir is failed");
124                         return -1;
125                 }
126         }
127
128         snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
129
130         fd = open(filepath, O_RDONLY);
131         if (fd == -1) {
132                 _D("Failed to open");
133                 return -1;
134         }
135
136         cnt = read(fd, buf, PATH_MAX);
137         close(fd);
138
139         if (cnt <= 0) {
140                 /* Read /proc/<pid>/cmdline error */
141                 _D("Failed to read");
142                 return -1;
143         }
144
145         snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
146
147         fd = open(filepath, O_CREAT | O_WRONLY, 0644);
148         if (fd == -1) {
149                 _D("Failed to open");
150                 return -1;
151         }
152
153         if (write(fd, buf, cnt) == -1) {
154                 _D("Failed to write");
155                 close(fd);
156                 return -1;
157         }
158         close(fd);
159
160         return 0;
161 }
162
163 API int system_conf_set_vip(int pid)
164 {
165         char buf[BUFF_MAX];
166         int fd;
167         int r;
168
169         if (pid < 1)
170                 return -1;
171
172         if (access(VIP_DIR, R_OK) < 0) {
173                 _D("no predefined matrix dir = %s, so created", VIP_DIR);
174                 r = mkdir(VIP_DIR, 0777);
175                 if(r < 0) {
176                         _E("sysconf_set_vip vip mkdir is failed");
177                         return -1;
178                 }
179         }
180
181         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
182         fd = open(buf, O_CREAT | O_RDWR, 0644);
183         if (fd < 0) {
184                 _E("sysconf_set_vip fd open failed");
185                 return -1;
186         }
187         close(fd);
188         if (util_process_group_set(PROCESS_VIP, pid) < 0) {
189                 _E("set vip failed");
190                 return -1;
191         }
192
193         return 0;
194 }
195
196 API int system_conf_is_vip(int pid)
197 {
198         if (pid < 1)
199                 return -1;
200
201         char buf[BUFF_MAX];
202
203         snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
204
205         if (access(buf, R_OK) == 0)
206                 return 1;
207         else
208                 return 0;
209 }
210
211 API int system_conf_set_permanent_bypid(int pid)
212 {
213         int fd;
214         if (already_permanent(pid))
215                 goto MEMPOL_SET;
216
217         if (copy_cmdline(pid) < 0)
218                 return -1;
219
220         if (util_process_group_set(PROCESS_PERMANENT, pid) < 0) {
221                 _E("set vip failed");
222                 return -1;
223         }
224
225  MEMPOL_SET:
226         util_oomadj_set(pid, -17);
227
228         return 0;
229 }
230
231 API int system_conf_set_permanent()
232 {
233         pid_t pid = getpid();
234         return system_conf_set_permanent_bypid(pid);
235 }