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