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 slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
144 case ELMTYP_ARRAY_DEV:
145 ses_ctrl_common_set_select(&slot->common, 1);
147 ses_ctrl_dev_slot_set_rqst_fault(slot, state);
149 ses_ctrl_dev_slot_set_rqst_ident(slot, state);
154 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
156 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
161 disk_match(const char *devnames, const char *disk, size_t len)
166 while ((dname = strstr(dname, disk)) != NULL) {
167 if (dname[len] == '\0' || dname[len] == ',') {
177 sesled(int argc, char **argv, bool setfault)
179 encioc_elm_devnames_t objdn;
180 encioc_element_t *objp;
183 size_t len, i, ndisks;
185 unsigned int nobj, j, sesid;
186 bool all, isses, onoff;
193 usage(stderr, (setfault ? "fault" : "locate"));
198 sesid = strtoul(disk, &endptr, 10);
199 if (*endptr == '\0') {
200 endptr = strrchr(uflag, '*');
201 if (endptr != NULL && *endptr == '*') {
202 xo_warnx("Must specifying a SES device (-u) to use a SES "
203 "id# to identify a disk");
204 usage(stderr, (setfault ? "fault" : "locate"));
209 if (strcmp(argv[2], "on") == 0) {
211 } else if (strcmp(argv[2], "off") == 0) {
214 usage(stderr, (setfault ? "fault" : "locate"));
217 if (strcmp(disk, "all") == 0) {
222 /* Get the list of ses devices */
223 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
226 xo_errx(EXIT_FAILURE, "No SES devices found");
230 for (i = 0; i < g.gl_pathc; i++) {
231 /* ensure we only got numbers after ses */
232 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
233 strlen(g.gl_pathv[i] + 8)) {
236 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
238 * Don't treat non-access errors as critical if we are
239 * accessing all devices
241 if (errno == EACCES && g.gl_pathc > 1) {
242 xo_err(EXIT_FAILURE, "unable to access SES device");
244 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
248 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
250 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
253 objp = calloc(nobj, sizeof(encioc_element_t));
256 xo_err(EXIT_FAILURE, "calloc()");
259 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
262 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
269 xo_errx(EXIT_FAILURE,
270 "Requested SES ID does not exist");
272 do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
278 for (j = 0; j < nobj; j++) {
279 const int devnames_size = 128;
280 char devnames[devnames_size];
283 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
287 memset(&objdn, 0, sizeof(objdn));
288 memset(devnames, 0, devnames_size);
289 objdn.elm_idx = objp[j].elm_idx;
290 objdn.elm_names_size = devnames_size;
291 objdn.elm_devnames = devnames;
292 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
293 (caddr_t) &objdn) <0) {
296 if (objdn.elm_names_len > 0) {
297 if (disk_match(objdn.elm_devnames, disk, len)) {
298 do_led(fd, objdn.elm_idx, objp[j].elm_type,
309 if (ndisks == 0 && all == false) {
310 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
314 return (EXIT_SUCCESS);
318 locate(int argc, char **argv)
321 return (sesled(argc, argv, false));
325 fault(int argc, char **argv)
328 return (sesled(argc, argv, true));
332 sesutil_print(int *style, const char *fmt, ...)
336 if (*style == PRINT_STYLE_DASHED) {
337 xo_open_container("extra_status");
338 xo_emit("\t\tExtra status:\n");
339 *style = PRINT_STYLE_DASHED_2;
340 } else if (*style == PRINT_STYLE_CSV) {
341 xo_open_container("extra_status");
342 *style = PRINT_STYLE_CSV_2;
345 if (*style == PRINT_STYLE_DASHED_2)
347 else if (*style == PRINT_STYLE_CSV_2)
350 xo_emit_hv(NULL, fmt, args);
352 if (*style == PRINT_STYLE_DASHED_2)
357 print_extra_status(int eletype, u_char *cstat, int style)
360 if (cstat[0] & 0x40) {
361 sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure");
363 if (cstat[0] & 0x20) {
364 sesutil_print(&style, "{e:disabled/true} Disabled");
366 if (cstat[0] & 0x10) {
367 sesutil_print(&style, "{e:swapped/true} Swapped");
371 case ELMTYP_ARRAY_DEV:
372 if (cstat[2] & 0x02) {
373 sesutil_print(&style, "LED={q:led/locate}");
375 if (cstat[2] & 0x20) {
376 sesutil_print(&style, "LED={q:led/fault}");
380 sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}",
381 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
385 sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}",
386 cstat[2] - TEMPERATURE_OFFSET);
388 sesutil_print(&style, "Temperature: -{q:temperature/reserved}");
392 sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}",
393 be16dec(cstat + 2) / 100.0);
397 xo_close_container("extra_status");
402 objmap(int argc, char **argv __unused)
404 encioc_string_t stri;
405 encioc_elm_devnames_t e_devname;
406 encioc_elm_status_t e_status;
407 encioc_elm_desc_t e_desc;
408 encioc_element_t *e_ptr;
411 unsigned int j, nobj;
416 usage(stderr, "map");
419 /* Get the list of ses devices */
420 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
422 xo_errx(EXIT_FAILURE, "No SES devices found");
424 xo_set_version(SESUTIL_XO_VERSION);
425 xo_open_container("sesutil");
426 xo_open_list("enclosures");
427 for (i = 0; i < g.gl_pathc; i++) {
428 /* ensure we only got numbers after ses */
429 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
430 strlen(g.gl_pathv[i] + 8)) {
433 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
435 * Don't treat non-access errors as critical if we are
436 * accessing all devices
438 if (errno == EACCES && g.gl_pathc > 1) {
439 xo_err(EXIT_FAILURE, "unable to access SES device");
441 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
445 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
447 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
450 e_ptr = calloc(nobj, sizeof(encioc_element_t));
453 xo_err(EXIT_FAILURE, "calloc()");
456 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
458 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
461 xo_open_instance("enclosures");
462 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
463 stri.bufsiz = sizeof(str);
465 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
466 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
467 stri.bufsiz = sizeof(str);
469 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
470 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
472 xo_open_list("elements");
473 for (j = 0; j < nobj; j++) {
474 /* Get the status of the element */
475 memset(&e_status, 0, sizeof(e_status));
476 e_status.elm_idx = e_ptr[j].elm_idx;
477 if (ioctl(fd, ENCIOC_GETELMSTAT,
478 (caddr_t) &e_status) < 0) {
480 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
482 /* Get the description of the element */
483 memset(&e_desc, 0, sizeof(e_desc));
484 e_desc.elm_idx = e_ptr[j].elm_idx;
485 e_desc.elm_desc_len = UINT16_MAX;
486 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
487 if (e_desc.elm_desc_str == NULL) {
489 xo_err(EXIT_FAILURE, "calloc()");
491 if (ioctl(fd, ENCIOC_GETELMDESC,
492 (caddr_t) &e_desc) < 0) {
494 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
496 /* Get the device name(s) of the element */
497 memset(&e_devname, 0, sizeof(e_devname));
498 e_devname.elm_idx = e_ptr[j].elm_idx;
499 e_devname.elm_names_size = 128;
500 e_devname.elm_devnames = calloc(128, sizeof(char));
501 if (e_devname.elm_devnames == NULL) {
503 xo_err(EXIT_FAILURE, "calloc()");
505 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
506 (caddr_t) &e_devname) <0) {
507 /* We don't care if this fails */
508 e_devname.elm_devnames[0] = '\0';
510 xo_open_instance("elements");
511 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
512 geteltnm(e_ptr[j].elm_type));
513 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
514 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
515 e_status.cstat[1], e_status.cstat[2],
517 if (e_desc.elm_desc_len > 0) {
518 xo_emit("\t\tDescription: {:description/%s}\n",
519 e_desc.elm_desc_str);
521 if (e_devname.elm_names_len > 0) {
522 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
523 e_devname.elm_devnames);
525 print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED);
526 xo_close_instance("elements");
527 free(e_devname.elm_devnames);
529 xo_close_list("elements");
534 xo_close_list("enclosures");
535 xo_close_container("sesutil");
538 return (EXIT_SUCCESS);
542 * Get rid of the 'passN' devices, unless there's nothing else to show.
545 skip_pass_devices(char *devnames, size_t devnameslen)
547 char *dev, devs[128], passes[128], *tmp;
549 devs[0] = passes[0] = '\0';
552 while ((dev = strsep(&tmp, ",")) != NULL) {
553 if (strncmp(dev, "pass", 4) == 0) {
554 if (passes[0] != '\0')
555 strlcat(passes, ",", sizeof(passes));
556 strlcat(passes, dev, sizeof(passes));
559 strlcat(devs, ",", sizeof(devs));
560 strlcat(devs, dev, sizeof(devs));
563 strlcpy(devnames, devs, devnameslen);
564 if (devnames[0] == '\0')
565 strlcpy(devnames, passes, devnameslen);
569 fetch_device_details(char *devnames, char **model, char **serial, off_t *size)
571 char ident[DISK_IDENT_SIZE];
572 struct diocgattr_arg arg;
578 comma = (int)strcspn(devnames, ",");
579 asprintf(&tmp, "/dev/%.*s", comma, devnames);
582 fd = open(tmp, O_RDONLY);
586 * This can happen with a disk so broken it cannot
589 *model = strdup("?");
590 *serial = strdup("?");
596 strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
597 arg.len = sizeof(arg.value.str);
598 if (ioctl(fd, DIOCGATTR, &arg) == 0)
599 *model = strdup(arg.value.str);
603 if (ioctl(fd, DIOCGIDENT, ident) == 0)
604 *serial = strdup(ident);
608 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0)
616 show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
618 encioc_elm_devnames_t e_devname;
619 char *model, *serial;
622 /* Get the device name(s) of the element */
623 memset(&e_devname, 0, sizeof(e_devname));
624 e_devname.elm_idx = elm_idx;
625 e_devname.elm_names_size = 128;
626 e_devname.elm_devnames = calloc(128, sizeof(char));
627 if (e_devname.elm_devnames == NULL) {
629 xo_err(EXIT_FAILURE, "calloc()");
632 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
633 (caddr_t) &e_devname) < 0) {
634 /* We don't care if this fails */
635 e_devname.elm_devnames[0] = '\0';
640 skip_pass_devices(e_devname.elm_devnames, 128);
641 fetch_device_details(e_devname.elm_devnames, &model, &serial, &size);
643 xo_open_instance("elements");
644 xo_emit("{e:type/device_slot}");
645 xo_emit("{d:description/%-8s} ", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "-");
646 xo_emit("{e:description/%-8s}", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "");
647 xo_emit("{d:device_names/%-7s} ", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "-");
648 xo_emit("{e:device_names/%s}", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "");
649 xo_emit("{d:model/%-25s} ", model ? model : "-");
650 xo_emit("{e:model/%s}", model ? model : "");
651 xo_emit("{d:serial/%-20s} ", serial != NULL ? serial : "-");
652 xo_emit("{e:serial/%s}", serial != NULL ? serial : "");
653 if (e_status.cstat[0] == SES_OBJSTAT_OK && size >= 0) {
654 xo_emit("{h,hn-1000:size/%ld}{e:status/%s}",
655 size, scode2ascii(e_status.cstat[0]));
657 xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0]));
659 print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV);
661 xo_close_instance("elements");
662 free(e_devname.elm_devnames);
666 show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
669 if (e_desc.elm_desc_len <= 0) {
670 /* We don't have a label to display; might as well skip it. */
674 if (e_status.cstat[2] == 0) {
675 /* No temperature to show. */
679 xo_open_instance("elements");
680 xo_emit("{e:type/temperature_sensor}");
681 xo_emit("{:description/%s}: {:temperature/%d}{Uw:C}",
682 e_desc.elm_desc_str, e_status.cstat[2] - TEMPERATURE_OFFSET);
683 xo_close_instance("elements");
687 show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
690 if (e_desc.elm_desc_len <= 0) {
691 /* We don't have a label to display; might as well skip it. */
695 if (e_status.cstat[2] == 0) {
696 /* No voltage to show. */
700 xo_open_instance("elements");
701 xo_emit("{e:type/voltage_sensor}");
702 xo_emit("{:description/%s}: {:voltage/%.2f}{Uw:V}",
703 e_desc.elm_desc_str, be16dec(e_status.cstat + 2) / 100.0);
704 xo_close_instance("elements");
708 show(int argc, char **argv __unused)
710 encioc_string_t stri;
711 encioc_elm_status_t e_status;
712 encioc_elm_desc_t e_desc;
713 encioc_element_t *e_ptr;
715 elm_type_t prev_type;
717 unsigned int j, nobj;
723 usage(stderr, "map");
728 /* Get the list of ses devices */
729 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
731 xo_errx(EXIT_FAILURE, "No SES devices found");
733 xo_set_version(SESUTIL_XO_VERSION);
734 xo_open_container("sesutil");
735 xo_open_list("enclosures");
736 for (i = 0; i < g.gl_pathc; i++) {
737 /* ensure we only got numbers after ses */
738 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
739 strlen(g.gl_pathv[i] + 8)) {
742 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
744 * Don't treat non-access errors as critical if we are
745 * accessing all devices
747 if (errno == EACCES && g.gl_pathc > 1) {
748 xo_err(EXIT_FAILURE, "unable to access SES device");
750 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
754 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
756 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
759 e_ptr = calloc(nobj, sizeof(encioc_element_t));
762 xo_err(EXIT_FAILURE, "calloc()");
765 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
767 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
770 xo_open_instance("enclosures");
777 xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5);
778 stri.bufsiz = sizeof(str);
780 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
781 xo_emit("<{t:name/%s}>; ", stri.buf);
782 stri.bufsiz = sizeof(str);
784 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
785 xo_emit("ID: {t:id/%s}", stri.buf);
788 xo_open_list("elements");
790 for (j = 0; j < nobj; j++) {
791 /* Get the status of the element */
792 memset(&e_status, 0, sizeof(e_status));
793 e_status.elm_idx = e_ptr[j].elm_idx;
794 if (ioctl(fd, ENCIOC_GETELMSTAT,
795 (caddr_t) &e_status) < 0) {
797 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
801 * Skip "Unsupported" elements; those usually precede
802 * the actual device entries and are not particularly
805 if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED)
808 /* Get the description of the element */
809 memset(&e_desc, 0, sizeof(e_desc));
810 e_desc.elm_idx = e_ptr[j].elm_idx;
811 e_desc.elm_desc_len = UINT16_MAX;
812 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
813 if (e_desc.elm_desc_str == NULL) {
815 xo_err(EXIT_FAILURE, "calloc()");
817 if (ioctl(fd, ENCIOC_GETELMDESC,
818 (caddr_t) &e_desc) < 0) {
820 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
823 switch (e_ptr[j].elm_type) {
825 case ELMTYP_ARRAY_DEV:
826 if (e_ptr[j].elm_type != prev_type)
827 xo_emit("Desc Dev Model Ident Size/Status\n");
829 show_device(fd, e_ptr[j].elm_idx, e_status, e_desc);
830 prev_type = e_ptr[j].elm_type;
833 if (e_ptr[j].elm_type != prev_type)
834 xo_emit("\nTemperatures: ");
837 prev_type = e_ptr[j].elm_type;
838 show_therm(e_status, e_desc);
841 if (e_ptr[j].elm_type != prev_type)
842 xo_emit("\nVoltages: ");
845 prev_type = e_ptr[j].elm_type;
846 show_vom(e_status, e_desc);
850 * Ignore stuff not interesting to the user.
855 if (prev_type != (elm_type_t)-1 &&
856 prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV)
858 xo_close_list("elements");
863 xo_close_list("enclosures");
864 xo_close_container("sesutil");
867 return (EXIT_SUCCESS);
871 encstatus(int argc, char **argv __unused)
880 usage(stderr, "status");
883 /* Get the list of ses devices */
884 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
886 xo_errx(EXIT_FAILURE, "No SES devices found");
889 xo_set_version(SESUTIL_XO_VERSION);
890 xo_open_container("sesutil");
891 xo_open_list("enclosures");
892 for (i = 0; i < g.gl_pathc; i++) {
893 /* ensure we only got numbers after ses */
894 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
895 strlen(g.gl_pathv[i] + 8)) {
898 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
900 * Don't treat non-access errors as critical if we are
901 * accessing all devices
903 if (errno == EACCES && g.gl_pathc > 1) {
904 xo_err(EXIT_FAILURE, "unable to access SES device");
906 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
910 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
911 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
915 xo_open_instance("enclosures");
916 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
922 xo_emit("{q:status/OK}");
924 if (estat & SES_ENCSTAT_INFO) {
925 xo_emit("{lq:status/INFO}");
928 if (estat & SES_ENCSTAT_NONCRITICAL) {
931 xo_emit("{lq:status/NONCRITICAL}");
934 if (estat & SES_ENCSTAT_CRITICAL) {
937 xo_emit("{lq:status/CRITICAL}");
941 if (estat & SES_ENCSTAT_UNRECOV) {
944 xo_emit("{lq:status/UNRECOV}");
949 xo_close_instance("enclosures");
955 xo_close_list("enclosures");
956 xo_close_container("sesutil");
960 return (EXIT_SUCCESS);
962 return (EXIT_FAILURE);
967 main(int argc, char **argv)
970 struct command *cmd = NULL;
972 argc = xo_parse_args(argc, argv);
976 uflag = "/dev/ses[0-9]*";
977 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
991 warnx("Missing command");
995 for (i = 0; i < nbcmds; i++) {
996 if (strcmp(argv[0], cmds[i].name) == 0) {
1003 warnx("unknown command %s", argv[0]);
1004 usage(stderr, NULL);
1007 return (cmd->exec(argc, argv));