tizen 2.3 release
[kernel/api/system-resource.git] / src / vip-agent / vip-process.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 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 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 #include <Ecore.h>
32
33 #include "const.h"
34 #include "resourced.h"
35 #include "trace.h"
36 #include "proc-main.h"
37 #include "cgroup.h"
38 #include "proc-process.h"
39 #include "macro.h"
40 #include "config-parser.h"
41 #include "file-helper.h"
42 #include "module.h"
43 #include "module-data.h"
44 #include "vip-process.h"
45
46 #define VIP_CONF_FILE           "/etc/resourced/vip-process.conf"
47 #define VIP_CONF_SECTION        "VIP_PROCESS"
48 #define VIP_CONF_NAME           "VIP_PROC_NAME"
49 #define AGENT_PATH              "/usr/bin/vip-release-agent"
50 #define VIP_RELEASE_AGENT       "/release_agent"
51 #define VIP_NOTIFY_ON_RELEASE   "/notify_on_release"
52
53
54 static int load_vip_config(struct parse_result *result, void *user_data)
55 {
56         pid_t pid = 0;
57         char cgroup_name[MAX_NAME_LENGTH];
58         if (!result)
59                 return -EINVAL;
60
61         if (strcmp(result->section, VIP_CONF_SECTION))
62                 return RESOURCED_ERROR_NONE;
63
64         if (!strcmp(result->name, VIP_CONF_NAME)) {
65                 /* 1. find pid */
66                 /* 2. decrease oom score adj for excepting it in oom candidate list */
67                 /* 3. make cgroup */
68                 pid =  find_pid_from_cmdline(result->value);
69                 if (pid) {
70                         make_cgroup_subdir(VIP_CGROUP, result->value, NULL);
71                         snprintf(cgroup_name, sizeof(cgroup_name), "%s/%s",
72                                     VIP_CGROUP, result->value);
73                         cgroup_write_node(cgroup_name, TASK_FILE_NAME, pid);
74                 }
75         }
76         return RESOURCED_ERROR_NONE;
77 }
78
79 static int resourced_vip_process_init(void *data)
80 {
81         int checkfd;
82         checkfd = open(CHECK_RELEASE_PROGRESS, O_RDONLY, 0666);
83         if (checkfd >= 0)
84         {
85                 if (unlink(CHECK_RELEASE_PROGRESS) < 0)
86                         _E("fail to remove %s file\n", CHECK_RELEASE_PROGRESS);
87                 close(checkfd);
88         }
89         make_cgroup_subdir(DEFAULT_CGROUP, "vip", NULL);
90         mount_cgroup_subsystem("vip_cgroup", VIP_CGROUP, "none,name=vip_cgroup");
91         cgroup_write_node_str(VIP_CGROUP, VIP_RELEASE_AGENT, AGENT_PATH);
92         cgroup_write_node(VIP_CGROUP, VIP_NOTIFY_ON_RELEASE , 1);
93         config_parse(VIP_CONF_FILE, load_vip_config, NULL);
94         return RESOURCED_ERROR_NONE;
95 }
96
97 static int resourced_vip_process_finalize(void *data)
98 {
99         return RESOURCED_ERROR_NONE;
100 }
101
102 static struct module_ops vip_modules_ops = {
103         .priority = MODULE_PRIORITY_NORMAL,
104         .name = "vip-process",
105         .init = resourced_vip_process_init,
106         .exit = resourced_vip_process_finalize,
107 };
108
109 MODULE_REGISTER(&vip_modules_ops)