4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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.
24 #include <sys/types.h>
38 #define PERMANENT_DIR "/tmp/permanent"
39 #define VIP_DIR "/tmp/vip"
43 * Opens "/proc/$pid/oom_score_adj" file for w/r;
44 * Return: FILE pointer or NULL
46 FILE * open_proc_oom_score_adj_file(int pid, const char *mode)
51 snprintf(buf, sizeof(buf), "/proc/%d/oom_score_adj", pid);
52 fp = fopen(buf, mode);
56 int get_exec_pid(const char *execpath)
59 struct dirent *dentry;
65 dp = opendir("/proc");
67 _E("FAIL: open /proc");
71 while ((dentry = readdir(dp)) != NULL) {
72 if (!isdigit(dentry->d_name[0]))
75 pid = atoi(dentry->d_name);
77 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
78 fd = open(buf, O_RDONLY);
81 ret = read(fd, buf2, PATH_MAX);
84 if (ret < 0 || ret >= PATH_MAX)
89 if (!strcmp(buf2, execpath)) {
100 int get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
103 char buf[PATH_MAX + 1];
106 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
107 fd = open(buf, O_RDONLY);
113 ret = read(fd, buf, PATH_MAX);
115 buf[PATH_MAX] = '\0';
117 filename = strrchr(buf, '/');
118 if (filename == NULL)
121 filename = filename + 1;
123 if (cmdline_size < strlen(filename) + 1) {
128 strncpy(cmdline, filename, cmdline_size - 1);
129 cmdline[cmdline_size - 1] = '\0';
140 snprintf(buf, PATH_MAX, "%s/%d", VIP_DIR, pid);
142 if (access(buf, R_OK) == 0)
148 static int remove_dir_internal(int fd)
157 while ((de = readdir(dir))) {
158 if (de->d_type == DT_DIR) {
159 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
161 subfd = openat(fd, de->d_name, O_RDONLY | O_DIRECTORY);
163 _SE("Couldn't openat %s: %s\n", de->d_name, strerror(errno));
167 if (remove_dir_internal(subfd)) {
171 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
172 _SE("Couldn't unlinkat %s: %s\n", de->d_name, strerror(errno));
176 if (unlinkat(fd, de->d_name, 0) < 0) {
177 _SE("Couldn't unlinkat %s: %s\n", de->d_name, strerror(errno));
186 int remove_dir(char *path, int del_dir)
192 fd = open(path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
194 _SE("Couldn't opendir %s: %s\n", path, strerror(errno));
197 ret = remove_dir_internal(fd);
202 _SE("Couldn't rmdir %s: %s\n", path, strerror(errno));
211 * - Read from sysfs entry
212 * - Write to sysfs entry
214 static int sys_read_buf(char *file, char *buf)
220 fd = open(file, O_RDONLY);
224 r = read(fd, buf, BUFF_MAX);
225 if ((r >= 0) && (r < BUFF_MAX))
235 static int sys_write_buf(char *file, char *buf)
241 fd = open(file, O_WRONLY);
245 r = write(fd, buf, strlen(buf));
254 int sys_get_int(char *fname, int *val)
259 if (sys_read_buf(fname, buf) == 0) {
269 int sys_set_int(char *fname, int val)
274 snprintf(buf, sizeof(buf), "%d", val);
276 if (sys_write_buf(fname, buf) != 0)
282 int sys_get_str(char *fname, char *str)
284 char buf[BUFF_MAX] = {0};
286 if (sys_read_buf(fname, buf) == 0) {
287 strncpy(str, buf, strlen(buf));
294 int sys_set_str(char *fname, char *val)
299 if (sys_write_buf(fname, val) == 0)
306 int terminate_process(char* partition, bool force)
308 const char *argv[7] = {"/sbin/fuser", "-m", "-k", "-S", NULL, NULL, NULL};
312 argv[4] = "-SIGKILL";
314 argv[4] = "-SIGTERM";
316 argc = sizeof(argv) / sizeof(argv[0]);
317 return run_child(argc, argv);
320 int mount_check(const char* path)
324 const char* table = "/etc/mtab";
327 fp = setmntent(table, "r");
330 while (mnt=getmntent(fp)) {
331 if (!strcmp(mnt->mnt_dir, path)) {
340 void print_time(const char *prefix)
344 gettimeofday(&tv, NULL);
345 tm = localtime(&(tv.tv_sec));
346 _D("%s --> %d:%02d:%02d %d",
347 prefix, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);