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