Tizen 2.1 base
[platform/core/system/libslp-sysman.git] / set_pmon / set_pmon.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 <errno.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <ctype.h>
23 #include <sysman.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #define BUFF_MAX 255
30
31 int set_oomadj(int pid, int oomadj_val)
32 {
33         char buf[BUFF_MAX];
34         FILE *fp;
35
36         snprintf(buf, BUFF_MAX, "/proc/%d/oom_adj", pid);
37         fp = fopen(buf, "w");
38         if (fp == NULL)
39                 return -1;
40         fprintf(fp, "%d", oomadj_val);
41         fclose(fp);
42
43         return 0;
44 }
45
46 int check_and_setpmon(int pid, int idx, char **argv, char type)
47 {
48         int fd;
49         char buf[BUFF_MAX];
50         char *filename;
51
52         snprintf(buf, BUFF_MAX, "/proc/%d/cmdline", pid);
53         fd = open(buf, O_RDONLY);
54         if (fd < 0)
55                 return -1;
56
57         read(fd, buf, BUFF_MAX-1);
58         close(fd);
59
60         buf[BUFF_MAX-1] = '\0';
61
62         if (type == 'v')
63                 set_oomadj(pid, -17);
64
65         filename = strrchr(buf, '/');
66         if (filename == NULL)
67                 filename = buf;
68         else
69                 filename = filename + 1;
70
71         while (idx > 1) {       /* argv[1] is monitoring type */
72                 if (!strcmp(argv[idx], filename)) {
73                         switch (type) {
74                         case 'v':
75                                 printf("====> found, %s - set vip\n",
76                                        argv[idx]);
77                                 sysconf_set_vip(pid);
78                                 return 0;
79                         case 'p':
80                                 printf("====> found, %s - set permanent\n",
81                                        argv[idx]);
82                                 sysconf_set_permanent_bypid(pid);
83                                 return 0;
84                         default:
85                                 break;
86                         }
87                 }
88                 idx--;
89         }
90         return -1;
91 }
92
93 int main(int argc, char **argv)
94 {
95         int pid = -1;
96         char type;
97         DIR *dp;
98         struct dirent *dentry;
99
100         if (argc < 3 ||
101             argv[1][0] != '-' ||
102             argv[1][1] == '\0' || (argv[1][1] != 'v' && argv[1][1] != 'p')) {
103                 printf("Usage: %s <monitoring type> <process name>\n", argv[0]);
104                 printf("Monitoring types: -v \t VIP process\n");
105                 printf("                  -p \t Permanent process\n");
106                 return 1;
107         }
108
109         dp = opendir("/proc");
110         if (!dp) {
111                 printf("open /proc : %s\n", strerror(errno));
112                 return 1;
113         }
114         type = argv[1][1];
115
116         while ((dentry = readdir(dp)) != NULL) {
117                 if (!isdigit(dentry->d_name[0]))
118                         continue;
119
120                 pid = atoi(dentry->d_name);
121                 check_and_setpmon(pid, argc - 1, argv, type);
122         }
123
124         closedir(dp);
125         return 0;
126 }