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 (onoff) { 122 o.cstat[2] |= (setfault ? 0x20 : 0x02); 123 } else { 124 o.cstat[2] &= (setfault ? 0xdf : 0xfd); 125 } 126 127 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) { 128 close(fd); 129 err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); 130 } 131 } 132 133 static bool 134 disk_match(const char *devnames, const char *disk, size_t len) 135 { 136 const char *dname; 137 138 dname = devnames; 139 while ((dname = strstr(dname, disk)) != NULL) { 140 if (dname[len] == '\0' || dname[len] == ',') { 141 return (true); 142 } 143 dname++; 144 } 145 146 return (false); 147 } 148 149 static int 150 sesled(int argc, char **argv, bool setfault) 151 { 152 encioc_elm_devnames_t objdn; 153 encioc_element_t *objp; 154 glob_t g; 155 char *disk, *endptr; 156 size_t len, i, ndisks; 157 int fd; 158 unsigned int nobj, j, sesid; 159 bool all, isses, onoff; 160 161 isses = false; 162 all = false; 163 onoff = false; 164 165 if (argc != 3) { 166 usage(stderr, (setfault ? "fault" : "locate")); 167 } 168 169 disk = argv[1]; 170 171 sesid = strtoul(disk, &endptr, 10); 172 if (*endptr == '\0') { 173 endptr = strrchr(uflag, '*'); 174 if (endptr != NULL && *endptr == '*') { 175 warnx("Must specifying a SES device (-u) to use a SES " 176 "id# to identify a disk"); 177 usage(stderr, (setfault ? "fault" : "locate")); 178 } 179 isses = true; 180 } 181 182 if (strcmp(argv[2], "on") == 0) { 183 onoff = true; 184 } else if (strcmp(argv[2], "off") == 0) { 185 onoff = false; 186 } else { 187 usage(stderr, (setfault ? "fault" : "locate")); 188 } 189 190 if (strcmp(disk, "all") == 0) { 191 all = true; 192 } 193 len = strlen(disk); 194 195 /* Get the list of ses devices */ 196 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) == 197 GLOB_NOMATCH) { 198 globfree(&g); 199 errx(EXIT_FAILURE, "No SES devices found"); 200 } 201 202 ndisks = 0; 203 for (i = 0; i < g.gl_pathc; i++) { 204 /* ensure we only got numbers after ses */ 205 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 206 strlen(g.gl_pathv[i] + 8)) { 207 continue; 208 } 209 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 210 /* 211 * Don't treat non-access errors as critical if we are 212 * accessing all devices 213 */ 214 if (errno == EACCES && g.gl_pathc > 1) { 215 err(EXIT_FAILURE, "unable to access SES device"); 216 } 217 warn("unable to access SES device: %s", g.gl_pathv[i]); 218 continue; 219 } 220 221 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { 222 close(fd); 223 err(EXIT_FAILURE, "ENCIOC_GETNELM"); 224 } 225 226 objp = calloc(nobj, sizeof(encioc_element_t)); 227 if (objp == NULL) { 228 close(fd); 229 err(EXIT_FAILURE, "calloc()"); 230 } 231 232 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) { 233 close(fd); 234 err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); 235 } 236 237 if (isses) { 238 if (sesid > nobj) { 239 close(fd); 240 errx(EXIT_FAILURE, 241 "Requested SES ID does not exist"); 242 } 243 do_led(fd, sesid, onoff, setfault); 244 ndisks++; 245 close(fd); 246 break; 247 } 248 for (j = 0; j < nobj; j++) { 249 memset(&objdn, 0, sizeof(objdn)); 250 objdn.elm_idx = objp[j].elm_idx; 251 objdn.elm_names_size = 128; 252 objdn.elm_devnames = calloc(128, sizeof(char)); 253 if (objdn.elm_devnames == NULL) { 254 close(fd); 255 err(EXIT_FAILURE, "calloc()"); 256 } 257 if (ioctl(fd, ENCIOC_GETELMDEVNAMES, 258 (caddr_t) &objdn) <0) { 259 continue; 260 } 261 if (objdn.elm_names_len > 0) { 262 if (all) { 263 do_led(fd, objdn.elm_idx, 264 onoff, setfault); 265 continue; 266 } 267 if (disk_match(objdn.elm_devnames, disk, len)) { 268 do_led(fd, objdn.elm_idx, 269 onoff, setfault); 270 ndisks++; 271 break; 272 } 273 } 274 } 275 free(objp); 276 close(fd); 277 } 278 globfree(&g); 279 if (ndisks == 0 && all == false) { 280 errx(EXIT_FAILURE, "Count not find the SES id of device '%s'", 281 disk); 282 } 283 284 return (EXIT_SUCCESS); 285 } 286 287 static int 288 locate(int argc, char **argv) 289 { 290 291 return (sesled(argc, argv, false)); 292 } 293 294 static int 295 fault(int argc, char **argv) 296 { 297 298 return (sesled(argc, argv, true)); 299 } 300 301 static int 302 objmap(int argc, char **argv __unused) 303 { 304 struct sbuf *extra; 305 encioc_string_t stri; 306 encioc_elm_devnames_t e_devname; 307 encioc_elm_status_t e_status; 308 encioc_elm_desc_t e_desc; 309 encioc_element_t *e_ptr; 310 glob_t g; 311 int fd; 312 unsigned int j, nobj; 313 size_t i; 314 char str[32]; 315 316 if (argc != 1) { 317 usage(stderr, "map"); 318 } 319 320 /* Get the list of ses devices */ 321 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { 322 globfree(&g); 323 errx(EXIT_FAILURE, "No SES devices found"); 324 } 325 for (i = 0; i < g.gl_pathc; i++) { 326 /* ensure we only got numbers after ses */ 327 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 328 strlen(g.gl_pathv[i] + 8)) { 329 continue; 330 } 331 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 332 /* 333 * Don't treat non-access errors as critical if we are 334 * accessing all devices 335 */ 336 if (errno == EACCES && g.gl_pathc > 1) { 337 err(EXIT_FAILURE, "unable to access SES device"); 338 } 339 warn("unable to access SES device: %s", g.gl_pathv[i]); 340 continue; 341 } 342 343 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { 344 close(fd); 345 err(EXIT_FAILURE, "ENCIOC_GETNELM"); 346 } 347 348 e_ptr = calloc(nobj, sizeof(encioc_element_t)); 349 if (e_ptr == NULL) { 350 close(fd); 351 err(EXIT_FAILURE, "calloc()"); 352 } 353 354 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) { 355 close(fd); 356 err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); 357 } 358 359 printf("%s:\n", g.gl_pathv[i] + 5); 360 stri.bufsiz = sizeof(str); 361 stri.buf = &str[0]; 362 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0) 363 printf("\tEnclosure Name: %s\n", stri.buf); 364 stri.bufsiz = sizeof(str); 365 stri.buf = &str[0]; 366 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0) 367 printf("\tEnclosure ID: %s\n", stri.buf); 368 369 for (j = 0; j < nobj; j++) { 370 /* Get the status of the element */ 371 memset(&e_status, 0, sizeof(e_status)); 372 e_status.elm_idx = e_ptr[j].elm_idx; 373 if (ioctl(fd, ENCIOC_GETELMSTAT, 374 (caddr_t) &e_status) < 0) { 375 close(fd); 376 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); 377 } 378 /* Get the description of the element */ 379 memset(&e_desc, 0, sizeof(e_desc)); 380 e_desc.elm_idx = e_ptr[j].elm_idx; 381 e_desc.elm_desc_len = UINT16_MAX; 382 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); 383 if (e_desc.elm_desc_str == NULL) { 384 close(fd); 385 err(EXIT_FAILURE, "calloc()"); 386 } 387 if (ioctl(fd, ENCIOC_GETELMDESC, 388 (caddr_t) &e_desc) < 0) { 389 close(fd); 390 err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); 391 } 392 /* Get the device name(s) of the element */ 393 memset(&e_devname, 0, sizeof(e_devname)); 394 e_devname.elm_idx = e_ptr[j].elm_idx; 395 e_devname.elm_names_size = 128; 396 e_devname.elm_devnames = calloc(128, sizeof(char)); 397 if (e_devname.elm_devnames == NULL) { 398 close(fd); 399 err(EXIT_FAILURE, "calloc()"); 400 } 401 if (ioctl(fd, ENCIOC_GETELMDEVNAMES, 402 (caddr_t) &e_devname) <0) { 403 /* We don't care if this fails */ 404 e_devname.elm_devnames[0] = '\0'; 405 } 406 printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx, 407 geteltnm(e_ptr[j].elm_type)); 408 printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n", 409 scode2ascii(e_status.cstat[0]), e_status.cstat[0], 410 e_status.cstat[1], e_status.cstat[2], 411 e_status.cstat[3]); 412 if (e_desc.elm_desc_len > 0) { 413 printf("\t\tDescription: %s\n", 414 e_desc.elm_desc_str); 415 } 416 if (e_devname.elm_names_len > 0) { 417 printf("\t\tDevice Names: %s\n", 418 e_devname.elm_devnames); 419 } 420 extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat); 421 if (sbuf_len(extra) > 0) { 422 printf("\t\tExtra status:\n%s", 423 sbuf_data(extra)); 424 } 425 sbuf_delete(extra); 426 free(e_devname.elm_devnames); 427 } 428 free(e_ptr); 429 close(fd); 430 } 431 globfree(&g); 432 433 return (EXIT_SUCCESS); 434 } 435 436 static int 437 encstatus(int argc, char **argv __unused) 438 { 439 glob_t g; 440 int fd, status; 441 size_t i, e; 442 u_char estat; 443 444 status = 0; 445 if (argc != 1) { 446 usage(stderr, "status"); 447 } 448 449 /* Get the list of ses devices */ 450 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { 451 globfree(&g); 452 errx(EXIT_FAILURE, "No SES devices found"); 453 } 454 for (i = 0; i < g.gl_pathc; i++) { 455 /* ensure we only got numbers after ses */ 456 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 457 strlen(g.gl_pathv[i] + 8)) { 458 continue; 459 } 460 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 461 /* 462 * Don't treat non-access errors as critical if we are 463 * accessing all devices 464 */ 465 if (errno == EACCES && g.gl_pathc > 1) { 466 err(EXIT_FAILURE, "unable to access SES device"); 467 } 468 warn("unable to access SES device: %s", g.gl_pathv[i]); 469 continue; 470 } 471 472 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) { 473 close(fd); 474 err(EXIT_FAILURE, "ENCIOC_GETENCSTAT"); 475 } 476 477 printf("%s: ", g.gl_pathv[i] + 5); 478 e = 0; 479 if (estat == 0) { 480 if (status == 0) { 481 status = 1; 482 } 483 printf("OK"); 484 } else { 485 if (estat & SES_ENCSTAT_INFO) { 486 printf("INFO"); 487 e++; 488 } 489 if (estat & SES_ENCSTAT_NONCRITICAL) { 490 if (e) 491 printf(","); 492 printf("NONCRITICAL"); 493 e++; 494 } 495 if (estat & SES_ENCSTAT_CRITICAL) { 496 if (e) 497 printf(","); 498 printf("CRITICAL"); 499 e++; 500 status = -1; 501 } 502 if (estat & SES_ENCSTAT_UNRECOV) { 503 if (e) 504 printf(","); 505 printf("UNRECOV"); 506 e++; 507 status = -1; 508 } 509 } 510 printf("\n"); 511 512 close(fd); 513 } 514 globfree(&g); 515 516 if (status == 1) { 517 return (EXIT_SUCCESS); 518 } else { 519 return (EXIT_FAILURE); 520 } 521 } 522 523 int 524 main(int argc, char **argv) 525 { 526 int i, ch; 527 struct command *cmd = NULL; 528 529 uflag = "/dev/ses[0-9]*"; 530 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) { 531 switch (ch) { 532 case 'u': 533 uflag = optarg; 534 break; 535 case '?': 536 default: 537 usage(stderr, NULL); 538 } 539 } 540 argc -= optind; 541 argv += optind; 542 543 if (argc < 1) { 544 warnx("Missing command"); 545 usage(stderr, NULL); 546 } 547 548 for (i = 0; i < nbcmds; i++) { 549 if (strcmp(argv[0], cmds[i].name) == 0) { 550 cmd = &cmds[i]; 551 break; 552 } 553 } 554 555 if (cmd == NULL) { 556 warnx("unknown command %s", argv[0]); 557 usage(stderr, NULL); 558 } 559 560 return (cmd->exec(argc, argv)); 561 } 562