2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "helper-cgroup.h"
19 #define RELEASE_AGENT "release_agent"
20 #define NOTIFY_ON_RELEASE "notify_on_release"
22 #define CGROUP_FILE_NAME "cgroup.procs"
23 #define CLASSID_FILE_NAME "net_cls.classid"
25 #define MAX_PATH_LENGTH 512
27 static bool cgroup_is_exists(const char *cgroup_full_path)
30 return stat(cgroup_full_path, &stat_buf) == 0;
33 static int cgroup_create(const char *cgroup_full_path)
35 if (mkdir(cgroup_full_path,
36 S_IRUSR | S_IWUSR | S_IRGRP) < 0)
43 * @desc place pid to cgroup.procs file
44 * @return 0 in case of success, errno value in case of failure
46 stc_error_e cgroup_write_pid_fullpath(const char *cgroup_full_path,
52 STC_LOGE("try to write empty pid to %s", cgroup_full_path);
53 return STC_ERROR_NO_DATA;
56 ret = cgroup_write_node_uint32(cgroup_full_path, CGROUP_FILE_NAME,
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;
64 stc_error_e cgroup_write_pid(const char *cgroup_subsystem,
65 const char *cgroup_name, const int pid)
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);
72 stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem,
73 const char *cgroup_name, const int pid)
75 char buf[MAX_PATH_LENGTH];
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)];
84 snprintf(buf, sizeof(buf), "%s/%s", cgroup_subsystem, cgroup_name);
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",
91 snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN,
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);
98 STC_LOGE("Invalid child pid!");
100 return STC_ERROR_FAIL;
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",
111 return STC_ERROR_NONE;
114 int cgroup_write_node_uint32(const char *cgroup_name,
115 const char *file_name, uint32_t value)
117 char buf[MAX_PATH_LENGTH];
118 snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
119 STC_LOGD("cgroup_buf %s, value %d\n", buf, value);
120 return fwrite_uint(buf, value);
123 int cgroup_write_node_str(const char *cgroup_name,
124 const char *file_name, const char *string)
126 char buf[MAX_PATH_LENGTH];
127 snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
128 STC_LOGD("cgroup_buf %s, string %s\n", buf, string);
129 return fwrite_str(buf, string);
132 int cgroup_read_node_uint32(const char *cgroup_name,
133 const char *file_name, uint32_t *value)
135 char buf[MAX_PATH_LENGTH];
137 snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
138 ret = fread_uint(buf, value);
139 STC_LOGD("cgroup_buf %s, value %d\n", buf, *value);
143 int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
146 char buf[MAX_PATH_LENGTH];
151 ret = snprintf(buf, sizeof(buf), "%s/%s",
152 parentdir, cgroup_name);
154 ret = snprintf(buf, sizeof(buf), "%s",
157 ret_value_msg_if(ret > sizeof(buf), STC_ERROR_FAIL,
158 "Not enought buffer size for %s%s",
159 parentdir, cgroup_name);
161 cgroup_exists = cgroup_is_exists(buf);
162 if (!cgroup_exists) {
163 bool cgroup_remount = false;
165 if (parentdir && !strncmp(parentdir, DEFAULT_CGROUP,
166 sizeof(DEFAULT_CGROUP))) {
167 ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
168 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
171 STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name);
172 return STC_ERROR_FAIL;
174 cgroup_remount = true;
177 ret = cgroup_create(buf);
178 ret_value_msg_if(ret < 0, STC_ERROR_FAIL,
179 "Fail to create cgroup %s : err %d",
182 if (cgroup_remount) {
183 ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
184 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY,
187 STC_LOGD("Fail to RO mount");
192 *already = cgroup_exists;
194 return STC_ERROR_NONE;
197 int cgroup_set_release_agent(const char *cgroup_subsys,
198 const char *release_agent)
200 _cleanup_free_ char *buf = NULL;
203 r = asprintf(&buf, "%s/%s", DEFAULT_CGROUP, cgroup_subsys);
207 r = cgroup_write_node_str(buf, RELEASE_AGENT, release_agent);
211 return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1");
214 void cgroup_init(void)
216 /* create stc cgroup directory */
217 cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL);
219 /* create background cgroup directory */
220 cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME,
223 cgroup_write_node_uint32(BACKGROUND_CGROUP_NETWORK,
224 CLASSID_FILE_NAME, STC_BACKGROUND_APP_CLASSID);
226 /* create foreground cgroup directory */
227 cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME,