4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
29 #include "deviced-priv.h"
30 #include "dd-deviced.h"
32 #define PERMANENT_DIR "/tmp/permanent"
33 #define VIP_DIR "/tmp/vip"
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"
46 int util_oomadj_set(int pid, int oomadj_val)
51 snprintf(buf1, sizeof(buf1), "%d", pid);
52 snprintf(buf2, sizeof(buf2), "%d", oomadj_val);
53 return deviced_call_predef_action(OOMADJ_SET, 2, buf1, buf2);
56 int util_process_group_set(const char* name, int pid)
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);
66 snprintf(buf, sizeof(buf), "%d", pid);
67 _E("pid(%d) is inserted at vip", pid);
69 return deviced_call_predef_action(PROCESS_GROUP_SET, 2, buf, name);
72 API int deviced_conf_set_mempolicy_bypid(int pid, enum mem_policy mempol)
90 return util_oomadj_set(pid, oomadj_val);
93 API int deviced_conf_set_mempolicy(enum mem_policy mempol)
95 return deviced_conf_set_mempolicy_bypid(getpid(), mempol);
98 static int already_permanent(int pid)
102 snprintf(buf, BUFF_MAX, "%s/%d", PERMANENT_DIR, pid);
104 if (access(buf, R_OK) == 0) {
105 _D("already_permanent process : %d", pid);
111 static int copy_cmdline(int pid)
114 char filepath[PATH_MAX];
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);
123 _E("permanent directory mkdir is failed");
128 snprintf(filepath, PATH_MAX, "/proc/%d/cmdline", pid);
130 fd = open(filepath, O_RDONLY);
132 _D("Failed to open");
136 cnt = read(fd, buf, PATH_MAX);
140 /* Read /proc/<pid>/cmdline error */
141 _D("Failed to read");
145 snprintf(filepath, PATH_MAX, "%s/%d", PERMANENT_DIR, pid);
147 fd = open(filepath, O_CREAT | O_WRONLY, 0644);
149 _D("Failed to open");
153 if (write(fd, buf, cnt) == -1) {
154 _D("Failed to write");
163 API int deviced_conf_set_vip(int pid)
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);
176 _E("sysconf_set_vip vip mkdir is failed");
181 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
182 fd = open(buf, O_CREAT | O_RDWR, 0644);
184 _E("sysconf_set_vip fd open failed");
188 if (util_process_group_set(PROCESS_VIP, pid) < 0) {
189 _E("set vip failed");
196 API int deviced_conf_is_vip(int pid)
203 snprintf(buf, BUFF_MAX, "%s/%d", VIP_DIR, pid);
205 if (access(buf, R_OK) == 0)
211 API int deviced_conf_set_permanent_bypid(int pid)
214 if (already_permanent(pid))
217 if (copy_cmdline(pid) < 0)
220 if (util_process_group_set(PROCESS_PERMANENT, pid) < 0) {
221 _E("set vip failed");
226 util_oomadj_set(pid, -17);
231 API int deviced_conf_set_permanent()
233 pid_t pid = getpid();
234 return deviced_conf_set_permanent_bypid(pid);