lib: Add library function for parsing kernel config
PATCH 1/1 lib: Add library function for parsing kernel config
This is meant as last resort action for disabling tests if certain kernel funcitonality was not present, in general case runtime checks are prefered.
For functionality that can be build as a module tst_check_driver() is most likely better fit since it will also insert requested kernel module into kernel if needed.
For newly added syscalls kernel version comparsion and/or checking errno is prefered.
However in rare cases certain core kernel functionality cannot be detected in any other way than checking the kernel config, which is where this API gets into the play.
The path to the kernel config could be specified by LTP_KCONFIG environment variable, which also takes precedence before the autodetection that attempts to read the config from known locations.
The required kernel options are passed as an array of strings via the .needs_kconfigs pointer in the tst_test structure. The purpose of this is twofold, one is that the test can disable itself at runtime if given functionality is missing from kernel .config and second is about being able to propagate this information to the testrunner (this could be done once we figure out how export the information from the structure to the test runner) then we can avoid running tests on unsuitable configurations from the start.
--- doc/test-writing-guidelines.txt | 32 +++++++ include/tst_kconfig.h | 34 ++++++++ include/tst_test.h | 6 ++ lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/tst_kconfig.c | 24 ++++++ lib/tst_kconfig.c | 183 ++++++++++++++++++++++++++++++++++++++++ lib/tst_test.c | 4 + 7 files changed, 284 insertions(+) create mode 100644 include/tst_kconfig.h create mode 100644 lib/newlib_tests/tst_kconfig.c create mode 100644 lib/tst_kconfig.c
doc/test-writing-guidelines.txt +32 -0
@@ -1504,6 +1504,38 @@ static struct tst_test test = { .save_restore = save_restore, }; +2.2.28 Parsing kernel .config +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Generally testcases should attempt to autodetect as much kernel features as +possible based on the currently running kernel. We do have tst_check_driver() +to check if functionality that could be compiled as kernel module is present +on the system, disabled syscalls can be detected by checking for 'ENOSYS' +errno etc. + +However in rare cases core kernel features couldn't be detected based on the +kernel userspace API and we have to resort on kernel .config parsing. + +For this cases the test should set the 'NULL' terminated needs_kconfig array +of kernel config options required for the test. The test will exit with +'TCONF' if any of the required options wasn't set to 'y' or 'm'. + +[source,c] +------------------------------------------------------------------------------- +#include "tst_test.h" + +static const char *kconfigs[] = { + "CONFIG_X86_INTEL_UMIP", + NULL +}; + +static struct tst_test test = { + ... + .needs_kconfigs = kconfigs, + ... +}; +------------------------------------------------------------------------------- + 2.3 Writing a testcase in shell ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_kconfig.h new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#ifndef TST_KCONFIG_H__ +#define TST_KCONFIG_H__ + +/* +* Parses kernel config given a CONFIG_FOO symbol. +* +* The location of the config is detected automatically in case that the config +* lives in one of the standard locations or can be set/overrided by setting +* LTP_KCONFIG environment variable. +* +* The functions returns: +* +* 'y' -- when compiled in +* 'm' -- when compiled as a module +* 'v' -- when set to some value e.g. CONFIG_BLK_DEV_RAM_COUNT=16 +* 'n' -- when not set +* 'u' -- when CONFIG_FOO wasn't present in CONFIG +* 'e' -- on error i.e. .config couldn't be located +*/ +char tst_kconfig (const char *id); + +/* +* Exits the test with TCONF on first config option that is not set to 'm' or 'y' +* +* @kconfigs: NULL terminated array of kernel config options such as "CONFIG_MMU" +*/ +void tst_kconfig_check (const char *const *kconfigs); + +#endif /* TST_KCONFIG_H__ */
include/tst_test.h +6 -0
@@ -182,6 +182,12 @@ struct tst_test {* before setup and restore after cleanup */ const char *const *save_restore; + +/* +* NULL terminated array of kernel config options required for the +* test. +*/ +const char *const *needs_kconfigs; };/*
lib/newlib_tests/.gitignore +1 -0
@@ -24,3 +24,4 @@ test19 tst_expiration_timer test_exec test_exec_child +tst_kconfig
lib/newlib_tests/tst_kconfig.c new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#include "tst_test.h" +#include "tst_kconfig.h" + +static void do_test (void ) +{ +tst_res (TPASS ,"Not reached!" ); +} + +static const char *kconfigs[] = { +"CONFIG_MMU" , +"CONFIG_EXT4_FS" , +/* Comment this to make the test run */ +"CONFIG_NONEXISTENT" , +NULL +}; + +static struct tst_test test = { + .test_all = do_test, + .needs_kconfigs = kconfigs, +};
lib/tst_kconfig.c new file +183 -0
183 lines not on this page · raw message
lib/tst_test.c +4 -0
@@ -36,6 +36,7 @@#include "tst_clocks.h" #include "tst_timer.h" #include "tst_sys_conf.h" +#include "tst_kconfig.h" #include "old_resource.h" #include "old_device.h"
@@ -770,6 +771,9 @@ static void do_setup(int argc, char *argv[])if (tst_test->tconf_msg)tst_brk (TCONF ,"%s" , tst_test->tconf_msg); +if (tst_test->needs_kconfigs) +tst_kconfig_check (tst_test->needs_kconfigs); +assert_test_fn (); tid =get_tid (argv);
Signature · 1 line
2.16.4
PATCH 1/1 lib: Add library function for parsing kernel config
Hi Cyril
Quoted 28 lines · Cyril Hrubis
This is meant as last resort action for disabling tests if certain kernel funcitonality was not present, in general case runtime checks are prefered.
For functionality that can be build as a module
tst_check_driver()is most likely better fit since it will also insert requested kernel module into kernel if needed.For newly added syscalls kernel version comparsion and/or checking errno is prefered.
However in rare cases certain core kernel functionality cannot be detected in any other way than checking the kernel config, which is where this API gets into the play.
The path to the kernel config could be specified by
LTP_KCONFIGenvironment variable, which also takes precedence before the autodetection that attempts to read the config from known locations.The required kernel options are passed as an array of strings via the .needs_kconfigs pointer in the
tst_teststructure. The purpose of this is twofold, one is that the test can disable itself at runtime if given functionality is missing from kernel .config and second is about being able to propagate this information to the testrunner (this could be done once we figure out how export the information from the structure to the test runner) then we can avoid running tests on unsuitable configurations from the start.
Quoted 4 lines
---
this morning I was just thinking about brutally zcatting config.gz to be able to skip some tests based upon current Kernel configs (not drivers/moduleoptions)...and I'd need a mechanism to do that...then I saw this patch :D
So I picked up your patch in my local tree for testing, but given that I would need all of the above checks inside a shell testcase, I tried building something on top of it.
FAR FROM BEING A FINAL PATCH...this adds the kconfig shell helper and my test case fix together...just to try...
(quoted diff) +0 -0
b/testcases/kernel/fs/quota_remount/quota_remount_test01.sh index 04b7af922..dd8f68ac4 100755
testcases/kernel/fs/quota_remount/quota_remount_test01.sh +5 -0
@@ -53,6 +53,11 @@ if tst_kvcmp -lt "2.6.25"; then exit 32 fi +if ! tst_check_kconfigs CONFIG_QFMT_V1 CONFIG_QFMT_V2; then + tst_resm TCONF "Kernel QUOTA Options NOT configured!" + exit 32 +fi + if [ ! -d /proc/sys/fs/quota ]; then tst_resm TCONF "Quota not supported in kernel!" exit 0
testcases/lib/Makefile +0 -0
@@ -28,6 +28,6 @@ INSTALL_TARGETS := *.sh MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod
tst_kvcmp\
tst_device tst_net_iface_prefixtst_net_ip_prefix tst_net_vars\
- tst_getconf tst_supported_fs tst_check_drivers
+ tst_getconf tst_supported_fstst_check_drivers tst_check_kconfigs
include $(top_srcdir)/include/mk/generic_leaf_target.mk
(quoted diff) +0 -0
b/testcases/lib/tst_check_kconfigs.c
new file mode 100644
index 000000000..71129e547
testcases/lib/tst_check_kconfigs.c new file +50 -0
50 lines not on this page · raw message
and it seems to work !
cmdline="quota_remount_test01.sh"
contacts=""
analysis=exit
<<<test_output>>>
incrementing stop
Check for: CONFIG_QFMT_V1
tst_check_kconfigs.c:44: INFO: Kernel is missing CONFIG_QFMT_V1quota_remount_test01 1 TCONF : ltpapicmd.c:188: Kernel QUOTA
Options NOT configured!
<<<execution_status>>>
initiation_status="ok"
duration=0 termination_type=exited termination_id=32 corefile=no cutime=0 cstime=0
Do you think could be useful ?
Or should be done another way really (I'm brand new to LTP tests...) ?
Thanks
Cristian
Quoted 363 lines
doc/test-writing-guidelines.txt | 32 +++++++ include/tst_kconfig.h | 34 ++++++++ include/tst_test.h | 6 ++ lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/tst_kconfig.c | 24 ++++++ lib/tst_kconfig.c | 183 ++++++++++++++++++++++++++++++++++++++++ lib/tst_test.c | 4 + 7 files changed, 284 insertions(+)create mode 100644
include/tst_kconfig.hcreate mode 100644lib/newlib_tests/tst_kconfig.ccreate mode 100644lib/tst_kconfig.cdoc/test-writing-guidelines.txt +32
-0@@ -1504,6 +1504,38 @@ static struct tst_test test = { .save_restore = save_restore, }; +2.2.28 Parsing kernel .config +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Generally testcases should attempt to autodetect as much kernel features as +possible based on the currently running kernel. We do have tst_check_driver() +to check if functionality that could be compiled as kernel module is present +on the system, disabled syscalls can be detected by checking for 'ENOSYS' +errno etc. + +However in rare cases core kernel features couldn't be detected based on the +kernel userspace API and we have to resort on kernel .config parsing. + +For this cases the test should set the 'NULL' terminated needs_kconfig array +of kernel config options required for the test. The test will exit with +'TCONF' if any of the required options wasn't set to 'y' or 'm'. + +[source,c] +------------------------------------------------------------------------------- +#include "tst_test.h" + +static const char *kconfigs[] = { + "CONFIG_X86_INTEL_UMIP", + NULL +}; + +static struct tst_test test = { + ... + .needs_kconfigs = kconfigs, + ... +}; +------------------------------------------------------------------------------- + 2.3 Writing a testcase in shell ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~include/tst_kconfig.h new file +34
-0@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#ifndef TST_KCONFIG_H__ +#define TST_KCONFIG_H__ + +/* +* Parses kernel config given a CONFIG_FOO symbol. +* +* The location of the config is detected automatically in case that the config +* lives in one of the standard locations or can be set/overrided by setting +* LTP_KCONFIG environment variable. +* +* The functions returns: +* +* 'y' -- when compiled in +* 'm' -- when compiled as a module +* 'v' -- when set to some value e.g. CONFIG_BLK_DEV_RAM_COUNT=16 +* 'n' -- when not set +* 'u' -- when CONFIG_FOO wasn't present in CONFIG +* 'e' -- on error i.e. .config couldn't be located +*/ +char tst_kconfig (const char *id); + +/* +* Exits the test with TCONF on first config option that is not set to 'm' or 'y' +* +* @kconfigs: NULL terminated array of kernel config options such as "CONFIG_MMU" +*/ +void tst_kconfig_check (const char *const *kconfigs); + +#endif /* TST_KCONFIG_H__ */ include/tst_test.h +6
-0@@ -182,6 +182,12 @@ struct tst_test {* before setup and restore after cleanup */ const char *const *save_restore; + +/* +* NULL terminated array of kernel config options required for the +* test. +*/ +const char *const *needs_kconfigs; };/* lib/newlib_tests/.gitignore +1
-0@@ -24,3 +24,4 @@ test19 tst_expiration_timer test_exec test_exec_child +tst_kconfiglib/newlib_tests/tst_kconfig.c new file +24
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#include "tst_test.h" +#include "tst_kconfig.h" + +static void do_test (void ) +{ +tst_res (TPASS ,"Not reached!" ); +} + +static const char *kconfigs[] = { +"CONFIG_MMU" , +"CONFIG_EXT4_FS" , +/* Comment this to make the test run */ +"CONFIG_NONEXISTENT" , +NULL +}; + +static struct tst_test test = { + .test_all = do_test, + .needs_kconfigs = kconfigs, +};lib/tst_kconfig.c new file +183
-0@@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#ifndef TST_KCONFIG_H__ +#define TST_KCONFIG_H__ + +#include <stdlib.h> +#include <stdio.h> +#include <sys/utsname.h> + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" + +static const char *kconfig_path (char *path_buf,size_t path_buf_len) +{ +const char *path =getenv ("LTP_KCONFIG" ); +struct utsname un; + +if (path) { +if (!access (path,F_OK )) +return path; + +tst_res (TWARN ,"LTP_KCONFIG='%s' does not exist" , path); + } + +if (!access ("/proc/config.gz" ,F_OK )) +return "/proc/config.gz" ; + +uname (&un); + +snprintf (path_buf, path_buf_len,"/boot/config-%s" , un.release); + +if (!access (path_buf,F_OK )) +return path_buf; + +tst_res (TINFO ,"Couldn't locate kernel config!" ); + +return NULL ; +} + +static char parse_line (const char *line) +{ +if (strstr (line,"=y" )) +return 'y' ; + +if (strstr (line,"=m" )) +return 'm' ; + +if (strstr (line,"=" )) +return 'v' ; + +return 'n' ; +} + +static char is_gzip; + +static FILE *open_kconfig (void ) +{ +FILE *fp; +char buf[1024 ]; +char path_buf[1024 ]; +const char *path =kconfig_path (path_buf,sizeof (path_buf)); + +if (!path) +return NULL ; + + is_gzip = !!strstr (path,".gz" ); + +if (is_gzip) { +snprintf (buf,sizeof (buf),"zcat '%s'" , path); + fp =popen (buf,"r" ); + }else { + fp =fopen (path,"r" ); + } + +if (!fp) +tst_brk (TBROK |TERRNO ,"Failed to open '%s'" , path); + +return fp; +} + +static void close_kconfig (FILE *fp) +{ +if (is_gzip) +pclose (fp); +else +fclose (fp); +} + +static int match_option (const char *line,const char *opt) +{ +size_t opt_len =strlen (opt); + +return !strncmp (line, opt, opt_len) && + (line[opt_len] =='=' || line[opt_len] ==' ' ); +} + +char tst_kconfig (const char *id) +{ +char buf[1024 ]; +char match ='u' ; +FILE *fp; + + fp =open_kconfig (); +if (!fp) +return 'e' ; + +while (fgets (buf,sizeof (buf), fp)) { +if (match_option (buf, id)) { + match =parse_line (buf); +break ; + } + } + +close_kconfig (fp); + +return match; +} + +static unsigned int array_len (const char *const *kconfigs) +{ +unsigned int i =0 ; + +while (kconfigs[i]) + i++; + +return i; +} + +void tst_kconfig_check (const char *const *kconfigs) +{ +char buf[1024 ]; +FILE *fp; +unsigned int conf_cnt =array_len (kconfigs); +unsigned int conf_match[conf_cnt]; +unsigned int i, j; + +memset (conf_match,0 ,sizeof (conf_match)); + + fp =open_kconfig (); +if (!fp) +tst_brk (TBROK ,"Cannot parse kernel .config" ); + +while (fgets (buf,sizeof (buf), fp)) { +for (i =0 ; i < conf_cnt; i++) { +if (conf_match[i]) +continue ; + +if (match_option (buf, kconfigs[i]) && + (strstr (buf,"=y" ) ||strstr (buf,"=m" ))) { + conf_match[i] =1 ; + +for (j =0 ; j < conf_cnt; j++) { +if (!conf_match[j]) +break ; + } + +if (j == i) + goto exit; + } + } + + } + +int abort_test =0 ; + +for (i =0 ; i < conf_cnt; i++) { +if (!conf_match[i]) { + abort_test =1 ; +tst_res (TINFO ,"Kernel is missing %s" , kconfigs[i]); + } + } + +if (abort_test) +tst_brk (TCONF ,"Aborting test due to missing kernel config options!" ); + +exit: +close_kconfig (fp); +} + +#endif /* TST_KCONFIG_H__ */ lib/tst_test.c +4
-0@@ -36,6 +36,7 @@#include "tst_clocks.h" #include "tst_timer.h" #include "tst_sys_conf.h" +#include "tst_kconfig.h" #include "old_resource.h" #include "old_device.h" @@ -770,6 +771,9 @@ static void do_setup(int argc, char *argv[])if (tst_test->tconf_msg)tst_brk (TCONF ,"%s" , tst_test->tconf_msg); +if (tst_test->needs_kconfigs) +tst_kconfig_check (tst_test->needs_kconfigs); +assert_test_fn (); tid =get_tid (argv);
PATCH 1/1 lib: Add library function for parsing kernel config
Hi!
Quoted 4 lines
this morning I was just thinking about brutally zcatting config.gz to be able to skip some tests based upon current Kernel configs (not drivers/moduleoptions)...and I'd need a mechanism to do that...then I saw this patch :D
That's the beauty of opensource, your problems gets solved even before you try :-).
...
Quoted 34 lines
testcases/lib/tst_check_kconfigs.c new file +33
-0+#define TST_NO_DEFAULT_MAIN +#include <stdio.h> +#include <stdlib.h> +#include <tst_test.h> +#include <tst_kconfig.h> + +int main (int argc,const char **argv) +{ +int missing =0 ; +const char **cfg =NULL ; + +if (argc <2 ) +return -1 ; + +for (cfg = &argv[1 ]; !missing && *cfg; cfg++) { +char rval ='\0' ; + +fprintf (stderr,"Check for: %s\n" , *cfg); + rval =tst_kconfig (*cfg); +switch (rval) { +case 'y' : +case 'm' : +tst_res (TINFO ,"Kernel %s found" , *cfg); +break ; +default : + missing =1 ; +tst_res (TINFO ,"Kernel is missing %s" , *cfg); +break ; + } + } + +return missing; +}
Quoted 1 line
and it seems to work !
Hmm, I guess that we can even pass argv+1 to the tst_kconfig_check(), the function will exit with 32 on missing modules and with 2 if something went wrong.
We can slightly modify it, if we want to have better control over the exit functions here, but generally that should be even easier than this.
Quoted 14 lines
cmdline="quota_remount_test01.sh"
contacts=""
analysis=exit
<<<test_output>>>
incrementing stop
Check for:CONFIG_QFMT_V1
tst_check_kconfigs.c:44: INFO: Kernel is missingCONFIG_QFMT_V1quota_remount_test011 TCONF : ltpapicmd.c:188: Kernel QUOTA
Options NOT configured!
<<<execution_status>>>
initiation_status="ok"
duration=0 termination_type=exited termination_id=32 corefile=no cutime=0 cstime=0
Quoted 1 line
Do you think could be useful ?
Yes, we will likely need shell helper for this API.
Quoted 1 line
Or should be done another way really (I'm brand new to LTP tests...) ?
Actually for the case quotas calling quotactl(2) and checking for ENOSYS may be easier and probably prefered.
We may want to record the required config options somewhere though.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: Add library function for parsing kernel config
In reply to Cyril HrubisQuoted 14 lines
-----Original Message-----
From: Cyril Hrubis on Thursday, November 15, 2018 5:45 AMThis is meant as last resort action for disabling tests if certain kernel funcitonality was not present, in general case runtime checks are prefered.
For functionality that can be build as a module
tst_check_driver()is most likely better fit since it will also insert requested kernel module into kernel if needed.For newly added syscalls kernel version comparsion and/or checking errno is prefered.
Quoted 7 lines
However in rare cases certain core kernel functionality cannot be detected in any other way than checking the kernel config, which is where this API gets into the play.
The path to the kernel config could be specified by
LTP_KCONFIGenvironment variable, which also takes precedence before the autodetection that attempts to read the config from known locations.
It would be nice to use a variable name here that was not project-specific.
In Fuego, we automatically check a few different places, but we don't check a location specified by an environment variable. However, I've wanted to add one. I'd prefer to use the same one as other projects, if possible.
I'm shooting from the hip here, but how about something like:KCONFIG_PATH
?
Is this something that kernel-producing projects, such as Yocto Project or Buildroot, (or Debian or Fedora, or the kernel Makefile, for that matter) already have, or would be willing to support?
To others on the automated-testing list, does anyone else have a variable used to specify the location of the kernel config file? If so, what's its name?
-- Tim
PATCH 1/1 lib: Add library function for parsing kernel config
Hi!
Quoted 9 lines
It would be nice to use a variable name here that was not project-specific.
In Fuego, we automatically check a few different places, but we don't check a location specified by an environment variable. However, I've wanted to add one. I'd prefer to use the same one as other projects, if possible.
I'm shooting from the hip here, but how about something like:
KCONFIG_PATH
?
Fine with me, should I switch to this one in v2?
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: Add library function for parsing kernel config
Sorry for the slow response. Some personal issues interfered with my work in the last week.
Quoted 7 lines
-----Original Message-----
From: Cyril Hrubis [mailto:[email protected]]
Hi!Quoted 4 lines
It would be nice to use a variable name here that was not project-specific.
In Fuego, we automatically check a few different places, but we don't check a location specified by an environment variable. However, I've
Quoted 8 lines
wanted to
Quoted 5 lines
add one. I'd prefer to use the same one as other projects, if possible.
I'm shooting from the hip here, but how about something like:
KCONFIG_PATH
?Fine with me, should I switch to this one in v2?
I didn't hear back from anyone with an alternative, so I would say yes.
That variable name is something I can try to promote to other groups and frameworks. IMHO, this would be a nice bit of info to get from the kernel build, or from the build system or distribution.
Thanks,
-- Tim
PATCH 1/1 lib: Add library function for parsing kernel config
In reply to Cyril HrubisQuoted 3 lines
-----Original Message-----
From: Cyril Hrubis on Thursday, November 15, 2018 5:45 AM
...
Quoted 47 lines
The required kernel options are passed as an array of strings via the .needs_kconfigs pointer in the
tst_teststructure. The purpose of this is twofold, one is that the test can disable itself at runtime if given functionality is missing from kernel .config and second is about being able to propagate this information to the testrunner (this could be done once we figure out how export the information from the structure to the test runner) then we can avoid running tests on unsuitable configurations from the start.--- doc/test-writing-guidelines.txt | 32 +++++++ include/tst_kconfig.h | 34 ++++++++ include/tst_test.h | 6 ++ lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/tst_kconfig.c | 24 ++++++ lib/tst_kconfig.c | 183 ++++++++++++++++++++++++++++++++++++++++ lib/tst_test.c | 4 + 7 files changed, 284 insertions(+) create mode 100644 include/tst_kconfig.h create mode 100644 lib/newlib_tests/tst_kconfig.c create mode 100644 lib/tst_kconfig.cdoc/test-writing-guidelines.txt +4
-0@@ -1504,6 +1504,38 @@ static struct tst_test test = { .save_restore = save_restore, }; +2.2.28 Parsing kernel .config +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Generally testcases should attempt to autodetect as much kernel featuresas
+possible based on the currently running kernel. We do havetst_check_driver()doc/test-writing-guidelines.txt +5
-0+to check if functionality that could be compiled as kernel module is present +on the system, disabled syscalls can be detected by checking for 'ENOSYS' +errno etc. + +However in rare cases core kernel features couldn't be detected based onthe
doc/test-writing-guidelines.txt static struct tst_test test = { · line 1521
Quoted 5 lines
doc/test-writing-guidelines.txt +5
-0+kernel userspace API and we have to resort on kernel .config parsing. + +For this cases the test should set the 'NULL' terminated needs_kconfig array +of kernel config options required for the test. The test will exit with +'TCONF' if any of the required options wasn't set to 'y' or 'm'.
This is good. Just a comment on the proposed syntax and options. Fuego uses NEED_KCONFIG (upper-case because it's a shell global variable), and 0-day uses need_kconfig.
There are a few slight differences in syntax between different systems. (I'm making no judgements, just noting the differences).
Fuego and 0day also support a string including the option value, such as:
CONFIG_PRINTK=y
CONFIG_LOCKDEP_SUPPORT=n
CONFIG_CAN=m
See https://elinux.org/Test_Dependencies
where I've started collecting some of these different syntaxes.
-- Tim
PATCH 1/1 lib: Add library function for parsing kernel config
Hi!
Quoted 4 lines
This is good. Just a comment on the proposed syntax and options. Fuego uses
NEED_KCONFIG(upper-case because it's a shell global variable), and 0-day usesneed_kconfig.
Quoted 8 lines
There are a few slight differences in syntax between different systems. (I'm making no judgements, just noting the differences).
Fuego and 0day also support a string including the option value, such as:
CONFIG_PRINTK=y
CONFIG_LOCKDEP_SUPPORT=n
CONFIG_CAN=m
I guess that it makes sense to support this as well.
I can easily switch to verbatim match if the option I'm looking for contains '='.
Quoted 3 lines
See https://elinux.org/Test_Dependencies
where I've started collecting some of these different syntaxes.
Thanks for the pointer, I will look into that.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: Add library function for parsing kernel config
In reply to Cyril HrubisHi Cyril,
Thanks for kconfig checking API!
It's worked in most of Linux OS.
Just add konfig to support Clear Linux which kconfig is saved in "/lib/kernel/config-xxx"
And for print comments, if you agree as below.
lib/tst_kconfig.c line 37
Quoted 8 lines · Cyril Hrubis
lib/tst_kconfig.c new file +6
-0+ +snprintf (path_buf, path_buf_len,"/boot/config-%s" , un.release); + +if (!access (path_buf,F_OK )) +return path_buf; +
Could add suport for Clear Linux kconfig location:
snprintf(path_buf, path_buf_len, "/lib/kernel/config-%s", un.release); if (!access(path_buf, F_OK)) return path_buf;
lib/tst_kconfig.c line 172
Quoted 11 lines
lib/tst_kconfig.c new file +10
-0+tst_res (TINFO ,"Couldn't locate kernel config!" ); + +return NULL ; +} +int abort_test =0 ; + +for (i =0 ; i < conf_cnt; i++) { +if (!conf_match[i]) { + abort_test =1 ; +tst_res (TINFO ,"Kernel is missing %s" , kconfigs[i]);
Could change TINFO above to TCONF?
lib/tst_kconfig.c line 177
Quoted 5 lines
lib/tst_kconfig.c new file +5
-0+ } + } + +if (abort_test) +tst_brk (TCONF ,"Aborting test due to missing kernel config options!" );
Add 'above' in print? tst_brk(TCONF, "Aborting test due to missing above kernel config options!");
Thanks!
B.R.
PATCH 1/1 lib: Add library function for parsing kernel config
Quoted 11 lines
-----Original Message-----
From: Pengfei XuHi Cyril,
Thanks for kconfig checking API!
It's worked in most of Linux OS.Just add konfig to support Clear Linux which kconfig is saved in "
/lib/kernel/config-xxx"
Pengfei,
Is /lib/kernel/config-xxx something that only Clear Linux does, or is this a new standard, done by lots of distros?
-- Tim
PATCH 1/1 lib: Add library function for parsing kernel config
Hi Tim,
Only Clear Linux does at present, not a new standard. Thanks.
Quoted 19 lines · On 2018-11-16 at 04:03:13 +0000, [email protected] wrote:
Quoted 11 lines
-----Original Message-----
From: Pengfei XuHi Cyril,
Thanks for kconfig checking API!
It's worked in most of Linux OS.Just add konfig to support Clear Linux which kconfig is saved in "
/lib/kernel/config-xxx"Pengfei,
Is
/lib/kernel/config-xxxsomething that only Clear Linux does, or is this a new standard, done by lots of distros?-- Tim
PATCH 1/1 lib: Add library function for parsing kernel config
In reply to Pengfei XuHi!
Quoted 9 lines
lib/tst_kconfig.c line 37
Quoted 8 lines
lib/tst_kconfig.c new file +6
-0+ +snprintf (path_buf, path_buf_len,"/boot/config-%s" , un.release); + +if (!access (path_buf,F_OK )) +return path_buf; +Could add suport for Clear Linux kconfig location:
Quoted 4 lines
snprintf(path_buf, path_buf_len, "/lib/kernel/config-%s", un.release); if (!access(path_buf, F_OK)) return path_buf;
No problem, will add that in v2?
Quoted 5 lines
Quoted 4 lines
lib/tst_kconfig.c new file +4
-0+tst_res (TINFO ,"Couldn't locate kernel config!" ); + +return NULL ; +}
Quoted 7 lines
lib/tst_kconfig.c line 172
Quoted 6 lines
lib/tst_kconfig.c new file +6
-0+int abort_test =0 ; + +for (i =0 ; i < conf_cnt; i++) { +if (!conf_match[i]) { + abort_test =1 ; +tst_res (TINFO ,"Kernel is missing %s" , kconfigs[i]);Could change TINFO above to TCONF?
What's the point? We do exit with TCONF below anyway.
Quoted 8 lines
lib/tst_kconfig.c line 177
Quoted 5 lines
lib/tst_kconfig.c new file +5
-0+ } + } + +if (abort_test) +tst_brk (TCONF ,"Aborting test due to missing kernel config options!" );Add 'above' in print? tst_brk(TCONF, "Aborting test due to missing above kernel config options!");
Sure, np.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: Add library function for parsing kernel config
Quoted 10 lines · Cyril Hrubis
Quoted 7 lines
lib/tst_kconfig.c line 172
Quoted 6 lines
lib/tst_kconfig.c new file +6
-0+int abort_test =0 ; + +for (i =0 ; i < conf_cnt; i++) { +if (!conf_match[i]) { + abort_test =1 ; +tst_res (TINFO ,"Kernel is missing %s" , kconfigs[i]);Could change TINFO above to TCONF?
What's the point? We do exit with TCONF below anyway.
All right, thanks.
Quoted 3 lines
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: Add library function for parsing kernel config
In reply to Cyril HrubisQuoted 208 lines · Cyril Hrubis
This is meant as last resort action for disabling tests if certain kernel funcitonality was not present, in general case runtime checks are prefered.
For functionality that can be build as a module
tst_check_driver()is most likely better fit since it will also insert requested kernel module into kernel if needed.For newly added syscalls kernel version comparsion and/or checking errno is prefered.
However in rare cases certain core kernel functionality cannot be detected in any other way than checking the kernel config, which is where this API gets into the play.
The path to the kernel config could be specified by
LTP_KCONFIGenvironment variable, which also takes precedence before the autodetection that attempts to read the config from known locations.The required kernel options are passed as an array of strings via the .needs_kconfigs pointer in the
tst_teststructure. The purpose of this is twofold, one is that the test can disable itself at runtime if given functionality is missing from kernel .config and second is about being able to propagate this information to the testrunner (this could be done once we figure out how export the information from the structure to the test runner) then we can avoid running tests on unsuitable configurations from the start.--- doc/test-writing-guidelines.txt | 32 +++++++ include/tst_kconfig.h | 34 ++++++++ include/tst_test.h | 6 ++ lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/tst_kconfig.c | 24 ++++++ lib/tst_kconfig.c | 183 ++++++++++++++++++++++++++++++++++++++++ lib/tst_test.c | 4 + 7 files changed, 284 insertions(+) create mode 100644 include/tst_kconfig.h create mode 100644 lib/newlib_tests/tst_kconfig.c create mode 100644 lib/tst_kconfig.cdoc/test-writing-guidelines.txt +32
-0@@ -1504,6 +1504,38 @@ static struct tst_test test = { .save_restore = save_restore, }; +2.2.28 Parsing kernel .config +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Generally testcases should attempt to autodetect as much kernel features as +possible based on the currently running kernel. We do have tst_check_driver() +to check if functionality that could be compiled as kernel module is present +on the system, disabled syscalls can be detected by checking for 'ENOSYS' +errno etc. + +However in rare cases core kernel features couldn't be detected based on the +kernel userspace API and we have to resort on kernel .config parsing. + +For this cases the test should set the 'NULL' terminated needs_kconfig array +of kernel config options required for the test. The test will exit with +'TCONF' if any of the required options wasn't set to 'y' or 'm'. + +[source,c] +------------------------------------------------------------------------------- +#include "tst_test.h" + +static const char *kconfigs[] = { + "CONFIG_X86_INTEL_UMIP", + NULL +}; + +static struct tst_test test = { + ... + .needs_kconfigs = kconfigs, + ... +}; +------------------------------------------------------------------------------- + 2.3 Writing a testcase in shell ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~include/tst_kconfig.h new file +34
-0@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#ifndef TST_KCONFIG_H__ +#define TST_KCONFIG_H__ + +/* +* Parses kernel config given a CONFIG_FOO symbol. +* +* The location of the config is detected automatically in case that the config +* lives in one of the standard locations or can be set/overrided by setting +* LTP_KCONFIG environment variable. +* +* The functions returns: +* +* 'y' -- when compiled in +* 'm' -- when compiled as a module +* 'v' -- when set to some value e.g. CONFIG_BLK_DEV_RAM_COUNT=16 +* 'n' -- when not set +* 'u' -- when CONFIG_FOO wasn't present in CONFIG +* 'e' -- on error i.e. .config couldn't be located +*/ +char tst_kconfig (const char *id); + +/* +* Exits the test with TCONF on first config option that is not set to 'm' or 'y' +* +* @kconfigs: NULL terminated array of kernel config options such as "CONFIG_MMU" +*/ +void tst_kconfig_check (const char *const *kconfigs); + +#endif /* TST_KCONFIG_H__ */ include/tst_test.h +6
-0@@ -182,6 +182,12 @@ struct tst_test {* before setup and restore after cleanup */ const char *const *save_restore; + +/* +* NULL terminated array of kernel config options required for the +* test. +*/ +const char *const *needs_kconfigs; };/* lib/newlib_tests/.gitignore +1
-0@@ -24,3 +24,4 @@ test19 tst_expiration_timer test_exec test_exec_child +tst_kconfiglib/newlib_tests/tst_kconfig.c new file +24
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#include "tst_test.h" +#include "tst_kconfig.h" + +static void do_test (void ) +{ +tst_res (TPASS ,"Not reached!" ); +} + +static const char *kconfigs[] = { +"CONFIG_MMU" , +"CONFIG_EXT4_FS" , +/* Comment this to make the test run */ +"CONFIG_NONEXISTENT" , +NULL +}; + +static struct tst_test test = { + .test_all = do_test, + .needs_kconfigs = kconfigs, +};lib/tst_kconfig.c new file +19
-0@@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2018 Cyril Hrubis <[email protected]> +*/ + +#ifndef TST_KCONFIG_H__ +#define TST_KCONFIG_H__ + +#include <stdlib.h> +#include <stdio.h> +#include <sys/utsname.h> + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" + +static const char *kconfig_path (char *path_buf,size_t path_buf_len) +{ +const char *path =getenv ("LTP_KCONFIG" ); +struct utsname un;
lib/tst_kconfig.c line 29
Quoted 10 lines
lib/tst_kconfig.c new file +10
-0+ +if (path) { +if (!access (path,F_OK )) +return path; + +tst_res (TWARN ,"LTP_KCONFIG='%s' does not exist" , path); + } + +if (!access ("/proc/config.gz" ,F_OK )) +return "/proc/config.gz" ;
<snip>
FWIW, this here covers all of Android now. Android _always_ has /proc/config.gz and this now allows us to double check the android required options before running some of those tests. Thanks for doing this :).