Debian Codegraph @

lib/fs.c

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