1 /*
2  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
19  *
20  * Test checks block device kernel API.
21  */
22 
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #include "test.h"
30 #include "safe_macros.h"
31 #include "old_module.h"
32 
33 char *TCID = "block_dev";
34 int TST_TOTAL = 9;
35 
36 static const char module_name[]	= "ltp_block_dev.ko";
37 static const char dev_result[]	= "/sys/devices/ltp_block_dev/result";
38 static const char dev_tcase[]	= "/sys/devices/ltp_block_dev/tcase";
39 static int module_loaded;
40 
41 static int run_all_testcases;
42 static const option_t options[] = {
43 	{"a", &run_all_testcases, NULL},
44 	{NULL, NULL, NULL}
45 };
46 
cleanup(void)47 static void cleanup(void)
48 {
49 	if (module_loaded)
50 		tst_module_unload(NULL, module_name);
51 }
52 
help(void)53 static void help(void)
54 {
55 	printf("  -a      Run all test-cases (can crash the kernel)\n");
56 }
57 
setup(int argc,char * argv[])58 void setup(int argc, char *argv[])
59 {
60 	tst_parse_opts(argc, argv, options, help);
61 
62 	tst_require_root();
63 
64 	if (tst_kvercmp(2, 6, 0) < 0) {
65 		tst_brkm(TCONF, NULL,
66 			"Test must be run with kernel 2.6 or newer");
67 	}
68 
69 	tst_sig(FORK, DEF_HANDLER, cleanup);
70 }
71 
test_run(void)72 static void test_run(void)
73 {
74 	int off = 0;
75 	/*
76 	 * test-cases #8 and #9 can crash the kernel.
77 	 * We have to wait for kernel fix where register_blkdev() &
78 	 * unregister_blkdev() checks the input device name parameter
79 	 * against NULL pointer.
80 	 */
81 	if (!run_all_testcases)
82 		off = 2;
83 
84 	tst_module_load(cleanup, module_name, NULL);
85 	module_loaded = 1;
86 
87 	int i, pass = 0;
88 	for (i = 0; i < TST_TOTAL - off; ++i) {
89 		SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i + 1);
90 		SAFE_FILE_SCANF(cleanup, dev_result, "%d", &pass);
91 		tst_resm((pass) ? TPASS : TFAIL, "Test-case '%d'", i + 1);
92 	}
93 }
94 
main(int argc,char * argv[])95 int main(int argc, char *argv[])
96 {
97 	setup(argc, argv);
98 
99 	test_run();
100 
101 	cleanup();
102 
103 	tst_exit();
104 }
105