xref: /freebsd/usr.sbin/sesutil/sesutil.c (revision d01498defbe804f66435b44f22da9278acddf082)
1 /*-
2  * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3  * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
4  * Copyright (c) 2000 by Matthew Jacob
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/sbuf.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <getopt.h>
41 #include <glob.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include <cam/scsi/scsi_enc.h>
51 
52 #include "eltsub.h"
53 
54 static int encstatus(int argc, char **argv);
55 static int fault(int argc, char **argv);
56 static int locate(int argc, char **argv);
57 static int objmap(int argc, char **argv);
58 static int sesled(int argc, char **argv, bool fault);
59 
60 static struct command {
61 	const char *name;
62 	const char *param;
63 	const char *desc;
64 	int (*exec)(int argc, char **argv);
65 } cmds[] = {
66 	{ "fault",
67 	    "(<disk>|<sesid>|all) (on|off)",
68 	    "Change the state of the fault LED associated with a disk",
69 	    fault },
70 	{ "locate",
71 	    "(<disk>|<sesid>|all) (on|off)",
72 	    "Change the state of the locate LED associated with a disk",
73 	    locate },
74 	{ "map", "",
75 	    "Print a map of the devices managed by the enclosure", objmap } ,
76 	{ "status", "", "Print the status of the enclosure",
77 	    encstatus },
78 };
79 
80 static const int nbcmds = nitems(cmds);
81 static const char *uflag;
82 
83 static void
84 usage(FILE *out, const char *subcmd)
85 {
86 	int i;
87 
88 	if (subcmd == NULL) {
89 		fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
90 		    getprogname());
91 		fprintf(out, "Commands supported:\n");
92 	}
93 	for (i = 0; i < nbcmds; i++) {
94 		if (subcmd != NULL) {
95 			if (strcmp(subcmd, cmds[i].name) == 0) {
96 				fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
97 				    "%s\n\t%s\n", getprogname(), subcmd,
98 				    cmds[i].param, cmds[i].desc);
99 				break;
100 			}
101 			continue;
102 		}
103 		fprintf(out, "    %-12s%s\n\t\t%s\n\n", cmds[i].name,
104 		    cmds[i].param, cmds[i].desc);
105 	}
106 
107 	exit(EXIT_FAILURE);
108 }
109 
110 static void
111 do_led(int fd, unsigned int idx, bool onoff, bool setfault)
112 {
113 	encioc_elm_status_t o;
114 
115 	o.elm_idx = idx;
116 	if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
117 		close(fd);
118 		err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
119 	}
120 	o.cstat[0] |= 0x80;
121 	if (setfault) {
122 		if (onoff)
123 			o.cstat[3] |= 0x20;
124 		else
125 			o.cstat[3] &= 0xdf;
126 	} else {
127 		if (onoff)
128 			o.cstat[2] |= 0x02;
129 		else
130 			o.cstat[2] &= 0xfd;
131 	}
132 
133 	if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
134 		close(fd);
135 		err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
136 	}
137 }
138 
139 static bool
140 disk_match(const char *devnames, const char *disk, size_t len)
141 {
142 	const char *dname;
143 
144 	dname = devnames;
145 	while ((dname = strstr(dname, disk)) != NULL) {
146 		if (dname[len] == '\0' || dname[len] == ',') {
147 			return (true);
148 		}
149 		dname++;
150 	}
151 
152 	return (false);
153 }
154 
155 static int
156 sesled(int argc, char **argv, bool setfault)
157 {
158 	encioc_elm_devnames_t objdn;
159 	encioc_element_t *objp;
160 	glob_t g;
161 	char *disk, *endptr;
162 	size_t len, i, ndisks;
163 	int fd;
164 	unsigned int nobj, j, sesid;
165 	bool all, isses, onoff;
166 
167 	isses = false;
168 	all = false;
169 	onoff = false;
170 
171 	if (argc != 3) {
172 		usage(stderr, (setfault ? "fault" : "locate"));
173 	}
174 
175 	disk = argv[1];
176 
177 	sesid = strtoul(disk, &endptr, 10);
178 	if (*endptr == '\0') {
179 		endptr = strrchr(uflag, '*');
180 		if (endptr != NULL && *endptr == '*') {
181 			warnx("Must specifying a SES device (-u) to use a SES "
182 			    "id# to identify a disk");
183 			usage(stderr, (setfault ? "fault" : "locate"));
184 		}
185 		isses = true;
186 	}
187 
188 	if (strcmp(argv[2], "on") == 0) {
189 		onoff = true;
190 	} else if (strcmp(argv[2], "off") == 0) {
191 		onoff = false;
192 	} else {
193 		usage(stderr, (setfault ? "fault" : "locate"));
194 	}
195 
196 	if (strcmp(disk, "all") == 0) {
197 		all = true;
198 	}
199 	len = strlen(disk);
200 
201 	/* Get the list of ses devices */
202 	if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
203 	    GLOB_NOMATCH) {
204 		globfree(&g);
205 		errx(EXIT_FAILURE, "No SES devices found");
206 	}
207 
208 	ndisks = 0;
209 	for (i = 0; i < g.gl_pathc; i++) {
210 		/* ensure we only got numbers after ses */
211 		if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
212 		    strlen(g.gl_pathv[i] + 8)) {
213 			continue;
214 		}
215 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
216 			/*
217 			 * Don't treat non-access errors as critical if we are
218 			 * accessing all devices
219 			 */
220 			if (errno == EACCES && g.gl_pathc > 1) {
221 				err(EXIT_FAILURE, "unable to access SES device");
222 			}
223 			warn("unable to access SES device: %s", g.gl_pathv[i]);
224 			continue;
225 		}
226 
227 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
228 			close(fd);
229 			err(EXIT_FAILURE, "ENCIOC_GETNELM");
230 		}
231 
232 		objp = calloc(nobj, sizeof(encioc_element_t));
233 		if (objp == NULL) {
234 			close(fd);
235 			err(EXIT_FAILURE, "calloc()");
236 		}
237 
238 		if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
239 			close(fd);
240 			err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
241 		}
242 
243 		if (isses) {
244 			if (sesid > nobj) {
245 				close(fd);
246 				errx(EXIT_FAILURE,
247 				     "Requested SES ID does not exist");
248 			}
249 			do_led(fd, sesid, onoff, setfault);
250 			ndisks++;
251 			close(fd);
252 			break;
253 		}
254 		for (j = 0; j < nobj; j++) {
255 			memset(&objdn, 0, sizeof(objdn));
256 			objdn.elm_idx = objp[j].elm_idx;
257 			objdn.elm_names_size = 128;
258 			objdn.elm_devnames = calloc(128, sizeof(char));
259 			if (objdn.elm_devnames == NULL) {
260 				close(fd);
261 				err(EXIT_FAILURE, "calloc()");
262 			}
263 			if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
264 			    (caddr_t) &objdn) <0) {
265 				continue;
266 			}
267 			if (objdn.elm_names_len > 0) {
268 				if (all) {
269 					do_led(fd, objdn.elm_idx,
270 					    onoff, setfault);
271 					continue;
272 				}
273 				if (disk_match(objdn.elm_devnames, disk, len)) {
274 					do_led(fd, objdn.elm_idx,
275 					    onoff, setfault);
276 					ndisks++;
277 					break;
278 				}
279 			}
280 		}
281 		free(objp);
282 		close(fd);
283 	}
284 	globfree(&g);
285 	if (ndisks == 0 && all == false) {
286 		errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
287 		    disk);
288 	}
289 
290 	return (EXIT_SUCCESS);
291 }
292 
293 static int
294 locate(int argc, char **argv)
295 {
296 
297 	return (sesled(argc, argv, false));
298 }
299 
300 static int
301 fault(int argc, char **argv)
302 {
303 
304 	return (sesled(argc, argv, true));
305 }
306 
307 static int
308 objmap(int argc, char **argv __unused)
309 {
310 	struct sbuf *extra;
311 	encioc_string_t stri;
312 	encioc_elm_devnames_t e_devname;
313 	encioc_elm_status_t e_status;
314 	encioc_elm_desc_t e_desc;
315 	encioc_element_t *e_ptr;
316 	glob_t g;
317 	int fd;
318 	unsigned int j, nobj;
319 	size_t i;
320 	char str[32];
321 
322 	if (argc != 1) {
323 		usage(stderr, "map");
324 	}
325 
326 	/* Get the list of ses devices */
327 	if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
328 		globfree(&g);
329 		errx(EXIT_FAILURE, "No SES devices found");
330 	}
331 	for (i = 0; i < g.gl_pathc; i++) {
332 		/* ensure we only got numbers after ses */
333 		if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
334 		    strlen(g.gl_pathv[i] + 8)) {
335 			continue;
336 		}
337 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
338 			/*
339 			 * Don't treat non-access errors as critical if we are
340 			 * accessing all devices
341 			 */
342 			if (errno == EACCES && g.gl_pathc > 1) {
343 				err(EXIT_FAILURE, "unable to access SES device");
344 			}
345 			warn("unable to access SES device: %s", g.gl_pathv[i]);
346 			continue;
347 		}
348 
349 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
350 			close(fd);
351 			err(EXIT_FAILURE, "ENCIOC_GETNELM");
352 		}
353 
354 		e_ptr = calloc(nobj, sizeof(encioc_element_t));
355 		if (e_ptr == NULL) {
356 			close(fd);
357 			err(EXIT_FAILURE, "calloc()");
358 		}
359 
360 		if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
361 			close(fd);
362 			err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
363 		}
364 
365 		printf("%s:\n", g.gl_pathv[i] + 5);
366 		stri.bufsiz = sizeof(str);
367 		stri.buf = &str[0];
368 		if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
369 			printf("\tEnclosure Name: %s\n", stri.buf);
370 		stri.bufsiz = sizeof(str);
371 		stri.buf = &str[0];
372 		if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
373 			printf("\tEnclosure ID: %s\n", stri.buf);
374 
375 		for (j = 0; j < nobj; j++) {
376 			/* Get the status of the element */
377 			memset(&e_status, 0, sizeof(e_status));
378 			e_status.elm_idx = e_ptr[j].elm_idx;
379 			if (ioctl(fd, ENCIOC_GETELMSTAT,
380 			    (caddr_t) &e_status) < 0) {
381 				close(fd);
382 				err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
383 			}
384 			/* Get the description of the element */
385 			memset(&e_desc, 0, sizeof(e_desc));
386 			e_desc.elm_idx = e_ptr[j].elm_idx;
387 			e_desc.elm_desc_len = UINT16_MAX;
388 			e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
389 			if (e_desc.elm_desc_str == NULL) {
390 				close(fd);
391 				err(EXIT_FAILURE, "calloc()");
392 			}
393 			if (ioctl(fd, ENCIOC_GETELMDESC,
394 			    (caddr_t) &e_desc) < 0) {
395 				close(fd);
396 				err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
397 			}
398 			/* Get the device name(s) of the element */
399 			memset(&e_devname, 0, sizeof(e_devname));
400 			e_devname.elm_idx = e_ptr[j].elm_idx;
401 			e_devname.elm_names_size = 128;
402 			e_devname.elm_devnames = calloc(128, sizeof(char));
403 			if (e_devname.elm_devnames == NULL) {
404 				close(fd);
405 				err(EXIT_FAILURE, "calloc()");
406 			}
407 			if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
408 			    (caddr_t) &e_devname) <0) {
409 				/* We don't care if this fails */
410 				e_devname.elm_devnames[0] = '\0';
411 			}
412 			printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx,
413 			    geteltnm(e_ptr[j].elm_type));
414 			printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n",
415 			    scode2ascii(e_status.cstat[0]), e_status.cstat[0],
416 			    e_status.cstat[1], e_status.cstat[2],
417 			    e_status.cstat[3]);
418 			if (e_desc.elm_desc_len > 0) {
419 				printf("\t\tDescription: %s\n",
420 				    e_desc.elm_desc_str);
421 			}
422 			if (e_devname.elm_names_len > 0) {
423 				printf("\t\tDevice Names: %s\n",
424 				    e_devname.elm_devnames);
425 			}
426 			extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat);
427 			if (sbuf_len(extra) > 0) {
428 				printf("\t\tExtra status:\n%s",
429 				   sbuf_data(extra));
430 			}
431 			sbuf_delete(extra);
432 			free(e_devname.elm_devnames);
433 		}
434 		free(e_ptr);
435 		close(fd);
436 	}
437 	globfree(&g);
438 
439 	return (EXIT_SUCCESS);
440 }
441 
442 static int
443 encstatus(int argc, char **argv __unused)
444 {
445 	glob_t g;
446 	int fd, status;
447 	size_t i, e;
448 	u_char estat;
449 
450 	status = 0;
451 	if (argc != 1) {
452 		usage(stderr, "status");
453 	}
454 
455 	/* Get the list of ses devices */
456 	if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
457 		globfree(&g);
458 		errx(EXIT_FAILURE, "No SES devices found");
459 	}
460 	for (i = 0; i < g.gl_pathc; i++) {
461 		/* ensure we only got numbers after ses */
462 		if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
463 		    strlen(g.gl_pathv[i] + 8)) {
464 			continue;
465 		}
466 		if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
467 			/*
468 			 * Don't treat non-access errors as critical if we are
469 			 * accessing all devices
470 			 */
471 			if (errno == EACCES && g.gl_pathc > 1) {
472 				err(EXIT_FAILURE, "unable to access SES device");
473 			}
474 			warn("unable to access SES device: %s", g.gl_pathv[i]);
475 			continue;
476 		}
477 
478 		if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
479 			close(fd);
480 			err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
481 		}
482 
483 		printf("%s: ", g.gl_pathv[i] + 5);
484 		e = 0;
485 		if (estat == 0) {
486 			if (status == 0) {
487 				status = 1;
488 			}
489 			printf("OK");
490 		} else {
491 			if (estat & SES_ENCSTAT_INFO) {
492 				printf("INFO");
493 				e++;
494 			}
495 			if (estat & SES_ENCSTAT_NONCRITICAL) {
496 				if (e)
497 					printf(",");
498 				printf("NONCRITICAL");
499 				e++;
500 			}
501 			if (estat & SES_ENCSTAT_CRITICAL) {
502 				if (e)
503 					printf(",");
504 				printf("CRITICAL");
505 				e++;
506 				status = -1;
507 			}
508 			if (estat & SES_ENCSTAT_UNRECOV) {
509 				if (e)
510 					printf(",");
511 				printf("UNRECOV");
512 				e++;
513 				status = -1;
514 			}
515 		}
516 		printf("\n");
517 
518 		close(fd);
519 	}
520 	globfree(&g);
521 
522 	if (status == 1) {
523 		return (EXIT_SUCCESS);
524 	} else {
525 		return (EXIT_FAILURE);
526 	}
527 }
528 
529 int
530 main(int argc, char **argv)
531 {
532 	int i, ch;
533 	struct command *cmd = NULL;
534 
535 	uflag = "/dev/ses[0-9]*";
536 	while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
537 		switch (ch) {
538 		case 'u':
539 			uflag = optarg;
540 			break;
541 		case '?':
542 		default:
543 			usage(stderr, NULL);
544 		}
545 	}
546 	argc -= optind;
547 	argv += optind;
548 
549 	if (argc < 1) {
550 		warnx("Missing command");
551 		usage(stderr, NULL);
552 	}
553 
554 	for (i = 0; i < nbcmds; i++) {
555 		if (strcmp(argv[0], cmds[i].name) == 0) {
556 			cmd = &cmds[i];
557 			break;
558 		}
559 	}
560 
561 	if (cmd == NULL) {
562 		warnx("unknown command %s", argv[0]);
563 		usage(stderr, NULL);
564 	}
565 
566 	return (cmd->exec(argc, argv));
567 }
568