1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #define _GNU_SOURCE 4 5 #include <errno.h> 6 #include <fcntl.h> 7 #include <linux/limits.h> 8 #include <signal.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/stat.h> 13 #include <sys/types.h> 14 #include <sys/wait.h> 15 #include <unistd.h> 16 17 #include "cgroup_util.h" 18 19 static ssize_t read_text(const char *path, char *buf, size_t max_len) 20 { 21 ssize_t len; 22 int fd; 23 24 fd = open(path, O_RDONLY); 25 if (fd < 0) 26 return fd; 27 28 len = read(fd, buf, max_len - 1); 29 if (len < 0) 30 goto out; 31 32 buf[len] = 0; 33 out: 34 close(fd); 35 return len; 36 } 37 38 static ssize_t write_text(const char *path, char *buf, ssize_t len) 39 { 40 int fd; 41 42 fd = open(path, O_WRONLY | O_APPEND); 43 if (fd < 0) 44 return fd; 45 46 len = write(fd, buf, len); 47 if (len < 0) { 48 close(fd); 49 return len; 50 } 51 52 close(fd); 53 54 return len; 55 } 56 57 char *cg_name(const char *root, const char *name) 58 { 59 size_t len = strlen(root) + strlen(name) + 2; 60 char *ret = malloc(len); 61 62 snprintf(ret, len, "%s/%s", root, name); 63 64 return ret; 65 } 66 67 char *cg_name_indexed(const char *root, const char *name, int index) 68 { 69 size_t len = strlen(root) + strlen(name) + 10; 70 char *ret = malloc(len); 71 72 snprintf(ret, len, "%s/%s_%d", root, name, index); 73 74 return ret; 75 } 76 77 char *cg_control(const char *cgroup, const char *control) 78 { 79 size_t len = strlen(cgroup) + strlen(control) + 2; 80 char *ret = malloc(len); 81 82 snprintf(ret, len, "%s/%s", cgroup, control); 83 84 return ret; 85 } 86 87 int cg_read(const char *cgroup, const char *control, char *buf, size_t len) 88 { 89 char path[PATH_MAX]; 90 91 snprintf(path, sizeof(path), "%s/%s", cgroup, control); 92 93 if (read_text(path, buf, len) >= 0) 94 return 0; 95 96 return -1; 97 } 98 99 int cg_read_strcmp(const char *cgroup, const char *control, 100 const char *expected) 101 { 102 size_t size; 103 char *buf; 104 int ret; 105 106 /* Handle the case of comparing against empty string */ 107 if (!expected) 108 size = 32; 109 else 110 size = strlen(expected) + 1; 111 112 buf = malloc(size); 113 if (!buf) 114 return -1; 115 116 if (cg_read(cgroup, control, buf, size)) { 117 free(buf); 118 return -1; 119 } 120 121 ret = strcmp(expected, buf); 122 free(buf); 123 return ret; 124 } 125 126 int cg_read_strstr(const char *cgroup, const char *control, const char *needle) 127 { 128 char buf[PAGE_SIZE]; 129 130 if (cg_read(cgroup, control, buf, sizeof(buf))) 131 return -1; 132 133 return strstr(buf, needle) ? 0 : -1; 134 } 135 136 long cg_read_long(const char *cgroup, const char *control) 137 { 138 char buf[128]; 139 140 if (cg_read(cgroup, control, buf, sizeof(buf))) 141 return -1; 142 143 return atol(buf); 144 } 145 146 long cg_read_key_long(const char *cgroup, const char *control, const char *key) 147 { 148 char buf[PAGE_SIZE]; 149 char *ptr; 150 151 if (cg_read(cgroup, control, buf, sizeof(buf))) 152 return -1; 153 154 ptr = strstr(buf, key); 155 if (!ptr) 156 return -1; 157 158 return atol(ptr + strlen(key)); 159 } 160 161 int cg_write(const char *cgroup, const char *control, char *buf) 162 { 163 char path[PATH_MAX]; 164 ssize_t len = strlen(buf); 165 166 snprintf(path, sizeof(path), "%s/%s", cgroup, control); 167 168 if (write_text(path, buf, len) == len) 169 return 0; 170 171 return -1; 172 } 173 174 int cg_find_unified_root(char *root, size_t len) 175 { 176 char buf[10 * PAGE_SIZE]; 177 char *fs, *mount, *type; 178 const char delim[] = "\n\t "; 179 180 if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0) 181 return -1; 182 183 /* 184 * Example: 185 * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0 186 */ 187 for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) { 188 mount = strtok(NULL, delim); 189 type = strtok(NULL, delim); 190 strtok(NULL, delim); 191 strtok(NULL, delim); 192 strtok(NULL, delim); 193 194 if (strcmp(fs, "cgroup") == 0 && 195 strcmp(type, "cgroup2") == 0) { 196 strncpy(root, mount, len); 197 return 0; 198 } 199 } 200 201 return -1; 202 } 203 204 int cg_create(const char *cgroup) 205 { 206 return mkdir(cgroup, 0644); 207 } 208 209 int cg_wait_for_proc_count(const char *cgroup, int count) 210 { 211 char buf[10 * PAGE_SIZE] = {0}; 212 int attempts; 213 char *ptr; 214 215 for (attempts = 10; attempts >= 0; attempts--) { 216 int nr = 0; 217 218 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf))) 219 break; 220 221 for (ptr = buf; *ptr; ptr++) 222 if (*ptr == '\n') 223 nr++; 224 225 if (nr >= count) 226 return 0; 227 228 usleep(100000); 229 } 230 231 return -1; 232 } 233 234 int cg_killall(const char *cgroup) 235 { 236 char buf[PAGE_SIZE]; 237 char *ptr = buf; 238 239 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf))) 240 return -1; 241 242 while (ptr < buf + sizeof(buf)) { 243 int pid = strtol(ptr, &ptr, 10); 244 245 if (pid == 0) 246 break; 247 if (*ptr) 248 ptr++; 249 else 250 break; 251 if (kill(pid, SIGKILL)) 252 return -1; 253 } 254 255 return 0; 256 } 257 258 int cg_destroy(const char *cgroup) 259 { 260 int ret; 261 262 retry: 263 ret = rmdir(cgroup); 264 if (ret && errno == EBUSY) { 265 cg_killall(cgroup); 266 usleep(100); 267 goto retry; 268 } 269 270 if (ret && errno == ENOENT) 271 ret = 0; 272 273 return ret; 274 } 275 276 int cg_enter(const char *cgroup, int pid) 277 { 278 char pidbuf[64]; 279 280 snprintf(pidbuf, sizeof(pidbuf), "%d", pid); 281 return cg_write(cgroup, "cgroup.procs", pidbuf); 282 } 283 284 int cg_enter_current(const char *cgroup) 285 { 286 char pidbuf[64]; 287 288 snprintf(pidbuf, sizeof(pidbuf), "%d", getpid()); 289 return cg_write(cgroup, "cgroup.procs", pidbuf); 290 } 291 292 int cg_run(const char *cgroup, 293 int (*fn)(const char *cgroup, void *arg), 294 void *arg) 295 { 296 int pid, retcode; 297 298 pid = fork(); 299 if (pid < 0) { 300 return pid; 301 } else if (pid == 0) { 302 char buf[64]; 303 304 snprintf(buf, sizeof(buf), "%d", getpid()); 305 if (cg_write(cgroup, "cgroup.procs", buf)) 306 exit(EXIT_FAILURE); 307 exit(fn(cgroup, arg)); 308 } else { 309 waitpid(pid, &retcode, 0); 310 if (WIFEXITED(retcode)) 311 return WEXITSTATUS(retcode); 312 else 313 return -1; 314 } 315 } 316 317 int cg_run_nowait(const char *cgroup, 318 int (*fn)(const char *cgroup, void *arg), 319 void *arg) 320 { 321 int pid; 322 323 pid = fork(); 324 if (pid == 0) { 325 char buf[64]; 326 327 snprintf(buf, sizeof(buf), "%d", getpid()); 328 if (cg_write(cgroup, "cgroup.procs", buf)) 329 exit(EXIT_FAILURE); 330 exit(fn(cgroup, arg)); 331 } 332 333 return pid; 334 } 335 336 int get_temp_fd(void) 337 { 338 return open(".", O_TMPFILE | O_RDWR | O_EXCL); 339 } 340 341 int alloc_pagecache(int fd, size_t size) 342 { 343 char buf[PAGE_SIZE]; 344 struct stat st; 345 int i; 346 347 if (fstat(fd, &st)) 348 goto cleanup; 349 350 size += st.st_size; 351 352 if (ftruncate(fd, size)) 353 goto cleanup; 354 355 for (i = 0; i < size; i += sizeof(buf)) 356 read(fd, buf, sizeof(buf)); 357 358 return 0; 359 360 cleanup: 361 return -1; 362 } 363 364 int alloc_anon(const char *cgroup, void *arg) 365 { 366 size_t size = (unsigned long)arg; 367 char *buf, *ptr; 368 369 buf = malloc(size); 370 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) 371 *ptr = 0; 372 373 free(buf); 374 return 0; 375 } 376 377 int is_swap_enabled(void) 378 { 379 char buf[PAGE_SIZE]; 380 const char delim[] = "\n"; 381 int cnt = 0; 382 char *line; 383 384 if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0) 385 return -1; 386 387 for (line = strtok(buf, delim); line; line = strtok(NULL, delim)) 388 cnt++; 389 390 return cnt > 1; 391 } 392 393 int set_oom_adj_score(int pid, int score) 394 { 395 char path[PATH_MAX]; 396 int fd, len; 397 398 sprintf(path, "/proc/%d/oom_score_adj", pid); 399 400 fd = open(path, O_WRONLY | O_APPEND); 401 if (fd < 0) 402 return fd; 403 404 len = dprintf(fd, "%d", score); 405 if (len < 0) { 406 close(fd); 407 return len; 408 } 409 410 close(fd); 411 return 0; 412 } 413 414 char proc_read_text(int pid, const char *item, char *buf, size_t size) 415 { 416 char path[PATH_MAX]; 417 418 snprintf(path, sizeof(path), "/proc/%d/%s", pid, item); 419 420 return read_text(path, buf, size); 421 } 422