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 encioc_elm_status_t es;
285 memset(&es, 0, sizeof(es));
286 es.elm_idx = objp[j].elm_idx;
287 if (ioctl(fd, ENCIOC_GETELMSTAT, &es) < 0) {
290 "ENCIOC_GETELMSTAT");
292 if ((es.cstat[0] & 0xf) == SES_OBJSTAT_NOACCESS)
294 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
298 memset(&objdn, 0, sizeof(objdn));
299 memset(devnames, 0, devnames_size);
300 objdn.elm_idx = objp[j].elm_idx;
301 objdn.elm_names_size = devnames_size;
302 objdn.elm_devnames = devnames;
303 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
304 (caddr_t) &objdn) <0) {
307 if (objdn.elm_names_len > 0) {
308 if (disk_match(objdn.elm_devnames, disk, len)) {
309 do_led(fd, objdn.elm_idx, objp[j].elm_type,
320 if (ndisks == 0 && all == false) {
321 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
325 return (EXIT_SUCCESS);
329 locate(int argc, char **argv)
332 return (sesled(argc, argv, false));
336 fault(int argc, char **argv)
339 return (sesled(argc, argv, true));
343 sesutil_print(int *style, const char *fmt, ...)
347 if (*style == PRINT_STYLE_DASHED) {
348 xo_open_container("extra_status");
349 xo_emit("\t\tExtra status:\n");
350 *style = PRINT_STYLE_DASHED_2;
351 } else if (*style == PRINT_STYLE_CSV) {
352 xo_open_container("extra_status");
353 *style = PRINT_STYLE_CSV_2;
356 if (*style == PRINT_STYLE_DASHED_2)
358 else if (*style == PRINT_STYLE_CSV_2)
361 xo_emit_hv(NULL, fmt, args);
363 if (*style == PRINT_STYLE_DASHED_2)
368 print_extra_status(int eletype, u_char *cstat, int style)
371 if (cstat[0] & 0x40) {
372 sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure");
374 if (cstat[0] & 0x20) {
375 sesutil_print(&style, "{e:disabled/true} Disabled");
377 if (cstat[0] & 0x10) {
378 sesutil_print(&style, "{e:swapped/true} Swapped");
382 case ELMTYP_ARRAY_DEV:
383 if (cstat[2] & 0x02) {
384 sesutil_print(&style, "LED={q:led/locate}");
386 if (cstat[2] & 0x20) {
387 sesutil_print(&style, "LED={q:led/fault}");
391 sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}",
392 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
396 sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}",
397 cstat[2] - TEMPERATURE_OFFSET);
399 sesutil_print(&style, "Temperature: -{q:temperature/reserved}");
403 sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}",
404 be16dec(cstat + 2) / 100.0);
408 xo_close_container("extra_status");
413 objmap(int argc, char **argv __unused)
415 encioc_string_t stri;
416 encioc_elm_devnames_t e_devname;
417 encioc_elm_status_t e_status;
418 encioc_elm_desc_t e_desc;
419 encioc_element_t *e_ptr;
422 unsigned int j, nobj;
427 usage(stderr, "map");
430 memset(&e_desc, 0, sizeof(e_desc));
431 /* SES4r02 allows element descriptors of up to 65536 characters */
432 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
433 if (e_desc.elm_desc_str == NULL)
434 xo_err(EXIT_FAILURE, "calloc()");
436 e_devname.elm_devnames = calloc(128, sizeof(char));
437 if (e_devname.elm_devnames == NULL)
438 xo_err(EXIT_FAILURE, "calloc()");
439 e_devname.elm_names_size = 128;
441 /* Get the list of ses devices */
442 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
444 xo_errx(EXIT_FAILURE, "No SES devices found");
446 xo_set_version(SESUTIL_XO_VERSION);
447 xo_open_container("sesutil");
448 xo_open_list("enclosures");
449 for (i = 0; i < g.gl_pathc; i++) {
450 /* ensure we only got numbers after ses */
451 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
452 strlen(g.gl_pathv[i] + 8)) {
455 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
457 * Don't treat non-access errors as critical if we are
458 * accessing all devices
460 if (errno == EACCES && g.gl_pathc > 1) {
461 xo_err(EXIT_FAILURE, "unable to access SES device");
463 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
467 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
469 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
472 e_ptr = calloc(nobj, sizeof(encioc_element_t));
475 xo_err(EXIT_FAILURE, "calloc()");
478 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
480 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
483 xo_open_instance("enclosures");
484 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
485 stri.bufsiz = sizeof(str);
487 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
488 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
489 stri.bufsiz = sizeof(str);
491 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
492 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
494 xo_open_list("elements");
495 for (j = 0; j < nobj; j++) {
496 /* Get the status of the element */
497 memset(&e_status, 0, sizeof(e_status));
498 e_status.elm_idx = e_ptr[j].elm_idx;
499 if (ioctl(fd, ENCIOC_GETELMSTAT,
500 (caddr_t) &e_status) < 0) {
502 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
504 /* Get the description of the element */
505 e_desc.elm_idx = e_ptr[j].elm_idx;
506 e_desc.elm_desc_len = UINT16_MAX;
507 if (ioctl(fd, ENCIOC_GETELMDESC,
508 (caddr_t) &e_desc) < 0) {
510 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
512 e_desc.elm_desc_str[e_desc.elm_desc_len] = '\0';
513 /* Get the device name(s) of the element */
514 e_devname.elm_idx = e_ptr[j].elm_idx;
515 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
516 (caddr_t) &e_devname) <0) {
517 /* Continue even if we can't look up devnames */
518 e_devname.elm_devnames[0] = '\0';
520 xo_open_instance("elements");
521 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
522 geteltnm(e_ptr[j].elm_type));
523 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
524 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
525 e_status.cstat[1], e_status.cstat[2],
527 if (e_desc.elm_desc_len > 0) {
528 xo_emit("\t\tDescription: {:description/%s}\n",
529 e_desc.elm_desc_str);
531 if (e_devname.elm_names_len > 0) {
532 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
533 e_devname.elm_devnames);
535 print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED);
536 xo_close_instance("elements");
538 xo_close_list("elements");
543 free(e_devname.elm_devnames);
544 free(e_desc.elm_desc_str);
545 xo_close_list("enclosures");
546 xo_close_container("sesutil");
549 return (EXIT_SUCCESS);
553 * Get rid of the 'passN' devices, unless there's nothing else to show.
556 skip_pass_devices(char *devnames, size_t devnameslen)
558 char *dev, devs[128], passes[128], *tmp;
560 devs[0] = passes[0] = '\0';
563 while ((dev = strsep(&tmp, ",")) != NULL) {
564 if (strncmp(dev, "pass", 4) == 0) {
565 if (passes[0] != '\0')
566 strlcat(passes, ",", sizeof(passes));
567 strlcat(passes, dev, sizeof(passes));
570 strlcat(devs, ",", sizeof(devs));
571 strlcat(devs, dev, sizeof(devs));
574 strlcpy(devnames, devs, devnameslen);
575 if (devnames[0] == '\0')
576 strlcpy(devnames, passes, devnameslen);
580 fetch_device_details(char *devnames, char **model, char **serial, off_t *size)
582 char ident[DISK_IDENT_SIZE];
583 struct diocgattr_arg arg;
589 comma = (int)strcspn(devnames, ",");
590 asprintf(&tmp, "/dev/%.*s", comma, devnames);
593 fd = open(tmp, O_RDONLY);
597 * This can happen with a disk so broken it cannot
600 *model = strdup("?");
601 *serial = strdup("?");
607 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
608 arg.len = sizeof(arg.value.str);
609 if (ioctl(fd, DIOCGATTR, &arg) == 0)
610 *model = strdup(arg.value.str);
614 if (ioctl(fd, DIOCGIDENT, ident) == 0)
615 *serial = strdup(ident);
619 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0)
627 show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
629 encioc_elm_devnames_t e_devname;
630 char *model = NULL, *serial = NULL;
633 /* Get the device name(s) of the element */
634 memset(&e_devname, 0, sizeof(e_devname));
635 e_devname.elm_idx = elm_idx;
636 e_devname.elm_names_size = 128;
637 e_devname.elm_devnames = calloc(128, sizeof(char));
638 if (e_devname.elm_devnames == NULL) {
640 xo_err(EXIT_FAILURE, "calloc()");
643 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
644 (caddr_t) &e_devname) < 0) {
645 /* We don't care if this fails */
646 e_devname.elm_devnames[0] = '\0';
649 skip_pass_devices(e_devname.elm_devnames, 128);
650 fetch_device_details(e_devname.elm_devnames, &model, &serial, &size);
652 xo_open_instance("elements");
653 xo_emit("{e:type/device_slot}");
654 xo_emit("{d:description/%-15s} ", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "-");
655 xo_emit("{e:description/%-15s}", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "");
656 xo_emit("{d:device_names/%-7s} ", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "-");
657 xo_emit("{e:device_names/%s}", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "");
658 xo_emit("{d:model/%-25s} ", model ? model : "-");
659 xo_emit("{e:model/%s}", model ? model : "");
660 xo_emit("{d:serial/%-20s} ", serial != NULL ? serial : "-");
661 xo_emit("{e:serial/%s}", serial != NULL ? serial : "");
662 if ((e_status.cstat[0] & 0xf) == SES_OBJSTAT_OK && size >= 0) {
663 xo_emit("{h,hn-1000:size/%ld}{e:status/%s}",
664 size, scode2ascii(e_status.cstat[0]));
666 xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0]));
668 print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV);
670 xo_close_instance("elements");
673 free(e_devname.elm_devnames);
677 show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
680 if (e_desc.elm_desc_len <= 0) {
681 /* We don't have a label to display; might as well skip it. */
685 if (e_status.cstat[2] == 0) {
686 /* No temperature to show. */
690 xo_open_instance("elements");
691 xo_emit("{e:type/temperature_sensor}");
692 xo_emit("{:description/%s}: {:temperature/%d}{Uw:C}",
693 e_desc.elm_desc_str, e_status.cstat[2] - TEMPERATURE_OFFSET);
694 xo_close_instance("elements");
698 show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
701 if (e_desc.elm_desc_len <= 0) {
702 /* We don't have a label to display; might as well skip it. */
706 if (e_status.cstat[2] == 0) {
707 /* No voltage to show. */
711 xo_open_instance("elements");
712 xo_emit("{e:type/voltage_sensor}");
713 xo_emit("{:description/%s}: {:voltage/%.2f}{Uw:V}",
714 e_desc.elm_desc_str, be16dec(e_status.cstat + 2) / 100.0);
715 xo_close_instance("elements");
719 show(int argc, char **argv __unused)
721 encioc_string_t stri;
722 encioc_elm_status_t e_status;
723 encioc_elm_desc_t e_desc;
724 encioc_element_t *e_ptr;
726 elm_type_t prev_type;
728 unsigned int j, nobj;
734 usage(stderr, "map");
739 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
740 if (e_desc.elm_desc_str == NULL)
741 xo_err(EXIT_FAILURE, "calloc()");
743 /* Get the list of ses devices */
744 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
746 xo_errx(EXIT_FAILURE, "No SES devices found");
748 xo_set_version(SESUTIL_XO_VERSION);
749 xo_open_container("sesutil");
750 xo_open_list("enclosures");
751 for (i = 0; i < g.gl_pathc; i++) {
752 /* ensure we only got numbers after ses */
753 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
754 strlen(g.gl_pathv[i] + 8)) {
757 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
759 * Don't treat non-access errors as critical if we are
760 * accessing all devices
762 if (errno == EACCES && g.gl_pathc > 1) {
763 xo_err(EXIT_FAILURE, "unable to access SES device");
765 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
769 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
771 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
774 e_ptr = calloc(nobj, sizeof(encioc_element_t));
777 xo_err(EXIT_FAILURE, "calloc()");
780 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
782 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
785 xo_open_instance("enclosures");
792 xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5);
793 stri.bufsiz = sizeof(str);
795 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
796 xo_emit("<{t:name/%s}>; ", stri.buf);
797 stri.bufsiz = sizeof(str);
799 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
800 xo_emit("ID: {t:id/%s}", stri.buf);
803 xo_open_list("elements");
805 for (j = 0; j < nobj; j++) {
806 /* Get the status of the element */
807 memset(&e_status, 0, sizeof(e_status));
808 e_status.elm_idx = e_ptr[j].elm_idx;
809 if (ioctl(fd, ENCIOC_GETELMSTAT,
810 (caddr_t) &e_status) < 0) {
812 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
816 * Skip "Unsupported" elements; those usually precede
817 * the actual device entries and are not particularly
820 if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED)
823 /* Get the description of the element */
824 e_desc.elm_idx = e_ptr[j].elm_idx;
825 e_desc.elm_desc_len = UINT16_MAX;
826 if (ioctl(fd, ENCIOC_GETELMDESC,
827 (caddr_t) &e_desc) < 0) {
829 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
831 e_desc.elm_desc_str[e_desc.elm_desc_len] = '\0';
833 switch (e_ptr[j].elm_type) {
835 case ELMTYP_ARRAY_DEV:
836 if (e_ptr[j].elm_type != prev_type)
837 xo_emit("Desc Dev Model Ident Size/Status\n");
839 show_device(fd, e_ptr[j].elm_idx, e_status, e_desc);
840 prev_type = e_ptr[j].elm_type;
843 if (e_ptr[j].elm_type != prev_type)
844 xo_emit("\nTemperatures: ");
847 prev_type = e_ptr[j].elm_type;
848 show_therm(e_status, e_desc);
851 if (e_ptr[j].elm_type != prev_type)
852 xo_emit("\nVoltages: ");
855 prev_type = e_ptr[j].elm_type;
856 show_vom(e_status, e_desc);
860 * Ignore stuff not interesting to the user.
865 if (prev_type != (elm_type_t)-1 &&
866 prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV)
868 xo_close_list("elements");
873 free(e_desc.elm_desc_str);
874 xo_close_list("enclosures");
875 xo_close_container("sesutil");
878 return (EXIT_SUCCESS);
882 encstatus(int argc, char **argv __unused)
891 usage(stderr, "status");
894 /* Get the list of ses devices */
895 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
897 xo_errx(EXIT_FAILURE, "No SES devices found");
900 xo_set_version(SESUTIL_XO_VERSION);
901 xo_open_container("sesutil");
902 xo_open_list("enclosures");
903 for (i = 0; i < g.gl_pathc; i++) {
904 /* ensure we only got numbers after ses */
905 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
906 strlen(g.gl_pathv[i] + 8)) {
909 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
911 * Don't treat non-access errors as critical if we are
912 * accessing all devices
914 if (errno == EACCES && g.gl_pathc > 1) {
915 xo_err(EXIT_FAILURE, "unable to access SES device");
917 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
921 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
922 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
926 xo_open_instance("enclosures");
927 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
933 xo_emit("{q:status/OK}");
935 if (estat & SES_ENCSTAT_INFO) {
936 xo_emit("{lq:status/INFO}");
939 if (estat & SES_ENCSTAT_NONCRITICAL) {
942 xo_emit("{lq:status/NONCRITICAL}");
945 if (estat & SES_ENCSTAT_CRITICAL) {
948 xo_emit("{lq:status/CRITICAL}");
952 if (estat & SES_ENCSTAT_UNRECOV) {
955 xo_emit("{lq:status/UNRECOV}");
960 xo_close_instance("enclosures");
966 xo_close_list("enclosures");
967 xo_close_container("sesutil");
971 return (EXIT_SUCCESS);
973 return (EXIT_FAILURE);
978 main(int argc, char **argv)
981 struct command *cmd = NULL;
983 argc = xo_parse_args(argc, argv);
987 uflag = "/dev/ses[0-9]*";
988 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
1002 warnx("Missing command");
1003 usage(stderr, NULL);
1006 for (i = 0; i < nbcmds; i++) {
1007 if (strcmp(argv[0], cmds[i].name) == 0) {
1014 warnx("unknown command %s", argv[0]);
1015 usage(stderr, NULL);
1018 return (cmd->exec(argc, argv));