]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/fs.c
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: David Ahern <dsa@cumulusnetworks.com>
13 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/mount.h>
24 #include <bsd/string.h>
31 #define CGROUP2_FS_NAME "cgroup2"
33 /* if not already mounted cgroup2 is mounted here for iproute2's use */
34 #define MNT_CGRP2_PATH "/var/run/cgroup2"
36 /* return mount path of first occurrence of given fstype */
37 static char *find_fs_mount(const char *fs_to_find
)
40 char fstype
[128]; /* max length of any filesystem name */
44 fp
= fopen("/proc/mounts", "r");
47 "Failed to open mounts file: %s\n", strerror(errno
));
51 while (fscanf(fp
, "%*s %4095s %127s %*s %*d %*d\n",
53 if (strcmp(fstype
, fs_to_find
) == 0) {
64 /* caller needs to free string returned */
65 char *find_cgroup2_mount(void)
67 char *mnt
= find_fs_mount(CGROUP2_FS_NAME
);
72 mnt
= strdup(MNT_CGRP2_PATH
);
74 fprintf(stderr
, "Failed to allocate memory for cgroup2 path\n");
79 if (make_path(mnt
, 0755)) {
80 fprintf(stderr
, "Failed to setup vrf cgroup2 directory\n");
85 if (mount("none", mnt
, CGROUP2_FS_NAME
, 0, NULL
)) {
86 /* EBUSY means already mounted */
90 if (errno
== ENODEV
) {
92 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
95 "Failed to mount cgroup2: %s\n",
105 int make_path(const char *path
, mode_t mode
)
110 delim
= dir
= strdup(path
);
112 fprintf(stderr
, "strdup failed copying path");
116 /* skip '/' -- it had better exist */
121 delim
= strchr(delim
, '/');
125 rc
= mkdir(dir
, mode
);
126 if (mkdir(dir
, mode
) != 0 && errno
!= EEXIST
) {
127 fprintf(stderr
, "mkdir failed for %s: %s\n",
128 dir
, strerror(errno
));
147 int get_command_name(const char *pid
, char *comm
, size_t len
)
153 if (snprintf(path
, sizeof(path
),
154 "/proc/%s/status", pid
) >= sizeof(path
)) {
158 fp
= fopen(path
, "r");
163 while (fgets(line
, sizeof(line
), fp
)) {
166 name
= strstr(line
, "Name:");
171 while (isspace(*name
))
174 nl
= strchr(name
, '\n');
178 strlcpy(comm
, name
, len
);