]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sesutil/sesutil.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / usr.sbin / sesutil / sesutil.c
1 /*-
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
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Edward Tomasz Napierala
9  * under sponsorship from Klara Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
20  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/endian.h>
37 #include <sys/param.h>
38 #include <sys/disk.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <getopt.h>
46 #include <glob.h>
47 #include <stdbool.h>
48 #include <stddef.h>
49 #include <stdint.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <libxo/xo.h>
55
56 #include <cam/scsi/scsi_enc.h>
57
58 #include "eltsub.h"
59
60 #define SESUTIL_XO_VERSION      "1"
61
62 #define TEMPERATURE_OFFSET      20
63
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
68
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);
76
77 static struct command {
78         const char *name;
79         const char *param;
80         const char *desc;
81         int (*exec)(int argc, char **argv);
82 } cmds[] = {
83         { "fault",
84             "(<disk>|<sesid>|all) (on|off)",
85             "Change the state of the fault LED associated with a disk",
86             fault },
87         { "locate",
88             "(<disk>|<sesid>|all) (on|off)",
89             "Change the state of the locate LED associated with a disk",
90             locate },
91         { "map", "",
92             "Print a map of the devices managed by the enclosure", objmap } ,
93         { "show", "",
94             "Print a human-friendly summary of the enclosure", show } ,
95         { "status", "", "Print the status of the enclosure",
96             encstatus },
97 };
98
99 static const int nbcmds = nitems(cmds);
100 static const char *uflag;
101
102 static void
103 usage(FILE *out, const char *subcmd)
104 {
105         int i;
106
107         if (subcmd == NULL) {
108                 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
109                     getprogname());
110                 fprintf(out, "Commands supported:\n");
111         }
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);
118                                 break;
119                         }
120                         continue;
121                 }
122                 fprintf(out, "    %-12s%s\n\t\t%s\n\n", cmds[i].name,
123                     cmds[i].param, cmds[i].desc);
124         }
125
126         exit(EXIT_FAILURE);
127 }
128
129 static void
130 do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault)
131 {
132         int state = onoff ? 1 : 0;
133         encioc_elm_status_t o;
134         struct ses_ctrl_dev_slot *slot;
135
136         o.elm_idx = idx;
137         if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
138                 close(fd);
139                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
140         }
141         slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
142         switch (type) {
143         case ELMTYP_DEVICE:
144         case ELMTYP_ARRAY_DEV:
145                 ses_ctrl_common_set_select(&slot->common, 1);
146                 if (setfault)
147                         ses_ctrl_dev_slot_set_rqst_fault(slot, state);
148                 else
149                         ses_ctrl_dev_slot_set_rqst_ident(slot, state);
150                 break;
151         default:
152                 return;
153         }
154         if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
155                 close(fd);
156                 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
157         }
158 }
159
160 static bool
161 disk_match(const char *devnames, const char *disk, size_t len)
162 {
163         const char *dname;
164
165         dname = devnames;
166         while ((dname = strstr(dname, disk)) != NULL) {
167                 if (dname[len] == '\0' || dname[len] == ',') {
168                         return (true);
169                 }
170                 dname++;
171         }
172
173         return (false);
174 }
175
176 static int
177 sesled(int argc, char **argv, bool setfault)
178 {
179         encioc_elm_devnames_t objdn;
180         encioc_element_t *objp;
181         glob_t g;
182         char *disk, *endptr;
183         size_t len, i, ndisks;
184         int fd;
185         unsigned int nobj, j, sesid;
186         bool all, isses, onoff;
187
188         isses = false;
189         all = false;
190         onoff = false;
191
192         if (argc != 3) {
193                 usage(stderr, (setfault ? "fault" : "locate"));
194         }
195
196         disk = argv[1];
197
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"));
205                 }
206                 isses = true;
207         }
208
209         if (strcmp(argv[2], "on") == 0) {
210                 onoff = true;
211         } else if (strcmp(argv[2], "off") == 0) {
212                 onoff = false;
213         } else {
214                 usage(stderr, (setfault ? "fault" : "locate"));
215         }
216
217         if (strcmp(disk, "all") == 0) {
218                 all = true;
219         }
220         len = strlen(disk);
221
222         /* Get the list of ses devices */
223         if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
224             GLOB_NOMATCH) {
225                 globfree(&g);
226                 xo_errx(EXIT_FAILURE, "No SES devices found");
227         }
228
229         ndisks = 0;
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)) {
234                         continue;
235                 }
236                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
237                         /*
238                          * Don't treat non-access errors as critical if we are
239                          * accessing all devices
240                          */
241                         if (errno == EACCES && g.gl_pathc > 1) {
242                                 xo_err(EXIT_FAILURE, "unable to access SES device");
243                         }
244                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
245                         continue;
246                 }
247
248                 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
249                         close(fd);
250                         xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
251                 }
252
253                 objp = calloc(nobj, sizeof(encioc_element_t));
254                 if (objp == NULL) {
255                         close(fd);
256                         xo_err(EXIT_FAILURE, "calloc()");
257                 }
258
259                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
260                         free(objp);
261                         close(fd);
262                         xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
263                 }
264
265                 if (isses) {
266                         if (sesid >= nobj) {
267                                 free(objp);
268                                 close(fd);
269                                 xo_errx(EXIT_FAILURE,
270                                      "Requested SES ID does not exist");
271                         }
272                         do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
273                         ndisks++;
274                         free(objp);
275                         close(fd);
276                         break;
277                 }
278                 for (j = 0; j < nobj; j++) {
279                         const int devnames_size = 128;
280                         char devnames[devnames_size];
281
282                         if (all) {
283                                 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
284                                     onoff, setfault);
285                                 continue;
286                         }
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) {
294                                 continue;
295                         }
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,
299                                             onoff, setfault);
300                                         ndisks++;
301                                         break;
302                                 }
303                         }
304                 }
305                 free(objp);
306                 close(fd);
307         }
308         globfree(&g);
309         if (ndisks == 0 && all == false) {
310                 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
311                     disk);
312         }
313
314         return (EXIT_SUCCESS);
315 }
316
317 static int
318 locate(int argc, char **argv)
319 {
320
321         return (sesled(argc, argv, false));
322 }
323
324 static int
325 fault(int argc, char **argv)
326 {
327
328         return (sesled(argc, argv, true));
329 }
330
331 static void
332 sesutil_print(int *style, const char *fmt, ...)
333 {
334         va_list args;
335
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;
343         }
344
345         if (*style == PRINT_STYLE_DASHED_2)
346                 xo_emit("\t\t- ");
347         else if (*style == PRINT_STYLE_CSV_2)
348                 xo_emit(", ");
349         va_start(args, fmt);
350         xo_emit_hv(NULL, fmt, args);
351         va_end(args);
352         if (*style == PRINT_STYLE_DASHED_2)
353                 xo_emit("\n");
354 }
355
356 static void
357 print_extra_status(int eletype, u_char *cstat, int style)
358 {
359
360         if (cstat[0] & 0x40) {
361                 sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure");
362         }
363         if (cstat[0] & 0x20) {
364                 sesutil_print(&style, "{e:disabled/true} Disabled");
365         }
366         if (cstat[0] & 0x10) {
367                 sesutil_print(&style, "{e:swapped/true} Swapped");
368         }
369         switch (eletype) {
370         case ELMTYP_DEVICE:
371         case ELMTYP_ARRAY_DEV:
372                 if (cstat[2] & 0x02) {
373                         sesutil_print(&style, "LED={q:led/locate}");
374                 }
375                 if (cstat[2] & 0x20) {
376                         sesutil_print(&style, "LED={q:led/fault}");
377                 }
378                 break;
379         case ELMTYP_FAN:
380                 sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}",
381                     (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
382                 break;
383         case ELMTYP_THERM:
384                 if (cstat[2]) {
385                         sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}",
386                             cstat[2] - TEMPERATURE_OFFSET);
387                 } else {
388                         sesutil_print(&style, "Temperature: -{q:temperature/reserved}");
389                 }
390                 break;
391         case ELMTYP_VOM:
392                 sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}",
393                     be16dec(cstat + 2) / 100.0);
394                 break;
395         }
396         if (style) {
397                 xo_close_container("extra_status");
398         }
399 }
400
401 static int
402 objmap(int argc, char **argv __unused)
403 {
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;
409         glob_t g;
410         int fd;
411         unsigned int j, nobj;
412         size_t i;
413         char str[32];
414
415         if (argc != 1) {
416                 usage(stderr, "map");
417         }
418
419         /* Get the list of ses devices */
420         if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
421                 globfree(&g);
422                 xo_errx(EXIT_FAILURE, "No SES devices found");
423         }
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)) {
431                         continue;
432                 }
433                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
434                         /*
435                          * Don't treat non-access errors as critical if we are
436                          * accessing all devices
437                          */
438                         if (errno == EACCES && g.gl_pathc > 1) {
439                                 xo_err(EXIT_FAILURE, "unable to access SES device");
440                         }
441                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
442                         continue;
443                 }
444
445                 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
446                         close(fd);
447                         xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
448                 }
449
450                 e_ptr = calloc(nobj, sizeof(encioc_element_t));
451                 if (e_ptr == NULL) {
452                         close(fd);
453                         xo_err(EXIT_FAILURE, "calloc()");
454                 }
455
456                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
457                         close(fd);
458                         xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
459                 }
460
461                 xo_open_instance("enclosures");
462                 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
463                 stri.bufsiz = sizeof(str);
464                 stri.buf = &str[0];
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);
468                 stri.buf = &str[0];
469                 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
470                         xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
471
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) {
479                                 close(fd);
480                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
481                         }
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) {
488                                 close(fd);
489                                 xo_err(EXIT_FAILURE, "calloc()");
490                         }
491                         if (ioctl(fd, ENCIOC_GETELMDESC,
492                             (caddr_t) &e_desc) < 0) {
493                                 close(fd);
494                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
495                         }
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) {
502                                 close(fd);
503                                 xo_err(EXIT_FAILURE, "calloc()");
504                         }
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';
509                         }
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],
516                             e_status.cstat[3]);
517                         if (e_desc.elm_desc_len > 0) {
518                                 xo_emit("\t\tDescription: {:description/%s}\n",
519                                     e_desc.elm_desc_str);
520                         }
521                         if (e_devname.elm_names_len > 0) {
522                                 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
523                                     e_devname.elm_devnames);
524                         }
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);
528                 }
529                 xo_close_list("elements");
530                 free(e_ptr);
531                 close(fd);
532         }
533         globfree(&g);
534         xo_close_list("enclosures");
535         xo_close_container("sesutil");
536         xo_finish();
537
538         return (EXIT_SUCCESS);
539 }
540
541 /*
542  * Get rid of the 'passN' devices, unless there's nothing else to show.
543  */
544 static void
545 skip_pass_devices(char *devnames, size_t devnameslen)
546 {
547         char *dev, devs[128], passes[128], *tmp;
548
549         devs[0] = passes[0] = '\0';
550         tmp = devnames;
551
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));
557                 } else {
558                         if (devs[0] != '\0')
559                                 strlcat(devs, ",", sizeof(devs));
560                         strlcat(devs, dev, sizeof(devs));
561                 }
562         }
563         strlcpy(devnames, devs, devnameslen);
564         if (devnames[0] == '\0')
565                 strlcpy(devnames, passes, devnameslen);
566 }
567
568 static void
569 fetch_device_details(char *devnames, char **model, char **serial, off_t *size)
570 {
571         char ident[DISK_IDENT_SIZE];
572         struct diocgattr_arg arg;
573         char *tmp;
574         off_t mediasize;
575         int comma;
576         int fd;
577
578         comma = (int)strcspn(devnames, ",");
579         asprintf(&tmp, "/dev/%.*s", comma, devnames);
580         if (tmp == NULL)
581                 err(1, "asprintf");
582         fd = open(tmp, O_RDONLY);
583         free(tmp);
584         if (fd < 0) {
585                 /*
586                  * This can happen with a disk so broken it cannot
587                  * be probed by GEOM.
588                  */
589                 *model = strdup("?");
590                 *serial = strdup("?");
591                 *size = -1;
592                 close(fd);
593                 return;
594         }
595
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);
600         else
601                 *model = NULL;
602
603         if (ioctl(fd, DIOCGIDENT, ident) == 0)
604                 *serial = strdup(ident);
605         else
606                 *serial = NULL;
607
608         if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0)
609                 *size = mediasize;
610         else
611                 *size = -1;
612         close(fd);
613 }
614
615 static void
616 show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
617 {
618         encioc_elm_devnames_t e_devname;
619         char *model, *serial;
620         off_t size;
621
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) {
628                 close(fd);
629                 xo_err(EXIT_FAILURE, "calloc()");
630         }
631
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';
636                 model = NULL;
637                 serial = NULL;
638                 size = -1;
639         } else {
640                 skip_pass_devices(e_devname.elm_devnames, 128);
641                 fetch_device_details(e_devname.elm_devnames, &model, &serial, &size);
642         }
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]));
656         } else {
657                 xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0]));
658         }
659         print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV);
660         xo_emit("\n");
661         xo_close_instance("elements");
662         free(e_devname.elm_devnames);
663 }
664
665 static void
666 show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
667 {
668
669         if (e_desc.elm_desc_len <= 0) {
670                 /* We don't have a label to display; might as well skip it. */
671                 return;
672         }
673
674         if (e_status.cstat[2] == 0) {
675                 /* No temperature to show. */
676                 return;
677         }
678
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");
684 }
685
686 static void
687 show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
688 {
689
690         if (e_desc.elm_desc_len <= 0) {
691                 /* We don't have a label to display; might as well skip it. */
692                 return;
693         }
694
695         if (e_status.cstat[2] == 0) {
696                 /* No voltage to show. */
697                 return;
698         }
699
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");
705 }
706
707 static int
708 show(int argc, char **argv __unused)
709 {
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;
714         glob_t g;
715         elm_type_t prev_type;
716         int fd;
717         unsigned int j, nobj;
718         size_t i;
719         bool first_ses;
720         char str[32];
721
722         if (argc != 1) {
723                 usage(stderr, "map");
724         }
725
726         first_ses = true;
727
728         /* Get the list of ses devices */
729         if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
730                 globfree(&g);
731                 xo_errx(EXIT_FAILURE, "No SES devices found");
732         }
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)) {
740                         continue;
741                 }
742                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
743                         /*
744                          * Don't treat non-access errors as critical if we are
745                          * accessing all devices
746                          */
747                         if (errno == EACCES && g.gl_pathc > 1) {
748                                 xo_err(EXIT_FAILURE, "unable to access SES device");
749                         }
750                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
751                         continue;
752                 }
753
754                 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
755                         close(fd);
756                         xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
757                 }
758
759                 e_ptr = calloc(nobj, sizeof(encioc_element_t));
760                 if (e_ptr == NULL) {
761                         close(fd);
762                         xo_err(EXIT_FAILURE, "calloc()");
763                 }
764
765                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
766                         close(fd);
767                         xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
768                 }
769
770                 xo_open_instance("enclosures");
771
772                 if (first_ses)
773                         first_ses = false;
774                 else
775                         xo_emit("\n");
776
777                 xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5);
778                 stri.bufsiz = sizeof(str);
779                 stri.buf = &str[0];
780                 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
781                         xo_emit("<{t:name/%s}>; ", stri.buf);
782                 stri.bufsiz = sizeof(str);
783                 stri.buf = &str[0];
784                 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
785                         xo_emit("ID: {t:id/%s}", stri.buf);
786                 xo_emit("\n");
787
788                 xo_open_list("elements");
789                 prev_type = -1;
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) {
796                                 close(fd);
797                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
798                         }
799
800                         /*
801                          * Skip "Unsupported" elements; those usually precede
802                          * the actual device entries and are not particularly
803                          * interesting.
804                          */
805                         if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED)
806                                 continue;
807
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) {
814                                 close(fd);
815                                 xo_err(EXIT_FAILURE, "calloc()");
816                         }
817                         if (ioctl(fd, ENCIOC_GETELMDESC,
818                             (caddr_t) &e_desc) < 0) {
819                                 close(fd);
820                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
821                         }
822
823                         switch (e_ptr[j].elm_type) {
824                         case ELMTYP_DEVICE:
825                         case ELMTYP_ARRAY_DEV:
826                                 if (e_ptr[j].elm_type != prev_type)
827                                         xo_emit("Desc     Dev     Model                     Ident                Size/Status\n");
828
829                                 show_device(fd, e_ptr[j].elm_idx, e_status, e_desc);
830                                 prev_type = e_ptr[j].elm_type;
831                                 break;
832                         case ELMTYP_THERM:
833                                 if (e_ptr[j].elm_type != prev_type)
834                                         xo_emit("\nTemperatures: ");
835                                 else
836                                         xo_emit(", ");
837                                 prev_type = e_ptr[j].elm_type;
838                                 show_therm(e_status, e_desc);
839                                 break;
840                         case ELMTYP_VOM:
841                                 if (e_ptr[j].elm_type != prev_type)
842                                         xo_emit("\nVoltages: ");
843                                 else
844                                         xo_emit(", ");
845                                 prev_type = e_ptr[j].elm_type;
846                                 show_vom(e_status, e_desc);
847                                 break;
848                         default:
849                                 /*
850                                  * Ignore stuff not interesting to the user.
851                                  */
852                                 break;
853                         }
854                 }
855                 if (prev_type != (elm_type_t)-1 &&
856                     prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV)
857                         xo_emit("\n");
858                 xo_close_list("elements");
859                 free(e_ptr);
860                 close(fd);
861         }
862         globfree(&g);
863         xo_close_list("enclosures");
864         xo_close_container("sesutil");
865         xo_finish();
866
867         return (EXIT_SUCCESS);
868 }
869
870 static int
871 encstatus(int argc, char **argv __unused)
872 {
873         glob_t g;
874         int fd, status;
875         size_t i, e;
876         u_char estat;
877
878         status = 0;
879         if (argc != 1) {
880                 usage(stderr, "status");
881         }
882
883         /* Get the list of ses devices */
884         if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
885                 globfree(&g);
886                 xo_errx(EXIT_FAILURE, "No SES devices found");
887         }
888
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)) {
896                         continue;
897                 }
898                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
899                         /*
900                          * Don't treat non-access errors as critical if we are
901                          * accessing all devices
902                          */
903                         if (errno == EACCES && g.gl_pathc > 1) {
904                                 xo_err(EXIT_FAILURE, "unable to access SES device");
905                         }
906                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
907                         continue;
908                 }
909
910                 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
911                         xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
912                         close(fd);
913                 }
914
915                 xo_open_instance("enclosures");
916                 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
917                 e = 0;
918                 if (estat == 0) {
919                         if (status == 0) {
920                                 status = 1;
921                         }
922                         xo_emit("{q:status/OK}");
923                 } else {
924                         if (estat & SES_ENCSTAT_INFO) {
925                                 xo_emit("{lq:status/INFO}");
926                                 e++;
927                         }
928                         if (estat & SES_ENCSTAT_NONCRITICAL) {
929                                 if (e)
930                                         xo_emit(",");
931                                 xo_emit("{lq:status/NONCRITICAL}");
932                                 e++;
933                         }
934                         if (estat & SES_ENCSTAT_CRITICAL) {
935                                 if (e)
936                                         xo_emit(",");
937                                 xo_emit("{lq:status/CRITICAL}");
938                                 e++;
939                                 status = -1;
940                         }
941                         if (estat & SES_ENCSTAT_UNRECOV) {
942                                 if (e)
943                                         xo_emit(",");
944                                 xo_emit("{lq:status/UNRECOV}");
945                                 e++;
946                                 status = -1;
947                         }
948                 }
949                 xo_close_instance("enclosures");
950                 xo_emit("\n");
951                 close(fd);
952         }
953         globfree(&g);
954
955         xo_close_list("enclosures");
956         xo_close_container("sesutil");
957         xo_finish();
958
959         if (status == 1) {
960                 return (EXIT_SUCCESS);
961         } else {
962                 return (EXIT_FAILURE);
963         }
964 }
965
966 int
967 main(int argc, char **argv)
968 {
969         int i, ch;
970         struct command *cmd = NULL;
971
972         argc = xo_parse_args(argc, argv);
973         if (argc < 0)
974                 exit(1);
975
976         uflag = "/dev/ses[0-9]*";
977         while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
978                 switch (ch) {
979                 case 'u':
980                         uflag = optarg;
981                         break;
982                 case '?':
983                 default:
984                         usage(stderr, NULL);
985                 }
986         }
987         argc -= optind;
988         argv += optind;
989
990         if (argc < 1) {
991                 warnx("Missing command");
992                 usage(stderr, NULL);
993         }
994
995         for (i = 0; i < nbcmds; i++) {
996                 if (strcmp(argv[0], cmds[i].name) == 0) {
997                         cmd = &cmds[i];
998                         break;
999                 }
1000         }
1001
1002         if (cmd == NULL) {
1003                 warnx("unknown command %s", argv[0]);
1004                 usage(stderr, NULL);
1005         }
1006
1007         return (cmd->exec(argc, argv));
1008 }