]>
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>
27 #define CGROUP2_FS_NAME "cgroup2"
29 /* if not already mounted cgroup2 is mounted here for iproute2's use */
30 #define MNT_CGRP2_PATH "/var/run/cgroup2"
32 /* return mount path of first occurrence of given fstype */
33 static char *find_fs_mount(const char *fs_to_find
)
36 char fstype
[128]; /* max length of any filesystem name */
40 fp
= fopen("/proc/mounts", "r");
43 "Failed to open mounts file: %s\n", strerror(errno
));
47 while (fscanf(fp
, "%*s %4096s %127s %*s %*d %*d\n",
49 if (strcmp(fstype
, fs_to_find
) == 0) {
60 /* caller needs to free string returned */
61 char *find_cgroup2_mount(void)
63 char *mnt
= find_fs_mount(CGROUP2_FS_NAME
);
68 mnt
= strdup(MNT_CGRP2_PATH
);
70 fprintf(stderr
, "Failed to allocate memory for cgroup2 path\n");
75 if (make_path(mnt
, 0755)) {
76 fprintf(stderr
, "Failed to setup vrf cgroup2 directory\n");
81 if (mount("none", mnt
, CGROUP2_FS_NAME
, 0, NULL
)) {
82 /* EBUSY means already mounted */
86 if (errno
== ENODEV
) {
88 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
91 "Failed to mount cgroup2: %s\n",
101 int make_path(const char *path
, mode_t mode
)
107 delim
= dir
= strdup(path
);
109 fprintf(stderr
, "strdup failed copying path");
113 /* skip '/' -- it had better exist */
118 delim
= strchr(delim
, '/');
122 if (stat(dir
, &sbuf
) != 0) {
123 if (errno
!= ENOENT
) {
125 "stat failed for %s: %s\n",
126 dir
, strerror(errno
));
130 if (mkdir(dir
, mode
) != 0) {
132 "mkdir failed for %s: %s\n",
133 dir
, strerror(errno
));