xref: /illumos-gate/usr/src/test/os-tests/tests/ksensor/ksensor_err.c (revision 1045e13a248d94941f864998aa859970ae3a4154)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Oxide Computer Company
14  */
15 
16 /*
17  * Describe the purpose of this file.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <err.h>
25 #include <sys/sensors.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/sysmacros.h>
30 
31 static const char *error_sensor = "/dev/sensors/test/test.eio.0";
32 static int error_exit;
33 
34 static void
error_kind(int fd,int exp)35 error_kind(int fd, int exp)
36 {
37 	sensor_ioctl_kind_t kind, alt_kind;
38 
39 	arc4random_buf(&alt_kind, sizeof (alt_kind));
40 	(void) memcpy(&kind, &alt_kind, sizeof (alt_kind));
41 
42 	if (ioctl(fd, SENSOR_IOCTL_KIND, &kind) == 0) {
43 		warnx("TEST FAILED: SENSOR_IOCTL_KIND succeeded on EIO "
44 		    "sensor");
45 		error_exit = EXIT_FAILURE;
46 	}
47 
48 	if (errno != exp) {
49 		warnx("TEST FAILED: SENSOR_IOCTL_KIND got errno %d, "
50 		    "expected %d", errno, exp);
51 		error_exit = EXIT_FAILURE;
52 	}
53 
54 	if (memcmp(&kind, &alt_kind, sizeof (alt_kind)) != 0) {
55 		warnx("TEST FAILED: SENSOR_IOCTL_KIND modified data on error");
56 		error_exit = EXIT_FAILURE;
57 	}
58 }
59 
60 static void
error_temp(int fd,int exp)61 error_temp(int fd, int exp)
62 {
63 	sensor_ioctl_scalar_t scalar, alt_scalar;
64 
65 	arc4random_buf(&alt_scalar, sizeof (alt_scalar));
66 	(void) memcpy(&scalar, &alt_scalar, sizeof (alt_scalar));
67 
68 	if (ioctl(fd, SENSOR_IOCTL_SCALAR, &scalar) == 0) {
69 		warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR suceeded on "
70 		    "EIO sensor");
71 		error_exit = EXIT_FAILURE;
72 	}
73 
74 	if (errno != exp) {
75 		warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR got errno %d, "
76 		    "expected %d", errno, EIO);
77 		error_exit = EXIT_FAILURE;
78 	}
79 
80 	if (memcmp(&scalar, &alt_scalar, sizeof (alt_scalar)) != 0) {
81 		warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR modified "
82 		    "data on error");
83 		error_exit = EXIT_FAILURE;
84 	}
85 }
86 
87 int
main(void)88 main(void)
89 {
90 	int i;
91 	int flags[] = { O_RDWR, O_WRONLY, O_RDONLY | O_NDELAY,
92 		O_RDONLY | O_NONBLOCK };
93 	int fd = open(error_sensor, O_RDONLY);
94 	if (fd < 0) {
95 		err(EXIT_FAILURE, "TEST FAILED: failed to open %s",
96 		    error_sensor);
97 	}
98 
99 	error_kind(fd, EIO);
100 	error_temp(fd, EIO);
101 	(void) close(fd);
102 
103 	/*
104 	 * Check for illegal open combinations.
105 	 */
106 	for (i = 0; i < ARRAY_SIZE(flags); i++) {
107 		fd = open(error_sensor, flags[i]);
108 		if (fd >= 0) {
109 			printf("i is %d\n", i);
110 			warnx("TEST FAILED: opened a sensor with flags 0x%x, "
111 			    "but expected failure", flags[i]);
112 			error_exit = EXIT_FAILURE;
113 		}
114 	}
115 
116 	return (error_exit);
117 }
118