xref: /openbmc/linux/tools/lib/api/fs/cgroup.c (revision add48ba4)
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"
10 
11 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
12 {
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;
17 
18 	fp = fopen("/proc/mounts", "r");
19 	if (!fp)
20 		return -1;
21 
22 	/*
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
26 	 */
27 	path_v1[0] = '\0';
28 	path_v2[0] = '\0';
29 
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) {
33 
34 		if (!path_v1[0] && !strcmp(type, "cgroup")) {
35 
36 			token = strtok_r(tokens, ",", &saved_ptr);
37 
38 			while (token != NULL) {
39 				if (subsys && !strcmp(token, subsys)) {
40 					strcpy(path_v1, mountpoint);
41 					break;
42 				}
43 				token = strtok_r(NULL, ",", &saved_ptr);
44 			}
45 		}
46 
47 		if (!path_v2[0] && !strcmp(type, "cgroup2"))
48 			strcpy(path_v2, mountpoint);
49 
50 		if (path_v1[0] && path_v2[0])
51 			break;
52 	}
53 	fclose(fp);
54 
55 	if (path_v1[0])
56 		path = path_v1;
57 	else if (path_v2[0])
58 		path = path_v2;
59 	else
60 		return -1;
61 
62 	if (strlen(path) < maxlen) {
63 		strcpy(buf, path);
64 		return 0;
65 	}
66 	return -1;
67 }
68