]>
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>
28 #define CGROUP2_FS_NAME "cgroup2"
30 /* if not already mounted cgroup2 is mounted here for iproute2's use */
31 #define MNT_CGRP2_PATH "/var/run/cgroup2"
33 /* return mount path of first occurrence of given fstype */
34 static char *find_fs_mount(const char *fs_to_find
)
37 char fstype
[128]; /* max length of any filesystem name */
41 fp
= fopen("/proc/mounts", "r");
44 "Failed to open mounts file: %s\n", strerror(errno
));
48 while (fscanf(fp
, "%*s %4096s %127s %*s %*d %*d\n",
50 if (strcmp(fstype
, fs_to_find
) == 0) {
61 /* caller needs to free string returned */
62 char *find_cgroup2_mount(void)
64 char *mnt
= find_fs_mount(CGROUP2_FS_NAME
);
69 mnt
= strdup(MNT_CGRP2_PATH
);
71 fprintf(stderr
, "Failed to allocate memory for cgroup2 path\n");
76 if (make_path(mnt
, 0755)) {
77 fprintf(stderr
, "Failed to setup vrf cgroup2 directory\n");
82 if (mount("none", mnt
, CGROUP2_FS_NAME
, 0, NULL
)) {
83 /* EBUSY means already mounted */
87 if (errno
== ENODEV
) {
89 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
92 "Failed to mount cgroup2: %s\n",
102 int make_path(const char *path
, mode_t mode
)
108 delim
= dir
= strdup(path
);
110 fprintf(stderr
, "strdup failed copying path");
114 /* skip '/' -- it had better exist */
119 delim
= strchr(delim
, '/');
123 if (stat(dir
, &sbuf
) != 0) {
124 if (errno
!= ENOENT
) {
126 "stat failed for %s: %s\n",
127 dir
, strerror(errno
));
131 if (mkdir(dir
, mode
) != 0) {
133 "mkdir failed for %s: %s\n",
134 dir
, strerror(errno
));
154 int get_command_name(const char *pid
, char *comm
, size_t len
)
160 if (snprintf(path
, sizeof(path
),
161 "/proc/%s/status", pid
) >= sizeof(path
)) {
165 fp
= fopen(path
, "r");
170 while (fgets(line
, sizeof(line
), fp
)) {
173 name
= strstr(line
, "Name:");
178 while (isspace(*name
))
181 nl
= strchr(name
, '\n');
185 strncpy(comm
, name
, len
- 1);
186 comm
[len
- 1] = '\0';