1 /* $FreeBSD$ */
2 /*
3 * Copyright (c) 2000 by Matthew Jacob
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * Alternatively, this software may be distributed under the terms of the
16 * the GNU Public License ("GPL").
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * Matthew Jacob
31 * Feral Software
32 * [email protected]
33 */
34
35 #include <unistd.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/ioctl.h>
42 #include <fcntl.h>
43 #include <cam/scsi/scsi_all.h>
44 #include <cam/scsi/scsi_enc.h>
45
46 #include "eltsub.h"
47
48 int
main(int a,char ** v)49 main(int a, char **v)
50 {
51 encioc_string_t stri;
52 encioc_element_t *objp;
53 encioc_elm_status_t ob;
54 encioc_elm_desc_t objd;
55 encioc_elm_devnames_t objdn;
56 int fd, nobj, f, i, verbose, quiet, errors;
57 u_char estat;
58 char str[32];
59
60 if (a < 2) {
61 fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);
62 return (1);
63 }
64 errors = quiet = verbose = 0;
65 if (strcmp(v[1], "-V") == 0) {
66 verbose = 2;
67 v++;
68 } else if (strcmp(v[1], "-v") == 0) {
69 verbose = 1;
70 v++;
71 } else if (strcmp(v[1], "-q") == 0) {
72 quiet = 1;
73 verbose = 0;
74 v++;
75 }
76 while (*++v) {
77
78 fd = open(*v, O_RDONLY);
79 if (fd < 0) {
80 perror(*v);
81 continue;
82 }
83 if (verbose > 1) {
84 stri.bufsiz = sizeof(str);
85 stri.buf = &str[0];
86 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
87 printf("%s: Enclosure Name: %s\n", *v, stri.buf);
88 stri.bufsiz = sizeof(str);
89 stri.buf = &str[0];
90 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
91 printf("%s: Enclosure ID: %s\n", *v, stri.buf);
92 }
93 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
94 perror("ENCIOC_GETNELM");
95 (void) close(fd);
96 continue;
97 }
98 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
99 perror("ENCIOC_GETENCSTAT");
100 (void) close(fd);
101 continue;
102 }
103 if ((verbose == 0 || quiet == 1) && estat == 0) {
104 if (quiet == 0)
105 fprintf(stdout, "%s: Enclosure OK\n", *v);
106 (void) close(fd);
107 continue;
108 }
109 fprintf(stdout, "%s: Enclosure Status ", *v);
110 if (estat == 0) {
111 fprintf(stdout, "<OK");
112 } else {
113 errors++;
114 f = '<';
115 if (estat & SES_ENCSTAT_INFO) {
116 fprintf(stdout, "%cINFO", f);
117 f = ',';
118 }
119 if (estat & SES_ENCSTAT_NONCRITICAL) {
120 fprintf(stdout, "%cNONCRITICAL", f);
121 f = ',';
122 }
123 if (estat & SES_ENCSTAT_CRITICAL) {
124 fprintf(stdout, "%cCRITICAL", f);
125 f = ',';
126 }
127 if (estat & SES_ENCSTAT_UNRECOV) {
128 fprintf(stdout, "%cUNRECOV", f);
129 f = ',';
130 }
131 }
132 fprintf(stdout, ">\n");
133 objp = calloc(nobj, sizeof (encioc_element_t));
134 if (objp == NULL) {
135 perror("calloc");
136 (void) close(fd);
137 continue;
138 }
139 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
140 perror("ENCIOC_GETELMMAP");
141 (void) close(fd);
142 continue;
143 }
144 for (i = 0; i < nobj; i++) {
145 ob.elm_idx = objp[i].elm_idx;
146 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &ob) < 0) {
147 perror("ENCIOC_GETELMSTAT");
148 (void) close(fd);
149 break;
150 }
151 bzero(&objd, sizeof(objd));
152 objd.elm_idx = objp[i].elm_idx;
153 objd.elm_desc_len = UINT16_MAX;
154 objd.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
155 if (objd.elm_desc_str == NULL) {
156 perror("calloc");
157 (void) close(fd);
158 continue;
159 }
160 if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t)&objd) < 0) {
161 perror("ENCIOC_GETELMDESC");
162 (void) close(fd);
163 break;
164 }
165 bzero(&objdn, sizeof(objdn));
166 objdn.elm_idx = objp[i].elm_idx;
167 objdn.elm_names_size = 128;
168 objdn.elm_devnames = calloc(128, sizeof(char));
169 if (objdn.elm_devnames == NULL) {
170 perror("calloc");
171 (void) close(fd);
172 break;
173 }
174 /*
175 * This ioctl isn't critical and has a good chance
176 * of returning -1.
177 */
178 (void)ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t)&objdn);
179 fprintf(stdout, "Element 0x%x: %s", ob.elm_idx,
180 geteltnm(objp[i].elm_type));
181 fprintf(stdout, ", %s",
182 stat2ascii(objp[i].elm_type, ob.cstat));
183 if (objd.elm_desc_len > 0)
184 fprintf(stdout, ", descriptor: '%s'",
185 objd.elm_desc_str);
186 if (objdn.elm_names_len > 0)
187 fprintf(stdout, ", dev: '%s'",
188 objdn.elm_devnames);
189 fprintf(stdout, "\n");
190 free(objdn.elm_devnames);
191 }
192 free(objp);
193 (void) close(fd);
194 }
195 return (errors);
196 }
197