13ce53722SRobert Mustacchi /*
23ce53722SRobert Mustacchi * This file and its contents are supplied under the terms of the
33ce53722SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
43ce53722SRobert Mustacchi * You may only use this file in accordance with the terms of version
53ce53722SRobert Mustacchi * 1.0 of the CDDL.
63ce53722SRobert Mustacchi *
73ce53722SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this
83ce53722SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at
93ce53722SRobert Mustacchi * http://www.illumos.org/license/CDDL.
103ce53722SRobert Mustacchi */
113ce53722SRobert Mustacchi
123ce53722SRobert Mustacchi /*
133ce53722SRobert Mustacchi * Copyright 2020 Oxide Computer Company
143ce53722SRobert Mustacchi */
153ce53722SRobert Mustacchi
163ce53722SRobert Mustacchi /*
173ce53722SRobert Mustacchi * Describe the purpose of this file.
183ce53722SRobert Mustacchi */
193ce53722SRobert Mustacchi
203ce53722SRobert Mustacchi #include <sys/types.h>
213ce53722SRobert Mustacchi #include <sys/stat.h>
223ce53722SRobert Mustacchi #include <fcntl.h>
233ce53722SRobert Mustacchi #include <stdlib.h>
243ce53722SRobert Mustacchi #include <err.h>
253ce53722SRobert Mustacchi #include <sys/sensors.h>
263ce53722SRobert Mustacchi #include <unistd.h>
273ce53722SRobert Mustacchi #include <errno.h>
283ce53722SRobert Mustacchi #include <string.h>
293ce53722SRobert Mustacchi #include <sys/sysmacros.h>
303ce53722SRobert Mustacchi
313ce53722SRobert Mustacchi static const char *error_sensor = "/dev/sensors/test/test.eio.0";
323ce53722SRobert Mustacchi static int error_exit;
333ce53722SRobert Mustacchi
343ce53722SRobert Mustacchi static void
error_kind(int fd,int exp)353ce53722SRobert Mustacchi error_kind(int fd, int exp)
363ce53722SRobert Mustacchi {
373ce53722SRobert Mustacchi sensor_ioctl_kind_t kind, alt_kind;
383ce53722SRobert Mustacchi
393ce53722SRobert Mustacchi arc4random_buf(&alt_kind, sizeof (alt_kind));
403ce53722SRobert Mustacchi (void) memcpy(&kind, &alt_kind, sizeof (alt_kind));
413ce53722SRobert Mustacchi
42*1045e13aSRobert Mustacchi if (ioctl(fd, SENSOR_IOCTL_KIND, &kind) == 0) {
43*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSOR_IOCTL_KIND succeeded on EIO "
443ce53722SRobert Mustacchi "sensor");
453ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
463ce53722SRobert Mustacchi }
473ce53722SRobert Mustacchi
483ce53722SRobert Mustacchi if (errno != exp) {
49*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSOR_IOCTL_KIND got errno %d, "
503ce53722SRobert Mustacchi "expected %d", errno, exp);
513ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
523ce53722SRobert Mustacchi }
533ce53722SRobert Mustacchi
543ce53722SRobert Mustacchi if (memcmp(&kind, &alt_kind, sizeof (alt_kind)) != 0) {
55*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSOR_IOCTL_KIND modified data on error");
563ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
573ce53722SRobert Mustacchi }
583ce53722SRobert Mustacchi }
593ce53722SRobert Mustacchi
603ce53722SRobert Mustacchi static void
error_temp(int fd,int exp)613ce53722SRobert Mustacchi error_temp(int fd, int exp)
623ce53722SRobert Mustacchi {
63*1045e13aSRobert Mustacchi sensor_ioctl_scalar_t scalar, alt_scalar;
643ce53722SRobert Mustacchi
65*1045e13aSRobert Mustacchi arc4random_buf(&alt_scalar, sizeof (alt_scalar));
66*1045e13aSRobert Mustacchi (void) memcpy(&scalar, &alt_scalar, sizeof (alt_scalar));
673ce53722SRobert Mustacchi
68*1045e13aSRobert Mustacchi if (ioctl(fd, SENSOR_IOCTL_SCALAR, &scalar) == 0) {
69*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR suceeded on "
703ce53722SRobert Mustacchi "EIO sensor");
713ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
723ce53722SRobert Mustacchi }
733ce53722SRobert Mustacchi
743ce53722SRobert Mustacchi if (errno != exp) {
75*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR got errno %d, "
763ce53722SRobert Mustacchi "expected %d", errno, EIO);
773ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
783ce53722SRobert Mustacchi }
793ce53722SRobert Mustacchi
80*1045e13aSRobert Mustacchi if (memcmp(&scalar, &alt_scalar, sizeof (alt_scalar)) != 0) {
81*1045e13aSRobert Mustacchi warnx("TEST FAILED: SENSIOR_IOCTL_SCALAR modified "
823ce53722SRobert Mustacchi "data on error");
833ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
843ce53722SRobert Mustacchi }
853ce53722SRobert Mustacchi }
863ce53722SRobert Mustacchi
873ce53722SRobert Mustacchi int
main(void)883ce53722SRobert Mustacchi main(void)
893ce53722SRobert Mustacchi {
903ce53722SRobert Mustacchi int i;
913ce53722SRobert Mustacchi int flags[] = { O_RDWR, O_WRONLY, O_RDONLY | O_NDELAY,
923ce53722SRobert Mustacchi O_RDONLY | O_NONBLOCK };
933ce53722SRobert Mustacchi int fd = open(error_sensor, O_RDONLY);
943ce53722SRobert Mustacchi if (fd < 0) {
953ce53722SRobert Mustacchi err(EXIT_FAILURE, "TEST FAILED: failed to open %s",
963ce53722SRobert Mustacchi error_sensor);
973ce53722SRobert Mustacchi }
983ce53722SRobert Mustacchi
993ce53722SRobert Mustacchi error_kind(fd, EIO);
1003ce53722SRobert Mustacchi error_temp(fd, EIO);
1013ce53722SRobert Mustacchi (void) close(fd);
1023ce53722SRobert Mustacchi
1033ce53722SRobert Mustacchi /*
1043ce53722SRobert Mustacchi * Check for illegal open combinations.
1053ce53722SRobert Mustacchi */
1063ce53722SRobert Mustacchi for (i = 0; i < ARRAY_SIZE(flags); i++) {
1073ce53722SRobert Mustacchi fd = open(error_sensor, flags[i]);
1083ce53722SRobert Mustacchi if (fd >= 0) {
1093ce53722SRobert Mustacchi printf("i is %d\n", i);
1103ce53722SRobert Mustacchi warnx("TEST FAILED: opened a sensor with flags 0x%x, "
1113ce53722SRobert Mustacchi "but expected failure", flags[i]);
1123ce53722SRobert Mustacchi error_exit = EXIT_FAILURE;
1133ce53722SRobert Mustacchi }
1143ce53722SRobert Mustacchi }
1153ce53722SRobert Mustacchi
1163ce53722SRobert Mustacchi return (error_exit);
1173ce53722SRobert Mustacchi }
118