Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / lib / api / fs / cgroup.c
blob889a6eb4aaca7811e4dfe236a51be2401ab91670
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/stringify.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "fs.h"
11 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
13 FILE *fp;
14 char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
15 char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
16 char *token, *saved_ptr = NULL;
18 fp = fopen("/proc/mounts", "r");
19 if (!fp)
20 return -1;
23 * in order to handle split hierarchy, we need to scan /proc/mounts
24 * and inspect every cgroupfs mount point to find one that has
25 * perf_event subsystem
27 path_v1[0] = '\0';
28 path_v2[0] = '\0';
30 while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
31 __stringify(PATH_MAX)"s %*d %*d\n",
32 mountpoint, type, tokens) == 3) {
34 if (!path_v1[0] && !strcmp(type, "cgroup")) {
36 token = strtok_r(tokens, ",", &saved_ptr);
38 while (token != NULL) {
39 if (subsys && !strcmp(token, subsys)) {
40 strcpy(path_v1, mountpoint);
41 break;
43 token = strtok_r(NULL, ",", &saved_ptr);
47 if (!path_v2[0] && !strcmp(type, "cgroup2"))
48 strcpy(path_v2, mountpoint);
50 if (path_v1[0] && path_v2[0])
51 break;
53 fclose(fp);
55 if (path_v1[0])
56 path = path_v1;
57 else if (path_v2[0])
58 path = path_v2;
59 else
60 return -1;
62 if (strlen(path) < maxlen) {
63 strcpy(buf, path);
64 return 0;
66 return -1;