~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/api/fs/cgroup.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 struct cgroupfs_cache_entry {
 12         char    subsys[32];
 13         char    mountpoint[PATH_MAX];
 14 };
 15 
 16 /* just cache last used one */
 17 static struct cgroupfs_cache_entry *cached;
 18 
 19 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 20 {
 21         FILE *fp;
 22         char *line = NULL;
 23         size_t len = 0;
 24         char *p, *path;
 25         char mountpoint[PATH_MAX];
 26 
 27         if (cached && !strcmp(cached->subsys, subsys)) {
 28                 if (strlen(cached->mountpoint) < maxlen) {
 29                         strcpy(buf, cached->mountpoint);
 30                         return 0;
 31                 }
 32                 return -1;
 33         }
 34 
 35         fp = fopen("/proc/mounts", "r");
 36         if (!fp)
 37                 return -1;
 38 
 39         /*
 40          * in order to handle split hierarchy, we need to scan /proc/mounts
 41          * and inspect every cgroupfs mount point to find one that has
 42          * the given subsystem.  If we found v1, just use it.  If not we can
 43          * use v2 path as a fallback.
 44          */
 45         mountpoint[0] = '\0';
 46 
 47         /*
 48          * The /proc/mounts has the follow format:
 49          *
 50          *   <devname> <mount point> <fs type> <options> ...
 51          *
 52          */
 53         while (getline(&line, &len, fp) != -1) {
 54                 /* skip devname */
 55                 p = strchr(line, ' ');
 56                 if (p == NULL)
 57                         continue;
 58 
 59                 /* save the mount point */
 60                 path = ++p;
 61                 p = strchr(p, ' ');
 62                 if (p == NULL)
 63                         continue;
 64 
 65                 *p++ = '\0';
 66 
 67                 /* check filesystem type */
 68                 if (strncmp(p, "cgroup", 6))
 69                         continue;
 70 
 71                 if (p[6] == '2') {
 72                         /* save cgroup v2 path */
 73                         strcpy(mountpoint, path);
 74                         continue;
 75                 }
 76 
 77                 /* now we have cgroup v1, check the options for subsystem */
 78                 p += 7;
 79 
 80                 p = strstr(p, subsys);
 81                 if (p == NULL)
 82                         continue;
 83 
 84                 /* sanity check: it should be separated by a space or a comma */
 85                 if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
 86                         continue;
 87 
 88                 strcpy(mountpoint, path);
 89                 break;
 90         }
 91         free(line);
 92         fclose(fp);
 93 
 94         if (!cached)
 95                 cached = calloc(1, sizeof(*cached));
 96 
 97         if (cached) {
 98                 strncpy(cached->subsys, subsys, sizeof(cached->subsys) - 1);
 99                 strcpy(cached->mountpoint, mountpoint);
100         }
101 
102         if (mountpoint[0] && strlen(mountpoint) < maxlen) {
103                 strcpy(buf, mountpoint);
104                 return 0;
105         }
106         return -1;
107 }
108 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php