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