9
9
10
10
#define _GNU_SOURCE
11
11
#include <fcntl.h>
12
+ #include <libgen.h>
12
13
#include <linux/landlock.h>
13
14
#include <linux/magic.h>
14
15
#include <sched.h>
@@ -4624,7 +4625,6 @@ FIXTURE(layout3_fs)
4624
4625
{
4625
4626
bool has_created_dir ;
4626
4627
bool has_created_file ;
4627
- char * dir_path ;
4628
4628
bool skip_test ;
4629
4629
};
4630
4630
@@ -4683,11 +4683,24 @@ FIXTURE_VARIANT_ADD(layout3_fs, hostfs) {
4683
4683
.cwd_fs_magic = HOSTFS_SUPER_MAGIC ,
4684
4684
};
4685
4685
4686
+ static char * dirname_alloc (const char * path )
4687
+ {
4688
+ char * dup ;
4689
+
4690
+ if (!path )
4691
+ return NULL ;
4692
+
4693
+ dup = strdup (path );
4694
+ if (!dup )
4695
+ return NULL ;
4696
+
4697
+ return dirname (dup );
4698
+ }
4699
+
4686
4700
FIXTURE_SETUP (layout3_fs )
4687
4701
{
4688
4702
struct stat statbuf ;
4689
- const char * slash ;
4690
- size_t dir_len ;
4703
+ char * dir_path = dirname_alloc (variant -> file_path );
4691
4704
4692
4705
if (!supports_filesystem (variant -> mnt .type ) ||
4693
4706
!cwd_matches_fs (variant -> cwd_fs_magic )) {
@@ -4697,25 +4710,15 @@ FIXTURE_SETUP(layout3_fs)
4697
4710
4698
4711
_metadata -> teardown_parent = true;
4699
4712
4700
- slash = strrchr (variant -> file_path , '/' );
4701
- ASSERT_NE (slash , NULL );
4702
- dir_len = (size_t )slash - (size_t )variant -> file_path ;
4703
- ASSERT_LT (0 , dir_len );
4704
- self -> dir_path = malloc (dir_len + 1 );
4705
- self -> dir_path [dir_len ] = '\0' ;
4706
- strncpy (self -> dir_path , variant -> file_path , dir_len );
4707
-
4708
4713
prepare_layout_opt (_metadata , & variant -> mnt );
4709
4714
4710
4715
/* Creates directory when required. */
4711
- if (stat (self -> dir_path , & statbuf )) {
4716
+ if (stat (dir_path , & statbuf )) {
4712
4717
set_cap (_metadata , CAP_DAC_OVERRIDE );
4713
- EXPECT_EQ (0 , mkdir (self -> dir_path , 0700 ))
4718
+ EXPECT_EQ (0 , mkdir (dir_path , 0700 ))
4714
4719
{
4715
4720
TH_LOG ("Failed to create directory \"%s\": %s" ,
4716
- self -> dir_path , strerror (errno ));
4717
- free (self -> dir_path );
4718
- self -> dir_path = NULL ;
4721
+ dir_path , strerror (errno ));
4719
4722
}
4720
4723
self -> has_created_dir = true;
4721
4724
clear_cap (_metadata , CAP_DAC_OVERRIDE );
@@ -4736,6 +4739,8 @@ FIXTURE_SETUP(layout3_fs)
4736
4739
self -> has_created_file = true;
4737
4740
clear_cap (_metadata , CAP_DAC_OVERRIDE );
4738
4741
}
4742
+
4743
+ free (dir_path );
4739
4744
}
4740
4745
4741
4746
FIXTURE_TEARDOWN (layout3_fs )
@@ -4754,16 +4759,17 @@ FIXTURE_TEARDOWN(layout3_fs)
4754
4759
}
4755
4760
4756
4761
if (self -> has_created_dir ) {
4762
+ char * dir_path = dirname_alloc (variant -> file_path );
4763
+
4757
4764
set_cap (_metadata , CAP_DAC_OVERRIDE );
4758
4765
/*
4759
4766
* Don't check for error because the directory might already
4760
4767
* have been removed (cf. release_inode test).
4761
4768
*/
4762
- rmdir (self -> dir_path );
4769
+ rmdir (dir_path );
4763
4770
clear_cap (_metadata , CAP_DAC_OVERRIDE );
4771
+ free (dir_path );
4764
4772
}
4765
- free (self -> dir_path );
4766
- self -> dir_path = NULL ;
4767
4773
4768
4774
cleanup_layout (_metadata );
4769
4775
}
@@ -4830,7 +4836,10 @@ TEST_F_FORK(layout3_fs, tag_inode_dir_mnt)
4830
4836
4831
4837
TEST_F_FORK (layout3_fs , tag_inode_dir_child )
4832
4838
{
4833
- layer3_fs_tag_inode (_metadata , self , variant , self -> dir_path );
4839
+ char * dir_path = dirname_alloc (variant -> file_path );
4840
+
4841
+ layer3_fs_tag_inode (_metadata , self , variant , dir_path );
4842
+ free (dir_path );
4834
4843
}
4835
4844
4836
4845
TEST_F_FORK (layout3_fs , tag_inode_file )
@@ -4857,9 +4866,13 @@ TEST_F_FORK(layout3_fs, release_inodes)
4857
4866
if (self -> has_created_file )
4858
4867
EXPECT_EQ (0 , remove_path (variant -> file_path ));
4859
4868
4860
- if (self -> has_created_dir )
4869
+ if (self -> has_created_dir ) {
4870
+ char * dir_path = dirname_alloc (variant -> file_path );
4871
+
4861
4872
/* Don't check for error because of cgroup specificities. */
4862
- remove_path (self -> dir_path );
4873
+ remove_path (dir_path );
4874
+ free (dir_path );
4875
+ }
4863
4876
4864
4877
ruleset_fd =
4865
4878
create_ruleset (_metadata , LANDLOCK_ACCESS_FS_READ_DIR , layer1 );
0 commit comments