]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib/bpf: Fix and simplify bpf_mnt_check_target()
authorAndrea Claudi <aclaudi@redhat.com>
Mon, 22 Feb 2021 17:43:10 +0000 (18:43 +0100)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 23 Feb 2021 02:19:01 +0000 (18:19 -0800)
As stated in commit ac3415f5c1b1 ("lib/fs: Fix and simplify make_path()"),
calling stat() before mkdir() is racey, because the entry might change in
between.

As the call to stat() seems to only check for target existence, we can
simply call mkdir() unconditionally and catch all errors but EEXIST.

Fixes: 95ae9a4870e7 ("bpf: fix mnt path when from env")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
lib/bpf_legacy.c

index bc869c3fec87831c9e8e770f236df8fba1d7331a..8a03b9c2ad37ce42e2f588c3221bd5464c2f8371 100644 (file)
@@ -510,20 +510,14 @@ static int bpf_mnt_fs(const char *target)
 
 static int bpf_mnt_check_target(const char *target)
 {
-       struct stat sb = {};
        int ret;
 
-       ret = stat(target, &sb);
-       if (ret) {
-               ret = mkdir(target, S_IRWXU);
-               if (ret) {
-                       fprintf(stderr, "mkdir %s failed: %s\n", target,
-                               strerror(errno));
-                       return ret;
-               }
-       }
+       ret = mkdir(target, S_IRWXU);
+       if (ret && errno != EEXIST)
+               fprintf(stderr, "mkdir %s failed: %s\n", target,
+                       strerror(errno));
 
-       return 0;
+       return ret;
 }
 
 static int bpf_valid_mntpt(const char *mnt, unsigned long magic)