]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sesutil/sesutil.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / usr.sbin / sesutil / sesutil.c
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/endian.h>
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.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 #include <libxo/xo.h>
50
51 #include <cam/scsi/scsi_enc.h>
52
53 #include "eltsub.h"
54
55 #define SESUTIL_XO_VERSION      "1"
56
57 static int encstatus(int argc, char **argv);
58 static int fault(int argc, char **argv);
59 static int locate(int argc, char **argv);
60 static int objmap(int argc, char **argv);
61 static int sesled(int argc, char **argv, bool fault);
62 static void sesutil_print(bool *title, const char *fmt, ...) __printflike(2,3);
63
64 static struct command {
65         const char *name;
66         const char *param;
67         const char *desc;
68         int (*exec)(int argc, char **argv);
69 } cmds[] = {
70         { "fault",
71             "(<disk>|<sesid>|all) (on|off)",
72             "Change the state of the fault LED associated with a disk",
73             fault },
74         { "locate",
75             "(<disk>|<sesid>|all) (on|off)",
76             "Change the state of the locate LED associated with a disk",
77             locate },
78         { "map", "",
79             "Print a map of the devices managed by the enclosure", objmap } ,
80         { "status", "", "Print the status of the enclosure",
81             encstatus },
82 };
83
84 static const int nbcmds = nitems(cmds);
85 static const char *uflag;
86
87 static void
88 usage(FILE *out, const char *subcmd)
89 {
90         int i;
91
92         if (subcmd == NULL) {
93                 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
94                     getprogname());
95                 fprintf(out, "Commands supported:\n");
96         }
97         for (i = 0; i < nbcmds; i++) {
98                 if (subcmd != NULL) {
99                         if (strcmp(subcmd, cmds[i].name) == 0) {
100                                 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
101                                     "%s\n\t%s\n", getprogname(), subcmd,
102                                     cmds[i].param, cmds[i].desc);
103                                 break;
104                         }
105                         continue;
106                 }
107                 fprintf(out, "    %-12s%s\n\t\t%s\n\n", cmds[i].name,
108                     cmds[i].param, cmds[i].desc);
109         }
110
111         exit(EXIT_FAILURE);
112 }
113
114 static void
115 do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault)
116 {
117         int state = onoff ? 1 : 0;
118         encioc_elm_status_t o;
119         struct ses_ctrl_dev_slot *slot;
120
121         o.elm_idx = idx;
122         if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
123                 close(fd);
124                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
125         }
126         slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
127         switch (type) {
128         case ELMTYP_DEVICE:
129         case ELMTYP_ARRAY_DEV:
130                 ses_ctrl_common_set_select(&slot->common, 1);
131                 if (setfault)
132                         ses_ctrl_dev_slot_set_rqst_fault(slot, state);
133                 else
134                         ses_ctrl_dev_slot_set_rqst_ident(slot, state);
135                 break;
136         default:
137                 return;
138         }
139         if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
140                 close(fd);
141                 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
142         }
143 }
144
145 static bool
146 disk_match(const char *devnames, const char *disk, size_t len)
147 {
148         const char *dname;
149
150         dname = devnames;
151         while ((dname = strstr(dname, disk)) != NULL) {
152                 if (dname[len] == '\0' || dname[len] == ',') {
153                         return (true);
154                 }
155                 dname++;
156         }
157
158         return (false);
159 }
160
161 static int
162 sesled(int argc, char **argv, bool setfault)
163 {
164         encioc_elm_devnames_t objdn;
165         encioc_element_t *objp;
166         glob_t g;
167         char *disk, *endptr;
168         size_t len, i, ndisks;
169         int fd;
170         unsigned int nobj, j, sesid;
171         bool all, isses, onoff;
172
173         isses = false;
174         all = false;
175         onoff = false;
176
177         if (argc != 3) {
178                 usage(stderr, (setfault ? "fault" : "locate"));
179         }
180
181         disk = argv[1];
182
183         sesid = strtoul(disk, &endptr, 10);
184         if (*endptr == '\0') {
185                 endptr = strrchr(uflag, '*');
186                 if (endptr != NULL && *endptr == '*') {
187                         xo_warnx("Must specifying a SES device (-u) to use a SES "
188                             "id# to identify a disk");
189                         usage(stderr, (setfault ? "fault" : "locate"));
190                 }
191                 isses = true;
192         }
193
194         if (strcmp(argv[2], "on") == 0) {
195                 onoff = true;
196         } else if (strcmp(argv[2], "off") == 0) {
197                 onoff = false;
198         } else {
199                 usage(stderr, (setfault ? "fault" : "locate"));
200         }
201
202         if (strcmp(disk, "all") == 0) {
203                 all = true;
204         }
205         len = strlen(disk);
206
207         /* Get the list of ses devices */
208         if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
209             GLOB_NOMATCH) {
210                 globfree(&g);
211                 xo_errx(EXIT_FAILURE, "No SES devices found");
212         }
213
214         ndisks = 0;
215         for (i = 0; i < g.gl_pathc; i++) {
216                 /* ensure we only got numbers after ses */
217                 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
218                     strlen(g.gl_pathv[i] + 8)) {
219                         continue;
220                 }
221                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
222                         /*
223                          * Don't treat non-access errors as critical if we are
224                          * accessing all devices
225                          */
226                         if (errno == EACCES && g.gl_pathc > 1) {
227                                 xo_err(EXIT_FAILURE, "unable to access SES device");
228                         }
229                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
230                         continue;
231                 }
232
233                 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
234                         close(fd);
235                         xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
236                 }
237
238                 objp = calloc(nobj, sizeof(encioc_element_t));
239                 if (objp == NULL) {
240                         close(fd);
241                         xo_err(EXIT_FAILURE, "calloc()");
242                 }
243
244                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
245                         close(fd);
246                         xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
247                 }
248
249                 if (isses) {
250                         if (sesid > nobj) {
251                                 close(fd);
252                                 xo_errx(EXIT_FAILURE,
253                                      "Requested SES ID does not exist");
254                         }
255                         do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
256                         ndisks++;
257                         close(fd);
258                         break;
259                 }
260                 for (j = 0; j < nobj; j++) {
261                         if (all) {
262                                 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
263                                     onoff, setfault);
264                                 continue;
265                         }
266                         memset(&objdn, 0, sizeof(objdn));
267                         objdn.elm_idx = objp[j].elm_idx;
268                         objdn.elm_names_size = 128;
269                         objdn.elm_devnames = calloc(128, sizeof(char));
270                         if (objdn.elm_devnames == NULL) {
271                                 close(fd);
272                                 xo_err(EXIT_FAILURE, "calloc()");
273                         }
274                         if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
275                             (caddr_t) &objdn) <0) {
276                                 continue;
277                         }
278                         if (objdn.elm_names_len > 0) {
279                                 if (disk_match(objdn.elm_devnames, disk, len)) {
280                                         do_led(fd, objdn.elm_idx, objp[j].elm_type,
281                                             onoff, setfault);
282                                         ndisks++;
283                                         break;
284                                 }
285                         }
286                 }
287                 free(objp);
288                 close(fd);
289         }
290         globfree(&g);
291         if (ndisks == 0 && all == false) {
292                 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
293                     disk);
294         }
295
296         return (EXIT_SUCCESS);
297 }
298
299 static int
300 locate(int argc, char **argv)
301 {
302
303         return (sesled(argc, argv, false));
304 }
305
306 static int
307 fault(int argc, char **argv)
308 {
309
310         return (sesled(argc, argv, true));
311 }
312
313 #define TEMPERATURE_OFFSET 20
314 static void
315 sesutil_print(bool *title, const char *fmt, ...)
316 {
317         va_list args;
318
319         if (!*title) {
320                 xo_open_container("extra_status");
321                 xo_emit("\t\tExtra status:\n");
322                 *title = true;
323         }
324         va_start(args, fmt);
325         xo_emit_hv(NULL, fmt, args);
326         va_end(args);
327 }
328
329 static void
330 print_extra_status(int eletype, u_char *cstat)
331 {
332         bool title = false;
333
334         if (cstat[0] & 0x40) {
335                 sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n");
336         }
337         if (cstat[0] & 0x20) {
338                 sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n");
339         }
340         if (cstat[0] & 0x10) {
341                 sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n");
342         }
343         switch (eletype) {
344         case ELMTYP_DEVICE:
345         case ELMTYP_ARRAY_DEV:
346                 if (cstat[2] & 0x02) {
347                         sesutil_print(&title, "\t\t- LED={q:led/locate}\n");
348                 }
349                 if (cstat[2] & 0x20) {
350                         sesutil_print(&title, "\t\t- LED={q:led/fault}\n");
351                 }
352                 break;
353         case ELMTYP_FAN:
354                 sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n",
355                     (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
356                 break;
357         case ELMTYP_THERM:
358                 if (cstat[2]) {
359                         sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n",
360                             cstat[2] - TEMPERATURE_OFFSET);
361                 } else {
362                         sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n");
363                 }
364                 break;
365         case ELMTYP_VOM:
366                 sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n",
367                     be16dec(cstat + 2) / 100.0);
368                 break;
369         }
370         if (title) {
371                 xo_close_container("extra_status");
372         }
373 }
374
375 static int
376 objmap(int argc, char **argv __unused)
377 {
378         encioc_string_t stri;
379         encioc_elm_devnames_t e_devname;
380         encioc_elm_status_t e_status;
381         encioc_elm_desc_t e_desc;
382         encioc_element_t *e_ptr;
383         glob_t g;
384         int fd;
385         unsigned int j, nobj;
386         size_t i;
387         char str[32];
388
389         if (argc != 1) {
390                 usage(stderr, "map");
391         }
392
393         /* Get the list of ses devices */
394         if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
395                 globfree(&g);
396                 xo_errx(EXIT_FAILURE, "No SES devices found");
397         }
398         xo_set_version(SESUTIL_XO_VERSION);
399         xo_open_container("sesutil");
400         xo_open_list("enclosures");
401         for (i = 0; i < g.gl_pathc; i++) {
402                 /* ensure we only got numbers after ses */
403                 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
404                     strlen(g.gl_pathv[i] + 8)) {
405                         continue;
406                 }
407                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
408                         /*
409                          * Don't treat non-access errors as critical if we are
410                          * accessing all devices
411                          */
412                         if (errno == EACCES && g.gl_pathc > 1) {
413                                 xo_err(EXIT_FAILURE, "unable to access SES device");
414                         }
415                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
416                         continue;
417                 }
418
419                 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
420                         close(fd);
421                         xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
422                 }
423
424                 e_ptr = calloc(nobj, sizeof(encioc_element_t));
425                 if (e_ptr == NULL) {
426                         close(fd);
427                         xo_err(EXIT_FAILURE, "calloc()");
428                 }
429
430                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
431                         close(fd);
432                         xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
433                 }
434
435                 xo_open_instance("enclosures");
436                 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
437                 stri.bufsiz = sizeof(str);
438                 stri.buf = &str[0];
439                 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
440                         xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
441                 stri.bufsiz = sizeof(str);
442                 stri.buf = &str[0];
443                 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
444                         xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
445
446                 xo_open_list("elements");
447                 for (j = 0; j < nobj; j++) {
448                         /* Get the status of the element */
449                         memset(&e_status, 0, sizeof(e_status));
450                         e_status.elm_idx = e_ptr[j].elm_idx;
451                         if (ioctl(fd, ENCIOC_GETELMSTAT,
452                             (caddr_t) &e_status) < 0) {
453                                 close(fd);
454                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
455                         }
456                         /* Get the description of the element */
457                         memset(&e_desc, 0, sizeof(e_desc));
458                         e_desc.elm_idx = e_ptr[j].elm_idx;
459                         e_desc.elm_desc_len = UINT16_MAX;
460                         e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
461                         if (e_desc.elm_desc_str == NULL) {
462                                 close(fd);
463                                 xo_err(EXIT_FAILURE, "calloc()");
464                         }
465                         if (ioctl(fd, ENCIOC_GETELMDESC,
466                             (caddr_t) &e_desc) < 0) {
467                                 close(fd);
468                                 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
469                         }
470                         /* Get the device name(s) of the element */
471                         memset(&e_devname, 0, sizeof(e_devname));
472                         e_devname.elm_idx = e_ptr[j].elm_idx;
473                         e_devname.elm_names_size = 128;
474                         e_devname.elm_devnames = calloc(128, sizeof(char));
475                         if (e_devname.elm_devnames == NULL) {
476                                 close(fd);
477                                 xo_err(EXIT_FAILURE, "calloc()");
478                         }
479                         if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
480                             (caddr_t) &e_devname) <0) {
481                                 /* We don't care if this fails */
482                                 e_devname.elm_devnames[0] = '\0';
483                         }
484                         xo_open_instance("elements");
485                         xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
486                             geteltnm(e_ptr[j].elm_type));
487                         xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
488                             scode2ascii(e_status.cstat[0]), e_status.cstat[0],
489                             e_status.cstat[1], e_status.cstat[2],
490                             e_status.cstat[3]);
491                         if (e_desc.elm_desc_len > 0) {
492                                 xo_emit("\t\tDescription: {:description/%s}\n",
493                                     e_desc.elm_desc_str);
494                         }
495                         if (e_devname.elm_names_len > 0) {
496                                 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
497                                     e_devname.elm_devnames);
498                         }
499                         print_extra_status(e_ptr[j].elm_type, e_status.cstat);
500                         xo_close_instance("elements");
501                         free(e_devname.elm_devnames);
502                 }
503                 xo_close_list("elements");
504                 free(e_ptr);
505                 close(fd);
506         }
507         globfree(&g);
508         xo_close_list("enclosures");
509         xo_close_container("sesutil");
510         xo_finish();
511
512         return (EXIT_SUCCESS);
513 }
514
515 static int
516 encstatus(int argc, char **argv __unused)
517 {
518         glob_t g;
519         int fd, status;
520         size_t i, e;
521         u_char estat;
522
523         status = 0;
524         if (argc != 1) {
525                 usage(stderr, "status");
526         }
527
528         /* Get the list of ses devices */
529         if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
530                 globfree(&g);
531                 xo_errx(EXIT_FAILURE, "No SES devices found");
532         }
533
534         xo_set_version(SESUTIL_XO_VERSION);
535         xo_open_container("sesutil");
536         xo_open_list("enclosures");
537         for (i = 0; i < g.gl_pathc; i++) {
538                 /* ensure we only got numbers after ses */
539                 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
540                     strlen(g.gl_pathv[i] + 8)) {
541                         continue;
542                 }
543                 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
544                         /*
545                          * Don't treat non-access errors as critical if we are
546                          * accessing all devices
547                          */
548                         if (errno == EACCES && g.gl_pathc > 1) {
549                                 xo_err(EXIT_FAILURE, "unable to access SES device");
550                         }
551                         xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
552                         continue;
553                 }
554
555                 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
556                         xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
557                         close(fd);
558                 }
559
560                 xo_open_instance("enclosures");
561                 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
562                 e = 0;
563                 if (estat == 0) {
564                         if (status == 0) {
565                                 status = 1;
566                         }
567                         xo_emit("{q:status/OK}");
568                 } else {
569                         if (estat & SES_ENCSTAT_INFO) {
570                                 xo_emit("{lq:status/INFO}");
571                                 e++;
572                         }
573                         if (estat & SES_ENCSTAT_NONCRITICAL) {
574                                 if (e)
575                                         xo_emit(",");
576                                 xo_emit("{lq:status/NONCRITICAL}");
577                                 e++;
578                         }
579                         if (estat & SES_ENCSTAT_CRITICAL) {
580                                 if (e)
581                                         xo_emit(",");
582                                 xo_emit("{lq:status/CRITICAL}");
583                                 e++;
584                                 status = -1;
585                         }
586                         if (estat & SES_ENCSTAT_UNRECOV) {
587                                 if (e)
588                                         xo_emit(",");
589                                 xo_emit("{lq:status/UNRECOV}");
590                                 e++;
591                                 status = -1;
592                         }
593                 }
594                 xo_close_instance("enclosures");
595                 xo_emit("\n");
596                 close(fd);
597         }
598         globfree(&g);
599
600         xo_close_list("enclosures");
601         xo_close_container("sesutil");
602         xo_finish();
603
604         if (status == 1) {
605                 return (EXIT_SUCCESS);
606         } else {
607                 return (EXIT_FAILURE);
608         }
609 }
610
611 int
612 main(int argc, char **argv)
613 {
614         int i, ch;
615         struct command *cmd = NULL;
616
617         argc = xo_parse_args(argc, argv);
618         if (argc < 0)
619                 exit(1);
620
621         uflag = "/dev/ses[0-9]*";
622         while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
623                 switch (ch) {
624                 case 'u':
625                         uflag = optarg;
626                         break;
627                 case '?':
628                 default:
629                         usage(stderr, NULL);
630                 }
631         }
632         argc -= optind;
633         argv += optind;
634
635         if (argc < 1) {
636                 warnx("Missing command");
637                 usage(stderr, NULL);
638         }
639
640         for (i = 0; i < nbcmds; i++) {
641                 if (strcmp(argv[0], cmds[i].name) == 0) {
642                         cmd = &cmds[i];
643                         break;
644                 }
645         }
646
647         if (cmd == NULL) {
648                 warnx("unknown command %s", argv[0]);
649                 usage(stderr, NULL);
650         }
651
652         return (cmd->exec(argc, argv));
653 }