]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/fs.c
lib/fs: Fix single return points for get_cgroup2_*
[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 <ctype.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <limits.h>
25
26 #include "utils.h"
27
28 #define CGROUP2_FS_NAME "cgroup2"
29
30 /* if not already mounted cgroup2 is mounted here for iproute2's use */
31 #define MNT_CGRP2_PATH "/var/run/cgroup2"
32
33 /* return mount path of first occurrence of given fstype */
34 static char *find_fs_mount(const char *fs_to_find)
35 {
36 char path[4096];
37 char fstype[128]; /* max length of any filesystem name */
38 char *mnt = NULL;
39 FILE *fp;
40
41 fp = fopen("/proc/mounts", "r");
42 if (!fp) {
43 fprintf(stderr,
44 "Failed to open mounts file: %s\n", strerror(errno));
45 return NULL;
46 }
47
48 while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
49 path, fstype) == 2) {
50 if (strcmp(fstype, fs_to_find) == 0) {
51 mnt = strdup(path);
52 break;
53 }
54 }
55
56 fclose(fp);
57
58 return mnt;
59 }
60
61 /* caller needs to free string returned */
62 char *find_cgroup2_mount(bool do_mount)
63 {
64 char *mnt = find_fs_mount(CGROUP2_FS_NAME);
65
66 if (mnt)
67 return mnt;
68
69 if (!do_mount) {
70 fprintf(stderr, "Failed to find cgroup2 mount\n");
71 return NULL;
72 }
73
74 mnt = strdup(MNT_CGRP2_PATH);
75 if (!mnt) {
76 fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
77 return NULL;
78
79 }
80
81 if (make_path(mnt, 0755)) {
82 fprintf(stderr, "Failed to setup cgroup2 directory\n");
83 free(mnt);
84 return NULL;
85 }
86
87 if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
88 /* EBUSY means already mounted */
89 if (errno == EBUSY)
90 goto out;
91
92 if (errno == ENODEV) {
93 fprintf(stderr,
94 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
95 } else {
96 fprintf(stderr,
97 "Failed to mount cgroup2: %s\n",
98 strerror(errno));
99 }
100 free(mnt);
101 return NULL;
102 }
103 out:
104 return mnt;
105 }
106
107 __u64 get_cgroup2_id(const char *path)
108 {
109 char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
110 struct file_handle *fhp = (struct file_handle *)fh_buf;
111 union {
112 __u64 id;
113 unsigned char bytes[sizeof(__u64)];
114 } cg_id = { .id = 0 };
115 char *mnt = NULL;
116 int mnt_fd = -1;
117 int mnt_id;
118
119 if (!path) {
120 fprintf(stderr, "Invalid cgroup2 path\n");
121 return 0;
122 }
123
124 fhp->handle_bytes = sizeof(__u64);
125 if (name_to_handle_at(AT_FDCWD, path, fhp, &mnt_id, 0) < 0) {
126 /* try at cgroup2 mount */
127
128 while (*path == '/')
129 path++;
130 if (*path == '\0') {
131 fprintf(stderr, "Invalid cgroup2 path\n");
132 goto out;
133 }
134
135 mnt = find_cgroup2_mount(false);
136 if (!mnt)
137 goto out;
138
139 mnt_fd = open(mnt, O_RDONLY);
140 if (mnt_fd < 0) {
141 fprintf(stderr, "Failed to open cgroup2 mount\n");
142 goto out;
143 }
144
145 fhp->handle_bytes = sizeof(__u64);
146 if (name_to_handle_at(mnt_fd, path, fhp, &mnt_id, 0) < 0) {
147 fprintf(stderr, "Failed to get cgroup2 ID: %s\n",
148 strerror(errno));
149 goto out;
150 }
151 }
152 if (fhp->handle_bytes != sizeof(__u64)) {
153 fprintf(stderr, "Invalid size of cgroup2 ID\n");
154 goto out;
155 }
156
157 memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64));
158
159 out:
160 if (mnt_fd >= 0)
161 close(mnt_fd);
162 free(mnt);
163
164 return cg_id.id;
165 }
166
167 #define FILEID_INO32_GEN 1
168
169 /* caller needs to free string returned */
170 char *get_cgroup2_path(__u64 id, bool full)
171 {
172 char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
173 struct file_handle *fhp = (struct file_handle *)fh_buf;
174 union {
175 __u64 id;
176 unsigned char bytes[sizeof(__u64)];
177 } cg_id = { .id = id };
178 int mnt_fd = -1, fd = -1;
179 char link_buf[PATH_MAX];
180 char *path = NULL;
181 char fd_path[64];
182 int link_len;
183 char *mnt = NULL;
184
185 if (!id) {
186 fprintf(stderr, "Invalid cgroup2 ID\n");
187 goto out;
188 }
189
190 mnt = find_cgroup2_mount(false);
191 if (!mnt)
192 goto out;
193
194 mnt_fd = open(mnt, O_RDONLY);
195 if (mnt_fd < 0) {
196 fprintf(stderr, "Failed to open cgroup2 mount\n");
197 goto out;
198 }
199
200 fhp->handle_bytes = sizeof(__u64);
201 fhp->handle_type = FILEID_INO32_GEN;
202 memcpy(fhp->f_handle, cg_id.bytes, sizeof(__u64));
203
204 fd = open_by_handle_at(mnt_fd, fhp, 0);
205 if (fd < 0) {
206 fprintf(stderr, "Failed to open cgroup2 by ID\n");
207 goto out;
208 }
209
210 snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
211 link_len = readlink(fd_path, link_buf, sizeof(link_buf) - 1);
212 if (link_len < 0) {
213 fprintf(stderr,
214 "Failed to read value of symbolic link %s\n",
215 fd_path);
216 goto out;
217 }
218 link_buf[link_len] = '\0';
219
220 if (full)
221 path = strdup(link_buf);
222 else
223 path = strdup(link_buf + strlen(mnt));
224 if (!path)
225 fprintf(stderr,
226 "Failed to allocate memory for cgroup2 path\n");
227
228 out:
229 if (fd >= 0)
230 close(fd);
231 if (mnt_fd >= 0)
232 close(mnt_fd);
233 free(mnt);
234
235 return path;
236 }
237
238 int make_path(const char *path, mode_t mode)
239 {
240 char *dir, *delim;
241 int rc = -1;
242
243 delim = dir = strdup(path);
244 if (dir == NULL) {
245 fprintf(stderr, "strdup failed copying path");
246 return -1;
247 }
248
249 /* skip '/' -- it had better exist */
250 if (*delim == '/')
251 delim++;
252
253 while (1) {
254 delim = strchr(delim, '/');
255 if (delim)
256 *delim = '\0';
257
258 rc = mkdir(dir, mode);
259 if (rc && errno != EEXIST) {
260 fprintf(stderr, "mkdir failed for %s: %s\n",
261 dir, strerror(errno));
262 goto out;
263 }
264
265 if (delim == NULL)
266 break;
267
268 *delim = '/';
269 delim++;
270 if (*delim == '\0')
271 break;
272 }
273 rc = 0;
274 out:
275 free(dir);
276
277 return rc;
278 }
279
280 int get_command_name(const char *pid, char *comm, size_t len)
281 {
282 char path[PATH_MAX];
283 char line[128];
284 FILE *fp;
285
286 if (snprintf(path, sizeof(path),
287 "/proc/%s/status", pid) >= sizeof(path)) {
288 return -1;
289 }
290
291 fp = fopen(path, "r");
292 if (!fp)
293 return -1;
294
295 comm[0] = '\0';
296 while (fgets(line, sizeof(line), fp)) {
297 char *nl, *name;
298
299 name = strstr(line, "Name:");
300 if (!name)
301 continue;
302
303 name += 5;
304 while (isspace(*name))
305 name++;
306
307 nl = strchr(name, '\n');
308 if (nl)
309 *nl = '\0';
310
311 strlcpy(comm, name, len);
312 break;
313 }
314
315 fclose(fp);
316
317 return 0;
318 }