2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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.
25 #include <sys/types.h>
26 #include "cl_cgroup.h"
28 #define CL_CGROOT "/sys/fs/cgroup/"
30 static char *cl_cgroup_base(cl_cgroup_t cgroup) {
31 switch (cgroup) { // LCOV_EXCL_BR_LINE 200: internal interface,code make sure
32 case CL_CGROUP_MEMORY:
33 return CL_CGROOT "/memory";
35 return CL_CGROOT "/cpu";
37 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
39 return NULL;// LCOV_EXCL_LINE 200:internal interface,code make sure nerver run
42 static char *cl_cgroup_create_path(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler) {
43 char *path = malloc(FILENAME_MAX);
45 if (path == NULL) { // LCOV_EXCL_BR_LINE 5:fail safe for libc malloc
46 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
48 errno = ENOMEM; // LCOV_EXCL_LINE 5:fail safe for libc malloc
49 goto error; // LCOV_EXCL_LINE 5:fail safe for libc malloc
52 if (cgroup != CL_CGROUP_MEMORY && cgroup != CL_CGROUP_CPU) { // LCOV_EXCL_BR_LINE 200: internal interface,code make sure
53 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
55 errno = EINVAL; // LCOV_EXCL_LINE 200: internal interface,code make sure
56 goto error; // LCOV_EXCL_LINE 200: internal interface,code make sure
59 if (cgroup_name == NULL) { // LCOV_EXCL_BR_LINE 200: internal interface,code make sure
60 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
62 errno = EINVAL; // LCOV_EXCL_LINE 200: internal interface,code make sure
63 goto error; // LCOV_EXCL_LINE 200: internal interface,code make sure
67 snprintf(path, FILENAME_MAX, "%s/%s/%s", cl_cgroup_base(cgroup), cgroup_name, controler);
69 snprintf(path, FILENAME_MAX, "%s/%s", cl_cgroup_base(cgroup), cgroup_name);
75 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
77 free(path); // LCOV_EXCL_LINE 200: internal interface,code make sure
78 return NULL; // LCOV_EXCL_LINE 200: internal interface,code make sure
81 int cl_cgroup_make(cl_cgroup_t cgroup, const char *cgroup_name) {
83 char *path = cl_cgroup_create_path(cgroup, cgroup_name, NULL);
85 if (path == NULL) { // LCOV_EXCL_BR_LINE 6: double check
86 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
88 goto exit; // LCOV_EXCL_LINE 6: double check
91 if (mkdir(path, 0777) < 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc mkdir
98 if (path) {// LCOV_EXCL_BR_LINE 6: double check
104 int cl_cgroup_remove(cl_cgroup_t cgroup, const char *cgroup_name) {
106 char *path = cl_cgroup_create_path(cgroup, cgroup_name, NULL);
108 if (path == NULL) { // LCOV_EXCL_BR_LINE 6: double check
109 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
111 goto exit; // LCOV_EXCL_LINE 6: double check
114 if (rmdir(path) < 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc rmdir
121 if (path) { // LCOV_EXCL_BR_LINE 6: double check
127 int cl_cgroup_exist(cl_cgroup_t cgroup, const char *cgroup_name) {
129 char *path = cl_cgroup_create_path(cgroup, cgroup_name, NULL);
131 if (path == NULL) { // LCOV_EXCL_BR_LINE 6: double check
132 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
134 goto exit; // LCOV_EXCL_LINE 6: double check
137 r = access(path, F_OK);
140 if (path) { // LCOV_EXCL_BR_LINE 6: double check
146 int cl_cgroup_open(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler, int flags) {
148 char *path = cl_cgroup_create_path(cgroup, cgroup_name, controler);
151 if (path == NULL) { // LCOV_EXCL_BR_LINE 6: double check
152 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
154 goto exit; // LCOV_EXCL_LINE 6: double check
157 if (controler == NULL) { // LCOV_EXCL_BR_LINE 6: double check
158 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
160 errno = EINVAL; // LCOV_EXCL_LINE 6: double check
161 goto exit; // LCOV_EXCL_LINE 6: double check
164 if ((fd = open(path, flags | O_CLOEXEC)) < 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc open
171 if (path) { // LCOV_EXCL_BR_LINE 6: double check
177 int cl_cgroup_set_string(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler, const char *string) {
181 if (controler == NULL || string == NULL) { // LCOV_EXCL_BR_LINE 6: double check
182 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
184 errno = EINVAL;// LCOV_EXCL_LINE 6: double check
185 goto exit;// LCOV_EXCL_LINE 6: double check
188 fd = cl_cgroup_open(cgroup, cgroup_name, controler, O_WRONLY);
189 if (fd < 0) { // LCOV_EXCL_BR_LINE 5: fail safe for glibc funtion open
193 if (write(fd, string, strlen(string)) < 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc write
200 if (fd >= 0) { // LCOV_EXCL_BR_LINE 5: fail safe for glibc funtion open
201 int save_err = errno;
208 int cl_cgroup_set_num(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler, int64_t value) {
211 snprintf(num_str, sizeof(num_str), "%" PRId64, value);
213 return cl_cgroup_set_string(cgroup, cgroup_name, controler, num_str);
216 int cl_cgroup_get_string(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler, char *string, size_t length) { // LCOV_EXCL_START 8: dead code // NOLINT (readability/nolint)
217 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
222 if (controler == NULL || string == NULL) {
227 fd = cl_cgroup_open(cgroup, cgroup_name, controler, O_RDONLY);
232 if (read(fd, string, length) < 0) {
240 int save_err = errno;
248 int64_t cl_cgroup_get_num(cl_cgroup_t cgroup, const char *cgroup_name, const char *controler) { // LCOV_EXCL_START 8: dead code // NOLINT (readability/nolint)
249 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
254 if ((r = cl_cgroup_get_string(cgroup, cgroup_name, controler, num_str, sizeof(num_str))) < 0) {
258 return atoll(num_str);