diff --git a/tests/sys/Makefile b/tests/sys/Makefile index 2ba60f41b76c..2faf04054d7f 100644 --- a/tests/sys/Makefile +++ b/tests/sys/Makefile @@ -1,53 +1,54 @@ # $FreeBSD$ .include TESTSDIR= ${TESTSBASE}/sys TESTS_SUBDIRS+= acl TESTS_SUBDIRS+= aio TESTS_SUBDIRS+= ${_audit} TESTS_SUBDIRS+= auditpipe TESTS_SUBDIRS+= capsicum TESTS_SUBDIRS+= ${_cddl} TESTS_SUBDIRS+= devrandom TESTS_SUBDIRS+= fifo TESTS_SUBDIRS+= file TESTS_SUBDIRS+= fs TESTS_SUBDIRS+= geom TESTS_SUBDIRS+= kern TESTS_SUBDIRS+= kqueue TESTS_SUBDIRS+= mac TESTS_SUBDIRS+= mqueue TESTS_SUBDIRS+= net TESTS_SUBDIRS+= ${_netgraph} TESTS_SUBDIRS+= netinet TESTS_SUBDIRS+= netinet6 TESTS_SUBDIRS+= netipsec TESTS_SUBDIRS+= netmap TESTS_SUBDIRS+= netpfil TESTS_SUBDIRS+= opencrypto TESTS_SUBDIRS+= posixshm +TESTS_SUBDIRS+= ses TESTS_SUBDIRS+= sys TESTS_SUBDIRS+= vfs TESTS_SUBDIRS+= vm TESTS_SUBDIRS+= vmm .if ${MK_AUDIT} != "no" _audit= audit .endif .if ${MK_CDDL} != "no" _cddl= cddl .endif .if ${MK_NETGRAPH} != "no" _netgraph= netgraph .endif # Items not integrated into kyua runs by default SUBDIR+= pjdfstest SUBDIR+= common .include diff --git a/tests/sys/ses/Makefile b/tests/sys/ses/Makefile new file mode 100644 index 000000000000..8a0c54a2dd52 --- /dev/null +++ b/tests/sys/ses/Makefile @@ -0,0 +1,11 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/sys/ses + +ATF_TESTS_C+= destructive +ATF_TESTS_C+= nondestructive + +# Some tests cases alter enclosure state, so they can't run concurrently. +TEST_METADATA.destructive+= is_exclusive=true + +.include diff --git a/tests/sys/ses/common.h b/tests/sys/ses/common.h new file mode 100644 index 000000000000..c4a74bd8505f --- /dev/null +++ b/tests/sys/ses/common.h @@ -0,0 +1,59 @@ +/*- + * Copyright (C) 2021 Axcient, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +typedef bool(*ses_cb)(const char *devname, int fd); + +// Run a test function on every available ses device +static void +for_each_ses_dev(ses_cb cb, int oflags) +{ + glob_t g; + int r; + unsigned i; + bool tested = false; + + g.gl_pathc = 0; + g.gl_pathv = NULL; + g.gl_offs = 0; + + r = glob("/dev/ses*", GLOB_NOSORT, NULL, &g); + ATF_REQUIRE_EQ(r, 0); + + for(i = 0; i < g.gl_pathc; i++) { + int fd; + + fd = open(g.gl_pathv[i], oflags); + ATF_REQUIRE(fd >= 0); + tested |= cb(g.gl_pathv[i], fd); + close(fd); + } + + if (!tested) + atf_tc_skip("No supported devices found"); + + globfree(&g); +} diff --git a/tests/sys/ses/destructive.c b/tests/sys/ses/destructive.c new file mode 100644 index 000000000000..307b272c447f --- /dev/null +++ b/tests/sys/ses/destructive.c @@ -0,0 +1,289 @@ +/*- + * Copyright (C) 2021 Axcient, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* Tests that alter an enclosure's state */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "common.h" + +// Run a test function on just one ses device +static void +for_one_ses_dev(ses_cb cb) +{ + glob_t g; + int fd, r; + + g.gl_pathc = 0; + g.gl_pathv = NULL; + g.gl_offs = 0; + + r = glob("/dev/ses*", GLOB_NOSORT, NULL, &g); + ATF_REQUIRE_EQ(r, 0); + if (g.gl_pathc == 0) + atf_tc_skip("No ses devices found"); + + fd = open(g.gl_pathv[0], O_RDWR); + ATF_REQUIRE(fd >= 0); + cb(g.gl_pathv[0], fd); + close(fd); + + globfree(&g); +} + +static bool do_setelmstat(const char *devname __unused, int fd) { + encioc_element_t *map; + unsigned elm_idx; + unsigned nobj; + int r; + elm_type_t last_elm_type = -1; + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + map = calloc(nobj, sizeof(encioc_element_t)); + ATF_REQUIRE(map != NULL); + r = ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) map); + + /* Set the IDENT bit for every disk slot */ + for (elm_idx = 0; elm_idx < nobj; elm_idx++) { + encioc_elm_status_t elmstat; + struct ses_ctrl_dev_slot *cslot; + struct ses_status_dev_slot *sslot; + + if (last_elm_type != map[elm_idx].elm_type) { + /* skip overall elements */ + last_elm_type = map[elm_idx].elm_type; + continue; + } + elmstat.elm_idx = elm_idx; + if (map[elm_idx].elm_type == ELMTYP_DEVICE || + map[elm_idx].elm_type == ELMTYP_ARRAY_DEV) + { + r = ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t)&elmstat); + ATF_REQUIRE_EQ(r, 0); + + cslot = (struct ses_ctrl_dev_slot*)&elmstat.cstat[0]; + sslot = (struct ses_status_dev_slot*)&elmstat.cstat[0]; + + ses_ctrl_common_set_select(&cslot->common, 1); + ses_ctrl_dev_slot_set_rqst_ident(cslot, 1); + r = ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t)&elmstat); + ATF_REQUIRE_EQ(r, 0); + } + } + + /* Check the IDENT bit for every disk slot */ + last_elm_type = -1; + for (elm_idx = 0; elm_idx < nobj; elm_idx++) { + encioc_elm_status_t elmstat; + struct ses_status_dev_slot *sslot; + + if (last_elm_type != map[elm_idx].elm_type) { + /* skip overall elements */ + last_elm_type = map[elm_idx].elm_type; + continue; + } + elmstat.elm_idx = elm_idx; + if (map[elm_idx].elm_type == ELMTYP_DEVICE || + map[elm_idx].elm_type == ELMTYP_ARRAY_DEV) + { + int i; + + r = ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t)&elmstat); + ATF_REQUIRE_EQ(r, 0); + + sslot = (struct ses_status_dev_slot*)&elmstat.cstat[0]; + + for (i = 0; i < 10; i++) { + r = ioctl(fd, ENCIOC_GETELMSTAT, + (caddr_t)&elmstat); + ATF_REQUIRE_EQ(r, 0); + if (0 == ses_status_dev_slot_get_ident(sslot)) { + /* Needs more time to take effect */ + usleep(100000); + } + } + ATF_CHECK(ses_status_dev_slot_get_ident(sslot) != 0); + + } + } + + free(map); + return (true); +} + +/* + * sg_ses doesn't provide "dump and restore" functionality. The closest is to + * dump status page 2, then manually edit the file to set every individual + * element select bit, then load the entire file. But that is much too hard. + * Instead, we'll just clear every ident bit. + */ +static bool +do_setelmstat_cleanup(const char *devname __unused, int fd __unused) { + encioc_element_t *map; + unsigned elm_idx; + unsigned nobj; + int r; + elm_type_t last_elm_type = -1; + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + map = calloc(nobj, sizeof(encioc_element_t)); + ATF_REQUIRE(map != NULL); + r = ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) map); + + /* Clear the IDENT bit for every disk slot */ + for (elm_idx = 0; elm_idx < nobj; elm_idx++) { + encioc_elm_status_t elmstat; + struct ses_ctrl_dev_slot *cslot; + + if (last_elm_type != map[elm_idx].elm_type) { + /* skip overall elements */ + last_elm_type = map[elm_idx].elm_type; + continue; + } + elmstat.elm_idx = elm_idx; + if (map[elm_idx].elm_type == ELMTYP_DEVICE || + map[elm_idx].elm_type == ELMTYP_ARRAY_DEV) + { + cslot = (struct ses_ctrl_dev_slot*)&elmstat.cstat[0]; + + ses_ctrl_common_set_select(&cslot->common, 1); + ses_ctrl_dev_slot_set_rqst_ident(cslot, 0); + r = ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t)&elmstat); + } + } + + return(true); +} + + +ATF_TC_WITH_CLEANUP(setelmstat); +ATF_TC_HEAD(setelmstat, tc) +{ + atf_tc_set_md_var(tc, "descr", "Exercise ENCIOC_SETELMSTAT"); + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(setelmstat, tc) +{ + for_one_ses_dev(do_setelmstat); +} +ATF_TC_CLEANUP(setelmstat, tc) +{ + for_one_ses_dev(do_setelmstat_cleanup); +} + + +static bool do_setencstat(const char *devname __unused, int fd) { + unsigned char encstat; + int r, i; + bool worked = false; + + /* + * SES provides no way to read the current setting of the enclosure + * control page common status bits. So we'll blindly set CRIT. + */ + encstat = 1 << SES_CTRL_PAGE_CRIT_SHIFT; + r = ioctl(fd, ENCIOC_SETENCSTAT, (caddr_t) &encstat); + ATF_REQUIRE_EQ(r, 0); + + /* Check that the status has changed */ + for (i = 0; i < 10; i++) { + r = ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &encstat); + ATF_REQUIRE_EQ(r, 0); + if (encstat & SES_CTRL_PAGE_CRIT_MASK) { + worked = true; + break; + } + usleep(100000); + } + if (!worked) { + /* Some enclosures don't support setting the enclosure status */ + return (false); + } else + return (true); +} + +static bool do_setencstat_cleanup(const char *devname __unused, int fd) { + unsigned char encstat; + + /* + * SES provides no way to read the current setting of the enclosure + * control page common status bits. So we don't know what they were + * set to before the test. We'll blindly clear all bits. + */ + encstat = 0; + ioctl(fd, ENCIOC_SETENCSTAT, (caddr_t) &encstat); + return (true); +} + +ATF_TC_WITH_CLEANUP(setencstat); +ATF_TC_HEAD(setencstat, tc) +{ + atf_tc_set_md_var(tc, "descr", "Exercise ENCIOC_SETENCSTAT"); + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(setencstat, tc) +{ + for_each_ses_dev(do_setencstat, O_RDWR); +} +ATF_TC_CLEANUP(setencstat, tc) +{ + for_each_ses_dev(do_setencstat_cleanup, O_RDWR); +} + +ATF_TP_ADD_TCS(tp) +{ + + /* + * Untested ioctls: + * + * * ENCIOC_INIT because SES doesn't need it and I don't have any + * SAF-TE devices. + * + * * ENCIOC_SETSTRING because it's seriously unsafe! It's normally + * used for stuff like firmware updates + */ + ATF_TP_ADD_TC(tp, setelmstat); + ATF_TP_ADD_TC(tp, setencstat); + // TODO ENCIOC_SETELMSTAT + + return (atf_no_error()); +} diff --git a/tests/sys/ses/nondestructive.c b/tests/sys/ses/nondestructive.c new file mode 100644 index 000000000000..e52a38a97862 --- /dev/null +++ b/tests/sys/ses/nondestructive.c @@ -0,0 +1,636 @@ +/*- + * Copyright (C) 2021 Axcient, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* Basic smoke test of the ioctl interface */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "common.h" + +static bool do_getelmdesc(const char *devname, int fd) { + regex_t re; + FILE *pipe; + char cmd[256]; + char line[256]; + char *actual; + unsigned nobj; + unsigned elm_idx = 0; + int r; + + actual = calloc(UINT16_MAX, sizeof(char)); + ATF_REQUIRE(actual != NULL); + r = regcomp(&re, "(Overall|Element [0-9]+) descriptor: ", REG_EXTENDED); + ATF_REQUIRE_EQ(r, 0); + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + snprintf(cmd, sizeof(cmd), "sg_ses -p7 %s", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + while(NULL != fgets(line, sizeof(line), pipe)) { + regmatch_t matches[1]; + encioc_elm_desc_t e_desc; + char *expected; + size_t elen; + + if (regexec(&re, line, 1, matches, 0) == REG_NOMATCH) { + continue; + } + + expected = &line[matches[0].rm_eo]; + /* Remove trailing newline */ + elen = strnlen(expected, sizeof(line) - matches[0].rm_eo); + expected[elen - 1] = '\0'; + /* + * Zero the result string. XXX we wouldn't have to do this if + * the kernel would nul-terminate the result. + */ + memset(actual, 0, UINT16_MAX); + e_desc.elm_idx = elm_idx; + e_desc.elm_desc_len = UINT16_MAX; + e_desc.elm_desc_str = actual; + r = ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc); + ATF_REQUIRE_EQ(r, 0); + if (0 == strcmp("", expected)) { + /* sg_ses replaces "" with "" */ + ATF_CHECK_STREQ("", actual); + } else + ATF_CHECK_STREQ(expected, actual); + elm_idx++; + } + + r = pclose(pipe); + regfree(&re); + free(actual); + if (r != 0) { + /* Probably an SGPIO device */ + + return (false); + } else { + ATF_CHECK_EQ_MSG(nobj, elm_idx, + "Did not find the expected number of element " + "descriptors in sg_ses's output"); + return (true); + } +} + +ATF_TC(getelmdesc); +ATF_TC_HEAD(getelmdesc, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETELMDESC's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getelmdesc, tc) +{ + for_each_ses_dev(do_getelmdesc, O_RDONLY); +} + +static bool do_getelmdevnames(const char *devname __unused, int fd) { + encioc_element_t *map; + unsigned nobj; + const size_t namesize = 128; + int r; + char *namebuf; + unsigned elm_idx; + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + namebuf = calloc(namesize, sizeof(char)); + ATF_REQUIRE(namebuf != NULL); + map = calloc(nobj, sizeof(encioc_element_t)); + ATF_REQUIRE(map != NULL); + r = ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) map); + ATF_REQUIRE_EQ(r, 0); + + for (elm_idx = 0; elm_idx < nobj; elm_idx++) { + /* + * devnames should be present if: + * * The element is of type Device Slot or Array Device Slot + * * It isn't an Overall Element + * * The element's status is not "Not Installed" + */ + encioc_elm_status_t e_status; + encioc_elm_devnames_t elmdn; + + memset(&e_status, 0, sizeof(e_status)); + e_status.elm_idx = elm_idx; + r = ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t)&e_status); + ATF_REQUIRE_EQ(r, 0); + + memset(&elmdn, 0, sizeof(elmdn)); + elmdn.elm_idx = elm_idx; + elmdn.elm_names_size = namesize; + elmdn.elm_devnames = namebuf; + namebuf[0] = '\0'; + r = ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &elmdn); + if (e_status.cstat[0] != SES_OBJSTAT_UNSUPPORTED && + e_status.cstat[0] != SES_OBJSTAT_NOTINSTALLED && + (map[elm_idx].elm_type == ELMTYP_DEVICE || + map[elm_idx].elm_type == ELMTYP_ARRAY_DEV)) + { + ATF_CHECK_EQ_MSG(r, 0, "devnames not found. This could be due to a buggy ses driver, buggy ses controller, dead HDD, or an ATA HDD in a SAS slot"); + } else { + ATF_CHECK(r != 0); + } + + if (r == 0) { + size_t z = 0; + int da = 0, ada = 0, pass = 0, nvd = 0; + int nvme = 0, unknown = 0; + + while(elmdn.elm_devnames[z] != '\0') { + size_t e; + char *s; + + if (elmdn.elm_devnames[z] == ',') + z++; /* Skip the comma */ + s = elmdn.elm_devnames + z; + e = strcspn(s, "0123456789"); + if (0 == strncmp("da", s, e)) + da++; + else if (0 == strncmp("ada", s, e)) + ada++; + else if (0 == strncmp("pass", s, e)) + pass++; + else if (0 == strncmp("nvd", s, e)) + nvd++; + else if (0 == strncmp("nvme", s, e)) + nvme++; + else + unknown++; + z += strcspn(elmdn.elm_devnames + z, ","); + } + /* There should be one pass dev for each non-pass dev */ + ATF_CHECK_EQ(pass, da + ada + nvd + nvme); + ATF_CHECK_EQ_MSG(0, unknown, + "Unknown device names %s", elmdn.elm_devnames); + } + } + free(map); + free(namebuf); + + return (true); +} + +ATF_TC(getelmdevnames); +ATF_TC_HEAD(getelmdevnames, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETELMDEVNAMES's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getelmdevnames, tc) +{ + for_each_ses_dev(do_getelmdevnames, O_RDONLY); +} + +static int +elm_type_name2int(const char *name) { + const char *elm_type_names[] = ELM_TYPE_NAMES; + int i; + + for (i = 0; i <= ELMTYP_LAST; i++) { + /* sg_ses uses different case than ses(4) */ + if (0 == strcasecmp(name, elm_type_names[i])) + return i; + } + return (-1); +} + +static bool do_getelmmap(const char *devname, int fd) { + encioc_element_t *map; + FILE *pipe; + char cmd[256]; + char line[256]; + unsigned elm_idx = 0; + unsigned nobj, subenc_id; + int r, elm_type; + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + map = calloc(nobj, sizeof(encioc_element_t)); + ATF_REQUIRE(map != NULL); + r = ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) map); + ATF_REQUIRE_EQ(r, 0); + + snprintf(cmd, sizeof(cmd), "sg_ses -p1 %s", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + while(NULL != fgets(line, sizeof(line), pipe)) { + char elm_type_name[80]; + int i, num_elm; + + r = sscanf(line, + " Element type: %[a-zA-Z0-9_ /], subenclosure id: %d", + elm_type_name, &subenc_id); + if (r == 2) { + elm_type = elm_type_name2int(elm_type_name); + continue; + } else { + r = sscanf(line, + " Element type: vendor specific [0x%x], subenclosure id: %d", + &elm_type, &subenc_id); + if (r == 2) + continue; + } + r = sscanf(line, " number of possible elements: %d", + &num_elm); + if (r != 1) + continue; + + /* Skip the Overall elements */ + elm_idx++; + for (i = 0; i < num_elm; i++, elm_idx++) { + ATF_CHECK_EQ(map[elm_idx].elm_idx, elm_idx); + ATF_CHECK_EQ(map[elm_idx].elm_subenc_id, subenc_id); + ATF_CHECK_EQ((int)map[elm_idx].elm_type, elm_type); + } + } + + free(map); + r = pclose(pipe); + if (r != 0) { + /* Probably an SGPIO device */ + return (false); + } else { + ATF_CHECK_EQ_MSG(nobj, elm_idx, + "Did not find the expected number of element " + "descriptors in sg_ses's output"); + return (true); + } +} + +ATF_TC(getelmmap); +ATF_TC_HEAD(getelmmap, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETELMMAP's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getelmmap, tc) +{ + for_each_ses_dev(do_getelmmap, O_RDONLY); +} + +static bool do_getelmstat(const char *devname, int fd) { + encioc_element_t *map; + unsigned elm_idx; + unsigned nobj; + int r, elm_subidx; + elm_type_t last_elm_type = -1; + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + + map = calloc(nobj, sizeof(encioc_element_t)); + ATF_REQUIRE(map != NULL); + r = ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) map); + + for (elm_idx = 0; elm_idx < nobj; elm_subidx++, elm_idx++) { + encioc_elm_status_t e_status; + FILE *pipe; + char cmd[256]; + uint32_t status; + int pr; + + if (last_elm_type != map[elm_idx].elm_type) + elm_subidx = -1; + last_elm_type = map[elm_idx].elm_type; + + snprintf(cmd, sizeof(cmd), + "sg_ses -Hp2 --index=_%d,%d --get=0:7:32 %s", + map[elm_idx].elm_type, elm_subidx, devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + r = fscanf(pipe, "0x%x", &status); + pr = pclose(pipe); + if (pr != 0) { + /* Probably an SGPIO device */ + free(map); + return (false); + } + ATF_REQUIRE_EQ(r, 1); + + memset(&e_status, 0, sizeof(e_status)); + e_status.elm_idx = map[elm_idx].elm_idx; + r = ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t)&e_status); + ATF_REQUIRE_EQ(r, 0); + + // Compare the common status field + ATF_CHECK_EQ(e_status.cstat[0], status >> 24); + /* + * Ignore the other fields, because some have values that can + * change frequently (voltage, temperature, etc) + */ + } + free(map); + + return (true); +} + +ATF_TC(getelmstat); +ATF_TC_HEAD(getelmstat, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETELMSTAT's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getelmstat, tc) +{ + for_each_ses_dev(do_getelmstat, O_RDONLY); +} + +static bool do_getencid(const char *devname, int fd) { + encioc_string_t stri; + FILE *pipe; + char cmd[256]; + char encid[32]; + char line[256]; + char sg_encid[32]; + int r, sg_ses_r; + + snprintf(cmd, sizeof(cmd), "sg_ses -p1 %s", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + sg_encid[0] = '\0'; + while(NULL != fgets(line, sizeof(line), pipe)) { + const char *f = " enclosure logical identifier (hex): %s"; + + if (1 == fscanf(pipe, f, sg_encid)) + break; + } + sg_ses_r = pclose(pipe); + + stri.bufsiz = sizeof(encid); + stri.buf = &encid[0]; + r = ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri); + ATF_REQUIRE_EQ(r, 0); + if (sg_ses_r == 0) { + ATF_REQUIRE(sg_encid[0] != '\0'); + ATF_CHECK_STREQ(sg_encid, (char*)stri.buf); + return (true); + } else { + /* Probably SGPIO; sg_ses unsupported */ + return (false); + } +} + +ATF_TC(getencid); +ATF_TC_HEAD(getencid, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETENCID's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getencid, tc) +{ + for_each_ses_dev(do_getencid, O_RDONLY); +} + +static bool do_getencname(const char *devname, int fd) { + encioc_string_t stri; + FILE *pipe; + char cmd[256]; + char encname[32]; + char line[256]; + int r; + + snprintf(cmd, sizeof(cmd), "sg_inq -o %s | awk '" + "/Vendor identification/ {vi=$NF} " + "/Product identification/ {pi=$NF} " + "/Product revision level/ {prl=$NF} " + "END {printf(vi \" \" pi \" \" prl)}'", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + ATF_REQUIRE(NULL != fgets(line, sizeof(line), pipe)); + pclose(pipe); + + stri.bufsiz = sizeof(encname); + stri.buf = &encname[0]; + r = ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri); + ATF_REQUIRE_EQ(r, 0); + if (strlen(line) < 3) { + // Probably an SGPIO device, INQUIRY unsupported + return (false); + } else { + ATF_CHECK_STREQ(line, (char*)stri.buf); + return (true); + } +} + +ATF_TC(getencname); +ATF_TC_HEAD(getencname, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETENCNAME's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_inq"); +} +ATF_TC_BODY(getencname, tc) +{ + for_each_ses_dev(do_getencname, O_RDONLY); +} + +static bool do_getencstat(const char *devname, int fd) { + FILE *pipe; + char cmd[256]; + unsigned char e, estat, invop, info, noncrit, crit, unrecov; + int r; + + snprintf(cmd, sizeof(cmd), "sg_ses -p2 %s " + "| grep 'INVOP='", + devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + r = fscanf(pipe, + " INVOP=%hhu, INFO=%hhu, NON-CRIT=%hhu, CRIT=%hhu, UNRECOV=%hhu", + &invop, &info, &noncrit, &crit, &unrecov); + pclose(pipe); + if (r != 5) { + /* Probably on SGPIO device */ + return (false); + } else { + r = ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat); + ATF_REQUIRE_EQ(r, 0); + /* Exclude the info bit because it changes frequently */ + e = (invop << 4) | (noncrit << 2) | (crit << 1) | unrecov; + ATF_CHECK_EQ(estat & ~0x08, e); + return (true); + } +} + +ATF_TC(getencstat); +ATF_TC_HEAD(getencstat, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETENCSTAT's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getencstat, tc) +{ + for_each_ses_dev(do_getencstat, O_RDONLY); +} + +static bool do_getnelm(const char *devname, int fd) { + FILE *pipe; + char cmd[256]; + char line[256]; + unsigned nobj, expected = 0; + int r, sg_ses_r; + + snprintf(cmd, sizeof(cmd), "sg_ses -p1 %s", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + + while(NULL != fgets(line, sizeof(line), pipe)) { + unsigned nelm; + + if (1 == fscanf(pipe, " number of possible elements: %u", + &nelm)) + { + expected += 1 + nelm; // +1 for the Overall element + } + } + sg_ses_r = pclose(pipe); + + r = ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj); + ATF_REQUIRE_EQ(r, 0); + if (sg_ses_r == 0) { + ATF_CHECK_EQ(expected, nobj); + return (true); + } else { + /* Probably SGPIO, sg_ses unsupported */ + return (false); + } +} + +ATF_TC(getnelm); +ATF_TC_HEAD(getnelm, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETNELM's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getnelm, tc) +{ + for_each_ses_dev(do_getnelm, O_RDONLY); +} + +static bool do_getstring(const char *devname, int fd) { + FILE *pipe; + char cmd[256]; + char *sg_ses_buf, *ses_buf; + ssize_t sg_ses_count; + encioc_string_t str_in; + int r; + + sg_ses_buf = malloc(65535); + ATF_REQUIRE(sg_ses_buf != NULL); + ses_buf = malloc(65535); + ATF_REQUIRE(ses_buf != NULL); + + snprintf(cmd, sizeof(cmd), "sg_ses -p4 -rr %s", devname); + pipe = popen(cmd, "r"); + ATF_REQUIRE(pipe != NULL); + sg_ses_count = fread(sg_ses_buf, 1, 65535, pipe); + r = pclose(pipe); + if (r != 0) { + // This SES device does not support the STRINGIN diagnostic page + return (false); + } + ATF_REQUIRE(sg_ses_count > 0); + + str_in.bufsiz = 65535; + str_in.buf = ses_buf; + r = ioctl(fd, ENCIOC_GETSTRING, (caddr_t) &str_in); + ATF_REQUIRE_EQ(r, 0); + ATF_CHECK_EQ(sg_ses_count, (ssize_t)str_in.bufsiz); + ATF_CHECK_EQ(0, memcmp(sg_ses_buf, ses_buf, str_in.bufsiz)); + + free(ses_buf); + free(sg_ses_buf); + + return (true); +} + +ATF_TC(getstring); +ATF_TC_HEAD(getstring, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Compare ENCIOC_GETSTRING's output to sg3_utils'"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.progs", "sg_ses"); +} +ATF_TC_BODY(getstring, tc) +{ + atf_tc_expect_fail("Bug 258188 ENCIO_GETSTRING does not set the string's returned size"); + for_each_ses_dev(do_getstring, O_RDWR); +} + +ATF_TP_ADD_TCS(tp) +{ + + /* + * Untested ioctls: + * + * * ENCIOC_GETTEXT because it was never implemented + * + */ + ATF_TP_ADD_TC(tp, getelmdesc); + ATF_TP_ADD_TC(tp, getelmdevnames); + ATF_TP_ADD_TC(tp, getelmmap); + ATF_TP_ADD_TC(tp, getelmstat); + ATF_TP_ADD_TC(tp, getencid); + ATF_TP_ADD_TC(tp, getencname); + ATF_TP_ADD_TC(tp, getencstat); + ATF_TP_ADD_TC(tp, getnelm); + ATF_TP_ADD_TC(tp, getstring); + + return (atf_no_error()); +} diff --git a/usr.sbin/sesutil/sesutil.c b/usr.sbin/sesutil/sesutil.c index e1bfa83d66b6..b11f02177c62 100644 --- a/usr.sbin/sesutil/sesutil.c +++ b/usr.sbin/sesutil/sesutil.c @@ -1,1009 +1,1010 @@ /*- * Copyright (c) 2019 Klara Inc. * Copyright (c) 2015 Baptiste Daroussin * Copyright (c) 2015 Allan Jude * Copyright (c) 2000 by Matthew Jacob * All rights reserved. * * Portions of this software were developed by Edward Tomasz Napierala * under sponsorship from Klara Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "eltsub.h" #define SESUTIL_XO_VERSION "1" #define TEMPERATURE_OFFSET 20 #define PRINT_STYLE_DASHED 0 #define PRINT_STYLE_DASHED_2 1 #define PRINT_STYLE_CSV 2 #define PRINT_STYLE_CSV_2 3 static int encstatus(int argc, char **argv); static int fault(int argc, char **argv); static int locate(int argc, char **argv); static int objmap(int argc, char **argv); static int sesled(int argc, char **argv, bool fault); static int show(int argc, char **argv); static void sesutil_print(int *style, const char *fmt, ...) __printflike(2,3); static struct command { const char *name; const char *param; const char *desc; int (*exec)(int argc, char **argv); } cmds[] = { { "fault", "(||all) (on|off)", "Change the state of the fault LED associated with a disk", fault }, { "locate", "(||all) (on|off)", "Change the state of the locate LED associated with a disk", locate }, { "map", "", "Print a map of the devices managed by the enclosure", objmap } , { "show", "", "Print a human-friendly summary of the enclosure", show } , { "status", "", "Print the status of the enclosure", encstatus }, }; static const int nbcmds = nitems(cmds); static const char *uflag; static void usage(FILE *out, const char *subcmd) { int i; if (subcmd == NULL) { fprintf(out, "Usage: %s [-u /dev/ses] [options]\n", getprogname()); fprintf(out, "Commands supported:\n"); } for (i = 0; i < nbcmds; i++) { if (subcmd != NULL) { if (strcmp(subcmd, cmds[i].name) == 0) { fprintf(out, "Usage: %s %s [-u /dev/ses] " "%s\n\t%s\n", getprogname(), subcmd, cmds[i].param, cmds[i].desc); break; } continue; } fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name, cmds[i].param, cmds[i].desc); } exit(EXIT_FAILURE); } static void do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault) { int state = onoff ? 1 : 0; encioc_elm_status_t o; struct ses_ctrl_dev_slot *slot; o.elm_idx = idx; if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } ses_status_to_ctrl(type, &o.cstat[0]); switch (type) { case ELMTYP_DEVICE: case ELMTYP_ARRAY_DEV: slot = (struct ses_ctrl_dev_slot *) &o.cstat[0]; ses_ctrl_common_set_select(&slot->common, 1); if (setfault) ses_ctrl_dev_slot_set_rqst_fault(slot, state); else ses_ctrl_dev_slot_set_rqst_ident(slot, state); break; default: return; } if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); } } static bool disk_match(const char *devnames, const char *disk, size_t len) { const char *dname; dname = devnames; while ((dname = strstr(dname, disk)) != NULL) { if (dname[len] == '\0' || dname[len] == ',') { return (true); } dname++; } return (false); } static int sesled(int argc, char **argv, bool setfault) { encioc_elm_devnames_t objdn; encioc_element_t *objp; glob_t g; char *disk, *endptr; size_t len, i, ndisks; int fd; unsigned int nobj, j, sesid; bool all, isses, onoff; isses = false; all = false; onoff = false; if (argc != 3) { usage(stderr, (setfault ? "fault" : "locate")); } disk = argv[1]; sesid = strtoul(disk, &endptr, 10); if (*endptr == '\0') { endptr = strrchr(uflag, '*'); if (endptr != NULL && *endptr == '*') { xo_warnx("Must specifying a SES device (-u) to use a SES " "id# to identify a disk"); usage(stderr, (setfault ? "fault" : "locate")); } isses = true; } if (strcmp(argv[2], "on") == 0) { onoff = true; } else if (strcmp(argv[2], "off") == 0) { onoff = false; } else { usage(stderr, (setfault ? "fault" : "locate")); } if (strcmp(disk, "all") == 0) { all = true; } len = strlen(disk); /* Get the list of ses devices */ if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); xo_errx(EXIT_FAILURE, "No SES devices found"); } ndisks = 0; for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { xo_err(EXIT_FAILURE, "unable to access SES device"); } xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); } objp = calloc(nobj, sizeof(encioc_element_t)); if (objp == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) { free(objp); close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); } if (isses) { if (sesid >= nobj) { free(objp); close(fd); xo_errx(EXIT_FAILURE, "Requested SES ID does not exist"); } do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault); ndisks++; free(objp); close(fd); break; } for (j = 0; j < nobj; j++) { const int devnames_size = 128; char devnames[devnames_size]; if (all) { do_led(fd, objp[j].elm_idx, objp[j].elm_type, onoff, setfault); continue; } memset(&objdn, 0, sizeof(objdn)); memset(devnames, 0, devnames_size); objdn.elm_idx = objp[j].elm_idx; objdn.elm_names_size = devnames_size; objdn.elm_devnames = devnames; if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &objdn) <0) { continue; } if (objdn.elm_names_len > 0) { if (disk_match(objdn.elm_devnames, disk, len)) { do_led(fd, objdn.elm_idx, objp[j].elm_type, onoff, setfault); ndisks++; break; } } } free(objp); close(fd); } globfree(&g); if (ndisks == 0 && all == false) { xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'", disk); } return (EXIT_SUCCESS); } static int locate(int argc, char **argv) { return (sesled(argc, argv, false)); } static int fault(int argc, char **argv) { return (sesled(argc, argv, true)); } static void sesutil_print(int *style, const char *fmt, ...) { va_list args; if (*style == PRINT_STYLE_DASHED) { xo_open_container("extra_status"); xo_emit("\t\tExtra status:\n"); *style = PRINT_STYLE_DASHED_2; } else if (*style == PRINT_STYLE_CSV) { xo_open_container("extra_status"); *style = PRINT_STYLE_CSV_2; } if (*style == PRINT_STYLE_DASHED_2) xo_emit("\t\t- "); else if (*style == PRINT_STYLE_CSV_2) xo_emit(", "); va_start(args, fmt); xo_emit_hv(NULL, fmt, args); va_end(args); if (*style == PRINT_STYLE_DASHED_2) xo_emit("\n"); } static void print_extra_status(int eletype, u_char *cstat, int style) { if (cstat[0] & 0x40) { sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure"); } if (cstat[0] & 0x20) { sesutil_print(&style, "{e:disabled/true} Disabled"); } if (cstat[0] & 0x10) { sesutil_print(&style, "{e:swapped/true} Swapped"); } switch (eletype) { case ELMTYP_DEVICE: case ELMTYP_ARRAY_DEV: if (cstat[2] & 0x02) { sesutil_print(&style, "LED={q:led/locate}"); } if (cstat[2] & 0x20) { sesutil_print(&style, "LED={q:led/fault}"); } break; case ELMTYP_FAN: sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}", (((0x7 & cstat[1]) << 8) + cstat[2]) * 10); break; case ELMTYP_THERM: if (cstat[2]) { sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}", cstat[2] - TEMPERATURE_OFFSET); } else { sesutil_print(&style, "Temperature: -{q:temperature/reserved}"); } break; case ELMTYP_VOM: sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}", be16dec(cstat + 2) / 100.0); break; } if (style) { xo_close_container("extra_status"); } } static int objmap(int argc, char **argv __unused) { encioc_string_t stri; encioc_elm_devnames_t e_devname; encioc_elm_status_t e_status; encioc_elm_desc_t e_desc; encioc_element_t *e_ptr; glob_t g; int fd; unsigned int j, nobj; size_t i; char str[32]; if (argc != 1) { usage(stderr, "map"); } /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); xo_errx(EXIT_FAILURE, "No SES devices found"); } xo_set_version(SESUTIL_XO_VERSION); xo_open_container("sesutil"); xo_open_list("enclosures"); for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { xo_err(EXIT_FAILURE, "unable to access SES device"); } xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); } e_ptr = calloc(nobj, sizeof(encioc_element_t)); if (e_ptr == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); } xo_open_instance("enclosures"); xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0) xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0) xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf); xo_open_list("elements"); for (j = 0; j < nobj; j++) { /* Get the status of the element */ memset(&e_status, 0, sizeof(e_status)); e_status.elm_idx = e_ptr[j].elm_idx; if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &e_status) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } /* Get the description of the element */ memset(&e_desc, 0, sizeof(e_desc)); e_desc.elm_idx = e_ptr[j].elm_idx; e_desc.elm_desc_len = UINT16_MAX; + /* XXX memory leak! */ e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); if (e_desc.elm_desc_str == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); } /* Get the device name(s) of the element */ memset(&e_devname, 0, sizeof(e_devname)); e_devname.elm_idx = e_ptr[j].elm_idx; e_devname.elm_names_size = 128; e_devname.elm_devnames = calloc(128, sizeof(char)); if (e_devname.elm_devnames == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &e_devname) <0) { /* Continue even if we can't look up devnames */ e_devname.elm_devnames[0] = '\0'; } xo_open_instance("elements"); xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx, geteltnm(e_ptr[j].elm_type)); xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n", scode2ascii(e_status.cstat[0]), e_status.cstat[0], e_status.cstat[1], e_status.cstat[2], e_status.cstat[3]); if (e_desc.elm_desc_len > 0) { xo_emit("\t\tDescription: {:description/%s}\n", e_desc.elm_desc_str); } if (e_devname.elm_names_len > 0) { xo_emit("\t\tDevice Names: {:device_names/%s}\n", e_devname.elm_devnames); } print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED); xo_close_instance("elements"); free(e_devname.elm_devnames); } xo_close_list("elements"); free(e_ptr); close(fd); } globfree(&g); xo_close_list("enclosures"); xo_close_container("sesutil"); xo_finish(); return (EXIT_SUCCESS); } /* * Get rid of the 'passN' devices, unless there's nothing else to show. */ static void skip_pass_devices(char *devnames, size_t devnameslen) { char *dev, devs[128], passes[128], *tmp; devs[0] = passes[0] = '\0'; tmp = devnames; while ((dev = strsep(&tmp, ",")) != NULL) { if (strncmp(dev, "pass", 4) == 0) { if (passes[0] != '\0') strlcat(passes, ",", sizeof(passes)); strlcat(passes, dev, sizeof(passes)); } else { if (devs[0] != '\0') strlcat(devs, ",", sizeof(devs)); strlcat(devs, dev, sizeof(devs)); } } strlcpy(devnames, devs, devnameslen); if (devnames[0] == '\0') strlcpy(devnames, passes, devnameslen); } static void fetch_device_details(char *devnames, char **model, char **serial, off_t *size) { char ident[DISK_IDENT_SIZE]; struct diocgattr_arg arg; char *tmp; off_t mediasize; int comma; int fd; comma = (int)strcspn(devnames, ","); asprintf(&tmp, "/dev/%.*s", comma, devnames); if (tmp == NULL) err(1, "asprintf"); fd = open(tmp, O_RDONLY); free(tmp); if (fd < 0) { /* * This can happen with a disk so broken it cannot * be probed by GEOM. */ *model = strdup("?"); *serial = strdup("?"); *size = -1; close(fd); return; } strlcpy(arg.name, "GEOM::descr", sizeof(arg.name)); arg.len = sizeof(arg.value.str); if (ioctl(fd, DIOCGATTR, &arg) == 0) *model = strdup(arg.value.str); else *model = NULL; if (ioctl(fd, DIOCGIDENT, ident) == 0) *serial = strdup(ident); else *serial = NULL; if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0) *size = mediasize; else *size = -1; close(fd); } static void show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc) { encioc_elm_devnames_t e_devname; char *model, *serial; off_t size; /* Get the device name(s) of the element */ memset(&e_devname, 0, sizeof(e_devname)); e_devname.elm_idx = elm_idx; e_devname.elm_names_size = 128; e_devname.elm_devnames = calloc(128, sizeof(char)); if (e_devname.elm_devnames == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &e_devname) < 0) { /* We don't care if this fails */ e_devname.elm_devnames[0] = '\0'; model = NULL; serial = NULL; size = -1; } else { skip_pass_devices(e_devname.elm_devnames, 128); fetch_device_details(e_devname.elm_devnames, &model, &serial, &size); } xo_open_instance("elements"); xo_emit("{e:type/device_slot}"); xo_emit("{d:description/%-15s} ", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "-"); xo_emit("{e:description/%-15s}", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : ""); xo_emit("{d:device_names/%-7s} ", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "-"); xo_emit("{e:device_names/%s}", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : ""); xo_emit("{d:model/%-25s} ", model ? model : "-"); xo_emit("{e:model/%s}", model ? model : ""); xo_emit("{d:serial/%-20s} ", serial != NULL ? serial : "-"); xo_emit("{e:serial/%s}", serial != NULL ? serial : ""); if ((e_status.cstat[0] & 0xf) == SES_OBJSTAT_OK && size >= 0) { xo_emit("{h,hn-1000:size/%ld}{e:status/%s}", size, scode2ascii(e_status.cstat[0])); } else { xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0])); } print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV); xo_emit("\n"); xo_close_instance("elements"); free(e_devname.elm_devnames); } static void show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc) { if (e_desc.elm_desc_len <= 0) { /* We don't have a label to display; might as well skip it. */ return; } if (e_status.cstat[2] == 0) { /* No temperature to show. */ return; } xo_open_instance("elements"); xo_emit("{e:type/temperature_sensor}"); xo_emit("{:description/%s}: {:temperature/%d}{Uw:C}", e_desc.elm_desc_str, e_status.cstat[2] - TEMPERATURE_OFFSET); xo_close_instance("elements"); } static void show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc) { if (e_desc.elm_desc_len <= 0) { /* We don't have a label to display; might as well skip it. */ return; } if (e_status.cstat[2] == 0) { /* No voltage to show. */ return; } xo_open_instance("elements"); xo_emit("{e:type/voltage_sensor}"); xo_emit("{:description/%s}: {:voltage/%.2f}{Uw:V}", e_desc.elm_desc_str, be16dec(e_status.cstat + 2) / 100.0); xo_close_instance("elements"); } static int show(int argc, char **argv __unused) { encioc_string_t stri; encioc_elm_status_t e_status; encioc_elm_desc_t e_desc; encioc_element_t *e_ptr; glob_t g; elm_type_t prev_type; int fd; unsigned int j, nobj; size_t i; bool first_ses; char str[32]; if (argc != 1) { usage(stderr, "map"); } first_ses = true; /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); xo_errx(EXIT_FAILURE, "No SES devices found"); } xo_set_version(SESUTIL_XO_VERSION); xo_open_container("sesutil"); xo_open_list("enclosures"); for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { xo_err(EXIT_FAILURE, "unable to access SES device"); } xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); } e_ptr = calloc(nobj, sizeof(encioc_element_t)); if (e_ptr == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); } xo_open_instance("enclosures"); if (first_ses) first_ses = false; else xo_emit("\n"); xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0) xo_emit("<{t:name/%s}>; ", stri.buf); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0) xo_emit("ID: {t:id/%s}", stri.buf); xo_emit("\n"); xo_open_list("elements"); prev_type = -1; for (j = 0; j < nobj; j++) { /* Get the status of the element */ memset(&e_status, 0, sizeof(e_status)); e_status.elm_idx = e_ptr[j].elm_idx; if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &e_status) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } /* * Skip "Unsupported" elements; those usually precede * the actual device entries and are not particularly * interesting. */ if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED) continue; /* Get the description of the element */ memset(&e_desc, 0, sizeof(e_desc)); e_desc.elm_idx = e_ptr[j].elm_idx; e_desc.elm_desc_len = UINT16_MAX; e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); if (e_desc.elm_desc_str == NULL) { close(fd); xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); } switch (e_ptr[j].elm_type) { case ELMTYP_DEVICE: case ELMTYP_ARRAY_DEV: if (e_ptr[j].elm_type != prev_type) xo_emit("Desc Dev Model Ident Size/Status\n"); show_device(fd, e_ptr[j].elm_idx, e_status, e_desc); prev_type = e_ptr[j].elm_type; break; case ELMTYP_THERM: if (e_ptr[j].elm_type != prev_type) xo_emit("\nTemperatures: "); else xo_emit(", "); prev_type = e_ptr[j].elm_type; show_therm(e_status, e_desc); break; case ELMTYP_VOM: if (e_ptr[j].elm_type != prev_type) xo_emit("\nVoltages: "); else xo_emit(", "); prev_type = e_ptr[j].elm_type; show_vom(e_status, e_desc); break; default: /* * Ignore stuff not interesting to the user. */ break; } } if (prev_type != (elm_type_t)-1 && prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV) xo_emit("\n"); xo_close_list("elements"); free(e_ptr); close(fd); } globfree(&g); xo_close_list("enclosures"); xo_close_container("sesutil"); xo_finish(); return (EXIT_SUCCESS); } static int encstatus(int argc, char **argv __unused) { glob_t g; int fd, status; size_t i, e; u_char estat; status = 0; if (argc != 1) { usage(stderr, "status"); } /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); xo_errx(EXIT_FAILURE, "No SES devices found"); } xo_set_version(SESUTIL_XO_VERSION); xo_open_container("sesutil"); xo_open_list("enclosures"); for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { xo_err(EXIT_FAILURE, "unable to access SES device"); } xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) { xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT"); close(fd); } xo_open_instance("enclosures"); xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5); e = 0; if (estat == 0) { if (status == 0) { status = 1; } xo_emit("{q:status/OK}"); } else { if (estat & SES_ENCSTAT_INFO) { xo_emit("{lq:status/INFO}"); e++; } if (estat & SES_ENCSTAT_NONCRITICAL) { if (e) xo_emit(","); xo_emit("{lq:status/NONCRITICAL}"); e++; } if (estat & SES_ENCSTAT_CRITICAL) { if (e) xo_emit(","); xo_emit("{lq:status/CRITICAL}"); e++; status = -1; } if (estat & SES_ENCSTAT_UNRECOV) { if (e) xo_emit(","); xo_emit("{lq:status/UNRECOV}"); e++; status = -1; } } xo_close_instance("enclosures"); xo_emit("\n"); close(fd); } globfree(&g); xo_close_list("enclosures"); xo_close_container("sesutil"); xo_finish(); if (status == 1) { return (EXIT_SUCCESS); } else { return (EXIT_FAILURE); } } int main(int argc, char **argv) { int i, ch; struct command *cmd = NULL; argc = xo_parse_args(argc, argv); if (argc < 0) exit(1); uflag = "/dev/ses[0-9]*"; while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) { switch (ch) { case 'u': uflag = optarg; break; case '?': default: usage(stderr, NULL); } } argc -= optind; argv += optind; if (argc < 1) { warnx("Missing command"); usage(stderr, NULL); } for (i = 0; i < nbcmds; i++) { if (strcmp(argv[0], cmds[i].name) == 0) { cmd = &cmds[i]; break; } } if (cmd == NULL) { warnx("unknown command %s", argv[0]); usage(stderr, NULL); } return (cmd->exec(argc, argv)); }