^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) char *token, *saved_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) fp = fopen("/proc/mounts", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * in order to handle split hierarchy, we need to scan /proc/mounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * and inspect every cgroupfs mount point to find one that has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * perf_event subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) path_v1[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) path_v2[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __stringify(PATH_MAX)"s %*d %*d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) mountpoint, type, tokens) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!path_v1[0] && !strcmp(type, "cgroup")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) token = strtok_r(tokens, ",", &saved_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) while (token != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (subsys && !strcmp(token, subsys)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) strcpy(path_v1, mountpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) token = strtok_r(NULL, ",", &saved_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!path_v2[0] && !strcmp(type, "cgroup2"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) strcpy(path_v2, mountpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (path_v1[0] && path_v2[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (path_v1[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) path = path_v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) else if (path_v2[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) path = path_v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (strlen(path) < maxlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) strcpy(buf, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }