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