xref: /freebsd-13.1/usr.sbin/sesutil/sesutil.c (revision 5b8c6776)
1 /*-
2  * Copyright (c) 2019 Klara Inc.
3  * Copyright (c) 2015 Baptiste Daroussin <[email protected]>
4  * Copyright (c) 2015 Allan Jude <[email protected]>
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
usage(FILE * out,const char * subcmd)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
do_led(int fd,unsigned int idx,elm_type_t type,bool onoff,bool setfault)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 	ses_status_to_ctrl(type, &o.cstat[0]);
142 	switch (type) {
143 	case ELMTYP_DEVICE:
144 	case ELMTYP_ARRAY_DEV:
145 		slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
146 		ses_ctrl_common_set_select(&slot->common, 1);
147 		if (setfault)
148 			ses_ctrl_dev_slot_set_rqst_fault(slot, state);
149 		else
150 			ses_ctrl_dev_slot_set_rqst_ident(slot, state);
151 		break;
152 	default:
153 		return;
154 	}
155 	if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
156 		close(fd);
157 		xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
158 	}
159 }
160 
161 static bool
disk_match(const char * devnames,const char * disk,size_t len)162 disk_match(const char *devnames, const char *disk, size_t len)
163 {
164 	const char *dname;
165 
166 	dname = devnames;
167 	while ((dname = strstr(dname, disk)) != NULL) {
168 		if (dname[len] == '\0' || dname[len] == ',') {
169 			return (true);
170 		}
171 		dname++;
172 	}
173 
174 	return (false);
175 }
176 
177 static int
sesled(int argc,char ** argv,bool setfault)178 sesled(int argc, char **argv, bool setfault)
179 {
180 	encioc_elm_devnames_t objdn;
181 	encioc_element_t *objp;
182 	glob_t g;
183 	char *disk, *endptr;
184 	size_t len, i, ndisks;
185 	int fd;
186 	unsigned int nobj, j, sesid;
187 	bool all, isses, onoff;
188 
189 	isses = false;
190 	all = false;
191 	onoff = false;
192 
193 	if (argc != 3) {
194 		usage(stderr, (setfault ? "fault" : "locate"));
195 	}
196 
197 	disk = argv[1];
198 
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"));
206 		}
207 		isses = true;
208 	}
209 
210 	if (strcmp(argv[2], "on") == 0) {
211 		onoff = true;
212 	} else if (strcmp(argv[2], "off") == 0) {
213 		onoff = false;
214 	} else {
215 		usage(stderr, (setfault ? "fault" : "locate"));
216 	}
217 
218 	if (strcmp(disk, "all") == 0) {
219 		all = true;
220 	}
221 	len = strlen(disk);
222 
223 	/* Get the list of ses devices */
224 	if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
225 	    GLOB_NOMATCH) {
226 		globfree(&g);
227 		xo_errx(EXIT_FAILURE, "No SES devices found");
228 	}
229 
230 	ndisks = 0;
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)) {
235 			continue;
236 		}
237 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
238 			/*
239 			 * Don't treat non-access errors as critical if we are
240 			 * accessing all devices
241 			 */
242 			if (errno == EACCES && g.gl_pathc > 1) {
243 				xo_err(EXIT_FAILURE, "unable to access SES device");
244 			}
245 			xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
246 			continue;
247 		}
248 
249 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
250 			close(fd);
251 			xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
252 		}
253 
254 		objp = calloc(nobj, sizeof(encioc_element_t));
255 		if (objp == NULL) {
256 			close(fd);
257 			xo_err(EXIT_FAILURE, "calloc()");
258 		}
259 
260 		if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
261 			free(objp);
262 			close(fd);
263 			xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
264 		}
265 
266 		if (isses) {
267 			if (sesid >= nobj) {
268 				free(objp);
269 				close(fd);
270 				xo_errx(EXIT_FAILURE,
271 				     "Requested SES ID does not exist");
272 			}
273 			do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
274 			ndisks++;
275 			free(objp);
276 			close(fd);
277 			break;
278 		}
279 		for (j = 0; j < nobj; j++) {
280 			const int devnames_size = 128;
281 			char devnames[devnames_size];
282 
283 			if (all) {
284 				do_led(fd, objp[j].elm_idx, objp[j].elm_type,
285 				    onoff, setfault);
286 				continue;
287 			}
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) {
295 				continue;
296 			}
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,
300 					    onoff, setfault);
301 					ndisks++;
302 					break;
303 				}
304 			}
305 		}
306 		free(objp);
307 		close(fd);
308 	}
309 	globfree(&g);
310 	if (ndisks == 0 && all == false) {
311 		xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
312 		    disk);
313 	}
314 
315 	return (EXIT_SUCCESS);
316 }
317 
318 static int
locate(int argc,char ** argv)319 locate(int argc, char **argv)
320 {
321 
322 	return (sesled(argc, argv, false));
323 }
324 
325 static int
fault(int argc,char ** argv)326 fault(int argc, char **argv)
327 {
328 
329 	return (sesled(argc, argv, true));
330 }
331 
332 static void
sesutil_print(int * style,const char * fmt,...)333 sesutil_print(int *style, const char *fmt, ...)
334 {
335 	va_list args;
336 
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;
344 	}
345 
346 	if (*style == PRINT_STYLE_DASHED_2)
347 		xo_emit("\t\t- ");
348 	else if (*style == PRINT_STYLE_CSV_2)
349 		xo_emit(", ");
350 	va_start(args, fmt);
351 	xo_emit_hv(NULL, fmt, args);
352 	va_end(args);
353 	if (*style == PRINT_STYLE_DASHED_2)
354 		xo_emit("\n");
355 }
356 
357 static void
print_extra_status(int eletype,u_char * cstat,int style)358 print_extra_status(int eletype, u_char *cstat, int style)
359 {
360 
361 	if (cstat[0] & 0x40) {
362 		sesutil_print(&style, "{e:predicted_failure/true} Predicted Failure");
363 	}
364 	if (cstat[0] & 0x20) {
365 		sesutil_print(&style, "{e:disabled/true} Disabled");
366 	}
367 	if (cstat[0] & 0x10) {
368 		sesutil_print(&style, "{e:swapped/true} Swapped");
369 	}
370 	switch (eletype) {
371 	case ELMTYP_DEVICE:
372 	case ELMTYP_ARRAY_DEV:
373 		if (cstat[2] & 0x02) {
374 			sesutil_print(&style, "LED={q:led/locate}");
375 		}
376 		if (cstat[2] & 0x20) {
377 			sesutil_print(&style, "LED={q:led/fault}");
378 		}
379 		break;
380 	case ELMTYP_FAN:
381 		sesutil_print(&style, "Speed: {:speed/%d}{Uw:rpm}",
382 		    (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
383 		break;
384 	case ELMTYP_THERM:
385 		if (cstat[2]) {
386 			sesutil_print(&style, "Temperature: {:temperature/%d}{Uw:C}",
387 			    cstat[2] - TEMPERATURE_OFFSET);
388 		} else {
389 			sesutil_print(&style, "Temperature: -{q:temperature/reserved}");
390 		}
391 		break;
392 	case ELMTYP_VOM:
393 		sesutil_print(&style, "Voltage: {:voltage/%.2f}{Uw:V}",
394 		    be16dec(cstat + 2) / 100.0);
395 		break;
396 	}
397 	if (style) {
398 		xo_close_container("extra_status");
399 	}
400 }
401 
402 static int
objmap(int argc,char ** argv __unused)403 objmap(int argc, char **argv __unused)
404 {
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;
410 	glob_t g;
411 	int fd;
412 	unsigned int j, nobj;
413 	size_t i;
414 	char str[32];
415 
416 	if (argc != 1) {
417 		usage(stderr, "map");
418 	}
419 
420 	/* Get the list of ses devices */
421 	if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
422 		globfree(&g);
423 		xo_errx(EXIT_FAILURE, "No SES devices found");
424 	}
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)) {
432 			continue;
433 		}
434 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
435 			/*
436 			 * Don't treat non-access errors as critical if we are
437 			 * accessing all devices
438 			 */
439 			if (errno == EACCES && g.gl_pathc > 1) {
440 				xo_err(EXIT_FAILURE, "unable to access SES device");
441 			}
442 			xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
443 			continue;
444 		}
445 
446 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
447 			close(fd);
448 			xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
449 		}
450 
451 		e_ptr = calloc(nobj, sizeof(encioc_element_t));
452 		if (e_ptr == NULL) {
453 			close(fd);
454 			xo_err(EXIT_FAILURE, "calloc()");
455 		}
456 
457 		if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
458 			close(fd);
459 			xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
460 		}
461 
462 		xo_open_instance("enclosures");
463 		xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
464 		stri.bufsiz = sizeof(str);
465 		stri.buf = &str[0];
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);
469 		stri.buf = &str[0];
470 		if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
471 			xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
472 
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) {
480 				close(fd);
481 				xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
482 			}
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 			e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
488 			if (e_desc.elm_desc_str == NULL) {
489 				close(fd);
490 				xo_err(EXIT_FAILURE, "calloc()");
491 			}
492 			if (ioctl(fd, ENCIOC_GETELMDESC,
493 			    (caddr_t) &e_desc) < 0) {
494 				close(fd);
495 				xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
496 			}
497 			/* Get the device name(s) of the element */
498 			memset(&e_devname, 0, sizeof(e_devname));
499 			e_devname.elm_idx = e_ptr[j].elm_idx;
500 			e_devname.elm_names_size = 128;
501 			e_devname.elm_devnames = calloc(128, sizeof(char));
502 			if (e_devname.elm_devnames == NULL) {
503 				close(fd);
504 				xo_err(EXIT_FAILURE, "calloc()");
505 			}
506 			if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
507 			    (caddr_t) &e_devname) <0) {
508 				/* Continue even if we can't look up devnames */
509 				e_devname.elm_devnames[0] = '\0';
510 			}
511 			xo_open_instance("elements");
512 			xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
513 			    geteltnm(e_ptr[j].elm_type));
514 			xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
515 			    scode2ascii(e_status.cstat[0]), e_status.cstat[0],
516 			    e_status.cstat[1], e_status.cstat[2],
517 			    e_status.cstat[3]);
518 			if (e_desc.elm_desc_len > 0) {
519 				xo_emit("\t\tDescription: {:description/%s}\n",
520 				    e_desc.elm_desc_str);
521 			}
522 			if (e_devname.elm_names_len > 0) {
523 				xo_emit("\t\tDevice Names: {:device_names/%s}\n",
524 				    e_devname.elm_devnames);
525 			}
526 			print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED);
527 			xo_close_instance("elements");
528 			free(e_devname.elm_devnames);
529 		}
530 		xo_close_list("elements");
531 		free(e_ptr);
532 		close(fd);
533 	}
534 	globfree(&g);
535 	xo_close_list("enclosures");
536 	xo_close_container("sesutil");
537 	xo_finish();
538 
539 	return (EXIT_SUCCESS);
540 }
541 
542 /*
543  * Get rid of the 'passN' devices, unless there's nothing else to show.
544  */
545 static void
skip_pass_devices(char * devnames,size_t devnameslen)546 skip_pass_devices(char *devnames, size_t devnameslen)
547 {
548 	char *dev, devs[128], passes[128], *tmp;
549 
550 	devs[0] = passes[0] = '\0';
551 	tmp = devnames;
552 
553 	while ((dev = strsep(&tmp, ",")) != NULL) {
554 		if (strncmp(dev, "pass", 4) == 0) {
555 			if (passes[0] != '\0')
556 				strlcat(passes, ",", sizeof(passes));
557 			strlcat(passes, dev, sizeof(passes));
558 		} else {
559 			if (devs[0] != '\0')
560 				strlcat(devs, ",", sizeof(devs));
561 			strlcat(devs, dev, sizeof(devs));
562 		}
563 	}
564 	strlcpy(devnames, devs, devnameslen);
565 	if (devnames[0] == '\0')
566 		strlcpy(devnames, passes, devnameslen);
567 }
568 
569 static void
fetch_device_details(char * devnames,char ** model,char ** serial,off_t * size)570 fetch_device_details(char *devnames, char **model, char **serial, off_t *size)
571 {
572 	char ident[DISK_IDENT_SIZE];
573 	struct diocgattr_arg arg;
574 	char *tmp;
575 	off_t mediasize;
576 	int comma;
577 	int fd;
578 
579 	comma = (int)strcspn(devnames, ",");
580 	asprintf(&tmp, "/dev/%.*s", comma, devnames);
581 	if (tmp == NULL)
582 		err(1, "asprintf");
583 	fd = open(tmp, O_RDONLY);
584 	free(tmp);
585 	if (fd < 0) {
586 		/*
587 		 * This can happen with a disk so broken it cannot
588 		 * be probed by GEOM.
589 		 */
590 		*model = strdup("?");
591 		*serial = strdup("?");
592 		*size = -1;
593 		close(fd);
594 		return;
595 	}
596 
597 	strlcpy(arg.name, "GEOM::descr", sizeof(arg.name));
598 	arg.len = sizeof(arg.value.str);
599 	if (ioctl(fd, DIOCGATTR, &arg) == 0)
600 		*model = strdup(arg.value.str);
601 	else
602 		*model = NULL;
603 
604 	if (ioctl(fd, DIOCGIDENT, ident) == 0)
605 		*serial = strdup(ident);
606 	else
607 		*serial = NULL;
608 
609 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0)
610 		*size = mediasize;
611 	else
612 		*size = -1;
613 	close(fd);
614 }
615 
616 static void
show_device(int fd,int elm_idx,encioc_elm_status_t e_status,encioc_elm_desc_t e_desc)617 show_device(int fd, int elm_idx, encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
618 {
619 	encioc_elm_devnames_t e_devname;
620 	char *model, *serial;
621 	off_t size;
622 
623 	/* Get the device name(s) of the element */
624 	memset(&e_devname, 0, sizeof(e_devname));
625 	e_devname.elm_idx = elm_idx;
626 	e_devname.elm_names_size = 128;
627 	e_devname.elm_devnames = calloc(128, sizeof(char));
628 	if (e_devname.elm_devnames == NULL) {
629 		close(fd);
630 		xo_err(EXIT_FAILURE, "calloc()");
631 	}
632 
633 	if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
634 	    (caddr_t) &e_devname) < 0) {
635 		/* We don't care if this fails */
636 		e_devname.elm_devnames[0] = '\0';
637 		model = NULL;
638 		serial = NULL;
639 		size = -1;
640 	} else {
641 		skip_pass_devices(e_devname.elm_devnames, 128);
642 		fetch_device_details(e_devname.elm_devnames, &model, &serial, &size);
643 	}
644 	xo_open_instance("elements");
645 	xo_emit("{e:type/device_slot}");
646 	xo_emit("{d:description/%-8s} ", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "-");
647 	xo_emit("{e:description/%-8s}", e_desc.elm_desc_len > 0 ? e_desc.elm_desc_str : "");
648 	xo_emit("{d:device_names/%-7s} ", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "-");
649 	xo_emit("{e:device_names/%s}", e_devname.elm_names_len > 0 ? e_devname.elm_devnames : "");
650 	xo_emit("{d:model/%-25s} ", model ? model : "-");
651 	xo_emit("{e:model/%s}", model ? model : "");
652 	xo_emit("{d:serial/%-20s} ", serial != NULL ? serial : "-");
653 	xo_emit("{e:serial/%s}", serial != NULL ? serial : "");
654 	if ((e_status.cstat[0] & 0xf) == SES_OBJSTAT_OK && size >= 0) {
655 		xo_emit("{h,hn-1000:size/%ld}{e:status/%s}",
656 		    size, scode2ascii(e_status.cstat[0]));
657 	} else {
658 		xo_emit("{:status/%s}", scode2ascii(e_status.cstat[0]));
659 	}
660 	print_extra_status(ELMTYP_ARRAY_DEV, e_status.cstat, PRINT_STYLE_CSV);
661 	xo_emit("\n");
662 	xo_close_instance("elements");
663 	free(e_devname.elm_devnames);
664 }
665 
666 static void
show_therm(encioc_elm_status_t e_status,encioc_elm_desc_t e_desc)667 show_therm(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
668 {
669 
670 	if (e_desc.elm_desc_len <= 0) {
671 		/* We don't have a label to display; might as well skip it. */
672 		return;
673 	}
674 
675 	if (e_status.cstat[2] == 0) {
676 		/* No temperature to show. */
677 		return;
678 	}
679 
680 	xo_open_instance("elements");
681 	xo_emit("{e:type/temperature_sensor}");
682 	xo_emit("{:description/%s}: {:temperature/%d}{Uw:C}",
683 	    e_desc.elm_desc_str, e_status.cstat[2] - TEMPERATURE_OFFSET);
684 	xo_close_instance("elements");
685 }
686 
687 static void
show_vom(encioc_elm_status_t e_status,encioc_elm_desc_t e_desc)688 show_vom(encioc_elm_status_t e_status, encioc_elm_desc_t e_desc)
689 {
690 
691 	if (e_desc.elm_desc_len <= 0) {
692 		/* We don't have a label to display; might as well skip it. */
693 		return;
694 	}
695 
696 	if (e_status.cstat[2] == 0) {
697 		/* No voltage to show. */
698 		return;
699 	}
700 
701 	xo_open_instance("elements");
702 	xo_emit("{e:type/voltage_sensor}");
703 	xo_emit("{:description/%s}: {:voltage/%.2f}{Uw:V}",
704 	    e_desc.elm_desc_str, be16dec(e_status.cstat + 2) / 100.0);
705 	xo_close_instance("elements");
706 }
707 
708 static int
show(int argc,char ** argv __unused)709 show(int argc, char **argv __unused)
710 {
711 	encioc_string_t stri;
712 	encioc_elm_status_t e_status;
713 	encioc_elm_desc_t e_desc;
714 	encioc_element_t *e_ptr;
715 	glob_t g;
716 	elm_type_t prev_type;
717 	int fd;
718 	unsigned int j, nobj;
719 	size_t i;
720 	bool first_ses;
721 	char str[32];
722 
723 	if (argc != 1) {
724 		usage(stderr, "map");
725 	}
726 
727 	first_ses = true;
728 
729 	/* Get the list of ses devices */
730 	if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
731 		globfree(&g);
732 		xo_errx(EXIT_FAILURE, "No SES devices found");
733 	}
734 	xo_set_version(SESUTIL_XO_VERSION);
735 	xo_open_container("sesutil");
736 	xo_open_list("enclosures");
737 	for (i = 0; i < g.gl_pathc; i++) {
738 		/* ensure we only got numbers after ses */
739 		if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
740 		    strlen(g.gl_pathv[i] + 8)) {
741 			continue;
742 		}
743 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
744 			/*
745 			 * Don't treat non-access errors as critical if we are
746 			 * accessing all devices
747 			 */
748 			if (errno == EACCES && g.gl_pathc > 1) {
749 				xo_err(EXIT_FAILURE, "unable to access SES device");
750 			}
751 			xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
752 			continue;
753 		}
754 
755 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
756 			close(fd);
757 			xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
758 		}
759 
760 		e_ptr = calloc(nobj, sizeof(encioc_element_t));
761 		if (e_ptr == NULL) {
762 			close(fd);
763 			xo_err(EXIT_FAILURE, "calloc()");
764 		}
765 
766 		if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
767 			close(fd);
768 			xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
769 		}
770 
771 		xo_open_instance("enclosures");
772 
773 		if (first_ses)
774 			first_ses = false;
775 		else
776 			xo_emit("\n");
777 
778 		xo_emit("{t:enc/%s}: ", g.gl_pathv[i] + 5);
779 		stri.bufsiz = sizeof(str);
780 		stri.buf = &str[0];
781 		if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
782 			xo_emit("<{t:name/%s}>; ", stri.buf);
783 		stri.bufsiz = sizeof(str);
784 		stri.buf = &str[0];
785 		if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
786 			xo_emit("ID: {t:id/%s}", stri.buf);
787 		xo_emit("\n");
788 
789 		xo_open_list("elements");
790 		prev_type = -1;
791 		for (j = 0; j < nobj; j++) {
792 			/* Get the status of the element */
793 			memset(&e_status, 0, sizeof(e_status));
794 			e_status.elm_idx = e_ptr[j].elm_idx;
795 			if (ioctl(fd, ENCIOC_GETELMSTAT,
796 			    (caddr_t) &e_status) < 0) {
797 				close(fd);
798 				xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
799 			}
800 
801 			/*
802 			 * Skip "Unsupported" elements; those usually precede
803 			 * the actual device entries and are not particularly
804 			 * interesting.
805 			 */
806 			if (e_status.cstat[0] == SES_OBJSTAT_UNSUPPORTED)
807 				continue;
808 
809 			/* Get the description of the element */
810 			memset(&e_desc, 0, sizeof(e_desc));
811 			e_desc.elm_idx = e_ptr[j].elm_idx;
812 			e_desc.elm_desc_len = UINT16_MAX;
813 			e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
814 			if (e_desc.elm_desc_str == NULL) {
815 				close(fd);
816 				xo_err(EXIT_FAILURE, "calloc()");
817 			}
818 			if (ioctl(fd, ENCIOC_GETELMDESC,
819 			    (caddr_t) &e_desc) < 0) {
820 				close(fd);
821 				xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
822 			}
823 
824 			switch (e_ptr[j].elm_type) {
825 			case ELMTYP_DEVICE:
826 			case ELMTYP_ARRAY_DEV:
827 				if (e_ptr[j].elm_type != prev_type)
828 					xo_emit("Desc     Dev     Model                     Ident                Size/Status\n");
829 
830 				show_device(fd, e_ptr[j].elm_idx, e_status, e_desc);
831 				prev_type = e_ptr[j].elm_type;
832 				break;
833 			case ELMTYP_THERM:
834 				if (e_ptr[j].elm_type != prev_type)
835 					xo_emit("\nTemperatures: ");
836 				else
837 					xo_emit(", ");
838 				prev_type = e_ptr[j].elm_type;
839 				show_therm(e_status, e_desc);
840 				break;
841 			case ELMTYP_VOM:
842 				if (e_ptr[j].elm_type != prev_type)
843 					xo_emit("\nVoltages: ");
844 				else
845 					xo_emit(", ");
846 				prev_type = e_ptr[j].elm_type;
847 				show_vom(e_status, e_desc);
848 				break;
849 			default:
850 				/*
851 				 * Ignore stuff not interesting to the user.
852 				 */
853 				break;
854 			}
855 		}
856 		if (prev_type != (elm_type_t)-1 &&
857 		    prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV)
858 			xo_emit("\n");
859 		xo_close_list("elements");
860 		free(e_ptr);
861 		close(fd);
862 	}
863 	globfree(&g);
864 	xo_close_list("enclosures");
865 	xo_close_container("sesutil");
866 	xo_finish();
867 
868 	return (EXIT_SUCCESS);
869 }
870 
871 static int
encstatus(int argc,char ** argv __unused)872 encstatus(int argc, char **argv __unused)
873 {
874 	glob_t g;
875 	int fd, status;
876 	size_t i, e;
877 	u_char estat;
878 
879 	status = 0;
880 	if (argc != 1) {
881 		usage(stderr, "status");
882 	}
883 
884 	/* Get the list of ses devices */
885 	if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
886 		globfree(&g);
887 		xo_errx(EXIT_FAILURE, "No SES devices found");
888 	}
889 
890 	xo_set_version(SESUTIL_XO_VERSION);
891 	xo_open_container("sesutil");
892 	xo_open_list("enclosures");
893 	for (i = 0; i < g.gl_pathc; i++) {
894 		/* ensure we only got numbers after ses */
895 		if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
896 		    strlen(g.gl_pathv[i] + 8)) {
897 			continue;
898 		}
899 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
900 			/*
901 			 * Don't treat non-access errors as critical if we are
902 			 * accessing all devices
903 			 */
904 			if (errno == EACCES && g.gl_pathc > 1) {
905 				xo_err(EXIT_FAILURE, "unable to access SES device");
906 			}
907 			xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
908 			continue;
909 		}
910 
911 		if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
912 			xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
913 			close(fd);
914 		}
915 
916 		xo_open_instance("enclosures");
917 		xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
918 		e = 0;
919 		if (estat == 0) {
920 			if (status == 0) {
921 				status = 1;
922 			}
923 			xo_emit("{q:status/OK}");
924 		} else {
925 			if (estat & SES_ENCSTAT_INFO) {
926 				xo_emit("{lq:status/INFO}");
927 				e++;
928 			}
929 			if (estat & SES_ENCSTAT_NONCRITICAL) {
930 				if (e)
931 					xo_emit(",");
932 				xo_emit("{lq:status/NONCRITICAL}");
933 				e++;
934 			}
935 			if (estat & SES_ENCSTAT_CRITICAL) {
936 				if (e)
937 					xo_emit(",");
938 				xo_emit("{lq:status/CRITICAL}");
939 				e++;
940 				status = -1;
941 			}
942 			if (estat & SES_ENCSTAT_UNRECOV) {
943 				if (e)
944 					xo_emit(",");
945 				xo_emit("{lq:status/UNRECOV}");
946 				e++;
947 				status = -1;
948 			}
949 		}
950 		xo_close_instance("enclosures");
951 		xo_emit("\n");
952 		close(fd);
953 	}
954 	globfree(&g);
955 
956 	xo_close_list("enclosures");
957 	xo_close_container("sesutil");
958 	xo_finish();
959 
960 	if (status == 1) {
961 		return (EXIT_SUCCESS);
962 	} else {
963 		return (EXIT_FAILURE);
964 	}
965 }
966 
967 int
main(int argc,char ** argv)968 main(int argc, char **argv)
969 {
970 	int i, ch;
971 	struct command *cmd = NULL;
972 
973 	argc = xo_parse_args(argc, argv);
974 	if (argc < 0)
975 		exit(1);
976 
977 	uflag = "/dev/ses[0-9]*";
978 	while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
979 		switch (ch) {
980 		case 'u':
981 			uflag = optarg;
982 			break;
983 		case '?':
984 		default:
985 			usage(stderr, NULL);
986 		}
987 	}
988 	argc -= optind;
989 	argv += optind;
990 
991 	if (argc < 1) {
992 		warnx("Missing command");
993 		usage(stderr, NULL);
994 	}
995 
996 	for (i = 0; i < nbcmds; i++) {
997 		if (strcmp(argv[0], cmds[i].name) == 0) {
998 			cmd = &cmds[i];
999 			break;
1000 		}
1001 	}
1002 
1003 	if (cmd == NULL) {
1004 		warnx("unknown command %s", argv[0]);
1005 		usage(stderr, NULL);
1006 	}
1007 
1008 	return (cmd->exec(argc, argv));
1009 }
1010