selftests/firmware/fw_namespace.c: sanity check on initialization
PATCH 1/1 selftests/firmware/fw_namespace.c: sanity check on initialization
From: Luis Chamberlain <[email protected]>
The fw_namespace.c test runs in a pretty self contained environment. It can easily fail with false positive if the DUT does not have the /lib/firmware directory created though, and CI tests will use minimal guests which may not have the directory created. Although this can be fixed by the test runners, it is also easy to just ensure the directory is created by the test itself.
While at it, clarify that the test is expected to run in the same namespace as the first process, this will save folks trying to debug this test some time in terms of context. The mounted tmpfs later will use the same init namespace for some temporary testing for this test.
--- .../testing/selftests/firmware/fw_namespace.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+)
tools/testing/selftests/firmware/fw_namespace.c +37 -0
@@ -112,6 +112,40 @@ static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_exit (EXIT_SUCCESS ); } +static void verify_init_ns (void ) +{ +struct stat init_ns, self_ns; + +if (stat ("/proc/1/ns/mnt" , &init_ns) !=0 ) +die ("Failed to stat init mount namespace: %s\n" , +strerror (errno)); + +if (stat ("/proc/self/ns/mnt" , &self_ns) !=0 ) +die ("Failed to stat self mount namespace: %s\n" , +strerror (errno)); + +if (init_ns.st_ino != self_ns.st_ino) +die ("Test must run in init mount namespace\n" ); +} + +static void ensure_firmware_dir (void ) +{ +struct stat st; + +if (stat ("/lib/firmware" , &st) ==0 ) { +if (!S_ISDIR (st.st_mode)) +die ("/lib/firmware exists but is not a directory\n" ); +return ; + } + +if (errno !=ENOENT ) +die ("Failed to stat /lib/firmware: %s\n" ,strerror (errno)); + +if (mkdir ("/lib/firmware" ,0755 ) !=0 ) +die ("Failed to create /lib/firmware directory: %s\n" , +strerror (errno)); +} +int main (int argc,char **argv) {const char *fw_name ="test-firmware.bin" ;
@@ -119,6 +153,9 @@ int main(int argc, char **argv)if (argc !=2 )die ("usage: %s sys_path\n" , argv[0 ]); +verify_init_ns (); +ensure_firmware_dir (); +/* Mount tmpfs to /lib/firmware so we don't have to assume that it is writable for us.*/ if (mount ("test" ,"/lib/firmware" ,"tmpfs" ,0 ,NULL ) ==-1 )
Signature · 1 line
2.43.0
Quoted 9 lines · Luis R. Rodriguez
From: Luis Chamberlain <[email protected]>
The
fw_namespace.ctest runs in a pretty self contained environment. It can easily fail with false positive if the DUT does not have the/lib/firmwaredirectory created though, and CI tests will use minimal guests which may not have the directory created. Although this can be fixed by the test runners, it is also easy to just ensure the directory is created by the test itself.
Quoted 7 lines
While at it, clarify that the test is expected to run in the same namespace as the first process, this will save folks trying to debug this test some time in terms of context. The mounted tmpfs later will use the same init namespace for some temporary testing for this test.
Fixes 901cff7cb9614 ("firmware_loader: load files from the mount namespace of init")Signed-off-by Luis Chamberlain <[email protected]>
Quoted 62 lines
--- .../testing/selftests/firmware/fw_namespace.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+)tools/testing/selftests/firmware/fw_namespace.c +37
-0@@ -112,6 +112,40 @@ static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_exit (EXIT_SUCCESS ); } +static void verify_init_ns (void ) +{ +struct stat init_ns, self_ns; + +if (stat ("/proc/1/ns/mnt" , &init_ns) !=0 ) +die ("Failed to stat init mount namespace: %s\n" , +strerror (errno)); + +if (stat ("/proc/self/ns/mnt" , &self_ns) !=0 ) +die ("Failed to stat self mount namespace: %s\n" , +strerror (errno)); + +if (init_ns.st_ino != self_ns.st_ino) +die ("Test must run in init mount namespace\n" ); +} + +static void ensure_firmware_dir (void ) +{ +struct stat st; + +if (stat ("/lib/firmware" , &st) ==0 ) { +if (!S_ISDIR (st.st_mode)) +die ("/lib/firmware exists but is not a directory\n" ); +return ; + } + +if (errno !=ENOENT ) +die ("Failed to stat /lib/firmware: %s\n" ,strerror (errno)); + +if (mkdir ("/lib/firmware" ,0755 ) !=0 ) +die ("Failed to create /lib/firmware directory: %s\n" , +strerror (errno)); +} +int main (int argc,char **argv) {const char *fw_name ="test-firmware.bin" ;@@ -119,6 +153,9 @@ int main(int argc, char **argv)if (argc !=2 )die ("usage: %s sys_path\n" , argv[0 ]); +verify_init_ns (); +ensure_firmware_dir (); +/* Mount tmpfs to /lib/firmware so we don't have to assume that it is writable for us.*/ if (mount ("test" ,"/lib/firmware" ,"tmpfs" ,0 ,NULL ) ==-1 )Signature · 1 line
2.43.0