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);
120 STC_LOGD("cgroup_buf %s, value %d\n", buf, value);
122 return fwrite_uint(buf, value);
125 int cgroup_write_node_str(const char *cgroup_name,
126 const char *file_name, const char *string)
128 char buf[MAX_PATH_LENGTH];
129 snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
131 STC_LOGD("cgroup_buf %s, string %s\n", buf, string);
133 return fwrite_str(buf, string);
136 int cgroup_read_node_uint32(const char *cgroup_name,
137 const char *file_name, uint32_t *value)
139 char buf[MAX_PATH_LENGTH];
141 snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
142 ret = fread_uint(buf, value);
144 STC_LOGD("cgroup_buf %s, value %d\n", buf, *value);
149 int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
152 char buf[MAX_PATH_LENGTH];
157 ret = snprintf(buf, sizeof(buf), "%s/%s",
158 parentdir, cgroup_name);
160 ret = snprintf(buf, sizeof(buf), "%s",
163 ret_value_msg_if(ret > sizeof(buf), STC_ERROR_FAIL,
164 "Not enought buffer size for %s%s",
165 parentdir, cgroup_name);
167 cgroup_exists = cgroup_is_exists(buf);
168 if (!cgroup_exists) {
169 bool cgroup_remount = false;
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,
177 STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name);
178 return STC_ERROR_FAIL;
180 cgroup_remount = true;
183 ret = cgroup_create(buf);
184 ret_value_msg_if(ret < 0, STC_ERROR_FAIL,
185 "Fail to create cgroup %s : err %d",
188 if (cgroup_remount) {
189 ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
190 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY,
193 STC_LOGD("Fail to RO mount");
198 *already = cgroup_exists;
200 return STC_ERROR_NONE;
203 int cgroup_set_release_agent(const char *cgroup_subsys,
204 const char *release_agent)
206 _cleanup_free_ char *buf = NULL;
209 r = asprintf(&buf, "%s/%s", DEFAULT_CGROUP, cgroup_subsys);
213 r = cgroup_write_node_str(buf, RELEASE_AGENT, release_agent);
217 return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1");
220 void cgroup_init(void)
222 /* create stc cgroup directory */
223 cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL);
225 /* create background cgroup directory */
226 cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME,
229 cgroup_write_node_uint32(BACKGROUND_CGROUP_NETWORK,
230 CLASSID_FILE_NAME, STC_BACKGROUND_APP_CLASSID);
232 /* create foreground cgroup directory */
233 cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME,