Set STC_DEBUG_LOG flag
[platform/core/connectivity/stc-manager.git] / src / helper / helper-cgroup.c
1 /*
2  * Copyright (c) 2016 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 #include "helper-cgroup.h"
18
19 #define RELEASE_AGENT   "release_agent"
20 #define NOTIFY_ON_RELEASE  "notify_on_release"
21
22 #define CGROUP_FILE_NAME "cgroup.procs"
23 #define CLASSID_FILE_NAME "net_cls.classid"
24
25 #define MAX_PATH_LENGTH 512
26
27 static bool cgroup_is_exists(const char *cgroup_full_path)
28 {
29         struct stat stat_buf;
30         return stat(cgroup_full_path, &stat_buf) == 0;
31 }
32
33 static int cgroup_create(const char *cgroup_full_path)
34 {
35         if (mkdir(cgroup_full_path,
36                   S_IRUSR | S_IWUSR | S_IRGRP) < 0)
37                 return -errno;
38
39         return 0;
40 }
41
42 /*
43  * @desc place pid to cgroup.procs file
44  * @return 0 in case of success, errno value in case of failure
45  */
46 stc_error_e cgroup_write_pid_fullpath(const char *cgroup_full_path,
47                                       const int pid)
48 {
49         int ret;
50
51         if (pid <= 0) {
52                 STC_LOGE("try to write empty pid to %s", cgroup_full_path);
53                 return STC_ERROR_NO_DATA;
54         }
55
56         ret = cgroup_write_node_uint32(cgroup_full_path, CGROUP_FILE_NAME,
57                                        (uint32_t)pid);
58
59         ret_value_msg_if(ret < 0, STC_ERROR_FAIL,
60                          "Failed place all pid to cgroup %s", cgroup_full_path);
61         return STC_ERROR_NONE;
62 }
63
64 stc_error_e cgroup_write_pid(const char *cgroup_subsystem,
65                              const char *cgroup_name, const int pid)
66 {
67         char buf[MAX_PATH_LENGTH];
68         snprintf(buf, sizeof(buf), "%s/%s", cgroup_subsystem, cgroup_name);
69         return cgroup_write_pid_fullpath(buf, pid);
70 }
71
72 stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem,
73                                  const char *cgroup_name, const int pid)
74 {
75         char buf[MAX_PATH_LENGTH];
76
77         /*/proc/%d/task/%d/children */
78         char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)];
79         char pidbuf[MAX_DEC_SIZE(int)];
80         stc_error_e ret;
81
82         FILE *f;
83
84         snprintf(buf, sizeof(buf), "%s/%s", cgroup_subsystem, cgroup_name);
85         /* place parent */
86         ret = cgroup_write_pid_fullpath(buf, pid);
87         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
88                          "Failed to put parent process %d into %s cgroup",
89                          pid, cgroup_name);
90
91         snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN,
92                  pid, pid);
93         f = fopen(child_buf, "r");
94         ret_value_msg_if(!f, STC_ERROR_FAIL, "Failed to get child pids!");
95         while (fgets(pidbuf, sizeof(pidbuf), f) != NULL) {
96                 int child_pid = atoi(pidbuf);
97                 if (child_pid < 0) {
98                         STC_LOGE("Invalid child pid!");
99                         fclose(f);
100                         return STC_ERROR_FAIL;
101                 }
102                 stc_error_e ret = cgroup_write_pid_fullpath(buf, child_pid);
103                 if (ret != STC_ERROR_NONE) {
104                         STC_LOGE("Failed to put parent process %d into %s cgroup",
105                            pid, cgroup_name);
106                         fclose(f);
107                         return ret;
108                 }
109         }
110         fclose(f);
111         return STC_ERROR_NONE;
112 }
113
114 int cgroup_write_node_uint32(const char *cgroup_name,
115                              const char *file_name, uint32_t value)
116 {
117         char buf[MAX_PATH_LENGTH];
118         snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
119 #if STC_DEBUG_LOG
120         STC_LOGD("cgroup_buf %s, value %d\n", buf, value);
121 #endif
122         return fwrite_uint(buf, value);
123 }
124
125 int cgroup_write_node_str(const char *cgroup_name,
126                           const char *file_name, const char *string)
127 {
128         char buf[MAX_PATH_LENGTH];
129         snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
130 #if STC_DEBUG_LOG
131         STC_LOGD("cgroup_buf %s, string %s\n", buf, string);
132 #endif
133         return fwrite_str(buf, string);
134 }
135
136 int cgroup_read_node_uint32(const char *cgroup_name,
137                             const char *file_name, uint32_t *value)
138 {
139         char buf[MAX_PATH_LENGTH];
140         int ret;
141         snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
142         ret = fread_uint(buf, value);
143 #if STC_DEBUG_LOG
144         STC_LOGD("cgroup_buf %s, value %d\n", buf, *value);
145 #endif
146         return ret;
147 }
148
149 int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
150                        bool *already)
151 {
152         char buf[MAX_PATH_LENGTH];
153         bool cgroup_exists;
154         int ret = 0;
155
156         if (parentdir)
157                 ret = snprintf(buf, sizeof(buf), "%s/%s",
158                                parentdir, cgroup_name);
159         else
160                 ret = snprintf(buf, sizeof(buf), "%s",
161                                cgroup_name);
162
163         ret_value_msg_if(ret > sizeof(buf), STC_ERROR_FAIL,
164                          "Not enought buffer size for %s%s",
165                          parentdir, cgroup_name);
166
167         cgroup_exists = cgroup_is_exists(buf);
168         if (!cgroup_exists) {
169                 bool cgroup_remount = false;
170
171                 if (parentdir && !strncmp(parentdir, DEFAULT_CGROUP,
172                                           sizeof(DEFAULT_CGROUP))) {
173                         ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
174                                     MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
175                                     "mode=755");
176                         if (ret < 0) {
177                                 STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name);
178                                 return STC_ERROR_FAIL;
179                         }
180                         cgroup_remount = true;
181                 }
182
183                 ret = cgroup_create(buf);
184                 ret_value_msg_if(ret < 0, STC_ERROR_FAIL,
185                                  "Fail to create cgroup %s : err %d",
186                                  cgroup_name, errno);
187
188                 if (cgroup_remount) {
189                         ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
190                                     MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY,
191                                     "mode=755");
192                         if (ret < 0)
193                                 STC_LOGD("Fail to RO mount");
194                 }
195         }
196
197         if (already)
198                 *already = cgroup_exists;
199
200         return STC_ERROR_NONE;
201 }
202
203 int cgroup_set_release_agent(const char *cgroup_subsys,
204                              const char *release_agent)
205 {
206         _cleanup_free_ char *buf = NULL;
207         int r;
208
209         r = asprintf(&buf, "%s/%s", DEFAULT_CGROUP, cgroup_subsys);
210         if (r < 0)
211                 return -ENOMEM;
212
213         r = cgroup_write_node_str(buf, RELEASE_AGENT, release_agent);
214         if (r < 0)
215                 return r;
216
217         return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1");
218 }
219
220 void cgroup_init(void)
221 {
222         /* create stc cgroup directory */
223         cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL);
224
225         /* create background cgroup directory */
226         cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME,
227                            NULL);
228
229         cgroup_write_node_uint32(BACKGROUND_CGROUP_NETWORK,
230                 CLASSID_FILE_NAME, STC_BACKGROUND_APP_CLASSID);
231
232         /* create foreground cgroup directory */
233         cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME,
234                            NULL);
235 }