1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/stringify.h>
11 int cgroupfs_find_mountpoint(char *buf
, size_t maxlen
, const char *subsys
)
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");
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
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
);
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])
62 if (strlen(path
) < maxlen
) {