]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/fs.c
ip: add support for seg6local End.BPF action
[mirror_iproute2.git] / lib / fs.c
index 39cc96dccca975cadabba43965b90ac3bcc7ec46..86efd4ed2ed8084ad147b5d53e62ac2458ccf289 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/mount.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -44,7 +45,7 @@ static char *find_fs_mount(const char *fs_to_find)
                return NULL;
        }
 
-       while (fscanf(fp, "%*s %4096s %127s %*s %*d %*d\n",
+       while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
                      path, fstype) == 2) {
                if (strcmp(fstype, fs_to_find) == 0) {
                        mnt = strdup(path);
@@ -80,20 +81,27 @@ char *find_cgroup2_mount(void)
 
        if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
                /* EBUSY means already mounted */
-               if (errno != EBUSY) {
+               if (errno == EBUSY)
+                       goto out;
+
+               if (errno == ENODEV) {
                        fprintf(stderr,
                                "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
-                       free(mnt);
-                       return NULL;
+               } else {
+                       fprintf(stderr,
+                               "Failed to mount cgroup2: %s\n",
+                               strerror(errno));
                }
+               free(mnt);
+               return NULL;
        }
+out:
        return mnt;
 }
 
 int make_path(const char *path, mode_t mode)
 {
        char *dir, *delim;
-       struct stat sbuf;
        int rc = -1;
 
        delim = dir = strdup(path);
@@ -111,20 +119,11 @@ int make_path(const char *path, mode_t mode)
                if (delim)
                        *delim = '\0';
 
-               if (stat(dir, &sbuf) != 0) {
-                       if (errno != ENOENT) {
-                               fprintf(stderr,
-                                       "stat failed for %s: %s\n",
-                                       dir, strerror(errno));
-                               goto out;
-                       }
-
-                       if (mkdir(dir, mode) != 0) {
-                               fprintf(stderr,
-                                       "mkdir failed for %s: %s",
-                                       dir, strerror(errno));
-                               goto out;
-                       }
+               rc = mkdir(dir, mode);
+               if (mkdir(dir, mode) != 0 && errno != EEXIST) {
+                       fprintf(stderr, "mkdir failed for %s: %s\n",
+                               dir, strerror(errno));
+                       goto out;
                }
 
                if (delim == NULL)
@@ -141,3 +140,43 @@ out:
 
        return rc;
 }
+
+int get_command_name(const char *pid, char *comm, size_t len)
+{
+       char path[PATH_MAX];
+       char line[128];
+       FILE *fp;
+
+       if (snprintf(path, sizeof(path),
+                    "/proc/%s/status", pid) >= sizeof(path)) {
+               return -1;
+       }
+
+       fp = fopen(path, "r");
+       if (!fp)
+               return -1;
+
+       comm[0] = '\0';
+       while (fgets(line, sizeof(line), fp)) {
+               char *nl, *name;
+
+               name = strstr(line, "Name:");
+               if (!name)
+                       continue;
+
+               name += 5;
+               while (isspace(*name))
+                       name++;
+
+               nl = strchr(name, '\n');
+               if (nl)
+                       *nl = '\0';
+
+               strlcpy(comm, name, len);
+               break;
+       }
+
+       fclose(fp);
+
+       return 0;
+}