1*49cdfc7eSAndroid Build Coastguard WorkerLTP C Test API 2*49cdfc7eSAndroid Build Coastguard Worker============== 3*49cdfc7eSAndroid Build Coastguard Worker 4*49cdfc7eSAndroid Build Coastguard WorkerNOTE: See also 5*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines], 6*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/C-Test-Case-Tutorial[C Test Case Tutorial], 7*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/Shell-Test-API[Shell Test API]. 8*49cdfc7eSAndroid Build Coastguard Worker 9*49cdfc7eSAndroid Build Coastguard Worker1 Writing a test in C 10*49cdfc7eSAndroid Build Coastguard Worker--------------------- 11*49cdfc7eSAndroid Build Coastguard Worker 12*49cdfc7eSAndroid Build Coastguard Worker1.1 Basic test structure 13*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 14*49cdfc7eSAndroid Build Coastguard Worker 15*49cdfc7eSAndroid Build Coastguard WorkerLet's start with an example, following code is a simple test for a 'getenv()'. 16*49cdfc7eSAndroid Build Coastguard Worker 17*49cdfc7eSAndroid Build Coastguard Worker[source,c] 18*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 19*49cdfc7eSAndroid Build Coastguard Worker/*\ 20*49cdfc7eSAndroid Build Coastguard Worker * [Description] 21*49cdfc7eSAndroid Build Coastguard Worker * Tests basic functionality of getenv(). 22*49cdfc7eSAndroid Build Coastguard Worker * 23*49cdfc7eSAndroid Build Coastguard Worker * - create an env variable and verify that getenv() can get it 24*49cdfc7eSAndroid Build Coastguard Worker * - call getenv() with nonexisting variable name, check that it returns NULL 25*49cdfc7eSAndroid Build Coastguard Worker */ 26*49cdfc7eSAndroid Build Coastguard Worker 27*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 28*49cdfc7eSAndroid Build Coastguard Worker 29*49cdfc7eSAndroid Build Coastguard Worker#define ENV1 "LTP_TEST_ENV" 30*49cdfc7eSAndroid Build Coastguard Worker#define ENV2 "LTP_TEST_THIS_DOES_NOT_EXIST" 31*49cdfc7eSAndroid Build Coastguard Worker#define ENV_VAL "val" 32*49cdfc7eSAndroid Build Coastguard Worker 33*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 34*49cdfc7eSAndroid Build Coastguard Worker{ 35*49cdfc7eSAndroid Build Coastguard Worker if (setenv(ENV1, ENV_VAL, 1)) 36*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "setenv() failed"); 37*49cdfc7eSAndroid Build Coastguard Worker} 38*49cdfc7eSAndroid Build Coastguard Worker 39*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 40*49cdfc7eSAndroid Build Coastguard Worker{ 41*49cdfc7eSAndroid Build Coastguard Worker char *ret; 42*49cdfc7eSAndroid Build Coastguard Worker 43*49cdfc7eSAndroid Build Coastguard Worker ret = getenv(ENV1); 44*49cdfc7eSAndroid Build Coastguard Worker 45*49cdfc7eSAndroid Build Coastguard Worker if (!ret) { 46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "getenv(" ENV1 ") = NULL"); 47*49cdfc7eSAndroid Build Coastguard Worker goto next; 48*49cdfc7eSAndroid Build Coastguard Worker } 49*49cdfc7eSAndroid Build Coastguard Worker 50*49cdfc7eSAndroid Build Coastguard Worker if (!strcmp(ret, ENV_VAL)) { 51*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "getenv(" ENV1 ") = '"ENV_VAL "'"); 52*49cdfc7eSAndroid Build Coastguard Worker } else { 53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "getenv(" ENV1 ") = '%s', expected '" 54*49cdfc7eSAndroid Build Coastguard Worker ENV_VAL "'", ret); 55*49cdfc7eSAndroid Build Coastguard Worker } 56*49cdfc7eSAndroid Build Coastguard Worker 57*49cdfc7eSAndroid Build Coastguard Workernext: 58*49cdfc7eSAndroid Build Coastguard Worker ret = getenv(ENV2); 59*49cdfc7eSAndroid Build Coastguard Worker 60*49cdfc7eSAndroid Build Coastguard Worker if (ret) 61*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "getenv(" ENV2 ") = '%s'", ret); 62*49cdfc7eSAndroid Build Coastguard Worker else 63*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "getenv(" ENV2 ") = NULL"); 64*49cdfc7eSAndroid Build Coastguard Worker} 65*49cdfc7eSAndroid Build Coastguard Worker 66*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 67*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 68*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 69*49cdfc7eSAndroid Build Coastguard Worker}; 70*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 71*49cdfc7eSAndroid Build Coastguard Worker 72*49cdfc7eSAndroid Build Coastguard WorkerEach test includes the 'tst_test.h' header and must define the 'struct 73*49cdfc7eSAndroid Build Coastguard Workertst_test test' structure. 74*49cdfc7eSAndroid Build Coastguard Worker 75*49cdfc7eSAndroid Build Coastguard WorkerThe overall test initialization is done in the 'setup()' function. 76*49cdfc7eSAndroid Build Coastguard Worker 77*49cdfc7eSAndroid Build Coastguard WorkerThe overall cleanup is done in a 'cleanup()' function. Here 'cleanup()' is 78*49cdfc7eSAndroid Build Coastguard Workeromitted as the test does not have anything to clean up. If cleanup is set in 79*49cdfc7eSAndroid Build Coastguard Workerthe test structure it's called on test exit just before the test library 80*49cdfc7eSAndroid Build Coastguard Workercleanup. That especially means that cleanup can be called at any point in a 81*49cdfc7eSAndroid Build Coastguard Workertest execution. For example even when a test setup step has failed, therefore 82*49cdfc7eSAndroid Build Coastguard Workerthe 'cleanup()' function must be able to cope with unfinished initialization, 83*49cdfc7eSAndroid Build Coastguard Workerand so on. 84*49cdfc7eSAndroid Build Coastguard Worker 85*49cdfc7eSAndroid Build Coastguard WorkerThe test itself is done in the 'test()' function. The test function must work 86*49cdfc7eSAndroid Build Coastguard Workerfine if called in a loop. 87*49cdfc7eSAndroid Build Coastguard Worker 88*49cdfc7eSAndroid Build Coastguard WorkerThere are two types of a test function pointers in the test structure. The 89*49cdfc7eSAndroid Build Coastguard Workerfirst one is a '.test_all' pointer that is used when test is implemented as a 90*49cdfc7eSAndroid Build Coastguard Workersingle function. Then there is a '.test' function along with the number of 91*49cdfc7eSAndroid Build Coastguard Workertests '.tcnt' that allows for more detailed result reporting. If the '.test' 92*49cdfc7eSAndroid Build Coastguard Workerpointer is set the function is called '.tcnt' times with an integer parameter 93*49cdfc7eSAndroid Build Coastguard Workerin range of [0, '.tcnt' - 1]. 94*49cdfc7eSAndroid Build Coastguard Worker 95*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: Only one of '.test' and '.test_all' can be set at a time. 96*49cdfc7eSAndroid Build Coastguard Worker 97*49cdfc7eSAndroid Build Coastguard WorkerEach test has a limit on how long it can run and the limit composes of two 98*49cdfc7eSAndroid Build Coastguard Workerparts max_runtime and timeout. The max_runtime is a limit for how long can the 99*49cdfc7eSAndroid Build Coastguard Worker'.test_all' or a set of '.test' functions take and the timeout is static part 100*49cdfc7eSAndroid Build Coastguard Workerthat should cover the duration of test setup and cleanup plus some safety. 101*49cdfc7eSAndroid Build Coastguard Worker 102*49cdfc7eSAndroid Build Coastguard WorkerAny test that runs for more than a second or two has to make sure to: 103*49cdfc7eSAndroid Build Coastguard Worker 104*49cdfc7eSAndroid Build Coastguard Worker- set the runtime either by setting the '.max_runtime' in tst_test or by 105*49cdfc7eSAndroid Build Coastguard Worker calling 'tst_set_max_runtime()' in the test setup 106*49cdfc7eSAndroid Build Coastguard Worker 107*49cdfc7eSAndroid Build Coastguard Worker- monitor remaning runtime by regular calls to 'tst_remaining_runtime()' and 108*49cdfc7eSAndroid Build Coastguard Worker exit when runtime has been used up 109*49cdfc7eSAndroid Build Coastguard Worker 110*49cdfc7eSAndroid Build Coastguard WorkerTest is free to exit before max_runtime has been used up for example when 111*49cdfc7eSAndroid Build Coastguard Workerminimal number of iteration was finished. 112*49cdfc7eSAndroid Build Coastguard Worker 113*49cdfc7eSAndroid Build Coastguard WorkerThe limit is applied to a single call of the '.test_all' function that means 114*49cdfc7eSAndroid Build Coastguard Workerthat for example when '.test_variants' or '.all_filesystems' is set the whole 115*49cdfc7eSAndroid Build Coastguard Workertest will be limited by 'variants * (max_runtime + timeout)' seconds and the 116*49cdfc7eSAndroid Build Coastguard Workertest runtime will be likely close to 'variants * max_runtime' seconds. 117*49cdfc7eSAndroid Build Coastguard Worker 118*49cdfc7eSAndroid Build Coastguard Worker[source,c] 119*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 120*49cdfc7eSAndroid Build Coastguard Worker/* 121*49cdfc7eSAndroid Build Coastguard Worker * Returns number of seconds or zero in case that runtime has been used up. 122*49cdfc7eSAndroid Build Coastguard Worker */ 123*49cdfc7eSAndroid Build Coastguard Worker 124*49cdfc7eSAndroid Build Coastguard Workerint tst_remaining_runtime(void); 125*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 126*49cdfc7eSAndroid Build Coastguard Worker 127*49cdfc7eSAndroid Build Coastguard WorkerLAPI headers 128*49cdfc7eSAndroid Build Coastguard Worker++++++++++++ 129*49cdfc7eSAndroid Build Coastguard Worker 130*49cdfc7eSAndroid Build Coastguard WorkerUse our LAPI headers ('include "lapi/foo.h"') to keep compatibility with old 131*49cdfc7eSAndroid Build Coastguard Workerdistributions. LAPI header should always include original header. Older linux 132*49cdfc7eSAndroid Build Coastguard Workerheaders were problematic, therefore we preferred to use libc headers. There are 133*49cdfc7eSAndroid Build Coastguard Workerstill some bugs when combining certain glibc headers with linux headers, see 134*49cdfc7eSAndroid Build Coastguard Workerhttps://sourceware.org/glibc/wiki/Synchronizing_Headers. 135*49cdfc7eSAndroid Build Coastguard Worker 136*49cdfc7eSAndroid Build Coastguard WorkerA word about the cleanup() callback 137*49cdfc7eSAndroid Build Coastguard Worker+++++++++++++++++++++++++++++++++++ 138*49cdfc7eSAndroid Build Coastguard Worker 139*49cdfc7eSAndroid Build Coastguard WorkerThere are a few rules that needs to be followed in order to write correct 140*49cdfc7eSAndroid Build Coastguard Workercleanup() callback. 141*49cdfc7eSAndroid Build Coastguard Worker 142*49cdfc7eSAndroid Build Coastguard Worker1. Free only resources that were initialized. Keep in mind that callback can 143*49cdfc7eSAndroid Build Coastguard Worker be executed at any point in the test run. 144*49cdfc7eSAndroid Build Coastguard Worker 145*49cdfc7eSAndroid Build Coastguard Worker2. Make sure to free resources in the reverse order they were 146*49cdfc7eSAndroid Build Coastguard Worker initialized. (Some of the steps may not depend on others and everything 147*49cdfc7eSAndroid Build Coastguard Worker will work if there were swapped but let's keep it in order.) 148*49cdfc7eSAndroid Build Coastguard Worker 149*49cdfc7eSAndroid Build Coastguard WorkerThe first rule may seem complicated at first however, on the contrary, it's 150*49cdfc7eSAndroid Build Coastguard Workerquite easy. All you have to do is to keep track of what was already 151*49cdfc7eSAndroid Build Coastguard Workerinitialized. For example file descriptors needs to be closed only if they were 152*49cdfc7eSAndroid Build Coastguard Workerassigned a valid file descriptor. For most of the things you need to create 153*49cdfc7eSAndroid Build Coastguard Workerextra flag that is set right after successful initialization though. Consider, 154*49cdfc7eSAndroid Build Coastguard Workerfor example, test setup below. 155*49cdfc7eSAndroid Build Coastguard Worker 156*49cdfc7eSAndroid Build Coastguard WorkerWe also prefer cleaning up resources that would otherwise be released on the 157*49cdfc7eSAndroid Build Coastguard Workerprogram exit. There are two main reasons for this decision. Resources such as 158*49cdfc7eSAndroid Build Coastguard Workerfile descriptors and mmaped memory could block umounting a block device in 159*49cdfc7eSAndroid Build Coastguard Workercases where the test library has mounted a filesystem for the test temporary 160*49cdfc7eSAndroid Build Coastguard Workerdirectory. Not freeing allocated memory would upset static analysis and tools 161*49cdfc7eSAndroid Build Coastguard Workersuch as valgrind and produce false-positives when checking for leaks in the 162*49cdfc7eSAndroid Build Coastguard Workerlibc and other low level libraries. 163*49cdfc7eSAndroid Build Coastguard Worker 164*49cdfc7eSAndroid Build Coastguard Worker[source,c] 165*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 166*49cdfc7eSAndroid Build Coastguard Workerstatic int fd0, fd1, mount_flag; 167*49cdfc7eSAndroid Build Coastguard Worker 168*49cdfc7eSAndroid Build Coastguard Worker#define MNTPOINT "mntpoint" 169*49cdfc7eSAndroid Build Coastguard Worker#define FILE1 "mntpoint/file1" 170*49cdfc7eSAndroid Build Coastguard Worker#define FILE2 "mntpoint/file2" 171*49cdfc7eSAndroid Build Coastguard Worker 172*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 173*49cdfc7eSAndroid Build Coastguard Worker{ 174*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(MNTPOINT, 0777); 175*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKFS(tst_device->dev, tst_device->fs_type, NULL, NULL); 176*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, 0); 177*49cdfc7eSAndroid Build Coastguard Worker mount_flag = 1; 178*49cdfc7eSAndroid Build Coastguard Worker 179*49cdfc7eSAndroid Build Coastguard Worker fd0 = SAFE_OPEN(cleanup, FILE1, O_CREAT | O_RDWR, 0666); 180*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN(cleanup, FILE2, O_CREAT | O_RDWR, 0666); 181*49cdfc7eSAndroid Build Coastguard Worker} 182*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 183*49cdfc7eSAndroid Build Coastguard Worker 184*49cdfc7eSAndroid Build Coastguard WorkerIn this case the 'cleanup()' function may be invoked when any of the 'SAFE_*' 185*49cdfc7eSAndroid Build Coastguard Workermacros has failed and therefore must be able to work with unfinished 186*49cdfc7eSAndroid Build Coastguard Workerinitialization as well. Since global variables are initialized to zero we can 187*49cdfc7eSAndroid Build Coastguard Workerjust check that fd > 0 before we attempt to close it. The mount function 188*49cdfc7eSAndroid Build Coastguard Workerrequires extra flag to be set after device was successfully mounted. 189*49cdfc7eSAndroid Build Coastguard Worker 190*49cdfc7eSAndroid Build Coastguard Worker[source,c] 191*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 192*49cdfc7eSAndroid Build Coastguard Workerstatic void cleanup(void) 193*49cdfc7eSAndroid Build Coastguard Worker{ 194*49cdfc7eSAndroid Build Coastguard Worker if (fd1 > 0) 195*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1); 196*49cdfc7eSAndroid Build Coastguard Worker 197*49cdfc7eSAndroid Build Coastguard Worker if (fd0 > 0) 198*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd0); 199*49cdfc7eSAndroid Build Coastguard Worker 200*49cdfc7eSAndroid Build Coastguard Worker if (mount_flag && tst_umouont(MNTPOINT)) 201*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "umount(%s)", MNTPOINT); 202*49cdfc7eSAndroid Build Coastguard Worker} 203*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 204*49cdfc7eSAndroid Build Coastguard Worker 205*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: 'SAFE_MACROS()' used in cleanup *do not* exit the test. Failure 206*49cdfc7eSAndroid Build Coastguard Worker only produces a warning and the 'cleanup()' carries on. This is 207*49cdfc7eSAndroid Build Coastguard Worker intentional as we want to execute as much 'cleanup()' as possible. 208*49cdfc7eSAndroid Build Coastguard Worker 209*49cdfc7eSAndroid Build Coastguard WorkerWARNING: Calling tst_brk() in test 'cleanup()' does not exit the test as well 210*49cdfc7eSAndroid Build Coastguard Worker and 'TBROK' is converted to 'TWARN'. 211*49cdfc7eSAndroid Build Coastguard Worker 212*49cdfc7eSAndroid Build Coastguard WorkerNOTE: Creation and removal of the test temporary directory is handled in 213*49cdfc7eSAndroid Build Coastguard Worker the test library and the directory is removed recursively. Therefore 214*49cdfc7eSAndroid Build Coastguard Worker we do not have to remove files and directories in the test cleanup. 215*49cdfc7eSAndroid Build Coastguard Worker 216*49cdfc7eSAndroid Build Coastguard Worker1.2 Basic test interface 217*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 218*49cdfc7eSAndroid Build Coastguard Worker 219*49cdfc7eSAndroid Build Coastguard Worker[source,c] 220*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 221*49cdfc7eSAndroid Build Coastguard Workervoid tst_res(int ttype, char *arg_fmt, ...); 222*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 223*49cdfc7eSAndroid Build Coastguard Worker 224*49cdfc7eSAndroid Build Coastguard WorkerPrintf-like function to report test result, it's mostly used with ttype: 225*49cdfc7eSAndroid Build Coastguard Worker 226*49cdfc7eSAndroid Build Coastguard Worker|============================== 227*49cdfc7eSAndroid Build Coastguard Worker| 'TPASS' | Test has passed. 228*49cdfc7eSAndroid Build Coastguard Worker| 'TFAIL' | Test has failed. 229*49cdfc7eSAndroid Build Coastguard Worker| 'TINFO' | General message. 230*49cdfc7eSAndroid Build Coastguard Worker| 'TDEBUG' | Debug message (new C API only, printed with '-D' or via 'LTP_ENABLE_DEBUG=1' or 'y' 231*49cdfc7eSAndroid Build Coastguard Worker environment variable), only for messages which would be too verbose for normal run. 232*49cdfc7eSAndroid Build Coastguard Worker| 'TWARN' | Something went wrong but we decided to continue. Mostly used in cleanup functions. 233*49cdfc7eSAndroid Build Coastguard Worker|============================== 234*49cdfc7eSAndroid Build Coastguard Worker 235*49cdfc7eSAndroid Build Coastguard WorkerThe 'ttype' can be combined bitwise with 'TERRNO' or 'TTERRNO' to print 236*49cdfc7eSAndroid Build Coastguard Worker'errno', 'TST_ERR' respectively. 237*49cdfc7eSAndroid Build Coastguard Worker 238*49cdfc7eSAndroid Build Coastguard Worker[source,c] 239*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 240*49cdfc7eSAndroid Build Coastguard Workervoid tst_brk(int ttype, char *arg_fmt, ...); 241*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 242*49cdfc7eSAndroid Build Coastguard Worker 243*49cdfc7eSAndroid Build Coastguard WorkerPrintf-like function to report error and exit the test, it can be used with ttype: 244*49cdfc7eSAndroid Build Coastguard Worker 245*49cdfc7eSAndroid Build Coastguard Worker|============================================================ 246*49cdfc7eSAndroid Build Coastguard Worker| 'TBROK' | Something has failed in test preparation phase. 247*49cdfc7eSAndroid Build Coastguard Worker| 'TCONF' | Test is not appropriate for current configuration 248*49cdfc7eSAndroid Build Coastguard Worker (syscall not implemented, unsupported arch, ...) 249*49cdfc7eSAndroid Build Coastguard Worker|============================================================ 250*49cdfc7eSAndroid Build Coastguard Worker 251*49cdfc7eSAndroid Build Coastguard WorkerThe 'ttype' can be combined bitwise with 'TERRNO' or 'TTERRNO' to print 252*49cdfc7eSAndroid Build Coastguard Worker'errno', 'TST_ERR' respectively. 253*49cdfc7eSAndroid Build Coastguard Worker 254*49cdfc7eSAndroid Build Coastguard WorkerThere are also 'TST_EXP_*()' macros that can simplify syscall unit tests to a 255*49cdfc7eSAndroid Build Coastguard Workersingle line, use them whenever possible. These macros take a function call as 256*49cdfc7eSAndroid Build Coastguard Workerthe first parameter and a printf-like format string and parameters as well. 257*49cdfc7eSAndroid Build Coastguard WorkerThese test macros then expand to a code that runs the call, checks the return 258*49cdfc7eSAndroid Build Coastguard Workervalue and errno and reports the test result. 259*49cdfc7eSAndroid Build Coastguard Worker 260*49cdfc7eSAndroid Build Coastguard Worker[source,c] 261*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 262*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 263*49cdfc7eSAndroid Build Coastguard Worker{ 264*49cdfc7eSAndroid Build Coastguard Worker ... 265*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(stat(fname, &statbuf), "stat(%s, ...)", fname); 266*49cdfc7eSAndroid Build Coastguard Worker 267*49cdfc7eSAndroid Build Coastguard Worker if (!TST_PASS) 268*49cdfc7eSAndroid Build Coastguard Worker return; 269*49cdfc7eSAndroid Build Coastguard Worker ... 270*49cdfc7eSAndroid Build Coastguard Worker} 271*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 272*49cdfc7eSAndroid Build Coastguard Worker 273*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_EXP_PASS()' can be used for calls that return -1 on failure and 0 on 274*49cdfc7eSAndroid Build Coastguard Workersuccess. It will check for the return value and reports failure if the return 275*49cdfc7eSAndroid Build Coastguard Workervalue is not equal to 0. The call also sets the 'TST_PASS' variable to 1 if 276*49cdfc7eSAndroid Build Coastguard Workerthe call succeeeded. 277*49cdfc7eSAndroid Build Coastguard Worker 278*49cdfc7eSAndroid Build Coastguard WorkerAs seen above, this and similar macros take optional variadic arguments. These 279*49cdfc7eSAndroid Build Coastguard Workerbegin with a format string and then appropriate values to be formatted. 280*49cdfc7eSAndroid Build Coastguard Worker 281*49cdfc7eSAndroid Build Coastguard Worker[source,c] 282*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 283*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 284*49cdfc7eSAndroid Build Coastguard Worker{ 285*49cdfc7eSAndroid Build Coastguard Worker ... 286*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FD(open(fname, O_RDONLY), "open(%s, O_RDONLY)", fname); 287*49cdfc7eSAndroid Build Coastguard Worker 288*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET); 289*49cdfc7eSAndroid Build Coastguard Worker ... 290*49cdfc7eSAndroid Build Coastguard Worker} 291*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 292*49cdfc7eSAndroid Build Coastguard Worker 293*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_EXP_FD()' is the same as 'TST_EXP_PASS()' the only difference is that 294*49cdfc7eSAndroid Build Coastguard Workerthe return value is expected to be a file descriptor so the call passes if 295*49cdfc7eSAndroid Build Coastguard Workerpositive integer is returned. 296*49cdfc7eSAndroid Build Coastguard Worker 297*49cdfc7eSAndroid Build Coastguard Worker[source,c] 298*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 299*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 300*49cdfc7eSAndroid Build Coastguard Worker{ 301*49cdfc7eSAndroid Build Coastguard Worker ... 302*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(stat(fname, &statbuf), ENOENT, "stat(%s, ...)", fname); 303*49cdfc7eSAndroid Build Coastguard Worker ... 304*49cdfc7eSAndroid Build Coastguard Worker} 305*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 306*49cdfc7eSAndroid Build Coastguard Worker 307*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_EXP_FAIL()' is similar to 'TST_EXP_PASS()' but it fails the test if 308*49cdfc7eSAndroid Build Coastguard Workerthe call haven't failed with -1 and 'errno' wasn't set to the expected one 309*49cdfc7eSAndroid Build Coastguard Workerpassed as the second argument. 310*49cdfc7eSAndroid Build Coastguard Worker 311*49cdfc7eSAndroid Build Coastguard Worker[source,c] 312*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 313*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 314*49cdfc7eSAndroid Build Coastguard Worker{ 315*49cdfc7eSAndroid Build Coastguard Worker ... 316*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL2(msgget(key, flags), EINVAL, "msgget(%i, %i)", key, flags); 317*49cdfc7eSAndroid Build Coastguard Worker ... 318*49cdfc7eSAndroid Build Coastguard Worker} 319*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 320*49cdfc7eSAndroid Build Coastguard Worker 321*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_EXP_FAIL2()' is the same as 'TST_EXP_FAIL()' except the return value is 322*49cdfc7eSAndroid Build Coastguard Workerexpected to be non-negative integer if call passes. These macros build upon the 323*49cdfc7eSAndroid Build Coastguard Worker+TEST()+ macro and associated variables. 324*49cdfc7eSAndroid Build Coastguard Worker 325*49cdfc7eSAndroid Build Coastguard Worker'TST_EXP_FAIL_SILENT()' and 'TST_EXP_FAIL2_SILENT()' variants are less verbose 326*49cdfc7eSAndroid Build Coastguard Workerand do not print TPASS messages when SCALL fails as expected. 327*49cdfc7eSAndroid Build Coastguard Worker 328*49cdfc7eSAndroid Build Coastguard Worker[source,c] 329*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 330*49cdfc7eSAndroid Build Coastguard WorkerTEST(socket(AF_INET, SOCK_RAW, 1)); 331*49cdfc7eSAndroid Build Coastguard Workerif (TST_RET > -1) { 332*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Created raw socket"); 333*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET); 334*49cdfc7eSAndroid Build Coastguard Worker} else if (TST_ERR != EPERM) { 335*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, 336*49cdfc7eSAndroid Build Coastguard Worker "Failed to create socket for wrong reason"); 337*49cdfc7eSAndroid Build Coastguard Worker} else { 338*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "Didn't create raw socket"); 339*49cdfc7eSAndroid Build Coastguard Worker} 340*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 341*49cdfc7eSAndroid Build Coastguard Worker 342*49cdfc7eSAndroid Build Coastguard WorkerThe +TEST+ macro sets +TST_RET+ to its argument's return value and +TST_ERR+ to 343*49cdfc7eSAndroid Build Coastguard Worker+errno+. The +TTERRNO+ flag can be used to print the error number's symbolic 344*49cdfc7eSAndroid Build Coastguard Workervalue. 345*49cdfc7eSAndroid Build Coastguard Worker 346*49cdfc7eSAndroid Build Coastguard WorkerNo LTP library function or macro, except those in 'tst_test_macros.h', will 347*49cdfc7eSAndroid Build Coastguard Workerwrite to these variables (rule 'LTP-002'). So their values will not be changed 348*49cdfc7eSAndroid Build Coastguard Workerunexpectedly. 349*49cdfc7eSAndroid Build Coastguard Worker 350*49cdfc7eSAndroid Build Coastguard Worker[source,c] 351*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 352*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_POSITIVE(wait(&status)); 353*49cdfc7eSAndroid Build Coastguard Worker 354*49cdfc7eSAndroid Build Coastguard Workerif (!TST_PASS) 355*49cdfc7eSAndroid Build Coastguard Worker return; 356*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 357*49cdfc7eSAndroid Build Coastguard Worker 358*49cdfc7eSAndroid Build Coastguard WorkerIf the return value of 'wait' is positive or zero, this macro will print a pass 359*49cdfc7eSAndroid Build Coastguard Workerresult and set +TST_PASS+ appropriately. If the return value is negative, then 360*49cdfc7eSAndroid Build Coastguard Workerit will print fail. There are many similar macros to those shown here, please 361*49cdfc7eSAndroid Build Coastguard Workersee 'tst_test_macros.h'. 362*49cdfc7eSAndroid Build Coastguard Worker 363*49cdfc7eSAndroid Build Coastguard Worker[source,c] 364*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 365*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_EQ_LI(val1, val2); 366*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_EQ_UI(val1, val2); 367*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_EQ_SZ(val1, val2); 368*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_EQ_SSZ(val1, val2); 369*49cdfc7eSAndroid Build Coastguard Worker 370*49cdfc7eSAndroid Build Coastguard Worker/* Use as */ 371*49cdfc7eSAndroid Build Coastguard WorkerTST_EXP_EQ_LI(sig_caught, SIGCHLD); 372*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 373*49cdfc7eSAndroid Build Coastguard Worker 374*49cdfc7eSAndroid Build Coastguard WorkerSet of macros for different integer type comparsions. These macros print the 375*49cdfc7eSAndroid Build Coastguard Workervariable names as well as values in both pass and fail scenarios. 376*49cdfc7eSAndroid Build Coastguard Worker 377*49cdfc7eSAndroid Build Coastguard Worker[source,c] 378*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 379*49cdfc7eSAndroid Build Coastguard Workerconst char *tst_strsig(int sig); 380*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 381*49cdfc7eSAndroid Build Coastguard Worker 382*49cdfc7eSAndroid Build Coastguard WorkerReturn the given signal number's corresponding string. 383*49cdfc7eSAndroid Build Coastguard Worker 384*49cdfc7eSAndroid Build Coastguard Worker[source,c] 385*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 386*49cdfc7eSAndroid Build Coastguard Workerconst char *tst_strerrno(int err); 387*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 388*49cdfc7eSAndroid Build Coastguard Worker 389*49cdfc7eSAndroid Build Coastguard WorkerReturn the given errno number's corresponding string. Using this function to 390*49cdfc7eSAndroid Build Coastguard Workertranslate 'errno' values to strings is preferred. You should not use the 391*49cdfc7eSAndroid Build Coastguard Worker'strerror()' function in the testcases. 392*49cdfc7eSAndroid Build Coastguard Worker 393*49cdfc7eSAndroid Build Coastguard Worker[source,c] 394*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 395*49cdfc7eSAndroid Build Coastguard Workerconst char *tst_strstatus(int status); 396*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 397*49cdfc7eSAndroid Build Coastguard Worker 398*49cdfc7eSAndroid Build Coastguard WorkerReturns string describing the status as returned by 'wait()'. 399*49cdfc7eSAndroid Build Coastguard Worker 400*49cdfc7eSAndroid Build Coastguard WorkerWARNING: This function is not thread safe. 401*49cdfc7eSAndroid Build Coastguard Worker 402*49cdfc7eSAndroid Build Coastguard Worker[source,c] 403*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 404*49cdfc7eSAndroid Build Coastguard Workervoid tst_set_max_runtime(int max_runtime); 405*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 406*49cdfc7eSAndroid Build Coastguard Worker 407*49cdfc7eSAndroid Build Coastguard WorkerAllows for setting max_runtime per test iteration dynamically in the test 'setup()', 408*49cdfc7eSAndroid Build Coastguard Workerthe timeout is specified in seconds. There are a few testcases whose runtime 409*49cdfc7eSAndroid Build Coastguard Workercan vary arbitrarily, these can disable timeouts by setting it to 410*49cdfc7eSAndroid Build Coastguard WorkerTST_UNLIMITED_RUNTIME. 411*49cdfc7eSAndroid Build Coastguard Worker 412*49cdfc7eSAndroid Build Coastguard Worker[source,c] 413*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 414*49cdfc7eSAndroid Build Coastguard Workervoid tst_flush(void); 415*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 416*49cdfc7eSAndroid Build Coastguard Worker 417*49cdfc7eSAndroid Build Coastguard WorkerFlush output streams, handling errors appropriately. 418*49cdfc7eSAndroid Build Coastguard Worker 419*49cdfc7eSAndroid Build Coastguard WorkerThis function is rarely needed when you have to flush the output streams 420*49cdfc7eSAndroid Build Coastguard Workerbefore calling 'fork()' or 'clone()'. Note that the 'SAFE_FORK()' and 'SAFE_CLONE()' 421*49cdfc7eSAndroid Build Coastguard Workercalls this function automatically. See 2.4 FILE buffers and fork() for explanation 422*49cdfc7eSAndroid Build Coastguard Workerwhy is this needed. 423*49cdfc7eSAndroid Build Coastguard Worker 424*49cdfc7eSAndroid Build Coastguard Worker1.3 Test temporary directory 425*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 426*49cdfc7eSAndroid Build Coastguard Worker 427*49cdfc7eSAndroid Build Coastguard WorkerIf '.needs_tmpdir' is set to '1' in the 'struct tst_test' unique test 428*49cdfc7eSAndroid Build Coastguard Workertemporary is created and it's set as the test working directory. Tests *MUST 429*49cdfc7eSAndroid Build Coastguard WorkerNOT* create temporary files outside that directory. The flag is not needed to 430*49cdfc7eSAndroid Build Coastguard Workerbe set when use these flags: '.all_filesystems', '.format_device', '.mntpoint', 431*49cdfc7eSAndroid Build Coastguard Worker'.mount_device' '.needs_checkpoints', '.needs_device', '.resource_file' 432*49cdfc7eSAndroid Build Coastguard Worker(these flags imply creating temporary directory). 433*49cdfc7eSAndroid Build Coastguard Worker 434*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: Close all file descriptors (that point to files in test temporary 435*49cdfc7eSAndroid Build Coastguard Worker directory, even the unlinked ones) either in the 'test()' function 436*49cdfc7eSAndroid Build Coastguard Worker or in the test 'cleanup()' otherwise the test may break temporary 437*49cdfc7eSAndroid Build Coastguard Worker directory removal on NFS (look for "NFS silly rename"). 438*49cdfc7eSAndroid Build Coastguard Worker 439*49cdfc7eSAndroid Build Coastguard Worker1.4 Safe macros 440*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~ 441*49cdfc7eSAndroid Build Coastguard Worker 442*49cdfc7eSAndroid Build Coastguard WorkerSafe macros aim to simplify error checking in test preparation. Instead of 443*49cdfc7eSAndroid Build Coastguard Workercalling system API functions, checking for their return value and aborting the 444*49cdfc7eSAndroid Build Coastguard Workertest if the operation has failed, you just use corresponding safe macro. 445*49cdfc7eSAndroid Build Coastguard Worker 446*49cdfc7eSAndroid Build Coastguard WorkerUse them whenever it's possible. 447*49cdfc7eSAndroid Build Coastguard Worker 448*49cdfc7eSAndroid Build Coastguard WorkerInstead of writing: 449*49cdfc7eSAndroid Build Coastguard Worker 450*49cdfc7eSAndroid Build Coastguard Worker[source,c] 451*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 452*49cdfc7eSAndroid Build Coastguard Workerfd = open("/dev/null", O_RDONLY); 453*49cdfc7eSAndroid Build Coastguard Workerif (fd < 0) 454*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "opening /dev/null failed"); 455*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 456*49cdfc7eSAndroid Build Coastguard Worker 457*49cdfc7eSAndroid Build Coastguard WorkerYou write just: 458*49cdfc7eSAndroid Build Coastguard Worker 459*49cdfc7eSAndroid Build Coastguard Worker[source,c] 460*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 461*49cdfc7eSAndroid Build Coastguard Workerfd = SAFE_OPEN("/dev/null", O_RDONLY); 462*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 463*49cdfc7eSAndroid Build Coastguard Worker 464*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: The 'SAFE_CLOSE()' function also sets the passed file descriptor to -1 465*49cdfc7eSAndroid Build Coastguard Worker after it's successfully closed. 466*49cdfc7eSAndroid Build Coastguard Worker 467*49cdfc7eSAndroid Build Coastguard WorkerThey can also simplify reading and writing of sysfs files, you can, for 468*49cdfc7eSAndroid Build Coastguard Workerexample, do: 469*49cdfc7eSAndroid Build Coastguard Worker 470*49cdfc7eSAndroid Build Coastguard Worker[source,c] 471*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 472*49cdfc7eSAndroid Build Coastguard WorkerSAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%lu", &pid_max); 473*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 474*49cdfc7eSAndroid Build Coastguard Worker 475*49cdfc7eSAndroid Build Coastguard WorkerSee 'include/tst_safe_macros.h', 'include/tst_safe_stdio.h' and 476*49cdfc7eSAndroid Build Coastguard Worker'include/tst_safe_file_ops.h' and 'include/tst_safe_net.h' for a complete list. 477*49cdfc7eSAndroid Build Coastguard Worker 478*49cdfc7eSAndroid Build Coastguard Worker1.5 Test specific command line options 479*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 480*49cdfc7eSAndroid Build Coastguard Worker 481*49cdfc7eSAndroid Build Coastguard Worker[source,c] 482*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 483*49cdfc7eSAndroid Build Coastguard Workerstruct tst_option { 484*49cdfc7eSAndroid Build Coastguard Worker char *optstr; 485*49cdfc7eSAndroid Build Coastguard Worker char **arg; 486*49cdfc7eSAndroid Build Coastguard Worker char *help; 487*49cdfc7eSAndroid Build Coastguard Worker}; 488*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 489*49cdfc7eSAndroid Build Coastguard Worker 490*49cdfc7eSAndroid Build Coastguard WorkerTest specific command line parameters can be passed with the 'NULL' terminated 491*49cdfc7eSAndroid Build Coastguard Workerarray of 'struct tst_option'. The 'optstr' is the command line option i.e. "o" 492*49cdfc7eSAndroid Build Coastguard Workeror "o:" if option has a parameter. Only short options are supported. The 'arg' 493*49cdfc7eSAndroid Build Coastguard Workeris where 'optarg' is stored upon match. If option has no parameter it's set to 494*49cdfc7eSAndroid Build Coastguard Workernon-'NULL' value if option was present. The 'help' is a short help string. 495*49cdfc7eSAndroid Build Coastguard Worker 496*49cdfc7eSAndroid Build Coastguard WorkerNOTE: The test parameters must not collide with common test parameters defined 497*49cdfc7eSAndroid Build Coastguard Worker in the library the currently used ones are +-i+, +-I+, +-C+, and +-h+. 498*49cdfc7eSAndroid Build Coastguard Worker 499*49cdfc7eSAndroid Build Coastguard Worker[source,c] 500*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 501*49cdfc7eSAndroid Build Coastguard Workerint tst_parse_int(const char *str, int *val, int min, int max); 502*49cdfc7eSAndroid Build Coastguard Workerint tst_parse_long(const char *str, long *val, long min, long max); 503*49cdfc7eSAndroid Build Coastguard Workerint tst_parse_float(const char *str, float *val, float min, float max); 504*49cdfc7eSAndroid Build Coastguard Workerint tst_parse_filesize(const char *str, long long *val, long long min, long long max); 505*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 506*49cdfc7eSAndroid Build Coastguard Worker 507*49cdfc7eSAndroid Build Coastguard WorkerHelpers for parsing the strings returned in the 'struct tst_option'. 508*49cdfc7eSAndroid Build Coastguard Worker 509*49cdfc7eSAndroid Build Coastguard WorkerHelpers return zero on success and 'errno', mostly 'EINVAL' or 'ERANGE', on 510*49cdfc7eSAndroid Build Coastguard Workerfailure. 511*49cdfc7eSAndroid Build Coastguard Worker 512*49cdfc7eSAndroid Build Coastguard WorkerHelpers functions are no-op if 'str' is 'NULL'. 513*49cdfc7eSAndroid Build Coastguard Worker 514*49cdfc7eSAndroid Build Coastguard WorkerThe valid range for result includes both 'min' and 'max'. 515*49cdfc7eSAndroid Build Coastguard Worker 516*49cdfc7eSAndroid Build Coastguard WorkerIn particular, 'tst_parse_filesize' function accepts prefix multiplies such as 517*49cdfc7eSAndroid Build Coastguard Worker"k/K for kilobytes, "m/M" for megabytes and "g/G" for gigabytes. For example, 518*49cdfc7eSAndroid Build Coastguard Worker10K are converted into 10240 bytes. 519*49cdfc7eSAndroid Build Coastguard Worker 520*49cdfc7eSAndroid Build Coastguard Worker[source,c] 521*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 522*49cdfc7eSAndroid Build Coastguard Worker#include <limits.h> 523*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 524*49cdfc7eSAndroid Build Coastguard Worker 525*49cdfc7eSAndroid Build Coastguard Workerstatic char *str_threads; 526*49cdfc7eSAndroid Build Coastguard Workerstatic int threads = 10; 527*49cdfc7eSAndroid Build Coastguard Worker 528*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 529*49cdfc7eSAndroid Build Coastguard Worker{ 530*49cdfc7eSAndroid Build Coastguard Worker if (tst_parse_int(str_threads, &threads, 1, INT_MAX)) 531*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Invalid number of threads '%s'", str_threads); 532*49cdfc7eSAndroid Build Coastguard Worker 533*49cdfc7eSAndroid Build Coastguard Worker ... 534*49cdfc7eSAndroid Build Coastguard Worker} 535*49cdfc7eSAndroid Build Coastguard Worker 536*49cdfc7eSAndroid Build Coastguard Workerstatic void test_threads(void) 537*49cdfc7eSAndroid Build Coastguard Worker{ 538*49cdfc7eSAndroid Build Coastguard Worker ... 539*49cdfc7eSAndroid Build Coastguard Worker 540*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < threads; i++) { 541*49cdfc7eSAndroid Build Coastguard Worker ... 542*49cdfc7eSAndroid Build Coastguard Worker } 543*49cdfc7eSAndroid Build Coastguard Worker 544*49cdfc7eSAndroid Build Coastguard Worker ... 545*49cdfc7eSAndroid Build Coastguard Worker} 546*49cdfc7eSAndroid Build Coastguard Worker 547*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 548*49cdfc7eSAndroid Build Coastguard Worker ... 549*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) { 550*49cdfc7eSAndroid Build Coastguard Worker {"t:", &str_threads, "Number of threads (default 10)"}, 551*49cdfc7eSAndroid Build Coastguard Worker {}, 552*49cdfc7eSAndroid Build Coastguard Worker ... 553*49cdfc7eSAndroid Build Coastguard Worker}; 554*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 555*49cdfc7eSAndroid Build Coastguard Worker 556*49cdfc7eSAndroid Build Coastguard Worker 557*49cdfc7eSAndroid Build Coastguard Worker1.6 Runtime kernel version detection 558*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 559*49cdfc7eSAndroid Build Coastguard Worker 560*49cdfc7eSAndroid Build Coastguard WorkerTestcases for newly added kernel functionality require kernel newer than a 561*49cdfc7eSAndroid Build Coastguard Workercertain version to run. All you need to skip a test on older kernels is to 562*49cdfc7eSAndroid Build Coastguard Workerset the '.min_kver' string in the 'struct tst_test' to a minimal required 563*49cdfc7eSAndroid Build Coastguard Workerkernel version, e.g. '.min_kver = "4.10.0"'. 564*49cdfc7eSAndroid Build Coastguard Worker 565*49cdfc7eSAndroid Build Coastguard WorkerFor more complicated operations such as skipping a test for a certain range 566*49cdfc7eSAndroid Build Coastguard Workerof kernel versions, following functions could be used: 567*49cdfc7eSAndroid Build Coastguard Worker 568*49cdfc7eSAndroid Build Coastguard Worker[source,c] 569*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 570*49cdfc7eSAndroid Build Coastguard Workerint tst_kvercmp(int r1, int r2, int r3); 571*49cdfc7eSAndroid Build Coastguard Worker 572*49cdfc7eSAndroid Build Coastguard Workerstruct tst_kern_exv { 573*49cdfc7eSAndroid Build Coastguard Worker char *dist_name; 574*49cdfc7eSAndroid Build Coastguard Worker char *extra_ver; 575*49cdfc7eSAndroid Build Coastguard Worker}; 576*49cdfc7eSAndroid Build Coastguard Worker 577*49cdfc7eSAndroid Build Coastguard Workerint tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers); 578*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 579*49cdfc7eSAndroid Build Coastguard Worker 580*49cdfc7eSAndroid Build Coastguard WorkerThese two functions are intended for runtime kernel version detection. They 581*49cdfc7eSAndroid Build Coastguard Workerparse the output from 'uname()' and compare it to the passed values. 582*49cdfc7eSAndroid Build Coastguard Worker 583*49cdfc7eSAndroid Build Coastguard WorkerThe return value is similar to the 'strcmp()' function, i.e. zero means equal, 584*49cdfc7eSAndroid Build Coastguard Workernegative value means that the kernel is older than the expected value and 585*49cdfc7eSAndroid Build Coastguard Workerpositive means that it's newer. 586*49cdfc7eSAndroid Build Coastguard Worker 587*49cdfc7eSAndroid Build Coastguard WorkerThe second function 'tst_kvercmp2()' allows for specifying per-vendor table of 588*49cdfc7eSAndroid Build Coastguard Workerkernel versions as vendors typically backport fixes to their kernels and the 589*49cdfc7eSAndroid Build Coastguard Workertest may be relevant even if the kernel version does not suggests so. 590*49cdfc7eSAndroid Build Coastguard Worker 591*49cdfc7eSAndroid Build Coastguard Worker[source,c] 592*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 593*49cdfc7eSAndroid Build Coastguard Workerif (tst_kvercmp(5, 19, 0) >= 0) 594*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "Test valid only for kernel < 5.19"); 595*49cdfc7eSAndroid Build Coastguard Worker 596*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_kern_exv kvers[] = { 597*49cdfc7eSAndroid Build Coastguard Worker { "UBUNTU", "4.4.0-48.69" }, 598*49cdfc7eSAndroid Build Coastguard Worker { NULL, NULL}, 599*49cdfc7eSAndroid Build Coastguard Worker}; 600*49cdfc7eSAndroid Build Coastguard Worker 601*49cdfc7eSAndroid Build Coastguard Workerif (tst_kvercmp2(4, 4, 27, kvers) < 0) 602*49cdfc7eSAndroid Build Coastguard Worker /* code for kernel < v4.4.27 or ubuntu kernel < 4.4.0-48.69 */ 603*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 604*49cdfc7eSAndroid Build Coastguard Worker 605*49cdfc7eSAndroid Build Coastguard WorkerWARNING: The shell 'tst_kvercmp' maps the result into unsigned integer - the 606*49cdfc7eSAndroid Build Coastguard Worker process exit value. 607*49cdfc7eSAndroid Build Coastguard Worker 608*49cdfc7eSAndroid Build Coastguard WorkerNOTE: See also LTP 609*49cdfc7eSAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp/wiki/Supported-kernel,-libc,-toolchain-versions#13-minimal-supported-kernel-version[minimal supported kernel version]. 610*49cdfc7eSAndroid Build Coastguard Worker 611*49cdfc7eSAndroid Build Coastguard Worker1.7 Fork()-ing 612*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~ 613*49cdfc7eSAndroid Build Coastguard Worker 614*49cdfc7eSAndroid Build Coastguard WorkerBe wary that if the test forks and there were messages printed by the 615*49cdfc7eSAndroid Build Coastguard Worker'tst_*()' interfaces, the data may still be in libc/kernel buffers and these 616*49cdfc7eSAndroid Build Coastguard Worker*ARE NOT* flushed automatically. 617*49cdfc7eSAndroid Build Coastguard Worker 618*49cdfc7eSAndroid Build Coastguard WorkerThis happens when 'stdout' gets redirected to a file. In this case, the 619*49cdfc7eSAndroid Build Coastguard Worker'stdout' is not line buffered, but block buffered. Hence after a fork content 620*49cdfc7eSAndroid Build Coastguard Workerof the buffers will be printed by the parent and each of the children. 621*49cdfc7eSAndroid Build Coastguard Worker 622*49cdfc7eSAndroid Build Coastguard WorkerTo avoid that you should use 'SAFE_FORK()', 'SAFE_CLONE()' or 'tst_clone()'. 623*49cdfc7eSAndroid Build Coastguard Worker 624*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: You have to set the '.forks_child' flag in the test structure 625*49cdfc7eSAndroid Build Coastguard Worker if your testcase forks or calls 'SAFE_CLONE()'. 626*49cdfc7eSAndroid Build Coastguard Worker 627*49cdfc7eSAndroid Build Coastguard Worker1.8 Doing the test in the child process 628*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 629*49cdfc7eSAndroid Build Coastguard Worker 630*49cdfc7eSAndroid Build Coastguard WorkerResults reported by 'tst_res()' are propagated to the parent test process via 631*49cdfc7eSAndroid Build Coastguard Workerblock of shared memory. 632*49cdfc7eSAndroid Build Coastguard Worker 633*49cdfc7eSAndroid Build Coastguard WorkerCalling 'tst_brk()' causes child process to exit with non-zero exit value. 634*49cdfc7eSAndroid Build Coastguard WorkerWhich means that it's safe to use 'SAFE_*()' macros in the child processes as 635*49cdfc7eSAndroid Build Coastguard Workerwell. 636*49cdfc7eSAndroid Build Coastguard Worker 637*49cdfc7eSAndroid Build Coastguard WorkerChildren that outlive the 'test()' function execution are waited for in the 638*49cdfc7eSAndroid Build Coastguard Workertest library. Unclean child exit (killed by signal, non-zero exit value, etc.) 639*49cdfc7eSAndroid Build Coastguard Workerwill cause the main test process to exit with 'tst_brk()', which especially 640*49cdfc7eSAndroid Build Coastguard Workermeans that 'TBROK' propagated from a child process will cause the whole test 641*49cdfc7eSAndroid Build Coastguard Workerto exit with 'TBROK'. 642*49cdfc7eSAndroid Build Coastguard Worker 643*49cdfc7eSAndroid Build Coastguard WorkerIf a test needs a child that segfaults or does anything else that cause it to 644*49cdfc7eSAndroid Build Coastguard Workerexit uncleanly all you need to do is to wait for such children from the 645*49cdfc7eSAndroid Build Coastguard Worker'test()' function so that it's reaped before the main test exits the 'test()' 646*49cdfc7eSAndroid Build Coastguard Workerfunction. 647*49cdfc7eSAndroid Build Coastguard Worker 648*49cdfc7eSAndroid Build Coastguard Worker[source,c] 649*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 650*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 651*49cdfc7eSAndroid Build Coastguard Worker 652*49cdfc7eSAndroid Build Coastguard Workervoid tst_reap_children(void); 653*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 654*49cdfc7eSAndroid Build Coastguard Worker 655*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_reap_children()' function makes the process wait for all of its 656*49cdfc7eSAndroid Build Coastguard Workerchildren and exits with 'tst_brk(TBROK, ...)' if any of them returned 657*49cdfc7eSAndroid Build Coastguard Workera non zero exit code. 658*49cdfc7eSAndroid Build Coastguard Worker 659*49cdfc7eSAndroid Build Coastguard WorkerWhen using 'SAFE_CLONE' or 'tst_clone', this may not work depending on 660*49cdfc7eSAndroid Build Coastguard Workerthe parameters passed to clone. The following call to 'SAFE_CLONE' is 661*49cdfc7eSAndroid Build Coastguard Workeridentical to 'fork()', so will work as expected. 662*49cdfc7eSAndroid Build Coastguard Worker 663*49cdfc7eSAndroid Build Coastguard Worker[source,c] 664*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 665*49cdfc7eSAndroid Build Coastguard Workerconst struct tst_clone_args args = { 666*49cdfc7eSAndroid Build Coastguard Worker .exit_signal = SIGCHLD, 667*49cdfc7eSAndroid Build Coastguard Worker}; 668*49cdfc7eSAndroid Build Coastguard Worker 669*49cdfc7eSAndroid Build Coastguard WorkerSAFE_CLONE(&args); 670*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 671*49cdfc7eSAndroid Build Coastguard Worker 672*49cdfc7eSAndroid Build Coastguard WorkerIf 'exit_signal' is set to something else, then this will break 673*49cdfc7eSAndroid Build Coastguard Worker'tst_reap_children'. It's not expected that all parameters to clone will 674*49cdfc7eSAndroid Build Coastguard Workerwork with the LTP library unless specific action is taken by the test code. 675*49cdfc7eSAndroid Build Coastguard Worker 676*49cdfc7eSAndroid Build Coastguard Worker.Using 'tst_res()' from binaries started by 'exec()' 677*49cdfc7eSAndroid Build Coastguard Worker[source,c] 678*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 679*49cdfc7eSAndroid Build Coastguard Worker/* test.c */ 680*49cdfc7eSAndroid Build Coastguard Worker#define _GNU_SOURCE 681*49cdfc7eSAndroid Build Coastguard Worker#include <unistd.h> 682*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 683*49cdfc7eSAndroid Build Coastguard Worker 684*49cdfc7eSAndroid Build Coastguard Workerstatic void do_test(void) 685*49cdfc7eSAndroid Build Coastguard Worker{ 686*49cdfc7eSAndroid Build Coastguard Worker char *const argv[] = {"test_exec_child", NULL}; 687*49cdfc7eSAndroid Build Coastguard Worker char path[4096]; 688*49cdfc7eSAndroid Build Coastguard Worker 689*49cdfc7eSAndroid Build Coastguard Worker if (tst_get_path("test_exec_child", path, sizeof(path))) 690*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "Couldn't find test_exec_child in $PATH"); 691*49cdfc7eSAndroid Build Coastguard Worker 692*49cdfc7eSAndroid Build Coastguard Worker execve(path, argv, environ); 693*49cdfc7eSAndroid Build Coastguard Worker 694*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "EXEC!"); 695*49cdfc7eSAndroid Build Coastguard Worker} 696*49cdfc7eSAndroid Build Coastguard Worker 697*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 698*49cdfc7eSAndroid Build Coastguard Worker .test_all = do_test, 699*49cdfc7eSAndroid Build Coastguard Worker .child_needs_reinit = 1, 700*49cdfc7eSAndroid Build Coastguard Worker}; 701*49cdfc7eSAndroid Build Coastguard Worker 702*49cdfc7eSAndroid Build Coastguard Worker/* test_exec_child.c */ 703*49cdfc7eSAndroid Build Coastguard Worker#define TST_NO_DEFAULT_MAIN 704*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 705*49cdfc7eSAndroid Build Coastguard Worker 706*49cdfc7eSAndroid Build Coastguard Workerint main(void) 707*49cdfc7eSAndroid Build Coastguard Worker{ 708*49cdfc7eSAndroid Build Coastguard Worker tst_reinit(); 709*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Child passed!"); 710*49cdfc7eSAndroid Build Coastguard Worker return 0; 711*49cdfc7eSAndroid Build Coastguard Worker} 712*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 713*49cdfc7eSAndroid Build Coastguard Worker 714*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_res()' function can be also used from binaries started by 'exec()', 715*49cdfc7eSAndroid Build Coastguard Workerthe parent test process has to set the '.child_needs_reinit' flag so that the 716*49cdfc7eSAndroid Build Coastguard Workerlibrary prepares for it and has to make sure the 'LTP_IPC_PATH' environment 717*49cdfc7eSAndroid Build Coastguard Workervariable is passed down, then the very first thing the program has to call in 718*49cdfc7eSAndroid Build Coastguard Worker'main()' is 'tst_reinit()' that sets up the IPC. 719*49cdfc7eSAndroid Build Coastguard Worker 720*49cdfc7eSAndroid Build Coastguard Worker1.9 Fork() and Parent-child synchronization 721*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 722*49cdfc7eSAndroid Build Coastguard Worker 723*49cdfc7eSAndroid Build Coastguard WorkerAs LTP tests are written for Linux, most of the tests involve fork()-ing and 724*49cdfc7eSAndroid Build Coastguard Workerparent-child process synchronization. LTP includes a checkpoint library that 725*49cdfc7eSAndroid Build Coastguard Workerprovides wait/wake futex based functions. 726*49cdfc7eSAndroid Build Coastguard Worker 727*49cdfc7eSAndroid Build Coastguard WorkerIn order to use checkpoints the '.needs_checkpoints' flag in the 'struct 728*49cdfc7eSAndroid Build Coastguard Workertst_test' must be set to '1', this causes the test library to initialize 729*49cdfc7eSAndroid Build Coastguard Workercheckpoints before the 'test()' function is called. 730*49cdfc7eSAndroid Build Coastguard Worker 731*49cdfc7eSAndroid Build Coastguard Worker[source,c] 732*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 733*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 734*49cdfc7eSAndroid Build Coastguard Worker 735*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAIT(id) 736*49cdfc7eSAndroid Build Coastguard Worker 737*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAIT2(id, msec_timeout) 738*49cdfc7eSAndroid Build Coastguard Worker 739*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE(id) 740*49cdfc7eSAndroid Build Coastguard Worker 741*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE2(id, nr_wake) 742*49cdfc7eSAndroid Build Coastguard Worker 743*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE_AND_WAIT(id) 744*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 745*49cdfc7eSAndroid Build Coastguard Worker 746*49cdfc7eSAndroid Build Coastguard WorkerThe checkpoint interface provides pair of wake and wait functions. The 'id' is 747*49cdfc7eSAndroid Build Coastguard Workerunsigned integer which specifies checkpoint to wake/wait for. As a matter of 748*49cdfc7eSAndroid Build Coastguard Workerfact it's an index to an array stored in a shared memory, so it starts on 749*49cdfc7eSAndroid Build Coastguard Worker'0' and there should be enough room for at least of hundred of them. 750*49cdfc7eSAndroid Build Coastguard Worker 751*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_CHECKPOINT_WAIT()' and 'TST_CHECKPOINT_WAIT2()' suspends process 752*49cdfc7eSAndroid Build Coastguard Workerexecution until it's woken up or until timeout is reached. 753*49cdfc7eSAndroid Build Coastguard Worker 754*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_CHECKPOINT_WAKE()' wakes one process waiting on the checkpoint. 755*49cdfc7eSAndroid Build Coastguard WorkerIf no process is waiting the function retries until it success or until 756*49cdfc7eSAndroid Build Coastguard Workertimeout is reached. 757*49cdfc7eSAndroid Build Coastguard Worker 758*49cdfc7eSAndroid Build Coastguard WorkerIf timeout has been reached process exits with appropriate error message (uses 759*49cdfc7eSAndroid Build Coastguard Worker'tst_brk()'). 760*49cdfc7eSAndroid Build Coastguard Worker 761*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_CHECKPOINT_WAKE2()' does the same as 'TST_CHECKPOINT_WAKE()' but can 762*49cdfc7eSAndroid Build Coastguard Workerbe used to wake precisely 'nr_wake' processes. 763*49cdfc7eSAndroid Build Coastguard Worker 764*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_CHECKPOINT_WAKE_AND_WAIT()' is a shorthand for doing wake and then 765*49cdfc7eSAndroid Build Coastguard Workerimmediately waiting on the same checkpoint. 766*49cdfc7eSAndroid Build Coastguard Worker 767*49cdfc7eSAndroid Build Coastguard WorkerChild processes created via 'SAFE_FORK()' are ready to use the checkpoint 768*49cdfc7eSAndroid Build Coastguard Workersynchronization functions, as they inherited the mapped page automatically. 769*49cdfc7eSAndroid Build Coastguard Worker 770*49cdfc7eSAndroid Build Coastguard WorkerChild processes started via 'exec()', or any other processes not forked from 771*49cdfc7eSAndroid Build Coastguard Workerthe test process must initialize the checkpoint by calling 'tst_reinit()'. 772*49cdfc7eSAndroid Build Coastguard Worker 773*49cdfc7eSAndroid Build Coastguard WorkerFor the details of the interface, look into the 'include/tst_checkpoint.h'. 774*49cdfc7eSAndroid Build Coastguard Worker 775*49cdfc7eSAndroid Build Coastguard Worker[source,c] 776*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 777*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 778*49cdfc7eSAndroid Build Coastguard Worker 779*49cdfc7eSAndroid Build Coastguard Worker/* 780*49cdfc7eSAndroid Build Coastguard Worker * Waits for process state change. 781*49cdfc7eSAndroid Build Coastguard Worker * 782*49cdfc7eSAndroid Build Coastguard Worker * The state is one of the following: 783*49cdfc7eSAndroid Build Coastguard Worker * 784*49cdfc7eSAndroid Build Coastguard Worker * R - process is running 785*49cdfc7eSAndroid Build Coastguard Worker * S - process is sleeping 786*49cdfc7eSAndroid Build Coastguard Worker * D - process sleeping uninterruptibly 787*49cdfc7eSAndroid Build Coastguard Worker * Z - zombie process 788*49cdfc7eSAndroid Build Coastguard Worker * T - process is traced 789*49cdfc7eSAndroid Build Coastguard Worker */ 790*49cdfc7eSAndroid Build Coastguard WorkerTST_PROCESS_STATE_WAIT(pid, state, msec_timeout) 791*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 792*49cdfc7eSAndroid Build Coastguard Worker 793*49cdfc7eSAndroid Build Coastguard WorkerThe 'TST_PROCESS_STATE_WAIT()' waits until process 'pid' is in requested 794*49cdfc7eSAndroid Build Coastguard Worker'state' or timeout is reached. The call polls +/proc/pid/stat+ to get this 795*49cdfc7eSAndroid Build Coastguard Workerinformation. A timeout of 0 will wait infinitely. 796*49cdfc7eSAndroid Build Coastguard Worker 797*49cdfc7eSAndroid Build Coastguard WorkerOn timeout -1 is returned and errno set to ETIMEDOUT. 798*49cdfc7eSAndroid Build Coastguard Worker 799*49cdfc7eSAndroid Build Coastguard WorkerIt's mostly used with state 'S' which means that process is sleeping in kernel 800*49cdfc7eSAndroid Build Coastguard Workerfor example in 'pause()' or any other blocking syscall. 801*49cdfc7eSAndroid Build Coastguard Worker 802*49cdfc7eSAndroid Build Coastguard Worker1.10 Signals and signal handlers 803*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 804*49cdfc7eSAndroid Build Coastguard Worker 805*49cdfc7eSAndroid Build Coastguard WorkerIf you need to use signal handlers, keep the code short and simple. Don't 806*49cdfc7eSAndroid Build Coastguard Workerforget that the signal handler is called asynchronously and can interrupt the 807*49cdfc7eSAndroid Build Coastguard Workercode execution at any place. 808*49cdfc7eSAndroid Build Coastguard Worker 809*49cdfc7eSAndroid Build Coastguard WorkerThis means that problems arise when global state is changed both from the test 810*49cdfc7eSAndroid Build Coastguard Workercode and signal handler, which will occasionally lead to: 811*49cdfc7eSAndroid Build Coastguard Worker 812*49cdfc7eSAndroid Build Coastguard Worker* Data corruption (data gets into inconsistent state), this may happen, for 813*49cdfc7eSAndroid Build Coastguard Worker example, for any operations on 'FILE' objects. 814*49cdfc7eSAndroid Build Coastguard Worker 815*49cdfc7eSAndroid Build Coastguard Worker* Deadlock, this happens, for example, if you call 'malloc(2)', 'free(2)', 816*49cdfc7eSAndroid Build Coastguard Worker etc. from both the test code and the signal handler at the same time since 817*49cdfc7eSAndroid Build Coastguard Worker 'malloc' has global lock for it's internal data structures. (Be wary that 818*49cdfc7eSAndroid Build Coastguard Worker 'malloc(2)' is used by the libc functions internally too.) 819*49cdfc7eSAndroid Build Coastguard Worker 820*49cdfc7eSAndroid Build Coastguard Worker* Any other unreproducible and unexpected behavior. 821*49cdfc7eSAndroid Build Coastguard Worker 822*49cdfc7eSAndroid Build Coastguard WorkerQuite common mistake is to call 'exit(3)' from a signal handler. Note that this 823*49cdfc7eSAndroid Build Coastguard Workerfunction is not signal-async-safe as it flushes buffers, etc. If you need to 824*49cdfc7eSAndroid Build Coastguard Workerexit a test immediately from a signal handler use '_exit(2)' instead. 825*49cdfc7eSAndroid Build Coastguard Worker 826*49cdfc7eSAndroid Build Coastguard WorkerTIP: See 'man 7 signal' for the list of signal-async-safe functions. 827*49cdfc7eSAndroid Build Coastguard Worker 828*49cdfc7eSAndroid Build Coastguard WorkerIf a signal handler sets a variable, its declaration must be 'volatile', 829*49cdfc7eSAndroid Build Coastguard Workerotherwise compiler may misoptimize the code. This is because the variable may 830*49cdfc7eSAndroid Build Coastguard Workernot be changed in the compiler code flow analysis. There is 'sig_atomic_t' 831*49cdfc7eSAndroid Build Coastguard Workertype defined in C99 but this one *DOES NOT* imply 'volatile' (it's just a 832*49cdfc7eSAndroid Build Coastguard Worker'typedef' to 'int'). So the correct type for a flag that is changed from a 833*49cdfc7eSAndroid Build Coastguard Workersignal handler is either 'volatile int' or 'volatile sig_atomic_t'. 834*49cdfc7eSAndroid Build Coastguard Worker 835*49cdfc7eSAndroid Build Coastguard WorkerIf a crash (e.g. triggered by signal SIGSEGV) is expected in testing, you 836*49cdfc7eSAndroid Build Coastguard Workercan avoid creation of core files by calling 'tst_no_corefile()' function. 837*49cdfc7eSAndroid Build Coastguard WorkerThis takes effect for process (and its children) which invoked it, unless 838*49cdfc7eSAndroid Build Coastguard Workerthey subsequently modify RLIMIT_CORE. 839*49cdfc7eSAndroid Build Coastguard Worker 840*49cdfc7eSAndroid Build Coastguard WorkerNote that LTP library will reap any processes that test didn't reap itself, 841*49cdfc7eSAndroid Build Coastguard Workerand report any non-zero exit code as failure. 842*49cdfc7eSAndroid Build Coastguard Worker 843*49cdfc7eSAndroid Build Coastguard Worker1.11 Kernel Modules 844*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~ 845*49cdfc7eSAndroid Build Coastguard Worker 846*49cdfc7eSAndroid Build Coastguard WorkerThere are certain cases where the test needs a kernel part and userspace part, 847*49cdfc7eSAndroid Build Coastguard Workerhappily, LTP can build a kernel module and then insert it to the kernel on test 848*49cdfc7eSAndroid Build Coastguard Workerstart for you. See 'testcases/kernel/device-drivers/block' for details. 849*49cdfc7eSAndroid Build Coastguard Worker 850*49cdfc7eSAndroid Build Coastguard Worker1.12 Useful macros 851*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~ 852*49cdfc7eSAndroid Build Coastguard Worker 853*49cdfc7eSAndroid Build Coastguard WorkerThese macros are defined in 'include/tst_common.h'. 854*49cdfc7eSAndroid Build Coastguard Worker 855*49cdfc7eSAndroid Build Coastguard Worker[source,c] 856*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 857*49cdfc7eSAndroid Build Coastguard WorkerARRAY_SIZE(arr) 858*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 859*49cdfc7eSAndroid Build Coastguard Worker 860*49cdfc7eSAndroid Build Coastguard WorkerReturns the size of statically defined array, i.e. 861*49cdfc7eSAndroid Build Coastguard Worker'(sizeof(arr) / sizeof(*arr))' 862*49cdfc7eSAndroid Build Coastguard Worker 863*49cdfc7eSAndroid Build Coastguard Worker[source,c] 864*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 865*49cdfc7eSAndroid Build Coastguard WorkerLTP_ALIGN(x, a) 866*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 867*49cdfc7eSAndroid Build Coastguard Worker 868*49cdfc7eSAndroid Build Coastguard WorkerAligns the x to be next multiple of a. The a must be power of 2. 869*49cdfc7eSAndroid Build Coastguard Worker 870*49cdfc7eSAndroid Build Coastguard Worker[source,c] 871*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 872*49cdfc7eSAndroid Build Coastguard WorkerTST_TO_STR(s) /* stringification */ 873*49cdfc7eSAndroid Build Coastguard WorkerTST_TO_STR_(s) /* macro expansion */ 874*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 875*49cdfc7eSAndroid Build Coastguard Worker 876*49cdfc7eSAndroid Build Coastguard WorkerMacros for stringification. 877*49cdfc7eSAndroid Build Coastguard Worker 878*49cdfc7eSAndroid Build Coastguard Worker1.13 Filesystem type detection and skiplist 879*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 880*49cdfc7eSAndroid Build Coastguard Worker 881*49cdfc7eSAndroid Build Coastguard WorkerSome tests are known to fail on certain filesystems (you cannot swap on TMPFS, 882*49cdfc7eSAndroid Build Coastguard Workerthere are unimplemented 'fcntl()' etc.). 883*49cdfc7eSAndroid Build Coastguard Worker 884*49cdfc7eSAndroid Build Coastguard WorkerIf your test needs to be skipped on certain filesystems use the 885*49cdfc7eSAndroid Build Coastguard Worker'.skip_filesystems' field in the tst_test structure as follows: 886*49cdfc7eSAndroid Build Coastguard Worker 887*49cdfc7eSAndroid Build Coastguard Worker[source,c] 888*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 889*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 890*49cdfc7eSAndroid Build Coastguard Worker 891*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 892*49cdfc7eSAndroid Build Coastguard Worker ... 893*49cdfc7eSAndroid Build Coastguard Worker .skip_filesystems = (const char *const []) { 894*49cdfc7eSAndroid Build Coastguard Worker "tmpfs", 895*49cdfc7eSAndroid Build Coastguard Worker "ramfs", 896*49cdfc7eSAndroid Build Coastguard Worker "nfs", 897*49cdfc7eSAndroid Build Coastguard Worker NULL 898*49cdfc7eSAndroid Build Coastguard Worker }, 899*49cdfc7eSAndroid Build Coastguard Worker}; 900*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 901*49cdfc7eSAndroid Build Coastguard Worker 902*49cdfc7eSAndroid Build Coastguard WorkerWhen the '.all_filesystems' flag is set the '.skip_filesystems' list is passed 903*49cdfc7eSAndroid Build Coastguard Workerto the function that detects supported filesystems any listed filesystem is 904*49cdfc7eSAndroid Build Coastguard Workernot included in the resulting list of supported filesystems. 905*49cdfc7eSAndroid Build Coastguard Worker 906*49cdfc7eSAndroid Build Coastguard WorkerIf test needs to adjust expectations based on filesystem type it's also 907*49cdfc7eSAndroid Build Coastguard Workerpossible to detect filesystem type at the runtime. This is preferably used 908*49cdfc7eSAndroid Build Coastguard Workerwhen only subset of the test is not applicable for a given filesystem. 909*49cdfc7eSAndroid Build Coastguard Worker 910*49cdfc7eSAndroid Build Coastguard WorkerNOTE: ext2, ext3 or ext4 in '.skip_filesystems' on tests which does *not* use 911*49cdfc7eSAndroid Build Coastguard Worker '.all_filesystems' needs to be defined as 'ext2/ext3/ext4'. The reason 912*49cdfc7eSAndroid Build Coastguard Worker is that it is hard to detect used filesystem due to overlapping the functionality. 913*49cdfc7eSAndroid Build Coastguard Worker OTOH tests which use '.skip_filesystems' *with* '.all_filesystems' can skip 914*49cdfc7eSAndroid Build Coastguard Worker only filesystems which are actually used in '.all_filesystems': ext2, ext3, 915*49cdfc7eSAndroid Build Coastguard Worker ext4, xfs, btrfs, vfat, exfat, ntfs, tmpfs (defined in 'fs_type_whitelist[]'). 916*49cdfc7eSAndroid Build Coastguard Worker It does not make sense to list other filesystems. 917*49cdfc7eSAndroid Build Coastguard Worker 918*49cdfc7eSAndroid Build Coastguard Worker 919*49cdfc7eSAndroid Build Coastguard Worker[source,c] 920*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 921*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 922*49cdfc7eSAndroid Build Coastguard Worker 923*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 924*49cdfc7eSAndroid Build Coastguard Worker{ 925*49cdfc7eSAndroid Build Coastguard Worker ... 926*49cdfc7eSAndroid Build Coastguard Worker 927*49cdfc7eSAndroid Build Coastguard Worker switch ((type = tst_fs_type("."))) { 928*49cdfc7eSAndroid Build Coastguard Worker case TST_NFS_MAGIC: 929*49cdfc7eSAndroid Build Coastguard Worker case TST_TMPFS_MAGIC: 930*49cdfc7eSAndroid Build Coastguard Worker case TST_RAMFS_MAGIC: 931*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "Subtest not supported on %s", 932*49cdfc7eSAndroid Build Coastguard Worker tst_fs_type_name(type)); 933*49cdfc7eSAndroid Build Coastguard Worker return; 934*49cdfc7eSAndroid Build Coastguard Worker break; 935*49cdfc7eSAndroid Build Coastguard Worker } 936*49cdfc7eSAndroid Build Coastguard Worker 937*49cdfc7eSAndroid Build Coastguard Worker ... 938*49cdfc7eSAndroid Build Coastguard Worker} 939*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 940*49cdfc7eSAndroid Build Coastguard Worker 941*49cdfc7eSAndroid Build Coastguard Worker1.14 Thread-safety in the LTP library 942*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 943*49cdfc7eSAndroid Build Coastguard Worker 944*49cdfc7eSAndroid Build Coastguard WorkerIt is safe to use library 'tst_res()' function in multi-threaded tests. 945*49cdfc7eSAndroid Build Coastguard Worker 946*49cdfc7eSAndroid Build Coastguard WorkerOnly the main thread must return from the 'test()' function to the test 947*49cdfc7eSAndroid Build Coastguard Workerlibrary and that must be done only after all threads that may call any library 948*49cdfc7eSAndroid Build Coastguard Workerfunction has been terminated. That especially means that threads that may call 949*49cdfc7eSAndroid Build Coastguard Worker'tst_brk()' must terminate before the execution of the 'test()' function 950*49cdfc7eSAndroid Build Coastguard Workerreturns to the library. This is usually done by the main thread joining all 951*49cdfc7eSAndroid Build Coastguard Workerworker threads at the end of the 'test()' function. Note that the main thread 952*49cdfc7eSAndroid Build Coastguard Workerwill never get to the library code in a case that 'tst_brk()' was called from 953*49cdfc7eSAndroid Build Coastguard Workerone of the threads since it will sleep at least in 'pthread_join()' on the 954*49cdfc7eSAndroid Build Coastguard Workerthread that called the 'tst_brk()' till 'exit()' is called by 'tst_brk()'. 955*49cdfc7eSAndroid Build Coastguard Worker 956*49cdfc7eSAndroid Build Coastguard WorkerThe test-supplied cleanup function runs *concurrently* to the rest of the 957*49cdfc7eSAndroid Build Coastguard Workerthreads in a case that cleanup was entered from 'tst_brk()'. Subsequent 958*49cdfc7eSAndroid Build Coastguard Workerthreads entering 'tst_brk()' must be suspended or terminated at the start of 959*49cdfc7eSAndroid Build Coastguard Workerthe user supplied cleanup function. It may be necessary to stop or exit 960*49cdfc7eSAndroid Build Coastguard Workerthe rest of the threads before the test cleans up as well. For example threads 961*49cdfc7eSAndroid Build Coastguard Workerthat create new files should be stopped before temporary directory is be 962*49cdfc7eSAndroid Build Coastguard Workerremoved. 963*49cdfc7eSAndroid Build Coastguard Worker 964*49cdfc7eSAndroid Build Coastguard WorkerFollowing code example shows thread safe cleanup function example using atomic 965*49cdfc7eSAndroid Build Coastguard Workerincrement as a guard. The library calls its cleanup after the execution returns 966*49cdfc7eSAndroid Build Coastguard Workerfrom the user supplied cleanup and expects that only one thread returns from 967*49cdfc7eSAndroid Build Coastguard Workerthe user supplied cleanup to the test library. 968*49cdfc7eSAndroid Build Coastguard Worker 969*49cdfc7eSAndroid Build Coastguard Worker[source,c] 970*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 971*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 972*49cdfc7eSAndroid Build Coastguard Worker 973*49cdfc7eSAndroid Build Coastguard Workerstatic void cleanup(void) 974*49cdfc7eSAndroid Build Coastguard Worker{ 975*49cdfc7eSAndroid Build Coastguard Worker static int flag; 976*49cdfc7eSAndroid Build Coastguard Worker 977*49cdfc7eSAndroid Build Coastguard Worker if (tst_atomic_inc(&flag) != 1) 978*49cdfc7eSAndroid Build Coastguard Worker pthread_exit(NULL); 979*49cdfc7eSAndroid Build Coastguard Worker 980*49cdfc7eSAndroid Build Coastguard Worker /* if needed stop the rest of the threads here */ 981*49cdfc7eSAndroid Build Coastguard Worker 982*49cdfc7eSAndroid Build Coastguard Worker ... 983*49cdfc7eSAndroid Build Coastguard Worker 984*49cdfc7eSAndroid Build Coastguard Worker /* then do cleanup work */ 985*49cdfc7eSAndroid Build Coastguard Worker 986*49cdfc7eSAndroid Build Coastguard Worker ... 987*49cdfc7eSAndroid Build Coastguard Worker 988*49cdfc7eSAndroid Build Coastguard Worker /* only one thread returns to the library */ 989*49cdfc7eSAndroid Build Coastguard Worker} 990*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 991*49cdfc7eSAndroid Build Coastguard Worker 992*49cdfc7eSAndroid Build Coastguard Worker 993*49cdfc7eSAndroid Build Coastguard Worker1.15 Testing with a block device 994*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 995*49cdfc7eSAndroid Build Coastguard Worker 996*49cdfc7eSAndroid Build Coastguard WorkerSome tests needs a block device (inotify tests, syscall 'EROFS' failures, 997*49cdfc7eSAndroid Build Coastguard Workeretc.). LTP library contains a code to prepare a testing device. 998*49cdfc7eSAndroid Build Coastguard Worker 999*49cdfc7eSAndroid Build Coastguard WorkerIf '.needs_device' flag in the 'struct tst_test' is set the 'tst_device' 1000*49cdfc7eSAndroid Build Coastguard Workerstructure is initialized with a path to a test device and default filesystem 1001*49cdfc7eSAndroid Build Coastguard Workerto be used. 1002*49cdfc7eSAndroid Build Coastguard Worker 1003*49cdfc7eSAndroid Build Coastguard WorkerYou can also request minimal device size in megabytes by setting 1004*49cdfc7eSAndroid Build Coastguard Worker'.dev_min_size' the device is guaranteed to have at least the requested size 1005*49cdfc7eSAndroid Build Coastguard Workerthen. 1006*49cdfc7eSAndroid Build Coastguard Worker 1007*49cdfc7eSAndroid Build Coastguard WorkerIf '.format_device' flag is set the device is formatted with a filesystem as 1008*49cdfc7eSAndroid Build Coastguard Workerwell. You can use '.dev_fs_type' to override the default filesystem type if 1009*49cdfc7eSAndroid Build Coastguard Workerneeded and pass additional options to mkfs via '.dev_fs_opts' and 1010*49cdfc7eSAndroid Build Coastguard Worker'.dev_extra_opts' pointers. Note that '.format_device' implies '.needs_device' 1011*49cdfc7eSAndroid Build Coastguard Workerthere is no need to set both. 1012*49cdfc7eSAndroid Build Coastguard Worker 1013*49cdfc7eSAndroid Build Coastguard WorkerIf '.mount_device' is set, the device is mounted at '.mntpoint' which is used 1014*49cdfc7eSAndroid Build Coastguard Workerto pass a directory name that will be created and used as mount destination. 1015*49cdfc7eSAndroid Build Coastguard WorkerYou can pass additional flags and data to the mount command via '.mnt_flags' 1016*49cdfc7eSAndroid Build Coastguard Workerand '.mnt_data' pointers. Note that '.mount_device' implies '.needs_device' 1017*49cdfc7eSAndroid Build Coastguard Workerand '.format_device' so there is no need to set the later two. 1018*49cdfc7eSAndroid Build Coastguard Worker 1019*49cdfc7eSAndroid Build Coastguard WorkerIf '.needs_rofs' is set, read-only filesystem is mounted at '.mntpoint' this 1020*49cdfc7eSAndroid Build Coastguard Workerone is supposed to be used for 'EROFS' tests. 1021*49cdfc7eSAndroid Build Coastguard Worker 1022*49cdfc7eSAndroid Build Coastguard WorkerIf '.all_filesystems' is set the test function is executed for all supported 1023*49cdfc7eSAndroid Build Coastguard Workerfilesystems. Supported filesystems are detected based on existence of the 1024*49cdfc7eSAndroid Build Coastguard Worker'mkfs.$fs' helper and on kernel support to mount it. For each supported 1025*49cdfc7eSAndroid Build Coastguard Workerfilesystem the 'tst_device.fs_type' is set to the currently tested fs type, if 1026*49cdfc7eSAndroid Build Coastguard Worker'.format_device' is set the device is formatted as well, if '.mount_device' is 1027*49cdfc7eSAndroid Build Coastguard Workerset it's mounted at '.mntpoint'. Also the test timeout is reset for each 1028*49cdfc7eSAndroid Build Coastguard Workerexecution of the test function. This flag is expected to be used for filesystem 1029*49cdfc7eSAndroid Build Coastguard Workerrelated syscalls that are at least partly implemented in the filesystem 1030*49cdfc7eSAndroid Build Coastguard Workerspecific code e.g. 'fallocate()'. 1031*49cdfc7eSAndroid Build Coastguard Worker 1032*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1033*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1034*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1035*49cdfc7eSAndroid Build Coastguard Worker 1036*49cdfc7eSAndroid Build Coastguard Workerstruct tst_device { 1037*49cdfc7eSAndroid Build Coastguard Worker const char *dev; 1038*49cdfc7eSAndroid Build Coastguard Worker const char *fs_type; 1039*49cdfc7eSAndroid Build Coastguard Worker}; 1040*49cdfc7eSAndroid Build Coastguard Worker 1041*49cdfc7eSAndroid Build Coastguard Workerextern struct tst_device *tst_device; 1042*49cdfc7eSAndroid Build Coastguard Worker 1043*49cdfc7eSAndroid Build Coastguard Workerint tst_umount(const char *path); 1044*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1045*49cdfc7eSAndroid Build Coastguard Worker 1046*49cdfc7eSAndroid Build Coastguard WorkerIn case that 'LTP_DEV' is passed to the test in an environment, the library 1047*49cdfc7eSAndroid Build Coastguard Workerchecks that the file exists and that it's a block device, if 1048*49cdfc7eSAndroid Build Coastguard Worker'.device_min_size' is set the device size is checked as well. If 'LTP_DEV' 1049*49cdfc7eSAndroid Build Coastguard Workerwasn't set or if size requirements were not met a temporary file is created 1050*49cdfc7eSAndroid Build Coastguard Workerand attached to a free loop device. 1051*49cdfc7eSAndroid Build Coastguard Worker 1052*49cdfc7eSAndroid Build Coastguard WorkerIf there is no usable device and loop device couldn't be initialized the test 1053*49cdfc7eSAndroid Build Coastguard Workerexits with 'TCONF'. 1054*49cdfc7eSAndroid Build Coastguard Worker 1055*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_umount()' function works exactly as 'umount(2)' but retries several 1056*49cdfc7eSAndroid Build Coastguard Workertimes on 'EBUSY'. This is because various desktop daemons (gvfsd-trash is known 1057*49cdfc7eSAndroid Build Coastguard Workerfor that) may be stupid enough to probe all newly mounted filesystem which 1058*49cdfc7eSAndroid Build Coastguard Workerresults in 'umount(2)' failing with 'EBUSY'. 1059*49cdfc7eSAndroid Build Coastguard Worker 1060*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: All testcases should use 'tst_umount()' instead of 'umount(2)' to 1061*49cdfc7eSAndroid Build Coastguard Worker umount filesystems. 1062*49cdfc7eSAndroid Build Coastguard Worker 1063*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1064*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1065*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1066*49cdfc7eSAndroid Build Coastguard Worker 1067*49cdfc7eSAndroid Build Coastguard Workerint tst_find_free_loopdev(const char *path, size_t path_len); 1068*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1069*49cdfc7eSAndroid Build Coastguard Worker 1070*49cdfc7eSAndroid Build Coastguard WorkerThis function finds a free loopdev and returns the free loopdev minor (-1 for no 1071*49cdfc7eSAndroid Build Coastguard Workerfree loopdev). If path is non-NULL, it will be filled with free loopdev path. 1072*49cdfc7eSAndroid Build Coastguard WorkerIf you want to use a customized loop device, we can call 'tst_find_free_loopdev(NULL, 0)' 1073*49cdfc7eSAndroid Build Coastguard Workerin tests to get a free minor number and then mknod. 1074*49cdfc7eSAndroid Build Coastguard Worker 1075*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1076*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1077*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1078*49cdfc7eSAndroid Build Coastguard Worker 1079*49cdfc7eSAndroid Build Coastguard Workerunsigned long tst_dev_bytes_written(const char *dev); 1080*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1081*49cdfc7eSAndroid Build Coastguard Worker 1082*49cdfc7eSAndroid Build Coastguard WorkerThis function reads test block device stat file ('/sys/block/<device>/stat') and 1083*49cdfc7eSAndroid Build Coastguard Workerreturns the bytes written since the last invocation of this function. To avoid 1084*49cdfc7eSAndroid Build Coastguard WorkerFS deferred IO metadata/cache interference, we suggest doing "syncfs" before the 1085*49cdfc7eSAndroid Build Coastguard Workertst_dev_bytes_written first invocation. And an inline function named 'tst_dev_sync()' 1086*49cdfc7eSAndroid Build Coastguard Workeris created for that intention. 1087*49cdfc7eSAndroid Build Coastguard Worker 1088*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1089*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1090*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1091*49cdfc7eSAndroid Build Coastguard Worker 1092*49cdfc7eSAndroid Build Coastguard Workervoid tst_find_backing_dev(const char *path, char *dev, size_t dev_size); 1093*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1094*49cdfc7eSAndroid Build Coastguard Worker 1095*49cdfc7eSAndroid Build Coastguard WorkerThis function finds the block dev that this path belongs to, using uevent in sysfs. 1096*49cdfc7eSAndroid Build Coastguard WorkerFor Btrfs it uses '/sys/fs/btrfs/UUID/devices/DEV_NAME/uevent'; for other 1097*49cdfc7eSAndroid Build Coastguard Workerfilesystems it uses '/sys/dev/block/MAJOR:MINOR/uevent'. 1098*49cdfc7eSAndroid Build Coastguard Worker 1099*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1100*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1101*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1102*49cdfc7eSAndroid Build Coastguard Worker 1103*49cdfc7eSAndroid Build Coastguard Workeruint64_t tst_get_device_size(const char *dev_path); 1104*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1105*49cdfc7eSAndroid Build Coastguard Worker 1106*49cdfc7eSAndroid Build Coastguard WorkerThis function gets size of the given block device, it checks the 'dev_path' is 1107*49cdfc7eSAndroid Build Coastguard Workervalid first, if yes, return the size in MB, otherwise return -1. 1108*49cdfc7eSAndroid Build Coastguard Worker 1109*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1110*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1111*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1112*49cdfc7eSAndroid Build Coastguard Worker 1113*49cdfc7eSAndroid Build Coastguard Workerint tst_dev_block_size(const char *path); 1114*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1115*49cdfc7eSAndroid Build Coastguard Worker 1116*49cdfc7eSAndroid Build Coastguard WorkerThis function returns the physical device block size for the specific `path`. 1117*49cdfc7eSAndroid Build Coastguard WorkerIt finds the device where `path` is located and then uses `ioctl` (BLKSSZGET) 1118*49cdfc7eSAndroid Build Coastguard Workerto get a physical device block size. 1119*49cdfc7eSAndroid Build Coastguard Worker 1120*49cdfc7eSAndroid Build Coastguard Worker1.16 Formatting a device with a filesystem 1121*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1122*49cdfc7eSAndroid Build Coastguard Worker 1123*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1124*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1125*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1126*49cdfc7eSAndroid Build Coastguard Worker 1127*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1128*49cdfc7eSAndroid Build Coastguard Worker{ 1129*49cdfc7eSAndroid Build Coastguard Worker ... 1130*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKFS(tst_device->dev, tst_device->fs_type, NULL, NULL); 1131*49cdfc7eSAndroid Build Coastguard Worker ... 1132*49cdfc7eSAndroid Build Coastguard Worker} 1133*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1134*49cdfc7eSAndroid Build Coastguard Worker 1135*49cdfc7eSAndroid Build Coastguard WorkerThis function takes a path to a device, filesystem type and an array of extra 1136*49cdfc7eSAndroid Build Coastguard Workeroptions passed to mkfs. 1137*49cdfc7eSAndroid Build Coastguard Worker 1138*49cdfc7eSAndroid Build Coastguard WorkerThe fs options 'fs_opts' should either be 'NULL' if there are none, or a 1139*49cdfc7eSAndroid Build Coastguard Worker'NULL' terminated array of strings such as: 1140*49cdfc7eSAndroid Build Coastguard Worker+const char *const opts[] = {"-b", "1024", NULL}+. 1141*49cdfc7eSAndroid Build Coastguard Worker 1142*49cdfc7eSAndroid Build Coastguard WorkerThe extra options 'extra_opts' should either be 'NULL' if there are none, or a 1143*49cdfc7eSAndroid Build Coastguard Worker'NULL' terminated array of strings such as +{"102400", NULL}+; 'extra_opts' 1144*49cdfc7eSAndroid Build Coastguard Workerwill be passed after device name. e.g: +mkfs -t ext4 -b 1024 /dev/sda1 102400+ 1145*49cdfc7eSAndroid Build Coastguard Workerin this case. 1146*49cdfc7eSAndroid Build Coastguard Worker 1147*49cdfc7eSAndroid Build Coastguard WorkerNote that perfer to store the options which can be passed before or after device 1148*49cdfc7eSAndroid Build Coastguard Workername by 'fs_opts' array. 1149*49cdfc7eSAndroid Build Coastguard Worker 1150*49cdfc7eSAndroid Build Coastguard Worker1.17 Verifying a filesystem's free space 1151*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1152*49cdfc7eSAndroid Build Coastguard Worker 1153*49cdfc7eSAndroid Build Coastguard WorkerSome tests have size requirements for the filesystem's free space. If these 1154*49cdfc7eSAndroid Build Coastguard Workerrequirements are not satisfied, the tests should be skipped. 1155*49cdfc7eSAndroid Build Coastguard Worker 1156*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1157*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1158*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1159*49cdfc7eSAndroid Build Coastguard Worker 1160*49cdfc7eSAndroid Build Coastguard Workerint tst_fs_has_free(const char *path, unsigned int size, unsigned int mult); 1161*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1162*49cdfc7eSAndroid Build Coastguard Worker 1163*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_fs_has_free()' function returns 1 if there is enough space and 0 if 1164*49cdfc7eSAndroid Build Coastguard Workerthere is not. 1165*49cdfc7eSAndroid Build Coastguard Worker 1166*49cdfc7eSAndroid Build Coastguard WorkerThe 'path' is the pathname of any directory/file within a filesystem. 1167*49cdfc7eSAndroid Build Coastguard Worker 1168*49cdfc7eSAndroid Build Coastguard WorkerThe 'mult' is a multiplier, one of 'TST_BYTES', 'TST_KB', 'TST_MB' or 'TST_GB'. 1169*49cdfc7eSAndroid Build Coastguard Worker 1170*49cdfc7eSAndroid Build Coastguard WorkerThe required free space is calculated by 'size * mult', e.g. 1171*49cdfc7eSAndroid Build Coastguard Worker'tst_fs_has_free("/tmp/testfile", 64, TST_MB)' will return 1 if the 1172*49cdfc7eSAndroid Build Coastguard Workerfilesystem, which '"/tmp/testfile"' is in, has 64MB free space at least, and 0 1173*49cdfc7eSAndroid Build Coastguard Workerif not. 1174*49cdfc7eSAndroid Build Coastguard Worker 1175*49cdfc7eSAndroid Build Coastguard Worker1.18 Files, directories and fs limits 1176*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1177*49cdfc7eSAndroid Build Coastguard Worker 1178*49cdfc7eSAndroid Build Coastguard WorkerSome tests need to know the maximum count of links to a regular file or 1179*49cdfc7eSAndroid Build Coastguard Workerdirectory, such as 'rename(2)' or 'linkat(2)' to test 'EMLINK' error. 1180*49cdfc7eSAndroid Build Coastguard Worker 1181*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1182*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1183*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1184*49cdfc7eSAndroid Build Coastguard Worker 1185*49cdfc7eSAndroid Build Coastguard Workerint tst_fs_fill_hardlinks(const char *dir); 1186*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1187*49cdfc7eSAndroid Build Coastguard Worker 1188*49cdfc7eSAndroid Build Coastguard WorkerTry to get maximum count of hard links to a regular file inside the 'dir'. 1189*49cdfc7eSAndroid Build Coastguard Worker 1190*49cdfc7eSAndroid Build Coastguard WorkerNOTE: This number depends on the filesystem 'dir' is on. 1191*49cdfc7eSAndroid Build Coastguard Worker 1192*49cdfc7eSAndroid Build Coastguard WorkerThis function uses 'link(2)' to create hard links to a single file until it 1193*49cdfc7eSAndroid Build Coastguard Workergets 'EMLINK' or creates 65535 links. If the limit is hit, the maximum number of 1194*49cdfc7eSAndroid Build Coastguard Workerhardlinks is returned and the 'dir' is filled with hardlinks in format 1195*49cdfc7eSAndroid Build Coastguard Worker"testfile%i", where i belongs to [0, limit) interval. If no limit is hit or if 1196*49cdfc7eSAndroid Build Coastguard Worker'link(2)' failed with 'ENOSPC' or 'EDQUOT', zero is returned and previously 1197*49cdfc7eSAndroid Build Coastguard Workercreated files are removed. 1198*49cdfc7eSAndroid Build Coastguard Worker 1199*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1200*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1201*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1202*49cdfc7eSAndroid Build Coastguard Worker 1203*49cdfc7eSAndroid Build Coastguard Workerint tst_fs_fill_subdirs(const char *dir); 1204*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1205*49cdfc7eSAndroid Build Coastguard Worker 1206*49cdfc7eSAndroid Build Coastguard WorkerTry to get maximum number of subdirectories in directory. 1207*49cdfc7eSAndroid Build Coastguard Worker 1208*49cdfc7eSAndroid Build Coastguard WorkerNOTE: This number depends on the filesystem 'dir' is on. For current kernel, 1209*49cdfc7eSAndroid Build Coastguard Workersubdir limit is not available for all filesystems (available for ext2, ext3, 1210*49cdfc7eSAndroid Build Coastguard Workerminix, sysv and more). If the test runs on some other filesystems, like ramfs, 1211*49cdfc7eSAndroid Build Coastguard Workertmpfs, it will not even try to reach the limit and return 0. 1212*49cdfc7eSAndroid Build Coastguard Worker 1213*49cdfc7eSAndroid Build Coastguard WorkerThis function uses 'mkdir(2)' to create directories in 'dir' until it gets 1214*49cdfc7eSAndroid Build Coastguard Worker'EMLINK' or creates 65535 directories. If the limit is hit, the maximum number 1215*49cdfc7eSAndroid Build Coastguard Workerof subdirectories is returned and the 'dir' is filled with subdirectories in 1216*49cdfc7eSAndroid Build Coastguard Workerformat "testdir%i", where i belongs to [0, limit - 2) interval (because each 1217*49cdfc7eSAndroid Build Coastguard Workernewly created dir has two links already - the '.' and the link from parent 1218*49cdfc7eSAndroid Build Coastguard Workerdir). If no limit is hit or if 'mkdir(2)' failed with 'ENOSPC' or 'EDQUOT', 1219*49cdfc7eSAndroid Build Coastguard Workerzero is returned and previously created directories are removed. 1220*49cdfc7eSAndroid Build Coastguard Worker 1221*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1222*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1223*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1224*49cdfc7eSAndroid Build Coastguard Worker 1225*49cdfc7eSAndroid Build Coastguard Workerint tst_dir_is_empty(const char *dir, int verbose); 1226*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1227*49cdfc7eSAndroid Build Coastguard Worker 1228*49cdfc7eSAndroid Build Coastguard WorkerReturns non-zero if directory is empty and zero otherwise. 1229*49cdfc7eSAndroid Build Coastguard Worker 1230*49cdfc7eSAndroid Build Coastguard WorkerDirectory is considered empty if it contains only '.' and '..'. 1231*49cdfc7eSAndroid Build Coastguard Worker 1232*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1233*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1234*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1235*49cdfc7eSAndroid Build Coastguard Worker 1236*49cdfc7eSAndroid Build Coastguard Workervoid tst_purge_dir(const char *path); 1237*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1238*49cdfc7eSAndroid Build Coastguard Worker 1239*49cdfc7eSAndroid Build Coastguard WorkerDeletes the contents of given directory but keeps the directory itself. Useful 1240*49cdfc7eSAndroid Build Coastguard Workerfor cleaning up the temporary directory and mount points between test cases or 1241*49cdfc7eSAndroid Build Coastguard Workertest iterations. Terminates the program with 'TBROK' on error. 1242*49cdfc7eSAndroid Build Coastguard Worker 1243*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1244*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1245*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1246*49cdfc7eSAndroid Build Coastguard Worker 1247*49cdfc7eSAndroid Build Coastguard Workerint tst_fill_fd(int fd, char pattern, size_t bs, size_t bcount); 1248*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1249*49cdfc7eSAndroid Build Coastguard Worker 1250*49cdfc7eSAndroid Build Coastguard WorkerFill a file with specified pattern using file descriptor. 1251*49cdfc7eSAndroid Build Coastguard Worker 1252*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1253*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1254*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1255*49cdfc7eSAndroid Build Coastguard Worker 1256*49cdfc7eSAndroid Build Coastguard Workerint tst_prealloc_size_fd(int fd, size_t bs, size_t bcount); 1257*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1258*49cdfc7eSAndroid Build Coastguard Worker 1259*49cdfc7eSAndroid Build Coastguard WorkerPreallocate the specified amount of space using 'fallocate()'. Falls back to 1260*49cdfc7eSAndroid Build Coastguard Worker'tst_fill_fd()' if 'fallocate()' fails. 1261*49cdfc7eSAndroid Build Coastguard Worker 1262*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1263*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1264*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1265*49cdfc7eSAndroid Build Coastguard Worker 1266*49cdfc7eSAndroid Build Coastguard Workerint tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount); 1267*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1268*49cdfc7eSAndroid Build Coastguard Worker 1269*49cdfc7eSAndroid Build Coastguard WorkerCreates/overwrites a file with specified pattern using file path. 1270*49cdfc7eSAndroid Build Coastguard Worker 1271*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1272*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1273*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1274*49cdfc7eSAndroid Build Coastguard Worker 1275*49cdfc7eSAndroid Build Coastguard Workerint tst_prealloc_file(const char *path, size_t bs, size_t bcount); 1276*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1277*49cdfc7eSAndroid Build Coastguard Worker 1278*49cdfc7eSAndroid Build Coastguard WorkerCreate/overwrite a file and preallocate the specified amount of space for it. 1279*49cdfc7eSAndroid Build Coastguard WorkerThe allocated space will not be initialized to any particular content. 1280*49cdfc7eSAndroid Build Coastguard Worker 1281*49cdfc7eSAndroid Build Coastguard Worker1.19 Getting an unused PID number 1282*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1283*49cdfc7eSAndroid Build Coastguard Worker 1284*49cdfc7eSAndroid Build Coastguard WorkerSome tests require a 'PID', which is not used by the OS (does not belong to 1285*49cdfc7eSAndroid Build Coastguard Workerany process within it). For example, kill(2) should set errno to 'ESRCH' if 1286*49cdfc7eSAndroid Build Coastguard Workerit's passed such 'PID'. 1287*49cdfc7eSAndroid Build Coastguard Worker 1288*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1289*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1290*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1291*49cdfc7eSAndroid Build Coastguard Worker 1292*49cdfc7eSAndroid Build Coastguard Workerpid_t tst_get_unused_pid(void); 1293*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1294*49cdfc7eSAndroid Build Coastguard Worker 1295*49cdfc7eSAndroid Build Coastguard WorkerReturn a 'PID' value not used by the OS or any process within it. 1296*49cdfc7eSAndroid Build Coastguard Worker 1297*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1298*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1299*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1300*49cdfc7eSAndroid Build Coastguard Worker 1301*49cdfc7eSAndroid Build Coastguard Workerint tst_get_free_pids(void); 1302*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1303*49cdfc7eSAndroid Build Coastguard Worker 1304*49cdfc7eSAndroid Build Coastguard WorkerReturns number of unused pids in the system. Note that this number may be 1305*49cdfc7eSAndroid Build Coastguard Workerdifferent once the call returns and should be used only for rough estimates. 1306*49cdfc7eSAndroid Build Coastguard Worker 1307*49cdfc7eSAndroid Build Coastguard Worker1.20 Running executables 1308*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 1309*49cdfc7eSAndroid Build Coastguard Worker 1310*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1311*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1312*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1313*49cdfc7eSAndroid Build Coastguard Worker 1314*49cdfc7eSAndroid Build Coastguard Workerint tst_cmd(const char *const argv[], 1315*49cdfc7eSAndroid Build Coastguard Worker const char *stdout_path, 1316*49cdfc7eSAndroid Build Coastguard Worker const char *stderr_path, 1317*49cdfc7eSAndroid Build Coastguard Worker enum tst_cmd_flags flags); 1318*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1319*49cdfc7eSAndroid Build Coastguard Worker 1320*49cdfc7eSAndroid Build Coastguard Worker'tst_cmd()' is a wrapper for 'vfork() + execvp()' which provides a way 1321*49cdfc7eSAndroid Build Coastguard Workerto execute an external program. 1322*49cdfc7eSAndroid Build Coastguard Worker 1323*49cdfc7eSAndroid Build Coastguard Worker'argv[]' is a 'NULL' terminated array of strings starting with the program name 1324*49cdfc7eSAndroid Build Coastguard Workerwhich is followed by optional arguments. 1325*49cdfc7eSAndroid Build Coastguard Worker 1326*49cdfc7eSAndroid Build Coastguard Worker'TST_CMD_PASS_RETVAL' enum 'tst_cmd_flags' makes 'tst_cmd()' 1327*49cdfc7eSAndroid Build Coastguard Workerreturn the program exit code to the caller, otherwise 'tst_cmd()' exit the 1328*49cdfc7eSAndroid Build Coastguard Workertests on failure. 'TST_CMD_TCONF_ON_MISSING' check for program in '$PATH' and exit 1329*49cdfc7eSAndroid Build Coastguard Workerwith 'TCONF' if not found. 1330*49cdfc7eSAndroid Build Coastguard Worker 1331*49cdfc7eSAndroid Build Coastguard WorkerIn case that 'execvp()' has failed and the enum 'TST_CMD_PASS_RETVAL' flag was set, the 1332*49cdfc7eSAndroid Build Coastguard Workerreturn value is '255' if 'execvp()' failed with 'ENOENT' and '254' otherwise. 1333*49cdfc7eSAndroid Build Coastguard Worker 1334*49cdfc7eSAndroid Build Coastguard Worker'stdout_path' and 'stderr_path' determine where to redirect the program 1335*49cdfc7eSAndroid Build Coastguard Workerstdout and stderr I/O streams. 1336*49cdfc7eSAndroid Build Coastguard Worker 1337*49cdfc7eSAndroid Build Coastguard Worker'SAFE_CMD()' is a wrapper for 'tst_cmd()' which can be used for automatic 1338*49cdfc7eSAndroid Build Coastguard Workerhandling non-zero exit (exits with 'TBROK') and 'ENOENT' (the program not in 1339*49cdfc7eSAndroid Build Coastguard Worker'$PATH', exits with 'TCONF'). 1340*49cdfc7eSAndroid Build Coastguard Worker 1341*49cdfc7eSAndroid Build Coastguard Worker.Example 1342*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1343*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1344*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1345*49cdfc7eSAndroid Build Coastguard Worker 1346*49cdfc7eSAndroid Build Coastguard Workerconst char *const cmd[] = { "ls", "-l", NULL }; 1347*49cdfc7eSAndroid Build Coastguard Worker 1348*49cdfc7eSAndroid Build Coastguard Worker... 1349*49cdfc7eSAndroid Build Coastguard Worker /* Store output of 'ls -l' into log.txt */ 1350*49cdfc7eSAndroid Build Coastguard Worker tst_cmd(cmd, "log.txt", NULL, 0); 1351*49cdfc7eSAndroid Build Coastguard Worker... 1352*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1353*49cdfc7eSAndroid Build Coastguard Worker 1354*49cdfc7eSAndroid Build Coastguard Worker1.21 Measuring elapsed time and helper functions 1355*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1356*49cdfc7eSAndroid Build Coastguard Worker 1357*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1358*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1359*49cdfc7eSAndroid Build Coastguard Worker#include "tst_timer.h" 1360*49cdfc7eSAndroid Build Coastguard Worker 1361*49cdfc7eSAndroid Build Coastguard Workervoid tst_timer_check(clockid_t clk_id); 1362*49cdfc7eSAndroid Build Coastguard Worker 1363*49cdfc7eSAndroid Build Coastguard Workervoid tst_timer_start(clockid_t clk_id); 1364*49cdfc7eSAndroid Build Coastguard Worker 1365*49cdfc7eSAndroid Build Coastguard Workervoid tst_timer_stop(void); 1366*49cdfc7eSAndroid Build Coastguard Worker 1367*49cdfc7eSAndroid Build Coastguard Workerstruct timespec tst_timer_elapsed(void); 1368*49cdfc7eSAndroid Build Coastguard Worker 1369*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timer_elapsed_ms(void); 1370*49cdfc7eSAndroid Build Coastguard Worker 1371*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timer_elapsed_us(void); 1372*49cdfc7eSAndroid Build Coastguard Worker 1373*49cdfc7eSAndroid Build Coastguard Workerint tst_timer_expired_ms(long long ms); 1374*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1375*49cdfc7eSAndroid Build Coastguard Worker 1376*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timer_check()' function checks if specified 'clk_id' is supported and 1377*49cdfc7eSAndroid Build Coastguard Workerexits the test with 'TCONF' otherwise. It's expected to be used in test 1378*49cdfc7eSAndroid Build Coastguard Worker'setup()' before any resources that needs to be cleaned up are initialized, 1379*49cdfc7eSAndroid Build Coastguard Workerhence it does not include a cleanup function parameter. 1380*49cdfc7eSAndroid Build Coastguard Worker 1381*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timer_start()' marks start time and stores the 'clk_id' for further 1382*49cdfc7eSAndroid Build Coastguard Workeruse. 1383*49cdfc7eSAndroid Build Coastguard Worker 1384*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timer_stop()' marks the stop time using the same 'clk_id' as last 1385*49cdfc7eSAndroid Build Coastguard Workercall to 'tst_timer_start()'. 1386*49cdfc7eSAndroid Build Coastguard Worker 1387*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timer_elapsed*()' returns time difference between the timer start and 1388*49cdfc7eSAndroid Build Coastguard Workerlast timer stop in several formats and units. 1389*49cdfc7eSAndroid Build Coastguard Worker 1390*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timer_expired_ms()' function checks if the timer started by 1391*49cdfc7eSAndroid Build Coastguard Worker'tst_timer_start()' has been running longer than ms milliseconds. The function 1392*49cdfc7eSAndroid Build Coastguard Workerreturns non-zero if timer has expired and zero otherwise. 1393*49cdfc7eSAndroid Build Coastguard Worker 1394*49cdfc7eSAndroid Build Coastguard WorkerIMPORTANT: The timer functions use 'clock_gettime()' internally which needs to 1395*49cdfc7eSAndroid Build Coastguard Worker be linked with '-lrt' on older glibc. Please do not forget to add 1396*49cdfc7eSAndroid Build Coastguard Worker 'LDLIBS+=-lrt' in Makefile. 1397*49cdfc7eSAndroid Build Coastguard Worker 1398*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1399*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1400*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1401*49cdfc7eSAndroid Build Coastguard Worker#include "tst_timer.h" 1402*49cdfc7eSAndroid Build Coastguard Worker 1403*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1404*49cdfc7eSAndroid Build Coastguard Worker{ 1405*49cdfc7eSAndroid Build Coastguard Worker ... 1406*49cdfc7eSAndroid Build Coastguard Worker tst_timer_check(CLOCK_MONOTONIC); 1407*49cdfc7eSAndroid Build Coastguard Worker ... 1408*49cdfc7eSAndroid Build Coastguard Worker} 1409*49cdfc7eSAndroid Build Coastguard Worker 1410*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 1411*49cdfc7eSAndroid Build Coastguard Worker{ 1412*49cdfc7eSAndroid Build Coastguard Worker ... 1413*49cdfc7eSAndroid Build Coastguard Worker tst_timer_start(CLOCK_MONOTONIC); 1414*49cdfc7eSAndroid Build Coastguard Worker ... 1415*49cdfc7eSAndroid Build Coastguard Worker while (!tst_timer_expired_ms(5000)) { 1416*49cdfc7eSAndroid Build Coastguard Worker ... 1417*49cdfc7eSAndroid Build Coastguard Worker } 1418*49cdfc7eSAndroid Build Coastguard Worker ... 1419*49cdfc7eSAndroid Build Coastguard Worker} 1420*49cdfc7eSAndroid Build Coastguard Worker 1421*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 1422*49cdfc7eSAndroid Build Coastguard Worker ... 1423*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 1424*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 1425*49cdfc7eSAndroid Build Coastguard Worker ... 1426*49cdfc7eSAndroid Build Coastguard Worker}; 1427*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1428*49cdfc7eSAndroid Build Coastguard Worker 1429*49cdfc7eSAndroid Build Coastguard WorkerExpiration timer example usage. 1430*49cdfc7eSAndroid Build Coastguard Worker 1431*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1432*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1433*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_to_us(struct timespec t); 1434*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_to_ms(struct timespec t); 1435*49cdfc7eSAndroid Build Coastguard Worker 1436*49cdfc7eSAndroid Build Coastguard Workerstruct timeval tst_us_to_timeval(long long us); 1437*49cdfc7eSAndroid Build Coastguard Workerstruct timeval tst_ms_to_timeval(long long ms); 1438*49cdfc7eSAndroid Build Coastguard Worker 1439*49cdfc7eSAndroid Build Coastguard Workerint tst_timespec_lt(struct timespec t1, struct timespec t2); 1440*49cdfc7eSAndroid Build Coastguard Worker 1441*49cdfc7eSAndroid Build Coastguard Workerstruct timespec tst_timespec_add_us(struct timespec t, long long us); 1442*49cdfc7eSAndroid Build Coastguard Worker 1443*49cdfc7eSAndroid Build Coastguard Workerstruct timespec tst_timespec_diff(struct timespec t1, struct timespec t2); 1444*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_diff_us(struct timespec t1, struct timespec t2); 1445*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_diff_ms(struct timespec t1, struct timespec t2); 1446*49cdfc7eSAndroid Build Coastguard Worker 1447*49cdfc7eSAndroid Build Coastguard Workerstruct timespec tst_timespec_abs_diff(struct timespec t1, struct timespec t2); 1448*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_abs_diff_us(struct timespec t1, struct timespec t2); 1449*49cdfc7eSAndroid Build Coastguard Workerlong long tst_timespec_abs_diff_ms(struct timespec t1, struct timespec t2); 1450*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1451*49cdfc7eSAndroid Build Coastguard Worker 1452*49cdfc7eSAndroid Build Coastguard WorkerThe first four functions are simple inline conversion functions. 1453*49cdfc7eSAndroid Build Coastguard Worker 1454*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timespec_lt()' function returns non-zero if 't1' is earlier than 1455*49cdfc7eSAndroid Build Coastguard Worker't2'. 1456*49cdfc7eSAndroid Build Coastguard Worker 1457*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timespec_add_us()' function adds 'us' microseconds to the timespec 1458*49cdfc7eSAndroid Build Coastguard Worker't'. The 'us' is expected to be positive. 1459*49cdfc7eSAndroid Build Coastguard Worker 1460*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timespec_diff*()' functions returns difference between two times, the 1461*49cdfc7eSAndroid Build Coastguard Worker't1' is expected to be later than 't2'. 1462*49cdfc7eSAndroid Build Coastguard Worker 1463*49cdfc7eSAndroid Build Coastguard WorkerThe 'tst_timespec_abs_diff*()' functions returns absolute value of difference 1464*49cdfc7eSAndroid Build Coastguard Workerbetween two times. 1465*49cdfc7eSAndroid Build Coastguard Worker 1466*49cdfc7eSAndroid Build Coastguard WorkerNOTE: All conversions to ms and us rounds the value. 1467*49cdfc7eSAndroid Build Coastguard Worker 1468*49cdfc7eSAndroid Build Coastguard Worker1.22 Datafiles 1469*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~ 1470*49cdfc7eSAndroid Build Coastguard Worker 1471*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1472*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1473*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1474*49cdfc7eSAndroid Build Coastguard Worker 1475*49cdfc7eSAndroid Build Coastguard Workerstatic const char *const res_files[] = { 1476*49cdfc7eSAndroid Build Coastguard Worker "foo", 1477*49cdfc7eSAndroid Build Coastguard Worker "bar", 1478*49cdfc7eSAndroid Build Coastguard Worker NULL 1479*49cdfc7eSAndroid Build Coastguard Worker}; 1480*49cdfc7eSAndroid Build Coastguard Worker 1481*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1482*49cdfc7eSAndroid Build Coastguard Worker ... 1483*49cdfc7eSAndroid Build Coastguard Worker .resource_files = res_files, 1484*49cdfc7eSAndroid Build Coastguard Worker ... 1485*49cdfc7eSAndroid Build Coastguard Worker} 1486*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1487*49cdfc7eSAndroid Build Coastguard Worker 1488*49cdfc7eSAndroid Build Coastguard WorkerIf the test needs additional files to be copied to the test temporary 1489*49cdfc7eSAndroid Build Coastguard Workerdirectory all you need to do is to list their filenames in the 1490*49cdfc7eSAndroid Build Coastguard Worker'NULL' terminated array '.resource_files' in the tst_test structure. 1491*49cdfc7eSAndroid Build Coastguard Worker 1492*49cdfc7eSAndroid Build Coastguard WorkerWhen resource files is set test temporary directory is created automatically, 1493*49cdfc7eSAndroid Build Coastguard Workerthere is need to set '.needs_tmpdir' as well. 1494*49cdfc7eSAndroid Build Coastguard Worker 1495*49cdfc7eSAndroid Build Coastguard WorkerThe test library looks for datafiles first, these are either stored in a 1496*49cdfc7eSAndroid Build Coastguard Workerdirectory called +datafiles+ in the +$PWD+ at the start of the test or in 1497*49cdfc7eSAndroid Build Coastguard Worker+$LTPROOT/testcases/data/${test_binary_name}+. If the file is not found the 1498*49cdfc7eSAndroid Build Coastguard Workerlibrary looks into +$LTPROOT/testcases/bin/+ and to +$PWD+ at the start of the 1499*49cdfc7eSAndroid Build Coastguard Workertest. This ensures that the testcases can copy the file(s) effortlessly both 1500*49cdfc7eSAndroid Build Coastguard Workerwhen test is started from the directory it was compiled in as well as when LTP 1501*49cdfc7eSAndroid Build Coastguard Workerwas installed. 1502*49cdfc7eSAndroid Build Coastguard Worker 1503*49cdfc7eSAndroid Build Coastguard WorkerThe file(s) are copied to the newly created test temporary directory which is 1504*49cdfc7eSAndroid Build Coastguard Workerset as the test working directory when the 'test()' functions is executed. 1505*49cdfc7eSAndroid Build Coastguard Worker 1506*49cdfc7eSAndroid Build Coastguard Worker1.23 Code path tracing 1507*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~ 1508*49cdfc7eSAndroid Build Coastguard Worker 1509*49cdfc7eSAndroid Build Coastguard Worker'tst_res' is a macro, so on when you define a function in one file: 1510*49cdfc7eSAndroid Build Coastguard Worker 1511*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1512*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1513*49cdfc7eSAndroid Build Coastguard Workerint do_action(int arg) 1514*49cdfc7eSAndroid Build Coastguard Worker{ 1515*49cdfc7eSAndroid Build Coastguard Worker ... 1516*49cdfc7eSAndroid Build Coastguard Worker 1517*49cdfc7eSAndroid Build Coastguard Worker if (ok) { 1518*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "check passed"); 1519*49cdfc7eSAndroid Build Coastguard Worker return 0; 1520*49cdfc7eSAndroid Build Coastguard Worker } else { 1521*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "check failed"); 1522*49cdfc7eSAndroid Build Coastguard Worker return -1; 1523*49cdfc7eSAndroid Build Coastguard Worker } 1524*49cdfc7eSAndroid Build Coastguard Worker} 1525*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1526*49cdfc7eSAndroid Build Coastguard Worker 1527*49cdfc7eSAndroid Build Coastguard Workerand call it from another file, the file and line reported by 'tst_res' in this 1528*49cdfc7eSAndroid Build Coastguard Workerfunction will be from the former file. 1529*49cdfc7eSAndroid Build Coastguard Worker 1530*49cdfc7eSAndroid Build Coastguard Worker'TST_TRACE' can make the analysis of such situations easier. It's a macro which 1531*49cdfc7eSAndroid Build Coastguard Workerinserts a call to 'tst_res(TINFO, ...)' in case its argument evaluates to 1532*49cdfc7eSAndroid Build Coastguard Workernon-zero. In this call to 'tst_res(TINFO, ...)' the file and line will be 1533*49cdfc7eSAndroid Build Coastguard Workerexpanded using the actual location of 'TST_TRACE'. 1534*49cdfc7eSAndroid Build Coastguard Worker 1535*49cdfc7eSAndroid Build Coastguard WorkerFor example, if this another file contains: 1536*49cdfc7eSAndroid Build Coastguard Worker 1537*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1538*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1539*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1540*49cdfc7eSAndroid Build Coastguard Worker 1541*49cdfc7eSAndroid Build Coastguard Workerif (TST_TRACE(do_action(arg))) { 1542*49cdfc7eSAndroid Build Coastguard Worker ... 1543*49cdfc7eSAndroid Build Coastguard Worker} 1544*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1545*49cdfc7eSAndroid Build Coastguard Worker 1546*49cdfc7eSAndroid Build Coastguard Workerthe generated output may look similar to: 1547*49cdfc7eSAndroid Build Coastguard Worker 1548*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1549*49cdfc7eSAndroid Build Coastguard Workercommon.h:9: FAIL: check failed 1550*49cdfc7eSAndroid Build Coastguard Workertest.c:8: INFO: do_action(arg) failed 1551*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1552*49cdfc7eSAndroid Build Coastguard Worker 1553*49cdfc7eSAndroid Build Coastguard Worker1.24 Tainted kernels 1554*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~ 1555*49cdfc7eSAndroid Build Coastguard Worker 1556*49cdfc7eSAndroid Build Coastguard WorkerIf you need to detect whether a testcase triggers a kernel warning, bug or 1557*49cdfc7eSAndroid Build Coastguard Workeroops, the following can be used to detect TAINT_W or TAINT_D: 1558*49cdfc7eSAndroid Build Coastguard Worker 1559*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1560*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1561*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1562*49cdfc7eSAndroid Build Coastguard Worker 1563*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1564*49cdfc7eSAndroid Build Coastguard Worker ... 1565*49cdfc7eSAndroid Build Coastguard Worker .taint_check = TST_TAINT_W | TST_TAINT_D, 1566*49cdfc7eSAndroid Build Coastguard Worker ... 1567*49cdfc7eSAndroid Build Coastguard Worker}; 1568*49cdfc7eSAndroid Build Coastguard Worker 1569*49cdfc7eSAndroid Build Coastguard Workervoid run(void) 1570*49cdfc7eSAndroid Build Coastguard Worker{ 1571*49cdfc7eSAndroid Build Coastguard Worker ... 1572*49cdfc7eSAndroid Build Coastguard Worker if (tst_taint_check() != 0) 1573*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "kernel has issues"); 1574*49cdfc7eSAndroid Build Coastguard Worker else 1575*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "kernel seems to be fine"); 1576*49cdfc7eSAndroid Build Coastguard Worker} 1577*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1578*49cdfc7eSAndroid Build Coastguard Worker 1579*49cdfc7eSAndroid Build Coastguard WorkerTo initialize taint checks, you have to set the taint flags you want to test 1580*49cdfc7eSAndroid Build Coastguard Workerfor in the 'taint_check' attribute of the tst_test struct. LTP library will 1581*49cdfc7eSAndroid Build Coastguard Workerthen automatically call 'tst_taint_init()' during test setup. The function 1582*49cdfc7eSAndroid Build Coastguard Workerwill generate a 'TCONF' if the requested flags are not fully supported on the 1583*49cdfc7eSAndroid Build Coastguard Workerrunning kernel, and 'TBROK' if the kernel is already tainted before executing 1584*49cdfc7eSAndroid Build Coastguard Workerthe test. 1585*49cdfc7eSAndroid Build Coastguard Worker 1586*49cdfc7eSAndroid Build Coastguard WorkerLTP library will then automatically check kernel taint at the end of testing. 1587*49cdfc7eSAndroid Build Coastguard WorkerIf '.all_filesystems' is set in struct tst_test, taint check will be performed 1588*49cdfc7eSAndroid Build Coastguard Workerafter each file system and taint will abort testing early with 'TFAIL'. You 1589*49cdfc7eSAndroid Build Coastguard Workercan optionally also call 'tst_taint_check()' during 'run()', which returns 0 1590*49cdfc7eSAndroid Build Coastguard Workeror the tainted flags set in '/proc/sys/kernel/tainted' as specified earlier. 1591*49cdfc7eSAndroid Build Coastguard Worker 1592*49cdfc7eSAndroid Build Coastguard WorkerDepending on your kernel version, not all tainted-flags will be supported. 1593*49cdfc7eSAndroid Build Coastguard Worker 1594*49cdfc7eSAndroid Build Coastguard WorkerFor reference to tainted kernels, see kernel documentation: 1595*49cdfc7eSAndroid Build Coastguard WorkerDocumentation/admin-guide/tainted-kernels.rst or 1596*49cdfc7eSAndroid Build Coastguard Workerhttps://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html 1597*49cdfc7eSAndroid Build Coastguard Worker 1598*49cdfc7eSAndroid Build Coastguard Worker1.25 Checksums 1599*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~ 1600*49cdfc7eSAndroid Build Coastguard Worker 1601*49cdfc7eSAndroid Build Coastguard WorkerCRC32c checksum generation is supported by LTP. In order to use it, the 1602*49cdfc7eSAndroid Build Coastguard Workertest should include 'tst_checksum.h' header, then can call 'tst_crc32c()'. 1603*49cdfc7eSAndroid Build Coastguard Worker 1604*49cdfc7eSAndroid Build Coastguard Worker1.26 Checking kernel for the driver support 1605*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1606*49cdfc7eSAndroid Build Coastguard Worker 1607*49cdfc7eSAndroid Build Coastguard WorkerSome tests may need specific kernel drivers, either compiled in, or built 1608*49cdfc7eSAndroid Build Coastguard Workeras a module. If '.needs_drivers' points to a 'NULL' terminated array of kernel 1609*49cdfc7eSAndroid Build Coastguard Workermodule names these are all checked and the test exits with 'TCONF' on the 1610*49cdfc7eSAndroid Build Coastguard Workerfirst missing driver. 1611*49cdfc7eSAndroid Build Coastguard Worker 1612*49cdfc7eSAndroid Build Coastguard WorkerThe detection is based on reading 'modules.dep' and 'modules.builtin' files 1613*49cdfc7eSAndroid Build Coastguard Workergenerated by kmod. The check is skipped on Android. 1614*49cdfc7eSAndroid Build Coastguard Worker 1615*49cdfc7eSAndroid Build Coastguard Worker1.27 Saving & restoring /proc|sys values 1616*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1617*49cdfc7eSAndroid Build Coastguard Worker 1618*49cdfc7eSAndroid Build Coastguard WorkerLTP library can be instructed to save and restore value of specified 1619*49cdfc7eSAndroid Build Coastguard Worker(/proc|sys) files. This is achieved by initialized tst_test struct 1620*49cdfc7eSAndroid Build Coastguard Workerfield 'save_restore'. It is a NULL-terminated array of struct 1621*49cdfc7eSAndroid Build Coastguard Worker'tst_path_val' where each tst_path_val.path represents a file, whose 1622*49cdfc7eSAndroid Build Coastguard Workervalue is saved at the beginning and restored at the end of the test. 1623*49cdfc7eSAndroid Build Coastguard WorkerIf non-NULL string is passed in tst_path_val.val, it is written 1624*49cdfc7eSAndroid Build Coastguard Workerto the respective file at the beginning of the test. Only the first line 1625*49cdfc7eSAndroid Build Coastguard Workerof a specified file is saved and restored. 1626*49cdfc7eSAndroid Build Coastguard Worker 1627*49cdfc7eSAndroid Build Coastguard WorkerBy default, the test will end with TCONF if the file is read-only or 1628*49cdfc7eSAndroid Build Coastguard Workerdoes not exist. If the optional write of new value fails, the test will end 1629*49cdfc7eSAndroid Build Coastguard Workerwith 'TBROK'. This behavior can be changed using tst_path_val.flags: 1630*49cdfc7eSAndroid Build Coastguard Worker 1631*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TBROK_MISSING' – End test with 'TBROK' if the file does not exist 1632*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TCONF_MISSING' – End test with 'TCONF' if the file does not exist 1633*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_SKIP_MISSING' – Continue without saving the file if it does not exist 1634*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TBROK_RO' – End test with 'TBROK' if the file is read-only 1635*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TCONF_RO' – End test with 'TCONF' if the file is read-only 1636*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_SKIP_RO' – Continue without saving the file if it is read-only 1637*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_IGNORE_ERR' – Ignore errors when writing new value into the file 1638*49cdfc7eSAndroid Build Coastguard Worker 1639*49cdfc7eSAndroid Build Coastguard WorkerCommon flag combinations also have shortcuts: 1640*49cdfc7eSAndroid Build Coastguard Worker 1641*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TCONF' – Equivalent to 'TST_SR_TCONF_MISSING | TST_SR_TCONF_RO' 1642*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_TBROK' – Equivalent to 'TST_SR_TBROK_MISSING | TST_SR_TBROK_RO' 1643*49cdfc7eSAndroid Build Coastguard Worker* 'TST_SR_SKIP' – Equivalent to 'TST_SR_SKIP_MISSING | TST_SR_SKIP_RO' 1644*49cdfc7eSAndroid Build Coastguard Worker 1645*49cdfc7eSAndroid Build Coastguard Worker'restore' is always strict and will TWARN if it encounters any error. 1646*49cdfc7eSAndroid Build Coastguard Worker 1647*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1648*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1649*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1650*49cdfc7eSAndroid Build Coastguard Worker ... 1651*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 1652*49cdfc7eSAndroid Build Coastguard Worker .save_restore = (const struct tst_path_val[]) { 1653*49cdfc7eSAndroid Build Coastguard Worker {"/proc/sys/kernel/core_pattern", NULL, TST_SR_TCONF}, 1654*49cdfc7eSAndroid Build Coastguard Worker {"/proc/sys/user/max_user_namespaces", NULL, TST_SR_SKIP}, 1655*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/run", "1", TST_SR_TBROK}, 1656*49cdfc7eSAndroid Build Coastguard Worker {} 1657*49cdfc7eSAndroid Build Coastguard Worker }, 1658*49cdfc7eSAndroid Build Coastguard Worker}; 1659*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1660*49cdfc7eSAndroid Build Coastguard Worker 1661*49cdfc7eSAndroid Build Coastguard Worker1.28 Parsing kernel .config 1662*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1663*49cdfc7eSAndroid Build Coastguard Worker 1664*49cdfc7eSAndroid Build Coastguard WorkerGenerally testcases should attempt to autodetect as much kernel features as 1665*49cdfc7eSAndroid Build Coastguard Workerpossible based on the currently running kernel. We do have tst_check_driver() 1666*49cdfc7eSAndroid Build Coastguard Workerto check if functionality that could be compiled as kernel module is present 1667*49cdfc7eSAndroid Build Coastguard Workeron the system, disabled syscalls can be detected by checking for 'ENOSYS' 1668*49cdfc7eSAndroid Build Coastguard Workererrno etc. 1669*49cdfc7eSAndroid Build Coastguard Worker 1670*49cdfc7eSAndroid Build Coastguard WorkerHowever in rare cases core kernel features couldn't be detected based on the 1671*49cdfc7eSAndroid Build Coastguard Workerkernel userspace API and we have to resort to parse the kernel .config. 1672*49cdfc7eSAndroid Build Coastguard Worker 1673*49cdfc7eSAndroid Build Coastguard WorkerFor this cases the test should set the 'NULL' terminated '.needs_kconfigs' 1674*49cdfc7eSAndroid Build Coastguard Workerarray of boolean expressions with constraints on the kconfig variables. The 1675*49cdfc7eSAndroid Build Coastguard Workerboolean expression consits of variables, two binary operations '&' and '|', 1676*49cdfc7eSAndroid Build Coastguard Workernegation '!' and correct sequence of parentesis '()'. Variables are expected 1677*49cdfc7eSAndroid Build Coastguard Workerto be in a form of "CONFIG_FOO[=bar]". 1678*49cdfc7eSAndroid Build Coastguard Worker 1679*49cdfc7eSAndroid Build Coastguard WorkerThe test will continue to run if all expressions are evaluated to 'True'. 1680*49cdfc7eSAndroid Build Coastguard WorkerMissing variable is mapped to 'False' as well as variable with different than 1681*49cdfc7eSAndroid Build Coastguard Workerspecified value, e.g. 'CONFIG_FOO=bar' will evaluate to 'False' if the value 1682*49cdfc7eSAndroid Build Coastguard Workeris anything else but 'bar'. If config variable is specified as plain 1683*49cdfc7eSAndroid Build Coastguard Worker'CONFIG_FOO' it's evaluated to true it's set to any value (typically =y or =m). 1684*49cdfc7eSAndroid Build Coastguard Worker 1685*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1686*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1687*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1688*49cdfc7eSAndroid Build Coastguard Worker 1689*49cdfc7eSAndroid Build Coastguard Workerstatic const char *kconfigs[] = { 1690*49cdfc7eSAndroid Build Coastguard Worker "CONFIG_X86_INTEL_UMIP | CONFIG_X86_UMIP", 1691*49cdfc7eSAndroid Build Coastguard Worker NULL 1692*49cdfc7eSAndroid Build Coastguard Worker}; 1693*49cdfc7eSAndroid Build Coastguard Worker 1694*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1695*49cdfc7eSAndroid Build Coastguard Worker ... 1696*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = kconfigs, 1697*49cdfc7eSAndroid Build Coastguard Worker ... 1698*49cdfc7eSAndroid Build Coastguard Worker}; 1699*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1700*49cdfc7eSAndroid Build Coastguard Worker 1701*49cdfc7eSAndroid Build Coastguard Worker1.29 Changing the Wall Clock Time during test execution 1702*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1703*49cdfc7eSAndroid Build Coastguard Worker 1704*49cdfc7eSAndroid Build Coastguard WorkerThere are some tests that, for different reasons, might need to change the 1705*49cdfc7eSAndroid Build Coastguard Workersystem-wide clock time. Whenever this happens, it is imperative that the clock 1706*49cdfc7eSAndroid Build Coastguard Workeris restored, at the end of test's execution, taking in consideration the amount 1707*49cdfc7eSAndroid Build Coastguard Workerof time elapsed during that test. 1708*49cdfc7eSAndroid Build Coastguard Worker 1709*49cdfc7eSAndroid Build Coastguard WorkerIn order for that to happen, struct tst_test has a variable called 1710*49cdfc7eSAndroid Build Coastguard Worker"restore_wallclock" that should be set to "1" so LTP knows it should: (1) 1711*49cdfc7eSAndroid Build Coastguard Workerinitialize a monotonic clock during test setup phase and (2) use that monotonic 1712*49cdfc7eSAndroid Build Coastguard Workerclock to fix the system-wide clock time at the test cleanup phase. 1713*49cdfc7eSAndroid Build Coastguard Worker 1714*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1715*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1716*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1717*49cdfc7eSAndroid Build Coastguard Worker 1718*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1719*49cdfc7eSAndroid Build Coastguard Worker{ 1720*49cdfc7eSAndroid Build Coastguard Worker ... 1721*49cdfc7eSAndroid Build Coastguard Worker} 1722*49cdfc7eSAndroid Build Coastguard Worker 1723*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 1724*49cdfc7eSAndroid Build Coastguard Worker{ 1725*49cdfc7eSAndroid Build Coastguard Worker ... 1726*49cdfc7eSAndroid Build Coastguard Worker} 1727*49cdfc7eSAndroid Build Coastguard Worker 1728*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 1729*49cdfc7eSAndroid Build Coastguard Worker ... 1730*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 1731*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 1732*49cdfc7eSAndroid Build Coastguard Worker .restore_wallclock = 1, 1733*49cdfc7eSAndroid Build Coastguard Worker ... 1734*49cdfc7eSAndroid Build Coastguard Worker}; 1735*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1736*49cdfc7eSAndroid Build Coastguard Worker 1737*49cdfc7eSAndroid Build Coastguard Worker1.30 Testing similar syscalls in one test 1738*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1739*49cdfc7eSAndroid Build Coastguard Worker 1740*49cdfc7eSAndroid Build Coastguard WorkerIn some cases kernel has several very similar syscalls that do either the same 1741*49cdfc7eSAndroid Build Coastguard Workeror very similar job. This is most noticeable on i386 where we commonly have 1742*49cdfc7eSAndroid Build Coastguard Workertwo or three syscall versions. That is because i386 was first platform that 1743*49cdfc7eSAndroid Build Coastguard WorkerLinux was developed on and because of that most mistakes in API happened there 1744*49cdfc7eSAndroid Build Coastguard Workeras well. However this is not limited to i386 at all, it's quite common that 1745*49cdfc7eSAndroid Build Coastguard Workerversion two syscall has added missing flags parameters or so. 1746*49cdfc7eSAndroid Build Coastguard Worker 1747*49cdfc7eSAndroid Build Coastguard WorkerIn such cases it does not make much sense to copy&paste the test code over and 1748*49cdfc7eSAndroid Build Coastguard Workerover, rather than that the test library provides support for test variants. 1749*49cdfc7eSAndroid Build Coastguard WorkerThe idea behind test variants is simple, we run the test several times each 1750*49cdfc7eSAndroid Build Coastguard Workertime with different syscall variant. 1751*49cdfc7eSAndroid Build Coastguard Worker 1752*49cdfc7eSAndroid Build Coastguard WorkerThe implementation consist of test_variants integer that, if set, denotes number 1753*49cdfc7eSAndroid Build Coastguard Workerof test variants. The test is then forked and executed test_variants times each 1754*49cdfc7eSAndroid Build Coastguard Workertime with different value in global tst_variant variable. 1755*49cdfc7eSAndroid Build Coastguard Worker 1756*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1757*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1758*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1759*49cdfc7eSAndroid Build Coastguard Worker 1760*49cdfc7eSAndroid Build Coastguard Workerstatic int do_foo(void) 1761*49cdfc7eSAndroid Build Coastguard Worker{ 1762*49cdfc7eSAndroid Build Coastguard Worker switch (tst_variant) { 1763*49cdfc7eSAndroid Build Coastguard Worker case 0: 1764*49cdfc7eSAndroid Build Coastguard Worker return foo(); 1765*49cdfc7eSAndroid Build Coastguard Worker case 1: 1766*49cdfc7eSAndroid Build Coastguard Worker return syscall(__NR_foo); 1767*49cdfc7eSAndroid Build Coastguard Worker } 1768*49cdfc7eSAndroid Build Coastguard Worker 1769*49cdfc7eSAndroid Build Coastguard Worker return -1; 1770*49cdfc7eSAndroid Build Coastguard Worker} 1771*49cdfc7eSAndroid Build Coastguard Worker 1772*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 1773*49cdfc7eSAndroid Build Coastguard Worker{ 1774*49cdfc7eSAndroid Build Coastguard Worker ... 1775*49cdfc7eSAndroid Build Coastguard Worker 1776*49cdfc7eSAndroid Build Coastguard Worker TEST(do_foo); 1777*49cdfc7eSAndroid Build Coastguard Worker 1778*49cdfc7eSAndroid Build Coastguard Worker ... 1779*49cdfc7eSAndroid Build Coastguard Worker} 1780*49cdfc7eSAndroid Build Coastguard Worker 1781*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1782*49cdfc7eSAndroid Build Coastguard Worker{ 1783*49cdfc7eSAndroid Build Coastguard Worker switch (tst_variant) { 1784*49cdfc7eSAndroid Build Coastguard Worker case 0: 1785*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing foo variant 1"); 1786*49cdfc7eSAndroid Build Coastguard Worker break; 1787*49cdfc7eSAndroid Build Coastguard Worker case 1: 1788*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing foo variant 2"); 1789*49cdfc7eSAndroid Build Coastguard Worker break; 1790*49cdfc7eSAndroid Build Coastguard Worker } 1791*49cdfc7eSAndroid Build Coastguard Worker} 1792*49cdfc7eSAndroid Build Coastguard Worker 1793*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 1794*49cdfc7eSAndroid Build Coastguard Worker ... 1795*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 1796*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 1797*49cdfc7eSAndroid Build Coastguard Worker .test_variants = 2, 1798*49cdfc7eSAndroid Build Coastguard Worker ... 1799*49cdfc7eSAndroid Build Coastguard Worker}; 1800*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1801*49cdfc7eSAndroid Build Coastguard Worker 1802*49cdfc7eSAndroid Build Coastguard Worker1.31 Guarded buffers 1803*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~ 1804*49cdfc7eSAndroid Build Coastguard Worker 1805*49cdfc7eSAndroid Build Coastguard WorkerThe test library supports guarded buffers, which are buffers allocated so 1806*49cdfc7eSAndroid Build Coastguard Workerthat: 1807*49cdfc7eSAndroid Build Coastguard Worker 1808*49cdfc7eSAndroid Build Coastguard Worker* The end of the buffer is followed by a PROT_NONE page 1809*49cdfc7eSAndroid Build Coastguard Worker 1810*49cdfc7eSAndroid Build Coastguard Worker* The remainder of the page before the buffer is filled with random canary 1811*49cdfc7eSAndroid Build Coastguard Worker data 1812*49cdfc7eSAndroid Build Coastguard Worker 1813*49cdfc7eSAndroid Build Coastguard WorkerWhich means that the any access after the buffer will yield a Segmentation 1814*49cdfc7eSAndroid Build Coastguard Workerfault or EFAULT depending on if the access happened in userspace or the kernel 1815*49cdfc7eSAndroid Build Coastguard Workerrespectively. The canary before the buffer will also catch any write access 1816*49cdfc7eSAndroid Build Coastguard Workeroutside of the buffer. 1817*49cdfc7eSAndroid Build Coastguard Worker 1818*49cdfc7eSAndroid Build Coastguard WorkerThe purpose of the patch is to catch off-by-one bugs which happens when 1819*49cdfc7eSAndroid Build Coastguard Workerbuffers and structures are passed to syscalls. New tests should allocate 1820*49cdfc7eSAndroid Build Coastguard Workerguarded buffers for all data passed to the tested syscall which are passed by 1821*49cdfc7eSAndroid Build Coastguard Workera pointer. 1822*49cdfc7eSAndroid Build Coastguard Worker 1823*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1824*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1825*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1826*49cdfc7eSAndroid Build Coastguard Worker 1827*49cdfc7eSAndroid Build Coastguard Workerstatic struct foo *foo_ptr; 1828*49cdfc7eSAndroid Build Coastguard Workerstatic struct iovec *iov; 1829*49cdfc7eSAndroid Build Coastguard Workerstatic void *buf_ptr; 1830*49cdfc7eSAndroid Build Coastguard Workerstatic char *id; 1831*49cdfc7eSAndroid Build Coastguard Worker... 1832*49cdfc7eSAndroid Build Coastguard Worker 1833*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 1834*49cdfc7eSAndroid Build Coastguard Worker{ 1835*49cdfc7eSAndroid Build Coastguard Worker ... 1836*49cdfc7eSAndroid Build Coastguard Worker 1837*49cdfc7eSAndroid Build Coastguard Worker foo_ptr->bar = 1; 1838*49cdfc7eSAndroid Build Coastguard Worker foo_ptr->buf = buf_ptr; 1839*49cdfc7eSAndroid Build Coastguard Worker 1840*49cdfc7eSAndroid Build Coastguard Worker ... 1841*49cdfc7eSAndroid Build Coastguard Worker} 1842*49cdfc7eSAndroid Build Coastguard Worker 1843*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1844*49cdfc7eSAndroid Build Coastguard Worker{ 1845*49cdfc7eSAndroid Build Coastguard Worker ... 1846*49cdfc7eSAndroid Build Coastguard Worker 1847*49cdfc7eSAndroid Build Coastguard Worker id = tst_strdup(string); 1848*49cdfc7eSAndroid Build Coastguard Worker 1849*49cdfc7eSAndroid Build Coastguard Worker ... 1850*49cdfc7eSAndroid Build Coastguard Worker} 1851*49cdfc7eSAndroid Build Coastguard Worker 1852*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1853*49cdfc7eSAndroid Build Coastguard Worker ... 1854*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) { 1855*49cdfc7eSAndroid Build Coastguard Worker {&foo_ptr, .size = sizeof(*foo_ptr)}, 1856*49cdfc7eSAndroid Build Coastguard Worker {&buf_ptr, .size = BUF_SIZE}, 1857*49cdfc7eSAndroid Build Coastguard Worker {&iov, .iov_sizes = (int[]){128, 32, -1}, 1858*49cdfc7eSAndroid Build Coastguard Worker {} 1859*49cdfc7eSAndroid Build Coastguard Worker } 1860*49cdfc7eSAndroid Build Coastguard Worker}; 1861*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 1862*49cdfc7eSAndroid Build Coastguard Worker 1863*49cdfc7eSAndroid Build Coastguard WorkerGuarded buffers can be allocated on runtime in a test setup() by a 1864*49cdfc7eSAndroid Build Coastguard Worker'tst_alloc()' or by 'tst_strdup()' as well as by filling up the .bufs array in 1865*49cdfc7eSAndroid Build Coastguard Workerthe tst_test structure. 1866*49cdfc7eSAndroid Build Coastguard Worker 1867*49cdfc7eSAndroid Build Coastguard WorkerSo far the tst_test structure supports allocating either a plain buffer by 1868*49cdfc7eSAndroid Build Coastguard Workersetting up the size or struct iovec, which is allocated recursively including 1869*49cdfc7eSAndroid Build Coastguard Workerthe individual buffers as described by an '-1' terminated array of buffer 1870*49cdfc7eSAndroid Build Coastguard Workersizes. 1871*49cdfc7eSAndroid Build Coastguard Worker 1872*49cdfc7eSAndroid Build Coastguard Worker1.32 Adding and removing capabilities 1873*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1874*49cdfc7eSAndroid Build Coastguard Worker 1875*49cdfc7eSAndroid Build Coastguard WorkerSome tests may require the presence or absence of particular 1876*49cdfc7eSAndroid Build Coastguard Workercapabilities. Using the API provided by 'tst_capability.h' the test author can 1877*49cdfc7eSAndroid Build Coastguard Workertry to ensure that some capabilities are either present or absent during the 1878*49cdfc7eSAndroid Build Coastguard Workertest. 1879*49cdfc7eSAndroid Build Coastguard Worker 1880*49cdfc7eSAndroid Build Coastguard WorkerFor example; below we try to create a raw socket, which requires 1881*49cdfc7eSAndroid Build Coastguard WorkerCAP_NET_ADMIN. During setup we should be able to do it, then during run it 1882*49cdfc7eSAndroid Build Coastguard Workershould be impossible. The LTP capability library will check before setup that 1883*49cdfc7eSAndroid Build Coastguard Workerwe have this capability, then after setup it will drop it. 1884*49cdfc7eSAndroid Build Coastguard Worker 1885*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1886*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 1887*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 1888*49cdfc7eSAndroid Build Coastguard Worker#include "tst_capability.h" 1889*49cdfc7eSAndroid Build Coastguard Worker#include "tst_safe_net.h" 1890*49cdfc7eSAndroid Build Coastguard Worker 1891*49cdfc7eSAndroid Build Coastguard Worker#include "lapi/socket.h" 1892*49cdfc7eSAndroid Build Coastguard Worker 1893*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 1894*49cdfc7eSAndroid Build Coastguard Worker{ 1895*49cdfc7eSAndroid Build Coastguard Worker TEST(socket(AF_INET, SOCK_RAW, 1)); 1896*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET > -1) { 1897*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Created raw socket"); 1898*49cdfc7eSAndroid Build Coastguard Worker } else if (TST_ERR != EPERM) { 1899*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, 1900*49cdfc7eSAndroid Build Coastguard Worker "Failed to create socket for wrong reason"); 1901*49cdfc7eSAndroid Build Coastguard Worker } else { 1902*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "Didn't create raw socket"); 1903*49cdfc7eSAndroid Build Coastguard Worker } 1904*49cdfc7eSAndroid Build Coastguard Worker} 1905*49cdfc7eSAndroid Build Coastguard Worker 1906*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1907*49cdfc7eSAndroid Build Coastguard Worker{ 1908*49cdfc7eSAndroid Build Coastguard Worker TEST(socket(AF_INET, SOCK_RAW, 1)); 1909*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0) 1910*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF | TTERRNO, "We don't have CAP_NET_RAW to begin with"); 1911*49cdfc7eSAndroid Build Coastguard Worker 1912*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET); 1913*49cdfc7eSAndroid Build Coastguard Worker} 1914*49cdfc7eSAndroid Build Coastguard Worker 1915*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1916*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 1917*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 1918*49cdfc7eSAndroid Build Coastguard Worker .caps = (struct tst_cap []) { 1919*49cdfc7eSAndroid Build Coastguard Worker TST_CAP(TST_CAP_REQ, CAP_NET_RAW), 1920*49cdfc7eSAndroid Build Coastguard Worker TST_CAP(TST_CAP_DROP, CAP_NET_RAW), 1921*49cdfc7eSAndroid Build Coastguard Worker {} 1922*49cdfc7eSAndroid Build Coastguard Worker }, 1923*49cdfc7eSAndroid Build Coastguard Worker}; 1924*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 1925*49cdfc7eSAndroid Build Coastguard Worker 1926*49cdfc7eSAndroid Build Coastguard WorkerLook at the test struct at the bottom. We have filled in the 'caps' field with 1927*49cdfc7eSAndroid Build Coastguard Workera 'NULL' terminated array containing two 'tst_cap' structs. 'TST_CAP_REQ' 1928*49cdfc7eSAndroid Build Coastguard Workeractions are executed before setup and 'TST_CAP_DROP' are executed after 1929*49cdfc7eSAndroid Build Coastguard Workersetup. This means it is possible to both request and drop a capability. 1930*49cdfc7eSAndroid Build Coastguard Worker 1931*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1932*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 1933*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 1934*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 1935*49cdfc7eSAndroid Build Coastguard Worker .caps = (struct tst_cap []) { 1936*49cdfc7eSAndroid Build Coastguard Worker TST_CAP(TST_CAP_REQ, CAP_NET_RAW), 1937*49cdfc7eSAndroid Build Coastguard Worker TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN), 1938*49cdfc7eSAndroid Build Coastguard Worker {} 1939*49cdfc7eSAndroid Build Coastguard Worker }, 1940*49cdfc7eSAndroid Build Coastguard Worker}; 1941*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 1942*49cdfc7eSAndroid Build Coastguard Worker 1943*49cdfc7eSAndroid Build Coastguard WorkerHere we request 'CAP_NET_RAW', but drop 'CAP_SYS_ADMIN'. If the capability is 1944*49cdfc7eSAndroid Build Coastguard Workerin the permitted set, but not the effective set, the library will try to 1945*49cdfc7eSAndroid Build Coastguard Workerpermit it. If it is not in the permitted set, then it will fail with 'TCONF'. 1946*49cdfc7eSAndroid Build Coastguard Worker 1947*49cdfc7eSAndroid Build Coastguard WorkerThis API does not require 'libcap' to be installed. However it has limited 1948*49cdfc7eSAndroid Build Coastguard Workerfeatures relative to 'libcap'. It only tries to add or remove capabilities 1949*49cdfc7eSAndroid Build Coastguard Workerfrom the effective set. This means that tests which need to spawn child 1950*49cdfc7eSAndroid Build Coastguard Workerprocesses may have difficulties ensuring the correct capabilities are 1951*49cdfc7eSAndroid Build Coastguard Workeravailable to the children (see the capabilities (7) manual pages). 1952*49cdfc7eSAndroid Build Coastguard Worker 1953*49cdfc7eSAndroid Build Coastguard WorkerHowever a lot of problems can be solved by using 'tst_cap_action(struct 1954*49cdfc7eSAndroid Build Coastguard Workertst_cap *cap)' directly which can be called at any time. This also helps if 1955*49cdfc7eSAndroid Build Coastguard Workeryou wish to drop a capability at the beginning of setup. 1956*49cdfc7eSAndroid Build Coastguard Worker 1957*49cdfc7eSAndroid Build Coastguard Worker1.33 Reproducing race-conditions 1958*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1959*49cdfc7eSAndroid Build Coastguard Worker 1960*49cdfc7eSAndroid Build Coastguard WorkerIf a bug is caused by two tasks in the kernel racing and you wish to create a 1961*49cdfc7eSAndroid Build Coastguard Workerregression test (or bug-fix validation test) then the 'tst_fuzzy_sync.h' 1962*49cdfc7eSAndroid Build Coastguard Workerlibrary should be used. 1963*49cdfc7eSAndroid Build Coastguard Worker 1964*49cdfc7eSAndroid Build Coastguard WorkerIt allows you to specify, in your code, two race windows. One window in each 1965*49cdfc7eSAndroid Build Coastguard Workerthread's loop (triggering a race usually requires many iterations). These 1966*49cdfc7eSAndroid Build Coastguard Workerwindows show fuzzy-sync where the race can happen. They don't need to be 1967*49cdfc7eSAndroid Build Coastguard Workerexact, hence the 'fuzzy' part. If the race condition is not immediately 1968*49cdfc7eSAndroid Build Coastguard Workertriggered then the library will begin experimenting with different timings. 1969*49cdfc7eSAndroid Build Coastguard Worker 1970*49cdfc7eSAndroid Build Coastguard Worker[source,c] 1971*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 1972*49cdfc7eSAndroid Build Coastguard Worker#include "tst_fuzzy_sync.h" 1973*49cdfc7eSAndroid Build Coastguard Worker 1974*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_fzsync_pair fzsync_pair; 1975*49cdfc7eSAndroid Build Coastguard Worker 1976*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 1977*49cdfc7eSAndroid Build Coastguard Worker{ 1978*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzsync_pair); 1979*49cdfc7eSAndroid Build Coastguard Worker} 1980*49cdfc7eSAndroid Build Coastguard Worker 1981*49cdfc7eSAndroid Build Coastguard Workerstatic void cleanup(void) 1982*49cdfc7eSAndroid Build Coastguard Worker{ 1983*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzsync_pair); 1984*49cdfc7eSAndroid Build Coastguard Worker} 1985*49cdfc7eSAndroid Build Coastguard Worker 1986*49cdfc7eSAndroid Build Coastguard Workerstatic void *thread_b(void *arg) 1987*49cdfc7eSAndroid Build Coastguard Worker{ 1988*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_b(&fzsync_pair)) { 1989*49cdfc7eSAndroid Build Coastguard Worker 1990*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzsync_pair); 1991*49cdfc7eSAndroid Build Coastguard Worker 1992*49cdfc7eSAndroid Build Coastguard Worker /* This is the race window for thread B */ 1993*49cdfc7eSAndroid Build Coastguard Worker 1994*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzsync_pair); 1995*49cdfc7eSAndroid Build Coastguard Worker } 1996*49cdfc7eSAndroid Build Coastguard Worker 1997*49cdfc7eSAndroid Build Coastguard Worker return arg; 1998*49cdfc7eSAndroid Build Coastguard Worker} 1999*49cdfc7eSAndroid Build Coastguard Worker 2000*49cdfc7eSAndroid Build Coastguard Workerstatic void thread_a(void) 2001*49cdfc7eSAndroid Build Coastguard Worker{ 2002*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzsync_pair, thread_b); 2003*49cdfc7eSAndroid Build Coastguard Worker 2004*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzsync_pair)) { 2005*49cdfc7eSAndroid Build Coastguard Worker 2006*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzsync_pair); 2007*49cdfc7eSAndroid Build Coastguard Worker 2008*49cdfc7eSAndroid Build Coastguard Worker /* This is the race window for thread A */ 2009*49cdfc7eSAndroid Build Coastguard Worker 2010*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzsync_pair); 2011*49cdfc7eSAndroid Build Coastguard Worker } 2012*49cdfc7eSAndroid Build Coastguard Worker} 2013*49cdfc7eSAndroid Build Coastguard Worker 2014*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 2015*49cdfc7eSAndroid Build Coastguard Worker .test_all = thread_a, 2016*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 2017*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup, 2018*49cdfc7eSAndroid Build Coastguard Worker}; 2019*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------- 2020*49cdfc7eSAndroid Build Coastguard Worker 2021*49cdfc7eSAndroid Build Coastguard WorkerAbove is a minimal template for a test using fuzzy-sync. In a simple case, you 2022*49cdfc7eSAndroid Build Coastguard Workerjust need to put the bits you want to race inbetween 'start_race' and 2023*49cdfc7eSAndroid Build Coastguard Worker'end_race'. Meanwhile, any setup you need to do per-iteration goes outside the 2024*49cdfc7eSAndroid Build Coastguard Workerwindows. 2025*49cdfc7eSAndroid Build Coastguard Worker 2026*49cdfc7eSAndroid Build Coastguard WorkerFuzzy sync synchronises 'run_a' and 'run_b', which act as barriers, so that 2027*49cdfc7eSAndroid Build Coastguard Workerneither thread can progress until the other has caught up with it. There is 2028*49cdfc7eSAndroid Build Coastguard Workeralso the 'pair_wait' function which can be used to add barriers in other 2029*49cdfc7eSAndroid Build Coastguard Workerlocations. Of course 'start/end_race_a/b' are also a barriers. 2030*49cdfc7eSAndroid Build Coastguard Worker 2031*49cdfc7eSAndroid Build Coastguard WorkerThe library decides how long the test should run for based on the timeout 2032*49cdfc7eSAndroid Build Coastguard Workerspecified by the user plus some other heuristics. 2033*49cdfc7eSAndroid Build Coastguard Worker 2034*49cdfc7eSAndroid Build Coastguard WorkerFor full documentation see the comments in 'include/tst_fuzzy_sync.h'. 2035*49cdfc7eSAndroid Build Coastguard Worker 2036*49cdfc7eSAndroid Build Coastguard Worker1.34 Reserving hugepages 2037*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 2038*49cdfc7eSAndroid Build Coastguard Worker 2039*49cdfc7eSAndroid Build Coastguard WorkerMany of the LTP tests need to use hugepage in their testing, this allows the 2040*49cdfc7eSAndroid Build Coastguard Workertest can reserve hugepages from system via '.hugepages = {xx, TST_REQUEST}'. 2041*49cdfc7eSAndroid Build Coastguard Worker 2042*49cdfc7eSAndroid Build Coastguard WorkerWe achieved two policies for reserving hugepages: 2043*49cdfc7eSAndroid Build Coastguard Worker 2044*49cdfc7eSAndroid Build Coastguard WorkerTST_REQUEST: 2045*49cdfc7eSAndroid Build Coastguard Worker It will try the best to reserve available huge pages and return the number 2046*49cdfc7eSAndroid Build Coastguard Worker of available hugepages in tst_hugepages, which may be 0 if hugepages are 2047*49cdfc7eSAndroid Build Coastguard Worker not supported at all. 2048*49cdfc7eSAndroid Build Coastguard Worker 2049*49cdfc7eSAndroid Build Coastguard WorkerTST_NEEDS: 2050*49cdfc7eSAndroid Build Coastguard Worker This is an enforced requirement, LTP should strictly do hpages applying and 2051*49cdfc7eSAndroid Build Coastguard Worker guarantee the 'HugePages_Free' no less than pages which makes that test can 2052*49cdfc7eSAndroid Build Coastguard Worker use these specified numbers correctly. Otherwise, test exits with TCONF if 2053*49cdfc7eSAndroid Build Coastguard Worker the attempt to reserve hugepages fails or reserves less than requested. 2054*49cdfc7eSAndroid Build Coastguard Worker 2055*49cdfc7eSAndroid Build Coastguard WorkerWith success test stores the reserved hugepage number in 'tst_hugepages'. For 2056*49cdfc7eSAndroid Build Coastguard Workersystem without hugetlb supporting, variable 'tst_hugepages' will be set to 0. 2057*49cdfc7eSAndroid Build Coastguard WorkerIf the hugepage number needs to be set to 0 on supported hugetlb system, please 2058*49cdfc7eSAndroid Build Coastguard Workeruse '.hugepages = {TST_NO_HUGEPAGES}'. 2059*49cdfc7eSAndroid Build Coastguard Worker 2060*49cdfc7eSAndroid Build Coastguard WorkerAlso, we do cleanup and restore work for the hpages resetting automatically. 2061*49cdfc7eSAndroid Build Coastguard Worker 2062*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2063*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2064*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 2065*49cdfc7eSAndroid Build Coastguard Worker 2066*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 2067*49cdfc7eSAndroid Build Coastguard Worker{ 2068*49cdfc7eSAndroid Build Coastguard Worker ... 2069*49cdfc7eSAndroid Build Coastguard Worker 2070*49cdfc7eSAndroid Build Coastguard Worker if (tst_hugepages == test.hugepages.number) 2071*49cdfc7eSAndroid Build Coastguard Worker TEST(do_hpage_test); 2072*49cdfc7eSAndroid Build Coastguard Worker else 2073*49cdfc7eSAndroid Build Coastguard Worker ... 2074*49cdfc7eSAndroid Build Coastguard Worker ... 2075*49cdfc7eSAndroid Build Coastguard Worker} 2076*49cdfc7eSAndroid Build Coastguard Worker 2077*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2078*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 2079*49cdfc7eSAndroid Build Coastguard Worker .hugepages = {2, TST_REQUEST}, 2080*49cdfc7eSAndroid Build Coastguard Worker ... 2081*49cdfc7eSAndroid Build Coastguard Worker}; 2082*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2083*49cdfc7eSAndroid Build Coastguard Worker 2084*49cdfc7eSAndroid Build Coastguard Workeror, 2085*49cdfc7eSAndroid Build Coastguard Worker 2086*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2087*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2088*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 2089*49cdfc7eSAndroid Build Coastguard Worker 2090*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 2091*49cdfc7eSAndroid Build Coastguard Worker{ 2092*49cdfc7eSAndroid Build Coastguard Worker ... 2093*49cdfc7eSAndroid Build Coastguard Worker} 2094*49cdfc7eSAndroid Build Coastguard Worker 2095*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 2096*49cdfc7eSAndroid Build Coastguard Worker{ 2097*49cdfc7eSAndroid Build Coastguard Worker /* TST_NEEDS achieved this automatically in the library */ 2098*49cdfc7eSAndroid Build Coastguard Worker if (tst_hugepages != test.hugepages.number) 2099*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "..."); 2100*49cdfc7eSAndroid Build Coastguard Worker} 2101*49cdfc7eSAndroid Build Coastguard Worker 2102*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2103*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 2104*49cdfc7eSAndroid Build Coastguard Worker .hugepages = {2, TST_NEEDS}, 2105*49cdfc7eSAndroid Build Coastguard Worker ... 2106*49cdfc7eSAndroid Build Coastguard Worker}; 2107*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2108*49cdfc7eSAndroid Build Coastguard Worker 2109*49cdfc7eSAndroid Build Coastguard Worker1.35 Checking for required commands 2110*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2111*49cdfc7eSAndroid Build Coastguard Worker 2112*49cdfc7eSAndroid Build Coastguard WorkerRequired commands can be checked with '.needs_cmds', which points to a 'NULL' 2113*49cdfc7eSAndroid Build Coastguard Workerterminated array of strings such as: 2114*49cdfc7eSAndroid Build Coastguard Worker 2115*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2116*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2117*49cdfc7eSAndroid Build Coastguard Worker.needs_cmds = (const char *const []) { 2118*49cdfc7eSAndroid Build Coastguard Worker "useradd", 2119*49cdfc7eSAndroid Build Coastguard Worker "userdel", 2120*49cdfc7eSAndroid Build Coastguard Worker NULL 2121*49cdfc7eSAndroid Build Coastguard Worker}, 2122*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2123*49cdfc7eSAndroid Build Coastguard Worker 2124*49cdfc7eSAndroid Build Coastguard WorkerAlso can check required command version whether is satisfied by using 'needs_cmds' 2125*49cdfc7eSAndroid Build Coastguard Workersuch as: 2126*49cdfc7eSAndroid Build Coastguard Worker 2127*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2128*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2129*49cdfc7eSAndroid Build Coastguard Worker.needs_cmds = (const char *const []) { 2130*49cdfc7eSAndroid Build Coastguard Worker "mkfs.ext4 >= 1.43.0", 2131*49cdfc7eSAndroid Build Coastguard Worker NULL 2132*49cdfc7eSAndroid Build Coastguard Worker}, 2133*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2134*49cdfc7eSAndroid Build Coastguard Worker 2135*49cdfc7eSAndroid Build Coastguard WorkerCurrently, we only support mkfs.ext4 command version check. 2136*49cdfc7eSAndroid Build Coastguard WorkerIf you want to support more commands, please fill your own .parser and .table_get 2137*49cdfc7eSAndroid Build Coastguard Workermethod in the version_parsers structure of lib/tst_cmd.c. 2138*49cdfc7eSAndroid Build Coastguard Worker 2139*49cdfc7eSAndroid Build Coastguard Worker1.36 Assert sys or proc file value 2140*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2141*49cdfc7eSAndroid Build Coastguard WorkerUsing TST_ASSERT_INT/STR(path, val) to assert that integer value or string stored in 2142*49cdfc7eSAndroid Build Coastguard Workerthe prefix field of file pointed by path equals to the value passed to this function. 2143*49cdfc7eSAndroid Build Coastguard Worker 2144*49cdfc7eSAndroid Build Coastguard WorkerAlso having a similar api pair TST_ASSERT_FILE_INT/STR(path, prefix, val) to assert 2145*49cdfc7eSAndroid Build Coastguard Workerthe field value of file. 2146*49cdfc7eSAndroid Build Coastguard Worker 2147*49cdfc7eSAndroid Build Coastguard Worker1.37 Using Control Group 2148*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 2149*49cdfc7eSAndroid Build Coastguard Worker 2150*49cdfc7eSAndroid Build Coastguard WorkerSome LTP tests need specific Control Group configurations. 'tst_cgroup.h' 2151*49cdfc7eSAndroid Build Coastguard Workerprovides APIs to discover and use CGroups. There are many differences between 2152*49cdfc7eSAndroid Build Coastguard WorkerCGroups API V1 and V2. We encapsulate the details of configuring CGroups in 2153*49cdfc7eSAndroid Build Coastguard Workerhigh-level functions which follow the V2 kernel API where possible. Allowing one 2154*49cdfc7eSAndroid Build Coastguard Workerto write code that works on both V1 or V2. At least some of the time anyway; 2155*49cdfc7eSAndroid Build Coastguard Workeroften the behavioural differences between V1 and V2 are too great. In such cases 2156*49cdfc7eSAndroid Build Coastguard Workerwe revert to branching on the CGroup version. 2157*49cdfc7eSAndroid Build Coastguard Worker 2158*49cdfc7eSAndroid Build Coastguard WorkerAlso, the LTP library will automatically mount/umount and configure the CGroup 2159*49cdfc7eSAndroid Build Coastguard Workerhierarchies if that is required (e.g. if you run the tests from init with no 2160*49cdfc7eSAndroid Build Coastguard Workersystem manager). 2161*49cdfc7eSAndroid Build Coastguard Worker 2162*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2163*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2164*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 2165*49cdfc7eSAndroid Build Coastguard Worker 2166*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 2167*49cdfc7eSAndroid Build Coastguard Worker{ 2168*49cdfc7eSAndroid Build Coastguard Worker ... 2169*49cdfc7eSAndroid Build Coastguard Worker // do test under cgroup 2170*49cdfc7eSAndroid Build Coastguard Worker ... 2171*49cdfc7eSAndroid Build Coastguard Worker} 2172*49cdfc7eSAndroid Build Coastguard Worker 2173*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void) 2174*49cdfc7eSAndroid Build Coastguard Worker{ 2175*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid()); 2176*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", MEMSIZE); 2177*49cdfc7eSAndroid Build Coastguard Worker if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) 2178*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%zu", memsw); 2179*49cdfc7eSAndroid Build Coastguard Worker} 2180*49cdfc7eSAndroid Build Coastguard Worker 2181*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2182*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 2183*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 2184*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup, 2185*49cdfc7eSAndroid Build Coastguard Worker .needs_cgroup_ctrls = (const char *const []){ "memory", NULL }, 2186*49cdfc7eSAndroid Build Coastguard Worker ... 2187*49cdfc7eSAndroid Build Coastguard Worker}; 2188*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2189*49cdfc7eSAndroid Build Coastguard Worker 2190*49cdfc7eSAndroid Build Coastguard WorkerAbove, we first ensure the memory controller is available on the 2191*49cdfc7eSAndroid Build Coastguard Workertest's CGroup with '.needs_cgroup_ctrls'. This populates a structure, 2192*49cdfc7eSAndroid Build Coastguard Worker'tst_cg', which represents the test's CGroup. 2193*49cdfc7eSAndroid Build Coastguard Worker 2194*49cdfc7eSAndroid Build Coastguard WorkerWe then write the current processes PID into 'cgroup.procs', which 2195*49cdfc7eSAndroid Build Coastguard Workermoves the current process into the test's CGroup. After which we set 2196*49cdfc7eSAndroid Build Coastguard Workerthe maximum memory size by writing to 'memory.max'. If the memory 2197*49cdfc7eSAndroid Build Coastguard Workercontroller is mounted on CGroups V1 then the library will actually 2198*49cdfc7eSAndroid Build Coastguard Workerwrite to 'memory.limit_in_bytes'. As a general rule, if a file exists 2199*49cdfc7eSAndroid Build Coastguard Workeron both CGroup versions, then we use the V2 naming. 2200*49cdfc7eSAndroid Build Coastguard Worker 2201*49cdfc7eSAndroid Build Coastguard WorkerSome controller features, such as 'memory.swap', can be 2202*49cdfc7eSAndroid Build Coastguard Workerdisabled. Therefor we need to check if they exist before accessing 2203*49cdfc7eSAndroid Build Coastguard Workerthem. This can be done with 'SAFE_CG_HAS' which can be called on 2204*49cdfc7eSAndroid Build Coastguard Workerany control file or feature. 2205*49cdfc7eSAndroid Build Coastguard Worker 2206*49cdfc7eSAndroid Build Coastguard WorkerMost tests only require setting a few limits similar to the above. In 2207*49cdfc7eSAndroid Build Coastguard Workersuch cases the differences between V1 and V2 are hidden. Setup and 2208*49cdfc7eSAndroid Build Coastguard Workercleanup is also mostly hidden. However things can get much worse. 2209*49cdfc7eSAndroid Build Coastguard Worker 2210*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2211*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2212*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_cg_group *cg_child; 2213*49cdfc7eSAndroid Build Coastguard Worker 2214*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void) 2215*49cdfc7eSAndroid Build Coastguard Worker{ 2216*49cdfc7eSAndroid Build Coastguard Worker char buf[BUFSIZ]; 2217*49cdfc7eSAndroid Build Coastguard Worker size_t mem = 0; 2218*49cdfc7eSAndroid Build Coastguard Worker 2219*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_mk(tst_cg, "child"); 2220*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid()); 2221*49cdfc7eSAndroid Build Coastguard Worker 2222*49cdfc7eSAndroid Build Coastguard Worker if (!TST_CG_VER_IS_V1(tst_cg, "memory")) 2223*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+memory"); 2224*49cdfc7eSAndroid Build Coastguard Worker if (!TST_CG_VER_IS_V1(tst_cg, "cpuset")) 2225*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+cpuset"); 2226*49cdfc7eSAndroid Build Coastguard Worker 2227*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) { 2228*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid()); 2229*49cdfc7eSAndroid Build Coastguard Worker 2230*49cdfc7eSAndroid Build Coastguard Worker if (SAFE_CG_HAS(cg_child, "memory.swap")) { 2231*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_SCANF(cg_child, 2232*49cdfc7eSAndroid Build Coastguard Worker "memory.swap.current", "%zu", &mem); 2233*49cdfc7eSAndroid Build Coastguard Worker } 2234*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_READ(cg_child, "cpuset.mems", buf, sizeof(buf)); 2235*49cdfc7eSAndroid Build Coastguard Worker 2236*49cdfc7eSAndroid Build Coastguard Worker // Do something with cpuset.mems and memory.current values 2237*49cdfc7eSAndroid Build Coastguard Worker ... 2238*49cdfc7eSAndroid Build Coastguard Worker 2239*49cdfc7eSAndroid Build Coastguard Worker exit(0); 2240*49cdfc7eSAndroid Build Coastguard Worker } 2241*49cdfc7eSAndroid Build Coastguard Worker 2242*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children(); 2243*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid()); 2244*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_rm(cg_child); 2245*49cdfc7eSAndroid Build Coastguard Worker} 2246*49cdfc7eSAndroid Build Coastguard Worker 2247*49cdfc7eSAndroid Build Coastguard Workerstatic void cleanup(void) 2248*49cdfc7eSAndroid Build Coastguard Worker{ 2249*49cdfc7eSAndroid Build Coastguard Worker if (cg_child) { 2250*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid()); 2251*49cdfc7eSAndroid Build Coastguard Worker cg_child = tst_cg_group_rm(cg_child); 2252*49cdfc7eSAndroid Build Coastguard Worker } 2253*49cdfc7eSAndroid Build Coastguard Worker} 2254*49cdfc7eSAndroid Build Coastguard Worker 2255*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2256*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 2257*49cdfc7eSAndroid Build Coastguard Worker .test_all = run, 2258*49cdfc7eSAndroid Build Coastguard Worker .needs_cgroup_ctrls = (const char *const []){ 2259*49cdfc7eSAndroid Build Coastguard Worker "cpuset", 2260*49cdfc7eSAndroid Build Coastguard Worker "memory", 2261*49cdfc7eSAndroid Build Coastguard Worker NULL 2262*49cdfc7eSAndroid Build Coastguard Worker }, 2263*49cdfc7eSAndroid Build Coastguard Worker ... 2264*49cdfc7eSAndroid Build Coastguard Worker}; 2265*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2266*49cdfc7eSAndroid Build Coastguard Worker 2267*49cdfc7eSAndroid Build Coastguard WorkerStarting with setup; we can see here that we fetch the 'drain' 2268*49cdfc7eSAndroid Build Coastguard WorkerCGroup. This is a shared group (between parallel tests) which may 2269*49cdfc7eSAndroid Build Coastguard Workercontain processes from other tests. It should have default settings 2270*49cdfc7eSAndroid Build Coastguard Workerand these should not be changed by the test. It can be used to remove 2271*49cdfc7eSAndroid Build Coastguard Workerprocesses from other CGroups incase the hierarchy root is not 2272*49cdfc7eSAndroid Build Coastguard Workeraccessible. 2273*49cdfc7eSAndroid Build Coastguard Worker 2274*49cdfc7eSAndroid Build Coastguard WorkerNote that 'tst_cg_get_drain_group' should not be called many times, 2275*49cdfc7eSAndroid Build Coastguard Workeras it is allocated in a guarded buffer (See section 2.2.31). Therefor 2276*49cdfc7eSAndroid Build Coastguard Workerit is best to call it once in 'setup' and not 'run' because 'run' may 2277*49cdfc7eSAndroid Build Coastguard Workerbe repeated with the '-i' option. 2278*49cdfc7eSAndroid Build Coastguard Worker 2279*49cdfc7eSAndroid Build Coastguard WorkerIn 'run', we first create a child CGroup with 'tst_cg_mk'. As we 2280*49cdfc7eSAndroid Build Coastguard Workercreate this CGroup in 'run' we should also remove it at the end of 2281*49cdfc7eSAndroid Build Coastguard Workerrun. We also need to check if it exists and remove it in cleanup as 2282*49cdfc7eSAndroid Build Coastguard Workerwell. Because there are 'SAFE_' functions which may jump to cleanup. 2283*49cdfc7eSAndroid Build Coastguard Worker 2284*49cdfc7eSAndroid Build Coastguard WorkerWe then move the main test process into the child CGroup. This is 2285*49cdfc7eSAndroid Build Coastguard Workerimportant as it means that before we destroy the child CGroup we have 2286*49cdfc7eSAndroid Build Coastguard Workerto move the main test process elsewhere. For that we use the 'drain' 2287*49cdfc7eSAndroid Build Coastguard Workergroup. 2288*49cdfc7eSAndroid Build Coastguard Worker 2289*49cdfc7eSAndroid Build Coastguard WorkerNext we enable the memory and cpuset controller configuration on the 2290*49cdfc7eSAndroid Build Coastguard Workertest CGroup's descendants (i.e. 'cg_child'). This allows each child to 2291*49cdfc7eSAndroid Build Coastguard Workerhave its own settings. The file 'cgroup.subtree_control' does not 2292*49cdfc7eSAndroid Build Coastguard Workerexist on V1. Because it is possible to have both V1 and V2 active at 2293*49cdfc7eSAndroid Build Coastguard Workerthe same time. We can not simply check if 'subtree_control' exists 2294*49cdfc7eSAndroid Build Coastguard Workerbefore writing to it. We have to check if a particular controller is 2295*49cdfc7eSAndroid Build Coastguard Workeron V2 before trying to add it to 'subtree_control'. Trying to add a V1 2296*49cdfc7eSAndroid Build Coastguard Workercontroller will result in 'ENOENT'. 2297*49cdfc7eSAndroid Build Coastguard Worker 2298*49cdfc7eSAndroid Build Coastguard WorkerWe then fork a child process and add this to the child CGroup. Within 2299*49cdfc7eSAndroid Build Coastguard Workerthe child process we try to read 'memory.swap.current'. It is possible 2300*49cdfc7eSAndroid Build Coastguard Workerthat the memory controller was compiled without swap support, so it is 2301*49cdfc7eSAndroid Build Coastguard Workernecessary to check if 'memory.swap' is enabled. That is unless the 2302*49cdfc7eSAndroid Build Coastguard Workertest will never reach the point where 'memory.swap.*' are used without 2303*49cdfc7eSAndroid Build Coastguard Workerswap support. 2304*49cdfc7eSAndroid Build Coastguard Worker 2305*49cdfc7eSAndroid Build Coastguard WorkerThe parent process waits for the child process to be reaped before 2306*49cdfc7eSAndroid Build Coastguard Workerdestroying the child CGroup. So there is no need to transfer the child 2307*49cdfc7eSAndroid Build Coastguard Workerto drain. However the parent process must be moved otherwise we will 2308*49cdfc7eSAndroid Build Coastguard Workerget 'EBUSY' when trying to remove the child CGroup. 2309*49cdfc7eSAndroid Build Coastguard Worker 2310*49cdfc7eSAndroid Build Coastguard WorkerAnother example of a behavioral difference between versions is shown below. 2311*49cdfc7eSAndroid Build Coastguard Worker 2312*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2313*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2314*49cdfc7eSAndroid Build Coastguard Worker if (TST_CG_VER_IS_V1(tst_cg, "memory")) 2315*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL); 2316*49cdfc7eSAndroid Build Coastguard Worker else 2317*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max"); 2318*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2319*49cdfc7eSAndroid Build Coastguard Worker 2320*49cdfc7eSAndroid Build Coastguard WorkerCGroups V2 introduced a feature where 'memory[.swap].max' could be set to 2321*49cdfc7eSAndroid Build Coastguard Worker"max". This does not appear to work on V1 'limit_in_bytes' however. For most 2322*49cdfc7eSAndroid Build Coastguard Workertests, simply using a large number is sufficient and there is no need to use 2323*49cdfc7eSAndroid Build Coastguard Worker"max". Importantly though, one should be careful to read both the V1 and V2 2324*49cdfc7eSAndroid Build Coastguard Workerkernel docs. Presently the LTP library does not attempt to handle most 2325*49cdfc7eSAndroid Build Coastguard Workerdifferences in semantics. It does the minimal amount of work to make testing on 2326*49cdfc7eSAndroid Build Coastguard Workerboth V1 and V2 feasible. 2327*49cdfc7eSAndroid Build Coastguard Worker 2328*49cdfc7eSAndroid Build Coastguard Worker1.38 Require minimum numbers of CPU for a testcase 2329*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2330*49cdfc7eSAndroid Build Coastguard Worker 2331*49cdfc7eSAndroid Build Coastguard WorkerSome tests require more than specific number of CPU. It can be defined with 2332*49cdfc7eSAndroid Build Coastguard Worker`.min_cpus = N`. 2333*49cdfc7eSAndroid Build Coastguard Worker 2334*49cdfc7eSAndroid Build Coastguard Worker1.39 Require minimum memory or swap size for a testcase 2335*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2336*49cdfc7eSAndroid Build Coastguard Worker 2337*49cdfc7eSAndroid Build Coastguard WorkerSome tests require at least size(MB) of free RAM or Swap. 2338*49cdfc7eSAndroid Build Coastguard Worker 2339*49cdfc7eSAndroid Build Coastguard WorkerTo make sure that test will run only on systems with more than minimal 2340*49cdfc7eSAndroid Build Coastguard Workerrequired amount of RAM set `.min_mem_avail = N`. 2341*49cdfc7eSAndroid Build Coastguard Worker 2342*49cdfc7eSAndroid Build Coastguard WorkerSimilarily for tests that require certain amount of free Swap use 2343*49cdfc7eSAndroid Build Coastguard Worker`.min_swap_avail = N`. 2344*49cdfc7eSAndroid Build Coastguard Worker 2345*49cdfc7eSAndroid Build Coastguard Worker1.40 Test tags 2346*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~ 2347*49cdfc7eSAndroid Build Coastguard Worker 2348*49cdfc7eSAndroid Build Coastguard WorkerTest tags are name-value pairs that can hold any test metadata. 2349*49cdfc7eSAndroid Build Coastguard Worker 2350*49cdfc7eSAndroid Build Coastguard WorkerWe have additional support for CVE entries, git commit in mainline kernel, 2351*49cdfc7eSAndroid Build Coastguard Workerstable kernel or glibc git repository. If a test is a regression test it 2352*49cdfc7eSAndroid Build Coastguard Workershould include these tags. They are printed when test fails and exported 2353*49cdfc7eSAndroid Build Coastguard Workerinto documentation. 2354*49cdfc7eSAndroid Build Coastguard Worker 2355*49cdfc7eSAndroid Build Coastguard WorkerCVE, mainline and stable kernel git commits in a regression test for a kernel bug: 2356*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2357*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2358*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2359*49cdfc7eSAndroid Build Coastguard Worker ... 2360*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) { 2361*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "9392a27d88b9"}, 2362*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "ff002b30181d"}, 2363*49cdfc7eSAndroid Build Coastguard Worker {"known-fail", "ustat() is known to fail with EINVAL on Btrfs"}, 2364*49cdfc7eSAndroid Build Coastguard Worker {"linux-stable-git", "c4a23c852e80"}, 2365*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2020-29373"}, 2366*49cdfc7eSAndroid Build Coastguard Worker {} 2367*49cdfc7eSAndroid Build Coastguard Worker } 2368*49cdfc7eSAndroid Build Coastguard Worker}; 2369*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2370*49cdfc7eSAndroid Build Coastguard Worker 2371*49cdfc7eSAndroid Build Coastguard WorkerNOTE: We don't track all backports to stable kernel but just those which are 2372*49cdfc7eSAndroid Build Coastguard Worker stable branch specific (unique), i.e. no commit in mainline. Example of 2373*49cdfc7eSAndroid Build Coastguard Worker commits: c4a23c852e80, cac68d12c531. 2374*49cdfc7eSAndroid Build Coastguard Worker 2375*49cdfc7eSAndroid Build Coastguard WorkerGlibc and musl git commits in a regression test for glibc and musl bugs: 2376*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2377*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2378*49cdfc7eSAndroid Build Coastguard Workerstruct tst_test test = { 2379*49cdfc7eSAndroid Build Coastguard Worker ... 2380*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) { 2381*49cdfc7eSAndroid Build Coastguard Worker {"glibc-git", "574500a108be"}, 2382*49cdfc7eSAndroid Build Coastguard Worker {"musl-git", "fa4a8abd06a4"}, 2383*49cdfc7eSAndroid Build Coastguard Worker {} 2384*49cdfc7eSAndroid Build Coastguard Worker } 2385*49cdfc7eSAndroid Build Coastguard Worker}; 2386*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2387*49cdfc7eSAndroid Build Coastguard Worker 2388*49cdfc7eSAndroid Build Coastguard Worker1.41 Testing on the specific architecture 2389*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2390*49cdfc7eSAndroid Build Coastguard WorkerTestcases for specific arch should be limited on that only being supported 2391*49cdfc7eSAndroid Build Coastguard Workerplatform to run, we now involve a '.supported_archs' to achieve this feature 2392*49cdfc7eSAndroid Build Coastguard Workerin LTP library. All you need to run a test on the expected arch is to set 2393*49cdfc7eSAndroid Build Coastguard Workerthe '.supported_archs' array in the 'struct tst_test' to choose the required 2394*49cdfc7eSAndroid Build Coastguard Workerarch list. e.g. 2395*49cdfc7eSAndroid Build Coastguard Worker 2396*49cdfc7eSAndroid Build Coastguard Worker .supported_archs = (const char *const []){"x86_64", "ppc64", NULL} 2397*49cdfc7eSAndroid Build Coastguard Worker 2398*49cdfc7eSAndroid Build Coastguard WorkerThis helps move the TCONF info from code to tst_test metadata as well. 2399*49cdfc7eSAndroid Build Coastguard Worker 2400*49cdfc7eSAndroid Build Coastguard WorkerAnd, we also export a struct tst_arch to save the system architecture for 2401*49cdfc7eSAndroid Build Coastguard Workerusing in the whole test cases. 2402*49cdfc7eSAndroid Build Coastguard Worker 2403*49cdfc7eSAndroid Build Coastguard Worker extern const struct tst_arch { 2404*49cdfc7eSAndroid Build Coastguard Worker char name[16]; 2405*49cdfc7eSAndroid Build Coastguard Worker enum tst_arch_type type; 2406*49cdfc7eSAndroid Build Coastguard Worker } tst_arch; 2407*49cdfc7eSAndroid Build Coastguard Worker 2408*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2409*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2410*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 2411*49cdfc7eSAndroid Build Coastguard Worker 2412*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 2413*49cdfc7eSAndroid Build Coastguard Worker ... 2414*49cdfc7eSAndroid Build Coastguard Worker .setup = setup, 2415*49cdfc7eSAndroid Build Coastguard Worker .supported_archs = (const char *const []) { 2416*49cdfc7eSAndroid Build Coastguard Worker "x86_64", 2417*49cdfc7eSAndroid Build Coastguard Worker "ppc64", 2418*49cdfc7eSAndroid Build Coastguard Worker "s390x", 2419*49cdfc7eSAndroid Build Coastguard Worker NULL 2420*49cdfc7eSAndroid Build Coastguard Worker }, 2421*49cdfc7eSAndroid Build Coastguard Worker}; 2422*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2423*49cdfc7eSAndroid Build Coastguard Worker 2424*49cdfc7eSAndroid Build Coastguard Worker1.42 Skipping test based on system state 2425*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2426*49cdfc7eSAndroid Build Coastguard WorkerTest can be skipped on various conditions: on enabled SecureBoot 2427*49cdfc7eSAndroid Build Coastguard Worker('.skip_in_secureboot = 1'), lockdown ('.skip_in_lockdown = 1') or in 32-bit 2428*49cdfc7eSAndroid Build Coastguard Workercompat mode ('.skip_in_compat = 1'). 2429*49cdfc7eSAndroid Build Coastguard Worker 2430*49cdfc7eSAndroid Build Coastguard Worker1.43 Set resource limits 2431*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~ 2432*49cdfc7eSAndroid Build Coastguard Worker 2433*49cdfc7eSAndroid Build Coastguard Worker'.ulimit' allows to set resource limits on particular resource. NOTE: It sets 'rlim_max' 2434*49cdfc7eSAndroid Build Coastguard Workeronly if it's higher than 'rlim_cur'. 2435*49cdfc7eSAndroid Build Coastguard Worker 2436*49cdfc7eSAndroid Build Coastguard Worker[source,c] 2437*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2438*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h" 2439*49cdfc7eSAndroid Build Coastguard Worker 2440*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = { 2441*49cdfc7eSAndroid Build Coastguard Worker ... 2442*49cdfc7eSAndroid Build Coastguard Worker .ulimit = (const struct tst_ulimit_val[]) { 2443*49cdfc7eSAndroid Build Coastguard Worker {RLIMIT_STACK, RLIM_INFINITY}, 2444*49cdfc7eSAndroid Build Coastguard Worker {} 2445*49cdfc7eSAndroid Build Coastguard Worker }, 2446*49cdfc7eSAndroid Build Coastguard Worker}; 2447*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------- 2448*49cdfc7eSAndroid Build Coastguard Worker 2449*49cdfc7eSAndroid Build Coastguard Worker2. Common problems 2450*49cdfc7eSAndroid Build Coastguard Worker------------------ 2451*49cdfc7eSAndroid Build Coastguard Worker 2452*49cdfc7eSAndroid Build Coastguard WorkerThis chapter describes common problems/misuses and less obvious design patters 2453*49cdfc7eSAndroid Build Coastguard Worker(quirks) in UNIX interfaces. Read it carefully :) 2454*49cdfc7eSAndroid Build Coastguard Worker 2455*49cdfc7eSAndroid Build Coastguard Worker2.1 umask() 2456*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~ 2457*49cdfc7eSAndroid Build Coastguard Worker 2458*49cdfc7eSAndroid Build Coastguard WorkerI've been hit by this one several times already... When you create files 2459*49cdfc7eSAndroid Build Coastguard Workerwith 'open()' or 'creat()' etc, the mode specified as the last parameter *is 2460*49cdfc7eSAndroid Build Coastguard Workernot* the mode the file is created with. The mode depends on current 'umask()' 2461*49cdfc7eSAndroid Build Coastguard Workersettings which may clear some of the bits. If your test depends on specific 2462*49cdfc7eSAndroid Build Coastguard Workerfile permissions you need either to change umask to 0 or 'chmod()' the file 2463*49cdfc7eSAndroid Build Coastguard Workerafterwards or use 'SAFE_TOUCH()' that does the 'chmod()' for you. 2464*49cdfc7eSAndroid Build Coastguard Worker 2465*49cdfc7eSAndroid Build Coastguard Worker2.2 access() 2466*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~ 2467*49cdfc7eSAndroid Build Coastguard Worker 2468*49cdfc7eSAndroid Build Coastguard WorkerIf 'access(some_file, W_OK)' is executed by root, it will return success even 2469*49cdfc7eSAndroid Build Coastguard Workerif the file doesn't have write permission bits set (the same holds for R_OK 2470*49cdfc7eSAndroid Build Coastguard Workertoo). For sysfs files you can use 'open()' as a workaround to check file 2471*49cdfc7eSAndroid Build Coastguard Workerread/write permissions. It might not work for other filesystems, for these you 2472*49cdfc7eSAndroid Build Coastguard Workerhave to use 'stat()', 'lstat()' or 'fstat()'. 2473*49cdfc7eSAndroid Build Coastguard Worker 2474*49cdfc7eSAndroid Build Coastguard Worker2.3 umount() EBUSY 2475*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~ 2476*49cdfc7eSAndroid Build Coastguard Worker 2477*49cdfc7eSAndroid Build Coastguard WorkerVarious desktop daemons (gvfsd-trash is known for that) may be stupid enough 2478*49cdfc7eSAndroid Build Coastguard Workerto probe all newly mounted filesystem which results in 'umount(2)' failing 2479*49cdfc7eSAndroid Build Coastguard Workerwith 'EBUSY'; use 'tst_umount()' described in 1.19 that retries in this case 2480*49cdfc7eSAndroid Build Coastguard Workerinstead of plain 'umount(2)'. 2481*49cdfc7eSAndroid Build Coastguard Worker 2482*49cdfc7eSAndroid Build Coastguard Worker2.4 FILE buffers and fork() 2483*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2484*49cdfc7eSAndroid Build Coastguard Worker 2485*49cdfc7eSAndroid Build Coastguard WorkerBe vary that if a process calls 'fork(2)' the child process inherits open 2486*49cdfc7eSAndroid Build Coastguard Workerdescriptors as well as copy of the parent memory so especially if there are 2487*49cdfc7eSAndroid Build Coastguard Workerany open 'FILE' buffers with a data in them they may be written both by the 2488*49cdfc7eSAndroid Build Coastguard Workerparent and children resulting in corrupted/duplicated data in the resulting 2489*49cdfc7eSAndroid Build Coastguard Workerfiles. 2490*49cdfc7eSAndroid Build Coastguard Worker 2491*49cdfc7eSAndroid Build Coastguard WorkerAlso open 'FILE' streams are flushed and closed at 'exit(3)' so if your 2492*49cdfc7eSAndroid Build Coastguard Workerprogram works with 'FILE' streams, does 'fork(2)', and the child may end up 2493*49cdfc7eSAndroid Build Coastguard Workercalling 'exit(3)' you will likely end up with corrupted files. 2494*49cdfc7eSAndroid Build Coastguard Worker 2495*49cdfc7eSAndroid Build Coastguard WorkerThe solution to this problem is either simply call 'fflush(NULL)' that flushes 2496*49cdfc7eSAndroid Build Coastguard Workerall open output 'FILE' streams just before doing 'fork(2)'. You may also use 2497*49cdfc7eSAndroid Build Coastguard Worker'_exit(2)' in child processes which does not flush 'FILE' buffers and also 2498*49cdfc7eSAndroid Build Coastguard Workerskips 'atexit(3)' callbacks. 2499