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 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "test.h"
25 #include "old_module.h"
26 #include "safe_macros.h"
27 
28 #include "ltp_acpi.h"
29 
30 char *TCID = "ltp_acpi";
31 int TST_TOTAL = ACPI_TC_NUM;
32 
33 static const char dev_result[]	= "/sys/devices/" ACPI_TEST_NAME "/result";
34 static const char dev_path[]	= "/sys/devices/" ACPI_TEST_NAME "/path";
35 static const char dev_str[]	= "/sys/devices/" ACPI_TEST_NAME "/str";
36 static const char dev_tcase[]	= "/sys/devices/" ACPI_TEST_NAME "/tcase";
37 static const char dev_acpi_disabled[] = "/sys/devices/" ACPI_TEST_NAME "/acpi_disabled";
38 static const char module_name[]	= "ltp_acpi_cmds.ko";
39 static int module_loaded;
40 
cleanup(void)41 static void cleanup(void)
42 {
43 	if (module_loaded)
44 		tst_module_unload(NULL, module_name);
45 }
46 
read_sysfs_file(const char * name,char * buf,int size)47 static int read_sysfs_file(const char *name, char *buf, int size)
48 {
49 	FILE *f = SAFE_FOPEN(cleanup, name, "r");
50 	char *res = fgets(buf, size, f);
51 	SAFE_FCLOSE(cleanup, f);
52 	return (res) ? 0 : 1;
53 }
54 
tc_acpi_str(void)55 static int tc_acpi_str(void)
56 {
57 	int res, ret = 0;
58 	char descr[4096], sysfs_path[4096];
59 
60 	int not_kver_3_7 = tst_kvercmp(3, 7, 0) < 0;
61 
62 	while (1) {
63 
64 		SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", ACPI_TRAVERSE);
65 		SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
66 		if (res)
67 			return TFAIL;
68 		/*
69 		 * if device has _STR object, we should get
70 		 * a valid string from 'str' sysfs file and then can
71 		 * find it in sysfs.
72 		 */
73 		if (read_sysfs_file(dev_str, descr, 4096)) {
74 			/* None of left devices has _STR */
75 			break;
76 		}
77 		tst_resm(TINFO, "read description %s", descr);
78 
79 		/* device's sysfs path */
80 		strcpy(sysfs_path, "/sys");
81 		if (read_sysfs_file(dev_path, sysfs_path + 4, 4092)) {
82 			/*
83 			 * Device doesn't have sysfs entry
84 			 * continue, because others might have it
85 			 */
86 			continue;
87 		}
88 
89 		/*
90 		 * Find device description in sysfs.
91 		 *
92 		 * New sysfs interface to export device description
93 		 * implemented since Linux 3.7
94 		 */
95 		if (not_kver_3_7) {
96 			tst_resm(TINFO, "sysfs _STR check required Linux 3.7+");
97 			ret = TCONF;
98 			/* continue, we can still traverse ACPI devices */
99 			continue;
100 		}
101 
102 		strcat(sysfs_path, "/description");
103 		if (access(sysfs_path, R_OK)) {
104 			tst_resm(TINFO, "can't find description file '%s'",
105 				sysfs_path);
106 			return TFAIL;
107 		}
108 		tst_resm(TINFO, "found description file '%s'", sysfs_path);
109 
110 		char sysfs_descr[4096];
111 		if (read_sysfs_file(sysfs_path, sysfs_descr, 4096))
112 			return TFAIL;
113 
114 		/*
115 		 * Compare sysfs file and description from test driver
116 		 */
117 		int res = strncmp(descr, sysfs_descr, strlen(descr));
118 
119 		ret |= res ? TFAIL : TPASS;
120 	}
121 
122 	return ret;
123 }
124 
test_run(void)125 static void test_run(void)
126 {
127 	int i, res;
128 
129 	for (i = 0; i < TST_TOTAL; ++i) {
130 
131 		if (i == ACPI_TRAVERSE) {
132 			res = tc_acpi_str();
133 		} else {
134 			SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i);
135 			SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
136 			res = res ? TFAIL : TPASS;
137 		}
138 
139 		tst_resm(res, "Test-case '%d'", i);
140 	}
141 }
142 
main(int argc,char * argv[])143 int main(int argc, char *argv[])
144 {
145 	int acpi_disabled;
146 
147 	tst_parse_opts(argc, argv, NULL, NULL);
148 
149 	tst_require_root();
150 
151 	if (tst_kvercmp(2, 6, 0) < 0) {
152 		tst_brkm(TCONF, NULL,
153 			"Test must be run with kernel 2.6 or newer");
154 	}
155 
156 	tst_sig(FORK, DEF_HANDLER, cleanup);
157 
158 	tst_module_load(NULL, module_name, NULL);
159 	module_loaded = 1;
160 
161 	SAFE_FILE_SCANF(cleanup, dev_acpi_disabled, "%d", &acpi_disabled);
162 	if (acpi_disabled)
163 		tst_brkm(TCONF, cleanup, "ACPI is disabled on the system");
164 
165 	test_run();
166 
167 	cleanup();
168 
169 	tst_exit();
170 }
171