2 * Copyright (c) 2019 Klara Inc.
3 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
4 * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
5 * Copyright (c) 2000 by Matthew Jacob
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from Klara Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in this position and unchanged.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/endian.h>
37 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
56 #include <cam/scsi/scsi_enc.h>
60 #define SESUTIL_XO_VERSION "1"
62 #define TEMPERATURE_OFFSET 20
64 #define PRINT_STYLE_DASHED 0
65 #define PRINT_STYLE_DASHED_2 1
66 #define PRINT_STYLE_CSV 2
67 #define PRINT_STYLE_CSV_2 3
69 static int encstatus(int argc, char **argv);
70 static int fault(int argc, char **argv);
71 static int locate(int argc, char **argv);
72 static int objmap(int argc, char **argv);
73 static int sesled(int argc, char **argv, bool fault);
74 static int show(int argc, char **argv);
75 static void sesutil_print(int *style, const char *fmt, ...) __printflike(2,3);
77 static struct command {
81 int (*exec)(int argc, char **argv);
84 "(<disk>|<sesid>|all) (on|off)",
85 "Change the state of the fault LED associated with a disk",
88 "(<disk>|<sesid>|all) (on|off)",
89 "Change the state of the locate LED associated with a disk",
92 "Print a map of the devices managed by the enclosure", objmap } ,
94 "Print a human-friendly summary of the enclosure", show } ,
95 { "status", "", "Print the status of the enclosure",
99 static const int nbcmds = nitems(cmds);
100 static const char *uflag;
103 usage(FILE *out, const char *subcmd)
107 if (subcmd == NULL) {
108 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
110 fprintf(out, "Commands supported:\n");
112 for (i = 0; i < nbcmds; i++) {
113 if (subcmd != NULL) {
114 if (strcmp(subcmd, cmds[i].name) == 0) {
115 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
116 "%s\n\t%s\n", getprogname(), subcmd,
117 cmds[i].param, cmds[i].desc);
122 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name,
123 cmds[i].param, cmds[i].desc);
130 do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault)
132 int state = onoff ? 1 : 0;
133 encioc_elm_status_t o;
134 struct ses_ctrl_dev_slot *slot;
137 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
139 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
141 ses_status_to_ctrl(type, &o.cstat[0]);
144 case ELMTYP_ARRAY_DEV:
145 slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
146 ses_ctrl_common_set_select(&slot->common, 1);
148 ses_ctrl_dev_slot_set_rqst_fault(slot, state);
150 ses_ctrl_dev_slot_set_rqst_ident(slot, state);
155 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
157 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
162 disk_match(const char *devnames, const char *disk, size_t len)
167 while ((dname = strstr(dname, disk)) != NULL) {
168 if (dname[len] == '\0' || dname[len] == ',') {
178 sesled(int argc, char **argv, bool setfault)
180 encioc_elm_devnames_t objdn;
181 encioc_element_t *objp;
184 size_t len, i, ndisks;
186 unsigned int nobj, j, sesid;
187 bool all, isses, onoff;
194 usage(stderr, (setfault ? "fault" : "locate"));
199 sesid = strtoul(disk, &endptr, 10);
200 if (*endptr == '\0') {
201 endptr = strrchr(uflag, '*');
202 if (endptr != NULL && *endptr == '*') {
203 xo_warnx("Must specifying a SES device (-u) to use a SES "
204 "id# to identify a disk");
205 usage(stderr, (setfault ? "fault" : "locate"));
210 if (strcmp(argv[2], "on") == 0) {
212 } else if (strcmp(argv[2], "off") == 0) {
215 usage(stderr, (setfault ? "fault" : "locate"));
218 if (strcmp(disk, "all") == 0) {
223 /* Get the list of ses devices */
224 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
227 xo_errx(EXIT_FAILURE, "No SES devices found");
231 for (i = 0; i < g.gl_pathc; i++) {
232 /* ensure we only got numbers after ses */
233 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
234 strlen(g.gl_pathv[i] + 8)) {
237 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
239 * Don't treat non-access errors as critical if we are
240 * accessing all devices
242 if (errno == EACCES && g.gl_pathc > 1) {
243 xo_err(EXIT_FAILURE, "unable to access SES device");
245 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
249 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
251 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
254 objp = calloc(nobj, sizeof(encioc_element_t));
257 xo_err(EXIT_FAILURE, "calloc()");
260 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
263 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
270 xo_errx(EXIT_FAILURE,
271 "Requested SES ID does not exist");
273 do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
279 for (j = 0; j < nobj; j++) {
280 const int devnames_size = 128;
281 char devnames[devnames_size];
284 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
288 memset(&objdn, 0, sizeof(objdn));
289 memset(devnames, 0, devnames_size);
290 objdn.elm_idx = objp[j].elm_idx;
291 objdn.elm_names_size = devnames_size;
292 objdn.elm_devnames = devnames;
293 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
294 (caddr_t) &objdn) <0) {
297 if (objdn.elm_names_len > 0) {
298 if (disk_match(objdn.elm_devnames, disk, len)) {
299 do_led(fd, objdn.elm_idx, objp[j].elm_type,
310 if (ndisks == 0 && all == false) {
311 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
315 return (EXIT_SUCCESS);
319 locate(int argc, char **argv)
322 return (sesled(argc, argv, false));
326 fault(int argc, char **argv)
329 return (sesled(argc, argv, true));
333 sesutil_print(int *style, const char *fmt, ...)
337 if (*style == PRINT_STYLE_DASHED) {
338 xo_open_container("extra_status");
339 xo_emit("\t\tExtra status:\n");
340 *style = PRINT_STYLE_DASHED_2;
341 } else if (*style == PRINT_STYLE_CSV) {
342 xo_open_container("extra_status");
343 *style = PRINT_STYLE_CSV_2;
346 if (*style == PRINT_STYLE_DASHED_2)
348 else if (*style == PRINT_STYLE_CSV_2)
351 xo_emit_hv(NULL, fmt, args);
353 if (*style == PRINT_STYLE_DASHED_2)
358 print_extra_status(int eletype, u_char *cstat, int style)
361 if (cstat[0] & 0x40) {
362 sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure");
364 if (cstat[0] & 0x20) {
365 sesutil_print(&style, "{e:disabled/true} Disabled");
367 if (cstat[0] & 0x10) {
368 sesutil_print(&style, "{e:swapped/true} Swapped");
372 case ELMTYP_ARRAY_DEV:
373 if (cstat[2] & 0x02) {
374 sesutil_print(&style, "LED={q:led/locate}");
376 if (cstat[2] & 0x20) {
377 sesutil_print(&style, "LED={q:led/fault}");
381 sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}",
382 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
386 sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}",
387 cstat[2] - TEMPERATURE_OFFSET);
389 sesutil_print(&style, "Temperature: -{q:temperature/reserved}");
393 sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}",
394 be16dec(cstat + 2) / 100.0);
398 xo_close_container("extra_status");
403 objmap(int argc, char **argv __unused)
405 encioc_string_t stri;
406 encioc_elm_devnames_t e_devname;
407 encioc_elm_status_t e_status;
408 encioc_elm_desc_t e_desc;
409 encioc_element_t *e_ptr;
412 unsigned int j, nobj;
417 usage(stderr, "map");
420 /* Get the list of ses devices */
421 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
423 xo_errx(EXIT_FAILURE, "No SES devices found");
425 xo_set_version(SESUTIL_XO_VERSION);
426 xo_open_container("sesutil");
427 xo_open_list("enclosures");
428 for (i = 0; i < g.gl_pathc; i++) {
429 /* ensure we only got numbers after ses */
430 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
431 strlen(g.gl_pathv[i] + 8)) {
434 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
436 * Don't treat non-access errors as critical if we are
437 * accessing all devices
439 if (errno == EACCES && g.gl_pathc > 1) {
440 xo_err(EXIT_FAILURE, "unable to access SES device");
442 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
446 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
448 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
451 e_ptr = calloc(nobj, sizeof(encioc_element_t));
454 xo_err(EXIT_FAILURE, "calloc()");
457 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
459 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
462 xo_open_instance("enclosures");
463 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
464 stri.bufsiz = sizeof(str);
466 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
467 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
468 stri.bufsiz = sizeof(str);
470 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
471 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
473 xo_open_list("elements");
474 for (j = 0; j < nobj; j++) {
475 /* Get the status of the element */
476 memset(&e_status, 0, sizeof(e_status));
477 e_status.elm_idx = e_ptr[j].elm_idx;
478 if (ioctl(fd, ENCIOC_GETELMSTAT,
479 (caddr_t) &e_status) < 0) {
481 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
483 /* Get the description of the element */
484 memset(&e_desc, 0, sizeof(e_desc));
485 e_desc.elm_idx = e_ptr[j].elm_idx;
486 e_desc.elm_desc_len = UINT16_MAX;
487 /* XXX memory leak! */
488 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
489 if (e_desc.elm_desc_str == NULL) {
491 xo_err(EXIT_FAILURE, "calloc()");
493 if (ioctl(fd, ENCIOC_GETELMDESC,
494 (caddr_t) &e_desc) < 0) {
496 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
498 /* Get the device name(s) of the element */
499 memset(&e_devname, 0, sizeof(e_devname));
500 e_devname.elm_idx = e_ptr[j].elm_idx;
501 e_devname.elm_names_size = 128;
502 e_devname.elm_devnames = calloc(128, sizeof(char));
503 if (e_devname.elm_devnames == NULL) {
505 xo_err(EXIT_FAILURE, "calloc()");
507 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
508 (caddr_t) &e_devname) <0) {
509 /* Continue even if we can't look up devnames */
510 e_devname.elm_devnames[0] = '\0';
512 xo_open_instance("elements");
513 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
514 geteltnm(e_ptr[j].elm_type));
515 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
516 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
517 e_status.cstat[1], e_status.cstat[2],
519 if (e_desc.elm_desc_len > 0) {
520 xo_emit("\t\tDescription: {:description/%s}\n",
521 e_desc.elm_desc_str);
523 if (e_devname.elm_names_len > 0) {
524 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
525 e_devname.elm_devnames);
527 print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED);
528 xo_close_instance("elements");
529 free(e_devname.elm_devnames);
531 xo_close_list("elements");
536 xo_close_list("enclosures");
537 xo_close_container("sesutil");
540 return (EXIT_SUCCESS);
544 * Get rid of the 'passN' devices, unless there's nothing else to show.
547 skip_pass_devices(char *devnames, size_t devnameslen)
549 char *dev, devs[128], passes[128], *tmp;
551 devs[0] = passes[0] = '\0';
554 while ((dev = strsep(&tmp, ",")) != NULL) {
555 if (strncmp(dev, "pass", 4) == 0) {
556 if (passes[0] != '\0')
557 strlcat(passes, ",", sizeof(passes));
558 strlcat(passes, dev, sizeof(passes));
561 strlcat(devs, ",", sizeof(devs));
562 strlcat(devs, dev, sizeof(devs));
565 strlcpy(devnames, devs, devnameslen);
566 if (devnames[0] == '\0')
567 strlcpy(devnames, passes, devnameslen);
571 fetch_device_details(char *devnames, char **model, char **serial, off_t *size)
573 char ident[DISK_IDENT_SIZE];
574 struct diocgattr_arg arg;
580 comma = (int)strcspn(devnames, ",");
581 asprintf(&tmp, "/dev/%.*s", comma, devnames);
584 fd = open(tmp, O_RDONLY);
588 * This can happen with a disk so broken it cannot
591 *model = strdup("?");
592 *serial = strdup("?");
598 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
599 arg.len = sizeof(arg.value.str);
600 if (ioctl(fd, DIOCGATTR, &arg) == 0)
601 *model = strdup(arg.value.str);
605 if (ioctl(fd, DIOCGIDENT, ident) == 0)
606 *serial = strdup(ident);
610 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0)
618 show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
620 encioc_elm_devnames_t e_devname;
621 char *model, *serial;
624 /* Get the device name(s) of the element */
625 memset(&e_devname, 0, sizeof(e_devname));
626 e_devname.elm_idx = elm_idx;
627 e_devname.elm_names_size = 128;
628 e_devname.elm_devnames = calloc(128, sizeof(char));
629 if (e_devname.elm_devnames == NULL) {
631 xo_err(EXIT_FAILURE, "calloc()");
634 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
635 (caddr_t) &e_devname) < 0) {
636 /* We don't care if this fails */
637 e_devname.elm_devnames[0] = '\0';
642 skip_pass_devices(e_devname.elm_devnames, 128);
643 fetch_device_details(e_devname.elm_devnames, &model, &serial, &size);
645 xo_open_instance("elements");
646 xo_emit("{e:type/device_slot}");
647 xo_emit("{d:description/%-15s} ", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "-");
648 xo_emit("{e:description/%-15s}", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "");
649 xo_emit("{d:device_names/%-7s} ", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "-");
650 xo_emit("{e:device_names/%s}", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "");
651 xo_emit("{d:model/%-25s} ", model ? model : "-");
652 xo_emit("{e:model/%s}", model ? model : "");
653 xo_emit("{d:serial/%-20s} ", serial != NULL ? serial : "-");
654 xo_emit("{e:serial/%s}", serial != NULL ? serial : "");
655 if ((e_status.cstat[0] & 0xf) == SES_OBJSTAT_OK && size >= 0) {
656 xo_emit("{h,hn-1000:size/%ld}{e:status/%s}",
657 size, scode2ascii(e_status.cstat[0]));
659 xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0]));
661 print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV);
663 xo_close_instance("elements");
664 free(e_devname.elm_devnames);
668 show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
671 if (e_desc.elm_desc_len <= 0) {
672 /* We don't have a label to display; might as well skip it. */
676 if (e_status.cstat[2] == 0) {
677 /* No temperature to show. */
681 xo_open_instance("elements");
682 xo_emit("{e:type/temperature_sensor}");
683 xo_emit("{:description/%s}: {:temperature/%d}{Uw:C}",
684 e_desc.elm_desc_str, e_status.cstat[2] - TEMPERATURE_OFFSET);
685 xo_close_instance("elements");
689 show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
692 if (e_desc.elm_desc_len <= 0) {
693 /* We don't have a label to display; might as well skip it. */
697 if (e_status.cstat[2] == 0) {
698 /* No voltage to show. */
702 xo_open_instance("elements");
703 xo_emit("{e:type/voltage_sensor}");
704 xo_emit("{:description/%s}: {:voltage/%.2f}{Uw:V}",
705 e_desc.elm_desc_str, be16dec(e_status.cstat + 2) / 100.0);
706 xo_close_instance("elements");
710 show(int argc, char **argv __unused)
712 encioc_string_t stri;
713 encioc_elm_status_t e_status;
714 encioc_elm_desc_t e_desc;
715 encioc_element_t *e_ptr;
717 elm_type_t prev_type;
719 unsigned int j, nobj;
725 usage(stderr, "map");
730 /* Get the list of ses devices */
731 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
733 xo_errx(EXIT_FAILURE, "No SES devices found");
735 xo_set_version(SESUTIL_XO_VERSION);
736 xo_open_container("sesutil");
737 xo_open_list("enclosures");
738 for (i = 0; i < g.gl_pathc; i++) {
739 /* ensure we only got numbers after ses */
740 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
741 strlen(g.gl_pathv[i] + 8)) {
744 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
746 * Don't treat non-access errors as critical if we are
747 * accessing all devices
749 if (errno == EACCES && g.gl_pathc > 1) {
750 xo_err(EXIT_FAILURE, "unable to access SES device");
752 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
756 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
758 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
761 e_ptr = calloc(nobj, sizeof(encioc_element_t));
764 xo_err(EXIT_FAILURE, "calloc()");
767 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
769 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
772 xo_open_instance("enclosures");
779 xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5);
780 stri.bufsiz = sizeof(str);
782 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
783 xo_emit("<{t:name/%s}>; ", stri.buf);
784 stri.bufsiz = sizeof(str);
786 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
787 xo_emit("ID: {t:id/%s}", stri.buf);
790 xo_open_list("elements");
792 for (j = 0; j < nobj; j++) {
793 /* Get the status of the element */
794 memset(&e_status, 0, sizeof(e_status));
795 e_status.elm_idx = e_ptr[j].elm_idx;
796 if (ioctl(fd, ENCIOC_GETELMSTAT,
797 (caddr_t) &e_status) < 0) {
799 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
803 * Skip "Unsupported" elements; those usually precede
804 * the actual device entries and are not particularly
807 if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED)
810 /* Get the description of the element */
811 memset(&e_desc, 0, sizeof(e_desc));
812 e_desc.elm_idx = e_ptr[j].elm_idx;
813 e_desc.elm_desc_len = UINT16_MAX;
814 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
815 if (e_desc.elm_desc_str == NULL) {
817 xo_err(EXIT_FAILURE, "calloc()");
819 if (ioctl(fd, ENCIOC_GETELMDESC,
820 (caddr_t) &e_desc) < 0) {
822 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
825 switch (e_ptr[j].elm_type) {
827 case ELMTYP_ARRAY_DEV:
828 if (e_ptr[j].elm_type != prev_type)
829 xo_emit("Desc Dev Model Ident Size/Status\n");
831 show_device(fd, e_ptr[j].elm_idx, e_status, e_desc);
832 prev_type = e_ptr[j].elm_type;
835 if (e_ptr[j].elm_type != prev_type)
836 xo_emit("\nTemperatures: ");
839 prev_type = e_ptr[j].elm_type;
840 show_therm(e_status, e_desc);
843 if (e_ptr[j].elm_type != prev_type)
844 xo_emit("\nVoltages: ");
847 prev_type = e_ptr[j].elm_type;
848 show_vom(e_status, e_desc);
852 * Ignore stuff not interesting to the user.
857 if (prev_type != (elm_type_t)-1 &&
858 prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV)
860 xo_close_list("elements");
865 xo_close_list("enclosures");
866 xo_close_container("sesutil");
869 return (EXIT_SUCCESS);
873 encstatus(int argc, char **argv __unused)
882 usage(stderr, "status");
885 /* Get the list of ses devices */
886 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
888 xo_errx(EXIT_FAILURE, "No SES devices found");
891 xo_set_version(SESUTIL_XO_VERSION);
892 xo_open_container("sesutil");
893 xo_open_list("enclosures");
894 for (i = 0; i < g.gl_pathc; i++) {
895 /* ensure we only got numbers after ses */
896 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
897 strlen(g.gl_pathv[i] + 8)) {
900 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
902 * Don't treat non-access errors as critical if we are
903 * accessing all devices
905 if (errno == EACCES && g.gl_pathc > 1) {
906 xo_err(EXIT_FAILURE, "unable to access SES device");
908 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
912 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
913 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
917 xo_open_instance("enclosures");
918 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
924 xo_emit("{q:status/OK}");
926 if (estat & SES_ENCSTAT_INFO) {
927 xo_emit("{lq:status/INFO}");
930 if (estat & SES_ENCSTAT_NONCRITICAL) {
933 xo_emit("{lq:status/NONCRITICAL}");
936 if (estat & SES_ENCSTAT_CRITICAL) {
939 xo_emit("{lq:status/CRITICAL}");
943 if (estat & SES_ENCSTAT_UNRECOV) {
946 xo_emit("{lq:status/UNRECOV}");
951 xo_close_instance("enclosures");
957 xo_close_list("enclosures");
958 xo_close_container("sesutil");
962 return (EXIT_SUCCESS);
964 return (EXIT_FAILURE);
969 main(int argc, char **argv)
972 struct command *cmd = NULL;
974 argc = xo_parse_args(argc, argv);
978 uflag = "/dev/ses[0-9]*";
979 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
993 warnx("Missing command");
997 for (i = 0; i < nbcmds; i++) {
998 if (strcmp(argv[0], cmds[i].name) == 0) {
1005 warnx("unknown command %s", argv[0]);
1006 usage(stderr, NULL);
1009 return (cmd->exec(argc, argv));