]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/fs.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / lib / fs.c
1 /*
2 * fs.c filesystem APIs
3 *
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.
8 *
9 * Authors: David Ahern <dsa@cumulusnetworks.com>
10 *
11 */
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <sys/mount.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #include "utils.h"
26
27 #define CGROUP2_FS_NAME "cgroup2"
28
29 /* if not already mounted cgroup2 is mounted here for iproute2's use */
30 #define MNT_CGRP2_PATH "/var/run/cgroup2"
31
32 /* return mount path of first occurrence of given fstype */
33 static char *find_fs_mount(const char *fs_to_find)
34 {
35 char path[4096];
36 char fstype[128]; /* max length of any filesystem name */
37 char *mnt = NULL;
38 FILE *fp;
39
40 fp = fopen("/proc/mounts", "r");
41 if (!fp) {
42 fprintf(stderr,
43 "Failed to open mounts file: %s\n", strerror(errno));
44 return NULL;
45 }
46
47 while (fscanf(fp, "%*s %4096s %127s %*s %*d %*d\n",
48 path, fstype) == 2) {
49 if (strcmp(fstype, fs_to_find) == 0) {
50 mnt = strdup(path);
51 break;
52 }
53 }
54
55 fclose(fp);
56
57 return mnt;
58 }
59
60 /* caller needs to free string returned */
61 char *find_cgroup2_mount(void)
62 {
63 char *mnt = find_fs_mount(CGROUP2_FS_NAME);
64
65 if (mnt)
66 return mnt;
67
68 mnt = strdup(MNT_CGRP2_PATH);
69 if (!mnt) {
70 fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
71 return NULL;
72
73 }
74
75 if (make_path(mnt, 0755)) {
76 fprintf(stderr, "Failed to setup vrf cgroup2 directory\n");
77 free(mnt);
78 return NULL;
79 }
80
81 if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
82 /* EBUSY means already mounted */
83 if (errno == EBUSY)
84 goto out;
85
86 if (errno == ENODEV) {
87 fprintf(stderr,
88 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
89 } else {
90 fprintf(stderr,
91 "Failed to mount cgroup2: %s\n",
92 strerror(errno));
93 }
94 free(mnt);
95 return NULL;
96 }
97 out:
98 return mnt;
99 }
100
101 int make_path(const char *path, mode_t mode)
102 {
103 char *dir, *delim;
104 struct stat sbuf;
105 int rc = -1;
106
107 delim = dir = strdup(path);
108 if (dir == NULL) {
109 fprintf(stderr, "strdup failed copying path");
110 return -1;
111 }
112
113 /* skip '/' -- it had better exist */
114 if (*delim == '/')
115 delim++;
116
117 while (1) {
118 delim = strchr(delim, '/');
119 if (delim)
120 *delim = '\0';
121
122 if (stat(dir, &sbuf) != 0) {
123 if (errno != ENOENT) {
124 fprintf(stderr,
125 "stat failed for %s: %s\n",
126 dir, strerror(errno));
127 goto out;
128 }
129
130 if (mkdir(dir, mode) != 0) {
131 fprintf(stderr,
132 "mkdir failed for %s: %s\n",
133 dir, strerror(errno));
134 goto out;
135 }
136 }
137
138 if (delim == NULL)
139 break;
140
141 *delim = '/';
142 delim++;
143 if (*delim == '\0')
144 break;
145 }
146 rc = 0;
147 out:
148 free(dir);
149
150 return rc;
151 }