]> 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 12a4657a0bc96baaa1f5982048a648183692c433..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);
@@ -101,7 +102,6 @@ out:
 int make_path(const char *path, mode_t mode)
 {
        char *dir, *delim;
-       struct stat sbuf;
        int rc = -1;
 
        delim = dir = strdup(path);
@@ -119,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\n",
-                                       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)
@@ -149,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;
+}